| 1 |
package x509ext |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"crypto/x509" |
| 6 |
"crypto/x509/pkix" |
| 7 |
"encoding/asn1" |
| 8 |
"errors" |
| 9 |
"fmt" |
| 10 |
"math/big" |
| 11 |
|
| 12 |
"github.com/google/go-attestation/oid" |
| 13 |
) |
| 14 |
|
| 15 |
var ( |
| 16 |
// The DER encoding of an empty SEQUENCE is 0x30 0x00. |
| 17 |
emptyASN1Subject = []byte{0x30, 0} |
| 18 |
oidAuthorityInfoAccess = []int{1, 3, 6, 1, 5, 5, 7, 1, 1} |
| 19 |
oidBasicConstraints = []int{2, 5, 29, 19} |
| 20 |
oidSubjectKeyIdentifier = []int{2, 5, 29, 14} |
| 21 |
oidKeyUsage = []int{2, 5, 29, 15} |
| 22 |
oidCRLDistributionPoints = []int{2, 5, 29, 31} |
| 23 |
oidAuthorityKeyID = []int{2, 5, 29, 35} |
| 24 |
oidExtendedKeyUsage = []int{2, 5, 29, 37} |
| 25 |
mustHaveExtensions = []asn1.ObjectIdentifier{ |
| 26 |
oid.SubjectAltName, |
| 27 |
oidBasicConstraints, |
| 28 |
oidKeyUsage, |
| 29 |
oidAuthorityKeyID, |
| 30 |
} |
| 31 |
oidToExtNameMap = map[string]string{ |
| 32 |
(asn1.ObjectIdentifier)(oid.SubjectAltName).String(): "SubjectAltName", |
| 33 |
(asn1.ObjectIdentifier)(oidBasicConstraints).String(): "BasicConstraints", |
| 34 |
(asn1.ObjectIdentifier)(oidKeyUsage).String(): "Key Usage", |
| 35 |
(asn1.ObjectIdentifier)(oidAuthorityKeyID).String(): "Authority Key Identifier", |
| 36 |
} |
| 37 |
) |
| 38 |
|
| 39 |
type attribute struct { |
| 40 |
Type asn1.ObjectIdentifier |
| 41 |
Values []asn1.RawValue `asn1:"set"` |
| 42 |
} |
| 43 |
|
| 44 |
// TpmSpecification represents the TPM specification of an EK certificate. |
| 45 |
type TpmSpecification struct { |
| 46 |
Family string |
| 47 |
Level int |
| 48 |
Revision int |
| 49 |
} |
| 50 |
|
| 51 |
// EKCertificate extends x509.certificate with helper methods for working with |
| 52 |
// TCG EK Certificates. |
| 53 |
type EKCertificate struct { |
| 54 |
*x509.Certificate |
| 55 |
TpmManufacturer, TpmModel, TpmVersion string |
| 56 |
TpmSpecification TpmSpecification |
| 57 |
// If the certificate contains a tcg-kp-EKCertificate (2.23.133.8.1) in the |
| 58 |
// Extended Key Usage, this will be true. |
| 59 |
HasEkcExtendedKeyUsage bool |
| 60 |
} |
| 61 |
|
| 62 |
// ParseEKCertificate parses a single certificate from the given ASN.1 DER data. |
| 63 |
func ParseEKCertificate(der []byte) (*EKCertificate, error) { |
| 64 |
cert, err := x509.ParseCertificate(der) |
| 65 |
if err != nil { |
| 66 |
return nil, err |
| 67 |
} |
| 68 |
return ToEKCertificate(cert) |
| 69 |
} |
| 70 |
|
| 71 |
// ToEKCertificate converts a x509 certificate to an EKCertificate. It also |
| 72 |
// validates the EK cert according to Section 3.2 of |
| 73 |
// https://trustedcomputinggroup.org/wp-content/uploads/TCG-EK-Credential-Profile-for-TPM-Family-2.0-Level-0-Version-2.6_pub.pdf |
| 74 |
// |
| 75 |
// This function checks for the presence and criticality of required extensions |
| 76 |
// and performs basic structural validation, but it does NOT validate the |
| 77 |
// semantic values of every extension. Some additional validation may be necessary depending on the use case. |
| 78 |
// |
| 79 |
// Extensions handled by this function will be removed from `cert.UnhandledCriticalExtensions` in place if present. |
| 80 |
// |
| 81 |
// TODO: Handle TPM Security Assertions (Section 3.1.1 from the EK Credential profile spec) |
| 82 |
func ToEKCertificate(cert *x509.Certificate) (*EKCertificate, error) { |
| 83 |
// Some older EK certificates have RSA-OAEP public keys, which are not |
| 84 |
// parsed by crypto/x509, resulting in PublicKey being nil. |
| 85 |
if cert.PublicKey == nil { |
| 86 |
return nil, errors.New("publicKey is nil") |
| 87 |
} |
| 88 |
|
| 89 |
var spec TpmSpecification |
| 90 |
var tpmManufacturer, tpmModel, tpmVersion string |
| 91 |
var hasEKCExtendedKeyUsage bool |
| 92 |
extPresent := make(map[string]bool) |
| 93 |
|
| 94 |
// Version must be 3. |
| 95 |
if cert.Version != 3 { |
| 96 |
return nil, fmt.Errorf("invalid version of EK certificate: %d", cert.Version) |
| 97 |
} |
| 98 |
|
| 99 |
// SerialNumber must be a positive integer and not nil. |
| 100 |
if err := validateSerialNumber(cert.SerialNumber); err != nil { |
| 101 |
return nil, err |
| 102 |
} |
| 103 |
|
| 104 |
// Issuer must be present. |
| 105 |
if bytes.Equal(cert.RawIssuer, emptyASN1Subject) { |
| 106 |
return nil, errors.New("issuer is empty") |
| 107 |
} |
| 108 |
|
| 109 |
isSubjectEmpty := bytes.Equal(cert.RawSubject, emptyASN1Subject) |
| 110 |
|
| 111 |
// Basic Constraints must be valid and the certificate must not be a CA. |
| 112 |
if !cert.BasicConstraintsValid || cert.IsCA { |
| 113 |
return nil, errors.New("BasicConstraints are not valid or it is a CA certificate") |
| 114 |
} |
| 115 |
|
| 116 |
for _, ext := range cert.Extensions { |
| 117 |
switch { |
| 118 |
case ext.Id.Equal(oid.SubjectAltName): |
| 119 |
if isSubjectEmpty { |
| 120 |
if !ext.Critical { |
| 121 |
return nil, errors.New("SubjectAltName extension must be critical when Subject is not present") |
| 122 |
} |
| 123 |
} |
| 124 |
san, err := ParseSubjectAltName(ext) |
| 125 |
if err != nil { |
| 126 |
return nil, err |
| 127 |
} |
| 128 |
if len(san.DirectoryNames) != 1 { |
| 129 |
return nil, errors.New("only a single DirectoryName is supported") |
| 130 |
} |
| 131 |
tpmManufacturer, tpmModel, tpmVersion, err = parseName(san.DirectoryNames[0]) |
| 132 |
if err != nil { |
| 133 |
return nil, err |
| 134 |
} |
| 135 |
case ext.Id.Equal(oid.SubjectDirectoryAttributes): |
| 136 |
subjectDirectoryAttributes, err := parseSubjectDirectoryAttributes(ext) |
| 137 |
if err != nil { |
| 138 |
return nil, err |
| 139 |
} |
| 140 |
if spec, err = parseTPMSpecification(subjectDirectoryAttributes); err != nil { |
| 141 |
return nil, err |
| 142 |
} |
| 143 |
case ext.Id.Equal(oidBasicConstraints): |
| 144 |
if !ext.Critical { |
| 145 |
return nil, errors.New("extension \"Basic Constraints\" is not critical, supposed to be critical") |
| 146 |
} |
| 147 |
case ext.Id.Equal(oidKeyUsage): |
| 148 |
if !ext.Critical { |
| 149 |
return nil, errors.New("extension \"Key Usage\" is not critical, supposed to be critical") |
| 150 |
} |
| 151 |
case ext.Id.Equal(oidAuthorityKeyID): |
| 152 |
if ext.Critical { |
| 153 |
return nil, errors.New("extension \"Authority Key Identifier\" is critical, supposed to be non-critical") |
| 154 |
} |
| 155 |
case ext.Id.Equal(oid.CertificatePolicies): |
| 156 |
if len(cert.PolicyIdentifiers) == 0 { |
| 157 |
return nil, errors.New("extension \"Certificate Policies\" should contain at least 1 policy identifier if the extension is present") |
| 158 |
} |
| 159 |
case ext.Id.Equal(oidAuthorityInfoAccess): |
| 160 |
if ext.Critical { |
| 161 |
return nil, errors.New("extension \"Authority Info Access\" is critical, supposed to be non-critical") |
| 162 |
} |
| 163 |
case ext.Id.Equal(oidCRLDistributionPoints): |
| 164 |
if ext.Critical { |
| 165 |
return nil, errors.New("extension \"CRL Distribution Points\" is critical, supposed to be non-critical") |
| 166 |
} |
| 167 |
case ext.Id.Equal(oidExtendedKeyUsage): |
| 168 |
if ext.Critical { |
| 169 |
return nil, errors.New("extension \"Extended Key Usage\" is critical, supposed to be non-critical") |
| 170 |
} |
| 171 |
case ext.Id.Equal(oidSubjectKeyIdentifier): |
| 172 |
if ext.Critical { |
| 173 |
return nil, errors.New("extension \"Subject Key Identifier\" is critical, supposed to be non-critical") |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
extPresent[ext.Id.String()] = true |
| 178 |
} |
| 179 |
|
| 180 |
// Check that all must-have extensions are present. |
| 181 |
for _, extOID := range mustHaveExtensions { |
| 182 |
if !extPresent[extOID.String()] { |
| 183 |
return nil, fmt.Errorf("extension %v is missing", oidToExtNameMap[extOID.String()]) |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
// Authority Key ID must be present and non-empty. |
| 188 |
if len(cert.AuthorityKeyId) == 0 { |
| 189 |
return nil, errors.New("missing Authority Key ID") |
| 190 |
} |
| 191 |
|
| 192 |
// KeyUsage must be set and correctly set for the public key type. |
| 193 |
if err := validateKeyUsage(cert.PublicKeyAlgorithm, cert.KeyUsage); err != nil { |
| 194 |
return nil, err |
| 195 |
} |
| 196 |
|
| 197 |
// Iterate through unknown/custom ExtKeyUsage OIDs |
| 198 |
for _, eku := range cert.UnknownExtKeyUsage { |
| 199 |
if eku.Equal(oid.EKCertificate) { |
| 200 |
hasEKCExtendedKeyUsage = true |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
// Iterate through the unhandled critical extensions to remove the handled extensions from the list. |
| 205 |
for i, ext := range cert.UnhandledCriticalExtensions { |
| 206 |
if ext.Equal(oid.SubjectAltName) { |
| 207 |
length := len(cert.UnhandledCriticalExtensions) |
| 208 |
// Remove the extension from the list of unhandled critical extensions. |
| 209 |
cert.UnhandledCriticalExtensions[i] = cert.UnhandledCriticalExtensions[length-1] |
| 210 |
cert.UnhandledCriticalExtensions = cert.UnhandledCriticalExtensions[:length-1] |
| 211 |
break |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
return &EKCertificate{ |
| 216 |
Certificate: cert, |
| 217 |
TpmManufacturer: tpmManufacturer, |
| 218 |
TpmModel: tpmModel, |
| 219 |
TpmVersion: tpmVersion, |
| 220 |
TpmSpecification: spec, |
| 221 |
HasEkcExtendedKeyUsage: hasEKCExtendedKeyUsage, |
| 222 |
}, nil |
| 223 |
} |
| 224 |
|
| 225 |
func validateSerialNumber(serialNumber *big.Int) error { |
| 226 |
if serialNumber == nil { |
| 227 |
return errors.New("SerialNumber is nil, expected a positive integer") |
| 228 |
} |
| 229 |
if serialNumber.Cmp(big.NewInt(0)) <= 0 { |
| 230 |
return errors.New("SerialNumber is not a positive integer") |
| 231 |
} |
| 232 |
return nil |
| 233 |
} |
| 234 |
|
| 235 |
func parseSubjectDirectoryAttributes(ext pkix.Extension) ([]attribute, error) { |
| 236 |
var attrs []attribute |
| 237 |
rest, err := asn1.Unmarshal(ext.Value, &attrs) |
| 238 |
if err != nil { |
| 239 |
return nil, err |
| 240 |
} |
| 241 |
if len(rest) != 0 { |
| 242 |
return nil, errors.New("trailing data after X.509 extension") |
| 243 |
} |
| 244 |
return attrs, nil |
| 245 |
} |
| 246 |
|
| 247 |
func parseTPMSpecification(subjectDirectoryAttributes []attribute) (TpmSpecification, error) { |
| 248 |
for _, attr := range subjectDirectoryAttributes { |
| 249 |
if attr.Type.Equal(oid.TPMSpecification) { |
| 250 |
if len(attr.Values) != 1 { |
| 251 |
return TpmSpecification{}, errors.New("expected SET size of 1") |
| 252 |
} |
| 253 |
value := attr.Values[0] |
| 254 |
var spec TpmSpecification |
| 255 |
rest, err := asn1.Unmarshal(value.FullBytes, &spec) |
| 256 |
if err != nil { |
| 257 |
return TpmSpecification{}, err |
| 258 |
} |
| 259 |
if len(rest) != 0 { |
| 260 |
return TpmSpecification{}, errors.New("trailing data after TPMSpecification") |
| 261 |
} |
| 262 |
return spec, nil |
| 263 |
} |
| 264 |
} |
| 265 |
return TpmSpecification{}, errors.New("TPMSpecification not present") |
| 266 |
} |
| 267 |
|
| 268 |
func parseName(name pkix.Name) (string, string, string, error) { |
| 269 |
var tpmManufacturer, tpmModel, tpmVersion string |
| 270 |
for _, attr := range name.Names { |
| 271 |
if attr.Type.Equal(oid.TPMManufacturer) { |
| 272 |
tpmManufacturer = fmt.Sprintf("%v", attr.Value) |
| 273 |
continue |
| 274 |
} |
| 275 |
if attr.Type.Equal(oid.TPMModel) { |
| 276 |
tpmModel = fmt.Sprintf("%v", attr.Value) |
| 277 |
continue |
| 278 |
} |
| 279 |
if attr.Type.Equal(oid.TPMVersion) { |
| 280 |
tpmVersion = fmt.Sprintf("%v", attr.Value) |
| 281 |
continue |
| 282 |
} |
| 283 |
return "", "", "", fmt.Errorf("unknown attribute type: %v", attr.Type) |
| 284 |
} |
| 285 |
if tpmManufacturer == "" { |
| 286 |
return "", "", "", fmt.Errorf("TPM Manufacturer not present") |
| 287 |
} |
| 288 |
if tpmModel == "" { |
| 289 |
return "", "", "", fmt.Errorf("TPM Model not present") |
| 290 |
} |
| 291 |
if tpmVersion == "" { |
| 292 |
return "", "", "", fmt.Errorf("TPM Version not present") |
| 293 |
} |
| 294 |
return tpmManufacturer, tpmModel, tpmVersion, nil |
| 295 |
} |
| 296 |
|
| 297 |
func validateKeyUsage(certType x509.PublicKeyAlgorithm, keyUsage x509.KeyUsage) error { |
| 298 |
if keyUsage == 0 { |
| 299 |
return fmt.Errorf("KeyUsage field is not set") |
| 300 |
} |
| 301 |
switch certType { |
| 302 |
case x509.RSA: |
| 303 |
if keyUsage&x509.KeyUsageKeyEncipherment == 0 { |
| 304 |
return fmt.Errorf("KeyUsageKeyEncipherment is not set for RSA public key type") |
| 305 |
} |
| 306 |
case x509.ECDSA: |
| 307 |
if keyUsage&x509.KeyUsageKeyAgreement == 0 { |
| 308 |
return fmt.Errorf("KeyUsageKeyAgreement is not set for ECDSA public key type") |
| 309 |
} |
| 310 |
default: |
| 311 |
return fmt.Errorf("unsupported public key type: %v", certType) |
| 312 |
} |
| 313 |
return nil |
| 314 |
} |