| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package jose |
| 18 |
|
| 19 |
import ( |
| 20 |
"crypto/aes" |
| 21 |
"crypto/cipher" |
| 22 |
"crypto/hmac" |
| 23 |
"crypto/rand" |
| 24 |
"crypto/sha256" |
| 25 |
"crypto/sha512" |
| 26 |
"crypto/subtle" |
| 27 |
"errors" |
| 28 |
"hash" |
| 29 |
"io" |
| 30 |
|
| 31 |
"gopkg.in/square/go-jose.v1/cipher" |
| 32 |
) |
| 33 |
|
| 34 |
|
| 35 |
var randReader = rand.Reader |
| 36 |
|
| 37 |
|
| 38 |
type symmetricKeyCipher struct { |
| 39 |
key []byte |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
type symmetricMac struct { |
| 44 |
key []byte |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
type aeadParts struct { |
| 49 |
iv, ciphertext, tag []byte |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
type aeadContentCipher struct { |
| 54 |
keyBytes int |
| 55 |
authtagBytes int |
| 56 |
getAead func(key []byte) (cipher.AEAD, error) |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
type randomKeyGenerator struct { |
| 61 |
size int |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
type staticKeyGenerator struct { |
| 66 |
key []byte |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
func newAESGCM(keySize int) contentCipher { |
| 71 |
return &aeadContentCipher{ |
| 72 |
keyBytes: keySize, |
| 73 |
authtagBytes: 16, |
| 74 |
getAead: func(key []byte) (cipher.AEAD, error) { |
| 75 |
aes, err := aes.NewCipher(key) |
| 76 |
if err != nil { |
| 77 |
return nil, err |
| 78 |
} |
| 79 |
|
| 80 |
return cipher.NewGCM(aes) |
| 81 |
}, |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
func newAESCBC(keySize int) contentCipher { |
| 87 |
return &aeadContentCipher{ |
| 88 |
keyBytes: keySize * 2, |
| 89 |
authtagBytes: 16, |
| 90 |
getAead: func(key []byte) (cipher.AEAD, error) { |
| 91 |
return josecipher.NewCBCHMAC(key, aes.NewCipher) |
| 92 |
}, |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
func getContentCipher(alg ContentEncryption) contentCipher { |
| 98 |
switch alg { |
| 99 |
case A128GCM: |
| 100 |
return newAESGCM(16) |
| 101 |
case A192GCM: |
| 102 |
return newAESGCM(24) |
| 103 |
case A256GCM: |
| 104 |
return newAESGCM(32) |
| 105 |
case A128CBC_HS256: |
| 106 |
return newAESCBC(16) |
| 107 |
case A192CBC_HS384: |
| 108 |
return newAESCBC(24) |
| 109 |
case A256CBC_HS512: |
| 110 |
return newAESCBC(32) |
| 111 |
default: |
| 112 |
return nil |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
func newSymmetricRecipient(keyAlg KeyAlgorithm, key []byte) (recipientKeyInfo, error) { |
| 118 |
switch keyAlg { |
| 119 |
case DIRECT, A128GCMKW, A192GCMKW, A256GCMKW, A128KW, A192KW, A256KW: |
| 120 |
default: |
| 121 |
return recipientKeyInfo{}, ErrUnsupportedAlgorithm |
| 122 |
} |
| 123 |
|
| 124 |
return recipientKeyInfo{ |
| 125 |
keyAlg: keyAlg, |
| 126 |
keyEncrypter: &symmetricKeyCipher{ |
| 127 |
key: key, |
| 128 |
}, |
| 129 |
}, nil |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
func newSymmetricSigner(sigAlg SignatureAlgorithm, key []byte) (recipientSigInfo, error) { |
| 134 |
|
| 135 |
switch sigAlg { |
| 136 |
case HS256, HS384, HS512: |
| 137 |
default: |
| 138 |
return recipientSigInfo{}, ErrUnsupportedAlgorithm |
| 139 |
} |
| 140 |
|
| 141 |
return recipientSigInfo{ |
| 142 |
sigAlg: sigAlg, |
| 143 |
signer: &symmetricMac{ |
| 144 |
key: key, |
| 145 |
}, |
| 146 |
}, nil |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
func (ctx randomKeyGenerator) genKey() ([]byte, rawHeader, error) { |
| 151 |
key := make([]byte, ctx.size) |
| 152 |
_, err := io.ReadFull(randReader, key) |
| 153 |
if err != nil { |
| 154 |
return nil, rawHeader{}, err |
| 155 |
} |
| 156 |
|
| 157 |
return key, rawHeader{}, nil |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
func (ctx randomKeyGenerator) keySize() int { |
| 162 |
return ctx.size |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
func (ctx staticKeyGenerator) genKey() ([]byte, rawHeader, error) { |
| 167 |
cek := make([]byte, len(ctx.key)) |
| 168 |
copy(cek, ctx.key) |
| 169 |
return cek, rawHeader{}, nil |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
func (ctx staticKeyGenerator) keySize() int { |
| 174 |
return len(ctx.key) |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
func (ctx aeadContentCipher) keySize() int { |
| 179 |
return ctx.keyBytes |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
func (ctx aeadContentCipher) encrypt(key, aad, pt []byte) (*aeadParts, error) { |
| 184 |
|
| 185 |
aead, err := ctx.getAead(key) |
| 186 |
if err != nil { |
| 187 |
return nil, err |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
iv := make([]byte, aead.NonceSize()) |
| 192 |
_, err = io.ReadFull(randReader, iv) |
| 193 |
if err != nil { |
| 194 |
return nil, err |
| 195 |
} |
| 196 |
|
| 197 |
ciphertextAndTag := aead.Seal(nil, iv, pt, aad) |
| 198 |
offset := len(ciphertextAndTag) - ctx.authtagBytes |
| 199 |
|
| 200 |
return &aeadParts{ |
| 201 |
iv: iv, |
| 202 |
ciphertext: ciphertextAndTag[:offset], |
| 203 |
tag: ciphertextAndTag[offset:], |
| 204 |
}, nil |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
func (ctx aeadContentCipher) decrypt(key, aad []byte, parts *aeadParts) ([]byte, error) { |
| 209 |
aead, err := ctx.getAead(key) |
| 210 |
if err != nil { |
| 211 |
return nil, err |
| 212 |
} |
| 213 |
|
| 214 |
if len(parts.iv) < aead.NonceSize() || len(parts.tag) < ctx.authtagBytes { |
| 215 |
return nil, ErrCryptoFailure |
| 216 |
} |
| 217 |
|
| 218 |
return aead.Open(nil, parts.iv, append(parts.ciphertext, parts.tag...), aad) |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
func (ctx *symmetricKeyCipher) encryptKey(cek []byte, alg KeyAlgorithm) (recipientInfo, error) { |
| 223 |
switch alg { |
| 224 |
case DIRECT: |
| 225 |
return recipientInfo{ |
| 226 |
header: &rawHeader{}, |
| 227 |
}, nil |
| 228 |
case A128GCMKW, A192GCMKW, A256GCMKW: |
| 229 |
aead := newAESGCM(len(ctx.key)) |
| 230 |
|
| 231 |
parts, err := aead.encrypt(ctx.key, []byte{}, cek) |
| 232 |
if err != nil { |
| 233 |
return recipientInfo{}, err |
| 234 |
} |
| 235 |
|
| 236 |
return recipientInfo{ |
| 237 |
header: &rawHeader{ |
| 238 |
Iv: newBuffer(parts.iv), |
| 239 |
Tag: newBuffer(parts.tag), |
| 240 |
}, |
| 241 |
encryptedKey: parts.ciphertext, |
| 242 |
}, nil |
| 243 |
case A128KW, A192KW, A256KW: |
| 244 |
block, err := aes.NewCipher(ctx.key) |
| 245 |
if err != nil { |
| 246 |
return recipientInfo{}, err |
| 247 |
} |
| 248 |
|
| 249 |
jek, err := josecipher.KeyWrap(block, cek) |
| 250 |
if err != nil { |
| 251 |
return recipientInfo{}, err |
| 252 |
} |
| 253 |
|
| 254 |
return recipientInfo{ |
| 255 |
encryptedKey: jek, |
| 256 |
header: &rawHeader{}, |
| 257 |
}, nil |
| 258 |
} |
| 259 |
|
| 260 |
return recipientInfo{}, ErrUnsupportedAlgorithm |
| 261 |
} |
| 262 |
|
| 263 |
|
| 264 |
func (ctx *symmetricKeyCipher) decryptKey(headers rawHeader, recipient *recipientInfo, generator keyGenerator) ([]byte, error) { |
| 265 |
switch KeyAlgorithm(headers.Alg) { |
| 266 |
case DIRECT: |
| 267 |
cek := make([]byte, len(ctx.key)) |
| 268 |
copy(cek, ctx.key) |
| 269 |
return cek, nil |
| 270 |
case A128GCMKW, A192GCMKW, A256GCMKW: |
| 271 |
aead := newAESGCM(len(ctx.key)) |
| 272 |
|
| 273 |
parts := &aeadParts{ |
| 274 |
iv: headers.Iv.bytes(), |
| 275 |
ciphertext: recipient.encryptedKey, |
| 276 |
tag: headers.Tag.bytes(), |
| 277 |
} |
| 278 |
|
| 279 |
cek, err := aead.decrypt(ctx.key, []byte{}, parts) |
| 280 |
if err != nil { |
| 281 |
return nil, err |
| 282 |
} |
| 283 |
|
| 284 |
return cek, nil |
| 285 |
case A128KW, A192KW, A256KW: |
| 286 |
block, err := aes.NewCipher(ctx.key) |
| 287 |
if err != nil { |
| 288 |
return nil, err |
| 289 |
} |
| 290 |
|
| 291 |
cek, err := josecipher.KeyUnwrap(block, recipient.encryptedKey) |
| 292 |
if err != nil { |
| 293 |
return nil, err |
| 294 |
} |
| 295 |
return cek, nil |
| 296 |
} |
| 297 |
|
| 298 |
return nil, ErrUnsupportedAlgorithm |
| 299 |
} |
| 300 |
|
| 301 |
|
| 302 |
func (ctx symmetricMac) signPayload(payload []byte, alg SignatureAlgorithm) (Signature, error) { |
| 303 |
mac, err := ctx.hmac(payload, alg) |
| 304 |
if err != nil { |
| 305 |
return Signature{}, errors.New("square/go-jose: failed to compute hmac") |
| 306 |
} |
| 307 |
|
| 308 |
return Signature{ |
| 309 |
Signature: mac, |
| 310 |
protected: &rawHeader{}, |
| 311 |
}, nil |
| 312 |
} |
| 313 |
|
| 314 |
|
| 315 |
func (ctx symmetricMac) verifyPayload(payload []byte, mac []byte, alg SignatureAlgorithm) error { |
| 316 |
expected, err := ctx.hmac(payload, alg) |
| 317 |
if err != nil { |
| 318 |
return errors.New("square/go-jose: failed to compute hmac") |
| 319 |
} |
| 320 |
|
| 321 |
if len(mac) != len(expected) { |
| 322 |
return errors.New("square/go-jose: invalid hmac") |
| 323 |
} |
| 324 |
|
| 325 |
match := subtle.ConstantTimeCompare(mac, expected) |
| 326 |
if match != 1 { |
| 327 |
return errors.New("square/go-jose: invalid hmac") |
| 328 |
} |
| 329 |
|
| 330 |
return nil |
| 331 |
} |
| 332 |
|
| 333 |
|
| 334 |
func (ctx symmetricMac) hmac(payload []byte, alg SignatureAlgorithm) ([]byte, error) { |
| 335 |
var hash func() hash.Hash |
| 336 |
|
| 337 |
switch alg { |
| 338 |
case HS256: |
| 339 |
hash = sha256.New |
| 340 |
case HS384: |
| 341 |
hash = sha512.New384 |
| 342 |
case HS512: |
| 343 |
hash = sha512.New |
| 344 |
default: |
| 345 |
return nil, ErrUnsupportedAlgorithm |
| 346 |
} |
| 347 |
|
| 348 |
hmac := hmac.New(hash, ctx.key) |
| 349 |
|
| 350 |
|
| 351 |
_, _ = hmac.Write(payload) |
| 352 |
return hmac.Sum(nil), nil |
| 353 |
} |