forked from hyperledger-archives/aries-framework-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathembedded_proof.go
176 lines (142 loc) · 5.05 KB
/
embedded_proof.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
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package verifiable
import (
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"github.com/hyperledger/aries-framework-go/pkg/doc/signature/jsonld"
"github.com/hyperledger/aries-framework-go/pkg/doc/signature/suite"
"github.com/hyperledger/aries-framework-go/pkg/doc/signature/suite/bbsblssignature2020"
"github.com/hyperledger/aries-framework-go/pkg/doc/signature/suite/bbsblssignatureproof2020"
"github.com/hyperledger/aries-framework-go/pkg/doc/signature/suite/ecdsasecp256k1signature2019"
"github.com/hyperledger/aries-framework-go/pkg/doc/signature/suite/ed25519signature2018"
"github.com/hyperledger/aries-framework-go/pkg/doc/signature/suite/jsonwebsignature2020"
"github.com/hyperledger/aries-framework-go/pkg/doc/signature/verifier"
)
const (
ed25519Signature2018 = "Ed25519Signature2018"
jsonWebSignature2020 = "JsonWebSignature2020"
ecdsaSecp256k1Signature2019 = "EcdsaSecp256k1Signature2019"
bbsBlsSignature2020 = "BbsBlsSignature2020"
bbsBlsSignatureProof2020 = "BbsBlsSignatureProof2020"
)
func getProofType(proofMap map[string]interface{}) (string, error) {
proofType, ok := proofMap["type"]
if !ok {
return "", errors.New("proof type is missing")
}
proofTypeStr := safeStringValue(proofType)
switch proofTypeStr {
case ed25519Signature2018, jsonWebSignature2020, ecdsaSecp256k1Signature2019,
bbsBlsSignature2020, bbsBlsSignatureProof2020:
return proofTypeStr, nil
default:
return "", fmt.Errorf("unsupported proof type: %s", proofType)
}
}
type embeddedProofCheckOpts struct {
publicKeyFetcher PublicKeyFetcher
disabledProofCheck bool
ldpSuites []verifier.SignatureSuite
jsonldCredentialOpts
}
func checkEmbeddedProof(docBytes []byte, opts *embeddedProofCheckOpts) ([]byte, error) {
if opts.disabledProofCheck {
return docBytes, nil
}
var jsonldDoc map[string]interface{}
if err := json.Unmarshal(docBytes, &jsonldDoc); err != nil {
return nil, fmt.Errorf("embedded proof is not JSON: %w", err)
}
proofElement, ok := jsonldDoc["proof"]
if !ok || proofElement == nil {
// do not make a check if there is no proof defined as proof presence is not mandatory
return docBytes, nil
}
proofs, err := getProofs(proofElement)
if err != nil {
return nil, fmt.Errorf("check embedded proof: %w", err)
}
ldpSuites, err := getSuites(proofs, opts)
if err != nil {
return nil, err
}
if opts.publicKeyFetcher == nil {
return nil, errors.New("public key fetcher is not defined")
}
checkedDoc := docBytes
if len(opts.externalContext) > 0 {
// Use external contexts for check of the linked data proofs to enrich JSON-LD context vocabulary.
jsonldDoc["@context"] = jsonld.AppendExternalContexts(jsonldDoc["@context"], opts.externalContext...)
checkedDoc, _ = json.Marshal(jsonldDoc) //nolint:errcheck
}
err = checkLinkedDataProof(checkedDoc, ldpSuites, opts.publicKeyFetcher, &opts.jsonldCredentialOpts)
if err != nil {
return nil, fmt.Errorf("check embedded proof: %w", err)
}
return docBytes, nil
}
func getSuites(proofs []map[string]interface{}, opts *embeddedProofCheckOpts) ([]verifier.SignatureSuite, error) {
ldpSuites := opts.ldpSuites
for i := range proofs {
t, err := getProofType(proofs[i])
if err != nil {
return nil, fmt.Errorf("check embedded proof: %w", err)
}
if len(opts.ldpSuites) == 0 {
switch t {
case ed25519Signature2018:
ldpSuites = append(ldpSuites, ed25519signature2018.New(
suite.WithVerifier(ed25519signature2018.NewPublicKeyVerifier())))
case jsonWebSignature2020:
ldpSuites = append(ldpSuites, jsonwebsignature2020.New(
suite.WithVerifier(jsonwebsignature2020.NewPublicKeyVerifier())))
case ecdsaSecp256k1Signature2019:
ldpSuites = append(ldpSuites, ecdsasecp256k1signature2019.New(
suite.WithVerifier(ecdsasecp256k1signature2019.NewPublicKeyVerifier())))
case bbsBlsSignature2020:
ldpSuites = append(ldpSuites, bbsblssignature2020.New(
suite.WithVerifier(bbsblssignature2020.NewG2PublicKeyVerifier())))
case bbsBlsSignatureProof2020:
nonce, err := getNonce(proofs[i])
if err != nil {
return nil, err
}
ldpSuites = append(ldpSuites, bbsblssignatureproof2020.New(
suite.WithVerifier(bbsblssignatureproof2020.NewG2PublicKeyVerifier(nonce))))
}
}
}
return ldpSuites, nil
}
func getNonce(proof map[string]interface{}) ([]byte, error) {
if nonce, ok := proof["nonce"]; ok {
n, err := base64.StdEncoding.DecodeString(nonce.(string))
if err != nil {
return nil, err
}
return n, nil
}
return []byte{}, nil
}
func getProofs(proofElement interface{}) ([]map[string]interface{}, error) {
switch p := proofElement.(type) {
case map[string]interface{}:
return []map[string]interface{}{p}, nil
case []interface{}:
proofs := make([]map[string]interface{}, len(p))
for i := range p {
proofMap, ok := p[i].(map[string]interface{})
if !ok {
return nil, errors.New("invalid proof type")
}
proofs[i] = proofMap
}
return proofs, nil
}
return nil, errors.New("invalid proof type")
}