-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcertificate.go
executable file
·172 lines (137 loc) · 3.46 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
package crypto
import (
"bytes"
"errors"
"time"
"github.com/alinz/hash.go"
)
var (
ErrKeyMismatched = errors.New("keys are mismatched")
ErrNotCA = errors.New("given certificate is not a CA")
ErrNotIssuer = errors.New("issuer value is not the same")
ErrFailedVerifyCertificate = errors.New("certificate is not verified by ca")
)
type CertificateDetails struct {
NotBefore time.Time
NotAfter time.Time
PublicKey PublicKey
Issuer []byte
Extra []byte
IsCA bool
}
type Certificate struct {
Details *CertificateDetails
Signature []byte
}
func (c *Certificate) Verify(ca *Certificate) error {
if !ca.Details.IsCA {
return ErrNotCA
}
if c.Details.Issuer != nil && !bytes.Equal(c.Details.Issuer, ca.Signature) {
return ErrNotIssuer
}
b, err := BinaryEncode(c.Details)
if err != nil {
return err
}
if !ca.Details.PublicKey.Verify(hash.Bytes(b), c.Signature) {
return ErrFailedVerifyCertificate
}
return nil
}
func (c *Certificate) Encode() ([]byte, error) {
var buffer bytes.Buffer
details, err := BinaryEncode(c.Details)
if err != nil {
return nil, err
}
// because Singature is always fixed size (64), it has to be written first
buffer.Write(c.Signature)
buffer.Write(details)
return buffer.Bytes(), nil
}
func (c *Certificate) Decode(b []byte) error {
c.Signature = make([]byte, 64)
copy(c.Signature, b[:64])
c.Details = &CertificateDetails{}
err := BinaryDecode(b[64:], c.Details)
if err != nil {
return err
}
return nil
}
func (c *Certificate) DecodeExtra(decoder BinaryDecoder) error {
return decoder.Decode(c.Details.Extra)
}
// creation of certificate
type CertificateDetailsFn func(details *CertificateDetails) error
func CertWithNotBefore(notBefore time.Time) CertificateDetailsFn {
return func(details *CertificateDetails) error {
details.NotBefore = time.Unix(notBefore.Unix(), 0)
return nil
}
}
func CertWithNotAfter(notAfter time.Time) CertificateDetailsFn {
return func(details *CertificateDetails) error {
details.NotAfter = time.Unix(notAfter.Unix(), 0)
return nil
}
}
func CertWithAuthority() CertificateDetailsFn {
return func(details *CertificateDetails) error {
details.IsCA = true
return nil
}
}
func CertWithExtra(extra BinaryEncoder) CertificateDetailsFn {
return func(details *CertificateDetails) error {
data, err := extra.Encode()
if err != nil {
return err
}
details.Extra = data
return nil
}
}
func CreateCertificate(parentPrivateKey *PrivateKey, parentCertificate *Certificate, opts ...CertificateDetailsFn) (*Certificate, *PrivateKey, error) {
parentProvided := false
if parentPrivateKey != nil && parentCertificate != nil {
if !IsKeysMatched(&parentCertificate.Details.PublicKey, parentPrivateKey) {
return nil, nil, ErrKeyMismatched
}
parentProvided = true
}
details := &CertificateDetails{}
for _, opt := range opts {
err := opt(details)
if err != nil {
return nil, nil, err
}
}
public, private, err := NewKeyPair()
if err != nil {
return nil, nil, err
}
details.PublicKey = *public
if parentProvided {
details.Issuer = parentCertificate.Signature
}
b, err := BinaryEncode(details)
if err != nil {
return nil, nil, err
}
var signKey *PrivateKey
if parentProvided {
signKey = parentPrivateKey
} else {
signKey = private
}
signature, err := signKey.Sign(hash.Bytes(b))
if err != nil {
return nil, nil, err
}
return &Certificate{
Details: details,
Signature: signature,
}, private, nil
}