| 1 |
// Package server contains functions to be ran on a server (no TPM needed), as oppose to a client (with TPM). |
| 2 |
package server |
| 3 |
|
| 4 |
import ( |
| 5 |
"crypto" |
| 6 |
"crypto/aes" |
| 7 |
"crypto/cipher" |
| 8 |
"crypto/elliptic" |
| 9 |
"crypto/hmac" |
| 10 |
"crypto/rand" |
| 11 |
"crypto/rsa" |
| 12 |
"fmt" |
| 13 |
"hash" |
| 14 |
"io" |
| 15 |
|
| 16 |
"github.com/google/go-tpm/legacy/tpm2" |
| 17 |
"github.com/google/go-tpm/tpmutil" |
| 18 |
|
| 19 |
"github.com/google/go-tpm-tools/client" |
| 20 |
"github.com/google/go-tpm-tools/internal" |
| 21 |
pb "github.com/google/go-tpm-tools/proto/tpm" |
| 22 |
) |
| 23 |
|
| 24 |
// CreateImportBlob uses the provided public EK to encrypt the sensitive data. |
| 25 |
// The returned ImportBlob can then be decrypted and imported using the |
| 26 |
// client Key.Import() method. A non-nil pcrs parameter adds a requirement |
| 27 |
// that the TPM must have specific PCR values for Import() to succeed. |
| 28 |
func CreateImportBlob(ekPub crypto.PublicKey, sensitive []byte, pcrs *pb.PCRs) (*pb.ImportBlob, error) { |
| 29 |
ek, err := CreateEKPublicAreaFromKey(ekPub) |
| 30 |
if err != nil { |
| 31 |
return nil, err |
| 32 |
} |
| 33 |
private := createPrivate(sensitive) |
| 34 |
public := createPublic(private) |
| 35 |
|
| 36 |
return createImportBlobHelper(ek, public, private, pcrs) |
| 37 |
} |
| 38 |
|
| 39 |
// CreateSigningKeyImportBlob uses the provided public EK to encrypt the signing |
| 40 |
// key into import blob format. The returned import blob can be used to import |
| 41 |
// the signing key into the TPM associated with the provided EK without exposing |
| 42 |
// the private area to the TPM's OS using the client Key.ImportSigningKey() |
| 43 |
// method. A non-nil pcrs parameter adds a requirement that the TPM must have |
| 44 |
// specific PCR values to use the signing key. |
| 45 |
func CreateSigningKeyImportBlob(ekPub crypto.PublicKey, signingKey crypto.PrivateKey, pcrs *pb.PCRs) (*pb.ImportBlob, error) { |
| 46 |
ek, err := CreateEKPublicAreaFromKey(ekPub) |
| 47 |
if err != nil { |
| 48 |
return nil, err |
| 49 |
} |
| 50 |
public, private, err := createPublicPrivateSign(signingKey) |
| 51 |
if err != nil { |
| 52 |
return nil, err |
| 53 |
} |
| 54 |
|
| 55 |
return createImportBlobHelper(ek, public, private, pcrs) |
| 56 |
} |
| 57 |
|
| 58 |
func createImportBlobHelper(ek, public tpm2.Public, private tpm2.Private, pcrs *pb.PCRs) (*pb.ImportBlob, error) { |
| 59 |
setPublicAuth(&public, pcrs) |
| 60 |
|
| 61 |
var seed, encryptedSeed []byte |
| 62 |
var err error |
| 63 |
switch ek.Type { |
| 64 |
case tpm2.AlgRSA: |
| 65 |
seed, encryptedSeed, err = createRSASeed(ek) |
| 66 |
if err != nil { |
| 67 |
return nil, err |
| 68 |
} |
| 69 |
case tpm2.AlgECC: |
| 70 |
seed, encryptedSeed, err = createECCSeed(ek) |
| 71 |
if err != nil { |
| 72 |
return nil, err |
| 73 |
} |
| 74 |
default: |
| 75 |
return nil, fmt.Errorf("unsupported EK type: %v", ek.Type) |
| 76 |
} |
| 77 |
duplicate, err := createDuplicate(private, seed, public, ek) |
| 78 |
if err != nil { |
| 79 |
return nil, err |
| 80 |
} |
| 81 |
pubEncoded, err := public.Encode() |
| 82 |
if err != nil { |
| 83 |
return nil, err |
| 84 |
} |
| 85 |
|
| 86 |
return &pb.ImportBlob{ |
| 87 |
Duplicate: duplicate, |
| 88 |
EncryptedSeed: encryptedSeed, |
| 89 |
PublicArea: pubEncoded, |
| 90 |
Pcrs: pcrs, |
| 91 |
}, nil |
| 92 |
} |
| 93 |
|
| 94 |
func setPublicAuth(public *tpm2.Public, pcrs *pb.PCRs) { |
| 95 |
if len(pcrs.GetPcrs()) == 0 { |
| 96 |
// Allow password authorization so we can use a nil AuthPolicy. |
| 97 |
public.AuthPolicy = nil |
| 98 |
public.Attributes |= tpm2.FlagUserWithAuth |
| 99 |
} else { |
| 100 |
public.AuthPolicy = internal.PCRSessionAuth(pcrs, client.SessionHashAlg) |
| 101 |
public.Attributes |= tpm2.FlagAdminWithPolicy |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
func createRSASeed(ek tpm2.Public) (seed, encryptedSeed []byte, err error) { |
| 106 |
seedSize := ek.RSAParameters.Symmetric.KeyBits / 8 |
| 107 |
seed = make([]byte, seedSize) |
| 108 |
if _, err := io.ReadFull(rand.Reader, seed); err != nil { |
| 109 |
panic(err) |
| 110 |
} |
| 111 |
|
| 112 |
ekPub, err := ek.Key() |
| 113 |
if err != nil { |
| 114 |
return nil, nil, err |
| 115 |
} |
| 116 |
encryptedSeed, err = rsa.EncryptOAEP( |
| 117 |
getHash(ek.NameAlg), |
| 118 |
rand.Reader, |
| 119 |
ekPub.(*rsa.PublicKey), |
| 120 |
seed, |
| 121 |
[]byte("DUPLICATE\x00")) |
| 122 |
if err != nil { |
| 123 |
return nil, nil, err |
| 124 |
} |
| 125 |
encryptedSeed, err = tpmutil.Pack(encryptedSeed) |
| 126 |
return seed, encryptedSeed, err |
| 127 |
} |
| 128 |
|
| 129 |
func createECCSeed(ek tpm2.Public) (seed, encryptedSeed []byte, err error) { |
| 130 |
curve, err := curveIDToGoCurve(ek.ECCParameters.CurveID) |
| 131 |
if err != nil { |
| 132 |
return nil, nil, err |
| 133 |
} |
| 134 |
//nolint:staticcheck |
| 135 |
// crypto/ecdh does not support P-224, while GCP vTPM supports P224. We should keep the deprecated library till P224 is supported by crypto/ecdh. |
| 136 |
priv, x, y, err := elliptic.GenerateKey(curve, rand.Reader) |
| 137 |
if err != nil { |
| 138 |
return nil, nil, err |
| 139 |
} |
| 140 |
ekPoint := ek.ECCParameters.Point |
| 141 |
//nolint:staticcheck |
| 142 |
// crypto/ecdh does not support P-224, while GCP vTPM supports P224. We should keep the deprecated library till P224 is supported by crypto/ecdh. |
| 143 |
z, _ := curve.ScalarMult(ekPoint.X(), ekPoint.Y(), priv) |
| 144 |
xBytes := eccIntToBytes(curve, x) |
| 145 |
|
| 146 |
seed, err = tpm2.KDFe( |
| 147 |
ek.NameAlg, |
| 148 |
eccIntToBytes(curve, z), |
| 149 |
"DUPLICATE", |
| 150 |
xBytes, |
| 151 |
eccIntToBytes(curve, ekPoint.X()), |
| 152 |
getHash(ek.NameAlg).Size()*8) |
| 153 |
if err != nil { |
| 154 |
return nil, nil, err |
| 155 |
} |
| 156 |
encryptedSeed, err = tpmutil.Pack(tpmutil.U16Bytes(xBytes), tpmutil.U16Bytes(eccIntToBytes(curve, y))) |
| 157 |
return seed, encryptedSeed, err |
| 158 |
} |
| 159 |
|
| 160 |
func createDuplicate(private tpm2.Private, seed []byte, public, ek tpm2.Public) ([]byte, error) { |
| 161 |
nameEncoded, err := getEncodedName(public) |
| 162 |
if err != nil { |
| 163 |
return nil, err |
| 164 |
} |
| 165 |
secret, err := private.Encode() |
| 166 |
if err != nil { |
| 167 |
return nil, err |
| 168 |
} |
| 169 |
packedSecret, err := tpmutil.Pack(tpmutil.U16Bytes(secret)) |
| 170 |
if err != nil { |
| 171 |
return nil, err |
| 172 |
} |
| 173 |
encryptedSecret, err := encryptSecret(packedSecret, seed, nameEncoded, ek) |
| 174 |
if err != nil { |
| 175 |
return nil, err |
| 176 |
} |
| 177 |
macSum, err := createHMAC(encryptedSecret, nameEncoded, seed, ek.NameAlg) |
| 178 |
if err != nil { |
| 179 |
return nil, err |
| 180 |
} |
| 181 |
return tpmutil.Pack(tpm2.IDObject{ |
| 182 |
IntegrityHMAC: macSum, |
| 183 |
EncIdentity: encryptedSecret, |
| 184 |
}) |
| 185 |
} |
| 186 |
|
| 187 |
func getEncodedName(public tpm2.Public) ([]byte, error) { |
| 188 |
name, err := public.Name() |
| 189 |
if err != nil { |
| 190 |
return nil, err |
| 191 |
} |
| 192 |
return name.Digest.Encode() |
| 193 |
} |
| 194 |
|
| 195 |
func encryptSecret(secret, seed, nameEncoded []byte, ek tpm2.Public) ([]byte, error) { |
| 196 |
var symSize int |
| 197 |
switch ek.Type { |
| 198 |
case tpm2.AlgRSA: |
| 199 |
symSize = int(ek.RSAParameters.Symmetric.KeyBits) |
| 200 |
case tpm2.AlgECC: |
| 201 |
symSize = int(ek.ECCParameters.Symmetric.KeyBits) |
| 202 |
default: |
| 203 |
return nil, fmt.Errorf("unsupported EK type: %v", ek.Type) |
| 204 |
} |
| 205 |
|
| 206 |
symmetricKey, err := tpm2.KDFa( |
| 207 |
ek.NameAlg, |
| 208 |
seed, |
| 209 |
"STORAGE", |
| 210 |
nameEncoded, |
| 211 |
/*contextV=*/ nil, |
| 212 |
symSize) |
| 213 |
if err != nil { |
| 214 |
return nil, err |
| 215 |
} |
| 216 |
c, err := aes.NewCipher(symmetricKey) |
| 217 |
if err != nil { |
| 218 |
return nil, err |
| 219 |
} |
| 220 |
encSecret := make([]byte, len(secret)) |
| 221 |
// The TPM spec requires an all-zero IV. |
| 222 |
iv := make([]byte, len(symmetricKey)) |
| 223 |
//nolint:staticcheck |
| 224 |
// crypto/cipher.NewCFBEncrypter is deprecated, but we need to use it to be compatible with the TPM spec. |
| 225 |
cipher.NewCFBEncrypter(c, iv).XORKeyStream(encSecret, secret) |
| 226 |
return encSecret, nil |
| 227 |
} |
| 228 |
|
| 229 |
func createHMAC(encryptedSecret, nameEncoded, seed []byte, hashAlg tpm2.Algorithm) ([]byte, error) { |
| 230 |
macKey, err := tpm2.KDFa( |
| 231 |
hashAlg, |
| 232 |
seed, |
| 233 |
"INTEGRITY", |
| 234 |
/*contextU=*/ nil, |
| 235 |
/*contextV=*/ nil, |
| 236 |
getHash(hashAlg).Size()*8) |
| 237 |
if err != nil { |
| 238 |
return nil, err |
| 239 |
} |
| 240 |
mac := hmac.New(func() hash.Hash { return getHash(hashAlg) }, macKey) |
| 241 |
mac.Write(encryptedSecret) |
| 242 |
mac.Write(nameEncoded) |
| 243 |
return mac.Sum(nil), nil |
| 244 |
} |
| 245 |
|
| 246 |
func getHash(hashAlg tpm2.Algorithm) hash.Hash { |
| 247 |
create, err := hashAlg.Hash() |
| 248 |
if err != nil { |
| 249 |
panic(err) |
| 250 |
} |
| 251 |
return create.New() |
| 252 |
} |