-
Notifications
You must be signed in to change notification settings - Fork 1
/
certificate.go
202 lines (181 loc) · 4.88 KB
/
certificate.go
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package bifrost
import (
"crypto/ecdsa"
"crypto/tls"
"crypto/x509"
"errors"
"fmt"
"github.com/google/uuid"
)
// Certificate is a bifrost certificate.
// It embeds the x509 certificate and adds the bifrost ID, namespace, and public key.
type Certificate struct {
*x509.Certificate
Id uuid.UUID
Namespace uuid.UUID
PublicKey *ecdsa.PublicKey
}
// ParseCertificate parses a DER encoded certificate and validates it.
// On success, it returns the bifrost certificate.
func ParseCertificate(asn1Data []byte) (*Certificate, error) {
cert, err := x509.ParseCertificate(asn1Data)
if err != nil {
return nil, err
}
c := &Certificate{
Certificate: cert,
}
if err := c.Verify(); err != nil {
return nil, err
}
return c, nil
}
// Verify validates a bifrost certificate. It checks for the correct signature algorithm,
// identity namespace, and identity. On success, it sets the ID, Namespace, and PublicKey fields.
func (c *Certificate) Verify() error {
// Check for bifrost signature algorithm
if c.SignatureAlgorithm != SignatureAlgorithm {
return fmt.Errorf(
"%w: unsupported signature algorithm '%s'",
ErrCertificateRequestInvalid,
c.SignatureAlgorithm,
)
}
// Parse identity namespace
if len(c.Subject.Organization) != 1 {
return fmt.Errorf("%w: missing identity namespace", ErrCertificateInvalid)
}
rawNS := c.Subject.Organization[0]
ns, err := uuid.Parse(rawNS)
if err != nil {
return fmt.Errorf(
"%w: invalid identity namespace %s: %w",
ErrCertificateInvalid,
rawNS,
err,
)
}
if ns == uuid.Nil {
return fmt.Errorf("%w: nil identity namespace", ErrCertificateInvalid)
}
pubkey, ok := c.Certificate.PublicKey.(*ecdsa.PublicKey)
if !ok {
return fmt.Errorf(
"%w: invalid public key type: '%T'",
ErrCertificateInvalid,
c.Certificate.PublicKey,
)
}
// Check if calculated UUID matches the UUID in the certificate
id := UUID(ns, pubkey)
cid, err := uuid.Parse(c.Subject.CommonName)
if err != nil {
return fmt.Errorf(
"%w: invalid subj CN '%s', %s",
ErrCertificateInvalid,
c.Subject.CommonName,
err.Error(),
)
}
if cid != id {
return fmt.Errorf("%w: incorrect identity", ErrCertificateInvalid)
}
c.Id = id
c.Namespace = ns
c.PublicKey = pubkey
return nil
}
// ToTLSCertificate puts a bifrost certificate inside a tls.Certificate.
func (c Certificate) ToTLSCertificate(key *ecdsa.PrivateKey) (*tls.Certificate, error) {
if c.PublicKey.X.Cmp(key.X) != 0 || c.PublicKey.Y.Cmp(key.Y) != 0 {
return nil, errors.New("private key does not match certificate public key")
}
return &tls.Certificate{
Certificate: [][]byte{
c.Raw,
},
PrivateKey: key,
Leaf: c.Certificate,
}, nil
}
type CertificateRequest struct {
*x509.CertificateRequest
Id uuid.UUID
Namespace uuid.UUID
PublicKey *ecdsa.PublicKey
}
// ParseCertificateRequest parses a DER encoded certificate request and validates it.
// On success, it returns the bifrost namespace, certificate request, and certificate public key.
func ParseCertificateRequest(asn1Data []byte) (*CertificateRequest, error) {
csr, err := x509.ParseCertificateRequest(asn1Data)
if err != nil {
return nil, fmt.Errorf("%w: %s", ErrCertificateRequestInvalid, err.Error())
}
c := &CertificateRequest{
CertificateRequest: csr,
}
if err := c.Verify(); err != nil {
return nil, err
}
return c, nil
}
// Verify validates a bifrost certificate request.
func (c *CertificateRequest) Verify() error {
// Check for bifrost signature algorithm
if c.SignatureAlgorithm != SignatureAlgorithm {
return fmt.Errorf(
"%w: unsupported signature algorithm '%s'",
ErrCertificateRequestInvalid,
c.SignatureAlgorithm,
)
}
// Parse identity namespace
if len(c.Subject.Organization) != 1 {
return fmt.Errorf(
"%w: missing identity namespace",
ErrCertificateRequestInvalid,
)
}
rawNS := c.Subject.Organization[0]
ns, err := uuid.Parse(rawNS)
if err != nil {
return fmt.Errorf(
"%w: invalid identity namespace %s: %w",
ErrCertificateRequestInvalid,
rawNS,
err,
)
}
pubkey, ok := c.CertificateRequest.PublicKey.(*ecdsa.PublicKey)
if !ok {
return fmt.Errorf(
"%w: invalid public key type: '%T'",
ErrCertificateRequestInvalid,
c.PublicKey,
)
}
// Check if calculated UUID matches the UUID in the certificate
id := UUID(ns, pubkey)
cid, err := uuid.Parse(c.Subject.CommonName)
if err != nil {
return fmt.Errorf("%w: invalid identity '%s', %s",
ErrCertificateRequestInvalid, c.Subject.CommonName, err.Error())
}
if cid != id {
return fmt.Errorf("%w: incorrect identity", ErrCertificateRequestInvalid)
}
c.Id = id
c.Namespace = ns
c.PublicKey = pubkey
return nil
}
// X509ToTLSCertificate puts an x509.Certificate inside a tls.Certificate.
func X509ToTLSCertificate(cert *x509.Certificate, key *ecdsa.PrivateKey) *tls.Certificate {
return &tls.Certificate{
Certificate: [][]byte{
cert.Raw,
},
PrivateKey: key,
Leaf: cert,
}
}