Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Microsoft TPM Simulator TCTI (WIP) #387

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
204 changes: 204 additions & 0 deletions tpm/internal/mssim/gotpm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
// Copyright (c) 2018, Google LLC All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Package mssim implements the Microsoft simulator TPM2 Transmission Interface
//
// The Microsoft simulator TPM Command Transmission Interface (TCTI) is a
// remote procedure interface donated to the TPM2 Specification by Microsoft.
// Its primary implementation is the tpm_server maintained by IBM.
//
// https://sourceforge.net/projects/ibmswtpm2/
//
// This package implements client code to communicate with server code described
// in the document "TPM2 Specification Part 4: Supporting Routines – Code"
//
// https://trustedcomputinggroup.org/wp-content/uploads/TPM-Rev-2.0-Part-4-Supporting-Routines-01.38-code.pdf
//
// This file was copied from https://github.com/google/go-tpm/tree/main/tpmutil/mssim.
// Some small changes were made so that platform commands can be skipped based on
// configuration.

//nolint:errorlint,gocritic // copied package
package mssim

import (
"bytes"
"encoding/binary"
"fmt"
"io"
"net"
)

// Constants defined in "D.3.2. Typedefs and Defines"
const (
tpmSignalPowerOn uint32 = 1
tpmSignalPowerOff uint32 = 2
tpmSendCommand uint32 = 8
tpmSignalNVOn uint32 = 11
tpmSessionEnd uint32 = 20
)

// Config holds configuration parameters for connecting to the simulator.
type config struct {
// Addresses of the command and platform handlers.
//
// Defaults to port 2321 and 2322 on localhost.
commandAddress string
platformAddress string

// Skip platform powerup/startup commands
skipPlatformCommands bool
}

// Open creates connections to the simulator's command and platform ports and
// power cycles the simulator to initialize it.
func open(config config) (*Conn, error) {
cmdAddr := config.commandAddress
if cmdAddr == "" {
cmdAddr = "127.0.0.1:2321"
}

Check warning on line 70 in tpm/internal/mssim/gotpm.go

View check run for this annotation

Codecov / codecov/patch

tpm/internal/mssim/gotpm.go#L66-L70

Added lines #L66 - L70 were not covered by tests

if !config.skipPlatformCommands {
platformAddr := config.platformAddress
if platformAddr == "" {
platformAddr = "127.0.0.1:2322"
}

Check warning on line 76 in tpm/internal/mssim/gotpm.go

View check run for this annotation

Codecov / codecov/patch

tpm/internal/mssim/gotpm.go#L72-L76

Added lines #L72 - L76 were not covered by tests

conn, err := net.Dial("tcp", platformAddr)
if err != nil {
return nil, fmt.Errorf("dial platform address: %v", err)
}
defer conn.Close()

// Startup the simulator. This order of commands copies IBM's TPM2 tool's
// "powerup" command line tool and will reset the simulator.
//
// https://sourceforge.net/projects/ibmtpm20tss/

if err := sendPlatformCommand(conn, tpmSignalPowerOff); err != nil {
return nil, fmt.Errorf("power off platform command failed: %v", err)
}
if err := sendPlatformCommand(conn, tpmSignalPowerOn); err != nil {
return nil, fmt.Errorf("power on platform command failed: %v", err)
}
if err := sendPlatformCommand(conn, tpmSignalNVOn); err != nil {
return nil, fmt.Errorf("nv on platform command failed: %v", err)
}

Check warning on line 97 in tpm/internal/mssim/gotpm.go

View check run for this annotation

Codecov / codecov/patch

tpm/internal/mssim/gotpm.go#L78-L97

Added lines #L78 - L97 were not covered by tests

// Gracefully close the connection.
if err := binary.Write(conn, binary.BigEndian, tpmSessionEnd); err != nil {
return nil, fmt.Errorf("shutdown platform connection failed: %v", err)
}

Check warning on line 102 in tpm/internal/mssim/gotpm.go

View check run for this annotation

Codecov / codecov/patch

tpm/internal/mssim/gotpm.go#L100-L102

Added lines #L100 - L102 were not covered by tests
}

cmdConn, err := net.Dial("tcp", cmdAddr)
if err != nil {
return nil, fmt.Errorf("dial command address: %v", err)
}

Check warning on line 108 in tpm/internal/mssim/gotpm.go

View check run for this annotation

Codecov / codecov/patch

tpm/internal/mssim/gotpm.go#L105-L108

Added lines #L105 - L108 were not covered by tests

return &Conn{conn: cmdConn}, nil

Check warning on line 110 in tpm/internal/mssim/gotpm.go

View check run for this annotation

Codecov / codecov/patch

tpm/internal/mssim/gotpm.go#L110

Added line #L110 was not covered by tests
}

