forked from vmware/govmomi
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
govc: Cmd to enc data for VMs w TPM2 devs
This patch introduces support for encrypting plain-text information for VMs with TPM2 devices without the system on which the command is run needing a TPM. Please refer to google/go-tpm#343 for more information.
- Loading branch information
Showing
4 changed files
with
187 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,178 @@ | ||
/* | ||
Copyright (c) 2023 VMware, Inc. 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 tpm2 | ||
|
||
import ( | ||
"context" | ||
"crypto/x509" | ||
"encoding/base64" | ||
"flag" | ||
"fmt" | ||
"io" | ||
"os" | ||
|
||
"github.com/vmware/govmomi/govc/cli" | ||
"github.com/vmware/govmomi/govc/flags" | ||
"github.com/vmware/govmomi/object" | ||
"github.com/vmware/govmomi/vim25/mo" | ||
"github.com/vmware/govmomi/vim25/types" | ||
|
||
"github.com/google/go-tpm/tpm2" | ||
) | ||
|
||
type seal struct { | ||
*flags.VirtualMachineFlag | ||
*flags.OutputFlag | ||
|
||
input string | ||
format string | ||
} | ||
|
||
func init() { | ||
cli.Register("vm.tpm2.seal", &seal{}) | ||
} | ||
|
||
func (cmd *seal) Register(ctx context.Context, f *flag.FlagSet) { | ||
cmd.VirtualMachineFlag, ctx = flags.NewVirtualMachineFlag(ctx) | ||
cmd.VirtualMachineFlag.Register(ctx, f) | ||
cmd.OutputFlag, ctx = flags.NewOutputFlag(ctx) | ||
cmd.OutputFlag.Register(ctx, f) | ||
|
||
f.StringVar(&cmd.input, "in", "-", `Input data. Defaults to STDIN via "-"`) | ||
f.StringVar(&cmd.format, "of", "json", | ||
"Output format. Options are \"json\", \"cmds\", and \"0\". "+ | ||
"The format \"json\" emits the encrypted data as a JSON object. "+ | ||
"The format \"cmds\" emits three shell commands that can be copied "+ | ||
"and pasted into another system to easily create the encrypted "+ | ||
"data's three parts as files. "+ | ||
"The format \"0\" emits the encrypted data as base64-encoded"+ | ||
"data structures delimited by the string \"@@NULL@@\".") | ||
} | ||
|
||
func (cmd *seal) Description() string { | ||
return `Seal plain-text data to the VM's TPM2 endorsement key. | ||
Examples: | ||
govc vm.tpm2.seal -vm VM -in plain.txt | ||
echo "Hello, world" | govc vm.tpm2.seal -vm VM` | ||
} | ||
|
||
func (cmd *seal) Process(ctx context.Context) error { | ||
if err := cmd.VirtualMachineFlag.Process(ctx); err != nil { | ||
return err | ||
} | ||
if err := cmd.OutputFlag.Process(ctx); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func (cmd *seal) Run(ctx context.Context, f *flag.FlagSet) error { | ||
vm, err := cmd.VirtualMachine() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if vm == nil { | ||
return flag.ErrHelp | ||
} | ||
|
||
// Read the plain-text data. | ||
var plainTextFile *os.File | ||
if cmd.input == "-" { | ||
plainTextFile = os.Stdin | ||
} else { | ||
f, err := os.Open(cmd.input) | ||
if err != nil { | ||
return err | ||
} | ||
plainTextFile = f | ||
defer f.Close() | ||
} | ||
plainTextData, err := io.ReadAll(plainTextFile) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Get the VM's EK. | ||
var moVM mo.VirtualMachine | ||
if err := vm.Properties( | ||
ctx, | ||
vm.Reference(), | ||
[]string{"config.hardware.device"}, | ||
&moVM); err != nil { | ||
return err | ||
} | ||
|
||
devices := object.VirtualDeviceList(moVM.Config.Hardware.Device) | ||
selectedDevices := devices.SelectByType(&types.VirtualTPM{}) | ||
if len(selectedDevices) == 0 { | ||
return fmt.Errorf("no VirtualTPM devices found") | ||
} | ||
if len(selectedDevices) > 1 { | ||
return fmt.Errorf("multiple VirtualTPM devices found") | ||
} | ||
|
||
vtpmDev := selectedDevices[0].(*types.VirtualTPM) | ||
|
||
// There will be two EK certs. The first is RSA and the second is ECC. | ||
// We want the RSA cert. Also, using DecodeString since Decode leaves | ||
// trailing data. | ||
certDer, err := base64.StdEncoding.DecodeString( | ||
string(vtpmDev.EndorsementKeyCertificate[0])) | ||
if err != nil { | ||
return err | ||
} | ||
base64.StdEncoding.Decode(certDer, vtpmDev.EndorsementKeyCertificate[0]) | ||
cert, err := x509.ParseCertificate(certDer) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
ek, err := tpm2.EKCertToTPMTPublic(*cert) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
pub, priv, seed, err := tpm2.EKSeal(ek, plainTextData) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return cmd.WriteResult(sealResult{ | ||
Public: base64.StdEncoding.EncodeToString(tpm2.Marshal(pub)), | ||
Private: base64.StdEncoding.EncodeToString(tpm2.Marshal(priv)), | ||
Seed: base64.StdEncoding.EncodeToString(tpm2.Marshal(seed)), | ||
}) | ||
} | ||
|
||
type sealResult struct { | ||
Public string `json:"public"` | ||
Private string `json:"private"` | ||
Seed string `json:"seed"` | ||
} | ||
|
||
func (r sealResult) Write(w io.Writer) error { | ||
_, err := fmt.Fprintf( | ||
w, | ||
"%s@@NULL@@%s@@NULL@@%s", | ||
r.Public, | ||
r.Private, | ||
r.Seed, | ||
) | ||
return err | ||
} |