| 1 |
//go:build !windows |
| 2 |
|
| 3 |
// Binary tpm2-ekcert reads an x509 certificate from a specific NVRAM index. |
| 4 |
package main |
| 5 |
|
| 6 |
import ( |
| 7 |
"crypto" |
| 8 |
"crypto/x509" |
| 9 |
"encoding/asn1" |
| 10 |
"errors" |
| 11 |
"flag" |
| 12 |
"fmt" |
| 13 |
"os" |
| 14 |
"reflect" |
| 15 |
|
| 16 |
"github.com/google/go-tpm/legacy/tpm2" |
| 17 |
"github.com/google/go-tpm/tpmutil" |
| 18 |
) |
| 19 |
|
| 20 |
var ( |
| 21 |
tpmPath = flag.String("tpm-path", "/dev/tpm0", "Path to the TPM device (character device or a Unix socket)") |
| 22 |
// Default value is defined in section 7.8, "NV Memory" of the latest version pdf on: |
| 23 |
// https://trustedcomputinggroup.org/resource/tcg-tpm-v2-0-provisioning-guidance/ |
| 24 |
certIndex = flag.Uint("cert-index", 0x01C00002, "NVRAM index of the certificate file") |
| 25 |
tmplIndex = flag.Uint("template-index", 0, "NVRAM index of the EK template; if zero, default RSA EK template is used") |
| 26 |
outPath = flag.String("output", "", "File path for output; leave blank to write to stdout") |
| 27 |
|
| 28 |
// Default EK template defined in: |
| 29 |
// https://trustedcomputinggroup.org/wp-content/uploads/Credential_Profile_EK_V2.0_R14_published.pdf |
| 30 |
defaultEKTemplate = tpm2.Public{ |
| 31 |
Type: tpm2.AlgRSA, |
| 32 |
NameAlg: tpm2.AlgSHA256, |
| 33 |
Attributes: tpm2.FlagFixedTPM | tpm2.FlagFixedParent | tpm2.FlagSensitiveDataOrigin | |
| 34 |
tpm2.FlagAdminWithPolicy | tpm2.FlagRestricted | tpm2.FlagDecrypt, |
| 35 |
AuthPolicy: []byte{ |
| 36 |
0x83, 0x71, 0x97, 0x67, 0x44, 0x84, |
| 37 |
0xB3, 0xF8, 0x1A, 0x90, 0xCC, 0x8D, |
| 38 |
0x46, 0xA5, 0xD7, 0x24, 0xFD, 0x52, |
| 39 |
0xD7, 0x6E, 0x06, 0x52, 0x0B, 0x64, |
| 40 |
0xF2, 0xA1, 0xDA, 0x1B, 0x33, 0x14, |
| 41 |
0x69, 0xAA, |
| 42 |
}, |
| 43 |
RSAParameters: &tpm2.RSAParams{ |
| 44 |
Symmetric: &tpm2.SymScheme{ |
| 45 |
Alg: tpm2.AlgAES, |
| 46 |
KeyBits: 128, |
| 47 |
Mode: tpm2.AlgCFB, |
| 48 |
}, |
| 49 |
KeyBits: 2048, |
| 50 |
ModulusRaw: make([]byte, 256), |
| 51 |
}, |
| 52 |
} |
| 53 |
) |
| 54 |
|
| 55 |
func main() { |
| 56 |
flag.Parse() |
| 57 |
|
| 58 |
cert, err := readEKCert(*tpmPath, uint32(*certIndex), uint32(*tmplIndex)) |
| 59 |
if err != nil { |
| 60 |
fmt.Fprintln(os.Stderr, err) |
| 61 |
os.Exit(1) |
| 62 |
} |
| 63 |
if *outPath == "" { |
| 64 |
fmt.Println(string(cert)) |
| 65 |
return |
| 66 |
} |
| 67 |
if err := os.WriteFile(*outPath, cert, os.ModePerm); err != nil { |
| 68 |
fmt.Fprintln(os.Stderr, err) |
| 69 |
os.Exit(1) |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
func readEKCert(path string, certIdx, tmplIdx uint32) ([]byte, error) { |
| 74 |
rwc, err := tpm2.OpenTPM(path) |
| 75 |
if err != nil { |
| 76 |
return nil, fmt.Errorf("can't open TPM at %q: %v", path, err) |
| 77 |
} |
| 78 |
defer rwc.Close() |
| 79 |
ekCert, err := tpm2.NVRead(rwc, tpmutil.Handle(certIdx)) |
| 80 |
if err != nil { |
| 81 |
return nil, fmt.Errorf("reading EK cert: %v", err) |
| 82 |
} |
| 83 |
|
| 84 |
// Identify if any `padding` exists in the EK cert that was read |
| 85 |
var raw asn1.RawValue |
| 86 |
paddingBytes, err := asn1.Unmarshal(ekCert, &raw) |
| 87 |
if err != nil { |
| 88 |
return nil, fmt.Errorf("ASN.1 Unmarshal failed: %v", err) |
| 89 |
} |
| 90 |
fmt.Printf("TPM NV Index bytes read: %d\n", len(ekCert)) |
| 91 |
fmt.Printf("Padding found from ASN.1 Unmarshal: %d\n", len(paddingBytes)) |
| 92 |
|
| 93 |
// Sanity-check that this is a valid certificate. |
| 94 |
cert, err := x509.ParseCertificate(ekCert[0 : len(ekCert)-len(paddingBytes)]) |
| 95 |
if err != nil { |
| 96 |
return nil, fmt.Errorf("parsing EK cert: %v", err) |
| 97 |
} |
| 98 |
|
| 99 |
// Initialize EK and compare public key to ekCert.PublicKey. |
| 100 |
var ekh tpmutil.Handle |
| 101 |
var ekPub crypto.PublicKey |
| 102 |
if tmplIdx != 0 { |
| 103 |
ekTemplate, err := tpm2.NVRead(rwc, tpmutil.Handle(tmplIdx)) |
| 104 |
if err != nil { |
| 105 |
return nil, fmt.Errorf("reading EK template: %v", err) |
| 106 |
} |
| 107 |
ekh, ekPub, err = tpm2.CreatePrimaryRawTemplate(rwc, tpm2.HandleEndorsement, tpm2.PCRSelection{}, "", "", ekTemplate) |
| 108 |
if err != nil { |
| 109 |
return nil, fmt.Errorf("creating EK: %v", err) |
| 110 |
} |
| 111 |
} else { |
| 112 |
ekh, ekPub, err = tpm2.CreatePrimary(rwc, tpm2.HandleEndorsement, tpm2.PCRSelection{}, "", "", defaultEKTemplate) |
| 113 |
if err != nil { |
| 114 |
return nil, fmt.Errorf("creating EK: %v", err) |
| 115 |
} |
| 116 |
} |
| 117 |
defer tpm2.FlushContext(rwc, ekh) |
| 118 |
|
| 119 |
if !reflect.DeepEqual(ekPub, cert.PublicKey) { |
| 120 |
return nil, errors.New("public key in EK certificate differs from public key created via EK template") |
| 121 |
} |
| 122 |
|
| 123 |
return ekCert, nil |
| 124 |
} |