-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcertificate.go
310 lines (254 loc) · 7.73 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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
//go:generate protoc --go_out=. --go_opt=paths=source_relative certificate.proto
// MIT License
// Copyright (c) 2018-2019 Slack Technologies, Inc.
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package main
import (
"bytes"
"crypto"
"crypto/ed25519"
"crypto/rand"
"crypto/sha256"
"encoding/hex"
"fmt"
"time"
"golang.org/x/net/idna"
"golang.org/x/crypto/curve25519"
"google.golang.org/protobuf/proto"
)
const x25519KeyLen = 32
type AetherportCertificate struct {
Details AetherportCertificateDetails
Signature []byte
}
type AetherportCertificateDetails struct {
Name string
Labels []label
NotBefore time.Time
NotAfter time.Time
PublicKey []byte
IsCA bool
Issuer string
}
func UnmarshalAetherportCertificate(b []byte) (ac *AetherportCertificate, err error) {
if len(b) == 0 {
return nil, fmt.Errorf("nil byte")
}
var acr AetherportCertificateRaw
if err := proto.Unmarshal(b, &acr); err != nil {
return nil, fmt.Errorf("invalid certificate: %w", err)
}
if acr.Details == nil {
return nil, fmt.Errorf("the certificate does not contain any details")
}
ac = &AetherportCertificate{
Details: AetherportCertificateDetails{
Name: acr.Details.Name,
Labels: make([]label, len(acr.Details.Labels)),
NotBefore: time.Unix(0, acr.Details.NotBefore),
NotAfter: time.Unix(0, acr.Details.NotAfter),
PublicKey: make([]byte, len(acr.Details.PublicKey)),
IsCA: acr.Details.IsCA,
},
Signature: make([]byte, len(acr.Signature)),
}
copy(ac.Signature, acr.Signature)
ac.Details.Issuer = hex.EncodeToString(acr.Details.Issuer)
for _, s := range acr.Details.Labels {
l, err := labelFromString(s)
if err != nil {
return nil, fmt.Errorf("invalid labels was found (%s): %w", s, err)
}
ac.Details.Labels = append(ac.Details.Labels, l)
}
if len(acr.Details.PublicKey) < x25519KeyLen {
return nil, fmt.Errorf("public key was fewer than %d bytes; %v", x25519KeyLen, len(acr.Details.PublicKey))
}
copy(ac.Details.PublicKey, acr.Details.PublicKey)
if _, err := idna.Lookup.ToASCII(ac.Details.Name); err != nil {
return nil, fmt.Errorf("certificate name does not comply with IDNA2018: %w", err)
}
return
}
func (ac *AetherportCertificate) Marshal() ([]byte, error) {
dr, err := ac.getDetailsRaw()
if err != nil {
return nil, err
}
cr := &AetherportCertificateRaw{
Details: dr,
Signature: ac.Signature,
}
return proto.Marshal(cr)
}
func (ac *AetherportCertificate) getDetailsRaw() (dr *AetherportCertificateDetailsRaw, err error) {
dr = &AetherportCertificateDetailsRaw{
Name: ac.Details.Name,
Labels: make([]string, len(ac.Details.Labels)),
NotBefore: ac.Details.NotBefore.UnixNano(),
NotAfter: ac.Details.NotAfter.UnixNano(),
PublicKey: make([]byte, len(ac.Details.PublicKey)),
IsCA: ac.Details.IsCA,
}
copy(dr.PublicKey, ac.Details.PublicKey[:])
for _, l := range ac.Details.Labels {
dr.Labels = append(dr.Labels, l.String())
}
if dr.Issuer, err = hex.DecodeString(ac.Details.Issuer); err != nil {
return nil, fmt.Errorf("invalid issuer (%s): %w", ac.Details.Issuer, err)
}
if _, err := idna.Lookup.ToASCII(dr.Name); err != nil {
return nil, fmt.Errorf("certificate name does not comply with IDNA2018: %w", err)
}
return
}
func (ac *AetherportCertificate) VerifyPrivateKey(key []byte) (err error) {
switch ac.Details.IsCA {
case true:
if len(key) != ed25519.PrivateKeySize {
return fmt.Errorf("key was not 64 bytes, is invalid ed25519 private key")
}
if !ed25519.PublicKey(ac.Details.PublicKey).Equal(ed25519.PrivateKey(key).Public()) {
return fmt.Errorf("public key in cert and private key supplied don't match")
}
case false:
pub, err := curve25519.X25519(key, curve25519.Basepoint)
if err != nil {
return err
}
if !bytes.Equal(pub, ac.Details.PublicKey) {
return fmt.Errorf("public key in cert and private key supplied don't match")
}
}
return
}
func (ac *AetherportCertificate) Sign(key ed25519.PrivateKey, cert *AetherportCertificate) (err error) {
if cert != nil {
ac.Details.Issuer, err = cert.Sha256Sum()
if err != nil {
return
}
}
r, err := ac.getDetailsRaw()
if err != nil {
return
}
b, err := proto.Marshal(r)
if err != nil {
return err
}
sig, err := key.Sign(rand.Reader, b, crypto.Hash(0))
if err != nil {
return err
}
ac.Signature = sig
return nil
}
func (ac *AetherportCertificate) Verify(t time.Time, acp *AetherportCAPool) (bool, error) {
if acp.IsBlocklisted(ac) {
return false, fmt.Errorf("certificate has been blocked")
}
signer, err := acp.GetCAForCert(ac)
if err != nil {
return false, err
}
if signer.Expired(t) {
return false, fmt.Errorf("root certificate is expired")
}
if ac.Expired(t) {
return false, fmt.Errorf("certificate is expired")
}
if !ac.CheckSignature(signer.Details.PublicKey) {
return false, fmt.Errorf("certificate signature did not match")
}
if err := ac.CheckRootConstrains(signer); err != nil {
return false, err
}
return true, nil
}
func (ac *AetherportCertificate) CheckSignature(key ed25519.PublicKey) bool {
r, err := ac.getDetailsRaw()
if err != nil {
return false
}
b, err := proto.Marshal(r)
if err != nil {
return false
}
return ed25519.Verify(key, b, ac.Signature)
}
func (nc *AetherportCertificate) CheckRootConstrains(signer *AetherportCertificate) (err error) {
if signer.Details.NotAfter.Before(nc.Details.NotAfter) {
return fmt.Errorf("certificate expires after signing certificate")
}
if signer.Details.NotBefore.After(nc.Details.NotBefore) {
return fmt.Errorf("certificate is valid before the signing certificate")
}
return
}
func (ac *AetherportCertificate) Sha256Sum() (string, error) {
b, err := ac.Marshal()
if err != nil {
return "", err
}
sum := sha256.Sum256(b)
return hex.EncodeToString(sum[:]), nil
}
func (ac *AetherportCertificate) Expired(t time.Time) bool {
return ac.Details.NotBefore.After(t) || ac.Details.NotAfter.Before(t)
}
type label struct {
key string
value string
}
func newLabel(key string, value string) label {
return label{key: key, value: value}
}
func labelFromString(s string) (l label, err error) {
err = l.UnmarshalText([]byte(s))
return
}
func (l label) String() string {
if l.key == "" {
return ""
}
return l.key + "=" + l.value
}
func (l label) MarshalText() (text []byte, err error) {
return []byte(l.String()), err
}
func (l *label) UnmarshalText(text []byte) error {
if len(text) == 0 {
return nil
}
i := bytes.IndexRune(text, '=')
if i < 1 {
return fmt.Errorf("invalid labels: %x", text)
}
l.key = string(text[:i-1])
if i < len(text)-1 {
l.value = string(text[i+1:])
}
return nil
}
func (l label) MarshalBinary() (data []byte, err error) {
return l.MarshalText()
}
func (l *label) UnmarshalBinary(data []byte) error {
return l.UnmarshalText(data)
}