| 1 |
//go:build !windows |
| 2 |
|
| 3 |
// Copyright (c) 2019, 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 tpm |
| 18 |
|
| 19 |
import ( |
| 20 |
"fmt" |
| 21 |
"io" |
| 22 |
|
| 23 |
"github.com/google/go-tpm/tpmutil" |
| 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 |
return openAndStartupTPM(path, false) |
| 31 |
} |
| 32 |
|
| 33 |
// openAndStartupTPM opens the TPM and optionally runs TPM_Startup if needed. |
| 34 |
// This feature is implemented only for testing. |
| 35 |
func openAndStartupTPM(path string, doStartup bool) (io.ReadWriteCloser, error) { |
| 36 |
rwc, err := tpmutil.OpenTPM(path) |
| 37 |
if err != nil { |
| 38 |
return nil, err |
| 39 |
} |
| 40 |
|
| 41 |
// Make sure this is a TPM 1.2 |
| 42 |
_, err = GetManufacturer(rwc) |
| 43 |
if doStartup && err == tpmError(errInvalidPostInit) { |
| 44 |
if err = startup(rwc); err == nil { |
| 45 |
_, err = GetManufacturer(rwc) |
| 46 |
} |
| 47 |
} |
| 48 |
if err != nil { |
| 49 |
rwc.Close() |
| 50 |
return nil, fmt.Errorf("open %s: device is not a TPM 1.2: %v", path, err) |
| 51 |
} |
| 52 |
return rwc, nil |
| 53 |
} |