options.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package scep
  2. import (
  3. "crypto"
  4. "crypto/rsa"
  5. "crypto/x509"
  6. "github.com/pkg/errors"
  7. )
  8. type Options struct {
  9. // CertificateChain is the issuer certificate, along with any other bundled certificates
  10. // to be returned in the chain for consumers. Configured in the ca.json crt property.
  11. CertificateChain []*x509.Certificate
  12. // Signer signs CSRs in SCEP. Configured in the ca.json key property.
  13. Signer crypto.Signer `json:"-"`
  14. // Decrypter decrypts encrypted SCEP messages. Configured in the ca.json key property.
  15. Decrypter crypto.Decrypter `json:"-"`
  16. }
  17. // Validate checks the fields in Options.
  18. func (o *Options) Validate() error {
  19. if o.CertificateChain == nil {
  20. return errors.New("certificate chain not configured correctly")
  21. }
  22. if len(o.CertificateChain) < 1 {
  23. return errors.New("certificate chain should at least have one certificate")
  24. }
  25. // According to the RFC: https://tools.ietf.org/html/rfc8894#section-3.1, SCEP
  26. // can be used with something different than RSA, but requires the encryption
  27. // to be performed using the challenge password. An older version of specification
  28. // states that only RSA is supported: https://tools.ietf.org/html/draft-nourse-scep-23#section-2.1.1
  29. // Other algorithms than RSA do not seem to be supported in certnanny/sscep, but it might work
  30. // in micromdm/scep. Currently only RSA is allowed, but it might be an option
  31. // to try other algorithms in the future.
  32. intermediate := o.CertificateChain[0]
  33. if intermediate.PublicKeyAlgorithm != x509.RSA {
  34. return errors.New("only the RSA algorithm is (currently) supported")
  35. }
  36. // TODO: add checks for key usage?
  37. signerPublicKey, ok := o.Signer.Public().(*rsa.PublicKey)
  38. if !ok {
  39. return errors.New("only RSA public keys are (currently) supported as signers")
  40. }
  41. // check if the intermediate ca certificate has the same public key as the signer.
  42. // According to the RFC it seems valid to have different keys for the intermediate
  43. // and the CA signing new certificates, so this might change in the future.
  44. if !signerPublicKey.Equal(intermediate.PublicKey) {
  45. return errors.New("mismatch between certificate chain and signer public keys")
  46. }
  47. decrypterPublicKey, ok := o.Decrypter.Public().(*rsa.PublicKey)
  48. if !ok {
  49. return errors.New("only RSA public keys are (currently) supported as decrypters")
  50. }
  51. // check if intermediate public key is the same as the decrypter public key.
  52. // In certnanny/sscep it's mentioned that the signing key can be different
  53. // from the decrypting (and encrypting) key. Currently that's not supported.
  54. if !decrypterPublicKey.Equal(intermediate.PublicKey) {
  55. return errors.New("mismatch between certificate chain and decrypter public keys")
  56. }
  57. return nil
  58. }