forked from hyperledger-archives/aries-framework-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
signer.go
169 lines (137 loc) · 5 KB
/
signer.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
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package signer
import (
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"time"
"github.com/Universal-Health-Chain/aries-framework-go/pkg/doc/signature/jsonld"
"github.com/Universal-Health-Chain/aries-framework-go/pkg/doc/signature/proof"
"github.com/Universal-Health-Chain/aries-framework-go/pkg/doc/util"
)
const defaultProofPurpose = "assertionMethod"
// SignatureSuite encapsulates signature suite methods required for signing documents.
type SignatureSuite interface {
// GetCanonicalDocument will return normalized/canonical version of the document
GetCanonicalDocument(doc map[string]interface{}, opts ...jsonld.ProcessorOpts) ([]byte, error)
// GetDigest returns document digest
GetDigest(doc []byte) []byte
// Accept registers this signature suite with the given signature type
Accept(signatureType string) bool
// Sign will sign document and return signature
Sign(doc []byte) ([]byte, error)
// CompactProof indicates weather to compact the proof doc before canonization
CompactProof() bool
}
// DocumentSigner implements signing of JSONLD documents.
type DocumentSigner struct {
signatureSuites []SignatureSuite
}
// Context holds signing options and private key.
type Context struct {
SignatureType string // required
Creator string // required
SignatureRepresentation proof.SignatureRepresentation // optional
Created *time.Time // optional
Domain string // optional
Nonce []byte // optional
VerificationMethod string // optional
Challenge string // optional
Purpose string // optional
CapabilityChain []interface{} // optional
}
// New returns new instance of document verifier.
func New(signatureSuites ...SignatureSuite) *DocumentSigner {
return &DocumentSigner{signatureSuites: signatureSuites}
}
// Sign will sign JSON LD document.
func (signer *DocumentSigner) Sign(context *Context, jsonLdDoc []byte, opts ...jsonld.ProcessorOpts) ([]byte, error) {
var jsonLdObject map[string]interface{}
err := json.Unmarshal(jsonLdDoc, &jsonLdObject)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal json ld document: %w", err)
}
err = signer.signObject(context, jsonLdObject, opts)
if err != nil {
return nil, err
}
signedDoc, err := json.Marshal(jsonLdObject)
if err != nil {
return nil, err
}
return signedDoc, nil
}
// signObject is a helper method that operates on JSON LD objects.
func (signer *DocumentSigner) signObject(context *Context, jsonLdObject map[string]interface{},
opts []jsonld.ProcessorOpts) error {
if err := isValidContext(context); err != nil {
return err
}
suite, err := signer.getSignatureSuite(context.SignatureType)
if err != nil {
return err
}
created := context.Created
if created == nil {
now := time.Now()
created = &now
}
p := &proof.Proof{
Type: context.SignatureType,
SignatureRepresentation: context.SignatureRepresentation,
Creator: context.Creator,
Created: &util.TimeWithTrailingZeroMsec{Time: *created},
Domain: context.Domain,
Nonce: context.Nonce,
VerificationMethod: context.VerificationMethod,
Challenge: context.Challenge,
ProofPurpose: context.Purpose,
CapabilityChain: context.CapabilityChain,
}
// TODO support custom proof purpose
// (https://github.com/hyperledger/aries-framework-go/issues/1586)
if p.ProofPurpose == "" {
p.ProofPurpose = defaultProofPurpose
}
if context.SignatureRepresentation == proof.SignatureJWS {
p.JWS = proof.CreateDetachedJWTHeader(p) + ".."
}
message, err := proof.CreateVerifyData(suite, jsonLdObject, p, append(opts, jsonld.WithValidateRDF())...)
if err != nil {
return err
}
s, err := suite.Sign(message)
if err != nil {
return err
}
signer.applySignatureValue(context, p, s)
return proof.AddProof(jsonLdObject, p)
}
func (signer *DocumentSigner) applySignatureValue(context *Context, p *proof.Proof, s []byte) {
switch context.SignatureRepresentation {
case proof.SignatureProofValue:
p.ProofValue = s
case proof.SignatureJWS:
p.JWS += base64.RawURLEncoding.EncodeToString(s)
}
}
// getSignatureSuite returns signature suite based on signature type.
func (signer *DocumentSigner) getSignatureSuite(signatureType string) (SignatureSuite, error) {
for _, s := range signer.signatureSuites {
if s.Accept(signatureType) {
return s, nil
}
}
return nil, fmt.Errorf("signature type %s not supported", signatureType)
}
// isValidContext checks required parameters (for signing).
func isValidContext(context *Context) error {
if context.SignatureType == "" {
return errors.New("signature type is missing")
}
return nil
}