|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "flag" |
| 6 | + "fmt" |
| 7 | + "os" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "go.step.sm/crypto/kms/apiv1" |
| 11 | + "go.step.sm/crypto/kms/cloudkms" |
| 12 | +) |
| 13 | + |
| 14 | +func usage() { |
| 15 | + fmt.Fprintf(flag.CommandLine.Output(), "%s [options] <resource>\n", os.Args[0]) |
| 16 | + flag.PrintDefaults() |
| 17 | +} |
| 18 | + |
| 19 | +func main() { |
| 20 | + var certs, attributes, content, help bool |
| 21 | + flag.BoolVar(&certs, "certs", false, "Print the attestation certificates") |
| 22 | + flag.BoolVar(&attributes, "attributes", false, "Print the attestation attributes") |
| 23 | + flag.BoolVar(&content, "content", false, "Print the attestation file") |
| 24 | + flag.BoolVar(&help, "help", false, "Print the program usage") |
| 25 | + flag.Parse() |
| 26 | + |
| 27 | + switch { |
| 28 | + case help: |
| 29 | + usage() |
| 30 | + os.Exit(0) |
| 31 | + case len(flag.Args()) != 1: |
| 32 | + usage() |
| 33 | + os.Exit(1) |
| 34 | + case certs && attributes, certs && content, attributes && content: |
| 35 | + fmt.Fprintln(flag.CommandLine.Output(), "flag --certs, --attributes, and --content are mutually exclusive") |
| 36 | + os.Exit(1) |
| 37 | + } |
| 38 | + |
| 39 | + km, err := cloudkms.New(context.Background(), apiv1.Options{}) |
| 40 | + if err != nil { |
| 41 | + fmt.Fprintln(os.Stderr, err.Error()) |
| 42 | + os.Exit(2) |
| 43 | + } |
| 44 | + |
| 45 | + att, err := km.VerifyAttestation(context.Background(), flag.Arg(0)) |
| 46 | + _ = km.Close() |
| 47 | + if err != nil { |
| 48 | + fmt.Fprintln(os.Stderr, err.Error()) |
| 49 | + os.Exit(3) |
| 50 | + } |
| 51 | + |
| 52 | + if certs { |
| 53 | + fmt.Println(strings.TrimSpace(att.CertChain.ManufacturerRoot)) |
| 54 | + fmt.Println(strings.TrimSpace(att.CertChain.ManufacturerCardCert)) |
| 55 | + fmt.Println(strings.TrimSpace(att.CertChain.ManufacturerPartitionCert)) |
| 56 | + fmt.Println(strings.TrimSpace(att.CertChain.OwnerRoot)) |
| 57 | + fmt.Println(strings.TrimSpace(att.CertChain.OwnerCardCert)) |
| 58 | + fmt.Println(strings.TrimSpace(att.CertChain.OwnerPartitionCert)) |
| 59 | + return |
| 60 | + } |
| 61 | + |
| 62 | + if attributes { |
| 63 | + if len(att.PublicKeyAttributes) > 0 { |
| 64 | + fmt.Println("Public Key Attestation") |
| 65 | + for _, v := range att.PublicKeyAttributes { |
| 66 | + fmt.Println(v.String()) |
| 67 | + } |
| 68 | + } |
| 69 | + if len(att.PrivateKeyAttributes) > 0 { |
| 70 | + fmt.Println("Private Key Attestation") |
| 71 | + for _, v := range att.PrivateKeyAttributes { |
| 72 | + fmt.Println(v.String()) |
| 73 | + } |
| 74 | + } |
| 75 | + if len(att.SymmetricKeyAttributes) > 0 { |
| 76 | + fmt.Println("Symmetric Key Attestation") |
| 77 | + for _, v := range att.SymmetricKeyAttributes { |
| 78 | + fmt.Println(v.String()) |
| 79 | + } |
| 80 | + } |
| 81 | + return |
| 82 | + } |
| 83 | + |
| 84 | + if content { |
| 85 | + os.Stdout.Write(att.Content) |
| 86 | + return |
| 87 | + } |
| 88 | + |
| 89 | + fmt.Println("Attested:", att.Valid) |
| 90 | + fmt.Println("Generated:", att.Generated) |
| 91 | + fmt.Println("Extractable:", att.Extractable) |
| 92 | + fmt.Println("KeyType:", att.KeyType) |
| 93 | + fmt.Println("Algorithm:", att.Algorithm) |
| 94 | +} |
0 commit comments