This repository has been archived by the owner on Mar 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 160
/
Copy pathjws.go
209 lines (173 loc) · 6.96 KB
/
jws.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
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package proof
import (
"encoding/base64"
"encoding/json"
"errors"
"strings"
"github.com/hyperledger/aries-framework-go/pkg/doc/signature/jsonld"
)
const securityContext = "https://w3id.org/security/v2"
const (
jwtPartsNumber = 3
jwtHeaderPart = 0
jwtSignaturePart = 2
)
// CreateDetachedJWTHeader creates detached JWT header.
func CreateDetachedJWTHeader(p *Proof) string {
var jwsAlg string
// TODO this is a hacky workaround, to be improved
// (https://github.com/hyperledger/aries-framework-go/issues/1589)
switch p.Type {
case "EcdsaSecp256k1Signature2019":
jwsAlg = "ES256K"
case "Ed25519Signature2018":
jwsAlg = "EdDSA"
default:
jwsAlg = p.Type
}
jwtHeaderMap := map[string]interface{}{
"alg": jwsAlg,
"b64": false,
"crit": []string{"b64"},
}
jwtHeaderBytes, err := json.Marshal(jwtHeaderMap)
if err != nil {
panic(err)
}
return base64.RawURLEncoding.EncodeToString(jwtHeaderBytes)
}
// GetJWTSignature returns signature part of JWT
func GetJWTSignature(jwt string) ([]byte, error) {
jwtParts := strings.Split(jwt, ".")
if len(jwtParts) != jwtPartsNumber || jwtParts[jwtSignaturePart] == "" {
return nil, errors.New("invalid JWT")
}
return base64.RawURLEncoding.DecodeString(jwtParts[jwtSignaturePart])
}
func getJWTHeader(jwt string) (string, error) {
jwtParts := strings.Split(jwt, ".")
if len(jwtParts) != jwtPartsNumber { // nolint:gomnd
return "", errors.New("invalid JWT")
}
return jwtParts[jwtHeaderPart], nil
}
// createVerifyJWS creates a data to be used to create/verify a digital signature in the
// form of JSON Web Signature (JWS) with detached content (https://tools.ietf.org/html/rfc7797).
// The algorithm of building the payload is similar to conventional Create Verify Hash algorithm.
// It differs by using https://w3id.org/security/v2 as context for JSON-LD canonization of both
// JSON and Signature documents and by preliminary JSON-LD compacting of JSON document.
// The current implementation is based on the https://github.com/digitalbazaar/jsonld-signatures.
func createVerifyJWS(suite signatureSuite, jsonldDoc map[string]interface{}, p *Proof,
opts ...jsonld.ProcessorOpts) ([]byte, error) {
proofOptions := p.JSONLdObject()
canonicalProofOptions, err := prepareJWSProof(suite, proofOptions, opts...)
if err != nil {
return nil, err
}
proofOptionsDigest := suite.GetDigest(canonicalProofOptions)
canonicalDoc, err := prepareDocumentForJWS(suite, jsonldDoc, opts...)
if err != nil {
return nil, err
}
docDigest := suite.GetDigest(canonicalDoc)
verifyData := append(proofOptionsDigest, docDigest...)
jwtHeader, err := getJWTHeader(p.JWS)
if err != nil {
return nil, err
}
return append([]byte(jwtHeader+"."), verifyData...), nil
}
func prepareJWSProof(suite signatureSuite, proofOptions map[string]interface{},
opts ...jsonld.ProcessorOpts) ([]byte, error) {
proofOptions[jsonldContext] = securityContext
proofOptionsCopy := make(map[string]interface{}, len(proofOptions))
for key, value := range proofOptions {
proofOptionsCopy[key] = value
}
delete(proofOptionsCopy, jsonldJWS)
delete(proofOptionsCopy, jsonldProofValue)
return suite.GetCanonicalDocument(proofOptionsCopy, append(opts, jsonld.WithRemoveAllInvalidRDF())...)
}
func prepareDocumentForJWS(suite signatureSuite, jsonldObject map[string]interface{},
opts ...jsonld.ProcessorOpts) ([]byte, error) {
// copy document object without proof
doc := GetCopyWithoutProof(jsonldObject)
if suite.CompactProof() {
docCompacted, err := getCompactedWithSecuritySchema(doc)
if err != nil {
return nil, err
}
doc = docCompacted
}
// build canonical document
return suite.GetCanonicalDocument(doc, opts...)
}
func getCompactedWithSecuritySchema(docMap map[string]interface{}) (map[string]interface{}, error) {
var contextMap map[string]interface{}
err := json.Unmarshal([]byte(securityJSONLD), &contextMap)
if err != nil {
return nil, err
}
return jsonld.Default().Compact(docMap, contextMap)
}
// cached value from https://w3id.org/security/v2
const securityJSONLD = `
{
"@context": [{
"@version": 1.1
}, "https://w3id.org/security/v1", {
"AesKeyWrappingKey2019": "sec:AesKeyWrappingKey2019",
"DeleteKeyOperation": "sec:DeleteKeyOperation",
"DeriveSecretOperation": "sec:DeriveSecretOperation",
"Ed25519Signature2018": "sec:Ed25519Signature2018",
"Ed25519VerificationKey2018": "sec:Ed25519VerificationKey2018",
"EquihashProof2018": "sec:EquihashProof2018",
"ExportKeyOperation": "sec:ExportKeyOperation",
"GenerateKeyOperation": "sec:GenerateKeyOperation",
"KmsOperation": "sec:KmsOperation",
"RevokeKeyOperation": "sec:RevokeKeyOperation",
"RsaSignature2018": "sec:RsaSignature2018",
"RsaVerificationKey2018": "sec:RsaVerificationKey2018",
"Sha256HmacKey2019": "sec:Sha256HmacKey2019",
"SignOperation": "sec:SignOperation",
"UnwrapKeyOperation": "sec:UnwrapKeyOperation",
"VerifyOperation": "sec:VerifyOperation",
"WrapKeyOperation": "sec:WrapKeyOperation",
"X25519KeyAgreementKey2019": "sec:X25519KeyAgreementKey2019",
"allowedAction": "sec:allowedAction",
"assertionMethod": {"@id": "sec:assertionMethod", "@type": "@id", "@container": "@set"},
"authentication": {"@id": "sec:authenticationMethod", "@type": "@id", "@container": "@set"},
"capability": {"@id": "sec:capability", "@type": "@id"},
"capabilityAction": "sec:capabilityAction",
"capabilityChain": {"@id": "sec:capabilityChain", "@type": "@id", "@container": "@list"},
"capabilityDelegation": {"@id": "sec:capabilityDelegationMethod", "@type": "@id", "@container": "@set"},
"capabilityInvocation": {"@id": "sec:capabilityInvocationMethod", "@type": "@id", "@container": "@set"},
"caveat": {"@id": "sec:caveat", "@type": "@id", "@container": "@set"},
"challenge": "sec:challenge",
"ciphertext": "sec:ciphertext",
"controller": {"@id": "sec:controller", "@type": "@id"},
"delegator": {"@id": "sec:delegator", "@type": "@id"},
"equihashParameterK": {"@id": "sec:equihashParameterK", "@type": "xsd:integer"},
"equihashParameterN": {"@id": "sec:equihashParameterN", "@type": "xsd:integer"},
"invocationTarget": {"@id": "sec:invocationTarget", "@type": "@id"},
"invoker": {"@id": "sec:invoker", "@type": "@id"},
"jws": "sec:jws",
"keyAgreement": {"@id": "sec:keyAgreementMethod", "@type": "@id", "@container": "@set"},
"kmsModule": {"@id": "sec:kmsModule"},
"parentCapability": {"@id": "sec:parentCapability", "@type": "@id"},
"plaintext": "sec:plaintext",
"proof": {"@id": "sec:proof", "@type": "@id", "@container": "@graph"},
"proofPurpose": {"@id": "sec:proofPurpose", "@type": "@vocab"},
"proofValue": "sec:proofValue",
"referenceId": "sec:referenceId",
"unwrappedKey": "sec:unwrappedKey",
"verificationMethod": {"@id": "sec:verificationMethod", "@type": "@id"},
"verifyData": "sec:verifyData",
"wrappedKey": "sec:wrappedKey"
}]
}
`