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 !secureCurves[pub.ECCParameters.CurveID] {
90 return fmt.Errorf("attestation key uses insecure curve")
91 }
92 if len(pub.ECCParameters.Point.XRaw)*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.XRaw)*8)
94 }
95 if len(pub.ECCParameters.Point.YRaw)*8 < minECCBits {
96 return fmt.Errorf("attestation key too small: must be at least %d bits but was %d bits", minECCBits, len(pub.ECCParameters.Point.YRaw)*8)
97 }
98 default:
99 return fmt.Errorf("public key of alg 0x%x not supported", pub.Type)
100 }
101
102 // Compute & verify that the creation data matches the digest in the
103 // attestation structure.
104 nameHash, err := pub.NameAlg.Hash()
105 if err != nil {
106 return fmt.Errorf("HashConstructor() failed: %v", err)
107 }
108 h := nameHash.New()
109 h.Write(p.AK.CreateData)
110 if !bytes.Equal(att.AttestedCreationInfo.OpaqueDigest, h.Sum(nil)) {
111 return errors.New("attestation refers to different public key")
112 }
113
114 // Make sure the AK has sane key parameters (Attestation can be faked if an AK
115 // can be used for arbitrary signatures).
116 // We verify the following:
117 // - Key is TPM backed.
118 // - Key is TPM generated.
119 // - Key is a restricted key (means it cannot do arbitrary signing/decrypt ops).
120 // - Key cannot be duplicated.
121 // - Key was generated by a call to TPM_Create*.
122 if att.Magic != generatedMagic {
123 return errors.New("creation attestation was not produced by a TPM")
124 }
125 if (pub.Attributes & tpm2.FlagFixedTPM) == 0 {
126 return errors.New("AK is exportable")
127 }
128 if ((pub.Attributes & tpm2.FlagRestricted) == 0) || ((pub.Attributes & tpm2.FlagFixedParent) == 0) || ((pub.Attributes & tpm2.FlagSensitiveDataOrigin) == 0) {
129 return errors.New("provided key is not limited to attestation")
130 }
131
132 // Verify the attested creation name matches what is computed from
133 // the public key.
134 match, err := att.AttestedCreationInfo.Name.MatchesPublic(pub)
135 if err != nil {
136 return err
137 }
138 if !match {
139 return errors.New("creation attestation refers to a different key")
140 }
141
142 // Check the signature over the attestation data verifies correctly.
143 switch pub.Type {
144 case tpm2.AlgRSA:
145 return verifyRSASignature(pub, p)
146 case tpm2.AlgECC:
147 return verifyECDSASignature(pub, p)
148 default:
149 return fmt.Errorf("public key of alg 0x%x not supported", pub.Type)
150 }
151 }
152
153 func verifyRSASignature(pub tpm2.Public, p *ActivationParameters) error {
154 pk := rsa.PublicKey{E: int(pub.RSAParameters.Exponent()), N: pub.RSAParameters.Modulus()}
155 signHash, err := pub.RSAParameters.Sign.Hash.Hash()
156 if err != nil {
157 return err
158 }
159 hsh := signHash.New()
160 hsh.Write(p.AK.CreateAttestation)
161 verifyHash, err := pub.RSAParameters.Sign.Hash.Hash()
162 if err != nil {
163 return err
164 }
165
166 if len(p.AK.CreateSignature) < 8 {
167 return fmt.Errorf("signature invalid: length of %d is shorter than 8", len(p.AK.CreateSignature))
168 }
169
170 sig, err := tpm2.DecodeSignature(bytes.NewBuffer(p.AK.CreateSignature))
171 if err != nil {
172 return fmt.Errorf("DecodeSignature() failed: %v", err)
173 }
174 if sig.RSA == nil {
175 return fmt.Errorf("rsa public key provided for non-rsa signature")
176 }
177
178 if err := rsa.VerifyPKCS1v15(&pk, verifyHash, hsh.Sum(nil), sig.RSA.Signature); err != nil {
179 return fmt.Errorf("could not verify attestation: %v", err)
180 }
181
182 return nil
183 }
184
185 func verifyECDSASignature(pub tpm2.Public, p *ActivationParameters) error {
186 key, err := pub.Key()
187 if err != nil {
188 return fmt.Errorf("failed to extract ECC public key: %v", err)
189 }
190 pk, ok := key.(*ecdsa.PublicKey)
191 if !ok {
192 return fmt.Errorf("expected *ecdsa.PublicKey, got %T", key)
193 }
194 signHash, err := pub.ECCParameters.Sign.Hash.Hash()
195 if err != nil {
196 return err
197 }
198 hsh := signHash.New()
199 _, err = hsh.Write(p.AK.CreateAttestation)
200 if err != nil {
201 return err
202 }
203
204 if len(p.AK.CreateSignature) < 8 {
205 return fmt.Errorf("signature invalid: length of %d is shorter than 8", len(p.AK.CreateSignature))
206 }
207
208 sig, err := tpm2.DecodeSignature(bytes.NewBuffer(p.AK.CreateSignature))
209 if err != nil {
210 return fmt.Errorf("DecodeSignature() failed: %v", err)
211 }
212 if sig.ECC == nil {
213 return fmt.Errorf("ecdsa public key provided for non-ecdsa signature")
214 }
215
216 if !ecdsa.Verify(pk, hsh.Sum(nil), sig.ECC.R, sig.ECC.S) {
217 return fmt.Errorf("unable to verify attestation for ecdsa credential")
218 }
219 return nil
220 }
221
222 // Generate returns a credential activation challenge, which can be provided
223 // to the TPM to verify the AK parameters given are authentic & the AK
224 // is present on the same TPM as the EK.
225 //
226 // It will call CheckAKParameters() at the beginning and return the error if
227 // there is any.
228 //
229 // The caller is expected to verify the secret returned from the TPM as
230 // as result of calling ActivateCredential() matches the secret returned here.
231 // The caller should use subtle.ConstantTimeCompare to avoid potential
232 // timing attack vectors.
233 func (p *ActivationParameters) Generate() (secret []byte, ec *EncryptedCredential, err error) {
234 if err := p.CheckAKParameters(); err != nil {
235 return nil, nil, err
236 }
237
238 if p.EK == nil {
239 return nil, nil, errors.New("no EK provided")
240 }
241
242 rnd, secret := p.Rand, make([]byte, activationSecretLen)
243 if rnd == nil {
244 rnd = rand.Reader
245 }
246 if _, err = io.ReadFull(rnd, secret); err != nil {
247 return nil, nil, fmt.Errorf("error generating activation secret: %v", err)
248 }
249
250 ec, err = p.generateChallenge(secret)
251 if err != nil {
252 return nil, nil, err
253 }
254 return secret, ec, nil
255 }
256
257 func (p *ActivationParameters) generateChallenge(secret []byte) (*EncryptedCredential, error) {
258 att, err := tpm2.DecodeAttestationData(p.AK.CreateAttestation)
259 if err != nil {
260 return nil, fmt.Errorf("DecodeAttestationData() failed: %v", err)
261 }
262 if att.AttestedCreationInfo == nil {
263 return nil, fmt.Errorf("attestation was not for a creation event")
264 }
265 if att.AttestedCreationInfo.Name.Digest == nil {
266 return nil, fmt.Errorf("attestation creation info name has no digest")
267 }
268 cred, encSecret, err := credactivation.Generate(att.AttestedCreationInfo.Name.Digest, p.EK, symBlockSize, secret)
269 if err != nil {
270 return nil, fmt.Errorf("credactivation.Generate() failed: %v", err)
271 }
272
273 return &EncryptedCredential{
274 Credential: cred,
275 Secret: encSecret,
276 }, nil
277 }