| 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 |
| 16 |
|
| 17 |
import ( |
| 18 |
"bytes" |
| 19 |
"encoding/binary" |
| 20 |
"fmt" |
| 21 |
"io" |
| 22 |
) |
| 23 |
|
| 24 |
// maxBytesBufferSize sets a sane upper bound on the size of a U32Bytes |
| 25 |
// buffer. This limit exists to prevent a maliciously large size prefix |
| 26 |
// from resulting in a massive memory allocation, potentially causing |
| 27 |
// an OOM condition on the system. |
| 28 |
// We expect no buffer from a TPM to approach 1Mb in size. |
| 29 |
const maxBytesBufferSize uint32 = 1024 * 1024 // 1Mb. |
| 30 |
|
| 31 |
// RawBytes is for Pack and RunCommand arguments that are already encoded. |
| 32 |
// Compared to []byte, RawBytes will not be prepended with slice length during |
| 33 |
// encoding. |
| 34 |
type RawBytes []byte |
| 35 |
|
| 36 |
// U16Bytes is a byte slice with a 16-bit header |
| 37 |
type U16Bytes []byte |
| 38 |
|
| 39 |
// TPMMarshal packs U16Bytes |
| 40 |
func (b *U16Bytes) TPMMarshal(out io.Writer) error { |
| 41 |
size := len([]byte(*b)) |
| 42 |
if err := binary.Write(out, binary.BigEndian, uint16(size)); err != nil { |
| 43 |
return err |
| 44 |
} |
| 45 |
|
| 46 |
n, err := out.Write(*b) |
| 47 |
if err != nil { |
| 48 |
return err |
| 49 |
} |
| 50 |
if n != size { |
| 51 |
return fmt.Errorf("unable to write all contents of U16Bytes") |
| 52 |
} |
| 53 |
return nil |
| 54 |
} |
| 55 |
|
| 56 |
// TPMUnmarshal unpacks a U16Bytes |
| 57 |
func (b *U16Bytes) TPMUnmarshal(in io.Reader) error { |
| 58 |
var tmpSize uint16 |
| 59 |
if err := binary.Read(in, binary.BigEndian, &tmpSize); err != nil { |
| 60 |
return err |
| 61 |
} |
| 62 |
size := int(tmpSize) |
| 63 |
|
| 64 |
if len(*b) >= size { |
| 65 |
*b = (*b)[:size] |
| 66 |
} else { |
| 67 |
*b = append(*b, make([]byte, size-len(*b))...) |
| 68 |
} |
| 69 |
|
| 70 |
n, err := in.Read(*b) |
| 71 |
if err != nil { |
| 72 |
return err |
| 73 |
} |
| 74 |
if n != size { |
| 75 |
return io.ErrUnexpectedEOF |
| 76 |
} |
| 77 |
return nil |
| 78 |
} |
| 79 |
|
| 80 |
// U32Bytes is a byte slice with a 32-bit header |
| 81 |
type U32Bytes []byte |
| 82 |
|
| 83 |
// TPMMarshal packs U32Bytes |
| 84 |
func (b *U32Bytes) TPMMarshal(out io.Writer) error { |
| 85 |
size := len([]byte(*b)) |
| 86 |
if err := binary.Write(out, binary.BigEndian, uint32(size)); err != nil { |
| 87 |
return err |
| 88 |
} |
| 89 |
|
| 90 |
n, err := out.Write(*b) |
| 91 |
if err != nil { |
| 92 |
return err |
| 93 |
} |
| 94 |
if n != size { |
| 95 |
return fmt.Errorf("unable to write all contents of U32Bytes") |
| 96 |
} |
| 97 |
return nil |
| 98 |
} |
| 99 |
|
| 100 |
// TPMUnmarshal unpacks a U32Bytes |
| 101 |
func (b *U32Bytes) TPMUnmarshal(in io.Reader) error { |
| 102 |
var tmpSize uint32 |
| 103 |
if err := binary.Read(in, binary.BigEndian, &tmpSize); err != nil { |
| 104 |
return err |
| 105 |
} |
| 106 |
|
| 107 |
if tmpSize > maxBytesBufferSize { |
| 108 |
return bytes.ErrTooLarge |
| 109 |
} |
| 110 |
// We can now safely cast to an int on 32-bit or 64-bit machines |
| 111 |
size := int(tmpSize) |
| 112 |
|
| 113 |
if len(*b) >= size { |
| 114 |
*b = (*b)[:size] |
| 115 |
} else { |
| 116 |
*b = append(*b, make([]byte, size-len(*b))...) |
| 117 |
} |
| 118 |
|
| 119 |
n, err := in.Read(*b) |
| 120 |
if err != nil { |
| 121 |
return err |
| 122 |
} |
| 123 |
if n != size { |
| 124 |
return fmt.Errorf("unable to read all contents in to U32Bytes") |
| 125 |
} |
| 126 |
return nil |
| 127 |
} |
| 128 |
|
| 129 |
// Tag is a command tag. |
| 130 |
type Tag uint16 |
| 131 |
|
| 132 |
// Command is an identifier of a TPM command. |
| 133 |
type Command uint32 |
| 134 |
|
| 135 |
// A commandHeader is the header for a TPM command. |
| 136 |
type commandHeader struct { |
| 137 |
Tag Tag |
| 138 |
Size uint32 |
| 139 |
Cmd Command |
| 140 |
} |
| 141 |
|
| 142 |
// ResponseCode is a response code returned by TPM. |
| 143 |
type ResponseCode uint32 |
| 144 |
|
| 145 |
// RCSuccess is response code for successful command. Identical for TPM 1.2 and |
| 146 |
// 2.0. |
| 147 |
const RCSuccess ResponseCode = 0x000 |
| 148 |
|
| 149 |
// RCRetry is response code for TPM is busy. |
| 150 |
const RCRetry ResponseCode = 0x922 |
| 151 |
|
| 152 |
// A responseHeader is a header for TPM responses. |
| 153 |
type responseHeader struct { |
| 154 |
Tag Tag |
| 155 |
Size uint32 |
| 156 |
Res ResponseCode |
| 157 |
} |
| 158 |
|
| 159 |
// A Handle is a reference to a TPM object. |
| 160 |
type Handle uint32 |
| 161 |
|
| 162 |
// HandleValue returns the handle value. This behavior is intended to satisfy |
| 163 |
// an interface that can be implemented by other, more complex types as well. |
| 164 |
func (h Handle) HandleValue() uint32 { |
| 165 |
return uint32(h) |
| 166 |
} |
| 167 |
|
| 168 |
type handleList []Handle |
| 169 |
|
| 170 |
func (l *handleList) TPMMarshal(_ io.Writer) error { |
| 171 |
return fmt.Errorf("TPMMarhsal on []Handle is not supported yet") |
| 172 |
} |
| 173 |
|
| 174 |
func (l *handleList) TPMUnmarshal(in io.Reader) error { |
| 175 |
var numHandles uint16 |
| 176 |
if err := binary.Read(in, binary.BigEndian, &numHandles); err != nil { |
| 177 |
return err |
| 178 |
} |
| 179 |
|
| 180 |
// Make len(e) match size exactly. |
| 181 |
size := int(numHandles) |
| 182 |
if len(*l) >= size { |
| 183 |
*l = (*l)[:size] |
| 184 |
} else { |
| 185 |
*l = append(*l, make([]Handle, size-len(*l))...) |
| 186 |
} |
| 187 |
return binary.Read(in, binary.BigEndian, *l) |
| 188 |
} |
| 189 |
|
| 190 |
// SelfMarshaler allows custom types to override default encoding/decoding |
| 191 |
// behavior in Pack, Unpack and UnpackBuf. |
| 192 |
type SelfMarshaler interface { |
| 193 |
TPMMarshal(out io.Writer) error |
| 194 |
TPMUnmarshal(in io.Reader) error |
| 195 |
} |