// sendPlatformCommand sends device management commands to the simulator.
//
// See: "D.4.3.2. PlatformServer()"
func sendPlatformCommand(conn net.Conn, u uint32) error {
if err := binary.Write(conn, binary.BigEndian, u); err != nil {
return fmt.Errorf("write platform command: %v", err)
}

Check warning on line 119 in tpm/internal/mssim/gotpm.go

View check run for this annotation

Codecov / codecov/patch

tpm/internal/mssim/gotpm.go#L116-L119

Added lines #L116 - L119 were not covered by tests

var rc uint32
if err := binary.Read(conn, binary.BigEndian, &rc); err != nil {
return fmt.Errorf("read platform command: %v", err)
}
if rc != 0 {
return fmt.Errorf("unexpected platform command response code: 0x%x", rc)
}
return nil

Check warning on line 128 in tpm/internal/mssim/gotpm.go

View check run for this annotation

Codecov / codecov/patch

tpm/internal/mssim/gotpm.go#L121-L128

Added lines #L121 - L128 were not covered by tests
}

// Conn is a Microsoft Simulator client that can be used as a connection for the
// tpm2 package.
type Conn struct {
// Cached connection
conn net.Conn

// Response bytes left over from the previous read.
prevRead *bytes.Reader
}

// Read a response from the simulator. If the response is longer than the provided
// buffer, the remainder will be cached for the next read.
func (c *Conn) Read(b []byte) (int, error) {
if c.prevRead != nil && c.prevRead.Len() > 0 {
return c.prevRead.Read(b)
}

Check warning on line 146 in tpm/internal/mssim/gotpm.go

View check run for this annotation

Codecov / codecov/patch

tpm/internal/mssim/gotpm.go#L143-L146

Added lines #L143 - L146 were not covered by tests

// Response frame:
// - uint32 (size of response)
// - []byte (response)
// - uint32 (always 0)
var respLen uint32
if err := binary.Read(c.conn, binary.BigEndian, &respLen); err != nil {
return 0, fmt.Errorf("read MS simulator response header: %v", err)
}

Check warning on line 155 in tpm/internal/mssim/gotpm.go

View check run for this annotation

Codecov / codecov/patch

tpm/internal/mssim/gotpm.go#L152-L155

Added lines #L152 - L155 were not covered by tests

resp := make([]byte, int(respLen))
if _, err := io.ReadFull(c.conn, resp[:]); err != nil {
return 0, fmt.Errorf("read MS simulator response: %v", err)
}

Check warning on line 160 in tpm/internal/mssim/gotpm.go

View check run for this annotation

Codecov / codecov/patch

tpm/internal/mssim/gotpm.go#L157-L160

Added lines #L157 - L160 were not covered by tests

var rc uint32
if err := binary.Read(c.conn, binary.BigEndian, &rc); err != nil {
return 0, fmt.Errorf("read MS simulator return code: %v", err)
}
if rc != 0 {
return 0, fmt.Errorf("MS simulator returned invalid return code: 0x%x", rc)
}

Check warning on line 168 in tpm/internal/mssim/gotpm.go

View check run for this annotation

Codecov / codecov/patch

tpm/internal/mssim/gotpm.go#L162-L168

Added lines #L162 - L168 were not covered by tests

c.prevRead = bytes.NewReader(resp)
return c.prevRead.Read(b)

Check warning on line 171 in tpm/internal/mssim/gotpm.go

View check run for this annotation

Codecov / codecov/patch

tpm/internal/mssim/gotpm.go#L170-L171

Added lines #L170 - L171 were not covered by tests
}

// Write a raw command to the simulator. Commands must be written in a single call
// to Write. Commands split over multiple calls will result in multiple framed
// requests.
func (c *Conn) Write(b []byte) (int, error) {
// See: D.4.3.12. TpmServer()
buff := &bytes.Buffer{}
// "send command" flag
binary.Write(buff, binary.BigEndian, tpmSendCommand)
// locality 0
buff.WriteByte(0)
// size of the command
binary.Write(buff, binary.BigEndian, uint32(len(b)))
// raw command
buff.Write(b)

if _, err := buff.WriteTo(c.conn); err != nil {
return 0, fmt.Errorf("write MS simulator command: %v", err)
}
return len(b), nil

Check warning on line 192 in tpm/internal/mssim/gotpm.go

View check run for this annotation

Codecov / codecov/patch

tpm/internal/mssim/gotpm.go#L177-L192

Added lines #L177 - L192 were not covered by tests
}

