scep.go 5.1 KB

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