| 1 |
package internal |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"crypto" |
| 6 |
"crypto/ecdsa" |
| 7 |
"crypto/rsa" |
| 8 |
"crypto/subtle" |
| 9 |
"fmt" |
| 10 |
|
| 11 |
pb "github.com/google/go-tpm-tools/proto/tpm" |
| 12 |
"github.com/google/go-tpm/legacy/tpm2" |
| 13 |
) |
| 14 |
|
| 15 |
// SignatureHashAlgs are the hash algorithms we support for Quote signatures, in |
| 16 |
// their preferred order of use. |
| 17 |
var SignatureHashAlgs = []tpm2.Algorithm{tpm2.AlgSHA512, tpm2.AlgSHA384, tpm2.AlgSHA256} |
| 18 |
|
| 19 |
// VerifyQuote performs the following checks to validate a Quote: |
| 20 |
// - the provided signature is generated by the trusted AK public key |
| 21 |
// - the signature signs the provided quote data |
| 22 |
// - the quote data starts with TPM_GENERATED_VALUE |
| 23 |
// - the quote data is a valid TPMS_QUOTE_INFO |
| 24 |
// - the quote data was taken over the provided PCRs |
| 25 |
// - the provided PCR values match the quote data internal digest |
| 26 |
// - the provided extraData matches that in the quote data |
| 27 |
// - the signature hash algorithm must be in HashAlgs |
| 28 |
// |
| 29 |
// Note that the caller must have already established trust in the provided |
| 30 |
// public key before validating the Quote. |
| 31 |
// |
| 32 |
// VerifyQuote supports ECDSA and RSASSA signature verification. |
| 33 |
func VerifyQuote(q *pb.Quote, trustedPub crypto.PublicKey, extraData []byte) error { |
| 34 |
sig, err := tpm2.DecodeSignature(bytes.NewBuffer(q.GetRawSig())) |
| 35 |
if err != nil { |
| 36 |
return fmt.Errorf("signature decoding failed: %v", err) |
| 37 |
} |
| 38 |
|
| 39 |
hash, err := verifyHashAlg(sig) |
| 40 |
if err != nil { |
| 41 |
return err |
| 42 |
} |
| 43 |
|
| 44 |
switch pub := trustedPub.(type) { |
| 45 |
case *ecdsa.PublicKey: |
| 46 |
if err = verifyECDSAQuoteSignature(pub, hash, q.GetQuote(), sig); err != nil { |
| 47 |
return err |
| 48 |
} |
| 49 |
case *rsa.PublicKey: |
| 50 |
if err = verifyRSASSAQuoteSignature(pub, hash, q.GetQuote(), sig); err != nil { |
| 51 |
return err |
| 52 |
} |
| 53 |
default: |
| 54 |
return fmt.Errorf("only RSA and ECC public keys are currently supported, received type: %T", pub) |
| 55 |
} |
| 56 |
|
| 57 |
// Decode and check for magic TPMS_GENERATED_VALUE. |
| 58 |
attestationData, err := tpm2.DecodeAttestationData(q.GetQuote()) |
| 59 |
if err != nil { |
| 60 |
return fmt.Errorf("decoding attestation data failed: %v", err) |
| 61 |
} |
| 62 |
if attestationData.Type != tpm2.TagAttestQuote { |
| 63 |
return fmt.Errorf("expected quote tag, got: %v", attestationData.Type) |
| 64 |
} |
| 65 |
attestedQuoteInfo := attestationData.AttestedQuoteInfo |
| 66 |
if attestedQuoteInfo == nil { |
| 67 |
return fmt.Errorf("attestation data does not contain quote info") |
| 68 |
} |
| 69 |
if subtle.ConstantTimeCompare(attestationData.ExtraData, extraData) == 0 { |
| 70 |
return fmt.Errorf("quote extraData %v did not match expected extraData %v", |
| 71 |
attestationData.ExtraData, extraData) |
| 72 |
} |
| 73 |
return validatePCRDigest(attestedQuoteInfo, q.GetPcrs(), hash) |
| 74 |
} |
| 75 |
|
| 76 |
// Get the cryptographic hash used for the signature and make sure we support it |
| 77 |
func verifyHashAlg(sig *tpm2.Signature) (crypto.Hash, error) { |
| 78 |
var hashAlg tpm2.Algorithm |
| 79 |
if sig.ECC != nil { |
| 80 |
hashAlg = sig.ECC.HashAlg |
| 81 |
} else if sig.RSA != nil { |
| 82 |
hashAlg = sig.RSA.HashAlg |
| 83 |
} else { |
| 84 |
return 0, fmt.Errorf("signature is missing hash algorithm") |
| 85 |
} |
| 86 |
|
| 87 |
// Convert from TPM2 hash algorithm to a Golang hash algorithm |
| 88 |
hash, err := hashAlg.Hash() |
| 89 |
if err != nil { |
| 90 |
return 0, err |
| 91 |
} |
| 92 |
for _, alg := range SignatureHashAlgs { |
| 93 |
if hashAlg == alg { |
| 94 |
return hash, nil |
| 95 |
} |
| 96 |
} |
| 97 |
return 0, fmt.Errorf("unsupported signature hash algorithm: %v", hash) |
| 98 |
} |
| 99 |
|
| 100 |
func verifyECDSAQuoteSignature(ecdsaPub *ecdsa.PublicKey, hash crypto.Hash, quoted []byte, sig *tpm2.Signature) error { |
| 101 |
if sig.Alg != tpm2.AlgECDSA { |
| 102 |
return fmt.Errorf("signature scheme 0x%x is not supported, only ECDSA is supported", sig.Alg) |
| 103 |
} |
| 104 |
|
| 105 |
hashConstructor := hash.New() |
| 106 |
hashConstructor.Write(quoted) |
| 107 |
if !ecdsa.Verify(ecdsaPub, hashConstructor.Sum(nil), sig.ECC.R, sig.ECC.S) { |
| 108 |
return fmt.Errorf("ECC signature verification failed") |
| 109 |
} |
| 110 |
return nil |
| 111 |
} |
| 112 |
|
| 113 |
func verifyRSASSAQuoteSignature(rsaPub *rsa.PublicKey, hash crypto.Hash, quoted []byte, sig *tpm2.Signature) error { |
| 114 |
if sig.Alg != tpm2.AlgRSASSA { |
| 115 |
return fmt.Errorf("signature scheme 0x%x is not supported, only RSASSA (PKCS#1 v1.5) is supported", sig.Alg) |
| 116 |
} |
| 117 |
|
| 118 |
hashConstructor := hash.New() |
| 119 |
hashConstructor.Write(quoted) |
| 120 |
if err := rsa.VerifyPKCS1v15(rsaPub, hash, hashConstructor.Sum(nil), sig.RSA.Signature); err != nil { |
| 121 |
return fmt.Errorf("RSASSA signature verification failed: %v", err) |
| 122 |
} |
| 123 |
return nil |
| 124 |
} |
| 125 |
|
| 126 |
func validatePCRDigest(quoteInfo *tpm2.QuoteInfo, pcrs *pb.PCRs, hash crypto.Hash) error { |
| 127 |
if !SamePCRSelection(pcrs, quoteInfo.PCRSelection) { |
| 128 |
return fmt.Errorf("given PCRs and Quote do not have the same PCR selection") |
| 129 |
} |
| 130 |
pcrDigest := PCRDigest(pcrs, hash) |
| 131 |
if subtle.ConstantTimeCompare(quoteInfo.PCRDigest, pcrDigest) == 0 { |
| 132 |
return fmt.Errorf("given PCRs digest not matching") |
| 133 |
} |
| 134 |
return nil |
| 135 |
} |