|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "spec_helper" |
| 4 | + |
| 5 | +require "openssl" |
| 6 | +require "webauthn/attestation_statement/apple" |
| 7 | + |
| 8 | +RSpec.describe "Apple attestation" do |
| 9 | + describe "#valid?" do |
| 10 | + let(:credential_key) { create_ec_key } |
| 11 | + let(:root_key) { create_ec_key } |
| 12 | + let(:root_certificate) { create_root_certificate(root_key) } |
| 13 | + |
| 14 | + let(:cred_cert) do |
| 15 | + issue_certificate(root_certificate, root_key, credential_key, extensions: [cred_cert_extension]) |
| 16 | + end |
| 17 | + |
| 18 | + let(:statement) { WebAuthn::AttestationStatement::Apple.new("x5c" => [cred_cert.to_der]) } |
| 19 | + |
| 20 | + let(:authenticator_data_bytes) do |
| 21 | + WebAuthn::FakeAuthenticator::AuthenticatorData.new( |
| 22 | + rp_id_hash: OpenSSL::Digest.digest("SHA256", "RP"), |
| 23 | + credential: { id: "0".b * 16, public_key: credential_key.public_key } |
| 24 | + ).serialize |
| 25 | + end |
| 26 | + |
| 27 | + let(:authenticator_data) { WebAuthn::AuthenticatorData.deserialize(authenticator_data_bytes) } |
| 28 | + let(:client_data_hash) { OpenSSL::Digest::SHA256.digest({}.to_json) } |
| 29 | + |
| 30 | + let(:nonce) { Digest::SHA256.digest(authenticator_data.data + client_data_hash) } |
| 31 | + let(:cred_cert_extension) do |
| 32 | + OpenSSL::X509::Extension.new( |
| 33 | + "1.2.840.113635.100.8.2", |
| 34 | + OpenSSL::ASN1::Sequence.new( |
| 35 | + [OpenSSL::ASN1::Sequence.new([OpenSSL::ASN1::OctetString.new(nonce)])] |
| 36 | + ) |
| 37 | + ) |
| 38 | + end |
| 39 | + |
| 40 | + around do |example| |
| 41 | + silence_warnings do |
| 42 | + original_apple_certificate = WebAuthn::AttestationStatement::Apple::ROOT_CERTIFICATE |
| 43 | + WebAuthn::AttestationStatement::Apple::ROOT_CERTIFICATE = root_certificate |
| 44 | + example.run |
| 45 | + WebAuthn::AttestationStatement::Apple::ROOT_CERTIFICATE = original_apple_certificate |
| 46 | + end |
| 47 | + end |
| 48 | + |
| 49 | + it "works if everything's fine" do |
| 50 | + expect(statement.valid?(authenticator_data, client_data_hash)).to be_truthy |
| 51 | + end |
| 52 | + |
| 53 | + context "when nonce is invalid" do |
| 54 | + let(:nonce) { Digest::SHA256.digest("Invalid") } |
| 55 | + |
| 56 | + it "fails" do |
| 57 | + expect(statement.valid?(authenticator_data, client_data_hash)).to be_falsy |
| 58 | + end |
| 59 | + end |
| 60 | + |
| 61 | + context "when the credential public key is invalid" do |
| 62 | + let(:cred_cert) do |
| 63 | + issue_certificate(root_certificate, root_key, create_ec_key, extensions: [cred_cert_extension]) |
| 64 | + end |
| 65 | + |
| 66 | + it "fails" do |
| 67 | + expect(statement.valid?(authenticator_data, client_data_hash)).to be_falsy |
| 68 | + end |
| 69 | + end |
| 70 | + end |
| 71 | +end |
0 commit comments