Debian Codegraph @

attest/activation.go

raw
1 package attest
2
3 import (
4 "bytes"
5 "crypto"
6 "crypto/ecdsa"
7 "crypto/rand"
8 "crypto/rsa"
9 "errors"
10 "fmt"
11 "io"
12
13 "github.com/google/go-tpm/legacy/tpm2"
14
15 // TODO(jsonp): Move activation generation code to internal package.
16 "github.com/google/go-tpm/legacy/tpm2/credactivation"
17 )
18
19 const (
20 // minRSABits is the minimum accepted bit size of an RSA key.
21 minRSABits = 2048
22 // minECCBits is the minimum accepted bit size of an ECC key.
23 minECCBits = 256
24 // activationSecretLen is the size in bytes of the generated secret
25 // which is generated for credential activation.
26 activationSecretLen = 32
27 // symBlockSize is the block size used for symmetric ciphers used
28 // when generating the credential activation challenge.
29 symBlockSize = 16
30 // generatedMagic is a magic tag when can only be present on a
31 // TPM structure if the structure was generated wholly by the TPM.
32 generatedMagic = 0xff544347
33 )
34
35 // ActivationParameters encapsulates the inputs for activating an AK.
36 type ActivationParameters struct {
37 // EK, the endorsement key, describes an asymmetric key whose
38 // private key is permanently bound to the TPM.
39 //
40 // Activation will verify that the provided EK is held on the same
41 // TPM as the AK. However, it is the caller's responsibility to
42 // ensure the EK they provide corresponds to the the device which
43 // they are trying to associate the AK with.
44 EK crypto.PublicKey
45
46 // AK, the Attestation Key, describes the properties of
47 // an asymmetric key (managed by the TPM) which signs attestation
48 // structures.
49 // The values from this structure can be obtained by calling
50 // Parameters() on an attest.AK.
51 AK AttestationParameters
52
53 // Rand is a source of randomness to generate a seed and secret for the
54 // challenge.
55 //
56 // If nil, this defaults to crypto.Rand.
57 Rand io.Reader
58 }
59
60 // CheckAKParameters examines properties of an AK and a creation
61 // attestation, to determine if it is suitable for use as an attestation key.
62 func (p *ActivationParameters) CheckAKParameters() error {
63 if len(p.AK.CreateSignature) < 8 {
64 return fmt.Errorf("signature is too short to be valid: only %d bytes", len(p.AK.CreateSignature))
65 }
66
67 pub, err := tpm2.DecodePublic(p.AK.Public)
68 if err != nil {
69 return fmt.Errorf("DecodePublic() failed: %v", err)
70 }
71 _, err = tpm2.DecodeCreationData(p.AK.CreateData)
72 if err != nil {
73 return fmt.Errorf("DecodeCreationData() failed: %v", err)
74 }
75 att, err := tpm2.DecodeAttestationData(p.AK.CreateAttestation)
76 if err != nil {
77 return fmt.Errorf("DecodeAttestationData() failed: %v", err)
78 }
79 if att.Type != tpm2.TagAttestCreation {
80 return fmt.Errorf("attestation does not apply to creation data, got tag %x", att.Type)
81 }
82
83 switch pub.Type {
84 case tpm2.AlgRSA:
85 if pub.RSAParameters.KeyBits < minRSABits {
86 return fmt.Errorf("attestation key too small: must be at least %d bits but was %d bits", minRSABits, pub.RSAParameters.KeyBits)
87 }
88 case tpm2.AlgECC:
89 if len(pub.ECCParameters.Point.XRaw)*8 < minECCBits {
90 return fmt.Errorf("attestation key too small: must be at least %d bits but was %d bits", minECCBits, len(pub.ECCParameters.Point.XRaw)*8)
91 }
92 if len(pub.ECCParameters.Point.YRaw)*8 < minECCBits {
93 return fmt.Errorf("attestation key too small: must be at least %d bits but was %d bits", minECCBits, len(pub.ECCParameters.Point.YRaw)*8)
94 }
95 default:
96 return fmt.Errorf("public key of alg 0x%x not supported", pub.Type)
97 }
98
99 // Compute & verify that the creation data matches the digest in the
100 // attestation structure.
101 nameHash, err := pub.NameAlg.Hash()
102 if err != nil {
103 return fmt.Errorf("HashConstructor() failed: %v", err)
104 }
105 h := nameHash.New()
106 h.Write(p.AK.CreateData)
107 if !bytes.Equal(att.AttestedCreationInfo.OpaqueDigest, h.Sum(nil)) {
108 return errors.New("attestation refers to different public key")
109 }
110
111 // Make sure the AK has sane key parameters (Attestation can be faked if an AK
112 // can be used for arbitrary signatures).
113 // We verify the following:
114 // - Key is TPM backed.
115 // - Key is TPM generated.
116 // - Key is a restricted key (means it cannot do arbitrary signing/decrypt ops).
117 // - Key cannot be duplicated.
118 // - Key was generated by a call to TPM_Create*.
119 if att.Magic != generatedMagic {
120 return errors.New("creation attestation was not produced by a TPM")
121 }
122 if (pub.Attributes & tpm2.FlagFixedTPM) == 0 {
123 return errors.New("AK is exportable")
124 }
125 if ((pub.Attributes & tpm2.FlagRestricted) == 0) || ((pub.Attributes & tpm2.FlagFixedParent) == 0) || ((pub.Attributes & tpm2.FlagSensitiveDataOrigin) == 0) {
126 return errors.New("provided key is not limited to attestation")
127 }
128
129 // Verify the attested creation name matches what is computed from
130 // the public key.
131 match, err := att.AttestedCreationInfo.Name.MatchesPublic(pub)
132 if err != nil {
133 return err
134 }
135 if !match {
136 return errors.New("creation attestation refers to a different key")
137 }
138
139 // Check the signature over the attestation data verifies correctly.
140 switch pub.Type {
141 case tpm2.AlgRSA:
142 return verifyRSASignature(pub, p)
143 case tpm2.AlgECC:
144 return verifyECDSASignature(pub, p)
145 default:
146 return fmt.Errorf("public key of alg 0x%x not supported", pub.Type)
147 }
148 }
149
150 func verifyRSASignature(pub tpm2.Public, p *ActivationParameters) error {
151 pk := rsa.PublicKey{E: int(pub.RSAParameters.Exponent()), N: pub.RSAParameters.Modulus()}
152 signHash, err := pub.RSAParameters.Sign.Hash.Hash()
153 if err != nil {
154 return err
155 }
156 hsh := signHash.New()
157 hsh.Write(p.AK.CreateAttestation)
158 verifyHash, err := pub.RSAParameters.Sign.Hash.Hash()
159 if err != nil {
160 return err
161 }
162
163 if len(p.AK.CreateSignature) < 8 {
164 return fmt.Errorf("signature invalid: length of %d is shorter than 8", len(p.AK.CreateSignature))
165 }
166
167 sig, err := tpm2.DecodeSignature(bytes.NewBuffer(p.AK.CreateSignature))
168 if err != nil {
169 return fmt.Errorf("DecodeSignature() failed: %v", err)
170 }
171
172 if err := rsa.VerifyPKCS1v15(&pk, verifyHash, hsh.Sum(nil), sig.RSA.Signature); err != nil {
173 return fmt.Errorf("could not verify attestation: %v", err)
174 }
175
176 return nil
177 }
178
179 func verifyECDSASignature(pub tpm2.Public, p *ActivationParameters) error {
180 key, err := pub.Key()
181 if err != nil {
182 return fmt.Errorf("failed to extract ECC public key: %v", err)
183 }
184 pk, ok := key.(*ecdsa.PublicKey)
185 if !ok {
186 return fmt.Errorf("expected *ecdsa.PublicKey, got %T", key)
187 }
188 signHash, err := pub.ECCParameters.Sign.Hash.Hash()
189 if err != nil {
190 return err
191 }
192 hsh := signHash.New()
193 _, err = hsh.Write(p.AK.CreateAttestation)
194 if err != nil {
195 return err
196 }
197
198 if len(p.AK.CreateSignature) < 8 {
199 return fmt.Errorf("signature invalid: length of %d is shorter than 8", len(p.AK.CreateSignature))
200 }
201
202 sig, err := tpm2.DecodeSignature(bytes.NewBuffer(p.AK.CreateSignature))
203 if err != nil {
204 return fmt.Errorf("DecodeSignature() failed: %v", err)
205 }
206
207 if !ecdsa.Verify(pk, hsh.Sum(nil), sig.ECC.R, sig.ECC.S) {
208 return fmt.Errorf("unable to verify attestation for ecdsa credential")
209 }
210 return nil
211 }
212
213 // Generate returns a credential activation challenge, which can be provided
214 // to the TPM to verify the AK parameters given are authentic & the AK
215 // is present on the same TPM as the EK.
216 //
217 // It will call CheckAKParameters() at the beginning and return the error if
218 // there is any.
219 //
220 // The caller is expected to verify the secret returned from the TPM as
221 // as result of calling ActivateCredential() matches the secret returned here.
222 // The caller should use subtle.ConstantTimeCompare to avoid potential
223 // timing attack vectors.
224 func (p *ActivationParameters) Generate() (secret []byte, ec *EncryptedCredential, err error) {
225 if err := p.CheckAKParameters(); err != nil {
226 return nil, nil, err
227 }
228
229 if p.EK == nil {
230 return nil, nil, errors.New("no EK provided")
231 }
232
233 rnd, secret := p.Rand, make([]byte, activationSecretLen)
234 if rnd == nil {
235 rnd = rand.Reader
236 }
237 if _, err = io.ReadFull(rnd, secret); err != nil {
238 return nil, nil, fmt.Errorf("error generating activation secret: %v", err)
239 }
240
241 ec, err = p.generateChallenge(secret)
242 if err != nil {
243 return nil, nil, err
244 }
245 return secret, ec, nil
246 }
247
248 func (p *ActivationParameters) generateChallenge(secret []byte) (*EncryptedCredential, error) {
249 att, err := tpm2.DecodeAttestationData(p.AK.CreateAttestation)
250 if err != nil {
251 return nil, fmt.Errorf("DecodeAttestationData() failed: %v", err)
252 }
253 if att.AttestedCreationInfo == nil {
254 return nil, fmt.Errorf("attestation was not for a creation event")
255 }
256 if att.AttestedCreationInfo.Name.Digest == nil {
257 return nil, fmt.Errorf("attestation creation info name has no digest")
258 }
259 cred, encSecret, err := credactivation.Generate(att.AttestedCreationInfo.Name.Digest, p.EK, symBlockSize, secret)
260 if err != nil {
261 return nil, fmt.Errorf("credactivation.Generate() failed: %v", err)
262 }
263
264 return &EncryptedCredential{
265 Credential: cred,
266 Secret: encSecret,
267 }, nil
268 }