| 1 |
/* |
| 2 |
* Copyright 2018 Google Inc. |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 |
* use this file except in compliance with the License. You may obtain a copy of |
| 6 |
* the License at |
| 7 |
* |
| 8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
* |
| 10 |
* Unless required by applicable law or agreed to in writing, software |
| 11 |
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 |
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 |
* License for the specific language governing permissions and limitations under |
| 14 |
* the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
// Package simulator provides a go interface to the Microsoft TPM2 simulator. |
| 18 |
package simulator |
| 19 |
|
| 20 |
import ( |
| 21 |
"bytes" |
| 22 |
"errors" |
| 23 |
"fmt" |
| 24 |
"math/rand" |
| 25 |
"sync" |
| 26 |
|
| 27 |
"github.com/google/go-tpm-tools/simulator/internal" |
| 28 |
"github.com/google/go-tpm/legacy/tpm2" |
| 29 |
) |
| 30 |
|
| 31 |
// Simulator represents a go-tpm compatible interface to the TPM2 simulator. |
| 32 |
// Similar to the file-based (for linux) or syscall-based (for Windows) TPM |
| 33 |
// handles, no synchronization is provided; the same simulator handle should not |
| 34 |
// be used from multiple threads. |
| 35 |
type Simulator struct { |
| 36 |
buf bytes.Buffer |
| 37 |
closed bool |
| 38 |
} |
| 39 |
|
| 40 |
// ErrUsingClosedSimulator is returned if any operation on a Simulator is |
| 41 |
// attempted after it is closed. |
| 42 |
var ErrUsingClosedSimulator = errors.New("attempting to use a closed simulator") |
| 43 |
|
| 44 |
// The simulator is a global resource, so we use the variables below to make |
| 45 |
// sure we only ever have one open reference to the Simulator at a time. |
| 46 |
var lock sync.Mutex |
| 47 |
|
| 48 |
// Get the pointer to an initialized, powered on, and started simulator. As only |
| 49 |
// one simulator may be running at a time, a second call to Get() block until |
| 50 |
// the first Simulator is Closed. |
| 51 |
func Get() (*Simulator, error) { |
| 52 |
lock.Lock() |
| 53 |
|
| 54 |
simulator := &Simulator{} |
| 55 |
internal.Reset(true) |
| 56 |
if err := simulator.on(true); err != nil { |
| 57 |
lock.Unlock() |
| 58 |
return nil, err |
| 59 |
} |
| 60 |
simulator.closed = false |
| 61 |
return simulator, nil |
| 62 |
} |
| 63 |
|
| 64 |
// GetWithFixedSeedInsecure behaves like Get() expect that all of the internal |
| 65 |
// hierarchy seeds are derived from the input seed. Note that this function |
| 66 |
// compromises the security of the keys/seeds and should only be used for tests. |
| 67 |
func GetWithFixedSeedInsecure(seed int64) (*Simulator, error) { |
| 68 |
s, err := Get() |
| 69 |
if err != nil { |
| 70 |
return nil, err |
| 71 |
} |
| 72 |
|
| 73 |
internal.SetSeeds(rand.New(rand.NewSource(seed))) |
| 74 |
return s, nil |
| 75 |
} |
| 76 |
|
| 77 |
// Reset the TPM as if the host computer had rebooted. |
| 78 |
func (s *Simulator) Reset() error { |
| 79 |
if s.IsClosed() { |
| 80 |
return ErrUsingClosedSimulator |
| 81 |
} |
| 82 |
if err := s.off(); err != nil { |
| 83 |
return err |
| 84 |
} |
| 85 |
internal.Reset(false) |
| 86 |
return s.on(false) |
| 87 |
} |
| 88 |
|
| 89 |
// ManufactureReset behaves like Reset() except that the TPM is complete wiped. |
| 90 |
// All data (NVData, Hierarchy seeds, etc...) is cleared or reset. |
| 91 |
func (s *Simulator) ManufactureReset() error { |
| 92 |
if s.IsClosed() { |
| 93 |
return ErrUsingClosedSimulator |
| 94 |
} |
| 95 |
if err := s.off(); err != nil { |
| 96 |
return err |
| 97 |
} |
| 98 |
internal.Reset(true) |
| 99 |
return s.on(true) |
| 100 |
} |
| 101 |
|
| 102 |
// Write executes the command specified by commandBuffer. The command response |
| 103 |
// can be retrieved with a subsequent call to Read(). |
| 104 |
func (s *Simulator) Write(commandBuffer []byte) (int, error) { |
| 105 |
if s.IsClosed() { |
| 106 |
return 0, ErrUsingClosedSimulator |
| 107 |
} |
| 108 |
resp, err := internal.RunCommand(commandBuffer) |
| 109 |
if err != nil { |
| 110 |
return 0, err |
| 111 |
} |
| 112 |
// write response to the internal response buffer. |
| 113 |
_, _ = s.buf.Write(resp) |
| 114 |
return len(commandBuffer), nil |
| 115 |
} |
| 116 |
|
| 117 |
// Read gets the response of a command previously issued by calling Write(). |
| 118 |
func (s *Simulator) Read(responseBuffer []byte) (int, error) { |
| 119 |
if s.IsClosed() { |
| 120 |
return 0, ErrUsingClosedSimulator |
| 121 |
} |
| 122 |
return s.buf.Read(responseBuffer) |
| 123 |
} |
| 124 |
|
| 125 |
// Close cleans up and stops the simulator, Close() should always be called when |
| 126 |
// the Simulator is no longer needed, freeing up other callers to use Get(). |
| 127 |
func (s *Simulator) Close() error { |
| 128 |
if s.IsClosed() { |
| 129 |
return ErrUsingClosedSimulator |
| 130 |
} |
| 131 |
err := s.off() |
| 132 |
s.closed = true |
| 133 |
lock.Unlock() |
| 134 |
return err |
| 135 |
} |
| 136 |
|
| 137 |
// IsClosed returns true if the simulator has been Closed() |
| 138 |
func (s *Simulator) IsClosed() bool { |
| 139 |
return s.closed |
| 140 |
} |
| 141 |
|
| 142 |
func (s *Simulator) on(_ bool) error { |
| 143 |
// TPM2_Startup must be the first command the TPM receives. |
| 144 |
if err := tpm2.Startup(s, tpm2.StartupClear); err != nil { |
| 145 |
return fmt.Errorf("startup: %w", err) |
| 146 |
} |
| 147 |
return nil |
| 148 |
} |
| 149 |
|
| 150 |
func (s *Simulator) off() error { |
| 151 |
// TPM2_Shutdown must be the last command the TPM receives. We call |
| 152 |
// Shutdown with StartupClear to simulate a full reboot. |
| 153 |
if err := tpm2.Shutdown(s, tpm2.StartupClear); err != nil { |
| 154 |
return fmt.Errorf("shutdown: %w", err) |
| 155 |
} |
| 156 |
return nil |
| 157 |
} |