tpm-key_attestation 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7a70caca43d540853ceca86581030de8e86925e630510deb34e3af736e314b55
4
- data.tar.gz: c7b0ab0e81a6607b9d24b8a8a01aa0447f2b4281f961f0180d27dd4f8fc88e00
3
+ metadata.gz: fbe3cdf38d6460a938f807e18343011b5b1222fd0a3451c40ca0675ea30e74b1
4
+ data.tar.gz: 7bad2c9779bc3c15cb591ca6ca7a54ac0c608727166056360c31d77bc5cc23fd
5
5
  SHA512:
6
- metadata.gz: babb217a8144f5fc34ce1f2b8a02485390b1c744beb3ed7f5f80e656415ad70dd674b2a7917b3e4db778aa7e69b20318179609c15d793126028a0699b38932e2
7
- data.tar.gz: 16e3f16f0cbe9d9d3032f112416d276532563c1730959cc8c41539e750e7476f3bc4a7074671628524bb051a7be17c792209ff4d931ac1808f56e390225782eb
6
+ metadata.gz: 0ffae87bcd8326dcbaaad92e9d487843461ae07d67dc20b0934d1130de00b8c167553c072e8674a41214f330952b9b6bb39ada04d2f6aad2bf62c3748c056e14
7
+ data.tar.gz: 2f650b3bd139b0db1c41eccbc99cd59f9a4af01454b94b31672cfad58c253c5c4cfc36ffccabc561b651d0b1a7789dc94420c8b680245cd1215053328ca2ce18
@@ -1,5 +1,11 @@
1
1
  # Changelog
2
2
 
3
+ ## [v0.3.0] - 2020-01-20
4
+
5
+ ### Added
6
+
7
+ - `TPM::KeyAttestation#key` returns attested key as an instance of `OpenSSL::PKey::PKey`
8
+
3
9
  ## [v0.2.0] - 2020-01-16
4
10
 
5
11
  ### Added
@@ -13,5 +19,6 @@
13
19
  - `TPM::EKCertificate` wrapper
14
20
  - `TPM::SAttest` wrapper
15
21
 
22
+ [v0.3.0]: https://github.com/cedarcode/tpm-key_attestation/compare/v0.2.0...v0.3.0/
16
23
  [v0.2.0]: https://github.com/cedarcode/tpm-key_attestation/compare/v0.1.0...v0.2.0/
17
24
  [v0.1.0]: https://github.com/cedarcode/tpm-key_attestation/compare/57c926ef7e83830cee8d111fdc5ccaf99ab2e861...v0.1.0/
data/README.md CHANGED
@@ -24,8 +24,19 @@ Or install it yourself as:
24
24
  ## Usage
25
25
 
26
26
  ```ruby
27
- TPM::SAttest.dererialize(certify_info).valid?(certified_object, certified_extra_data_hash)
28
- TPM::EKCertificate.from_der(certificate_der).conformant?
27
+ key_attestation =
28
+ TPM::KeyAttestation.new(
29
+ certify_info,
30
+ signature,
31
+ certified_object,
32
+ signing_key,
33
+ hash_function,
34
+ quilifying_data
35
+ )
36
+
37
+ if key_attestation.valid?
38
+ key_attestation.key
39
+ end
29
40
  ```
30
41
 
31
42
  ## Development
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "tpm/constants"
4
+ require "tpm/public_area"
5
+ require "tpm/s_attest"
6
+
7
+ module TPM
8
+ class CertifyValidator
9
+ attr_reader :info, :signature, :nonce, :object
10
+
11
+ def initialize(info, signature, nonce, object)
12
+ @info = info
13
+ @signature = signature
14
+ @nonce = nonce
15
+ @object = object
16
+ end
17
+
18
+ def valid?(signing_key, hash_function)
19
+ valid_info? && valid_signature?(signing_key, hash_function)
20
+ end
21
+
22
+ private
23
+
24
+ def valid_info?
25
+ attest.attested_type == TPM::ST_ATTEST_CERTIFY &&
26
+ attest.extra_data.buffer == nonce &&
27
+ attest.magic == TPM::GENERATED_VALUE &&
28
+ attest.attested.name.buffer == TPM::PublicArea.new(object).name
29
+ end
30
+
31
+ def valid_signature?(signing_key, hash_function)
32
+ signing_key.verify(hash_function, signature, info)
33
+ end
34
+
35
+ def attest
36
+ @attest ||= TPM::SAttest.deserialize(info)
37
+ end
38
+ end
39
+ end
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "tpm/key_attestation/version"
4
+ require "tpm/certify_validator"
4
5
 
5
6
  module TPM
6
7
  class KeyAttestation
