|
91 | 91 | end
|
92 | 92 |
|
93 | 93 | shared_examples_for :verify_cbc_authentication_tag do
|
94 |
| - let(:jwe_string) do |
| 94 | + let(:jwe_parts) do |
95 | 95 | _jwe_ = JSON::JWE.new plain_text
|
96 | 96 | _jwe_.alg, _jwe_.enc = alg, enc
|
97 | 97 | _jwe_.encrypt! key
|
98 |
| - hdr, extra, iv, cipher_text, _ = _jwe_.to_s.split '.' |
99 |
| - [hdr, extra, iv, cipher_text, ''].join '.' |
| 98 | + _jwe_.to_s.split '.' |
100 | 99 | end
|
101 | 100 |
|
102 |
| - it do |
103 |
| - # fetching those variables outside of exception block to make sure |
104 |
| - # we intercept exception in decrypt! and not in other place |
105 |
| - j = jwe |
106 |
| - k = key |
107 |
| - expect do |
108 |
| - j.decrypt! k |
109 |
| - end.to raise_error JSON::JWE::DecryptionFailed |
| 101 | + let(:hdr) { jwe_parts[0] } |
| 102 | + let(:extra) { jwe_parts[1] } |
| 103 | + let(:iv) { jwe_parts[2] } |
| 104 | + let(:cipher_text) { jwe_parts[3] } |
| 105 | + let(:signature) { jwe_parts[4] } |
| 106 | + |
| 107 | + let(:jwe_string) { [hdr, extra, iv, cipher_text, signature].join '.' } |
| 108 | + |
| 109 | + shared_examples_for :signature_verification_failure do |
| 110 | + it do |
| 111 | + # fetching those variables outside of exception block to make sure |
| 112 | + # we intercept exception in decrypt! and not in other place |
| 113 | + j = jwe |
| 114 | + k = key |
| 115 | + expect do |
| 116 | + j.decrypt! k |
| 117 | + end.to raise_error JSON::JWE::DecryptionFailed |
| 118 | + end |
| 119 | + end |
| 120 | + |
| 121 | + describe "with missing signature" do |
| 122 | + let(:signature) { "" } |
| 123 | + it_behaves_like :signature_verification_failure |
| 124 | + end |
| 125 | + |
| 126 | + describe "with good pkcs7 padding and bad signature" do |
| 127 | + let(:iv) do |
| 128 | + good_padding_byte = 16 - (plain_text.bytesize % 16) |
| 129 | + bad_padding_byte = 1 |
| 130 | + iv_bytes = Base64.urlsafe_decode64(super()).bytes |
| 131 | + iv_bytes[-1] = iv_bytes[-1] ^ good_padding_byte ^ bad_padding_byte |
| 132 | + Base64.urlsafe_encode64(iv_bytes.pack("C*"), padding: false) |
| 133 | + end |
| 134 | + |
| 135 | + it_behaves_like :signature_verification_failure |
| 136 | + end |
| 137 | + |
| 138 | + describe "with bad pkcs7 padding and bad signature" do |
| 139 | + let(:iv) do |
| 140 | + good_padding_byte = 16 - (plain_text.bytesize % 16) |
| 141 | + bad_padding_byte = good_padding_byte - 1 |
| 142 | + iv_bytes = Base64.urlsafe_decode64(super()).bytes |
| 143 | + iv_bytes[-1] = iv_bytes[-1] ^ good_padding_byte ^ bad_padding_byte |
| 144 | + Base64.urlsafe_encode64(iv_bytes.pack("C*"), padding: false) |
| 145 | + end |
| 146 | + |
| 147 | + it_behaves_like :signature_verification_failure |
110 | 148 | end
|
111 | 149 | end
|
112 | 150 |
|
|
0 commit comments