Skip to content

Commit 8b9ccbc

Browse files
Merge pull request #41 from nicolastemciuc/nt--invalid-public-area-edge-cases
Handle incompatibility between `parameters` and `unique` in `TPublic`
2 parents 8ea5976 + d72d666 commit 8b9ccbc

File tree

3 files changed

+45
-28
lines changed

3 files changed

+45
-28
lines changed

Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
tpm-key_attestation (0.12.1)
4+
tpm-key_attestation (0.13.1)
55
bindata (~> 2.4)
66
openssl (> 2.0)
77
openssl-signature_algorithm (~> 1.0)

lib/tpm/t_public.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ def ecc_key
9999

100100
OpenSSL::PKey::EC.new(asn1.to_der)
101101
end
102+
rescue OpenSSL::PKey::EC::Point::Error
103+
nil
102104
end
103105

104106
def rsa_key

spec/tpm/key_attestation_spec.rb

Lines changed: 42 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -358,38 +358,53 @@
358358
end
359359

360360
context 'when ECDSA algorithm' do
361+
let(:root_key) { create_ecc_key(key_curve_id) }
362+
let(:attestation_key) { create_ecc_key(key_curve_id) }
363+
let(:attested_key) { create_ecc_key(key_curve_id) }
364+
let(:key_curve_id) { TPM::ECC_NIST_P256 }
365+
366+
let(:signature_algorithm) { TPM::ALG_ECDSA }
367+
let(:hash_algorithm) { TPM::ALG_SHA256 }
368+
let(:hash_function) { "SHA256" }
369+
370+
let(:certified_key) do
371+
t_public = TPM::TPublic.new
372+
t_public.alg_type = TPM::ALG_ECC
373+
t_public.name_alg = name_alg
374+
t_public.parameters.symmetric = TPM::ALG_NULL
375+
t_public.parameters.scheme = scheme
376+
t_public.parameters.curve_id = t_public_curve_id
377+
t_public.parameters.kdf = TPM::ALG_NULL
378+
379+
public_key_bytes = attested_key.public_key.to_bn.to_s(2)[1..-1]
380+
coordinate_length = public_key_bytes.size / 2
381+
t_public.unique.x.buffer = public_key_bytes[0..(coordinate_length - 1)]
382+
t_public.unique.y.buffer = public_key_bytes[coordinate_length..-1]
383+
384+
t_public.to_binary_s
385+
end
386+
361387
context "when the scheme parameter from pubArea is TPM_ALG_NULL" do
362-
let(:root_key) { create_ecc_key(curve_id) }
363-
let(:attestation_key) { create_ecc_key(curve_id) }
364-
let(:attested_key) { create_ecc_key(curve_id) }
388+
let(:scheme) { TPM::ALG_NULL }
365389

366-
let(:signature_algorithm) { TPM::ALG_ECDSA }
367-
let(:hash_algorithm) { TPM::ALG_SHA256 }
368-
let(:hash_function) { "SHA256" }
369-
370-
let(:certified_key) do
371-
t_public = TPM::TPublic.new
372-
t_public.alg_type = TPM::ALG_ECC
373-
t_public.name_alg = name_alg
374-
t_public.parameters.symmetric = TPM::ALG_NULL
375-
t_public.parameters.scheme = TPM::ALG_NULL
376-
t_public.parameters.curve_id = curve_id
377-
t_public.parameters.kdf = TPM::ALG_NULL
378-
379-
public_key_bytes = attested_key.public_key.to_bn.to_s(2)[1..-1]
380-
coordinate_length = public_key_bytes.size / 2
381-
t_public.unique.x.buffer = public_key_bytes[0..(coordinate_length - 1)]
382-
t_public.unique.y.buffer = public_key_bytes[coordinate_length..-1]
383-
384-
t_public.to_binary_s
390+
context "when t_public.parameters and t_public.unique are compatible" do
391+
let(:t_public_curve_id) { TPM::ECC_NIST_P256 }
392+
393+
it "returns a public ECDSA key with the correct properties" do
394+
expect(key_attestation.key).to be_a(OpenSSL::PKey::EC)
395+
expect(key_attestation.key.group.curve_name).to eq("prime256v1")
396+
expect(key_attestation.key.public_key).to eq(attested_key.public_key)
397+
end
385398
end
386399

387-
let(:curve_id) { TPM::ECC_NIST_P256 }
400+
context "when t_public.parameters and t_public.unique are incompatible" do
401+
# Make the curve in t_public.parameters different than the curve
402+
# used to generate the coordinates of the t_public.unique field
403+
let(:t_public_curve_id) { TPM::ECC_NIST_P384 }
388404

389-
it "returns a public ECDSA key with the correct properties" do
390-
expect(key_attestation.key).to be_a(OpenSSL::PKey::EC)
391-
expect(key_attestation.key.group.curve_name).to eq("prime256v1")
392-
expect(key_attestation.key.public_key).to eq(attested_key.public_key)
405+
it "returns nil" do
406+
expect(key_attestation.key).to be nil
407+
end
393408
end
394409
end
395410
end

0 commit comments

Comments
 (0)