options.go 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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.Signer == nil:
  37. return errors.New("no signer available for SCEP authority")
  38. case o.SignerCert == nil:
  39. return errors.New("no signer certificate available for SCEP authority")
  40. }
  41. // check if the signer (intermediate CA) certificate has the same public key as
  42. // the signer. According to the RFC it seems valid to have different keys for
  43. // the intermediate and the CA signing new certificates, so this might change
  44. // in the future.
  45. signerPublicKey := o.Signer.Public().(comparablePublicKey)
  46. if !signerPublicKey.Equal(o.SignerCert.PublicKey) {
  47. return errors.New("mismatch between signer certificate and public key")
  48. }
  49. // decrypter can be nil in case a signing only key is used; validation complete.
  50. if o.Decrypter == nil {
  51. return nil
  52. }
  53. // If a decrypter is available, check that it's backed by an RSA key. According to the
  54. // RFC: https://tools.ietf.org/html/rfc8894#section-3.1, SCEP can be used with something
  55. // different than RSA, but requires the encryption to be performed using the challenge
  56. // password in that case. An older version of specification states that only RSA is
  57. // supported: https://tools.ietf.org/html/draft-nourse-scep-23#section-2.1.1. Other
  58. // algorithms do not seem to be supported in certnanny/sscep, but it might work
  59. // in micromdm/scep. Currently only RSA is allowed, but it might be an option
  60. // to try other algorithms in the future.
  61. decrypterPublicKey, ok := o.Decrypter.Public().(*rsa.PublicKey)
  62. if !ok {
  63. return errors.New("only RSA keys are (currently) supported as decrypters")
  64. }
  65. // check if intermediate public key is the same as the decrypter public key.
  66. // In certnanny/sscep it's mentioned that the signing key can be different
  67. // from the decrypting (and encrypting) key. These options are only used and
  68. // validated when the intermediate CA is also used as the decrypter, though,
  69. // so they should match.
  70. if !decrypterPublicKey.Equal(o.SignerCert.PublicKey) {
  71. return errors.New("mismatch between certificate chain and decrypter public keys")
  72. }
  73. return nil
  74. }