| 1 |
package attest |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"crypto/rsa" |
| 6 |
"crypto/sha256" |
| 7 |
"encoding/base64" |
| 8 |
"math/big" |
| 9 |
"math/rand" |
| 10 |
"testing" |
| 11 |
|
| 12 |
"github.com/google/go-tpm/legacy/tpm2" |
| 13 |
"github.com/google/go-tpm/tpmutil" |
| 14 |
) |
| 15 |
|
| 16 |
func decodeBase10(base10 string, t *testing.T) *big.Int { |
| 17 |
i, ok := new(big.Int).SetString(base10, 10) |
| 18 |
if !ok { |
| 19 |
t.Fatalf("failed decode of base10: %q", base10) |
| 20 |
} |
| 21 |
return i |
| 22 |
} |
| 23 |
|
| 24 |
func decodeBase64(in string, t *testing.T) []byte { |
| 25 |
out, err := base64.StdEncoding.DecodeString(in) |
| 26 |
if err != nil { |
| 27 |
t.Fatal(err) |
| 28 |
} |
| 29 |
return out |
| 30 |
} |
| 31 |
|
| 32 |
func ekCertSigner(t *testing.T) *rsa.PrivateKey { |
| 33 |
return &rsa.PrivateKey{ |
| 34 |
PublicKey: rsa.PublicKey{ |
| 35 |
N: decodeBase10("14314132931241006650998084889274020608918049032671858325988396851334124245188214251956198731333464217832226406088020736932173064754214329009979944037640912127943488972644697423190955557435910767690712778463524983667852819010259499695177313115447116110358524558307947613422897787329221478860907963827160223559690523660574329011927531289655711860504630573766609239332569210831325633840174683944553667352219670930408593321661375473885147973879086994006440025257225431977751512374815915392249179976902953721486040787792801849818254465486633791826766873076617116727073077821584676715609985777563958286637185868165868520557", t), |
| 36 |
E: 3, |
| 37 |
}, |
| 38 |
D: decodeBase10("9542755287494004433998723259516013739278699355114572217325597900889416163458809501304132487555642811888150937392013824621448709836142886006653296025093941418628992648429798282127303704957273845127141852309016655778568546006839666463451542076964744073572349705538631742281931858219480985907271975884773482372966847639853897890615456605598071088189838676728836833012254065983259638538107719766738032720239892094196108713378822882383694456030043492571063441943847195939549773271694647657549658603365629458610273821292232646334717612674519997533901052790334279661754176490593041941863932308687197618671528035670452762731", t), |
| 39 |
Primes: []*big.Int{ |
| 40 |
decodeBase10("130903255182996722426771613606077755295583329135067340152947172868415809027537376306193179624298874215608270802054347609836776473930072411958753044562214537013874103802006369634761074377213995983876788718033850153719421695468704276694983032644416930879093914927146648402139231293035971427838068945045019075433", t), |
| 41 |
decodeBase10("109348945610485453577574767652527472924289229538286649661240938988020367005475727988253438647560958573506159449538793540472829815903949343191091817779240101054552748665267574271163617694640513549693841337820602726596756351006149518830932261246698766355347898158548465400674856021497190430791824869615170301029", t), |
| 42 |
}, |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
func TestActivation(t *testing.T) { |
| 47 |
priv := ekCertSigner(t) |
| 48 |
rand := rand.New(rand.NewSource(123456)) |
| 49 |
|
| 50 |
// These parameters represent an AK generated on a real-world, infineon TPM. |
| 51 |
params := ActivationParameters{ |
| 52 |
AK: AttestationParameters{ |
| 53 |
Public: decodeBase64("AAEACwAFBHIAIJ3/y/NsODrmmfuYaNxty4nXFTiEvigDkiwSQVi/rSKuABAAFAAECAAAAAAAAQC/08gj/04z4xGMIVTmr02lzhI5epufXgU831xEpf2qpXfvtNGUfqTcgWF2EUux2HDPqgcj59dtXRobQdlr4uCGNzfZIGAej4JusLa4MjpG6W2DtJPot6F1Mry63talzJ36U47niy9Iesd34CO2p9Xk3+86ZmBnQ6PQ2roUNK3l7bKz6cFLM9drOLwCqU0AUl6pHvzYPPz+xXsPl3iaA2cM97oneUiJNmJM7wtR9OcaKyIA4wVlX5TndB9NwWq5Iuj8q2Sp40Dg0noXXGSPliAtVD8flkXtAcuI9UHkQbzu9cGPRdSJPMn743GONg3bYalFtcgh2VpACXkPbXB32J7B", t), |
| 54 |
CreateData: decodeBase64("AAAAAAAg47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFUBAAsAIgALWI9hwDRB3zYSkannqM5z0J1coQNA1Jz/oCRxJQwTaNwAIgALmyFYBhHeIU3FUKIAPgXFD3NXyasP3siQviDEyH7avu4AAA==", t), |
| 55 |
CreateAttestation: decodeBase64("/1RDR4AaACIAC41+jhmEOue1MZhJjIk79ENar6i15rBvamXLpQnGTBCOAAAAAAAAD3GRNfU4syzJ1jQGATDCDteFC5C4ACIAC3ToMYGy9GXxcf8A0HvOuLOHbU7HPEppM47C7CMcU8TtACBDmJFUFO1f5+BYevaYdd3VtfMCsxIuHhoTZJczzLP2BA==", t), |
| 56 |
CreateSignature: decodeBase64("ABQABAEALVzJSnKRJU39gHjETaI89/sM1L6HwBPGNekw6NojSW8bwD5/W1cLRDakCsYKUQu68mmbjs8xaIVBRvVM2YWP10tbTWNB0iJc9b8rERhkk3QIIFm/XsiVZsb0mysTxfeh8zygaAKQ/50sYyzp+raD0Ho0mYIRKJOEdQ6chsBflM3eB8mCXGTugUfrET80q3iu0gncaKWbfxQaQUb9ZTPSJrTN64HQ9tlOfnGT+8++WA3hV0NqKMnoAqiI9GZnI5MPXs6XxEncu/GJLJpAYZakBiS74Jvlr34Pur32B4xjm1M25AUGHEIgb6r49S0sV+hzaKu45858lQRMXj01GcyBhw==", t), |
| 57 |
}, |
| 58 |
EK: &rsa.PublicKey{ |
| 59 |
E: priv.E, |
| 60 |
N: priv.N, |
| 61 |
}, |
| 62 |
Rand: rand, |
| 63 |
} |
| 64 |
|
| 65 |
secret, _, err := params.Generate() |
| 66 |
if err != nil { |
| 67 |
t.Fatalf("Generate() returned err: %v", err) |
| 68 |
} |
| 69 |
if got, want := secret, decodeBase64("0vhS7HtORX9uf/iyQ8Sf9WkpJuoJ1olCfTjSZuyNNxY=", t); !bytes.Equal(got, want) { |
| 70 |
t.Fatalf("secret = %v, want %v", got, want) |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
func TestECCActivation(t *testing.T) { |
| 75 |
priv := ekCertSigner(t) |
| 76 |
rand := rand.New(rand.NewSource(123456)) |
| 77 |
|
| 78 |
// These parameters represent an ECC P256 AK generated on a real-world, |
| 79 |
// Google vTPM. |
| 80 |
params := ActivationParameters{ |
| 81 |
AK: AttestationParameters{ |
| 82 |
Public: decodeBase64("ACMACwAFBHIAIJ3/y/NsODrmmfuYaNxty4nXFTiEvigDkiwSQVi/rSKuABAAGAAEAAMAEAAgydcfFWW6O7PjW9vnOE4IDx3545TxylD1iVHP8MIFI78AIJuD/QM9EbqM+3SEl7PgiXlWV1NhmvnmE2AHiEfI/hUn", t), |
| 83 |
CreateData: decodeBase64("AAAAAAAg47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFUBAAsAIgALLEyPGewwEIKqPNw9Lx7QXsfp0MsOZFt4EzHFT4tSXekAIgALf7O+OxqTNzTuIhi1YGQoulPZoyxsNKHpBgT4dHqT3+UAAA==", t), |
| 84 |
CreateAttestation: decodeBase64("/1RDR4AaACIAC8Uqr5OAkfcyLJ2gLU2oSvIoPi8XdoLHGdpS5h/JJz8aAAAAAAAEXezQqarG49k44bBnAUna81DZr1xhACIAC3AoqIzDrusUtdH3uAwqbqUrybtu35H1XPQDyLBHVGF+ACCKEKpwL1LFbw/IG+vtJ6CtXHIYBhVWyrkAqYLkHleYMw==", t), |
| 85 |
CreateSignature: decodeBase64("ABgABAAgbTEBcZvjb9uEEYZCiSqPh/XO0BZQS+egnJ8WKtpSbmkAIDNgvF9iyiOCvd5480hOCzjRj7GP3YZ3XqjEVvN3Q3Ca", t), |
| 86 |
}, |
| 87 |
EK: &rsa.PublicKey{ |
| 88 |
E: priv.E, |
| 89 |
N: priv.N, |
| 90 |
}, |
| 91 |
Rand: rand, |
| 92 |
} |
| 93 |
|
| 94 |
secret, _, err := params.Generate() |
| 95 |
if err != nil { |
| 96 |
t.Fatalf("Generate() returned err: %v", err) |
| 97 |
} |
| 98 |
if got, want := secret, decodeBase64("0vhS7HtORX9uf/iyQ8Sf9WkpJuoJ1olCfTjSZuyNNxY=", t); !bytes.Equal(got, want) { |
| 99 |
t.Fatalf("secret = %v, want %v", got, want) |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
func TestCheckAKParametersRejectsECCPublicKeyParseFailures(t *testing.T) { |
| 104 |
t.Parallel() |
| 105 |
|
| 106 |
pub := testECCAKPublic(t, tpm2.CurveBNP256) |
| 107 |
params := testActivationParameters(t, pub, testGarbageSignatureBytes()) |
| 108 |
|
| 109 |
if err := params.CheckAKParameters(); err == nil { |
| 110 |
t.Fatal("CheckAKParameters() unexpectedly accepted an ECC AK when pub.Key() failed") |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
func TestCheckAKParametersRejectsInvalidECDSASignature(t *testing.T) { |
| 115 |
t.Parallel() |
| 116 |
|
| 117 |
pub := testECCAKPublic(t, tpm2.CurveNISTP256) |
| 118 |
params := testActivationParameters(t, pub, testInvalidECDSASignature(t)) |
| 119 |
|
| 120 |
if err := params.CheckAKParameters(); err == nil { |
| 121 |
t.Fatal("CheckAKParameters() unexpectedly accepted an invalid ECDSA signature") |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
func testActivationParameters(t *testing.T, pub tpm2.Public, createSignature []byte) *ActivationParameters { |
| 126 |
t.Helper() |
| 127 |
|
| 128 |
publicBlob, err := pub.Encode() |
| 129 |
if err != nil { |
| 130 |
t.Fatalf("pub.Encode(): %v", err) |
| 131 |
} |
| 132 |
|
| 133 |
createData := testCreationData(t) |
| 134 |
createAttestation := testCreationAttestation(t, pub, createData) |
| 135 |
|
| 136 |
return &ActivationParameters{ |
| 137 |
AK: AttestationParameters{ |
| 138 |
Public: publicBlob, |
| 139 |
CreateData: createData, |
| 140 |
CreateAttestation: createAttestation, |
| 141 |
CreateSignature: createSignature, |
| 142 |
}, |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
func testECCAKPublic(t *testing.T, curveID tpm2.EllipticCurve) tpm2.Public { |
| 147 |
t.Helper() |
| 148 |
|
| 149 |
xy := make([]byte, 32) |
| 150 |
xy[len(xy)-1] = 1 |
| 151 |
|
| 152 |
return tpm2.Public{ |
| 153 |
Type: tpm2.AlgECC, |
| 154 |
NameAlg: tpm2.AlgSHA256, |
| 155 |
Attributes: tpm2.FlagSignerDefault, |
| 156 |
ECCParameters: &tpm2.ECCParams{ |
| 157 |
Sign: &tpm2.SigScheme{ |
| 158 |
Alg: tpm2.AlgECDSA, |
| 159 |
Hash: tpm2.AlgSHA256, |
| 160 |
}, |
| 161 |
CurveID: curveID, |
| 162 |
Point: tpm2.ECPoint{ |
| 163 |
XRaw: tpmutil.U16Bytes(xy), |
| 164 |
YRaw: tpmutil.U16Bytes(xy), |
| 165 |
}, |
| 166 |
}, |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
func testCreationData(t *testing.T) []byte { |
| 171 |
t.Helper() |
| 172 |
|
| 173 |
createData := tpm2.CreationData{ |
| 174 |
PCRSelection: tpm2.PCRSelection{ |
| 175 |
Hash: tpm2.AlgSHA256, |
| 176 |
PCRs: nil, |
| 177 |
}, |
| 178 |
PCRDigest: nil, |
| 179 |
Locality: 0, |
| 180 |
ParentNameAlg: tpm2.AlgSHA256, |
| 181 |
ParentName: tpm2.Name{}, |
| 182 |
ParentQualifiedName: tpm2.Name{}, |
| 183 |
OutsideInfo: nil, |
| 184 |
} |
| 185 |
encoded, err := createData.EncodeCreationData() |
| 186 |
if err != nil { |
| 187 |
t.Fatalf("EncodeCreationData(): %v", err) |
| 188 |
} |
| 189 |
return encoded |
| 190 |
} |
| 191 |
|
| 192 |
func testCreationAttestation(t *testing.T, pub tpm2.Public, createData []byte) []byte { |
| 193 |
t.Helper() |
| 194 |
|
| 195 |
name, err := pub.Name() |
| 196 |
if err != nil { |
| 197 |
t.Fatalf("pub.Name(): %v", err) |
| 198 |
} |
| 199 |
|
| 200 |
digest := sha256.Sum256(createData) |
| 201 |
attestation, err := (tpm2.AttestationData{ |
| 202 |
Magic: 0xff544347, |
| 203 |
Type: tpm2.TagAttestCreation, |
| 204 |
QualifiedSigner: tpm2.Name{}, |
| 205 |
ExtraData: nil, |
| 206 |
ClockInfo: tpm2.ClockInfo{}, |
| 207 |
FirmwareVersion: 0, |
| 208 |
AttestedCreationInfo: &tpm2.CreationInfo{ |
| 209 |
Name: name, |
| 210 |
OpaqueDigest: tpmutil.U16Bytes(digest[:]), |
| 211 |
}, |
| 212 |
}).Encode() |
| 213 |
if err != nil { |
| 214 |
t.Fatalf("AttestationData.Encode(): %v", err) |
| 215 |
} |
| 216 |
return attestation |
| 217 |
} |
| 218 |
|
| 219 |
func testGarbageSignatureBytes() []byte { |
| 220 |
return []byte{0, 0, 0, 0, 0, 0, 0, 0} |
| 221 |
} |
| 222 |
|
| 223 |
func testInvalidECDSASignature(t *testing.T) []byte { |
| 224 |
t.Helper() |
| 225 |
|
| 226 |
signature, err := (tpm2.Signature{ |
| 227 |
Alg: tpm2.AlgECDSA, |
| 228 |
ECC: &tpm2.SignatureECC{ |
| 229 |
HashAlg: tpm2.AlgSHA256, |
| 230 |
R: big.NewInt(1), |
| 231 |
S: big.NewInt(1), |
| 232 |
}, |
| 233 |
}).Encode() |
| 234 |
if err != nil { |
| 235 |
t.Fatalf("Signature.Encode(): %v", err) |
| 236 |
} |
| 237 |
if len(signature) < 8 { |
| 238 |
t.Fatalf("unexpected signature size: %d", len(signature)) |
| 239 |
} |
| 240 |
return signature |
| 241 |
} |