|
| 1 | +//go:build didcomm && vc20 |
| 2 | + |
| 3 | +// Package crypto provides HSM and external signer support for DIDComm operations. |
| 4 | +package crypto |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "crypto" |
| 9 | + "crypto/ecdsa" |
| 10 | + "crypto/ed25519" |
| 11 | + "crypto/elliptic" |
| 12 | + "encoding/base64" |
| 13 | + "encoding/json" |
| 14 | + "fmt" |
| 15 | + |
| 16 | + "github.com/lestrrat-go/jwx/v3/jwa" |
| 17 | + "github.com/lestrrat-go/jwx/v3/jwk" |
| 18 | + "github.com/lestrrat-go/jwx/v3/jws" |
| 19 | + |
| 20 | + vccrypto "vc/pkg/vc20/crypto" |
| 21 | +) |
| 22 | + |
| 23 | +// ExternalSigner wraps a VCSigner for use with DIDComm JWS operations. |
| 24 | +// This enables HSM-backed keys and other external signers to be used with DIDComm. |
| 25 | +type ExternalSigner struct { |
| 26 | + signer vccrypto.VCSigner |
| 27 | + kid string |
| 28 | + algorithm jwa.SignatureAlgorithm |
| 29 | +} |
| 30 | + |
| 31 | +// NewExternalSigner creates a DIDComm-compatible signer from a VCSigner. |
| 32 | +// The kid parameter should be the DID URL of the verification method. |
| 33 | +func NewExternalSigner(signer vccrypto.VCSigner, kid string) (*ExternalSigner, error) { |
| 34 | + alg, err := algorithmForVCSigner(signer) |
| 35 | + if err != nil { |
| 36 | + return nil, err |
| 37 | + } |
| 38 | + |
| 39 | + return &ExternalSigner{ |
| 40 | + signer: signer, |
| 41 | + kid: kid, |
| 42 | + algorithm: alg, |
| 43 | + }, nil |
| 44 | +} |
| 45 | + |
| 46 | +// algorithmForVCSigner determines the JWA algorithm from the VCSigner. |
| 47 | +func algorithmForVCSigner(signer vccrypto.VCSigner) (jwa.SignatureAlgorithm, error) { |
| 48 | + switch signer.Algorithm() { |
| 49 | + case "ES256": |
| 50 | + return jwa.ES256(), nil |
| 51 | + case "ES384": |
| 52 | + return jwa.ES384(), nil |
| 53 | + case "ES512": |
| 54 | + return jwa.ES512(), nil |
| 55 | + case "EdDSA", "Ed25519": |
| 56 | + return jwa.EdDSA(), nil |
| 57 | + default: |
| 58 | + return jwa.NoSignature(), fmt.Errorf("unsupported algorithm: %s", signer.Algorithm()) |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +// Sign creates a JWS using the external signer. |
| 63 | +func (es *ExternalSigner) Sign(ctx context.Context, plaintext []byte, opts SignOptions) ([]byte, error) { |
| 64 | + // Create a JWK from the public key for use in the header |
| 65 | + pubKeyJWK, err := es.publicJWK() |
| 66 | + if err != nil { |
| 67 | + return nil, fmt.Errorf("failed to create public key JWK: %w", err) |
| 68 | + } |
| 69 | + |
| 70 | + // Build protected header as a map |
| 71 | + header := map[string]interface{}{ |
| 72 | + "alg": es.algorithm.String(), |
| 73 | + } |
| 74 | + if es.kid != "" { |
| 75 | + header["kid"] = es.kid |
| 76 | + } |
| 77 | + |
| 78 | + // Create signing input: BASE64URL(header) || '.' || BASE64URL(payload) |
| 79 | + headerBytes, err := json.Marshal(header) |
| 80 | + if err != nil { |
| 81 | + return nil, fmt.Errorf("failed to marshal header: %w", err) |
| 82 | + } |
| 83 | + |
| 84 | + headerB64 := base64.RawURLEncoding.EncodeToString(headerBytes) |
| 85 | + payloadB64 := "" |
| 86 | + if !opts.Detached { |
| 87 | + payloadB64 = base64.RawURLEncoding.EncodeToString(plaintext) |
| 88 | + } |
| 89 | + |
| 90 | + signingInput := []byte(headerB64 + "." + payloadB64) |
| 91 | + |
| 92 | + // Hash the signing input according to the algorithm |
| 93 | + digest, err := hashForAlgorithm(es.algorithm, signingInput) |
| 94 | + if err != nil { |
| 95 | + return nil, fmt.Errorf("failed to hash signing input: %w", err) |
| 96 | + } |
| 97 | + |
| 98 | + // Sign using the external signer |
| 99 | + signature, err := es.signer.SignDigest(ctx, digest) |
| 100 | + if err != nil { |
| 101 | + return nil, fmt.Errorf("external signer failed: %w", err) |
| 102 | + } |
| 103 | + |
| 104 | + // Encode signature |
| 105 | + sigB64 := base64.RawURLEncoding.EncodeToString(signature) |
| 106 | + |
| 107 | + // Return compact serialization: header.payload.signature |
| 108 | + result := headerB64 + "." + payloadB64 + "." + sigB64 |
| 109 | + |
| 110 | + // Verify the signature works by using the public key |
| 111 | + _, err = jws.Verify([]byte(result), jws.WithKey(es.algorithm, pubKeyJWK)) |
| 112 | + if err != nil { |
| 113 | + return nil, fmt.Errorf("signature verification failed: %w", err) |
| 114 | + } |
| 115 | + |
| 116 | + return []byte(result), nil |
| 117 | +} |
| 118 | + |
| 119 | +// hashForAlgorithm computes the appropriate hash for the signing algorithm. |
| 120 | +func hashForAlgorithm(alg jwa.SignatureAlgorithm, data []byte) ([]byte, error) { |
| 121 | + var hashFunc crypto.Hash |
| 122 | + |
| 123 | + switch alg { |
| 124 | + case jwa.ES256(): |
| 125 | + hashFunc = crypto.SHA256 |
| 126 | + case jwa.ES384(): |
| 127 | + hashFunc = crypto.SHA384 |
| 128 | + case jwa.ES512(): |
| 129 | + hashFunc = crypto.SHA512 |
| 130 | + case jwa.EdDSA(): |
| 131 | + // EdDSA doesn't use pre-hashing - return the data as-is |
| 132 | + return data, nil |
| 133 | + default: |
| 134 | + return nil, fmt.Errorf("unsupported algorithm for hashing: %s", alg) |
| 135 | + } |
| 136 | + |
| 137 | + if !hashFunc.Available() { |
| 138 | + return nil, fmt.Errorf("hash function not available: %s", hashFunc) |
| 139 | + } |
| 140 | + |
| 141 | + h := hashFunc.New() |
| 142 | + h.Write(data) |
| 143 | + return h.Sum(nil), nil |
| 144 | +} |
| 145 | + |
| 146 | +// publicJWK creates a JWK from the signer's public key. |
| 147 | +func (es *ExternalSigner) publicJWK() (jwk.Key, error) { |
| 148 | + pubKey := es.signer.PublicKey() |
| 149 | + |
| 150 | + switch pk := pubKey.(type) { |
| 151 | + case *ecdsa.PublicKey: |
| 152 | + return jwk.Import(pk) |
| 153 | + case ed25519.PublicKey: |
| 154 | + return jwk.Import(pk) |
| 155 | + default: |
| 156 | + return nil, fmt.Errorf("unsupported public key type: %T", pubKey) |
| 157 | + } |
| 158 | +} |
| 159 | + |
| 160 | +// KeyID returns the key identifier. |
| 161 | +func (es *ExternalSigner) KeyID() string { |
| 162 | + return es.kid |
| 163 | +} |
| 164 | + |
| 165 | +// Algorithm returns the JWA signature algorithm. |
| 166 | +func (es *ExternalSigner) Algorithm() jwa.SignatureAlgorithm { |
| 167 | + return es.algorithm |
| 168 | +} |
| 169 | + |
| 170 | +// PublicKey returns the public key. |
| 171 | +func (es *ExternalSigner) PublicKey() crypto.PublicKey { |
| 172 | + return es.signer.PublicKey() |
| 173 | +} |
| 174 | + |
| 175 | +// SignMessage wraps Sign with DIDComm standard options. |
| 176 | +// This is a convenience method for typical DIDComm signing. |
| 177 | +func (es *ExternalSigner) SignMessage(ctx context.Context, message []byte) ([]byte, error) { |
| 178 | + return es.Sign(ctx, message, SignOptions{}) |
| 179 | +} |
| 180 | + |
| 181 | +// VCSignerAdapter wraps a pki.RawSigner to implement VCSigner. |
| 182 | +// This enables HSM keys configured through pki to be used with both VC20 and DIDComm. |
| 183 | +type VCSignerAdapter struct { |
| 184 | + algorithm string |
| 185 | + pubKey crypto.PublicKey |
| 186 | + signFn func(ctx context.Context, digest []byte) ([]byte, error) |
| 187 | +} |
| 188 | + |
| 189 | +// NewVCSignerFromRawSigner creates a VCSigner from a pki.RawSigner. |
| 190 | +// This bridges the pki HSM configuration to the VCSigner interface. |
| 191 | +func NewVCSignerFromECDSA(key *ecdsa.PrivateKey) vccrypto.VCSigner { |
| 192 | + return vccrypto.NewECDSAKeyWrapper(key) |
| 193 | +} |
| 194 | + |
| 195 | +// NewVCSignerFromEdDSA creates a VCSigner from an Ed25519 private key. |
| 196 | +func NewVCSignerFromEdDSA(key ed25519.PrivateKey) vccrypto.VCSigner { |
| 197 | + return vccrypto.NewEdDSAKeyWrapper(key) |
| 198 | +} |
| 199 | + |
| 200 | +// Algorithms returns the JWA algorithm for a given curve. |
| 201 | +func ECDSAAlgorithmForCurve(curve elliptic.Curve) string { |
| 202 | + switch curve { |
| 203 | + case elliptic.P256(): |
| 204 | + return "ES256" |
| 205 | + case elliptic.P384(): |
| 206 | + return "ES384" |
| 207 | + case elliptic.P521(): |
| 208 | + return "ES512" |
| 209 | + default: |
| 210 | + return "" |
| 211 | + } |
| 212 | +} |
0 commit comments