| 1 |
package server |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"crypto/x509" |
| 6 |
_ "embed" // Necessary to use go:embed |
| 7 |
"errors" |
| 8 |
"fmt" |
| 9 |
"strconv" |
| 10 |
|
| 11 |
pb "github.com/google/go-tpm-tools/proto/attest" |
| 12 |
) |
| 13 |
|
| 14 |
// Expected TCG Event Log Event Types. |
| 15 |
// |
| 16 |
// Taken from TCG PC Client Platform Firmware Profile Specification, |
| 17 |
// Table 14 Events. |
| 18 |
const ( |
| 19 |
NoAction uint32 = 0x00000003 |
| 20 |
Separator uint32 = 0x00000004 |
| 21 |
EventTag uint32 = 0x00000006 |
| 22 |
SCRTMVersion uint32 = 0x00000008 |
| 23 |
IPL uint32 = 0x0000000D |
| 24 |
NonhostInfo uint32 = 0x00000011 |
| 25 |
EFIBootServicesApplication uint32 = 0x80000003 |
| 26 |
EFIAction uint32 = 0x80000007 |
| 27 |
) |
| 28 |
|
| 29 |
// EventTagLoadedImageHex used with type "EV_EVENT_TAG". |
| 30 |
// This corresponds to a TLV struct of type LOAD_OPTIONS_EVENT_TAG_ID (0x8F3B22ED, reversed endian), length 0x1a (26), value `LOADED_IMAGE::LoadOptions\n`. |
| 31 |
const EventTagLoadedImageHex = "ed223b8f1a0000004c4f414445445f494d4147453a3a4c6f61644f7074696f6e7300" |
| 32 |
|
| 33 |
// Constant events used with type "EV_EFI_ACTION". |
| 34 |
// Taken from TCG PC Client Platform Firmware Profile Specification, |
| 35 |
// Table 17 EV_EFI_ACTION Strings. |
| 36 |
const ( |
| 37 |
// Measured when Boot Manager attempts to execute code from a Boot Option. |
| 38 |
CallingEFIApplication string = "Calling EFI Application from Boot Option" |
| 39 |
ExitBootServicesInvocation string = "Exit Boot Services Invocation" |
| 40 |
) |
| 41 |
|
| 42 |
var ( |
| 43 |
// GCENonHostInfoSignature identifies the GCE Non-Host info event, which |
| 44 |
// indicates if memory encryption is enabled. This event is 32-bytes consisting |
| 45 |
// of the below signature (16 bytes), followed by a byte indicating whether |
| 46 |
// it is confidential, followed by 15 reserved bytes. |
| 47 |
GCENonHostInfoSignature = []byte("GCE NonHostInfo\x00") |
| 48 |
// GceVirtualFirmwarePrefix is the little-endian UCS-2 encoded string |
| 49 |
// "GCE Virtual Firmware v" without a null terminator. All GCE firmware |
| 50 |
// versions are UCS-2 encoded, start with this prefix, contain the firmware |
| 51 |
// version encoded as an integer, and end with a null terminator. |
| 52 |
GceVirtualFirmwarePrefix = []byte{0x47, 0x00, 0x43, 0x00, |
| 53 |
0x45, 0x00, 0x20, 0x00, 0x56, 0x00, 0x69, 0x00, 0x72, 0x00, |
| 54 |
0x74, 0x00, 0x75, 0x00, 0x61, 0x00, 0x6c, 0x00, 0x20, 0x00, |
| 55 |
0x46, 0x00, 0x69, 0x00, 0x72, 0x00, 0x6d, 0x00, 0x77, 0x00, |
| 56 |
0x61, 0x00, 0x72, 0x00, 0x65, 0x00, 0x20, 0x00, 0x76, 0x00} |
| 57 |
) |
| 58 |
|
| 59 |
// Standard Secure Boot certificates (DER encoded) |
| 60 |
var ( |
| 61 |
//go:embed secure-boot/GcePk.crt |
| 62 |
GceDefaultPKCert []byte |
| 63 |
//go:embed secure-boot/MicCorKEKCA2011_2011-06-24.crt |
| 64 |
MicrosoftKEKCA2011Cert []byte |
| 65 |
//go:embed secure-boot/MicWinProPCA2011_2011-10-19.crt |
| 66 |
WindowsProductionPCA2011Cert []byte |
| 67 |
//go:embed secure-boot/MicCorUEFCA2011_2011-06-27.crt |
| 68 |
MicrosoftUEFICA2011Cert []byte |
| 69 |
) |
| 70 |
|
| 71 |
// Revoked Signing certificates (DER encoded) |
| 72 |
var ( |
| 73 |
//go:embed secure-boot/canonical-boothole.crt |
| 74 |
RevokedCanonicalBootholeCert []byte |
| 75 |
//go:embed secure-boot/debian-boothole.crt |
| 76 |
RevokedDebianBootholeCert []byte |
| 77 |
//go:embed secure-boot/cisco-boothole.crt |
| 78 |
RevokedCiscoCert []byte |
| 79 |
) |
| 80 |
|
| 81 |
// Known GCE EK CA certs. |
| 82 |
var ( |
| 83 |
//go:embed ca-certs/tpm_ek_root_1.cer |
| 84 |
gceEKRootCA []byte |
| 85 |
//go:embed ca-certs/tpm_ek_intermediate_2.crt |
| 86 |
gceEKIntermediateCA2 []byte |
| 87 |
//go:embed ca-certs/tpm_ek_intermediate_3.crt |
| 88 |
gceEKIntermediateCA3 []byte |
| 89 |
//go:embed ca-certs/gcp_ek_ak_ca_root.crt |
| 90 |
gcpCASEKRootCA []byte |
| 91 |
//go:embed ca-certs/gcp_ek_ak_ca_intermediate_v3.crt |
| 92 |
gcpCASEKIntermediateCA3 []byte |
| 93 |
) |
| 94 |
|
| 95 |
// Certificates corresponding to the known CA certs for GCE. |
| 96 |
var ( |
| 97 |
GceEKRoots []*x509.Certificate |
| 98 |
GceEKIntermediates []*x509.Certificate |
| 99 |
) |
| 100 |
|
| 101 |
func init() { |
| 102 |
var err error |
| 103 |
GceEKRoots, err = parseCerts([][]byte{gceEKRootCA}) |
| 104 |
if err != nil { |
| 105 |
panic(fmt.Sprintf("failed to create the root cert pool: %v", err)) |
| 106 |
} |
| 107 |
GceEKIntermediates, err = parseCerts([][]byte{gceEKIntermediateCA2}) |
| 108 |
if err != nil { |
| 109 |
panic(fmt.Sprintf("failed to create the intermediate cert pool: %v", err)) |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
func parseCerts(rawCerts [][]byte) ([]*x509.Certificate, error) { |
| 114 |
certs := make([]*x509.Certificate, len(rawCerts)) |
| 115 |
for i, certBytes := range rawCerts { |
| 116 |
cert, err := x509.ParseCertificate(certBytes) |
| 117 |
if err != nil { |
| 118 |
return nil, fmt.Errorf("failed to parse cert: %w", err) |
| 119 |
} |
| 120 |
certs[i] = cert |
| 121 |
} |
| 122 |
return certs, nil |
| 123 |
} |
| 124 |
|
| 125 |
// ConvertSCRTMVersionToGCEFirmwareVersion attempts to parse the Firmware |
| 126 |
// Version of a GCE VM from the bytes of the version string of the SCRTM. This |
| 127 |
// data should come from a valid and verified EV_S_CRTM_VERSION event. |
| 128 |
func ConvertSCRTMVersionToGCEFirmwareVersion(version []byte) (uint32, error) { |
| 129 |
prefixLen := len(GceVirtualFirmwarePrefix) |
| 130 |
if (len(version) <= prefixLen) || (len(version)%2 != 0) { |
| 131 |
return 0, fmt.Errorf("length of GCE version (%d) is invalid", len(version)) |
| 132 |
} |
| 133 |
if !bytes.Equal(version[:prefixLen], GceVirtualFirmwarePrefix) { |
| 134 |
return 0, errors.New("prefix for GCE version is missing") |
| 135 |
} |
| 136 |
asciiVersion := []byte{} |
| 137 |
for i, b := range version[prefixLen:] { |
| 138 |
// Skip the UCS-2 null bytes and the null terminator |
| 139 |
if b == '\x00' { |
| 140 |
continue |
| 141 |
} |
| 142 |
// All odd bytes in our UCS-2 string should be Null |
| 143 |
if i%2 != 0 { |
| 144 |
return 0, errors.New("invalid UCS-2 in the version string") |
| 145 |
} |
| 146 |
asciiVersion = append(asciiVersion, b) |
| 147 |
} |
| 148 |
|
| 149 |
versionNum, err := strconv.ParseUint(string(asciiVersion), 10, 32) |
| 150 |
if err != nil { |
| 151 |
return 0, fmt.Errorf("when parsing GCE firmware version: %w", err) |
| 152 |
} |
| 153 |
return uint32(versionNum), nil |
| 154 |
} |
| 155 |
|
| 156 |
// ConvertGCEFirmwareVersionToSCRTMVersion creates the corresponding SCRTM |
| 157 |
// version string from a numerical GCE firmware version. The returned string |
| 158 |
// is UCS2 encoded with a null terminator. A version of 0 corresponds to an |
| 159 |
// empty string (representing old GCE VMs that just used an empty string). |
| 160 |
func ConvertGCEFirmwareVersionToSCRTMVersion(version uint32) []byte { |
| 161 |
if version == 0 { |
| 162 |
return []byte{} |
| 163 |
} |
| 164 |
versionString := GceVirtualFirmwarePrefix |
| 165 |
for _, b := range []byte(strconv.Itoa(int(version))) { |
| 166 |
// Convert ACSII to little-endian UCS-2 |
| 167 |
versionString = append(versionString, b, 0) |
| 168 |
} |
| 169 |
// Add the null terminator |
| 170 |
return append(versionString, 0, 0) |
| 171 |
} |
| 172 |
|
| 173 |
// ParseGCENonHostInfo attempts to parse the Confidential VM |
| 174 |
// technology used by a GCE VM from the GCE Non-Host info event. This data |
| 175 |
// should come from a valid and verified EV_NONHOST_INFO event. |
| 176 |
func ParseGCENonHostInfo(nonHostInfo []byte) (pb.GCEConfidentialTechnology, error) { |
| 177 |
prefixLen := len(GCENonHostInfoSignature) |
| 178 |
if len(nonHostInfo) < (prefixLen + 1) { |
| 179 |
return pb.GCEConfidentialTechnology_NONE, fmt.Errorf("length of GCE Non-Host info (%d) is too short", len(nonHostInfo)) |
| 180 |
} |
| 181 |
|
| 182 |
if !bytes.Equal(nonHostInfo[:prefixLen], GCENonHostInfoSignature) { |
| 183 |
return pb.GCEConfidentialTechnology_NONE, errors.New("prefix for GCE Non-Host info is missing") |
| 184 |
} |
| 185 |
tech := nonHostInfo[prefixLen] |
| 186 |
if tech > byte(pb.GCEConfidentialTechnology_AMD_SEV_SNP) { |
| 187 |
return pb.GCEConfidentialTechnology_NONE, fmt.Errorf("unknown GCE Confidential Technology: %d", tech) |
| 188 |
} |
| 189 |
return pb.GCEConfidentialTechnology(tech), nil |
| 190 |
} |