Class: Virgil::SDK::Client::CardValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/virgil/sdk/client/card_validator.rb

Overview

Class used for cards signatures validation.

Constant Summary collapse

SERVICE_CARD_ID =
'3e29d43373348cfb373b7eae189214dc01d7237765e572db685839b64adca853'
SERVICE_PUBLIC_KEY =
'LS0tLS1CRUdJTiBQVUJMSUMgS0VZLS0tLS0KTUNvd0JRWURLMlZ3QXlFQVlSNTAx'\
'a1YxdFVuZTJ1T2RrdzRrRXJSUmJKcmMyU3lhejVWMWZ1RytyVnM9Ci0tLS0tRU5E'\
'IFBVQkxJQyBLRVktLS0tLQo='

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(crypto) ⇒ CardValidator

Returns a new instance of CardValidator.



48
49
50
51
52
53
54
55
# File 'lib/virgil/sdk/client/card_validator.rb', line 48

def initialize(crypto)
  @crypto = crypto
  @public_key_bytes = Crypto::Bytes.from_base64(SERVICE_PUBLIC_KEY)
  @public_key = crypto.import_public_key(@public_key_bytes)
  @verifiers = {
      SERVICE_CARD_ID => @public_key
  }
end

Instance Attribute Details

#cryptoObject (readonly)

Returns the value of attribute crypto.



46
47
48
# File 'lib/virgil/sdk/client/card_validator.rb', line 46

def crypto
  @crypto
end

#verifiersObject (readonly)

Returns the value of attribute verifiers.



46
47
48
# File 'lib/virgil/sdk/client/card_validator.rb', line 46

def verifiers
  @verifiers
end

Instance Method Details

#add_verifier(card_id, public_key) ⇒ Object

Add signature verifier.

Args:
    card_id: Card identifier
    public_key: Public key used for signature verification.


62
63
64
# File 'lib/virgil/sdk/client/card_validator.rb', line 62

def add_verifier(card_id, public_key)
  @verifiers[card_id] = public_key
end

#is_valid?(card) ⇒ Boolean

Validates Card using verifiers.

Args:

card: Card for validation.

Returns:

True if card signatures are valid, false otherwise.

Returns:

  • (Boolean)


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
# File 'lib/virgil/sdk/client/card_validator.rb', line 72

def is_valid?(card)

  return true if (card.version == '3.0')

  if (card.nil? || !card.is_a?(Card) || card.snapshot.nil? || (card.signatures.nil? || card.signatures.empty?))
    return false
  end

  # add self signature verifier
  fingerprint = self.crypto.calculate_fingerprint(
      Crypto::Bytes.from_string(card.snapshot)
  )
  fingerprint_hex = fingerprint.to_hex
  return false if fingerprint_hex != card.id

  verifiers = self.verifiers.clone
  card_public_key = self.crypto.import_public_key(card.public_key)
  verifiers[fingerprint_hex] = card_public_key

  verifiers.each do |id, key|
    unless card.signatures.has_key?(id)
      return false
    end
    is_valid = self.crypto.verify(
        fingerprint.value,
        Crypto::Bytes.from_base64(card.signatures[id]),
        key
    )
    return false unless is_valid
  end
  true
end