Debian Codegraph @

cert/sign.go

raw
1 package cert
2
3 import (
4 "crypto/ecdsa"
5 "crypto/ed25519"
6 "crypto/elliptic"
7 "crypto/rand"
8 "crypto/sha256"
9 "fmt"
10 "net/netip"
11 "time"
12
13 "github.com/slackhq/nebula/cert/p256"
14 )
15
16 // TBSCertificate represents a certificate intended to be signed.
17 // It is invalid to use this structure as a Certificate.
18 type TBSCertificate struct {
19 Version Version
20 Name string
21 Networks []netip.Prefix
22 UnsafeNetworks []netip.Prefix
23 Groups []string
24 IsCA bool
25 NotBefore time.Time
26 NotAfter time.Time
27 PublicKey []byte
28 Curve Curve
29 issuer string
30 }
31
32 type beingSignedCertificate interface {
33 // fromTBSCertificate copies the values from the TBSCertificate to this versions internal representation
34 // Implementations must validate the resulting certificate contains valid information
35 fromTBSCertificate(*TBSCertificate) error
36
37 // marshalForSigning returns the bytes that should be signed
38 marshalForSigning() ([]byte, error)
39
40 // setSignature sets the signature for the certificate that has just been signed. The signature must not be blank.
41 setSignature([]byte) error
42 }
43
44 type SignerLambda func(certBytes []byte) ([]byte, error)
45
46 // Sign will create a sealed certificate using details provided by the TBSCertificate as long as those
47 // details do not violate constraints of the signing certificate.
48 // If the TBSCertificate is a CA then signer must be nil.
49 func (t *TBSCertificate) Sign(signer Certificate, curve Curve, key []byte) (Certificate, error) {
50 switch t.Curve {
51 case Curve_CURVE25519:
52 pk := ed25519.PrivateKey(key)
53 sp := func(certBytes []byte) ([]byte, error) {
54 sig := ed25519.Sign(pk, certBytes)
55 return sig, nil
56 }
57 return t.SignWith(signer, curve, sp)
58 case Curve_P256:
59 pk, err := ecdsa.ParseRawPrivateKey(elliptic.P256(), key)
60 if err != nil {
61 return nil, err
62 }
63 sp := func(certBytes []byte) ([]byte, error) {
64 // We need to hash first for ECDSA
65 // - https://pkg.go.dev/crypto/ecdsa#SignASN1
66 hashed := sha256.Sum256(certBytes)
67 return ecdsa.SignASN1(rand.Reader, pk, hashed[:])
68 }
69 return t.SignWith(signer, curve, sp)
70 default:
71 return nil, fmt.Errorf("invalid curve: %s", t.Curve)
72 }
73 }
74
75 // SignWith does the same thing as sign, but uses the function in `sp` to calculate the signature.
76 // You should only use SignWith if you do not have direct access to your private key.
77 func (t *TBSCertificate) SignWith(signer Certificate, curve Curve, sp SignerLambda) (Certificate, error) {
78 if curve != t.Curve {
79 return nil, fmt.Errorf("curve in cert and private key supplied don't match")
80 }
81
82 if signer != nil {
83 if t.IsCA {
84 return nil, fmt.Errorf("can not sign a CA certificate with another")
85 }
86
87 err := checkCAConstraints(signer, t.NotBefore, t.NotAfter, t.Groups, t.Networks, t.UnsafeNetworks)
88 if err != nil {
89 return nil, err
90 }
91
92 issuer, err := signer.Fingerprint()
93 if err != nil {
94 return nil, fmt.Errorf("error computing issuer: %v", err)
95 }
96 t.issuer = issuer
97 } else {
98 if !t.IsCA {
99 return nil, fmt.Errorf("self signed certificates must have IsCA set to true")
100 }
101 }
102
103 var c beingSignedCertificate
104 switch t.Version {
105 case Version1:
106 c = &certificateV1{}
107 err := c.fromTBSCertificate(t)
108 if err != nil {
109 return nil, err
110 }
111 case Version2:
112 c = &certificateV2{}
113 err := c.fromTBSCertificate(t)
114 if err != nil {
115 return nil, err
116 }
117 default:
118 return nil, fmt.Errorf("unknown cert version %d", t.Version)
119 }
120
121 certBytes, err := c.marshalForSigning()
122 if err != nil {
123 return nil, err
124 }
125
126 sig, err := sp(certBytes)
127 if err != nil {
128 return nil, err
129 }
130
131 if curve == Curve_P256 {
132 sig, err = p256.Normalize(sig)
133 if err != nil {
134 return nil, err
135 }
136 }
137
138 err = c.setSignature(sig)
139 if err != nil {
140 return nil, err
141 }
142
143 sc, ok := c.(Certificate)
144 if !ok {
145 return nil, fmt.Errorf("invalid certificate")
146 }
147
148 return sc, nil
149 }
150
151 func comparePrefix(a, b netip.Prefix) int {
152 addr := a.Addr().Compare(b.Addr())
153 if addr == 0 {
154 return a.Bits() - b.Bits()
155 }
156 return addr
157 }
158
159 // findDuplicatePrefix returns an error if there is a duplicate prefix in the pre-sorted input slice sortedPrefixes
160 func findDuplicatePrefix(sortedPrefixes []netip.Prefix) error {
161 if len(sortedPrefixes) < 2 {
162 return nil
163 }
164 for i := 1; i < len(sortedPrefixes); i++ {
165 if comparePrefix(sortedPrefixes[i], sortedPrefixes[i-1]) == 0 {
166 return NewErrInvalidCertificateProperties("duplicate network detected: %v", sortedPrefixes[i])
167 }
168 }
169 return nil
170 }