| 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 |
var ephemeralCurve elliptic.Curve |
| 96 |
|
| 97 |
if ephemeralCurve, err = ecc.CurveByName(crv); err != nil { |
| 98 |
return nil, fmt.Errorf("Ecdh.Unwrap(): unknown or unsupported curve: '%v'", crv) |
| 99 |
} |
| 100 |
|
| 101 |
if xBytes, err = base64url.Decode(x); err != nil { |
| 102 |
return nil, err |
| 103 |
} |
| 104 |
if yBytes, err = base64url.Decode(y); err != nil { |
| 105 |
return nil, err |
| 106 |
} |
| 107 |
|
| 108 |
pubKey := ecc.NewPublic(xBytes, yBytes) |
| 109 |
|
| 110 |
if pubKey.Curve==nil { |
| 111 |
return nil, errors.New("Ecdh.Unwrap(): Ephemeral public key received in header is not matching any known curve.") |
| 112 |
} |
| 113 |
|
| 114 |
if !ephemeralCurve.IsOnCurve(pubKey.X, pubKey.Y) { |
| 115 |
return nil, errors.New(fmt.Sprintf("Ecdh.Unwrap(): Ephemeral public key received in header is not matching its curve.")) |
| 116 |
} |
| 117 |
|
| 118 |
if !privKey.Curve.IsOnCurve(pubKey.X, pubKey.Y) { |
| 119 |
return nil, errors.New(fmt.Sprintf("Ecdh.Unwrap(): Ephemeral public key received in header is invalid for reciever's private key.")) |
| 120 |
} |
| 121 |
|
| 122 |
return alg.deriveKey(pubKey, privKey, cekSizeBits, header), nil |
| 123 |
} |
| 124 |
|
| 125 |
return nil, errors.New("Ecdh.Unwrap(): expected key to be '*ecdsa.PrivateKey'") |
| 126 |
} |
| 127 |
|
| 128 |
func (alg *Ecdh) deriveKey(pubKey *ecdsa.PublicKey, privKey *ecdsa.PrivateKey, keySizeBits int, header map[string]interface{}) []byte { |
| 129 |
|
| 130 |
var enc, apv, apu []byte |
| 131 |
var err error |
| 132 |
|
| 133 |
enc = []byte(header[alg.idHeader()].(string)) |
| 134 |
|
| 135 |
if a, ok := header["apv"].(string); !ok { |
| 136 |
if apv, err = base64url.Decode(a); err != nil { |
| 137 |
apv = nil |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
if a, ok := header["apu"].(string); !ok { |
| 142 |
if apu, err = base64url.Decode(a); err != nil { |
| 143 |
apu = nil |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
z, _ := pubKey.Curve.ScalarMult(pubKey.X, pubKey.Y, privKey.D.Bytes()) |
| 148 |
zBytes := padding.Align(z.Bytes(), privKey.Curve.Params().BitSize) |
| 149 |
|
| 150 |
return kdf.DeriveConcatKDF(keySizeBits, zBytes, prependDatalen(enc), prependDatalen(apu), prependDatalen(apv), arrays.UInt32ToBytes(uint32(keySizeBits)), nil, sha256.New()) |
| 151 |
} |
| 152 |
|
| 153 |
func (alg *Ecdh) idHeader() string { |
| 154 |
if alg.directAgreement { |
| 155 |
return "enc" |
| 156 |
} |
| 157 |
|
| 158 |
return "alg" |
| 159 |
} |
| 160 |
|
| 161 |
func name(curve elliptic.Curve) string { |
| 162 |
return fmt.Sprintf("P-%v", curve.Params().BitSize) |
| 163 |
} |
| 164 |
|
| 165 |
func prependDatalen(bytes []byte) []byte { |
| 166 |
return arrays.Concat(arrays.UInt32ToBytes(uint32(len(bytes))), bytes) |
| 167 |
} |