forked from hyperledger-archives/aries-framework-go
/
credential_sdjwt.go
424 lines (338 loc) · 11.8 KB
/
credential_sdjwt.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
/*
Copyright Avast Software. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package verifiable
import (
"crypto"
"encoding/json"
"fmt"
"github.com/hyperledger/aries-framework-go/pkg/doc/jose"
"github.com/hyperledger/aries-framework-go/pkg/doc/sdjwt/common"
"github.com/hyperledger/aries-framework-go/pkg/doc/sdjwt/holder"
"github.com/hyperledger/aries-framework-go/pkg/doc/sdjwt/issuer"
)
type marshalDisclosureOpts struct {
includeAllDisclosures bool
discloseIfAvailable []string
discloseRequired []string
holderBinding *holder.BindingInfo
signer jose.Signer
signingKeyID string
}
// MarshalDisclosureOption provides an option for Credential.MarshalWithDisclosure.
type MarshalDisclosureOption func(opts *marshalDisclosureOpts)
// TODO: should DiscloseGiven(IfAvailable|Required) have path semantics for disclosure?
// DiscloseGivenIfAvailable sets that the disclosures with the given claim names will be disclosed by
// Credential.MarshalWithDisclosure.
//
// If any name provided does not have a matching disclosure, Credential.MarshalWithDisclosure will skip the name.
//
// Will result in an error if this option is provided alongside DiscloseAll.
func DiscloseGivenIfAvailable(disclosureNames []string) MarshalDisclosureOption {
return func(opts *marshalDisclosureOpts) {
opts.discloseIfAvailable = disclosureNames
}
}
// DiscloseGivenRequired sets that the disclosures with the given claim names will be disclosed by
// Credential.MarshalWithDisclosure.
//
// If any name provided does not have a matching disclosure, Credential.MarshalWithDisclosure will return an error.
//
// Will result in an error if this option is provided alongside DiscloseAll.
func DiscloseGivenRequired(disclosureNames []string) MarshalDisclosureOption {
return func(opts *marshalDisclosureOpts) {
opts.discloseRequired = disclosureNames
}
}
// DiscloseAll sets that all disclosures in the given Credential will be disclosed by Credential.MarshalWithDisclosure.
//
// Will result in an error if this option is provided alongside DiscloseGivenIfAvailable or DiscloseGivenRequired.
func DiscloseAll() MarshalDisclosureOption {
return func(opts *marshalDisclosureOpts) {
opts.includeAllDisclosures = true
}
}
// DisclosureHolderBinding option configures Credential.MarshalWithDisclosure to include a holder binding.
func DisclosureHolderBinding(binding *holder.BindingInfo) MarshalDisclosureOption {
return func(opts *marshalDisclosureOpts) {
opts.holderBinding = binding
}
}
// DisclosureSigner option provides Credential.MarshalWithDisclosure with a signer that will be used to create an SD-JWT
// if the given Credential wasn't already parsed from SD-JWT.
func DisclosureSigner(signer jose.Signer, signingKeyID string) MarshalDisclosureOption {
return func(opts *marshalDisclosureOpts) {
opts.signer = signer
opts.signingKeyID = signingKeyID
}
}
// MarshalWithDisclosure marshals a SD-JWT credential in combined format for presentation, including precisely
// the disclosures indicated by provided options, and optionally a holder binding if given the requisite option.
func (vc *Credential) MarshalWithDisclosure(opts ...MarshalDisclosureOption) (string, error) {
options := &marshalDisclosureOpts{}
for _, opt := range opts {
opt(options)
}
if options.includeAllDisclosures && (len(options.discloseIfAvailable) > 0 || len(options.discloseRequired) > 0) {
return "", fmt.Errorf("incompatible options provided")
}
if vc.JWT != "" && vc.SDJWTHashAlg != "" {
return filterSDJWTVC(vc, options)
}
if options.signer == nil {
return "", fmt.Errorf("credential needs signer to create SD-JWT")
}
return createSDJWTPresentation(vc, options)
}
func filterSDJWTVC(vc *Credential, options *marshalDisclosureOpts) (string, error) {
disclosureCodes, err := filteredDisclosureCodes(vc.SDJWTDisclosures, options)
if err != nil {
return "", err
}
cf := common.CombinedFormatForPresentation{
SDJWT: vc.JWT,
Disclosures: disclosureCodes,
HolderBinding: vc.SDHolderBinding,
}
if options.holderBinding != nil {
cf.HolderBinding, err = holder.CreateHolderBinding(options.holderBinding)
if err != nil {
return "", fmt.Errorf("failed to create holder binding: %w", err)
}
}
return cf.Serialize(), nil
}
func createSDJWTPresentation(vc *Credential, options *marshalDisclosureOpts) (string, error) {
issued, err := makeSDJWT(vc, options.signer, options.signingKeyID)
if err != nil {
return "", fmt.Errorf("creating SD-JWT from Credential: %w", err)
}
disclosureClaims, err := common.GetDisclosureClaims(issued.Disclosures)
if err != nil {
return "", fmt.Errorf("parsing disclosure claims from vc sdjwt: %w", err)
}
disclosureCodes, err := filteredDisclosureCodes(disclosureClaims, options)
if err != nil {
return "", err
}
var presOpts []holder.Option
if options.holderBinding != nil {
presOpts = append(presOpts, holder.WithHolderBinding(options.holderBinding))
}
issuedSerialized, err := issued.Serialize(false)
if err != nil {
return "", fmt.Errorf("serializing SD-JWT for presentation: %w", err)
}
combinedSDJWT, err := holder.CreatePresentation(issuedSerialized, disclosureCodes, presOpts...)
if err != nil {
return "", fmt.Errorf("create SD-JWT presentation: %w", err)
}
return combinedSDJWT, nil
}
func filteredDisclosureCodes(
availableDisclosures []*common.DisclosureClaim,
options *marshalDisclosureOpts,
) ([]string, error) {
var (
useDisclosures []*common.DisclosureClaim
err error
disclosureCodes []string
)
if options.includeAllDisclosures {
useDisclosures = availableDisclosures
} else {
useDisclosures, err = filterDisclosures(availableDisclosures,
options.discloseIfAvailable, options.discloseRequired)
if err != nil {
return nil, err
}
}
for _, disclosure := range useDisclosures {
disclosureCodes = append(disclosureCodes, disclosure.Disclosure)
}
return disclosureCodes, nil
}
func filterDisclosures(
disclosures []*common.DisclosureClaim,
ifAvailable, required []string,
) ([]*common.DisclosureClaim, error) {
ifAvailMap := map[string]*common.DisclosureClaim{}
reqMap := map[string]*common.DisclosureClaim{}
for _, name := range ifAvailable {
ifAvailMap[name] = nil
}
for _, name := range required {
reqMap[name] = nil
delete(ifAvailMap, name) // avoid listing a disclosure twice, if it's in both lists
}
for _, disclosure := range disclosures {
if _, ok := ifAvailMap[disclosure.Name]; ok {
ifAvailMap[disclosure.Name] = disclosure
}
if _, ok := reqMap[disclosure.Name]; ok {
reqMap[disclosure.Name] = disclosure
}
}
var out []*common.DisclosureClaim
for _, claim := range ifAvailMap {
if claim != nil {
out = append(out, claim)
}
}
for _, claim := range reqMap {
if claim == nil {
return nil, fmt.Errorf("disclosure list missing required claim")
}
out = append(out, claim)
}
return out, nil
}
type makeSDJWTOpts struct {
hashAlg crypto.Hash
}
// MakeSDJWTOption provides an option for creating an SD-JWT from a VC.
type MakeSDJWTOption func(opts *makeSDJWTOpts)
// MakeSDJWTWithHash sets the hash to use for an SD-JWT VC.
func MakeSDJWTWithHash(hash crypto.Hash) MakeSDJWTOption {
return func(opts *makeSDJWTOpts) {
opts.hashAlg = hash
}
}
// MakeSDJWT creates an SD-JWT in combined format for issuance, with all fields in credentialSubject converted
// recursively into selectively-disclosable SD-JWT claims.
func (vc *Credential) MakeSDJWT(signer jose.Signer, signingKeyID string, options ...MakeSDJWTOption) (string, error) {
sdjwt, err := makeSDJWT(vc, signer, signingKeyID, options...)
if err != nil {
return "", err
}
sdjwtSerialized, err := sdjwt.Serialize(false)
if err != nil {
return "", fmt.Errorf("serializing SD-JWT: %w", err)
}
return sdjwtSerialized, nil
}
func makeSDJWT(vc *Credential, signer jose.Signer, signingKeyID string, options ...MakeSDJWTOption,
) (*issuer.SelectiveDisclosureJWT, error) {
opts := &makeSDJWTOpts{}
for _, option := range options {
option(opts)
}
claims, err := vc.JWTClaims(false)
if err != nil {
return nil, fmt.Errorf("constructing VC JWT claims: %w", err)
}
claimBytes, err := json.Marshal(claims)
if err != nil {
return nil, err
}
claimMap := map[string]interface{}{}
err = json.Unmarshal(claimBytes, &claimMap)
if err != nil {
return nil, err
}
headers := map[string]interface{}{
jose.HeaderKeyID: signingKeyID,
}
issuerOptions := []issuer.NewOpt{
issuer.WithStructuredClaims(true),
issuer.WithNonSelectivelyDisclosableClaims([]string{"id"}),
}
if opts.hashAlg != 0 {
issuerOptions = append(issuerOptions, issuer.WithHashAlgorithm(opts.hashAlg))
}
sdjwt, err := issuer.NewFromVC(claimMap, headers, signer, issuerOptions...)
if err != nil {
return nil, fmt.Errorf("creating SD-JWT from VC: %w", err)
}
return sdjwt, nil
}
type displayCredOpts struct {
displayAll bool
displayGiven []string
}
// DisplayCredentialOption provides an option for Credential.CreateDisplayCredential.
type DisplayCredentialOption func(opts *displayCredOpts)
// DisplayAllDisclosures sets that Credential.CreateDisplayCredential will include all disclosures in the generated
// credential.
func DisplayAllDisclosures() DisplayCredentialOption {
return func(opts *displayCredOpts) {
opts.displayAll = true
}
}
// DisplayGivenDisclosures sets that Credential.CreateDisplayCredential will include only the given disclosures in the
// generated credential.
func DisplayGivenDisclosures(given []string) DisplayCredentialOption {
return func(opts *displayCredOpts) {
opts.displayGiven = append(opts.displayGiven, given...)
}
}
// CreateDisplayCredential creates, for SD-JWT credentials, a Credential whose selective-disclosure subject fields
// are replaced with the disclosure data.
//
// Options may be provided to filter the disclosures that will be included in the display credential. If a disclosure is
// not included, the associated claim will not be present in the returned credential.
//
// If the calling Credential is not an SD-JWT credential, this method returns the credential itself.
func (vc *Credential) CreateDisplayCredential( // nolint:funlen,gocyclo
opts ...DisplayCredentialOption,
) (*Credential, error) {
options := &displayCredOpts{}
for _, opt := range opts {
opt(options)
}
if options.displayAll && len(options.displayGiven) > 0 {
return nil, fmt.Errorf("incompatible options provided")
}
if vc.SDJWTHashAlg == "" || vc.JWT == "" {
return vc, nil
}
credClaims, err := unmarshalJWSClaims(vc.JWT, false, nil)
if err != nil {
return nil, fmt.Errorf("unmarshal VC JWT claims: %w", err)
}
credClaims.refineFromJWTClaims()
useDisclosures := filterDisclosureList(vc.SDJWTDisclosures, options)
newVCObj, err := common.GetDisclosedClaims(useDisclosures, credClaims.VC)
if err != nil {
return nil, fmt.Errorf("assembling disclosed claims into vc: %w", err)
}
if subj, ok := newVCObj["credentialSubject"].(map[string]interface{}); ok {
clearEmpty(subj)
}
vcBytes, err := json.Marshal(&newVCObj)
if err != nil {
return nil, fmt.Errorf("marshalling vc object to JSON: %w", err)
}
newVC, err := populateCredential(vcBytes, nil)
if err != nil {
return nil, fmt.Errorf("parsing new VC from JSON: %w", err)
}
return newVC, nil
}
func filterDisclosureList(disclosures []*common.DisclosureClaim, options *displayCredOpts) []*common.DisclosureClaim {
if options.displayAll {
return disclosures
}
displayGivenMap := map[string]struct{}{}
for _, given := range options.displayGiven {
displayGivenMap[given] = struct{}{}
}
var out []*common.DisclosureClaim
for _, disclosure := range disclosures {
if _, ok := displayGivenMap[disclosure.Name]; ok {
out = append(out, disclosure)
}
}
return out
}
func clearEmpty(claims map[string]interface{}) {
for name, value := range claims {
if valueObj, ok := value.(map[string]interface{}); ok {
clearEmpty(valueObj)
if len(valueObj) == 0 {
delete(claims, name)
}
}
}
}