1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package test
25
26 import (
27 "context"
28 "crypto/x509"
29 "testing"
30
31
32 . "github.com/sigstore/cosign/v2/cmd/cosign/cli/pivcli"
33 )
34
35 func TestSetManagementKeyCmd(t *testing.T) {
36 ctx := context.Background()
37
38 Confirm = func(_ string) bool { return true }
39 must(ResetKeyCmd(ctx), t)
40
41
42 mustErr(SetManagementKeyCmd(ctx, "foobar", "newkey", false), t)
43 must(SetManagementKeyCmd(ctx, "", "newkey", false), t)
44
45
46 mustErr(SetManagementKeyCmd(ctx, "", "otherkey", false), t)
47
48 must(SetManagementKeyCmd(ctx, "newkey", "otherkey", false), t)
49
50
51 must(ResetKeyCmd(ctx), t)
52 must(SetManagementKeyCmd(ctx, "", "", true), t)
53 mustErr(SetManagementKeyCmd(ctx, "", "", true), t)
54 }
55
56 func TestSetPUKCmd(t *testing.T) {
57 ctx := context.Background()
58
59 Confirm = func(_ string) bool { return true }
60 must(ResetKeyCmd(ctx), t)
61
62
63 mustErr(SetPukCmd(ctx, "11111111", "12121212"), t)
64 must(SetPukCmd(ctx, "", "12121212"), t)
65
66
67 mustErr(SetPukCmd(ctx, "", "43214321"), t)
68
69 must(SetPukCmd(ctx, "12121212", "43214321"), t)
70 }
71
72 func TestSetPinCmd(t *testing.T) {
73 ctx := context.Background()
74
75 Confirm = func(_ string) bool { return true }
76 must(ResetKeyCmd(ctx), t)
77
78
79 mustErr(SetPinCmd(ctx, "111111", "222222"), t)
80 must(SetPinCmd(ctx, "", "222222"), t)
81
82
83 mustErr(SetPinCmd(ctx, "333333", "444444"), t)
84
85 must(SetPinCmd(ctx, "222222", "111111"), t)
86 }
87
88 func TestUnblockCmd(t *testing.T) {
89 ctx := context.Background()
90
91 Confirm = func(_ string) bool { return true }
92 must(ResetKeyCmd(ctx), t)
93
94
95 must(SetPukCmd(ctx, "", "43214321"), t)
96
97 must(SetPinCmd(ctx, "", "111111"), t)
98
99 for i := 0; i < 5; i++ {
100 mustErr(SetPinCmd(ctx, "222222", "333333"), t)
101 }
102
103
104 mustErr(SetPinCmd(ctx, "111111", "222222"), t)
105
106
107 must(UnblockCmd(ctx, "43214321", "222222"), t)
108 must(SetPinCmd(ctx, "222222", "333333"), t)
109 }
110
111 func TestGenerateKeyCmd(t *testing.T) {
112 ctx := context.Background()
113
114 Confirm = func(_ string) bool { return true }
115 must(ResetKeyCmd(ctx), t)
116
117
118 must(GenerateKeyCmd(ctx, "", false, "", "", ""), t)
119
120
121 must(SetManagementKeyCmd(ctx, "", "mynewkey", false), t)
122
123 mustErr(GenerateKeyCmd(ctx, "", false, "", "", ""), t)
124
125 must(GenerateKeyCmd(ctx, "mynewkey", false, "", "", ""), t)
126
127
128 must(GenerateKeyCmd(ctx, "mynewkey", true, "", "", ""), t)
129
130 mustErr(GenerateKeyCmd(ctx, "mynewkey", false, "", "", ""), t)
131 }
132
133 func TestAttestationCmd(t *testing.T) {
134 ctx := context.Background()
135
136 Confirm = func(_ string) bool { return true }
137 must(ResetKeyCmd(ctx), t)
138 must(GenerateKeyCmd(ctx, "", false, "", "", ""), t)
139
140 attestations, err := AttestationCmd(ctx, "")
141 if err != nil {
142 t.Fatal(err)
143 }
144
145 root := x509.NewCertPool()
146 if !root.AppendCertsFromPEM([]byte(yubicoCert)) {
147 t.Fatal("error adding roots")
148 }
149
150
151 if _, err := attestations.DeviceCert.Verify(x509.VerifyOptions{
152 Roots: root,
153 }); err != nil {
154 t.Fatal(err)
155 }
156
157 intermediate := x509.NewCertPool()
158 intermediate.AddCert(attestations.DeviceCert)
159
160 if _, err := attestations.KeyCert.Verify(x509.VerifyOptions{
161 Roots: root,
162 Intermediates: intermediate,
163 }); err != nil {
164
165
166
167 if attestations.KeyAttestation.Version.Major == 4 &&
168 attestations.KeyAttestation.Version.Minor == 3 {
169 t.Skipf("key attestation cert chain verification is known to be broken on firmware 4.3")
170 } else {
171 t.Fatal(err)
172 }
173
174 }
175
176 }
177
178 const yubicoCert =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