// Close closes any outgoing connections to the TPM simulator.
func (c *Conn) Close() error {
// See: D.4.3.12. TpmServer()
// Gracefully close the connection.
if err := binary.Write(c.conn, binary.BigEndian, tpmSessionEnd); err != nil {
c.conn.Close()
return fmt.Errorf("shutdown platform connection failed: %v", err)
}
return c.conn.Close()

Check warning on line 203 in tpm/internal/mssim/gotpm.go

View check run for this annotation

Codecov / codecov/patch

tpm/internal/mssim/gotpm.go#L196-L203

Added lines #L196 - L203 were not covered by tests
}
50 changes: 50 additions & 0 deletions tpm/internal/mssim/mssim.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package mssim

import (
"fmt"
"io"
"net"
"strconv"

"go.step.sm/crypto/kms/uri"
)

func New(u *uri.URI) (rwc io.ReadWriteCloser, err error) {
host := "127.0.0.1"
port := 2321
if h := u.Get("host"); h != "" {
host = h
}
if p := u.Get("port"); p != "" {
port, err = strconv.Atoi(p)
if err != nil {
return nil, fmt.Errorf("failed parsing %q as integer: %w", p, err)
}

Check warning on line 22 in tpm/internal/mssim/mssim.go

View check run for this annotation

Codecov / codecov/patch

tpm/internal/mssim/mssim.go#L12-L22

Added lines #L12 - L22 were not covered by tests
}

c := config{
commandAddress: net.JoinHostPort(host, fmt.Sprint(port)),
platformAddress: net.JoinHostPort(host, fmt.Sprint(port+1)),
skipPlatformCommands: true, // TODO(hs): assumes these steps are performed out of band; does that make sense?
}

rwc, err = open(c)
if err != nil {
return nil, fmt.Errorf("failed opening connection to TPM: %w", err)
}

Check warning on line 34 in tpm/internal/mssim/mssim.go

View check run for this annotation

Codecov / codecov/patch

tpm/internal/mssim/mssim.go#L25-L34

Added lines #L25 - L34 were not covered by tests

// TODO(hs): make connection open lazily? And/or support connection management internally?
// Sometimes it happens that the connection is very slow, or there seems to be no connection
// at all. This is likely due to how we've implemented opening the TPM (once, generally), and
// then reusing that instance.

return

Check warning on line 41 in tpm/internal/mssim/mssim.go

View check run for this annotation

Codecov / codecov/patch

tpm/internal/mssim/mssim.go#L41

Added line #L41 was not covered by tests
}

type CommandChannelWithoutMeasurementLog struct {
io.ReadWriteCloser
}

func (c *CommandChannelWithoutMeasurementLog) MeasurementLog() ([]byte, error) {
return nil, nil

Check warning on line 49 in tpm/internal/mssim/mssim.go

View check run for this annotation

Codecov / codecov/patch

tpm/internal/mssim/mssim.go#L48-L49

Added lines #L48 - L49 were not covered by tests
}
17 changes: 17 additions & 0 deletions tpm/internal/open/open.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,26 @@
package open

import (
"fmt"
"io"
"strings"

"go.step.sm/crypto/kms/uri"
"go.step.sm/crypto/tpm/internal/mssim"
)

