| 1 |
// Copyright (c) 2014, Google LLC All rights reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
package tpm |
| 16 |
|
| 17 |
import ( |
| 18 |
"crypto" |
| 19 |
"crypto/rsa" |
| 20 |
"crypto/sha1" |
| 21 |
"errors" |
| 22 |
"math/big" |
| 23 |
|
| 24 |
"github.com/google/go-tpm/tpmutil" |
| 25 |
) |
| 26 |
|
| 27 |
// This file provides functions to extract a crypto/rsa public key from a key |
| 28 |
// blob or a TPM_KEY of the right type. It also provides a function for |
| 29 |
// verifying a quote value given a public key for the key it was signed with. |
| 30 |
|
| 31 |
// UnmarshalRSAPublicKey takes in a blob containing a serialized RSA TPM_KEY and |
| 32 |
// converts it to a crypto/rsa.PublicKey. |
| 33 |
func UnmarshalRSAPublicKey(keyBlob []byte) (*rsa.PublicKey, error) { |
| 34 |
// Parse the blob as a key. |
| 35 |
var k key |
| 36 |
if _, err := tpmutil.Unpack(keyBlob, &k); err != nil { |
| 37 |
return nil, err |
| 38 |
} |
| 39 |
|
| 40 |
return k.unmarshalRSAPublicKey() |
| 41 |
} |
| 42 |
|
| 43 |
// unmarshalRSAPublicKey unmarshals a TPM key into a crypto/rsa.PublicKey. |
| 44 |
func (k *key) unmarshalRSAPublicKey() (*rsa.PublicKey, error) { |
| 45 |
// Currently, we only support algRSA |
| 46 |
if k.AlgorithmParams.AlgID != AlgRSA { |
| 47 |
return nil, errors.New("only TPM_ALG_RSA is supported") |
| 48 |
} |
| 49 |
|
| 50 |
// This means that k.AlgorithmsParams.Params is an rsaKeyParams, which is |
| 51 |
// enough to create the exponent, and k.PubKey contains the key. |
| 52 |
var rsakp rsaKeyParams |
| 53 |
if _, err := tpmutil.Unpack(k.AlgorithmParams.Params, &rsakp); err != nil { |
| 54 |
return nil, err |
| 55 |
} |
| 56 |
|
| 57 |
// Make sure that the exponent will fit into an int before using it blindly. |
| 58 |
if len(rsakp.Exponent) > 4 { |
| 59 |
return nil, errors.New("exponent value doesn't fit into an int") |
| 60 |
} |
| 61 |
pk := &rsa.PublicKey{ |
| 62 |
N: new(big.Int).SetBytes(k.PubKey), |
| 63 |
// The exponent isn't set here, but it's fixed to 0x10001 |
| 64 |
E: 0x10001, |
| 65 |
} |
| 66 |
return pk, nil |
| 67 |
} |
| 68 |
|
| 69 |
// UnmarshalPubRSAPublicKey takes in a blob containing a serialized RSA |
| 70 |
// TPM_PUBKEY and converts it to a crypto/rsa.PublicKey. |
| 71 |
func UnmarshalPubRSAPublicKey(keyBlob []byte) (*rsa.PublicKey, error) { |
| 72 |
// Parse the blob as a key. |
| 73 |
var pk pubKey |
| 74 |
if _, err := tpmutil.Unpack(keyBlob, &pk); err != nil { |
| 75 |
return nil, err |
| 76 |
} |
| 77 |
|
| 78 |
return pk.unmarshalRSAPublicKey() |
| 79 |
} |
| 80 |
|
| 81 |
// unmarshalRSAPublicKey unmarshals a TPM pub key into a crypto/rsa.PublicKey. |
| 82 |
// This is almost identical to the identically named function for a TPM key. |
| 83 |
func (pk *pubKey) unmarshalRSAPublicKey() (*rsa.PublicKey, error) { |
| 84 |
// Currently, we only support AlgRSA |
| 85 |
if pk.AlgorithmParams.AlgID != AlgRSA { |
| 86 |
return nil, errors.New("only TPM_ALG_RSA is supported") |
| 87 |
} |
| 88 |
|
| 89 |
// This means that pk.AlgorithmsParams.Params is an rsaKeyParams, which is |
| 90 |
// enough to create the exponent, and pk.Key contains the key. |
| 91 |
var rsakp rsaKeyParams |
| 92 |
if _, err := tpmutil.Unpack(pk.AlgorithmParams.Params, &rsakp); err != nil { |
| 93 |
return nil, err |
| 94 |
} |
| 95 |
|
| 96 |
// Make sure that the exponent will fit into an int before using it blindly. |
| 97 |
if len(rsakp.Exponent) > 4 { |
| 98 |
return nil, errors.New("exponent value doesn't fit into an int") |
| 99 |
} |
| 100 |
rsapk := &rsa.PublicKey{ |
| 101 |
N: new(big.Int).SetBytes(pk.Key), |
| 102 |
// The exponent isn't set here, but it's fixed to 0x10001 |
| 103 |
E: 0x10001, |
| 104 |
} |
| 105 |
return rsapk, nil |
| 106 |
} |
| 107 |
|
| 108 |
// NewQuoteInfo computes a quoteInfo structure for a given pair of data and PCR |
| 109 |
// values. |
| 110 |
func NewQuoteInfo(data []byte, pcrNums []int, pcrs []byte) ([]byte, error) { |
| 111 |
// Compute the composite hash for these PCRs. |
| 112 |
pcrSel, err := newPCRSelection(pcrNums) |
| 113 |
if err != nil { |
| 114 |
return nil, err |
| 115 |
} |
| 116 |
|
| 117 |
comp, err := createPCRComposite(pcrSel.Mask, pcrs) |
| 118 |
if err != nil { |
| 119 |
return nil, err |
| 120 |
} |
| 121 |
|
| 122 |
qi := "eInfo{ |
| 123 |
Version: quoteVersion, |
| 124 |
Fixed: fixedQuote, |
| 125 |
Nonce: sha1.Sum(data), |
| 126 |
} |
| 127 |
copy(qi.CompositeDigest[:], comp) |
| 128 |
|
| 129 |
return tpmutil.Pack(qi) |
| 130 |
} |
| 131 |
|
| 132 |
// VerifyQuote verifies a quote against a given set of PCRs. |
| 133 |
func VerifyQuote(pk *rsa.PublicKey, data []byte, quote []byte, pcrNums []int, pcrs []byte) error { |
| 134 |
p, err := NewQuoteInfo(data, pcrNums, pcrs) |
| 135 |
if err != nil { |
| 136 |
return err |
| 137 |
} |
| 138 |
|
| 139 |
s := sha1.Sum(p) |
| 140 |
|
| 141 |
// Try to do a direct encryption to reverse the value and see if it's padded |
| 142 |
// with PKCS1v1.5. |
| 143 |
return rsa.VerifyPKCS1v15(pk, crypto.SHA1, s[:], quote) |
| 144 |
} |
| 145 |
|
| 146 |
// TODO(tmroeder): add VerifyQuote2 instead of VerifyQuote. This means I'll |
| 147 |
// probably have to look at the signature scheme and use that to choose how to |
| 148 |
// verify the signature, whether PKCS1v1.5 or OAEP. And this will have to be set |
| 149 |
// on the key before it's passed to ordQuote2 |
| 150 |
// TODO(tmroeder): handle key12 |