scep.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. // IncludeRoot makes the provisioner return the CA root in addition to the
  18. // intermediate in the GetCACerts response
  19. IncludeRoot bool `json:"includeRoot,omitempty"`
  20. // MinimumPublicKeyLength is the minimum length for public keys in CSRs
  21. MinimumPublicKeyLength int `json:"minimumPublicKeyLength,omitempty"`
  22. // Numerical identifier for the ContentEncryptionAlgorithm as defined in github.com/mozilla-services/pkcs7
  23. // at https://github.com/mozilla-services/pkcs7/blob/33d05740a3526e382af6395d3513e73d4e66d1cb/encrypt.go#L63
  24. // Defaults to 0, being DES-CBC
  25. EncryptionAlgorithmIdentifier int `json:"encryptionAlgorithmIdentifier,omitempty"`
  26. Options *Options `json:"options,omitempty"`
  27. Claims *Claims `json:"claims,omitempty"`
  28. secretChallengePassword string
  29. encryptionAlgorithm int
  30. ctl *Controller
  31. }
  32. // GetID returns the provisioner unique identifier.
  33. func (s *SCEP) GetID() string {
  34. if s.ID != "" {
  35. return s.ID
  36. }
  37. return s.GetIDForToken()
  38. }
  39. // GetIDForToken returns an identifier that will be used to load the provisioner
  40. // from a token.
  41. func (s *SCEP) GetIDForToken() string {
  42. return "scep/" + s.Name
  43. }
  44. // GetName returns the name of the provisioner.
  45. func (s *SCEP) GetName() string {
  46. return s.Name
  47. }
  48. // GetType returns the type of provisioner.
  49. func (s *SCEP) GetType() Type {
  50. return TypeSCEP
  51. }
  52. // GetEncryptedKey returns the base provisioner encrypted key if it's defined.
  53. func (s *SCEP) GetEncryptedKey() (string, string, bool) {
  54. return "", "", false
  55. }
  56. // GetTokenID returns the identifier of the token.
  57. func (s *SCEP) GetTokenID(ott string) (string, error) {
  58. return "", errors.New("scep provisioner does not implement GetTokenID")
  59. }
  60. // GetOptions returns the configured provisioner options.
  61. func (s *SCEP) GetOptions() *Options {
  62. return s.Options
  63. }
  64. // DefaultTLSCertDuration returns the default TLS cert duration enforced by
  65. // the provisioner.
  66. func (s *SCEP) DefaultTLSCertDuration() time.Duration {
  67. return s.ctl.Claimer.DefaultTLSCertDuration()
  68. }
  69. // Init initializes and validates the fields of a SCEP type.
  70. func (s *SCEP) Init(config Config) (err error) {
  71. switch {
  72. case s.Type == "":
  73. return errors.New("provisioner type cannot be empty")
  74. case s.Name == "":
  75. return errors.New("provisioner name cannot be empty")
  76. }
  77. // Mask the actual challenge value, so it won't be marshaled
  78. s.secretChallengePassword = s.ChallengePassword
  79. s.ChallengePassword = "*** redacted ***"
  80. // Default to 2048 bits minimum public key length (for CSRs) if not set
  81. if s.MinimumPublicKeyLength == 0 {
  82. s.MinimumPublicKeyLength = 2048
  83. }
  84. if s.MinimumPublicKeyLength%8 != 0 {
  85. return errors.Errorf("%d bits is not exactly divisible by 8", s.MinimumPublicKeyLength)
  86. }
  87. s.encryptionAlgorithm = s.EncryptionAlgorithmIdentifier // TODO(hs): we might want to upgrade the default security to AES-CBC?
  88. if s.encryptionAlgorithm < 0 || s.encryptionAlgorithm > 4 {
  89. return errors.New("only encryption algorithm identifiers from 0 to 4 are valid")
  90. }
  91. // TODO: add other, SCEP specific, options?
  92. s.ctl, err = NewController(s, s.Claims, config)
  93. return
  94. }
  95. // AuthorizeSign does not do any verification, because all verification is handled
  96. // in the SCEP protocol. This method returns a list of modifiers / constraints
  97. // on the resulting certificate.
  98. func (s *SCEP) AuthorizeSign(ctx context.Context, token string) ([]SignOption, error) {
  99. return []SignOption{
  100. // modifiers / withOptions
  101. newProvisionerExtensionOption(TypeSCEP, s.Name, ""),
  102. newForceCNOption(s.ForceCN),
  103. profileDefaultDuration(s.ctl.Claimer.DefaultTLSCertDuration()),
  104. // validators
  105. newPublicKeyMinimumLengthValidator(s.MinimumPublicKeyLength),
  106. newValidityValidator(s.ctl.Claimer.MinTLSCertDuration(), s.ctl.Claimer.MaxTLSCertDuration()),
  107. }, nil
  108. }
  109. // GetChallengePassword returns the challenge password
  110. func (s *SCEP) GetChallengePassword() string {
  111. return s.secretChallengePassword
  112. }
  113. // GetCapabilities returns the CA capabilities
  114. func (s *SCEP) GetCapabilities() []string {
  115. return s.Capabilities
  116. }
  117. // ShouldIncludeRootInChain indicates if the CA should
  118. // return its intermediate, which is currently used for
  119. // both signing and decryption, as well as the root in
  120. // its chain.
  121. func (s *SCEP) ShouldIncludeRootInChain() bool {
  122. return s.IncludeRoot
  123. }
  124. // GetContentEncryptionAlgorithm returns the numeric identifier
  125. // for the pkcs7 package encryption algorithm to use.
  126. func (s *SCEP) GetContentEncryptionAlgorithm() int {
  127. return s.encryptionAlgorithm
  128. }