-
Notifications
You must be signed in to change notification settings - Fork 53
/
authenticator_assertion_response_spec.rb
360 lines (305 loc) · 11.9 KB
/
authenticator_assertion_response_spec.rb
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
# frozen_string_literal: true
require "spec_helper"
require "support/seeds"
require "webauthn/attestation_statement/fido_u2f/public_key"
require "webauthn/authenticator_assertion_response"
require "webauthn/u2f_migrator"
RSpec.describe WebAuthn::AuthenticatorAssertionResponse do
let(:client) { WebAuthn::FakeClient.new(actual_origin, encoding: false) }
let!(:credential) { create_credential(client: client) }
let(:credential_public_key) { credential[1] }
let(:origin) { fake_origin }
let(:actual_origin) { origin }
let(:original_challenge) { fake_challenge }
let(:assertion) { client.get(challenge: original_challenge) }
let(:authenticator_data) { assertion["response"]["authenticatorData"] }
let(:assertion_response) do
WebAuthn::AuthenticatorAssertionResponse.new(
client_data_json: assertion["response"]["clientDataJSON"],
authenticator_data: authenticator_data,
signature: assertion["response"]["signature"]
)
end
before do
WebAuthn.configuration.origin = origin
end
context "when everything's in place" do
it "verifies" do
expect(
assertion_response.verify(original_challenge, public_key: credential_public_key, sign_count: 0)
).to be_truthy
end
it "is valid" do
expect(
assertion_response.valid?(original_challenge, public_key: credential_public_key, sign_count: 0)
).to be_truthy
end
end
# Gem version v1.11.0 and lower, used to behave so that Credential#public_key
# returned an EC P-256 uncompressed point.
#
# Because of https://github.com/cedarcode/webauthn-ruby/issues/137 this was changed
# and Credential#public_key started returning the unchanged COSE_Key formatted
# credentialPublicKey (as in https://www.w3.org/TR/webauthn/#credentialpublickey).
#
# Given that the credential public key is expected to be stored long-term by the gem
# user and later be passed as the public_key argument in the
# AuthenticatorAssertionResponse.verify call, we then need to support the two formats.
context "when everything's in place with the old public key format" do
it "verifies" do
old_format_key =
WebAuthn::AttestationStatement::FidoU2f::PublicKey
.new(credential_public_key)
.to_uncompressed_point
expect(assertion_response.verify(original_challenge, public_key: old_format_key, sign_count: 0)).to be_truthy
end
end
context "if signature was signed with a different key" do
let(:different_public_key) do
_different_id, different_public_key = create_credential(client: client)
different_public_key
end
it "is invalid" do
expect(assertion_response.valid?(original_challenge, public_key: different_public_key, sign_count: 0)).to be_falsy
end
it "doesn't verify" do
expect {
assertion_response.verify(original_challenge, public_key: different_public_key, sign_count: 0)
}.to raise_exception(WebAuthn::SignatureVerificationError)
end
end
describe "type validation" do
context "if type is create instead of get" do
before do
allow(client).to receive(:type_for).and_return("webauthn.create")
end
it "doesn't verify" do
expect {
assertion_response.verify(original_challenge, public_key: credential_public_key, sign_count: 0)
}.to raise_exception(WebAuthn::TypeVerificationError)
end
it "is invalid" do
expect(
assertion_response.valid?(original_challenge, public_key: credential_public_key, sign_count: 0)
).to be_falsy
end
end
end
describe "user present validation" do
let(:assertion) { client.get(challenge: original_challenge, user_present: false, user_verified: false) }
context "if user flags are off" do
it "doesn't verify" do
expect {
assertion_response.verify(original_challenge, public_key: credential_public_key, sign_count: 0)
}.to raise_exception(WebAuthn::UserPresenceVerificationError)
end
it "is invalid" do
expect(
assertion_response.valid?(original_challenge, public_key: credential_public_key, sign_count: 0)
).to be_falsy
end
end
end
describe "user verified validation" do
context "if user flags are off" do
let(:assertion) { client.get(challenge: original_challenge, user_present: true, user_verified: false) }
it "doesn't verify" do
expect {
assertion_response.verify(
original_challenge,
public_key: credential_public_key,
sign_count: 0,
user_verification: true
)
}.to raise_exception(WebAuthn::UserVerifiedVerificationError)
end
end
end
describe "challenge validation" do
context "if challenge doesn't match" do
it "doesn't verify" do
expect {
assertion_response.verify(fake_challenge, public_key: credential_public_key, sign_count: 0)
}.to raise_exception(WebAuthn::ChallengeVerificationError)
end
it "is invalid" do
expect(assertion_response.valid?(fake_challenge, public_key: credential_public_key, sign_count: 0)).to be_falsy
end
end
end
describe "origin validation" do
context "if origin doesn't match" do
let(:actual_origin) { "http://different-origin" }
it "doesn't verify" do
expect {
assertion_response.verify(original_challenge, public_key: credential_public_key, sign_count: 0)
}.to raise_exception(WebAuthn::OriginVerificationError)
end
it "is invalid" do
expect(
assertion_response.valid?(original_challenge, public_key: credential_public_key, sign_count: 0)
).to be_falsy
end
end
end
describe "tokenBinding validation" do
let(:client) { WebAuthn::FakeClient.new(actual_origin, token_binding: token_binding, encoding: false) }
context "it has stuff" do
let(:token_binding) { { status: "supported" } }
it "verifies" do
expect(
assertion_response.verify(original_challenge, public_key: credential_public_key, sign_count: 0)
).to be_truthy
end
it "is valid" do
expect(
assertion_response.valid?(original_challenge, public_key: credential_public_key, sign_count: 0)
).to be_truthy
end
end
context "has an invalid format" do
let(:token_binding) { "invalid token binding format" }
it "doesn't verify" do
expect {
assertion_response.verify(original_challenge, public_key: credential_public_key, sign_count: 0)
}.to raise_exception(WebAuthn::TokenBindingVerificationError)
end
it "isn't valid" do
expect(
assertion_response.valid?(original_challenge, public_key: credential_public_key, sign_count: 0)
).to be_falsy
end
end
end
describe "rp_id validation" do
before do
WebAuthn.configuration.rp_id = "different-rp_id"
end
context "if rp_id_hash doesn't match" do
it "doesn't verify" do
expect {
assertion_response.verify(original_challenge, public_key: credential_public_key, sign_count: 0)
}.to raise_exception(WebAuthn::RpIdVerificationError)
end
it "is invalid" do
expect(
assertion_response.valid?(original_challenge, public_key: credential_public_key, sign_count: 0)
).to be_falsy
end
end
context "when correct rp_id is explicitly given" do
it "verifies" do
expect(
assertion_response.verify(
original_challenge,
public_key: credential_public_key,
sign_count: 0,
rp_id: URI.parse(origin).host
)
).to be_truthy
end
it "is valid" do
expect(
assertion_response.valid?(
original_challenge,
public_key: credential_public_key,
sign_count: 0,
rp_id: URI.parse(origin).host
)
).to be_truthy
end
end
end
describe "sign_count validation" do
context "if authenticator does not support counter" do
let(:assertion) { client.get(challenge: original_challenge, sign_count: 0) }
it "verifies" do
expect(
assertion_response.verify(original_challenge, public_key: credential_public_key, sign_count: 0)
).to be_truthy
end
end
context "when the authenticator supports counter" do
context "and it's greater than the stored counter" do
let(:assertion) { client.get(challenge: original_challenge, sign_count: 6) }
it "verifies" do
expect(
assertion_response.verify(original_challenge, public_key: credential_public_key, sign_count: 5)
).to be_truthy
end
end
context "and it's equal to the stored counter" do
let(:assertion) { client.get(challenge: original_challenge, sign_count: 5) }
it "doesn't verify" do
expect {
assertion_response.verify(original_challenge, public_key: credential_public_key, sign_count: 5)
}.to raise_exception(WebAuthn::SignCountVerificationError)
end
end
context "and it's less than the stored counter" do
let(:assertion) { client.get(challenge: original_challenge, sign_count: 4) }
it "doesn't verify" do
expect {
assertion_response.verify(original_challenge, public_key: credential_public_key, sign_count: 5)
}.to raise_exception(WebAuthn::SignCountVerificationError)
end
end
context "when the RP opts out of verifying the signature counter" do
let(:assertion) { client.get(challenge: original_challenge, sign_count: false) }
it "verifies" do
expect {
assertion_response.verify(original_challenge, public_key: credential_public_key, sign_count: 5)
}.to raise_exception(WebAuthn::SignCountVerificationError)
end
end
end
end
describe "migrated U2F credential" do
let(:origin) { "https://f69df4d9.ngrok.io" }
let(:app_id) { "#{origin}/appid" }
let(:migrated_credential) do
WebAuthn::U2fMigrator.new(
app_id: app_id,
certificate: seeds[:u2f_migration][:stored_credential][:certificate],
key_handle: seeds[:u2f_migration][:stored_credential][:key_handle],
public_key: seeds[:u2f_migration][:stored_credential][:public_key],
counter: 41
)
end
let(:credential_public_key) { migrated_credential.credential.public_key }
let(:assertion_data) { seeds[:u2f_migration][:assertion] }
let(:assertion_response) do
WebAuthn::AuthenticatorAssertionResponse.new(
client_data_json: Base64.strict_decode64(assertion_data[:response][:client_data_json]),
authenticator_data: Base64.strict_decode64(assertion_data[:response][:authenticator_data]),
signature: Base64.strict_decode64(assertion_data[:response][:signature])
)
end
let(:original_challenge) { Base64.strict_decode64(assertion_data[:challenge]) }
context "when correct FIDO AppID is given as rp_id" do
it "verifies" do
expect(
assertion_response.verify(original_challenge, public_key: credential_public_key, sign_count: 0, rp_id: app_id)
).to be_truthy
end
it "is valid" do
expect(
assertion_response.valid?(original_challenge, public_key: credential_public_key, sign_count: 0, rp_id: app_id)
).to be_truthy
end
end
end
context "when Authenticator Data is invalid" do
let(:authenticator_data) { assertion["response"]["authenticatorData"][0..31] }
it "doesn't verify" do
expect {
assertion_response.verify(original_challenge, public_key: credential_public_key, sign_count: 0)
}.to raise_exception(WebAuthn::AuthenticatorDataVerificationError)
end
it "is invalid" do
expect(
assertion_response.valid?(original_challenge, public_key: credential_public_key, sign_count: 0)
).to be_falsy
end
end
end