| 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 mssim implements the Microsoft simulator TPM2 Transmission Interface |
| 16 |
// |
| 17 |
// The Microsoft simulator TPM Command Transmission Interface (TCTI) is a |
| 18 |
// remote procedure interface donated to the TPM2 Specification by Microsoft. |
| 19 |
// Its primary implementation is the tpm_server maintained by IBM. |
| 20 |
// |
| 21 |
// https://sourceforge.net/projects/ibmswtpm2/ |
| 22 |
// |
| 23 |
// This package implements client code to communicate with server code described |
| 24 |
// in the document "TPM2 Specification Part 4: Supporting Routines – Code" |
| 25 |
// |
| 26 |
// https://trustedcomputinggroup.org/wp-content/uploads/TPM-Rev-2.0-Part-4-Supporting-Routines-01.38-code.pdf |
| 27 |
package mssim |
| 28 |
|
| 29 |
import ( |
| 30 |
"bytes" |
| 31 |
"encoding/binary" |
| 32 |
"fmt" |
| 33 |
"io" |
| 34 |
"net" |
| 35 |
) |
| 36 |
|
| 37 |
// Constants defined in "D.3.2. Typedefs and Defines" |
| 38 |
const ( |
| 39 |
tpmSignalPowerOn uint32 = 1 |
| 40 |
tpmSignalPowerOff uint32 = 2 |
| 41 |
tpmSendCommand uint32 = 8 |
| 42 |
tpmSignalNVOn uint32 = 11 |
| 43 |
tpmSessionEnd uint32 = 20 |
| 44 |
) |
| 45 |
|
| 46 |
// Config holds configuration parameters for connecting to the simulator. |
| 47 |
type Config struct { |
| 48 |
// Addresses of the command and platform handlers. |
| 49 |
// |
| 50 |
// Defaults to port 2321 and 2322 on localhost. |
| 51 |
CommandAddress string |
| 52 |
PlatformAddress string |
| 53 |
} |
| 54 |
|
| 55 |
// Open creates connections to the simulator's command and platform ports and |
| 56 |
// power cycles the simulator to initialize it. |
| 57 |
func Open(config Config) (*Conn, error) { |
| 58 |
cmdAddr := config.CommandAddress |
| 59 |
if cmdAddr == "" { |
| 60 |
cmdAddr = "127.0.0.1:2321" |
| 61 |
} |
| 62 |
|
| 63 |
platformAddr := config.PlatformAddress |
| 64 |
if platformAddr == "" { |
| 65 |
platformAddr = "127.0.0.1:2322" |
| 66 |
} |
| 67 |
|
| 68 |
conn, err := net.Dial("tcp", platformAddr) |
| 69 |
if err != nil { |
| 70 |
return nil, fmt.Errorf("dial platform address: %v", err) |
| 71 |
} |
| 72 |
defer conn.Close() |
| 73 |
|
| 74 |
// Startup the simulator. This order of commands copies IBM's TPM2 tool's |
| 75 |
// "powerup" command line tool and will reset the simulator. |
| 76 |
// |
| 77 |
// https://sourceforge.net/projects/ibmtpm20tss/ |
| 78 |
|
| 79 |
if err := sendPlatformCommand(conn, tpmSignalPowerOff); err != nil { |
| 80 |
return nil, fmt.Errorf("power off platform command failed: %v", err) |
| 81 |
} |
| 82 |
if err := sendPlatformCommand(conn, tpmSignalPowerOn); err != nil { |
| 83 |
return nil, fmt.Errorf("power on platform command failed: %v", err) |
| 84 |
} |
| 85 |
if err := sendPlatformCommand(conn, tpmSignalNVOn); err != nil { |
| 86 |
return nil, fmt.Errorf("nv on platform command failed: %v", err) |
| 87 |
} |
| 88 |
|
| 89 |
// Gracefully close the connection. |
| 90 |
if err := binary.Write(conn, binary.BigEndian, tpmSessionEnd); err != nil { |
| 91 |
return nil, fmt.Errorf("shutdown platform connection failed: %v", err) |
| 92 |
} |
| 93 |
|
| 94 |
cmdConn, err := net.Dial("tcp", cmdAddr) |
| 95 |
if err != nil { |
| 96 |
return nil, fmt.Errorf("dial command address: %v", err) |
| 97 |
} |
| 98 |
|
| 99 |
return &Conn{conn: cmdConn}, nil |
| 100 |
} |
| 101 |
|
| 102 |
// sendPlatformCommand sends device management commands to the simulator. |
| 103 |
// |
| 104 |
// See: "D.4.3.2. PlatformServer()" |
| 105 |
func sendPlatformCommand(conn net.Conn, u uint32) error { |
| 106 |
if err := binary.Write(conn, binary.BigEndian, u); err != nil { |
| 107 |
return fmt.Errorf("write platform command: %v", err) |
| 108 |
} |
| 109 |
|
| 110 |
var rc uint32 |
| 111 |
if err := binary.Read(conn, binary.BigEndian, &rc); err != nil { |
| 112 |
return fmt.Errorf("read platform command: %v", err) |
| 113 |
} |
| 114 |
if rc != 0 { |
| 115 |
return fmt.Errorf("unexpected platform command response code: 0x%x", rc) |
| 116 |
} |
| 117 |
return nil |
| 118 |
} |
| 119 |
|
| 120 |
// Conn is a Microsoft Simulator client that can be used as a connection for the |
| 121 |
// tpm2 package. |
| 122 |
type Conn struct { |
| 123 |
// Cached connection |
| 124 |
conn net.Conn |
| 125 |
|
| 126 |
// Response bytes left over from the previous read. |
| 127 |
prevRead *bytes.Reader |
| 128 |
} |
| 129 |
|
| 130 |
// Read a response from the simulator. If the response is longer than the provided |
| 131 |
// buffer, the remainder will be cached for the next read. |
| 132 |
func (c *Conn) Read(b []byte) (int, error) { |
| 133 |
if c.prevRead != nil && c.prevRead.Len() > 0 { |
| 134 |
return c.prevRead.Read(b) |
| 135 |
} |
| 136 |
|
| 137 |
// Response frame: |
| 138 |
// - uint32 (size of response) |
| 139 |
// - []byte (response) |
| 140 |
// - uint32 (always 0) |
| 141 |
var respLen uint32 |
| 142 |
if err := binary.Read(c.conn, binary.BigEndian, &respLen); err != nil { |
| 143 |
return 0, fmt.Errorf("read MS simulator response header: %v", err) |
| 144 |
} |
| 145 |
|
| 146 |
resp := make([]byte, int(respLen)) |
| 147 |
if _, err := io.ReadFull(c.conn, resp[:]); err != nil { |
| 148 |
return 0, fmt.Errorf("read MS simulator response: %v", err) |
| 149 |
} |
| 150 |
|
| 151 |
var rc uint32 |
| 152 |
if err := binary.Read(c.conn, binary.BigEndian, &rc); err != nil { |
| 153 |
return 0, fmt.Errorf("read MS simulator return code: %v", err) |
| 154 |
} |
| 155 |
if rc != 0 { |
| 156 |
return 0, fmt.Errorf("MS simulator returned invalid return code: 0x%x", rc) |
| 157 |
} |
| 158 |
|
| 159 |
c.prevRead = bytes.NewReader(resp) |
| 160 |
return c.prevRead.Read(b) |
| 161 |
} |
| 162 |
|
| 163 |
// Write a raw command to the simulator. Commands must be written in a single call |
| 164 |
// to Write. Commands split over multiple calls will result in multiple framed |
| 165 |
// requests. |
| 166 |
func (c *Conn) Write(b []byte) (int, error) { |
| 167 |
// See: D.4.3.12. TpmServer() |
| 168 |
buff := &bytes.Buffer{} |
| 169 |
// "send command" flag |
| 170 |
binary.Write(buff, binary.BigEndian, tpmSendCommand) |
| 171 |
// locality 0 |
| 172 |
buff.WriteByte(0) |
| 173 |
// size of the command |
| 174 |
binary.Write(buff, binary.BigEndian, uint32(len(b))) |
| 175 |
// raw command |
| 176 |
buff.Write(b) |
| 177 |
|
| 178 |
if _, err := buff.WriteTo(c.conn); err != nil { |
| 179 |
return 0, fmt.Errorf("write MS simulator command: %v", err) |
| 180 |
} |
| 181 |
return len(b), nil |
| 182 |
} |
| 183 |
|
| 184 |
// Close closes any outgoing connections to the TPM simulator. |
| 185 |
func (c *Conn) Close() error { |
| 186 |
// See: D.4.3.12. TpmServer() |
| 187 |
// Gracefully close the connection. |
| 188 |
if err := binary.Write(c.conn, binary.BigEndian, tpmSessionEnd); err != nil { |
| 189 |
c.conn.Close() |
| 190 |
return fmt.Errorf("shutdown platform connection failed: %v", err) |
| 191 |
} |
| 192 |
return c.conn.Close() |
| 193 |
} |