| 1 |
// Copyright (c) 2018, 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 tpmutil provides common utility functions for both TPM 1.2 and TPM |
| 16 |
// 2.0 devices. |
| 17 |
package tpmutil |
| 18 |
|
| 19 |
import ( |
| 20 |
"errors" |
| 21 |
"io" |
| 22 |
"os" |
| 23 |
"time" |
| 24 |
) |
| 25 |
|
| 26 |
// maxTPMResponse is the largest possible response from the TPM. We need to know |
| 27 |
// this because we don't always know the length of the TPM response, and |
| 28 |
// /dev/tpm insists on giving it all back in a single value rather than |
| 29 |
// returning a header and a body in separate responses. |
| 30 |
const maxTPMResponse = 4096 |
| 31 |
|
| 32 |
// RunCommandRaw executes the given raw command and returns the raw response. |
| 33 |
// Does not check the response code except to execute retry logic. |
| 34 |
func RunCommandRaw(rw io.ReadWriter, inb []byte) ([]byte, error) { |
| 35 |
if rw == nil { |
| 36 |
return nil, errors.New("nil TPM handle") |
| 37 |
} |
| 38 |
|
| 39 |
// f(t) = (2^t)ms, up to 2s |
| 40 |
var backoffFac uint |
| 41 |
var rh responseHeader |
| 42 |
var outb []byte |
| 43 |
|
| 44 |
for { |
| 45 |
if _, err := rw.Write(inb); err != nil { |
| 46 |
return nil, err |
| 47 |
} |
| 48 |
|
| 49 |
// If the TPM is a real device, it may not be ready for reading |
| 50 |
// immediately after writing the command. Wait until the file |
| 51 |
// descriptor is ready to be read from. |
| 52 |
if f, ok := rw.(*os.File); ok { |
| 53 |
if err := poll(f); err != nil { |
| 54 |
return nil, err |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
outb = make([]byte, maxTPMResponse) |
| 59 |
outlen, err := rw.Read(outb) |
| 60 |
if err != nil { |
| 61 |
return nil, err |
| 62 |
} |
| 63 |
// Resize the buffer to match the amount read from the TPM. |
| 64 |
outb = outb[:outlen] |
| 65 |
|
| 66 |
_, err = Unpack(outb, &rh) |
| 67 |
if err != nil { |
| 68 |
return nil, err |
| 69 |
} |
| 70 |
|
| 71 |
// If TPM is busy, retry the command after waiting a few ms. |
| 72 |
if rh.Res == RCRetry { |
| 73 |
if backoffFac < 11 { |
| 74 |
dur := (1 << backoffFac) * time.Millisecond |
| 75 |
time.Sleep(dur) |
| 76 |
backoffFac++ |
| 77 |
} else { |
| 78 |
return nil, err |
| 79 |
} |
| 80 |
} else { |
| 81 |
break |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
return outb, nil |
| 86 |
} |
| 87 |
|
| 88 |
// RunCommand executes cmd with given tag and arguments. Returns TPM response |
| 89 |
// body (without response header) and response code from the header. Returned |
| 90 |
// error may be nil if response code is not RCSuccess; caller should check |
| 91 |
// both. |
| 92 |
func RunCommand(rw io.ReadWriter, tag Tag, cmd Command, in ...interface{}) ([]byte, ResponseCode, error) { |
| 93 |
inb, err := packWithHeader(commandHeader{tag, 0, cmd}, in...) |
| 94 |
if err != nil { |
| 95 |
return nil, 0, err |
| 96 |
} |
| 97 |
|
| 98 |
outb, err := RunCommandRaw(rw, inb) |
| 99 |
if err != nil { |
| 100 |
return nil, 0, err |
| 101 |
} |
| 102 |
|
| 103 |
var rh responseHeader |
| 104 |
read, err := Unpack(outb, &rh) |
| 105 |
if err != nil { |
| 106 |
return nil, 0, err |
| 107 |
} |
| 108 |
if rh.Res != RCSuccess { |
| 109 |
return nil, rh.Res, nil |
| 110 |
} |
| 111 |
|
| 112 |
return outb[read:], rh.Res, nil |
| 113 |
} |