Class: U2F::FakeU2F

Inherits:
Object
  • Object
show all
Defined in:
lib/u2f/fake_u2f.rb

Constant Summary collapse

CURVE_NAME =
"prime256v1".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app_id, options = {}) ⇒ FakeU2F

Initialize a new FakeU2F device for use in tests.

app_id - The appId/origin this is being tested against. options - A Hash of optional parameters (optional).

:counter      - The initial counter for this device.
:key_handle   - The raw key-handle this device should use.
:cert_subject - The subject field for the certificate generated
                for this device.

Returns nothing.



16
17
18
19
20
21
# File 'lib/u2f/fake_u2f.rb', line 16

def initialize(app_id, options = {})
  @app_id = app_id
  @counter = options.fetch(:counter, 0)
  @key_handle_raw = options.fetch(:key_handle, SecureRandom.random_bytes(32))
  @cert_subject = options.fetch(:cert_subject, "/CN=U2FTest")
end

Instance Attribute Details

#app_idObject

Returns the value of attribute app_id.



4
5
6
# File 'lib/u2f/fake_u2f.rb', line 4

def app_id
  @app_id
end

#cert_subjectObject

Returns the value of attribute cert_subject.



4
5
6
# File 'lib/u2f/fake_u2f.rb', line 4

def cert_subject
  @cert_subject
end

#counterObject

Returns the value of attribute counter.



4
5
6
# File 'lib/u2f/fake_u2f.rb', line 4

def counter
  @counter
end

#key_handle_rawObject

Returns the value of attribute key_handle_raw.



4
5
6
# File 'lib/u2f/fake_u2f.rb', line 4

def key_handle_raw
  @key_handle_raw
end

Instance Method Details

#cert_rawObject

The raw device attestation certificate as returned in the registrationData field of a RegisterResponse Hash.

Returns a DER formatted certificate String.



67
68
69
# File 'lib/u2f/fake_u2f.rb', line 67

def cert_raw
  cert.to_der
end

#origin_public_key_rawObject

The appId specific public key as returned in the registrationData field of a RegisterResponse Hash.

Returns a binary formatted EC public key String.



59
60
61
# File 'lib/u2f/fake_u2f.rb', line 59

def origin_public_key_raw
  [origin_key.public_key.to_bn.to_s(16)].pack('H*')
end

#register_response(challenge, error = false) ⇒ Object

A registerResponse hash as returned by the u2f.register JavaScript API.

challenge - The challenge to sign. error - Boolean. Whether to return an error response (optional).

Returns a JSON encoded Hash String.



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/u2f/fake_u2f.rb', line 29

def register_response(challenge, error = false)
  if error
    JSON.dump(:errorCode => 4)
  else
    client_data_json = client_data(U2F::ClientData::REGISTRATION_TYP, challenge)
    JSON.dump(
      :registrationData => reg_registration_data(client_data_json),
      :clientData => U2F.urlsafe_encode64(client_data_json)
    )
  end
end

#sign_response(challenge) ⇒ Object

A SignResponse hash as returned by the u2f.sign JavaScript API.

challenge - The challenge to sign.

Returns a JSON encoded Hash String.



46
47
48
49
50
51
52
53
# File 'lib/u2f/fake_u2f.rb', line 46

def sign_response(challenge)
  client_data_json = client_data(U2F::ClientData::AUTHENTICATION_TYP, challenge)
  JSON.dump(
    :clientData => U2F.urlsafe_encode64(client_data_json),
    :keyHandle => U2F.urlsafe_encode64(key_handle_raw),
    :signatureData => auth_signature_data(client_data_json)
  )
end