@@ -18,22 +19,30 @@ module TPM
18
19
  @qualifying_data = qualifying_data
19
20
  end
20
21
 
22
+ def key
23
+ if valid?
24
+ public_area.key
25
+ end
26
+ end
27
+
21
28
  def valid?
22
- valid_signature? && valid_certify_info?
29
+ certify_validator.valid?(signing_key, hash_function)
23
30
  end
24
31
 
25
32
  private
26
33
 
27
- def valid_signature?
28
- signing_key.verify(hash_function, signature, certify_info)
29
- end
30
-
31
- def valid_certify_info?
32
- s_attest.valid?(certified_object, qualifying_data)
34
+ def certify_validator
35
+ @certify_validator ||=
36
+ TPM::CertifyValidator.new(
37
+ certify_info,
38
+ signature,
39
+ qualifying_data,
40
+ certified_object
41
+ )
33
42
  end
34
43
 
35
- def s_attest
36
- @s_attest ||= ::TPM::SAttest.read(certify_info)
44
+ def public_area
45
+ @public_area ||= TPM::PublicArea.new(certified_object)
37
46
  end
38
47
  end
39
48
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module TPM
4
4
  class KeyAttestation
5
- VERSION = "0.2.0"
5
+ VERSION = "0.3.0"
6
6
  end
7
7
  end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "openssl"
4
+ require "tpm/t_public"
5
+
6
+ module TPM
7
+ TPM_TO_OPENSSL_HASH_ALG = {
8
+ TPM::ALG_SHA1 => "SHA1",
9
+ TPM::ALG_SHA256 => "SHA256"
10
+ }.freeze
11
+
12
+ class PublicArea
13
+ attr_reader :object
14
+
15
+ def initialize(object)
16
+ @object = object
17
+ end
18
+
19
+ def name
20
+ [name_alg].pack("n") + name_digest
21
+ end
22
+
23
+ def key
24
+ t_public.key
25
+ end
26
+
27
+ private
28
+
29
+ def name_digest
30
+ OpenSSL::Digest.digest(TPM_TO_OPENSSL_HASH_ALG[name_alg], object)
31
+ end
32
+
33
+ def name_alg
34
+ t_public.name_alg
35
+ end
36
+
37
+ def t_public
38
+ @t_public ||= TPM::TPublic.deserialize(object)
39
+ end
40
+ end
41
+ end
@@ -1,7 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "bindata"
4
- require "openssl"
5
4
  require "tpm/constants"
6
5
  require "tpm/sized_buffer"
7
6
  require "tpm/s_attest/s_certify_info"
@@ -9,11 +8,6 @@ require "tpm/s_attest/s_certify_info"
9
8
  module TPM
10
9
  # Section 10.12.8 in https://trustedcomputinggroup.org/wp-content/uploads/TPM-Rev-2.0-Part-2-Structures-01.38.pdf
11
10
  class SAttest < BinData::Record
12
- TPM_TO_OPENSSL_HASH_ALG = {
13
- ::TPM::ALG_SHA1 => "SHA1",
14
- ::TPM::ALG_SHA256 => "SHA256"
15
- }.freeze
16
-
17
11
  class << self
18
12
  alias_method :deserialize, :read
19
13
  end
@@ -32,19 +26,5 @@ module TPM
32
26
  choice :attested, selection: :attested_type do
33
27
  s_certify_info TPM::ST_ATTEST_CERTIFY
34
28
  end
35
-
36
- def valid?(attested_object, expected_extra_data)
37
- magic == TPM::GENERATED_VALUE &&
38
- valid_attested_object?(attested_object) &&
39
- extra_data.buffer == expected_extra_data
40
- end
41
-
42
- private
43
-
44
- def valid_attested_object?(attested_object)
45
- name_hash_alg = attested.name.buffer[0..1].unpack("n")[0]
46
-
47
- attested.name.buffer[2..-1] == OpenSSL::Digest.digest(TPM_TO_OPENSSL_HASH_ALG[name_hash_alg], attested_object)
48
- end
49
29
  end
50
30
  end