func TPM(deviceName string) (io.ReadWriteCloser, error) {
if strings.HasPrefix(deviceName, "mssim:") {
u, err := uri.ParseWithScheme("mssim", deviceName)
if err != nil {
return nil, fmt.Errorf("failed parsing %q: %w", deviceName, err)
}
rwc, err := mssim.New(u)
if err != nil {
return nil, err
}
return rwc, nil

Check warning on line 22 in tpm/internal/open/open.go

View check run for this annotation

Codecov / codecov/patch

tpm/internal/open/open.go#L13-L22

Added lines #L13 - L22 were not covered by tests
}

return open(deviceName)
}
2 changes: 1 addition & 1 deletion tpm/manufacturer/manufacturers.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func GetEncodings(id ID) (ascii, hexa string) {
// GetNameByASCII returns the manufacturer name based on its
// ASCII identifier.
func GetNameByASCII(ascii string) string {
if name, ok := manufacturerByASCII[ascii]; ok {
if name, ok := manufacturerByASCII[strings.TrimSpace(ascii)]; ok {
return name
}
return "unknown"
Expand Down
53 changes: 41 additions & 12 deletions tpm/tpm.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@
"fmt"
"io"
"net/http"
"strings"
"sync"

"github.com/smallstep/go-attestation/attest"

"go.step.sm/crypto/kms/uri"
closer "go.step.sm/crypto/tpm/internal/close"
"go.step.sm/crypto/tpm/internal/mssim"
"go.step.sm/crypto/tpm/internal/open"
"go.step.sm/crypto/tpm/internal/socket"
"go.step.sm/crypto/tpm/simulator"
Expand Down Expand Up @@ -230,19 +233,30 @@
t.commandChannel = t.options.commandChannel
}

// finally, check if the device name points to a UNIX socket, and use that
// as the command channel, if available.
if t.commandChannel == nil {
if socketCommandChannel, err := trySocketCommandChannel(t.deviceName); err != nil {
switch {
case errors.Is(err, socket.ErrNotSupported):
// don't try to use socket command channel if not supported. No need to return
// an error, because the code should still rely on the default TPM command channel.
case !errors.Is(err, socket.ErrNotAvailable):
return err
switch {
case strings.HasPrefix(t.deviceName, "mssim:"):
// if the TPM device name looks like a URI that starts with "mssim:", try
// using a TCP connection to the TPM using the Microsoft PTM simulator TCTI.
msSimCommandChannel, err := tryMsSimCommandChannel(t.deviceName)
if err != nil {
return fmt.Errorf("invalid Microsoft TPM Simulator device name %q: %w", t.deviceName, err)
}
t.commandChannel = msSimCommandChannel
default:
// finally, check if the device name points to a UNIX socket, and use that
// as the command channel, if available.
if socketCommandChannel, err := trySocketCommandChannel(t.deviceName); err != nil {
switch {
case errors.Is(err, socket.ErrNotSupported):

Check warning on line 251 in tpm/tpm.go

View check run for this annotation

Codecov / codecov/patch

tpm/tpm.go#L237-L251

Added lines #L237 - L251 were not covered by tests
// don't try to use socket command channel if not supported. No need to return
// an error, because the code should still rely on the default TPM command channel.
case !errors.Is(err, socket.ErrNotAvailable):
return err

Check warning on line 255 in tpm/tpm.go

View check run for this annotation

Codecov / codecov/patch

tpm/tpm.go#L254-L255

Added lines #L254 - L255 were not covered by tests
}
} else {
t.commandChannel = socketCommandChannel

Check warning on line 258 in tpm/tpm.go

View check run for this annotation

Codecov / codecov/patch

tpm/tpm.go#L257-L258

Added lines #L257 - L258 were not covered by tests
}
} else {
t.commandChannel = socketCommandChannel
}
}

Expand All @@ -257,7 +271,8 @@
return nil
}

// trySocketCommandChannel tries
// trySocketCommandChannel tries to establish connections to a TPM using
// a UNIX socket.
func trySocketCommandChannel(path string) (*socket.CommandChannelWithoutMeasurementLog, error) {
rwc, err := socket.New(path)
if err != nil {
Expand All @@ -266,6 +281,20 @@
return &socket.CommandChannelWithoutMeasurementLog{ReadWriteCloser: rwc}, nil
}

// tryMsSimCommandChannel tries to establish connections to a TPM using the
// Microsoft TPM Simulator TCTI.
func tryMsSimCommandChannel(rawuri string) (*mssim.CommandChannelWithoutMeasurementLog, error) {
u, err := uri.ParseWithScheme("mssim", rawuri)
if err != nil {
return nil, fmt.Errorf("failed parsing %q: %w", rawuri, err)
}
rwc, err := mssim.New(u)
if err != nil {
return nil, err
}
return &mssim.CommandChannelWithoutMeasurementLog{ReadWriteCloser: rwc}, nil

Check warning on line 295 in tpm/tpm.go

View check run for this annotation

Codecov / codecov/patch

tpm/tpm.go#L286-L295

Added lines #L286 - L295 were not covered by tests
}

// Close closes the TPM instance, cleaning up resources and
// marking it ready to be use again.
func (t *TPM) close(ctx context.Context) error {
Expand Down