| 1 |
package jose |
| 2 |
|
| 3 |
import ( |
| 4 |
"crypto/sha256" |
| 5 |
"crypto/sha512" |
| 6 |
"errors" |
| 7 |
"fmt" |
| 8 |
"hash" |
| 9 |
|
| 10 |
"github.com/dvsekhvalnov/jose2go/arrays" |
| 11 |
"github.com/dvsekhvalnov/jose2go/base64url" |
| 12 |
"github.com/dvsekhvalnov/jose2go/kdf" |
| 13 |
) |
| 14 |
|
| 15 |
func init() { |
| 16 |
RegisterJwa(NewPbse2HmacAesKWAlg(128, 1300000, 0)) |
| 17 |
RegisterJwa(NewPbse2HmacAesKWAlg(192, 950000, 0)) |
| 18 |
RegisterJwa(NewPbse2HmacAesKWAlg(256, 600000, 0)) |
| 19 |
} |
| 20 |
|
| 21 |
// PBSE2 with HMAC key management algorithm implementation |
| 22 |
type Pbse2HmacAesKW struct { |
| 23 |
keySizeBits int |
| 24 |
aesKW JwaAlgorithm |
| 25 |
maxIterations int64 |
| 26 |
minIterations int64 |
| 27 |
} |
| 28 |
|
| 29 |
func NewPbse2HmacAesKWAlg(keySize int, maxIters int64, minIters int64) JwaAlgorithm { |
| 30 |
switch keySize { |
| 31 |
case 128: |
| 32 |
return &Pbse2HmacAesKW{keySizeBits: 128, maxIterations: maxIters, minIterations: minIters, aesKW: &AesKW{keySizeBits: 128}} |
| 33 |
case 192: |
| 34 |
return &Pbse2HmacAesKW{keySizeBits: 192, maxIterations: maxIters, minIterations: minIters, aesKW: &AesKW{keySizeBits: 192}} |
| 35 |
default: |
| 36 |
return &Pbse2HmacAesKW{keySizeBits: 256, maxIterations: maxIters, minIterations: minIters, aesKW: &AesKW{keySizeBits: 256}} |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
func (alg *Pbse2HmacAesKW) Name() string { |
| 41 |
switch alg.keySizeBits { |
| 42 |
case 128: |
| 43 |
return PBES2_HS256_A128KW |
| 44 |
case 192: |
| 45 |
return PBES2_HS384_A192KW |
| 46 |
default: |
| 47 |
return PBES2_HS512_A256KW |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
func (alg *Pbse2HmacAesKW) WrapNewKey(cekSizeBits int, key interface{}, header map[string]interface{}) (cek []byte, encryptedCek []byte, err error) { |
| 52 |
if passphrase, ok := key.(string); ok { |
| 53 |
|
| 54 |
algId := []byte(header["alg"].(string)) |
| 55 |
|
| 56 |
iterationCount := 8192 |
| 57 |
var saltInput []byte |
| 58 |
|
| 59 |
if saltInput, err = arrays.Random(12); err != nil { |
| 60 |
return nil, nil, err |
| 61 |
} |
| 62 |
|
| 63 |
// use user provided iteration counts if any |
| 64 |
if p2c, ok := header["p2c"].(int); ok { |
| 65 |
iterationCount = p2c |
| 66 |
} |
| 67 |
|
| 68 |
if int64(iterationCount) > alg.maxIterations { |
| 69 |
return nil, nil, errors.New( |
| 70 |
fmt.Sprintf("Pbse2HmacAesKW.Unwrap(): expected 'p2c' to be less than %v but got %v", alg.maxIterations, iterationCount)) |
| 71 |
} |
| 72 |
|
| 73 |
if int64(iterationCount) < alg.minIterations { |
| 74 |
return nil, nil, errors.New( |
| 75 |
fmt.Sprintf("Pbse2HmacAesKW.Unwrap(): expected 'p2c' to be higher than %v but got %v", alg.minIterations, iterationCount)) |
| 76 |
} |
| 77 |
|
| 78 |
header["p2c"] = iterationCount |
| 79 |
header["p2s"] = base64url.Encode(saltInput) |
| 80 |
|
| 81 |
salt := arrays.Concat(algId, []byte{0}, saltInput) |
| 82 |
|
| 83 |
kek := kdf.DerivePBKDF2([]byte(passphrase), salt, iterationCount, alg.keySizeBits, alg.prf) |
| 84 |
return alg.aesKW.WrapNewKey(cekSizeBits, kek, header) |
| 85 |
} |
| 86 |
|
| 87 |
return nil, nil, errors.New("Pbse2HmacAesKW.WrapNewKey(): expected key to be 'string' array") |
| 88 |
} |
| 89 |
|
| 90 |
func (alg *Pbse2HmacAesKW) Unwrap(encryptedCek []byte, key interface{}, cekSizeBits int, header map[string]interface{}) (cek []byte, err error) { |
| 91 |
|
| 92 |
if passphrase, ok := key.(string); ok { |
| 93 |
|
| 94 |
var p2s string |
| 95 |
var p2c float64 |
| 96 |
|
| 97 |
if p2c, ok = header["p2c"].(float64); !ok { |
| 98 |
return nil, errors.New("Pbse2HmacAesKW.Unwrap(): expected 'p2c' param in JWT header, but was not found.") |
| 99 |
} |
| 100 |
|
| 101 |
if int64(p2c) > alg.maxIterations { |
| 102 |
return nil, errors.New( |
| 103 |
fmt.Sprintf("Pbse2HmacAesKW.Unwrap(): expected 'p2c' to be less than %v but got %v", alg.maxIterations, p2c)) |
| 104 |
} |
| 105 |
|
| 106 |
if int64(p2c) < alg.minIterations { |
| 107 |
return nil, errors.New( |
| 108 |
fmt.Sprintf("Pbse2HmacAesKW.Unwrap(): expected 'p2c' to be higher than %v but got %v", alg.minIterations, p2c)) |
| 109 |
} |
| 110 |
|
| 111 |
if p2s, ok = header["p2s"].(string); !ok { |
| 112 |
return nil, errors.New("Pbse2HmacAesKW.Unwrap(): expected 'p2s' param in JWT header, but was not found") |
| 113 |
} |
| 114 |
|
| 115 |
var saltInput []byte |
| 116 |
|
| 117 |
algId := []byte(header["alg"].(string)) |
| 118 |
|
| 119 |
if saltInput, err = base64url.Decode(p2s); err != nil { |
| 120 |
return nil, err |
| 121 |
} |
| 122 |
|
| 123 |
salt := arrays.Concat(algId, []byte{0}, saltInput) |
| 124 |
|
| 125 |
kek := kdf.DerivePBKDF2([]byte(passphrase), salt, int(p2c), alg.keySizeBits, alg.prf) |
| 126 |
|
| 127 |
return alg.aesKW.Unwrap(encryptedCek, kek, cekSizeBits, header) |
| 128 |
} |
| 129 |
|
| 130 |
return nil, errors.New("Pbse2HmacAesKW.Unwrap(): expected key to be 'string' array") |
| 131 |
} |
| 132 |
|
| 133 |
func (alg *Pbse2HmacAesKW) prf() hash.Hash { |
| 134 |
switch alg.keySizeBits { |
| 135 |
case 128: |
| 136 |
return sha256.New() |
| 137 |
case 192: |
| 138 |
return sha512.New384() |
| 139 |
default: |
| 140 |
return sha512.New() |
| 141 |
} |
| 142 |
} |