scep.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package provisioner
  2. import (
  3. "context"
  4. "time"
  5. "github.com/pkg/errors"
  6. )
  7. // SCEP is the SCEP provisioner type, an entity that can authorize the
  8. // SCEP provisioning flow
  9. type SCEP struct {
  10. *base
  11. ID string `json:"-"`
  12. Type string `json:"type"`
  13. Name string `json:"name"`
  14. ForceCN bool `json:"forceCN,omitempty"`
  15. ChallengePassword string `json:"challenge,omitempty"`
  16. Capabilities []string `json:"capabilities,omitempty"`
  17. // MinimumPublicKeyLength is the minimum length for public keys in CSRs
  18. MinimumPublicKeyLength int `json:"minimumPublicKeyLength,omitempty"`
  19. Options *Options `json:"options,omitempty"`
  20. Claims *Claims `json:"claims,omitempty"`
  21. claimer *Claimer
  22. secretChallengePassword string
  23. }
  24. // GetID returns the provisioner unique identifier.
  25. func (s *SCEP) GetID() string {
  26. if s.ID != "" {
  27. return s.ID
  28. }
  29. return s.GetIDForToken()
  30. }
  31. // GetIDForToken returns an identifier that will be used to load the provisioner
  32. // from a token.
  33. func (s *SCEP) GetIDForToken() string {
  34. return "scep/" + s.Name
  35. }
  36. // GetName returns the name of the provisioner.
  37. func (s *SCEP) GetName() string {
  38. return s.Name
  39. }
  40. // GetType returns the type of provisioner.
  41. func (s *SCEP) GetType() Type {
  42. return TypeSCEP
  43. }
  44. // GetEncryptedKey returns the base provisioner encrypted key if it's defined.
  45. func (s *SCEP) GetEncryptedKey() (string, string, bool) {
  46. return "", "", false
  47. }
  48. // GetTokenID returns the identifier of the token.
  49. func (s *SCEP) GetTokenID(ott string) (string, error) {
  50. return "", errors.New("scep provisioner does not implement GetTokenID")
  51. }
  52. // GetOptions returns the configured provisioner options.
  53. func (s *SCEP) GetOptions() *Options {
  54. return s.Options
  55. }
  56. // DefaultTLSCertDuration returns the default TLS cert duration enforced by
  57. // the provisioner.
  58. func (s *SCEP) DefaultTLSCertDuration() time.Duration {
  59. return s.claimer.DefaultTLSCertDuration()
  60. }
  61. // Init initializes and validates the fields of a SCEP type.
  62. func (s *SCEP) Init(config Config) (err error) {
  63. switch {
  64. case s.Type == "":
  65. return errors.New("provisioner type cannot be empty")
  66. case s.Name == "":
  67. return errors.New("provisioner name cannot be empty")
  68. }
  69. // Update claims with global ones
  70. if s.claimer, err = NewClaimer(s.Claims, config.Claims); err != nil {
  71. return err
  72. }
  73. // Mask the actual challenge value, so it won't be marshaled
  74. s.secretChallengePassword = s.ChallengePassword
  75. s.ChallengePassword = "*** redacted ***"
  76. // Default to 2048 bits minimum public key length (for CSRs) if not set
  77. if s.MinimumPublicKeyLength == 0 {
  78. s.MinimumPublicKeyLength = 2048
  79. }
  80. if s.MinimumPublicKeyLength%8 != 0 {
  81. return errors.Errorf("only minimum public keys exactly divisible by 8 are supported; %d is not exactly divisible by 8", s.MinimumPublicKeyLength)
  82. }
  83. // TODO: add other, SCEP specific, options?
  84. return err
  85. }
  86. // AuthorizeSign does not do any verification, because all verification is handled
  87. // in the SCEP protocol. This method returns a list of modifiers / constraints
  88. // on the resulting certificate.
  89. func (s *SCEP) AuthorizeSign(ctx context.Context, token string) ([]SignOption, error) {
  90. return []SignOption{
  91. // modifiers / withOptions
  92. newProvisionerExtensionOption(TypeSCEP, s.Name, ""),
  93. newForceCNOption(s.ForceCN),
  94. profileDefaultDuration(s.claimer.DefaultTLSCertDuration()),
  95. // validators
  96. newPublicKeyMinimumLengthValidator(s.MinimumPublicKeyLength),
  97. newValidityValidator(s.claimer.MinTLSCertDuration(), s.claimer.MaxTLSCertDuration()),
  98. }, nil
  99. }
  100. // GetChallengePassword returns the challenge password
  101. func (s *SCEP) GetChallengePassword() string {
  102. return s.secretChallengePassword
  103. }
  104. // GetCapabilities returns the CA capabilities
  105. func (s *SCEP) GetCapabilities() []string {
  106. return s.Capabilities
  107. }