/
signer.go
246 lines (192 loc) · 6.71 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
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
/*
Copyright Gen Digital Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package dataintegrity
import (
"encoding/json"
"errors"
"fmt"
"strings"
"github.com/tidwall/sjson"
"github.com/trustbloc/did-go/doc/did"
"github.com/trustbloc/vc-go/dataintegrity/models"
"github.com/trustbloc/vc-go/dataintegrity/suite"
)
const (
// AssertionMethod assertionMethod.
AssertionMethod = "assertionMethod"
// Authentication authentication.
Authentication = "authentication"
// CapabilityDelegation capabilityDelegation.
CapabilityDelegation = "capabilityDelegation"
// CapabilityInvocation capabilityInvocation.
CapabilityInvocation = "capabilityInvocation"
creatorParts = 2
invalidFormatErrMsgFmt = "verificationMethod value %s should be in did#keyID format"
)
// Signer implements the Add Proof algorithm of the verifiable credential data
// integrity specification, using a set of provided cryptographic suites.
type Signer struct {
suites map[string]suite.Signer
resolver didResolver
}
// NewSigner initializes a Signer that supports using the provided cryptographic
// suites to perform data integrity signing.
func NewSigner(opts *Options, suites ...suite.SignerInitializer) (*Signer, error) {
if opts == nil {
opts = &Options{}
}
signer := &Signer{
suites: map[string]suite.Signer{},
resolver: opts.DIDResolver,
}
for _, initializer := range suites {
suiteType := initializer.Type()
if _, ok := signer.suites[suiteType]; ok {
continue
}
signingSuite, err := initializer.Signer()
if err != nil {
return nil, err
}
signer.suites[suiteType] = signingSuite
}
return signer, nil
}
var (
// ErrProofGeneration is returned when Signer.AddProof() fails to generate a
// proof using a supported cryptographic suite.
ErrProofGeneration = errors.New("data integrity proof generation error")
)
// AddProof returns the provided JSON doc, with a top-level "proof" field added,
// signed using the provided options.
//
// If the provided options request a cryptographic suite that this Signer does
// not support, AddProof returns ErrUnsupportedSuite.
//
// If signing fails, or the created proof is invalid, AddProof returns
// ErrProofGeneration.
func (s *Signer) AddProof(doc []byte, opts *models.ProofOptions) ([]byte, error) { // nolint:gocyclo
signerSuite, ok := s.suites[opts.SuiteType]
if !ok {
return nil, ErrUnsupportedSuite
}
err := resolveVM(opts, s.resolver, "")
if err != nil {
return nil, err
}
proof, err := signerSuite.CreateProof(doc, opts)
if err != nil {
// TODO update linter to use go 1.20: https://github.com/hyperledger/aries-framework-go/issues/3613
return nil, errors.Join(ErrProofGeneration, err) // nolint:typecheck
}
if proof.Type == "" || proof.ProofPurpose == "" || proof.VerificationMethod == "" {
return nil, ErrProofGeneration
}
if proof.Created == "" && signerSuite.RequiresCreated() {
return nil, ErrProofGeneration
}
if opts.Domain != "" && opts.Domain != proof.Domain {
return nil, ErrProofGeneration
}
if opts.Challenge != "" && opts.Challenge != proof.Challenge {
return nil, ErrProofGeneration
}
proofRaw, err := json.Marshal(proof)
if err != nil {
return nil, ErrProofGeneration
}
out, err := sjson.SetRawBytes(doc, proofPath, proofRaw)
if err != nil {
// TODO update linter to use go 1.20: https://github.com/hyperledger/aries-framework-go/issues/3613
return nil, errors.Join(ErrProofGeneration, err) // nolint:typecheck
}
return out, nil
}
func resolveVM(opts *models.ProofOptions, resolver didResolver, vmID string) error {
if opts.VerificationMethod == nil {
if opts.VerificationMethodID == "" {
opts.VerificationMethodID = vmID
}
if resolver == nil {
return ErrNoResolver
}
didDoc, err := getDIDDocFromVerificationMethod(opts.VerificationMethodID, resolver)
if err != nil {
// TODO update linter to use go 1.20: https://github.com/hyperledger/aries-framework-go/issues/3613
return errors.Join(ErrVMResolution, err) // nolint:typecheck
}
vm, err := getVMByPurpose(opts.Purpose, opts.VerificationMethodID, didDoc)
if err != nil {
// TODO update linter to use go 1.20: https://github.com/hyperledger/aries-framework-go/issues/3613
return errors.Join(ErrVMResolution, err) // nolint:typecheck
}
opts.VerificationMethod = vm
}
return nil
}
func getVMByPurpose(purpose, vmID string, didDoc *did.Doc) (*did.VerificationMethod, error) {
var verificationMethod *did.VerificationMethod
vmIDFragment := vmIDFragmentOnly(vmID)
switch purpose {
case AssertionMethod:
assertionMethods := didDoc.VerificationMethods(did.AssertionMethod)[did.AssertionMethod]
verificationMethod = getVM(vmIDFragment, assertionMethods)
if verificationMethod == nil {
// A VM with general relationship is allowed for assertion
generalMethods :=
didDoc.VerificationMethods(did.VerificationRelationshipGeneral)[did.VerificationRelationshipGeneral]
verificationMethod = getVM(vmIDFragment, generalMethods)
}
case Authentication:
authMethods := didDoc.VerificationMethods(did.Authentication)[did.Authentication]
verificationMethod = getVM(vmIDFragment, authMethods)
case CapabilityDelegation:
capabilityDelegationMethods := didDoc.VerificationMethods(did.CapabilityDelegation)[did.CapabilityDelegation]
verificationMethod = getVM(vmIDFragment, capabilityDelegationMethods)
case CapabilityInvocation:
capabilityInvocationMethods := didDoc.VerificationMethods(did.CapabilityInvocation)[did.CapabilityInvocation]
verificationMethod = getVM(vmIDFragment, capabilityInvocationMethods)
default:
return nil, fmt.Errorf("purpose %s not supported", purpose)
}
if verificationMethod == nil {
return nil, fmt.Errorf("unable to find matching %s key IDs for given verification method ID %s",
purpose, vmID)
}
return verificationMethod, nil
}
func getVM(vmID string, vms []did.Verification) *did.VerificationMethod {
for _, verification := range vms {
if vmID == vmIDFragmentOnly(verification.VerificationMethod.ID) {
return &verification.VerificationMethod
}
}
return nil
}
func vmIDFragmentOnly(vmID string) string {
vmSplit := strings.Split(vmID, "#")
if len(vmSplit) == 1 {
return vmSplit[0]
}
return vmSplit[1]
}
func getDIDDocFromVerificationMethod(verificationMethod string, didResolver didResolver) (*did.Doc, error) {
didID, err := getDIDFromVerificationMethod(verificationMethod)
if err != nil {
return nil, err
}
docResolution, err := didResolver.Resolve(didID)
if err != nil {
return nil, err
}
return docResolution.DIDDocument, nil
}
func getDIDFromVerificationMethod(creator string) (string, error) {
idSplit := strings.Split(creator, "#")
if len(idSplit) != creatorParts {
return "", fmt.Errorf(fmt.Sprintf(invalidFormatErrMsgFmt, creator))
}
return idSplit[0], nil
}