tpm-key_attestation 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +7 -0
- data/README.md +13 -2
- data/lib/tpm/certify_validator.rb +39 -0
- data/lib/tpm/key_attestation.rb +18 -9
- data/lib/tpm/key_attestation/version.rb +1 -1
- data/lib/tpm/public_area.rb +41 -0
- data/lib/tpm/s_attest.rb +0 -20
- data/lib/tpm/t_public.rb +90 -0
- data/lib/tpm/t_public/s_ecc_parms.rb +17 -0
- data/lib/tpm/t_public/s_rsa_parms.rb +17 -0
- metadata +7 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fbe3cdf38d6460a938f807e18343011b5b1222fd0a3451c40ca0675ea30e74b1
|
4
|
+
data.tar.gz: 7bad2c9779bc3c15cb591ca6ca7a54ac0c608727166056360c31d77bc5cc23fd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0ffae87bcd8326dcbaaad92e9d487843461ae07d67dc20b0934d1130de00b8c167553c072e8674a41214f330952b9b6bb39ada04d2f6aad2bf62c3748c056e14
|
7
|
+
data.tar.gz: 2f650b3bd139b0db1c41eccbc99cd59f9a4af01454b94b31672cfad58c253c5c4cfc36ffccabc561b651d0b1a7789dc94420c8b680245cd1215053328ca2ce18
|
data/CHANGELOG.md
CHANGED
@@ -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
|
-
|
28
|
-
TPM::
|
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
|
data/lib/tpm/key_attestation.rb
CHANGED
@@ -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
|
-
|
29
|
+
certify_validator.valid?(signing_key, hash_function)
|
23
30
|
end
|
24
31
|
|
25
32
|
private
|
26
33
|
|
27
|
-
def
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
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
|
36
|
-
@
|
44
|
+
def public_area
|
45
|
+
@public_area ||= TPM::PublicArea.new(certified_object)
|
37
46
|
end
|
38
47
|
end
|
39
48
|
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
|
data/lib/tpm/s_attest.rb
CHANGED
@@ -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
|
data/lib/tpm/t_public.rb
ADDED
@@ -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.
|
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-
|
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:
|