4
0

options.go 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package scep
  2. import (
  3. "crypto"
  4. "crypto/rsa"
  5. "crypto/x509"
  6. "errors"
  7. )
  8. type Options struct {
  9. // Roots contains the (federated) CA roots certificate(s)
  10. Roots []*x509.Certificate `json:"-"`
  11. // Intermediates points issuer certificate, along with any other bundled certificates
  12. // to be returned in the chain for consumers.
  13. Intermediates []*x509.Certificate `json:"-"`
  14. // SignerCert points to the certificate of the CA signer. It usually is the same as the
  15. // first certificate in the CertificateChain.
  16. SignerCert *x509.Certificate `json:"-"`
  17. // Signer signs CSRs in SCEP. Configured in the ca.json key property.
  18. Signer crypto.Signer `json:"-"`
  19. // Decrypter decrypts encrypted SCEP messages. Configured in the ca.json key property.
  20. Decrypter crypto.Decrypter `json:"-"`
  21. // DecrypterCert points to the certificate of the CA decrypter.
  22. DecrypterCert *x509.Certificate `json:"-"`
  23. // SCEPProvisionerNames contains the currently configured SCEP provioner names. These
  24. // are used to be able to load the provisioners when the SCEP authority is being
  25. // validated.
  26. SCEPProvisionerNames []string
  27. }
  28. type comparablePublicKey interface {
  29. Equal(crypto.PublicKey) bool
  30. }
  31. // Validate checks the fields in Options.
  32. func (o *Options) Validate() error {
  33. switch {
  34. case len(o.Intermediates) == 0:
  35. return errors.New("no intermediate certificate available for SCEP authority")
  36. case o.SignerCert == nil:
  37. return errors.New("no signer certificate available for SCEP authority")
  38. }
  39. // the signer is optional, but if it's set, its public key must match the signer
  40. // certificate public key.
  41. if o.Signer != nil {
  42. // check if the signer (intermediate CA) certificate has the same public key as
  43. // the signer. According to the RFC it seems valid to have different keys for
  44. // the intermediate and the CA signing new certificates, so this might change
  45. // in the future.
  46. signerPublicKey := o.Signer.Public().(comparablePublicKey)
  47. if !signerPublicKey.Equal(o.SignerCert.PublicKey) {
  48. return errors.New("mismatch between signer certificate and public key")
  49. }
  50. }
  51. // decrypter can be nil in case a signing only key is used; validation complete.
  52. if o.Decrypter == nil {
  53. return nil
  54. }
  55. // If a decrypter is available, check that it's backed by an RSA key. According to the
  56. // RFC: https://tools.ietf.org/html/rfc8894#section-3.1, SCEP can be used with something
  57. // different than RSA, but requires the encryption to be performed using the challenge
  58. // password in that case. An older version of specification states that only RSA is
  59. // supported: https://tools.ietf.org/html/draft-nourse-scep-23#section-2.1.1. Other
  60. // algorithms do not seem to be supported in certnanny/sscep, but it might work
  61. // in micromdm/scep. Currently only RSA is allowed, but it might be an option
  62. // to try other algorithms in the future.
  63. decrypterPublicKey, ok := o.Decrypter.Public().(*rsa.PublicKey)
  64. if !ok {
  65. return errors.New("only RSA keys are (currently) supported as decrypters")
  66. }
  67. // check if intermediate public key is the same as the decrypter public key.
  68. // In certnanny/sscep it's mentioned that the signing key can be different
  69. // from the decrypting (and encrypting) key. These options are only used and
  70. // validated when the intermediate CA is also used as the decrypter, though,
  71. // so they should match.
  72. if !decrypterPublicKey.Equal(o.SignerCert.PublicKey) {
  73. return errors.New("mismatch between certificate chain and decrypter public keys")
  74. }
  75. return nil
  76. }