Debian Codegraph @

attest/tpm_test.go

raw
1 package attest
2
3 import (
4 "crypto/rsa"
5 "crypto/x509"
6 "encoding/pem"
7 "fmt"
8 "testing"
9 )
10
11 // Created by downloading the base64-url encoded PEM data from
12 // https://ekop.intel.com/ekcertservice/WVEG2rRwkQ7m3RpXlUphgo6Y2HLxl18h6ZZkkOAdnBE%3D,
13 // extracting its public key, and formatting it to PEM using
14 //
15 // openssl x509 -in ekcert.pem -pubkey
16 //
17 // This is the public key from the EK cert that's used for testing tpm2-tools:
18 // https://github.com/tpm2-software/tpm2-tools/blob/master/test/integration/tests/getekcertificate.sh
19 var testIntelRSAKey = mustParseRSAKey(`-----BEGIN PUBLIC KEY-----
20 MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAwyDi8kSoYBqs8+AdJsZl
21 JJk1Vi3h2hl+nn8HbEaWE8+2U+mOwsOG/B0TPyyMbMM4tzLwsgi9g4qHej5bvD4d
22 QIToNcfIkGocBbTS0w/b68HbrZUPprFlvUtqhkYDFGFkwMT1nUiQEe8fko3upukA
23 YfPTdeVkYnMVHvYiJSCYvhpKsB3AoSInxgn9rOsRWvQI1Gk6b0mRl3RpWwwSvBih
24 /3EgpzN7L7XxlR2Lt/CU1bVUwRyVI7MHKf5keH0KE7nmMEiNq039hmNKUnDscvzF
25 pE3GeajzKTjdgZfina6Dn1tMoPXeJ8lSLCPFThws5XhZUlEYvURwsYGA7veK5CZ7
26 zQIDAQAB
27 -----END PUBLIC KEY-----`)
28
29 // Created by downloading the binary PEM data from
30 // https://ftpm.amd.com/pki/aia/D027B3CE6A9B6B56846D2B9935884A88
31 // extracting its public key, and formatting it to PEM using
32 //
33 // openssl x509 -in ekcert.crt -pubkey
34 //
35 // This public key is from the EK cert from a real AMD fTPM platform
36 var testAMDRSAKey = mustParseRSAKey(`-----BEGIN PUBLIC KEY-----
37 MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAo8ID3MRLZQWgq2WIK1qT
38 e2HQxzZgiDWn6Tzw6uQOoXI1iyO9pxIailRxll2NeK1lRVP/dEKCV+mGwv75T+y2
39 MmzpFhUY/O5RtEG8TiocDw6WkHRAJ9A9h1OMP+vD3mPClNoA9/ssB36/0ScmVtYR
40 0gRkL+cZkAT6qro7xz4eRKLt8KfX6OG/Y9kCfJsKDCtYbc4OavHSf11VgbBLtxm7
41 jSVE+pnO+x/om6qwACjZbU4qrq4PUbAxD1S9dJ2cZzKaYSsCA8wMIho0umYa3jGv
42 eptunXDcE993BlsUGjLNbXC4aWVEtgo9yu98gKqhYGFEx7Mtk5NYOvWoNvcUBe2L
43 2QIDAQAB
44 -----END PUBLIC KEY-----`)
45
46 func mustParseRSAKey(data string) *rsa.PublicKey {
47 pub, err := parseRSAKey(data)
48 if err != nil {
49 panic(err)
50 }
51 return pub
52 }
53
54 func parseRSAKey(data string) (*rsa.PublicKey, error) {
55 b, _ := pem.Decode([]byte(data))
56 if b == nil {
57 return nil, fmt.Errorf("failed to parse PEM key")
58 }
59 pub, err := x509.ParsePKIXPublicKey(b.Bytes)
60 if err != nil {
61 return nil, fmt.Errorf("parsing public key: %v", err)
62 }
63 if rsaPub, ok := pub.(*rsa.PublicKey); ok {
64 return rsaPub, nil
65 }
66 return nil, fmt.Errorf("expected *rsa.PublicKey, got %T", pub)
67 }
68
69 func TestIntelEKURL(t *testing.T) {
70 want := "https://ekop.intel.com/ekcertservice/WVEG2rRwkQ7m3RpXlUphgo6Y2HLxl18h6ZZkkOAdnBE%3D"
71 got := intelEKURL(testIntelRSAKey)
72 if got != want {
73 t.Fatalf("intelEKURL(), got=%q, want=%q", got, want)
74 }
75 }
76
77 func TestAMDEKURL(t *testing.T) {
78 want := "https://ftpm.amd.com/pki/aia/D027B3CE6A9B6B56846D2B9935884A88"
79 got := amdEKURL(testAMDRSAKey)
80 if got != want {
81 t.Fatalf("amdEKURL(), got=%q, want=%q", got, want)
82 }
83 }
84
85 func TestEKCertURL(t *testing.T) {
86 tests := []struct {
87 name string
88 manufacturer string
89 pub *rsa.PublicKey
90 want string
91 }{
92 {
93 name: "Intel",
94 manufacturer: manufacturerIntel,
95 pub: testIntelRSAKey,
96 want: "https://ekop.intel.com/ekcertservice/WVEG2rRwkQ7m3RpXlUphgo6Y2HLxl18h6ZZkkOAdnBE%3D",
97 },
98 {
99 name: "AMD",
100 manufacturer: manufacturerAMD,
101 pub: testAMDRSAKey,
102 want: "https://ftpm.amd.com/pki/aia/D027B3CE6A9B6B56846D2B9935884A88",
103 },
104 {
105 name: "None",
106 manufacturer: "None",
107 pub: testIntelRSAKey,
108 want: "",
109 },
110 }
111
112 for _, tc := range tests {
113 t.Run(tc.name, func(t *testing.T) {
114 got := ekCertURL(tc.pub, tc.manufacturer)
115 if got != tc.want {
116 t.Fatalf("ekCertURL() = %q, want %q", got, tc.want)
117 }
118 })
119 }
120 }