@@ -0,0 +1,90 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bindata"
4
+ require "tpm/constants"
5
+ require "tpm/sized_buffer"
6
+ require "tpm/t_public/s_ecc_parms"
7
+ require "tpm/t_public/s_rsa_parms"
8
+
9
+ module TPM
10
+ # Section 12.2.4 in https://trustedcomputinggroup.org/wp-content/uploads/TPM-Rev-2.0-Part-2-Structures-01.38.pdf
11
+ class TPublic < BinData::Record
12
+ BYTE_LENGTH = 8
13
+ CURVE_TPM_TO_OPENSSL = { TPM::ECC_NIST_P256 => "prime256v1" }.freeze
14
+
15
+ class << self
16
+ alias_method :deserialize, :read
17
+ end
18
+
19
+ endian :big
20
+
21
+ uint16 :alg_type
22
+ uint16 :name_alg
23
+
24
+ # :object_attributes
25
+ skip length: 4
26
+
27
+ sized_buffer :auth_policy
28
+
29
+ choice :parameters, selection: :alg_type do
30
+ s_ecc_parms TPM::ALG_ECC
31
+ s_rsa_parms TPM::ALG_RSA
32
+ end
33
+
34
+ choice :unique, selection: :alg_type do
35
+ sized_buffer TPM::ALG_ECC
36
+ sized_buffer TPM::ALG_RSA
37
+ end
38
+
39
+ def key
40
+ if parameters.symmetric == ::TPM::ALG_NULL
41
+ case alg_type
42
+ when TPM::ALG_ECC
43
+ ecc_key
44
+ when TPM::ALG_RSA
45
+ rsa_key
46
+ else
47
+ raise "Type #{alg_type} not supported"
48
+ end
49
+ end
50
+ end
51
+
52
+ private
53
+
54
+ def ecc_key
55
+ if parameters.scheme == TPM::ALG_ECDSA
56
+ curve = CURVE_TPM_TO_OPENSSL[parameters.curve_id]
57
+
58
+ if curve
59
+ group = OpenSSL::PKey::EC::Group.new(curve)
60
+ pkey = OpenSSL::PKey::EC.new(group)
61
+ public_key_bn = OpenSSL::BN.new("\x04" + unique.buffer.value, 2)
62
+ public_key_point = OpenSSL::PKey::EC::Point.new(group, public_key_bn)
63
+ pkey.public_key = public_key_point
64
+
65
+ pkey
66
+ end
67
+ end
68
+ end
69
+
70
+ def rsa_key
71
+ case parameters.scheme
72
+ when TPM::ALG_RSASSA, TPM::ALG_RSAPSS, TPM::ALG_NULL
73
+ n = unique.buffer.value
74
+
75
+ if parameters.key_bits / BYTE_LENGTH == n.size
76
+ key = OpenSSL::PKey::RSA.new(parameters.key_bits.value)
77
+ key.set_key(bn(n), nil, nil)
78
+
79
+ key.public_key
80
+ end
81
+ end
82
+ end
83
+
84
+ def bn(data)
85
+ if data
86
+ OpenSSL::BN.new(data, 2)
87
+ end
88
+ end
89
+ end
90
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bindata"
4
+
5
+ module TPM
6
+ class TPublic < BinData::Record
7
+ # Section 12.2.3.6 in https://trustedcomputinggroup.org/wp-content/uploads/TPM-Rev-2.0-Part-2-Structures-01.38.pdf
8
+ class SEccParms < BinData::Record
9
+ endian :big
10
+
11
+ uint16 :symmetric
12
+ uint16 :scheme
13
+ uint16 :curve_id
14
+ uint16 :kdf
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bindata"
4
+
5
+ module TPM
6
+ class TPublic < BinData::Record
7
+ # Section 12.2.3.5 in https://trustedcomputinggroup.org/wp-content/uploads/TPM-Rev-2.0-Part-2-Structures-01.38.pdf
8
+ class SRsaParms < BinData::Record
9
+ endian :big
10
+
11
+ uint16 :symmetric
12
+ uint16 :scheme
13
+ uint16 :key_bits
14
+ uint32 :exponent
15
+ end
16
+ end
17
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tpm-key_attestation
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gonzalo
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-01-16 00:00:00.000000000 Z
11
+ date: 2020-01-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bindata
@@ -48,13 +48,18 @@ files:
48
48
  - gemfiles/openssl_2_1.gemfile
49
49
  - gemfiles/openssl_default.gemfile
50
50
  - gemfiles/openssl_head.gemfile
51
+ - lib/tpm/certify_validator.rb
51
52
  - lib/tpm/constants.rb
52
53
  - lib/tpm/ek_certificate.rb
53
54
  - lib/tpm/key_attestation.rb
54
55
  - lib/tpm/key_attestation/version.rb
56
+ - lib/tpm/public_area.rb
55
57
  - lib/tpm/s_attest.rb
56
58
  - lib/tpm/s_attest/s_certify_info.rb
57
59
  - lib/tpm/sized_buffer.rb
60
+ - lib/tpm/t_public.rb
61
+ - lib/tpm/t_public/s_ecc_parms.rb
62
+ - lib/tpm/t_public/s_rsa_parms.rb
58
63
  - tpm-key_attestation.gemspec
59
64
  homepage: https://github.com/cedarcode/tpm-key_attestation
60
65
  licenses: