File: options.go

package info (click to toggle)
golang-github-smallstep-certificates 0.29.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,720 kB
  • sloc: sh: 385; makefile: 129
file content (85 lines) | stat: -rw-r--r-- 3,491 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package scep

import (
	"crypto"
	"crypto/rsa"
	"crypto/x509"
	"errors"
)

type Options struct {
	// Roots contains the (federated) CA roots certificate(s)
	Roots []*x509.Certificate `json:"-"`
	// Intermediates points issuer certificate, along with any other bundled certificates
	// to be returned in the chain for consumers.
	Intermediates []*x509.Certificate `json:"-"`
	// SignerCert points to the certificate of the CA signer. It usually is the same as the
	// first certificate in the CertificateChain.
	SignerCert *x509.Certificate `json:"-"`
	// Signer signs CSRs in SCEP. Configured in the ca.json key property.
	Signer crypto.Signer `json:"-"`
	// Decrypter decrypts encrypted SCEP messages. Configured in the ca.json key property.
	Decrypter crypto.Decrypter `json:"-"`
	// DecrypterCert points to the certificate of the CA decrypter.
	DecrypterCert *x509.Certificate `json:"-"`
	// SCEPProvisionerNames contains the currently configured SCEP provioner names. These
	// are used to be able to load the provisioners when the SCEP authority is being
	// validated.
	SCEPProvisionerNames []string
}

type comparablePublicKey interface {
	Equal(crypto.PublicKey) bool
}

// Validate checks the fields in Options.
func (o *Options) Validate() error {
	switch {
	case len(o.Intermediates) == 0:
		return errors.New("no intermediate certificate available for SCEP authority")
	case o.SignerCert == nil:
		return errors.New("no signer certificate available for SCEP authority")
	}

	// the signer is optional, but if it's set, its public key must match the signer
	// certificate public key.
	if o.Signer != nil {
		// check if the signer (intermediate CA) certificate has the same public key as
		// the signer. According to the RFC it seems valid to have different keys for
		// the intermediate and the CA signing new certificates, so this might change
		// in the future.
		signerPublicKey := o.Signer.Public().(comparablePublicKey)
		if !signerPublicKey.Equal(o.SignerCert.PublicKey) {
			return errors.New("mismatch between signer certificate and public key")
		}
	}

	// decrypter can be nil in case a signing only key is used; validation complete.
	if o.Decrypter == nil {
		return nil
	}

	// If a decrypter is available, check that it's backed by an RSA key. According to the
	// RFC: https://tools.ietf.org/html/rfc8894#section-3.1, SCEP can be used with something
	// different than RSA, but requires the encryption to be performed using the challenge
	// password in that case. An older version of specification states that only RSA is
	// supported: https://tools.ietf.org/html/draft-nourse-scep-23#section-2.1.1. Other
	// algorithms do not seem to be supported in certnanny/sscep, but it might work
	// in micromdm/scep. Currently only RSA is allowed, but it might be an option
	// to try other algorithms in the future.
	decrypterPublicKey, ok := o.Decrypter.Public().(*rsa.PublicKey)
	if !ok {
		return errors.New("only RSA keys are (currently) supported as decrypters")
	}

	// check if intermediate public key is the same as the decrypter public key.
	// In certnanny/sscep it's mentioned that the signing key can be different
	// from the decrypting (and encrypting) key. These options are only used and
	// validated when the intermediate CA is also used as the decrypter, though,
	// so they should match.
	if !decrypterPublicKey.Equal(o.SignerCert.PublicKey) {
		return errors.New("mismatch between certificate chain and decrypter public keys")
	}

	return nil
}