Skip to content
This repository was archived by the owner on Feb 28, 2024. It is now read-only.

Commit d92969b

Browse files
* Adding support for AES CBC decryption
1 parent f608f7c commit d92969b

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

lib/mcapi/encryption/crypto/jwe-crypto.rb

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ def decrypt_data(encrypted_data:)
6868
if enc_method == "A256GCM"
6969
enc_string = "aes-256-gcm"
7070
elsif enc_method == "A128CBC-HS256"
71+
cek = cek.byteslice(16, cek.length)
7172
enc_string = "aes-128-cbc"
7273
else
7374
raise Exception, "Encryption method '#{enc_method}' not supported."
@@ -78,11 +79,12 @@ def decrypt_data(encrypted_data:)
7879
cipher.key = cek
7980
cipher.iv = iv
8081
cipher.padding = 0
81-
cipher.auth_data = encrypted_header
82-
cipher.auth_tag = cipher_tag
82+
if enc_method == "A256GCM"
83+
cipher.auth_data = encrypted_header
84+
cipher.auth_tag = cipher_tag
85+
end
8386

84-
plain_text = cipher.update(cipher_text) + cipher.final
85-
plain_text
87+
cipher.update(cipher_text) + cipher.final
8688
end
8789

8890
private
@@ -92,8 +94,7 @@ def compute_public_fingerprint
9294
end
9395

9496
def generate_header(alg, enc)
95-
header = { alg: alg, enc: enc, kid: @public_key_fingerprint, cty: 'application/json' }
96-
header
97+
{ alg: alg, enc: enc, kid: @public_key_fingerprint, cty: 'application/json' }
9798
end
9899

99100
def jwe_encode(payload)

test/test_jwe_encryption.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,11 @@ def test_decrypt_gcm
9292
assert_equal decrypted['body']['mapping']['customer_identifier'], 'CUST_12345'
9393
assert !decrypted['body']['encrypted_data']
9494
end
95+
96+
def test_decrypt_cbc
97+
resp = File.read('./test/mock/jwe-response-cbc.json')
98+
jwe = McAPI::Encryption::JweEncryption.new(@test_config)
99+
decrypted = JSON.parse(jwe.decrypt(resp))
100+
assert !decrypted['body']['encrypted_data']
101+
end
95102
end

0 commit comments

Comments
 (0)