| 1 | package jose |
| 2 | |
| 3 | import ( |
| 4 | "crypto/ecdsa" |
| 5 | "crypto/elliptic" |
| 6 | "crypto/rand" |
| 7 | "crypto/sha256" |
| 8 | "errors" |
| 9 | "fmt" |
| 10 | "github.com/dvsekhvalnov/jose2go/arrays" |
| 11 | "github.com/dvsekhvalnov/jose2go/base64url" |
| 12 | "github.com/dvsekhvalnov/jose2go/kdf" |
| 13 | "github.com/dvsekhvalnov/jose2go/keys/ecc" |
| 14 | "github.com/dvsekhvalnov/jose2go/padding" |
| 15 | "math/big" |
| 16 | ) |
| 17 | |
| 18 | func init() { |
| 19 | RegisterJwa(&Ecdh{directAgreement: true}) |
| 20 | } |
| 21 | |
| 22 | // Elliptic curve Diffie–Hellman key management (key agreement) algorithm implementation |
| 23 | type Ecdh struct { |
| 24 | directAgreement bool |
| 25 | } |
| 26 | |
| 27 | func (alg *Ecdh) Name() string { |
| 28 | return ECDH_ES |
| 29 | } |
| 30 | |
| 31 | func (alg *Ecdh) WrapNewKey(cekSizeBits int, key interface{}, header map[string]interface{}) (cek []byte, encryptedCek []byte, err error) { |
| 32 | |
| 33 | if pubKey, ok := key.(*ecdsa.PublicKey); ok { |
| 34 | |
| 35 | if _, ok := header[alg.idHeader()].(string); !ok { |
| 36 | return nil, nil, errors.New(fmt.Sprintf("Ecdh.WrapNewKey(): expected '%v' param in JWT header, but was not found.", alg.idHeader())) |
| 37 | } |
| 38 | |
| 39 | var d []byte |
| 40 | var x, y *big.Int |
| 41 | |
| 42 | if d, x, y, err = elliptic.GenerateKey(pubKey.Curve, rand.Reader); err != nil { |
| 43 | return nil, nil, err |
| 44 | } |
| 45 | |
| 46 | ephemeral := ecc.NewPrivate(x.Bytes(), y.Bytes(), d) |
| 47 | |
| 48 | xBytes := padding.Align(x.Bytes(), pubKey.Curve.Params().BitSize) |
| 49 | yBytes := padding.Align(y.Bytes(), pubKey.Curve.Params().BitSize) |
| 50 | |
| 51 | epk := map[string]string{ |
| 52 | "kty": "EC", |
| 53 | "x": base64url.Encode(xBytes), |
| 54 | "y": base64url.Encode(yBytes), |
| 55 | "crv": name(pubKey.Curve), |
| 56 | } |
| 57 | |
| 58 | header["epk"] = epk |
| 59 | |
| 60 | return alg.deriveKey(pubKey, ephemeral, cekSizeBits, header), nil, nil |
| 61 | } |
| 62 | |
| 63 | return nil, nil, errors.New("Ecdh.WrapNewKey(): expected key to be '*ecdsa.PublicKey'") |
| 64 | } |
| 65 | |
| 66 | func (alg *Ecdh) Unwrap(encryptedCek []byte, key interface{}, cekSizeBits int, header map[string]interface{}) (cek []byte, err error) { |
| 67 | |
| 68 | if privKey, ok := key.(*ecdsa.PrivateKey); ok { |
| 69 | |
| 70 | var epk map[string]interface{} |
| 71 | |
| 72 | if epk, ok = header["epk"].(map[string]interface{}); !ok { |
| 73 | return nil, errors.New("Ecdh.Unwrap(): expected 'epk' param in JWT header, but was not found.") |
| 74 | } |
| 75 | |
| 76 | if _, ok := header[alg.idHeader()].(string); !ok { |
| 77 | return nil, errors.New(fmt.Sprintf("Ecdh.Unwrap(): expected '%v' param in JWT header, but was not found.", alg.idHeader())) |
| 78 | } |
| 79 | |
| 80 | var x, y, crv string |
| 81 | var xBytes, yBytes []byte |
| 82 | |
| 83 | if x, ok = epk["x"].(string); !ok { |
| 84 | return nil, errors.New("Ecdh.Unwrap(): expects 'epk' key to contain 'x','y' and 'crv' fields, but 'x' was not found.") |
| 85 | } |
| 86 | |
| 87 | if y, ok = epk["y"].(string); !ok { |
| 88 | return nil, errors.New("Ecdh.Unwrap(): expects 'epk' key to contain 'x','y' and 'crv' fields, but 'y' was not found.") |
| 89 | } |
| 90 | |
| 91 | if crv, ok = epk["crv"].(string); !ok { |
| 92 | return nil, errors.New("Ecdh.Unwrap(): expects 'epk' key to contain 'x','y' and 'crv' fields, but 'crv' was not found.") |
| 93 | } |
| 94 | |
| 95 | if crv != "P-256" && crv != "P-384" && crv != "P-521" { |
| 96 | return nil, errors.New(fmt.Sprintf("Ecdh.Unwrap(): unknown or unsupported curve %v", crv)) |
| 97 | } |
| 98 | |
| 99 | if xBytes, err = base64url.Decode(x); err != nil { |
| 100 | return nil, err |
| 101 | } |
| 102 | if yBytes, err = base64url.Decode(y); err != nil { |
| 103 | return nil, err |
| 104 | } |
| 105 | |
| 106 | pubKey := ecc.NewPublic(xBytes, yBytes) |
| 107 | |
| 108 | if !privKey.Curve.IsOnCurve(pubKey.X, pubKey.Y) { |
| 109 | return nil, errors.New(fmt.Sprintf("Ephemeral public key received in header is invalid for reciever's private key.")) |
| 110 | } |
| 111 | |
| 112 | return alg.deriveKey(pubKey, privKey, cekSizeBits, header), nil |
| 113 | } |
| 114 | |
| 115 | return nil, errors.New("Ecdh.Unwrap(): expected key to be '*ecdsa.PrivateKey'") |
| 116 | } |
| 117 | |
| 118 | func (alg *Ecdh) deriveKey(pubKey *ecdsa.PublicKey, privKey *ecdsa.PrivateKey, keySizeBits int, header map[string]interface{}) []byte { |
| 119 | |
| 120 | var enc, apv, apu []byte |
| 121 | var err error |
| 122 | |
| 123 | enc = []byte(header[alg.idHeader()].(string)) |
| 124 | |
| 125 | if a, ok := header["apv"].(string); !ok { |
| 126 | if apv, err = base64url.Decode(a); err != nil { |
| 127 | apv = nil |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | if a, ok := header["apu"].(string); !ok { |
| 132 | if apu, err = base64url.Decode(a); err != nil { |
| 133 | apu = nil |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | z, _ := pubKey.Curve.ScalarMult(pubKey.X, pubKey.Y, privKey.D.Bytes()) |
| 138 | zBytes := padding.Align(z.Bytes(), privKey.Curve.Params().BitSize) |
| 139 | |
| 140 | return kdf.DeriveConcatKDF(keySizeBits, zBytes, prependDatalen(enc), prependDatalen(apu), prependDatalen(apv), arrays.UInt32ToBytes(uint32(keySizeBits)), nil, sha256.New()) |
| 141 | } |
| 142 | |
| 143 | func (alg *Ecdh) idHeader() string { |
| 144 | if alg.directAgreement { |
| 145 | return "enc" |
| 146 | } |
| 147 | |
| 148 | return "alg" |
| 149 | } |
| 150 | |
| 151 | func name(curve elliptic.Curve) string { |
| 152 | return fmt.Sprintf("P-%v", curve.Params().BitSize) |
| 153 | } |
| 154 | |
| 155 | func prependDatalen(bytes []byte) []byte { |
| 156 | return arrays.Concat(arrays.UInt32ToBytes(uint32(len(bytes))), bytes) |
| 157 | } |