-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcertificate.go
254 lines (200 loc) · 6.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
package crypto
import (
"encoding/json"
"time"
"github.com/pkg/errors"
jose "gopkg.in/square/go-jose.v1"
"fmt"
"github.com/Brickchain/go-document.v2"
)
// VerifyDocumentInJWS wrapps VerifyDocumentWithCertificateChain
func VerifyDocumentInJWS(docString string, keyLevel int) (document.Document, []*jose.JsonWebKey, *jose.JsonWebKey, error) {
jws, err := UnmarshalSignature([]byte(docString))
if err != nil {
return nil, nil, nil, errors.Wrap(err, "failed to unmarshal JWS")
}
if len(jws.Signatures) < 1 {
return nil, nil, nil, errors.New("No signers of JWS")
}
signer := jws.Signatures[0].Header.JsonWebKey
_, _, payload, err := jws.VerifyMulti(signer)
if err != nil {
return nil, nil, nil, errors.Wrap(err, "failed to verify signature")
}
doc, err := document.Unmarshal(payload)
if err != nil {
return nil, nil, nil, errors.Wrap(err, "failed to unmarshal document")
}
if doc.GetCertificate() == "" {
return doc, []*jose.JsonWebKey{signer}, signer, nil
}
ok, signers, subject, err := VerifyDocumentWithCertificateChain(doc, keyLevel)
if err != nil {
return nil, nil, nil, errors.Wrap(err, "failed to validate certificate chain")
}
if !ok {
return nil, nil, nil, errors.New("Validation of certificate chain failed")
}
signTP := Thumbprint(signer)
subTP := Thumbprint(subject)
if signTP != subTP {
return nil, nil, nil, errors.New("Signer of document is not the subject of the certificate")
}
return doc, signers, subject, nil
}
// VerifyDocumentWithTypeInJWS wrapps VerifyDocumentWithCertificateChain
func VerifyDocumentWithTypeInJWS(docString string, keyLevel int, doc document.Document) ([]*jose.JsonWebKey, *jose.JsonWebKey, error) {
jws, err := UnmarshalSignature([]byte(docString))
if err != nil {
return nil, nil, errors.Wrap(err, "failed to unmarshal JWS")
}
if len(jws.Signatures) < 1 {
return nil, nil, errors.New("No signers of JWS")
}
signer := jws.Signatures[0].Header.JsonWebKey
_, _, payload, err := jws.VerifyMulti(signer)
if err != nil {
return nil, nil, errors.Wrap(err, "failed to verify signature")
}
err = json.Unmarshal(payload, &doc)
if err != nil {
return nil, nil, errors.Wrap(err, "failed to unmarshal document")
}
if doc.GetCertificate() == "" {
return []*jose.JsonWebKey{signer}, signer, nil
}
ok, signers, subject, err := VerifyDocumentWithCertificateChain(doc, keyLevel)
if err != nil {
return nil, nil, errors.Wrap(err, "failed to validate certificate chain")
}
if !ok {
return nil, nil, errors.New("Validation of certificate chain failed")
}
signTP := Thumbprint(signer)
subTP := Thumbprint(subject)
if signTP != subTP {
return nil, nil, errors.New("Signer of document is not the subject of the certificate")
}
return signers, subject, nil
}
// VerifyDocumentWithCertificateChain wrapps VerifyCertificate
func VerifyDocumentWithCertificateChain(doc document.Document, keyLevel int) (bool, []*jose.JsonWebKey, *jose.JsonWebKey, error) {
if doc.GetCertificate() == "" {
return false, nil, nil, errors.New("No certificateChain in document")
}
var subject *jose.JsonWebKey
signers := make([]*jose.JsonWebKey, 0)
certChain := doc.GetCertificate()
prevDoc := doc
prevKeyLevel := keyLevel
prevIssuerTP := ""
for {
cert, err := VerifyCertificate(certChain, keyLevel)
if err != nil {
return false, nil, nil, err
}
if subject == nil {
subject = cert.Subject
}
if prevIssuerTP != "" {
subjectTP := Thumbprint(cert.Subject)
if subjectTP != prevIssuerTP {
return false, nil, nil, fmt.Errorf("Chain is broken, current subject is not issuer of next certificate")
}
}
signers = append(signers, cert.Issuer)
if !cert.AllowedType(prevDoc) {
return false, nil, nil, fmt.Errorf("Certificate not allowed to sign document of type %s", prevDoc.GetType())
}
if !cert.AllowedType(doc) {
return false, nil, nil, fmt.Errorf("Certificate not allowed to sign document of type %s", doc.GetType())
}
if prevKeyLevel == keyLevel {
prevKeyLevel = cert.KeyLevel
}
if cert.KeyLevel > prevKeyLevel {
return false, nil, nil, errors.New("Not possible to have parent certificate with lower keyLevel than child")
}
if cert.Certificate == "" {
break
}
prevDoc = cert
prevKeyLevel = cert.KeyLevel
prevIssuerTP = Thumbprint(cert.Issuer)
certChain = cert.Certificate
}
return true, signers, subject, nil
}
// VerifyCertificate parses from string and keylevel
func VerifyCertificate(certificateString string, keyLevel int) (*document.Certificate, error) {
certChainJWS, err := UnmarshalSignature([]byte(certificateString))
if err != nil {
return nil, fmt.Errorf("Invalid certificate JWS")
}
if len(certChainJWS.Signatures) < 1 {
return nil, fmt.Errorf("Invalid header of JWS, does not contain signing key")
}
if certChainJWS.Signatures[0].Header.JsonWebKey == nil {
return nil, fmt.Errorf("Invalid header of JWS, does not contain signing key")
}
signingIssuerTP := Thumbprint(certChainJWS.Signatures[0].Header.JsonWebKey)
payload, err := certChainJWS.Verify(certChainJWS.Signatures[0].Header.JsonWebKey)
if err != nil {
return nil, fmt.Errorf("Invalid signature of certificate chain")
}
certificate := document.Certificate{}
err = json.Unmarshal(payload, &certificate)
if err != nil {
return nil, fmt.Errorf("Could not unmarshal certificate")
}
if certificate.KeyLevel > keyLevel {
return nil, fmt.Errorf("Key level %d is higher than allowed level of %d", certificate.KeyLevel, keyLevel)
}
if certificate.Issuer == nil {
return nil, fmt.Errorf("Missing issuer key")
}
issuerTP := Thumbprint(certificate.Issuer)
if issuerTP != signingIssuerTP {
return nil, fmt.Errorf("Chain was not signed by root key specified in chain")
}
if certificate.TTL != 0 && certificate.Timestamp.Add(time.Second*time.Duration(certificate.TTL)).Before(time.Now().UTC()) {
return nil, fmt.Errorf("Certificate has expired")
}
return &certificate, nil
}
// CreateCertificate creates document.Certificate
func CreateCertificate(issuer, subject *jose.JsonWebKey, keyLevel int, documentTypes []string, ttl int, certificateChain string) (string, error) {
issuerPK, err := NewPublicKey(issuer)
if err != nil {
return "", err
}
chain := &document.Certificate{
Base: document.Base{
Type: document.CertificateType,
Timestamp: time.Now().UTC(),
Certificate: certificateChain,
},
TTL: ttl,
Issuer: issuerPK,
Subject: subject,
DocumentTypes: documentTypes,
KeyLevel: keyLevel,
}
signer, err := NewSigner(issuer)
if err != nil {
return "", err
}
chainBytes, err := json.Marshal(chain)
if err != nil {
return "", err
}
sig, err := signer.Sign(chainBytes)
if err != nil {
return "", err
}
sigString, err := sig.CompactSerialize()
if err != nil {
return "", err
}
return sigString, nil
}