| 1 |
//go:build !windows |
| 2 |
|
| 3 |
// Copyright (c) 2018, Google LLC All rights reserved. |
| 4 |
// |
| 5 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 |
// you may not use this file except in compliance with the License. |
| 7 |
// You may obtain a copy of the License at |
| 8 |
// |
| 9 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
// |
| 11 |
// Unless required by applicable law or agreed to in writing, software |
| 12 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
// See the License for the specific language governing permissions and |
| 15 |
// limitations under the License. |
| 16 |
|
| 17 |
package tpmutil |
| 18 |
|
| 19 |
import ( |
| 20 |
"fmt" |
| 21 |
"io" |
| 22 |
"net" |
| 23 |
"os" |
| 24 |
) |
| 25 |
|
| 26 |
// OpenTPM opens a channel to the TPM at the given path. If the file is a |
| 27 |
// device, then it treats it like a normal TPM device, and if the file is a |
| 28 |
// Unix domain socket, then it opens a connection to the socket. |
| 29 |
func OpenTPM(path string) (io.ReadWriteCloser, error) { |
| 30 |
// If it's a regular file, then open it |
| 31 |
var rwc io.ReadWriteCloser |
| 32 |
fi, err := os.Stat(path) |
| 33 |
if err != nil { |
| 34 |
return nil, err |
| 35 |
} |
| 36 |
|
| 37 |
if fi.Mode()&os.ModeDevice != 0 { |
| 38 |
var f *os.File |
| 39 |
f, err = os.OpenFile(path, os.O_RDWR, 0600) |
| 40 |
if err != nil { |
| 41 |
return nil, err |
| 42 |
} |
| 43 |
rwc = io.ReadWriteCloser(f) |
| 44 |
} else if fi.Mode()&os.ModeSocket != 0 { |
| 45 |
rwc = NewEmulatorReadWriteCloser(path) |
| 46 |
} else { |
| 47 |
return nil, fmt.Errorf("unsupported TPM file mode %s", fi.Mode().String()) |
| 48 |
} |
| 49 |
|
| 50 |
return rwc, nil |
| 51 |
} |
| 52 |
|
| 53 |
// dialer abstracts the net.Dial call so test code can provide its own net.Conn |
| 54 |
// implementation. |
| 55 |
type dialer func(network, path string) (net.Conn, error) |
| 56 |
|
| 57 |
// EmulatorReadWriteCloser manages connections with a TPM emulator over a Unix |
| 58 |
// domain socket. These emulators often operate in a write/read/disconnect |
| 59 |
// sequence, so the Write method always connects, and the Read method always |
| 60 |
// closes. EmulatorReadWriteCloser is not thread safe. |
| 61 |
type EmulatorReadWriteCloser struct { |
| 62 |
path string |
| 63 |
conn net.Conn |
| 64 |
dialer dialer |
| 65 |
} |
| 66 |
|
| 67 |
// NewEmulatorReadWriteCloser stores information about a Unix domain socket to |
| 68 |
// write to and read from. |
| 69 |
func NewEmulatorReadWriteCloser(path string) *EmulatorReadWriteCloser { |
| 70 |
return &EmulatorReadWriteCloser{ |
| 71 |
path: path, |
| 72 |
dialer: net.Dial, |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
// Read implements io.Reader by reading from the Unix domain socket and closing |
| 77 |
// it. |
| 78 |
func (erw *EmulatorReadWriteCloser) Read(p []byte) (int, error) { |
| 79 |
// Read is always the second operation in a Write/Read sequence. |
| 80 |
if erw.conn == nil { |
| 81 |
return 0, fmt.Errorf("must call Write then Read in an alternating sequence") |
| 82 |
} |
| 83 |
n, err := erw.conn.Read(p) |
| 84 |
erw.conn.Close() |
| 85 |
erw.conn = nil |
| 86 |
return n, err |
| 87 |
} |
| 88 |
|
| 89 |
// Write implements io.Writer by connecting to the Unix domain socket and |
| 90 |
// writing. |
| 91 |
func (erw *EmulatorReadWriteCloser) Write(p []byte) (int, error) { |
| 92 |
if erw.conn != nil { |
| 93 |
return 0, fmt.Errorf("must call Write then Read in an alternating sequence") |
| 94 |
} |
| 95 |
var err error |
| 96 |
erw.conn, err = erw.dialer("unix", erw.path) |
| 97 |
if err != nil { |
| 98 |
return 0, err |
| 99 |
} |
| 100 |
return erw.conn.Write(p) |
| 101 |
} |
| 102 |
|
| 103 |
// Close implements io.Closer by closing the Unix domain socket if one is open. |
| 104 |
func (erw *EmulatorReadWriteCloser) Close() error { |
| 105 |
if erw.conn == nil { |
| 106 |
return fmt.Errorf("cannot call Close when no connection is open") |
| 107 |
} |
| 108 |
err := erw.conn.Close() |
| 109 |
erw.conn = nil |
| 110 |
return err |
| 111 |
} |