-
Notifications
You must be signed in to change notification settings - Fork 0
/
key.go
389 lines (345 loc) · 13.2 KB
/
key.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
package key
import (
gocrypto "crypto"
"crypto/ed25519"
"fmt"
"strings"
"github.com/cyware/ssi-sdk/cryptosuite/jws2020"
"github.com/cyware/ssi-sdk/did"
"github.com/jorrizza/ed2curve25519"
"github.com/mr-tron/base58"
"github.com/multiformats/go-multibase"
"github.com/multiformats/go-multicodec"
"github.com/cyware/ssi-sdk/cryptosuite"
"github.com/pkg/errors"
"github.com/cyware/ssi-sdk/crypto"
"github.com/multiformats/go-varint"
)
type (
DIDKey string
)
const (
// Prefix did:key prefix
Prefix = "did:key"
// Expansion options
EnableEncryptionKeyDerivationOption = "EnableEncryptionKeyDerivation"
PublicKeyFormatOption = "PublicKeyFormat"
)
func (d DIDKey) IsValid() bool {
_, err := d.Expand()
return err == nil
}
func (d DIDKey) String() string {
return string(d)
}
// Suffix returns the value without the `did:key` prefix
func (d DIDKey) Suffix() (string, error) {
split := strings.Split(string(d), Prefix+":")
if len(split) != 2 {
return "", fmt.Errorf("invalid did:key: %s", d)
}
return split[1], nil
}
func (DIDKey) Method() did.Method {
return did.KeyMethod
}
type Option struct {
Name string
Value any
}
var (
EnableEncryptionKeyDerivation = Option{
Name: EnableEncryptionKeyDerivationOption,
Value: true,
}
DisableEncryptionKeyDerivation = Option{
Name: EnableEncryptionKeyDerivationOption,
Value: false,
}
PublicKeyFormatJSONWebKey2020 = Option{
Name: PublicKeyFormatOption,
Value: cryptosuite.JSONWebKey2020Type,
}
PublicKeyFormatMultibase = Option{
Name: PublicKeyFormatOption,
Value: cryptosuite.MultikeyType,
}
)
// GenerateDIDKey takes in a key type value that this library supports and constructs a conformant did:key identifier.
// The function returns the associated private key value cast to the generic golang crypto.PrivateKey interface.
// To use the private key, it is recommended to re-cast to the associated type. For example, called with the input
// for a secp256k1 key:
// privKey, didKey, err := GenerateDIDKey(SECP256k1)
// if err != nil { ... }
// // where secp is an import alias to the secp256k1 library we use "github.com/decred/dcrd/dcrec/secp256k1/v4"
// secpPrivKey, ok := privKey.(secp.PrivateKey)
// if !ok { ... }
func GenerateDIDKey(kt crypto.KeyType) (gocrypto.PrivateKey, *DIDKey, error) {
if !IsSupportedDIDKeyType(kt) {
return nil, nil, fmt.Errorf("unsupported did:key type: %s", kt)
}
pubKey, privKey, err := crypto.GenerateKeyByKeyType(kt)
if err != nil {
return nil, nil, errors.Wrap(err, "generating key for did:key")
}
pubKeyBytes, err := crypto.PubKeyToBytes(pubKey, crypto.ECDSAMarshalCompressed)
if err != nil {
return nil, nil, errors.Wrap(err, "converting public key to byte")
}
didKey, err := CreateDIDKey(kt, pubKeyBytes)
if err != nil {
return nil, nil, errors.Wrap(err, "creating DID key")
}
return privKey, didKey, err
}
// CreateDIDKey constructs a did:key from a specific key type and its corresponding public key
// This method does not attempt to validate that the provided public key is of the specified key type.
// A safer method is `GenerateDIDKey` which handles key generation based on the provided key type.
func CreateDIDKey(kt crypto.KeyType, publicKey []byte) (*DIDKey, error) {
if !IsSupportedDIDKeyType(kt) {
return nil, fmt.Errorf("unsupported did:key type: %s", kt)
}
// did:key:<multibase encoded, multicodec identified, public key>
encoded, err := MultibaseEncodedKey(kt, publicKey)
if err != nil {
return nil, errors.Wrap(err, "multibase encoding key")
}
didKey := DIDKey(fmt.Sprintf("%s:%s", Prefix, encoded))
return &didKey, nil
}
// MultibaseEncodedKey takes a key type and a public key value and returns the multibase encoded key
func MultibaseEncodedKey(kt crypto.KeyType, publicKey []byte) (string, error) {
multiCodec, err := did.KeyTypeToMultiCodec(kt)
if err != nil {
return "", fmt.Errorf("could find mutlicodec for key type<%s>", kt)
}
prefix := varint.ToUvarint(uint64(multiCodec))
codec := append(prefix, publicKey...)
encoded, err := multibase.Encode(did.Base58BTCMultiBase, codec)
if err != nil {
return "", errors.Wrap(err, "multibase encoding")
}
return encoded, nil
}
// Decode takes a did:key and returns the underlying public key value as bytes, the key type, and a possible error
// https://w3c-ccg.github.io/did-method-key/#document-creation-algorithm
func (d DIDKey) Decode() ([]byte, crypto.KeyType, error) {
parsed, err := d.Suffix()
if err != nil {
return nil, "", errors.Wrap(err, "parsing did:key")
}
if parsed == "" {
return nil, "", fmt.Errorf("could not decode did:key value: %s", string(d))
}
encoding, decoded, err := multibase.Decode(parsed)
if err != nil {
return nil, "", errors.Wrap(err, "decoding did:key")
}
if encoding != did.Base58BTCMultiBase {
return nil, "", fmt.Errorf("expected %d encoding but found %d", did.Base58BTCMultiBase, encoding)
}
// n = # bytes for the int, which we expect to be two from our multicodec
multiCodec, n, err := varint.FromUvarint(decoded)
if err != nil {
return nil, "", err
}
if n != 2 {
return nil, "", errors.New("error parsing did:key varint")
}
pubKeyBytes := decoded[n:]
multiCodecValue := multicodec.Code(multiCodec)
cryptoKeyType, err := did.MultiCodecToKeyType(multiCodecValue)
if err != nil {
return nil, "", errors.Wrap(err, "determining key type")
}
return pubKeyBytes, cryptoKeyType, nil
}
// Expand turns the DID key into a compliant DID Document
// Accepts the following options:
// - EnableEncryptionKeyDerivationOption (default to true)
// - PublicKeyFormatOption (defaults to JWK)
//
// TODO(gabe) support BLS curves https://github.com/cyware/ssi-sdk/issues/381
func (d DIDKey) Expand(opts ...Option) (*did.Document, error) {
publicKeyFormat, enableEncryptionDerivation, err := processExpansionOptions(opts...)
if err != nil {
return nil, err
}
id := string(d)
suffix, err := d.Suffix()
if err != nil {
return nil, errors.Wrap(err, "parsing did:key")
}
pubKey, cryptoKeyType, err := d.Decode()
if err != nil {
return nil, errors.Wrap(err, "decoding did:key")
}
var verificationMethod *did.VerificationMethod
keyID := id + "#" + suffix
switch publicKeyFormat {
case cryptosuite.JSONWebKey2020Type:
verificationMethod, err = did.ConstructJWKVerificationMethod(keyID, id, pubKey, cryptoKeyType)
if err != nil {
return nil, errors.Wrapf(err, "could not construct %s verification method", publicKeyFormat)
}
case cryptosuite.MultikeyType:
multiKeyType, err := did.KeyTypeToMultikeyLDType(cryptoKeyType)
if err != nil {
return nil, errors.Wrap(err, "converting key type to multikey type")
}
verificationMethod, err = did.ConstructMultibaseVerificationMethod(keyID, id, pubKey, multiKeyType)
if err != nil {
return nil, errors.Wrapf(err, "could not construct %s verification method", publicKeyFormat)
}
default:
return nil, fmt.Errorf("unsupported public key format: %s", publicKeyFormat)
}
// always include the first key as a verification method
verificationMethodSet := []did.VerificationMethodSet{keyID}
doc := did.Document{
ID: id,
VerificationMethod: []did.VerificationMethod{*verificationMethod},
Authentication: verificationMethodSet,
AssertionMethod: verificationMethodSet,
CapabilityDelegation: verificationMethodSet,
CapabilityInvocation: verificationMethodSet,
}
// https://w3c-ccg.github.io/did-method-key/#context-creation-algorithm
contexts := []string{did.KnownDIDContext}
if publicKeyFormat == cryptosuite.JSONWebKey2020Type {
contexts = append(contexts, cryptosuite.JSONWebKey2020Context)
} else {
switch cryptoKeyType {
case crypto.SECP256k1:
contexts = append(contexts, cryptosuite.SECP256k1VerificationKey2019Context)
case crypto.Ed25519:
if enableEncryptionDerivation {
contexts = append(contexts, cryptosuite.X25519KeyAgreementKey2020Context)
}
contexts = append(contexts, cryptosuite.Ed25519VerificationKey2020Context)
case crypto.X25519:
contexts = append(contexts, cryptosuite.X25519KeyAgreementKey2020Context)
case crypto.BLS12381G1, crypto.BLS12381G2:
contexts = append(contexts, cryptosuite.BLS12381G2Key2020Context)
case crypto.P224, crypto.P256, crypto.P384, crypto.P521:
contexts = append(contexts, cryptosuite.Multikey2021Context)
}
}
doc.Context = contexts
// X25519 doesn't have any property except key agreement
isVerificationMethodX25519Key := false
if (publicKeyFormat == cryptosuite.JSONWebKey2020Type && verificationMethod.PublicKeyJWK.CRV == string(jws2020.X25519)) ||
(publicKeyFormat == cryptosuite.MultikeyType && (verificationMethod.Type == cryptosuite.X25519KeyAgreementKey2020 ||
verificationMethod.Type == cryptosuite.X25519KeyAgreementKey2019)) {
isVerificationMethodX25519Key = true
doc.Authentication = nil
doc.AssertionMethod = nil
doc.CapabilityDelegation = nil
doc.CapabilityInvocation = nil
doc.KeyAgreement = []did.VerificationMethodSet{keyID}
}
// https://w3c-ccg.github.io/did-method-key/#derive-encryption-key-algorithm
// the only case we have to consider is if the verification method is X25519
if enableEncryptionDerivation && !isVerificationMethodX25519Key {
keyAgreementVerificationMethod, keyAgreementVerificationMethodSet, err := generateKeyAgreementVerificationMethod(*verificationMethod)
if err != nil {
return nil, errors.Wrap(err, "generating key agreement verification method")
}
if keyAgreementVerificationMethod != nil {
doc.VerificationMethod = append(doc.VerificationMethod, *keyAgreementVerificationMethod)
}
doc.KeyAgreement = keyAgreementVerificationMethodSet
}
return &doc, nil
}
func generateKeyAgreementVerificationMethod(vm did.VerificationMethod) (*did.VerificationMethod, []did.VerificationMethodSet, error) {
var verificationMethod *did.VerificationMethod
var verificationMethodSet []did.VerificationMethodSet
var vmErr error
if vm.Type == cryptosuite.Ed25519VerificationKey2018 || vm.Type == cryptosuite.Ed25519VerificationKey2020 {
// convert key to X25519
base58PubKey, err := base58.Decode(vm.PublicKeyBase58)
if err != nil {
return nil, nil, errors.Wrap(err, "decoding base58 public key")
}
ed25519PubKey, err := crypto.BytesToPubKey(base58PubKey, crypto.Ed25519)
if err != nil {
return nil, nil, errors.Wrap(err, "converting base58 public key to ed25519")
}
x25519Key, id, err := x25519KeyAndID(vm.Controller, ed25519PubKey.(ed25519.PublicKey))
if err != nil {
return nil, nil, errors.Wrap(err, "generating x25519 key and id")
}
verificationMethod, vmErr = did.ConstructMultibaseVerificationMethod(id, vm.Controller, x25519Key, cryptosuite.X25519KeyAgreementKey2020)
verificationMethodSet = []did.VerificationMethodSet{id}
return verificationMethod, verificationMethodSet, vmErr
} else if vm.Type == cryptosuite.JSONWebKey2020Type && vm.PublicKeyJWK.KTY == string(jws2020.OKP) &&
vm.PublicKeyJWK.CRV == string(jws2020.Ed25519) {
// convert key to X25519
ed25519PubKey, err := vm.PublicKeyJWK.ToPublicKey()
if err != nil {
return nil, nil, errors.Wrap(err, "converting ed25519 public key to x25519")
}
x25519Key, id, err := x25519KeyAndID(vm.Controller, ed25519PubKey.(ed25519.PublicKey))
if err != nil {
return nil, nil, errors.Wrap(err, "generating x25519 key and id")
}
verificationMethod, vmErr = did.ConstructJWKVerificationMethod(id, vm.Controller, x25519Key, crypto.X25519)
verificationMethodSet = []did.VerificationMethodSet{id}
} else {
verificationMethodSet = []did.VerificationMethodSet{vm.ID}
}
return verificationMethod, verificationMethodSet, vmErr
}
func x25519KeyAndID(id string, ed25519PubKey ed25519.PublicKey) ([]byte, string, error) {
if len(ed25519PubKey) != ed25519.PublicKeySize {
return nil, "", errors.New("ed25519 public key is not the right size")
}
x25519Key := ed2curve25519.Ed25519PublicKeyToCurve25519(ed25519PubKey)
keyAgreementDIDKey, err := CreateDIDKey(crypto.X25519, x25519Key)
if err != nil {
return nil, "", errors.Wrap(err, "creating key agreement did key")
}
suffix, err := keyAgreementDIDKey.Suffix()
if err != nil {
return nil, "", errors.Wrap(err, "getting suffix from did:key")
}
return x25519Key, id + "#" + suffix, nil
}
func processExpansionOptions(opts ...Option) (cryptosuite.LDKeyType, bool, error) {
publicKeyFormat := cryptosuite.JSONWebKey2020Type
enableEncryptionKeyDerivation := true
var ok bool
for _, opt := range opts {
switch opt.Name {
case "":
continue
case PublicKeyFormatOption:
publicKeyFormat, ok = opt.Value.(cryptosuite.LDKeyType)
if !ok {
return "", false, fmt.Errorf("invalid public key format option type: %T", opt.Value)
}
case EnableEncryptionKeyDerivationOption:
enableEncryptionKeyDerivation, ok = opt.Value.(bool)
if !ok {
return "", false, fmt.Errorf("invalid enable encryption key derivation option type: %T", opt.Value)
}
default:
return "", false, fmt.Errorf("invalid option: %s", opt.Name)
}
}
return publicKeyFormat, enableEncryptionKeyDerivation, nil
}
func IsSupportedDIDKeyType(kt crypto.KeyType) bool {
keyTypes := GetSupportedDIDKeyTypes()
for _, t := range keyTypes {
if t == kt {
return true
}
}
return false
}
func GetSupportedDIDKeyTypes() []crypto.KeyType {
return []crypto.KeyType{crypto.Ed25519, crypto.X25519, crypto.SECP256k1,
crypto.P256, crypto.P384, crypto.P521, crypto.RSA}
}