-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathld_signaturesuite.go
452 lines (395 loc) · 12.9 KB
/
ld_signaturesuite.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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
package proof
import (
"crypto/sha256"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"strings"
"time"
jcs "github.com/cyberphone/json-canonicalization/go/src/webpki.org/jsoncanonicalizer"
"github.com/mr-tron/base58"
"github.com/piprate/json-gold/ld"
"github.com/workdaycredentials/ledger-common/util"
)
const (
jwsSeparator = "."
jwsSignature = 2
w3SecurityContext = "https://w3id.org/security/v2"
)
var (
ed25519JWTHeader = map[string]interface{}{
"alg": "EdDSA",
"b64": false,
"crit": []string{"b64"},
}
)
// LDSignatureSuite is a SignatureSuite based on the Linked-Data Signatures specification.
// See https://w3c-ccg.github.io/ld-proofs/#linked-data-signatures.
type LDSignatureSuite struct {
SignatureType SignatureType
KeyType KeyType
ProofFactory ProofFactory
Encoder Encoder
}
// Type returns the SignatureType that this suite is capable of generating and verifying.
func (s LDSignatureSuite) Type() SignatureType {
return s.SignatureType
}
// Sign adds a digital signature to the provable object in the form of a Proof.
// The type of Proof is determined by the ProofFactory used to construct this suite.
// Returns an error if the provable object already contains a Proof or if any error is
// encountered when generating the digital signature.
func (s LDSignatureSuite) Sign(provable Provable, signer Signer, opts *ProofOptions) error {
if provable.GetProof() != nil {
return errors.New("attempt to overwrite existing proof")
}
if signer.Type() != s.KeyType {
return errors.New("incorrect key type")
}
p := s.ProofFactory.Create(signer, s.SignatureType, opts)
provable.SetProof(p)
toBeSigned, err := s.Encoder.Encode(provable)
if err != nil {
provable.SetProof(nil)
return err
}
signature, err := signer.Sign(toBeSigned)
if err != nil {
provable.SetProof(nil)
return err
}
if err := s.Encoder.SetSignatureValue(provable, signature); err != nil {
return err
}
return nil
}
type EncodeOptions struct {
JWSHeader string
}
// Verify checks that the provable's Proof is valid.
// Returns an error if the Proof is missing or invalid.
func (s LDSignatureSuite) Verify(provable Provable, verifier Verifier) error {
p := provable.GetProof()
if p.IsEmpty() {
return fmt.Errorf("missing proof")
}
signature, err := s.Encoder.DecodeSignature(provable)
if err != nil {
return err
}
data, err := s.Encoder.Encode(provable)
if err != nil {
return err
}
if success, err := verifier.Verify(data, signature); err != nil {
return err
} else if !success {
return errors.New("signature verification failed")
}
return nil
}
// ProofFactory creates proofs given a signer and signature type
type ProofFactory interface {
Create(signer Signer, signatureType SignatureType, opts *ProofOptions) *Proof
}
// proofFactoryV1 is a factory for creating proofs using the "creator" field.
type proofFactoryV1 struct {
SignatureType SignatureType
}
func (f *proofFactoryV1) Create(signer Signer, signatureType SignatureType, opts *ProofOptions) *Proof {
proof := &Proof{
Created: time.Now().UTC().Format(time.RFC3339),
Creator: signer.ID(),
Type: signatureType,
}
if opts != nil {
// proof.ProofPurpose = opts.ProofPurpose
proof.Domain = opts.Domain
proof.Challenge = opts.Challenge
}
return proof
}
// proofFactoryV2 is a factory for creating proofs using the "verificationMethod" field.
type proofFactoryV2 struct {
SignatureType SignatureType
UsesNonce bool
}
func (f *proofFactoryV2) Create(signer Signer, signatureType SignatureType, opts *ProofOptions) *Proof {
var nonce string
if f.UsesNonce {
nonce = util.GetNonce()
}
proof := &Proof{
Created: time.Now().UTC().Format(time.RFC3339),
VerificationMethod: signer.ID(),
Type: signatureType,
}
if opts != nil {
// TODO(gabe) comment back in once dynamic mobile models issue is fixed
// proof.ProofPurpose = opts.ProofPurpose
proof.Domain = opts.Domain
proof.Challenge = opts.Challenge
proof.Nonce = nonce
proof.VerificationMethod = signer.ID()
}
return proof
}
type Encoder interface {
Encode(provable Provable) ([]byte, error)
SetSignatureValue(provable Provable, signature []byte) error
DecodeSignature(provable Provable) ([]byte, error)
}
type JWSEncoder struct {
header string
marshaler Marshaler
canonicalizer Canonicalizer
digester MessageDigest
optionsAppender OptionsAppender
}
// Prepare a provable for JWSification
// https://w3c-ccg.github.io/ld-proofs/#create-verify-hash-algorithm
func (e *JWSEncoder) Encode(provable Provable) ([]byte, error) {
var ldProof Proof
if err := util.DeepCopy(provable.GetProof(), &ldProof); err != nil {
return nil, err
}
// canonicalize proof
proofBytes, err := e.marshaler.Marshal(&ldProof)
if err != nil {
return nil, err
}
var proofMap map[string]interface{}
if err := json.Unmarshal(proofBytes, &proofMap); err != nil {
return nil, err
}
proofMap["@context"] = w3SecurityContext
proofMapBytes, err := json.Marshal(proofMap)
if err != nil {
return nil, err
}
if e.canonicalizer == nil {
return nil, errors.New("canonicalizer must be initialized")
}
canonicalProofBytes, err := e.canonicalizer.Canonicalize(proofMapBytes)
if err != nil {
return nil, err
}
// get digest of proof
if e.digester == nil {
return nil, errors.New("digester must be initialized")
}
proofDigest, err := e.digester.Digest(canonicalProofBytes)
if err != nil {
return nil, err
}
// canonicalize doc
defer provable.SetProof(&ldProof)
provable.SetProof(nil)
docBytes, err := json.Marshal(provable)
if err != nil {
return nil, err
}
canonicalDocBytes, err := e.canonicalizer.Canonicalize(docBytes)
if err != nil {
return nil, err
}
// get digest of doc
docDigest, err := e.digester.Digest(canonicalDocBytes)
if err != nil {
return nil, err
}
// append header before finishing encoding
data := append(proofDigest, docDigest...)
return e.optionsAppender.Append(data, &AppendOptions{Header: e.header}), nil
}
func (e *JWSEncoder) SetSignatureValue(provable Provable, signature []byte) error {
p := provable.GetProof()
if p == nil {
p = &Proof{}
}
if p.JWS != "" {
return errors.New("jws value already set")
}
if p.SignatureValue != "" {
return errors.New("signature value set on jws proof type")
}
p.JWS = e.header + ".." + base64.RawURLEncoding.EncodeToString(signature)
provable.SetProof(p)
return nil
}
func (e *JWSEncoder) DecodeSignature(provable Provable) ([]byte, error) {
splitJWS := strings.Split(provable.GetProof().JWS, jwsSeparator)
if len(splitJWS) != 3 {
return nil, errors.New("signature verification failed")
}
return base64.RawURLEncoding.DecodeString(splitJWS[jwsSignature])
}
// As per https://w3c-ccg.github.io/lds-rsa2018/#modifications-to-signature-algorithm
func getEd25519SignatureJWSHeader() string {
headerBytes, err := json.Marshal(ed25519JWTHeader)
if err != nil {
panic(err)
}
return base64.RawURLEncoding.EncodeToString(headerBytes)
}
type LDSignatureEncoder struct {
marshaler Marshaler
canonicalizer Canonicalizer
digester MessageDigest
optionsAppender OptionsAppender
}
func (e *LDSignatureEncoder) Encode(provable Provable) ([]byte, error) {
if e.marshaler == nil {
return nil, errors.New("marshaler cannot be nil")
}
jsonBytes, err := e.marshaler.Marshal(provable)
if err != nil {
return nil, err
}
if e.canonicalizer != nil {
jsonBytes, err = e.canonicalizer.Canonicalize(jsonBytes)
if err != nil {
return nil, err
}
}
if e.digester != nil {
jsonBytes, err = e.digester.Digest(jsonBytes)
if err != nil {
return nil, err
}
}
if e.optionsAppender != nil {
jsonBytes = e.optionsAppender.Append(jsonBytes, &AppendOptions{Nonce: provable.GetProof().Nonce})
}
return jsonBytes, nil
}
func (e *LDSignatureEncoder) SetSignatureValue(provable Provable, signature []byte) error {
p := provable.GetProof()
if p.SignatureValue != "" {
return errors.New("signature value already set")
}
if p.JWS != "" {
return errors.New("jws value set on signature proof type")
}
p.SignatureValue = base58.Encode(signature)
p.SetProof(p)
return nil
}
func (e *LDSignatureEncoder) DecodeSignature(provable Provable) ([]byte, error) {
signatureB58 := provable.GetProof().SignatureValue
return base58.Decode(signatureB58)
}
// Marshaler turns a Provable object into a JSON byte array. The JSON is not expected to be in
// canonical form; we have a separate Canonicalizer for that. Instead, this method gives the
// flexibility to add custom marshaling over the standard json.Marshal(). For example,
// some suites expect an embedded Proof object, while others do not. This type of manipulation
// should be handled here (or in OptionAppender if after canonicalization).
type Marshaler interface {
Marshal(provable Provable) ([]byte, error)
}
// EmbeddedProofMarshaler transforms the Provable into JSON, and leaves an embedded Proof sans the
// signature value. This will effectively pass the Proof Options (metadata) into the signing
// algorithm as part of the canonicalized JSON payload.
type EmbeddedProofMarshaler struct{}
func (m *EmbeddedProofMarshaler) Marshal(provable Provable) ([]byte, error) {
p := provable.GetProof()
signatureB58 := p.SignatureValue
jws := p.JWS
if p.SignatureValue != "" {
p.SignatureValue = ""
defer func() { p.SignatureValue = signatureB58 }()
}
if p.JWS != "" {
p.JWS = ""
defer func() { p.JWS = jws }()
}
return json.Marshal(provable)
}
// WithoutProofMarshaler transforms the Provable into JSON, and strips the proof.
type WithoutProofMarshaler struct{}
func (m *WithoutProofMarshaler) Marshal(provable Provable) ([]byte, error) {
p := provable.GetProof()
provable.SetProof(nil)
defer func() { provable.SetProof(p) }()
return json.Marshal(provable)
}
type JWSProofMarshaler struct{}
// Canonicalizer transforms a JSON byte array into its canonical form.
type Canonicalizer interface {
Canonicalize(jsonBytes []byte) ([]byte, error)
}
// JCSCanonicalizer transforms a JSON byte array using the JSON Canonicalization Scheme algorithm.
type JCSCanonicalizer struct{}
func (c *JCSCanonicalizer) Canonicalize(jsonBytes []byte) ([]byte, error) {
return jcs.Transform(jsonBytes)
}
const (
// Used in RDF Dataset Canonicalization
format = "application/n-quads"
algorithm = "URDNA2015"
)
type RDFCanonicalizer struct{}
func (c *RDFCanonicalizer) Canonicalize(jsonBytes []byte) ([]byte, error) {
processor := ld.NewJsonLdProcessor()
options := ld.NewJsonLdOptions("")
options.Algorithm = algorithm
options.Format = format
options.ProcessingMode = ld.JsonLd_1_1
options.ProduceGeneralizedRdf = true
// convert jsonBytes to map[string]interface{} which the library expects
var out map[string]interface{}
if err := json.Unmarshal(jsonBytes, &out); err != nil {
return nil, err
}
normalized, err := processor.Normalize(out, options)
if err != nil {
return nil, err
}
s := normalized.(string)
return []byte(s), nil
}
// MessageDigest transforms a byte array into a more compact byte array using a hashing digest
// algorithm, such as sha256. This creates a smaller payload for the digital signature.
//
// It should be noted that some ProofAlgorithms natively use a digest, such as the Ed25519 Ecdsa,
// which uses SHA512 under the hood. Therefore, we don't need to use a separate MessageDigest
// when signing with Ed25519 keys.
type MessageDigest interface {
Digest(data []byte) ([]byte, error)
}
// TODO(gabe) consider moving the Base64 encoding into the Canonicalizer instead of as a Digest.
// Base64Encoder base64 encodes the payload. This is only included to be compatible with the
// existing proof signatures on verifiable credentials. There's no benefit to base64 encoding a
// byte array that represents utf-8 characters, since little- and big-endianness does not apply.
type Base64Encoder struct{}
func (e *Base64Encoder) Digest(data []byte) ([]byte, error) {
encoded := base64.StdEncoding.EncodeToString(data)
return []byte(encoded), nil
}
// SHA256Encoder creates a SHA-256 hash of the payload.
type SHA256Encoder struct{}
func (e *SHA256Encoder) Digest(data []byte) ([]byte, error) {
hash := sha256.Sum256(data)
return hash[:], nil
}
type AppendOptions struct {
Nonce string
Header string
}
// OptionsAppender appends the proof options (metadata) to the payload before signing or verifying.
type OptionsAppender interface {
Append(data []byte, options *AppendOptions) []byte
}
// NonceAppender appends ".<nonce>" to the payload before signing or verifying.
// The nonce adds randomness in order to prevent a replay attack. Workday's earlier signature
// algorithms only included this field and did not sign over the other proof metadata fields.
type NonceAppender struct{}
func (n *NonceAppender) Append(data []byte, options *AppendOptions) []byte {
return util.AddNonceToDoc(data, options.Nonce)
}
// HeaderPrepender prepends a header to the data for a JWS
type HeaderPrepender struct{}
func (h *HeaderPrepender) Append(data []byte, options *AppendOptions) []byte {
return append([]byte(options.Header+"."), data...)
}