| 1 |
// Copyright 2019 Google Inc. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 4 |
// use this file except in compliance with the License. You may obtain a copy of |
| 5 |
// the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 |
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 |
// License for the specific language governing permissions and limitations under |
| 13 |
// the License. |
| 14 |
|
| 15 |
package attest |
| 16 |
|
| 17 |
import ( |
| 18 |
"bytes" |
| 19 |
"crypto" |
| 20 |
"flag" |
| 21 |
"fmt" |
| 22 |
"reflect" |
| 23 |
"testing" |
| 24 |
) |
| 25 |
|
| 26 |
var ( |
| 27 |
testLocal = flag.Bool("testLocal", false, "run tests against local hardware") |
| 28 |
) |
| 29 |
|
| 30 |
func TestOpen(t *testing.T) { |
| 31 |
if !*testLocal { |
| 32 |
t.SkipNow() |
| 33 |
} |
| 34 |
tpm, err := OpenTPM(nil) |
| 35 |
if err != nil { |
| 36 |
t.Fatalf("OpenTPM() failed: %v", err) |
| 37 |
} |
| 38 |
if tpm == nil { |
| 39 |
t.Fatalf("Expected non-nil tpm struct") |
| 40 |
} |
| 41 |
defer tpm.Close() |
| 42 |
} |
| 43 |
|
| 44 |
func TestInfo(t *testing.T) { |
| 45 |
if !*testLocal { |
| 46 |
t.SkipNow() |
| 47 |
} |
| 48 |
tpm, err := OpenTPM(nil) |
| 49 |
if err != nil { |
| 50 |
t.Fatalf("OpenTPM() failed: %v", err) |
| 51 |
} |
| 52 |
defer tpm.Close() |
| 53 |
|
| 54 |
info, err := tpm.Info() |
| 55 |
if err != nil { |
| 56 |
t.Errorf("tpm.Info() failed: %v", err) |
| 57 |
} |
| 58 |
if info.Manufacturer.String() == "" { |
| 59 |
t.Error("Expected info.Manufacturer.String() != ''") |
| 60 |
} |
| 61 |
t.Logf("TPM Info = %+v", info) |
| 62 |
} |
| 63 |
|
| 64 |
func TestEKs(t *testing.T) { |
| 65 |
if !*testLocal { |
| 66 |
t.SkipNow() |
| 67 |
} |
| 68 |
tpm, err := OpenTPM(nil) |
| 69 |
if err != nil { |
| 70 |
t.Fatalf("OpenTPM() failed: %v", err) |
| 71 |
} |
| 72 |
defer tpm.Close() |
| 73 |
|
| 74 |
eks, err := tpm.EKs() |
| 75 |
if err != nil { |
| 76 |
t.Errorf("EKs() failed: %v", err) |
| 77 |
} |
| 78 |
if len(eks) == 0 { |
| 79 |
t.Log("EKs() did not return anything. This could be an issue if an EK is present.") |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
func TestEKCertificates(t *testing.T) { |
| 84 |
if !*testLocal { |
| 85 |
t.SkipNow() |
| 86 |
} |
| 87 |
tpm, err := OpenTPM(nil) |
| 88 |
if err != nil { |
| 89 |
t.Fatalf("OpenTPM() failed: %v", err) |
| 90 |
} |
| 91 |
defer tpm.Close() |
| 92 |
|
| 93 |
eks, err := tpm.EKCertificates() |
| 94 |
if err != nil { |
| 95 |
t.Errorf("EKCertificates() failed: %v", err) |
| 96 |
} |
| 97 |
if len(eks) == 0 { |
| 98 |
t.Log("EKCertificates() did not return anything. This could be an issue if an EK is present.") |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
func TestAKCreateAndLoad(t *testing.T) { |
| 103 |
if !*testLocal { |
| 104 |
t.SkipNow() |
| 105 |
} |
| 106 |
for _, test := range []struct { |
| 107 |
name string |
| 108 |
opts *AKConfig |
| 109 |
}{ |
| 110 |
{ |
| 111 |
name: "NoConfig", |
| 112 |
opts: nil, |
| 113 |
}, |
| 114 |
{ |
| 115 |
name: "EmptyConfig", |
| 116 |
opts: &AKConfig{}, |
| 117 |
}, |
| 118 |
{ |
| 119 |
name: "RSA", |
| 120 |
opts: &AKConfig{Algorithm: RSA}, |
| 121 |
}, |
| 122 |
{ |
| 123 |
name: "ECDSA", |
| 124 |
opts: &AKConfig{Algorithm: ECDSA}, |
| 125 |
}, |
| 126 |
} { |
| 127 |
t.Run(test.name, func(t *testing.T) { |
| 128 |
tpm, err := OpenTPM(nil) |
| 129 |
if err != nil { |
| 130 |
t.Fatalf("OpenTPM() failed: %v", err) |
| 131 |
} |
| 132 |
defer tpm.Close() |
| 133 |
|
| 134 |
ak, err := tpm.NewAK(test.opts) |
| 135 |
if err != nil { |
| 136 |
t.Fatalf("NewAK() failed: %v", err) |
| 137 |
} |
| 138 |
|
| 139 |
enc, err := ak.Marshal() |
| 140 |
if err != nil { |
| 141 |
ak.Close(tpm) |
| 142 |
t.Fatalf("ak.Marshal() failed: %v", err) |
| 143 |
} |
| 144 |
if err := ak.Close(tpm); err != nil { |
| 145 |
t.Fatalf("ak.Close() failed: %v", err) |
| 146 |
} |
| 147 |
|
| 148 |
loaded, err := tpm.LoadAK(enc) |
| 149 |
if err != nil { |
| 150 |
t.Fatalf("LoadAK() failed: %v", err) |
| 151 |
} |
| 152 |
defer loaded.Close(tpm) |
| 153 |
|
| 154 |
k1 := ak.ak.attestationParameters() |
| 155 |
k2 := loaded.ak.attestationParameters() |
| 156 |
|
| 157 |
if !bytes.Equal(k1.Public, k2.Public) { |
| 158 |
t.Error("Original & loaded AK public blobs did not match.") |
| 159 |
t.Logf("Original = %v", k1.Public) |
| 160 |
t.Logf("Loaded = %v", k2.Public) |
| 161 |
} |
| 162 |
}) |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
func TestPCRs(t *testing.T) { |
| 167 |
if !*testLocal { |
| 168 |
t.SkipNow() |
| 169 |
} |
| 170 |
tpm, err := OpenTPM(nil) |
| 171 |
if err != nil { |
| 172 |
t.Fatalf("OpenTPM() failed: %v", err) |
| 173 |
} |
| 174 |
defer tpm.Close() |
| 175 |
|
| 176 |
PCRs, err := tpm.PCRs(HashSHA1) |
| 177 |
if err != nil { |
| 178 |
t.Fatalf("PCRs() failed: %v", err) |
| 179 |
} |
| 180 |
if len(PCRs) != 24 { |
| 181 |
t.Errorf("len(PCRs) = %d, want %d", len(PCRs), 24) |
| 182 |
} |
| 183 |
for i, pcr := range PCRs { |
| 184 |
if pcr.Index != i { |
| 185 |
t.Errorf("PCR index %d does not match map index %d", pcr.Index, i) |
| 186 |
} |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
func TestBug139(t *testing.T) { |
| 191 |
// Tests ParseAKPublic() with known parseable but non-spec-compliant blobs, and ensure |
| 192 |
// an error is returned rather than a segfault. |
| 193 |
// https://github.com/google/go-attestation/issues/139 |
| 194 |
badBlob := []byte{0, 1, 0, 4, 0, 1, 0, 0, 0, 0, 0, 6, 0, 128, 0, 67, 0, 16, 8, 0, 0, 1, 0, 1, 0, 0} |
| 195 |
msg := "parsing public key: missing rsa signature scheme" |
| 196 |
if _, err := ParseAKPublic(badBlob); err == nil || err.Error() != msg { |
| 197 |
t.Errorf("ParseAKPublic() err = %v, want %v", err, msg) |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
func TestBug142(t *testing.T) { |
| 202 |
// Tests ParseEKCertificate() with a malformed size prefix which would overflow |
| 203 |
// an int16, ensuring an error is returned rather than a panic occurring. |
| 204 |
input := []byte{0x10, 0x01, 0x00, 0xff, 0xff, 0x20} |
| 205 |
wantErr := fmt.Errorf("parsing nvram header: ekCert size %d smaller than specified cert length %d", len(input), 65535) |
| 206 |
if _, err := ParseEKCertificate(input); !reflect.DeepEqual(err, wantErr) { |
| 207 |
t.Errorf("ParseEKCertificate() = %v, want %v", err, wantErr) |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
func TestFromCryptoHash(t *testing.T) { |
| 212 |
tests := []struct { |
| 213 |
hash crypto.Hash |
| 214 |
want HashAlg |
| 215 |
err bool |
| 216 |
}{ |
| 217 |
{ |
| 218 |
hash: crypto.SHA1, |
| 219 |
want: HashSHA1, |
| 220 |
}, |
| 221 |
{ |
| 222 |
hash: crypto.SHA256, |
| 223 |
want: HashSHA256, |
| 224 |
}, |
| 225 |
{ |
| 226 |
hash: crypto.SHA384, |
| 227 |
want: HashSHA384, |
| 228 |
}, |
| 229 |
{ |
| 230 |
hash: crypto.SHA512, |
| 231 |
want: HashSHA512, |
| 232 |
}, |
| 233 |
{ |
| 234 |
hash: crypto.MD5, |
| 235 |
err: true, |
| 236 |
}, |
| 237 |
} |
| 238 |
|
| 239 |
for _, tc := range tests { |
| 240 |
got, err := FromCryptoHash(tc.hash) |
| 241 |
if tc.err != (err != nil) { |
| 242 |
t.Errorf("FromCryptoHash(%v) returned err=%v, want err=%v", tc.hash, err, tc.err) |
| 243 |
} |
| 244 |
if got != tc.want { |
| 245 |
t.Errorf("FromCryptoHash(%v) = %v, want %v", tc.hash, got, tc.want) |
| 246 |
} |
| 247 |
} |
| 248 |
} |