/
json.go
369 lines (322 loc) · 10.9 KB
/
json.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
// Copyright (c) 2021 Fraunhofer AISEC
// Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package attestationreport
import (
"bytes"
"crypto"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/asn1"
"encoding/base64"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"math/big"
"github.com/Fraunhofer-AISEC/cmc/internal"
"gopkg.in/square/go-jose.v2"
)
// Custom type for JSON unmarshaller as byte arrays are
// encoded as hex strings in JSON but used as byte arrays
// internally and by CBOR encoding
type HexByte []byte
// MarshalJSON marshalls a byte array into a hex string
func (h *HexByte) MarshalJSON() ([]byte, error) {
return json.Marshal(hex.EncodeToString(*h))
}
// UnmarshalJSON unmarshalls JSON hex strings into
// byte arrays
func (h *HexByte) UnmarshalJSON(data []byte) error {
var v string
err := json.Unmarshal(data, &v)
if err != nil {
return fmt.Errorf("failed to unmarshal: %v", err)
}
*h, err = hex.DecodeString(v)
if err != nil {
return fmt.Errorf("failed to decode string: %v", err)
}
return nil
}
type JsonSerializer struct{}
func (s JsonSerializer) GetPayload(raw []byte) ([]byte, error) {
// Extract plain payload out of base64-encoded JSON Web Signature
jws, err := jose.ParseSigned(string(raw))
if err != nil {
return nil, fmt.Errorf("failed to parse jws object: %v", err)
}
data := jws.UnsafePayloadWithoutVerification()
return data, nil
}
func (s JsonSerializer) Marshal(v any) ([]byte, error) {
return json.Marshal(v)
}
func (s JsonSerializer) Unmarshal(data []byte, v any) error {
return json.Unmarshal(data, v)
}
// Sign signs the attestation report with the specified signer 'signer'
func (s JsonSerializer) Sign(report []byte, signer Signer) (bool, []byte) {
var err error
log.Trace("Signing attestation report")
// create list of all certificates in the correct order
certChain := signer.GetCertChain()
certsPem := make([][]byte, 0)
certsPem = append(certsPem, certChain.Leaf)
certsPem = append(certsPem, certChain.Intermediates...)
certsPem = append(certsPem, certChain.Ca)
// certificate chain in base64 encoding
certsb64 := make([]string, 0)
for i, certPem := range certsPem {
cert, err := internal.LoadCert(certPem)
if err != nil {
log.Errorf("Failed to load cert[%v]: %v. PEM: %v", i, err, string(certPem))
return false, nil
}
certsb64 = append(certsb64, base64.StdEncoding.EncodeToString(cert.Raw))
}
priv, pub, err := signer.GetSigningKeys()
if err != nil {
log.Errorf("Failed to get signing keys: %v", err)
return false, nil
}
// Get jose.SignatureAlgorithm
// Saltlength and further algorithms are set to the recommended default by x509
// we assume these defaults are correct
var alg jose.SignatureAlgorithm
alg, err = algFromKeyType(pub)
if err != nil {
log.Error(err)
return false, nil
}
log.Trace("Chosen signature algorithm: ", alg)
// create jose.OpaqueSigner with hwSigner wrapper (tpm key)
var hws *hwSigner
var opaqueSigner jose.OpaqueSigner
hws = &hwSigner{
pk: &jose.JSONWebKey{Key: pub},
signer: priv,
alg: alg,
}
opaqueSigner = jose.OpaqueSigner(hws)
// Create jose.Signer with OpaqueSigner
// x5c: adds Certificate Chain in later result
var opt jose.SignerOptions
var joseSigner jose.Signer
joseSigner, err = jose.NewSigner(jose.SigningKey{Algorithm: alg, Key: opaqueSigner}, opt.WithHeader("x5c", certsb64))
if err != nil {
log.Error("Failed to setup signer for the Attestation Report: ", err)
return false, nil
}
// This allows the signer to ensure mutual access for signing, if required
signer.Lock()
defer signer.Unlock()
// sign
log.Trace("Performing Sign operation")
obj, err := joseSigner.Sign(report)
if err != nil {
log.Error("Failed to sign the Attestation Report: ", err)
return false, nil
}
log.Trace("Signed attestation report")
// return signature in bytes
msg := obj.FullSerialize()
return true, []byte(msg)
}
// VerifyToken verifies signatures and certificate chains for JWS tokens
func (s JsonSerializer) VerifyToken(data []byte, roots []*x509.Certificate) (TokenResult, []byte, bool) {
var rootpool *x509.CertPool
var err error
result := TokenResult{}
ok := true
if len(roots) == 0 {
log.Debug("Using system certificate pool in absence of provided root certifcates")
rootpool, err = x509.SystemCertPool()
if err != nil {
msg := "Failed to setup trusted cert pool with system certificate pool"
result.Summary.setFalseMulti(&msg)
return result, nil, false
}
} else {
rootpool = x509.NewCertPool()
for _, cert := range roots {
rootpool.AddCert(cert)
}
}
opts := x509.VerifyOptions{
KeyUsages: []x509.ExtKeyUsage{x509.ExtKeyUsageAny},
Roots: rootpool,
}
jwsData, err := jose.ParseSigned(string(data))
if err != nil {
msg := fmt.Sprintf("Data could not be parsed - %v", err)
result.Summary.setFalseMulti(&msg)
return result, nil, false
}
if len(jwsData.Signatures) == 0 {
msg := "JWS does not contain signatures"
result.Summary.setFalseMulti(&msg)
return result, nil, false
}
index := make([]int, len(jwsData.Signatures))
payloads := make([][]byte, len(jwsData.Signatures))
for i, sig := range jwsData.Signatures {
result.SignatureCheck = append(result.SignatureCheck, SignatureResult{})
certs, err := sig.Protected.Certificates(opts)
if err != nil {
msg := fmt.Sprintf("Failed to verify certificate chain: %v", err)
result.SignatureCheck[i].CertCheck.setFalse(&msg)
ok = false
continue
}
// TODO log whole certificate chains including all fields
result.SignatureCheck[i].Name = certs[0][0].Subject.CommonName
result.SignatureCheck[i].Organization = certs[0][0].Subject.Organization
result.SignatureCheck[i].SubjectKeyId = hex.EncodeToString(certs[0][0].SubjectKeyId)
result.SignatureCheck[i].AuthorityKeyId = hex.EncodeToString(certs[0][0].AuthorityKeyId)
result.SignatureCheck[i].CertCheck.Success = true
index[i], _, payloads[i], err = jwsData.VerifyMulti(certs[0][0].PublicKey)
if err == nil {
result.SignatureCheck[i].Signature.Success = true
} else {
msg := fmt.Sprintf("Signature verification failed: %v", err)
result.SignatureCheck[i].Signature.setFalse(&msg)
ok = false
}
if index[i] != i {
msg := "order of signatures incorrect"
result.Summary.setFalseMulti(&msg)
}
if i > 0 {
if !bytes.Equal(payloads[i], payloads[i-1]) {
msg := "payloads differ for jws with multiple signatures"
result.Summary.setFalseMulti(&msg)
}
}
}
payload := payloads[0]
result.Summary.Success = ok
return result, payload, ok
}
// Deduces jose signature algorithm from provided key type
func algFromKeyType(pub crypto.PublicKey) (jose.SignatureAlgorithm, error) {
switch key := pub.(type) {
case *rsa.PublicKey:
switch key.Size() {
case 256:
// FUTURE: use RSA PSS: PS256
return jose.RS256, nil
case 512:
// FUTURE: use RSA PSS: PS512
return jose.RS512, nil
default:
return jose.RS256, fmt.Errorf("failed to determine algorithm from key type: unknown RSA key size: %v", key.Size())
}
case *ecdsa.PublicKey:
switch key.Curve {
case elliptic.P224(), elliptic.P256():
return jose.ES256, nil
case elliptic.P384():
return jose.ES384, nil
case elliptic.P521():
return jose.ES512, nil
default:
return jose.RS256, errors.New("failed to determine algorithm from key type: unknown elliptic curve")
}
default:
return jose.RS256, errors.New("failed to determine algorithm from key type: unknown key type")
}
}
// Used for the JOSE Opaque Signer Interface. This enables signing
// the attestation report with hardware-based keys (such as TPM-based keys)
type hwSigner struct {
pk *jose.JSONWebKey
signer crypto.PrivateKey
alg jose.SignatureAlgorithm
}
// Implements the JOSE Opaque Signer Interface. This enables signing
// the attestation report with hardware-based keys (such as TPM-based keys)
func (hws *hwSigner) Public() *jose.JSONWebKey {
return hws.pk
}
// Implements the JOSE Opaque Signer Interface. This enables signing
// the attestation report with hardware-based keys (such as TPM-based keys)
func (hws *hwSigner) Algs() []jose.SignatureAlgorithm {
return []jose.SignatureAlgorithm{hws.alg}
}
// Implements the JOSE Opaque Signer Interface. This enables signing
// the attestation report with hardware-based keys (such as TPM-based keys)
func (hws *hwSigner) SignPayload(payload []byte, alg jose.SignatureAlgorithm) ([]byte, error) {
// EC-specific: key size in byte for later padding
var keySize int
// Determine hash / SignerOpts from algorithm
var opts crypto.SignerOpts
switch alg {
case jose.RS256, jose.ES256: // RSA, ECDSA with SHA256
keySize = 32 // 256 bit
opts = crypto.SHA256
case jose.PS256: // RSA PSS with SHA256
// we force default saltLengths, same as x509 and TPM2.0
opts = &rsa.PSSOptions{SaltLength: 32, Hash: crypto.SHA256}
case jose.RS384, jose.ES384: // RSA, ECDSA with SHA384
keySize = 48
opts = crypto.SHA384
case jose.PS384: // RSA PSS with SHA384
opts = &rsa.PSSOptions{SaltLength: 48, Hash: crypto.SHA384}
case jose.RS512, jose.ES512: // RSA, ECDSA with SHA512
keySize = 66 // 521 bit + padding
opts = crypto.SHA512
case jose.PS512: // RSA PSS with SHA512
opts = &rsa.PSSOptions{SaltLength: 64, Hash: crypto.SHA512}
default:
return nil, errors.New("Signing failed: Could not determine appropriate hash type")
}
// Hash payload
hasher := opts.HashFunc().New()
// According to documentation, Write() on hash never fails
_, _ = hasher.Write(payload)
hashed := hasher.Sum(nil)
// sign payload
switch alg {
case jose.ES256, jose.ES384, jose.ES512:
// Obtain signature
asn1Sig, err := hws.signer.(crypto.Signer).Sign(rand.Reader, hashed, opts)
if err != nil {
return nil, err
}
// Convert from asn1 format (as specified in crypto) to concatenated and padded format for go-jose
type ecdsasig struct {
R *big.Int
S *big.Int
}
var esig ecdsasig
ret := make([]byte, 2*keySize)
_, err = asn1.Unmarshal(asn1Sig, &esig)
if err != nil {
return nil, errors.New("ECDSA signature was not in expected format")
}
// Fill return buffer with padded keys
rBytes := esig.R.Bytes()
sBytes := esig.S.Bytes()
copy(ret[keySize-len(rBytes):keySize], rBytes)
copy(ret[2*keySize-len(sBytes):2*keySize], sBytes)
return ret, nil
default:
// The return format of all other signatures does not need to be adapted for go-jose
return hws.signer.(crypto.Signer).Sign(rand.Reader, hashed, opts)
}
}