Skip to content

Commit

Permalink
Resolving did:key DIDs (#2523)
Browse files Browse the repository at this point in the history
  • Loading branch information
reinkrul authored Oct 17, 2023
1 parent ae52770 commit a32e8cb
Show file tree
Hide file tree
Showing 6 changed files with 439 additions and 0 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ require (
github.com/lestrrat-go/jwx v1.2.26
github.com/magiconair/properties v1.8.7
github.com/mdp/qrterminal/v3 v3.1.1
github.com/multiformats/go-multicodec v0.9.0
github.com/nats-io/nats-server/v2 v2.10.2
github.com/nats-io/nats.go v1.30.2
github.com/nuts-foundation/crypto-ecies v0.0.0-20211207143025-5b84f9efce2b
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,8 @@ github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lN
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/mr-tron/base58 v1.1.3 h1:v+sk57XuaCKGXpWtVBX8YJzO7hMGx4Aajh4TQbdEFdc=
github.com/mr-tron/base58 v1.1.3/go.mod h1:BinMc/sQntlIE1frQmRFPUoPA1Zkr8VRgBdjWI2mNwc=
github.com/multiformats/go-multicodec v0.9.0 h1:pb/dlPnzee/Sxv/j4PmkDRxCOi3hXTz3IbPKOXWJkmg=
github.com/multiformats/go-multicodec v0.9.0/go.mod h1:L3QTQvMIaVBkXOXXtVmYE+LI16i14xuaojr/H7Ai54k=
github.com/multiformats/go-multihash v0.0.11 h1:yEyBxwoR/7vBM5NfLVXRnpQNVLrMhpS6MRb7Z/1pnzc=
github.com/multiformats/go-multihash v0.0.11/go.mod h1:LXRDJcYYY+9BjlsFe6i5LV7uekf0OoEJdnRmitUshxk=
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
Expand Down
144 changes: 144 additions & 0 deletions vdr/didkey/resolver.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
/*
* Copyright (C) 2023 Nuts community
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/

package didkey

import (
"bytes"
"crypto"
"crypto/ecdsa"
"crypto/ed25519"
"crypto/elliptic"
"crypto/x509"
"encoding/binary"
"errors"
"fmt"
"github.com/lestrrat-go/jwx/x25519"
"github.com/multiformats/go-multicodec"
ssi "github.com/nuts-foundation/go-did"
"github.com/nuts-foundation/go-did/did"
"github.com/nuts-foundation/nuts-node/vdr/resolver"
"github.com/shengdoushi/base58"
"io"
)

// MethodName is the name of this DID method.
const MethodName = "key"

var _ resolver.DIDResolver = &Resolver{}

var errInvalidPublicKeyLength = errors.New("invalid did:key: invalid public key length")

// NewResolver creates a new Resolver.
func NewResolver() *Resolver {
return &Resolver{}
}

type Resolver struct {
}

func (r Resolver) Resolve(id did.DID, _ *resolver.ResolveMetadata) (*did.Document, *resolver.DocumentMetadata, error) {
if id.Method != MethodName {
return nil, nil, fmt.Errorf("unsupported DID method: %s", id.Method)
}
encodedKey := id.ID
if len(encodedKey) == 0 || encodedKey[0] != 'z' {
return nil, nil, errors.New("did:key does not start with 'z'")
}
mcBytes, err := base58.Decode(encodedKey[1:], base58.BitcoinAlphabet)
if err != nil {
return nil, nil, fmt.Errorf("did:key: invalid base58btc: %w", err)
}
reader := bytes.NewReader(mcBytes)
keyType, err := binary.ReadUvarint(reader)
if err != nil {
return nil, nil, fmt.Errorf("did:key: invalid multicodec value: %w", err)
}
// See https://w3c-ccg.github.io/did-method-key/#signature-method-creation-algorithm
var key crypto.PublicKey
mcBytes, _ = io.ReadAll(reader)
keyLength := len(mcBytes)

switch multicodec.Code(keyType) {
case multicodec.Bls12_381G2Pub:
return nil, nil, errors.New("did:key: bls12381 public keys are not supported")
case multicodec.X25519Pub:
if keyLength != 32 {
return nil, nil, errInvalidPublicKeyLength
}
key = x25519.PublicKey(mcBytes)
case multicodec.Ed25519Pub:
if keyLength != 32 {
return nil, nil, errInvalidPublicKeyLength
}
key = ed25519.PublicKey(mcBytes)
case multicodec.Secp256k1Pub:
// lestrrat/jwk.New() is missing support for secp256k1
return nil, nil, errors.New("did:key: secp256k1 public keys are not supported")
case multicodec.P256Pub:
if key, err = unmarshalEC(elliptic.P256(), 33, mcBytes); err != nil {
return nil, nil, err
}
case multicodec.P384Pub:
if key, err = unmarshalEC(elliptic.P384(), 49, mcBytes); err != nil {
return nil, nil, err
}
case multicodec.P521Pub:
key, _ = unmarshalEC(elliptic.P521(), -1, mcBytes)
case multicodec.RsaPub:
rsaKey, err := x509.ParsePKCS1PublicKey(mcBytes)
if err != nil {
return nil, nil, fmt.Errorf("did:key: invalid PKCS#1 encoded RSA public key: %w", err)
}
// Safe RSA keys must be at least 2048 bits
if rsaKey.Size() < 256 {
return nil, nil, errors.New("did:key: RSA public key is too small (must be at least 2048 bits)")
}
key = rsaKey
default:
return nil, nil, fmt.Errorf("did:key: unsupported public key type: 0x%x", keyType)
}

document := did.Document{
Context: []ssi.URI{
ssi.MustParseURI("https://w3c-ccg.github.io/lds-jws2020/contexts/lds-jws2020-v1.json"),
did.DIDContextV1URI(),
},
ID: id,
}
keyID := id
keyID.Fragment = id.ID
vm, err := did.NewVerificationMethod(keyID, ssi.JsonWebKey2020, id, key)
if err != nil {
return nil, nil, err
}
document.AddAssertionMethod(vm)
document.AddAuthenticationMethod(vm)
document.AddKeyAgreement(vm)
document.AddCapabilityDelegation(vm)
document.AddCapabilityInvocation(vm)
return &document, &resolver.DocumentMetadata{}, nil
}

func unmarshalEC(curve elliptic.Curve, expectedLen int, pubKeyBytes []byte) (ecdsa.PublicKey, error) {
if expectedLen != -1 && len(pubKeyBytes) != expectedLen {
return ecdsa.PublicKey{}, errInvalidPublicKeyLength
}
x, y := elliptic.UnmarshalCompressed(curve, pubKeyBytes)
return ecdsa.PublicKey{Curve: curve, X: x, Y: y}, nil
}
Loading

0 comments on commit a32e8cb

Please sign in to comment.