File: rsa_spec.rb

package info (click to toggle)
ruby-jwt 3.1.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 876 kB
  • sloc: ruby: 5,550; makefile: 4
file content (285 lines) | stat: -rw-r--r-- 10,329 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# frozen_string_literal: true

RSpec.describe JWT::JWK::RSA do
  let(:rsa_key) { OpenSSL::PKey::RSA.new(2048) }

  describe '.new' do
    subject { described_class.new(keypair) }

    context 'when a keypair with both keys given' do
      let(:keypair) { rsa_key }
      it 'creates an instance of the class' do
        expect(subject).to be_a described_class
        expect(subject.private?).to eq true
      end
    end

    context 'when a keypair with only public key is given' do
      let(:keypair) { rsa_key.public_key }
      it 'creates an instance of the class' do
        expect(subject).to be_a described_class
        expect(subject.private?).to eq false
      end
    end
  end

  describe '#keypair' do
    subject(:jwk) { described_class.new(rsa_key) }

    it 'warns to stderr' do
      expect(jwk.keypair).to eq(rsa_key)
    end
  end

  describe '#export' do
    subject { described_class.new(keypair).export }

    context 'when keypair with private key is exported' do
      let(:keypair) { rsa_key }
      it 'returns a hash with the public parts of the key' do
        expect(subject).to be_a Hash
        expect(subject).to include(:kty, :n, :e, :kid)
        expect(subject).not_to include(:d, :p, :dp, :dq, :qi)
      end
    end

    context 'when keypair with public key is exported' do
      let(:keypair) { rsa_key.public_key }
      it 'returns a hash with the public parts of the key' do
        expect(subject).to be_a Hash
        expect(subject).to include(:kty, :n, :e, :kid)
        expect(subject).not_to include(:d, :p, :dp, :dq, :qi)
      end
    end

    context 'when unsupported keypair is given' do
      let(:keypair) { 'key' }
      it 'raises an error' do
        expect { subject }.to raise_error(ArgumentError)
      end
    end

    context 'when private key is requested' do
      subject { described_class.new(keypair).export(include_private: true) }
      let(:keypair) { rsa_key }
      it 'returns a hash with the public AND private parts of the key' do
        expect(subject).to be_a Hash
        expect(subject).to include(:kty, :n, :e, :kid, :d, :p, :q, :dp, :dq, :qi)
      end
    end
  end

  describe '.kid' do
    context 'when configuration says to use :rfc7638_thumbprint' do
      before do
        JWT.configuration.jwk.kid_generator_type = :rfc7638_thumbprint
      end

      it 'generates the kid based on the thumbprint' do
        expect(described_class.new(OpenSSL::PKey::RSA.new(2048)).kid.size).to eq(43)
      end
    end

    context 'when kid is given as a String parameter' do
      it 'uses the given kid' do
        expect(described_class.new(OpenSSL::PKey::RSA.new(2048), 'given').kid).to eq('given')
      end
    end

    context 'when kid is given in a hash parameter' do
      it 'uses the given kid' do
        expect(described_class.new(OpenSSL::PKey::RSA.new(2048), kid: 'given').kid).to eq('given')
      end
    end
  end

  describe '#verify' do
    let(:rsa) { described_class.new(rsa_key, alg: 'RS256') }
    let(:data) { 'data_to_sign' }
    let(:signature) { rsa.sign(data: data) }

    context 'when the signature is valid' do
      it 'returns true' do
        expect(rsa.verify(data: data, signature: signature)).to be(true)
      end
    end

    context 'when the signature is invalid' do
      it 'returns false' do
        expect(rsa.verify(data: data, signature: 'invalid_signature')).to be(false)
      end
    end

    context 'when the jwk is missing the alg header' do
      let(:rsa) { described_class.new(rsa_key) }
      it 'raises JWT::JWKError' do
        expect { rsa.verify(data: data, signature: 'signature') }.to raise_error(JWT::JWKError, 'Could not resolve the JWA, the "alg" parameter is missing')
      end
    end

    context 'when the jwk has an invalid alg header' do
      let(:rsa) { described_class.new(rsa_key, alg: 'INVALID') }
      it 'raises JWT::VerificationError' do
        expect { rsa.verify(data: data, signature: 'signature') }.to raise_error(JWT::VerificationError, 'Algorithm not supported')
      end
    end

    context 'when the jwk has none as the alg parameter' do
      let(:rsa) { described_class.new(rsa_key, alg: 'none') }
      it 'raises JWT::JWKError' do
        expect { rsa.verify(data: data, signature: 'signature') }.to raise_error(JWT::JWKError, 'none algorithm usage not supported via JWK')
      end
    end

    context 'when the jwk has HS256 as the alg parameter' do
      let(:rsa) { described_class.new(rsa_key, alg: 'HS256') }
      it 'raises JWT::DecodeError' do
        expect { rsa.verify(data: data, signature: 'signature') }.to raise_error(JWT::DecodeError, 'HMAC key expected to be a String')
      end
    end
  end

  describe '.common_parameters' do
    context 'when a common parameters hash is given' do
      it 'imports the common parameter' do
        expect(described_class.new(OpenSSL::PKey::RSA.new(2048), use: 'sig')[:use]).to eq('sig')
      end

      it 'converts string keys to symbol keys' do
        expect(described_class.new(OpenSSL::PKey::RSA.new(2048), { 'use' => 'sig' })[:use]).to eq('sig')
      end
    end
  end

  describe '.import' do
    subject { described_class.import(params) }
    let(:exported_key) { described_class.new(rsa_key).export }

    context 'when keypair is imported with symbol keys' do
      let(:params) { { kty: 'RSA', e: exported_key[:e], n: exported_key[:n] } }
      it 'returns a hash with the public parts of the key' do
        expect(subject).to be_a described_class
        expect(subject.private?).to eq false
        expect(subject.export).to eq(exported_key)
      end
    end

    context 'when keypair is imported with string keys from JSON' do
      let(:params) { { 'kty' => 'RSA', 'e' => exported_key[:e], 'n' => exported_key[:n] } }
      it 'returns a hash with the public parts of the key' do
        expect(subject).to be_a described_class
        expect(subject.private?).to eq false
        expect(subject.export).to eq(exported_key)
      end
    end

    context 'when private key is included in the data' do
      let(:exported_key) { described_class.new(rsa_key).export(include_private: true) }
      let(:params) { exported_key }
      it 'creates a complete keypair' do
        expect(subject).to be_a described_class
        expect(subject.private?).to eq true
      end
    end

    context 'when jwk_data is given without e and/or n' do
      let(:params) { { kty: 'RSA' } }
      it 'raises an error' do
        expect { subject }.to raise_error(JWT::JWKError, 'Key format is invalid for RSA')
      end
    end
  end

  shared_examples 'creating an RSA object from complete JWK parameters' do
    let(:rsa_parameters) { jwk_parameters.transform_values { |value| described_class.decode_open_ssl_bn(value) } }
    let(:all_jwk_parameters) { described_class.new(rsa_key).export(include_private: true) }

    context 'when public parameters (e, n) are given' do
      let(:jwk_parameters) { all_jwk_parameters.slice(:e, :n) }

      it 'creates a valid RSA object representing a public key' do
        expect(subject).to be_a(OpenSSL::PKey::RSA)
        expect(subject.private?).to eq(false)
      end
    end

    context 'when only e, n, d, p and q are given' do
      let(:jwk_parameters) { all_jwk_parameters.slice(:e, :n, :d, :p, :q) }

      it 'raises an error telling all the exponents are required' do
        expect { subject }.to raise_error(JWT::JWKError, 'When one of p, q, dp, dq or qi is given all the other optimization parameters also needs to be defined')
      end
    end

    context 'when all key components n, e, d, p, q, dp, dq, qi are given' do
      let(:jwk_parameters) { all_jwk_parameters.slice(:n, :e, :d, :p, :q, :dp, :dq, :qi) }

      it 'creates a valid RSA object representing a public key' do
        expect(subject).to be_a(OpenSSL::PKey::RSA)
        expect(subject.private?).to eq(true)
      end
    end
  end

  shared_examples 'creating an RSA object from partial JWK parameters' do
    context 'when e, n, d is given' do
      let(:jwk_parameters) { all_jwk_parameters.slice(:e, :n, :d) }

      before do
        skip 'OpenSSL prior to 2.2 does not seem to support partial parameters' if JWT.openssl_version < Gem::Version.new('2.2')
      end

      it 'creates a valid RSA object representing a private key' do
        expect(subject).to be_a(OpenSSL::PKey::RSA)
        expect(subject.private?).to eq(true)
      end

      it 'can be used for encryption and decryption' do
        expect(subject.private_decrypt(subject.public_encrypt('secret'))).to eq('secret')
      end

      it 'can be used for signing and verification' do
        data = 'data_to_sign'
        signature = subject.sign(OpenSSL::Digest.new('SHA512'), data)
        expect(subject.verify(OpenSSL::Digest.new('SHA512'), signature, data)).to eq(true)
      end
    end
  end

  describe '.create_rsa_key_using_der' do
    subject(:rsa) { described_class.create_rsa_key_using_der(rsa_parameters) }

    include_examples 'creating an RSA object from complete JWK parameters'

    context 'when e, n, d is given' do
      let(:jwk_parameters) { all_jwk_parameters.slice(:e, :n, :d) }

      it 'expects all CRT parameters given and raises error' do
        expect { subject }.to raise_error(JWT::JWKError, 'Creating a RSA key with a private key requires the CRT parameters to be defined')
      end
    end
  end

  describe '.create_rsa_key_using_sets' do
    before do
      skip 'OpenSSL without the RSA#set_key method not supported' unless OpenSSL::PKey::RSA.new.respond_to?(:set_key)
      skip 'OpenSSL 3.0 does not allow mutating objects anymore' if JWT.openssl_3?
    end

    subject(:rsa) { described_class.create_rsa_key_using_sets(rsa_parameters) }

    include_examples 'creating an RSA object from complete JWK parameters'
    include_examples 'creating an RSA object from partial JWK parameters'
  end

  describe '.create_rsa_key_using_accessors' do
    before do
      skip 'OpenSSL if RSA#d= is not available there is no accessors anymore' unless OpenSSL::PKey::RSA.new.respond_to?(:d=)
    end

    subject(:rsa) { described_class.create_rsa_key_using_accessors(rsa_parameters) }

    include_examples 'creating an RSA object from complete JWK parameters'
    include_examples 'creating an RSA object from partial JWK parameters'
  end
end