| 1 |
// Package tcp provides access to a TPM over TCP. |
| 2 |
package tcp |
| 3 |
|
| 4 |
import ( |
| 5 |
"encoding/binary" |
| 6 |
"errors" |
| 7 |
"fmt" |
| 8 |
"net" |
| 9 |
) |
| 10 |
|
| 11 |
var ( |
| 12 |
ErrPlatformFailed = errors.New("platform command failed") |
| 13 |
ErrTPMFailed = errors.New("TPM command failed") |
| 14 |
ErrResponseTooBig = errors.New("response too big") |
| 15 |
ErrTransport = errors.New("TCP transport error") |
| 16 |
ErrEmptyResponse = errors.New("TPM returned empty response (does it need to be powered on?)") |
| 17 |
) |
| 18 |
|
| 19 |
const ( |
| 20 |
maxBufferSize = 1048576 |
| 21 |
) |
| 22 |
|
| 23 |
// The de-facto TPM-over-TCP protocol is defined by the Reference Implementation. |
| 24 |
// See https://github.com/TrustedComputingGroup/TPM/blob/main/TPMCmd/Simulator/include/TpmTcpProtocol.h |
| 25 |
|
| 26 |
type regularCommand uint32 |
| 27 |
|
| 28 |
const ( |
| 29 |
tpmHashStart regularCommand = 5 |
| 30 |
tpmHashData regularCommand = 6 |
| 31 |
tpmHashEnd regularCommand = 7 |
| 32 |
tpmSendCommand regularCommand = 8 |
| 33 |
tpmRemoteHandshake regularCommand = 15 |
| 34 |
tpmSetAlternativeResult regularCommand = 16 |
| 35 |
tpmSessionEnd regularCommand = 20 |
| 36 |
tpmStop regularCommand = 21 |
| 37 |
) |
| 38 |
|
| 39 |
func (c regularCommand) String() string { |
| 40 |
switch c { |
| 41 |
case tpmHashStart: |
| 42 |
return "HASH_START" |
| 43 |
case tpmHashData: |
| 44 |
return "HASH_DATA" |
| 45 |
case tpmHashEnd: |
| 46 |
return "HASH_END" |
| 47 |
case tpmSendCommand: |
| 48 |
return "SEND_COMMAND" |
| 49 |
case tpmRemoteHandshake: |
| 50 |
return "REMOTE_HANDSHAKE" |
| 51 |
case tpmSetAlternativeResult: |
| 52 |
return "SET_ALTERNATIVE_RESULT" |
| 53 |
case tpmSessionEnd: |
| 54 |
return "SESSION_END" |
| 55 |
case tpmStop: |
| 56 |
return "STOP" |
| 57 |
default: |
| 58 |
return fmt.Sprintf("unknown TPM command (%v)", uint32(c)) |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
type platformCommand uint32 |
| 63 |
|
| 64 |
const ( |
| 65 |
platformPowerOn platformCommand = 1 |
| 66 |
platformPowerOff platformCommand = 2 |
| 67 |
platformPPOn platformCommand = 3 |
| 68 |
platformPPOff platformCommand = 4 |
| 69 |
platformCancelOn platformCommand = 9 |
| 70 |
platformCancelOff platformCommand = 10 |
| 71 |
platformNVOn platformCommand = 11 |
| 72 |
platformNVOff platformCommand = 12 |
| 73 |
platformKeyCacheOn platformCommand = 13 |
| 74 |
platformKeyCacheOff platformCommand = 14 |
| 75 |
platformReset platformCommand = 17 |
| 76 |
platformRestart platformCommand = 18 |
| 77 |
platformSessionEnd platformCommand = 20 |
| 78 |
platformStop platformCommand = 21 |
| 79 |
platformGetCommandResponseSizes platformCommand = 25 |
| 80 |
platformACTGetSignaled platformCommand = 26 |
| 81 |
platformTestFailureMode platformCommand = 30 |
| 82 |
platformSetFWHash platformCommand = 35 |
| 83 |
platformSetFWSVN platformCommand = 36 |
| 84 |
) |
| 85 |
|
| 86 |
func (c platformCommand) String() string { |
| 87 |
switch c { |
| 88 |
case platformPowerOn: |
| 89 |
return "POWER_ON" |
| 90 |
case platformPowerOff: |
| 91 |
return "POWER_OFF" |
| 92 |
case platformPPOn: |
| 93 |
return "PP_ON" |
| 94 |
case platformPPOff: |
| 95 |
return "PP_OFF" |
| 96 |
case platformCancelOn: |
| 97 |
return "CANCEL_ON" |
| 98 |
case platformCancelOff: |
| 99 |
return "CANCEL_OFF" |
| 100 |
case platformNVOn: |
| 101 |
return "NV_ON" |
| 102 |
case platformNVOff: |
| 103 |
return "NV_OFF" |
| 104 |
case platformKeyCacheOn: |
| 105 |
return "KEY_CACHE_ON" |
| 106 |
case platformKeyCacheOff: |
| 107 |
return "KEY_CACHE_OFF" |
| 108 |
case platformReset: |
| 109 |
return "RESET" |
| 110 |
case platformRestart: |
| 111 |
return "RESTART" |
| 112 |
case platformSessionEnd: |
| 113 |
return "SESSION_END" |
| 114 |
case platformStop: |
| 115 |
return "STOP" |
| 116 |
case platformGetCommandResponseSizes: |
| 117 |
return "GET_COMMAND_RESPONSE_SIZES" |
| 118 |
case platformACTGetSignaled: |
| 119 |
return "ACT_GET_SIGNALED" |
| 120 |
case platformTestFailureMode: |
| 121 |
return "TEST_FAILURE_MODE" |
| 122 |
case platformSetFWHash: |
| 123 |
return "SET_FW_HASH" |
| 124 |
case platformSetFWSVN: |
| 125 |
return "SET_FW_SVN" |
| 126 |
default: |
| 127 |
return fmt.Sprintf("unknown platform command (%v)", uint32(c)) |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
type TPM struct { |
| 132 |
cmd *net.TCPConn |
| 133 |
plat *net.TCPConn |
| 134 |
} |
| 135 |
|
| 136 |
type tpmCommandHeader struct { |
| 137 |
tcpCommand regularCommand |
| 138 |
locality uint8 |
| 139 |
cmdLen uint32 |
| 140 |
} |
| 141 |
|
| 142 |
// Send implements the TPMCloser interface. |
| 143 |
func (t *TPM) Send(cmd []byte) ([]byte, error) { |
| 144 |
hdr := tpmCommandHeader{ |
| 145 |
tcpCommand: tpmSendCommand, |
| 146 |
locality: 0, |
| 147 |
cmdLen: uint32(len(cmd)), |
| 148 |
} |
| 149 |
// Write the header followed by the request |
| 150 |
if err := binary.Write(t.cmd, binary.BigEndian, hdr); err != nil { |
| 151 |
return nil, fmt.Errorf("%w: could not send TPM command to service: %v", ErrTransport, err) |
| 152 |
} |
| 153 |
if n, err := t.cmd.Write(cmd); err != nil { |
| 154 |
return nil, fmt.Errorf("%w: could not send TPM command to service: %v", ErrTransport, err) |
| 155 |
} else if n != len(cmd) { |
| 156 |
return nil, fmt.Errorf("%w: could not send full TPM command: only sent %v out of %v bytes", ErrTransport, n, len(cmd)) |
| 157 |
} |
| 158 |
|
| 159 |
// Read the response |
| 160 |
var rspLen uint32 |
| 161 |
if err := binary.Read(t.cmd, binary.BigEndian, &rspLen); err != nil { |
| 162 |
return nil, fmt.Errorf("%w: could not read TPM response from service: %v", ErrTransport, err) |
| 163 |
} |
| 164 |
if rspLen > maxBufferSize { |
| 165 |
return nil, fmt.Errorf("%w: response (%v bytes) was bigger than max size (%v bytes)", ErrResponseTooBig, rspLen, maxBufferSize) |
| 166 |
} |
| 167 |
rsp := make([]byte, int(rspLen)) |
| 168 |
if n, err := t.cmd.Read(rsp); err != nil { |
| 169 |
return nil, fmt.Errorf("%w: could not read TPM response from service: %v", ErrTransport, err) |
| 170 |
} else if n != len(rsp) { |
| 171 |
return nil, fmt.Errorf("%w: could not read full TPM response: only got %v out of %v bytes", ErrTransport, n, len(rsp)) |
| 172 |
} |
| 173 |
// The server also provides a TCP error code at the end. |
| 174 |
var rspCode uint32 |
| 175 |
if err := binary.Read(t.cmd, binary.BigEndian, &rspCode); err != nil { |
| 176 |
return nil, fmt.Errorf("%w: %v returned %v", ErrTPMFailed, hdr.tcpCommand, rspCode) |
| 177 |
} |
| 178 |
if rspLen == 0 { |
| 179 |
return nil, ErrEmptyResponse |
| 180 |
} |
| 181 |
return rsp, nil |
| 182 |
} |
| 183 |
|
| 184 |
// Close implements the TPMCloser interface. |
| 185 |
func (t *TPM) Close() error { |
| 186 |
return errors.Join(t.cmd.Close(), t.plat.Close()) |
| 187 |
} |
| 188 |
|
| 189 |
// PowerOn powers on the TPM. |
| 190 |
// Note: This is distinct from sending the TPM2_Startup command. |
| 191 |
func (t *TPM) PowerOn() error { |
| 192 |
return errors.Join(t.sendBasicPlatformCommand(platformPowerOn), |
| 193 |
t.sendBasicPlatformCommand(platformNVOn)) |
| 194 |
} |
| 195 |
|
| 196 |
// PowerOff powers off the TPM. |
| 197 |
func (t *TPM) PowerOff() error { |
| 198 |
return errors.Join(t.sendBasicPlatformCommand(platformPowerOff), |
| 199 |
t.sendBasicPlatformCommand(platformNVOff)) |
| 200 |
} |
| 201 |
|
| 202 |
// Reset power-cycles the TPM if it is already on. If it is not already on, |
| 203 |
// nothing happens. |
| 204 |
func (t *TPM) Reset() error { |
| 205 |
return t.sendBasicPlatformCommand(platformReset) |
| 206 |
} |
| 207 |
|
| 208 |
// Config provides the connection information for a running TCP TPM. |
| 209 |
type Config struct { |
| 210 |
// CommandAddress is the full host:port address of the Command server, e.g., |
| 211 |
// "localhost:2321" |
| 212 |
CommandAddress string |
| 213 |
// CommandAddress is the full host:port address of the Platform server, e.g., |
| 214 |
// "localhost:2322" |
| 215 |
PlatformAddress string |
| 216 |
} |
| 217 |
|
| 218 |
func resolveAndConnect(addr string) (*net.TCPConn, error) { |
| 219 |
tcpAddr, err := net.ResolveTCPAddr("tcp", addr) |
| 220 |
if err != nil { |
| 221 |
return nil, fmt.Errorf("could not resolve %q: %w", addr, err) |
| 222 |
} |
| 223 |
|
| 224 |
conn, err := net.DialTCP("tcp", nil, tcpAddr) |
| 225 |
if err != nil { |
| 226 |
return nil, fmt.Errorf("could not dial %q: %w", addr, err) |
| 227 |
} |
| 228 |
return conn, nil |
| 229 |
} |
| 230 |
|
| 231 |
// Open opens a connection to the TPM. It may still need to be powered on using PowerOn(). |
| 232 |
func Open(config Config) (*TPM, error) { |
| 233 |
cmd, err := resolveAndConnect(config.CommandAddress) |
| 234 |
if err != nil { |
| 235 |
return nil, fmt.Errorf("could not connect to command service at %q: %w", config.CommandAddress, err) |
| 236 |
} |
| 237 |
plat, err := resolveAndConnect(config.PlatformAddress) |
| 238 |
if err != nil { |
| 239 |
return nil, fmt.Errorf("could not connect to platform service at %q: %w", config.PlatformAddress, err) |
| 240 |
} |
| 241 |
|
| 242 |
return &TPM{ |
| 243 |
cmd: cmd, |
| 244 |
plat: plat, |
| 245 |
}, nil |
| 246 |
} |
| 247 |
|
| 248 |
// sendBasicPlatformCommand sends a command to the platform service. This only |
| 249 |
// supports 'basic' commands (i.e., send just a command code, receive just a |
| 250 |
// response code). |
| 251 |
func (t *TPM) sendBasicPlatformCommand(cmd platformCommand) error { |
| 252 |
if err := binary.Write(t.plat, binary.BigEndian, cmd); err != nil { |
| 253 |
return fmt.Errorf("could not write %v to platform service: %w", cmd, err) |
| 254 |
} |
| 255 |
var result uint32 |
| 256 |
if err := binary.Read(t.plat, binary.BigEndian, &result); err != nil { |
| 257 |
return fmt.Errorf("could not read %v result from platform service: %w", cmd, err) |
| 258 |
} |
| 259 |
if result != 0 { |
| 260 |
return fmt.Errorf("%w: %v returned %v", ErrPlatformFailed, cmd, result) |
| 261 |
} |
| 262 |
return nil |
| 263 |
} |