Debian Codegraph @

tpm/commands.go

raw
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 "encoding/binary"
19 "errors"
20 "io"
21
22 "github.com/google/go-tpm/tpmutil"
23 )
24
25 // submitTPMRequest sends a structure to the TPM device file and gets results
26 // back, interpreting them as a new provided structure.
27 func submitTPMRequest(rw io.ReadWriter, tag uint16, ord uint32, in []interface{}, out []interface{}) (uint32, error) {
28 resp, code, err := tpmutil.RunCommand(rw, tpmutil.Tag(tag), tpmutil.Command(ord), in...)
29 if err != nil {
30 return 0, err
31 }
32 if code != tpmutil.RCSuccess {
33 return uint32(code), tpmError(code)
34 }
35
36 _, err = tpmutil.Unpack(resp, out...)
37 return 0, err
38 }
39
40 // oiap sends an OIAP command to the TPM and gets back an auth value and a
41 // nonce.
42 func oiap(rw io.ReadWriter) (*oiapResponse, error) {
43 var resp oiapResponse
44 out := []interface{}{&resp}
45 // In this case, we don't need to check ret, since all the information is
46 // contained in err.
47 if _, err := submitTPMRequest(rw, tagRQUCommand, ordOIAP, nil, out); err != nil {
48 return nil, err
49 }
50
51 return &resp, nil
52 }
53
54 // osap sends an OSAPCommand to the TPM and gets back authentication
55 // information in an OSAPResponse.
56 func osap(rw io.ReadWriter, osap *osapCommand) (*osapResponse, error) {
57 in := []interface{}{osap}
58 var resp osapResponse
59 out := []interface{}{&resp}
60 // In this case, we don't need to check the ret value, since all the
61 // information is contained in err.
62 if _, err := submitTPMRequest(rw, tagRQUCommand, ordOSAP, in, out); err != nil {
63 return nil, err
64 }
65
66 return &resp, nil
67 }
68
69 // seal performs a seal operation on the TPM.
70 func seal(rw io.ReadWriter, sc *sealCommand, pcrs *pcrInfoLong, data tpmutil.U32Bytes, ca *commandAuth) (*tpmStoredData, *responseAuth, uint32, error) {
71 pcrsize := binary.Size(pcrs)
72 if pcrsize < 0 {
73 return nil, nil, 0, errors.New("couldn't compute the size of a pcrInfoLong")
74 }
75
76 // TODO(tmroeder): special-case pcrInfoLong in pack/unpack so we don't have
77 // to write out the length explicitly here.
78 in := []interface{}{sc, uint32(pcrsize), pcrs, data, ca}
79
80 var tsd tpmStoredData
81 var ra responseAuth
82 out := []interface{}{&tsd, &ra}
83 ret, err := submitTPMRequest(rw, tagRQUAuth1Command, ordSeal, in, out)
84 if err != nil {
85 return nil, nil, 0, err
86 }
87
88 return &tsd, &ra, ret, nil
89 }
90
91 // unseal data sealed by the TPM.
92 func unseal(rw io.ReadWriter, keyHandle tpmutil.Handle, sealed *tpmStoredData, ca1 *commandAuth, ca2 *commandAuth) ([]byte, *responseAuth, *responseAuth, uint32, error) {
93 in := []interface{}{keyHandle, sealed, ca1, ca2}
94 var outb tpmutil.U32Bytes
95 var ra1 responseAuth
96 var ra2 responseAuth
97 out := []interface{}{&outb, &ra1, &ra2}
98 ret, err := submitTPMRequest(rw, tagRQUAuth2Command, ordUnseal, in, out)
99 if err != nil {
100 return nil, nil, nil, 0, err
101 }
102
103 return outb, &ra1, &ra2, ret, nil
104 }
105
106 // authorizeMigrationKey authorizes a public key for migrations.
107 func authorizeMigrationKey(rw io.ReadWriter, migrationScheme MigrationScheme, migrationKey pubKey, ca *commandAuth) ([]byte, *responseAuth, uint32, error) {
108 in := []interface{}{migrationScheme, migrationKey, ca}
109 var ra responseAuth
110 var migrationAuth migrationKeyAuth
111 out := []interface{}{&migrationAuth, &ra}
112 ret, err := submitTPMRequest(rw, tagRQUAuth1Command, ordAuthorizeMigrationKey, in, out)
113 if err != nil {
114 return nil, nil, 0, err
115 }
116 authBlob, err := tpmutil.Pack(migrationAuth)
117 if err != nil {
118 return nil, nil, 0, err
119 }
120
121 return authBlob, &ra, ret, nil
122 }
123
124 // createMigrationBlob migrates a key from the TPM.
125 func createMigrationBlob(rw io.ReadWriter, parentHandle tpmutil.Handle, migrationScheme MigrationScheme, migrationKey []byte, encData tpmutil.U32Bytes, ca1 *commandAuth, ca2 *commandAuth) ([]byte, []byte, *responseAuth, *responseAuth, uint32, error) {
126 in := []interface{}{parentHandle, migrationScheme, migrationKey, encData, ca1, ca2}
127 var rand tpmutil.U32Bytes
128 var outData tpmutil.U32Bytes
129 var ra1 responseAuth
130 var ra2 responseAuth
131 out := []interface{}{&rand, &outData, &ra1, &ra2}
132 ret, err := submitTPMRequest(rw, tagRQUAuth2Command, ordCreateMigrationBlob, in, out)
133 if err != nil {
134 return nil, nil, nil, nil, 0, err
135 }
136
137 return rand, outData, &ra1, &ra2, ret, nil
138 }
139
140 // flushSpecific removes a handle from the TPM. Note that removing a handle
141 // doesn't require any authentication.
142 func flushSpecific(rw io.ReadWriter, handle tpmutil.Handle, resourceType uint32) error {
143 // In this case, all the information is in err, so we don't check the
144 // specific return-value details.
145 _, err := submitTPMRequest(rw, tagRQUCommand, ordFlushSpecific, []interface{}{handle, resourceType}, nil)
146 return err
147 }
148
149 // loadKey2 loads a key into the TPM. It's a tagRQUAuth1Command, so it only
150 // needs one auth parameter.
151 // TODO(tmroeder): support key12, too.
152 func loadKey2(rw io.ReadWriter, k *key, ca *commandAuth) (tpmutil.Handle, *responseAuth, uint32, error) {
153 // We always load our keys with the SRK as the parent key.
154 in := []interface{}{khSRK, k, ca}
155 var keyHandle tpmutil.Handle
156 var ra responseAuth
157 out := []interface{}{&keyHandle, &ra}
158 ret, err := submitTPMRequest(rw, tagRQUAuth1Command, ordLoadKey2, in, out)
159 if err != nil {
160 return 0, nil, 0, err
161 }
162
163 return keyHandle, &ra, ret, nil
164 }
165
166 // getPubKey gets a public key from the TPM
167 func getPubKey(rw io.ReadWriter, keyHandle tpmutil.Handle, ca *commandAuth) (*pubKey, *responseAuth, uint32, error) {
168 in := []interface{}{keyHandle, ca}
169 var pk pubKey
170 var ra responseAuth
171 out := []interface{}{&pk, &ra}
172 ret, err := submitTPMRequest(rw, tagRQUAuth1Command, ordGetPubKey, in, out)
173 if err != nil {
174 return nil, nil, 0, err
175 }
176
177 return &pk, &ra, ret, nil
178 }
179
180 // getCapability reads the requested capability and sub-capability from the TPM
181 func getCapability(rw io.ReadWriter, cap, subcap uint32) ([]byte, error) {
182 subCapBytes, err := tpmutil.Pack(subcap)
183 if err != nil {
184 return nil, err
185 }
186 var b tpmutil.U32Bytes
187 in := []interface{}{cap, tpmutil.U32Bytes(subCapBytes)}
188 out := []interface{}{&b}
189 if _, err := submitTPMRequest(rw, tagRQUCommand, ordGetCapability, in, out); err != nil {
190 return nil, err
191 }
192 return b, nil
193 }
194
195 // nvDefineSpace allocates space in NVRAM
196 func nvDefineSpace(rw io.ReadWriter, nvData NVDataPublic, enc Digest, ca *commandAuth) (*responseAuth, uint32, error) {
197 var ra responseAuth
198 in := []interface{}{nvData, enc}
199 if ca != nil {
200 in = append(in, ca)
201 }
202 out := []interface{}{&ra}
203 ret, err := submitTPMRequest(rw, tagRQUAuth1Command, ordNVDefineSpace, in, out)
204 if err != nil {
205 return nil, 0, err
206 }
207 return &ra, ret, nil
208
209 }
210
211 // nvReadValue reads from the NVRAM
212 // If TPM isn't locked, and for some nv permission no authentication is needed.
213 // See TPM-Main-Part-3-Commands-20.4
214 func nvReadValue(rw io.ReadWriter, index, offset, len uint32, ca *commandAuth) ([]byte, *responseAuth, uint32, error) {
215 var b tpmutil.U32Bytes
216 var ra responseAuth
217 var ret uint32
218 var err error
219 in := []interface{}{index, offset, len}
220 out := []interface{}{&b}
221 // Auth is needed
222 if ca != nil {
223 in = append(in, ca)
224 out = append(out, ra)
225 ret, err = submitTPMRequest(rw, tagRQUAuth1Command, ordNVReadValue, in, out)
226 } else {
227 // Auth is not needed
228 ret, err = submitTPMRequest(rw, tagRQUCommand, ordNVReadValue, in, out)
229 }
230 if err != nil {
231 return nil, nil, 0, err
232 }
233 return b, &ra, ret, nil
234 }
235
236 // nvReadValueAuth reads nvram with enforced authentication.
237 // No Owner needs to be present.
238 // See TPM-Main-Part-3-Commands-20.5
239 func nvReadValueAuth(rw io.ReadWriter, index, offset, len uint32, ca *commandAuth) ([]byte, *responseAuth, uint32, error) {
240 var b tpmutil.U32Bytes
241 var ra responseAuth
242 in := []interface{}{index, offset, len, ca}
243 out := []interface{}{&b, &ra}
244 ret, err := submitTPMRequest(rw, tagRQUAuth1Command, ordNVReadValueAuth, in, out)
245 if err != nil {
246 return nil, nil, 0, err
247 }
248 return b, &ra, ret, nil
249 }
250
251 // nvWriteValue writes to the NVRAM
252 // If TPM isn't locked, no authentication is needed.
253 // See TPM-Main-Part-3-Commands-20.2
254 func nvWriteValue(rw io.ReadWriter, index, offset, len uint32, data []byte, ca *commandAuth) ([]byte, *responseAuth, uint32, error) {
255 var b tpmutil.U32Bytes
256 var ra responseAuth
257 in := []interface{}{index, offset, len, data}
258 if ca != nil {
259 in = append(in, ca)
260 }
261 out := []interface{}{&b, &ra}
262 ret, err := submitTPMRequest(rw, tagRQUAuth1Command, ordNVWriteValue, in, out)
263 if err != nil {
264 return nil, nil, 0, err
265 }
266 return b, &ra, ret, nil
267 }
268
269 // nvWriteValue writes to the NVRAM
270 // If TPM isn't locked, no authentication is needed.
271 // See TPM-Main-Part-3-Commands-20.3
272 func nvWriteValueAuth(rw io.ReadWriter, index, offset, len uint32, data []byte, ca *commandAuth) ([]byte, *responseAuth, uint32, error) {
273 var b tpmutil.U32Bytes
274 var ra responseAuth
275 in := []interface{}{index, offset, len, data, ca}
276 out := []interface{}{&b, &ra}
277 ret, err := submitTPMRequest(rw, tagRQUAuth1Command, ordNVWriteValueAuth, in, out)
278 if err != nil {
279 return nil, nil, 0, err
280 }
281 return b, &ra, ret, nil
282 }
283
284 // quote2 signs arbitrary data under a given set of PCRs and using a key
285 // specified by keyHandle. It returns information about the PCRs it signed
286 // under, the signature, auth information, and optionally information about the
287 // TPM itself. Note that the input to quote2 must be exactly 20 bytes, so it is
288 // normally the SHA1 hash of the data.
289 func quote2(rw io.ReadWriter, keyHandle tpmutil.Handle, hash [20]byte, pcrs *pcrSelection, addVersion byte, ca *commandAuth) (*pcrInfoShort, *CapVersionInfo, []byte, []byte, *responseAuth, uint32, error) {
290 in := []interface{}{keyHandle, hash, pcrs, addVersion, ca}
291 var pcrShort pcrInfoShort
292 var capInfo CapVersionInfo
293 var capBytes tpmutil.U32Bytes
294 var sig tpmutil.U32Bytes
295 var ra responseAuth
296 out := []interface{}{&pcrShort, &capBytes, &sig, &ra}
297 ret, err := submitTPMRequest(rw, tagRQUAuth1Command, ordQuote2, in, out)
298 if err != nil {
299 return nil, nil, nil, nil, nil, 0, err
300 }
301
302 // Deserialize the capInfo, if any.
303 if len([]byte(capBytes)) == 0 {
304 return &pcrShort, nil, capBytes, sig, &ra, ret, nil
305 }
306
307 err = capInfo.Decode(capBytes)
308 if err != nil {
309 return nil, nil, nil, nil, nil, 0, err
310 }
311
312 return &pcrShort, &capInfo, capBytes, sig, &ra, ret, nil
313 }
314
315 // quote performs a TPM 1.1 quote operation: it signs data using the
316 // TPM_QUOTE_INFO structure for the current values of a selected set of PCRs.
317 func quote(rw io.ReadWriter, keyHandle tpmutil.Handle, hash [20]byte, pcrs *pcrSelection, ca *commandAuth) (*pcrComposite, []byte, *responseAuth, uint32, error) {
318 in := []interface{}{keyHandle, hash, pcrs, ca}
319 var pcrc pcrComposite
320 var sig tpmutil.U32Bytes
321 var ra responseAuth
322 out := []interface{}{&pcrc, &sig, &ra}
323 ret, err := submitTPMRequest(rw, tagRQUAuth1Command, ordQuote, in, out)
324 if err != nil {
325 return nil, nil, nil, 0, err
326 }
327
328 return &pcrc, sig, &ra, ret, nil
329 }
330
331 // makeIdentity requests that the TPM create a new AIK. It returns the handle to
332 // this new key.
333 func makeIdentity(rw io.ReadWriter, encAuth Digest, idDigest Digest, k *key, ca1 *commandAuth, ca2 *commandAuth) (*key, []byte, *responseAuth, *responseAuth, uint32, error) {
334 in := []interface{}{encAuth, idDigest, k, ca1, ca2}
335 var aik key
336 var sig tpmutil.U32Bytes
337 var ra1 responseAuth
338 var ra2 responseAuth
339 out := []interface{}{&aik, &sig, &ra1, &ra2}
340 ret, err := submitTPMRequest(rw, tagRQUAuth2Command, ordMakeIdentity, in, out)
341 if err != nil {
342 return nil, nil, nil, nil, 0, err
343 }
344
345 return &aik, sig, &ra1, &ra2, ret, nil
346 }
347
348 // activateIdentity provides the TPM with an EK encrypted challenge and asks it to
349 // decrypt the challenge and return the secret (symmetric key).
350 func activateIdentity(rw io.ReadWriter, keyHandle tpmutil.Handle, blob tpmutil.U32Bytes, ca1 *commandAuth, ca2 *commandAuth) (*symKey, *responseAuth, *responseAuth, uint32, error) {
351 in := []interface{}{keyHandle, blob, ca1, ca2}
352 var symkey symKey
353 var ra1 responseAuth
354 var ra2 responseAuth
355 out := []interface{}{&symkey, &ra1, &ra2}
356 ret, err := submitTPMRequest(rw, tagRQUAuth2Command, ordActivateIdentity, in, out)
357 if err != nil {
358 return nil, nil, nil, 0, err
359 }
360
361 return &symkey, &ra1, &ra2, ret, nil
362 }
363
364 // resetLockValue resets the dictionary-attack lock in the TPM, using owner
365 // auth.
366 func resetLockValue(rw io.ReadWriter, ca *commandAuth) (*responseAuth, uint32, error) {
367 in := []interface{}{ca}
368 var ra responseAuth
369 out := []interface{}{&ra}
370 ret, err := submitTPMRequest(rw, tagRQUAuth1Command, ordResetLockValue, in, out)
371 if err != nil {
372 return nil, 0, err
373 }
374
375 return &ra, ret, nil
376 }
377
378 // ownerReadInternalPub uses owner auth and OSAP to read either the endorsement
379 // key (using khEK) or the SRK (using khSRK).
380 func ownerReadInternalPub(rw io.ReadWriter, kh tpmutil.Handle, ca *commandAuth) (*pubKey, *responseAuth, uint32, error) {
381 in := []interface{}{kh, ca}
382 var pk pubKey
383 var ra responseAuth
384 out := []interface{}{&pk, &ra}
385 ret, err := submitTPMRequest(rw, tagRQUAuth1Command, ordOwnerReadInternalPub, in, out)
386 if err != nil {
387 return nil, nil, 0, err
388 }
389
390 return &pk, &ra, ret, nil
391 }
392
393 // readPubEK requests the public part of the endorsement key from the TPM. Note
394 // that this call can only be made when there is no owner in the TPM. Once an
395 // owner is established, the endorsement key can be retrieved using
396 // ownerReadInternalPub.
397 func readPubEK(rw io.ReadWriter, n Nonce) (*pubKey, Digest, uint32, error) {
398 in := []interface{}{n}
399 var pk pubKey
400 var d Digest
401 out := []interface{}{&pk, &d}
402 ret, err := submitTPMRequest(rw, tagRQUCommand, ordReadPubEK, in, out)
403 if err != nil {
404 return nil, d, 0, err
405 }
406
407 return &pk, d, ret, nil
408 }
409
410 // ownerClear uses owner auth to clear the TPM. After this operation, a caller
411 // can take ownership of the TPM with TPM_TakeOwnership.
412 func ownerClear(rw io.ReadWriter, ca *commandAuth) (*responseAuth, uint32, error) {
413 in := []interface{}{ca}
414 var ra responseAuth
415 out := []interface{}{&ra}
416 ret, err := submitTPMRequest(rw, tagRQUAuth1Command, ordOwnerClear, in, out)
417 if err != nil {
418 return nil, 0, err
419 }
420
421 return &ra, ret, nil
422 }
423
424 // takeOwnership takes ownership of the TPM and establishes a new SRK and
425 // owner auth. This operation can only be performed if there is no owner. The
426 // TPM can be put into this state using TPM_OwnerClear. The encOwnerAuth and
427 // encSRKAuth values must be encrypted using the endorsement key.
428 func takeOwnership(rw io.ReadWriter, encOwnerAuth tpmutil.U32Bytes, encSRKAuth tpmutil.U32Bytes, srk *key, ca *commandAuth) (*key, *responseAuth, uint32, error) {
429 in := []interface{}{pidOwner, encOwnerAuth, encSRKAuth, srk, ca}
430 var k key
431 var ra responseAuth
432 out := []interface{}{&k, &ra}
433 ret, err := submitTPMRequest(rw, tagRQUAuth1Command, ordTakeOwnership, in, out)
434 if err != nil {
435 return nil, nil, 0, err
436 }
437
438 return &k, &ra, ret, nil
439 }
440
441 // Creates a wrapped key under the SRK.
442 func createWrapKey(rw io.ReadWriter, encUsageAuth Digest, encMigrationAuth Digest, keyInfo *key, ca *commandAuth) (*key, *responseAuth, uint32, error) {
443 in := []interface{}{khSRK, encUsageAuth, encMigrationAuth, keyInfo, ca}
444 var k key
445 var ra responseAuth
446 out := []interface{}{&k, &ra}
447 ret, err := submitTPMRequest(rw, tagRQUAuth1Command, ordCreateWrapKey, in, out)
448 if err != nil {
449 return nil, nil, 0, err
450 }
451
452 return &k, &ra, ret, nil
453 }
454
455 func sign(rw io.ReadWriter, keyHandle tpmutil.Handle, data tpmutil.U32Bytes, ca *commandAuth) ([]byte, *responseAuth, uint32, error) {
456 in := []interface{}{keyHandle, data, ca}
457 var signature tpmutil.U32Bytes
458 var ra responseAuth
459 out := []interface{}{&signature, &ra}
460 ret, err := submitTPMRequest(rw, tagRQUAuth1Command, ordSign, in, out)
461 if err != nil {
462 return nil, nil, 0, err
463 }
464
465 return signature, &ra, ret, nil
466 }
467
468 func pcrReset(rw io.ReadWriter, pcrs *pcrSelection) error {
469 _, err := submitTPMRequest(rw, tagRQUCommand, ordPcrReset, []interface{}{pcrs}, nil)
470 if err != nil {
471 return err
472 }
473 return nil
474 }