File: validate_test.go

package info (click to toggle)
golang-github-smallstep-crypto 0.63.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,800 kB
  • sloc: sh: 66; makefile: 50
file content (323 lines) | stat: -rw-r--r-- 11,151 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
package jose

import (
	"bytes"
	"crypto"
	"crypto/ed25519"
	"crypto/rand"
	"crypto/sha1"
	"crypto/x509"
	"encoding/base64"
	"os"
	"testing"

	"github.com/pkg/errors"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"

	"go.step.sm/crypto/keyutil"
	"go.step.sm/crypto/pemutil"
)

var (
	badCertFile = "./testdata/bad-rsa.crt"
	badKeyFile  = "./testdata/bad-rsa.key"
	certFile    = "./testdata/rsa2048.crt"
	keyFile     = "./testdata/rsa2048.key"
)

func TestValidateSSHPOP(t *testing.T) {
	key, err := pemutil.Read("testdata/host-key")
	if err != nil {
		t.Fatal(err)
	}
	b, err := os.ReadFile("testdata/host-key-cert.pub")
	if err != nil {
		t.Fatal(err)
	}
	fields := bytes.Fields(b)
	if len(fields) != 3 {
		t.Fatalf("unexpected number of fields, got = %d, want 3", len(fields))
	}
	certBase64 := string(fields[1])

	_, otherKey, err := ed25519.GenerateKey(rand.Reader)
	if err != nil {
		t.Fatal(err)
	}

	type args struct {
		certFile string
		key      interface{}
	}
	tests := []struct {
		name    string
		args    args
		want    string
		wantErr bool
	}{
		{"ok crypto.PrivateKey", args{"testdata/host-key-cert.pub", key}, certBase64, false},
		{"ok JSONWebKey", args{"testdata/host-key-cert.pub", &JSONWebKey{Key: key}}, certBase64, false},
		{"ok OpaqueSigner", args{"testdata/host-key-cert.pub", NewOpaqueSigner(key.(crypto.Signer))}, certBase64, false},
		{"fail certFile", args{"", key}, "", true},
		{"fail missing", args{"testdata/missing", key}, "", true},
		{"fail not ssh", args{"testdata/rsa2048.crt", key}, "", true},
		{"fail not a cert", args{"testdata/host-key.pub", key}, "", true},
		{"fail validate crypto.PrivateKey", args{"testdata/host-key-cert.pub", otherKey}, "", true},
		{"fail validate JSONWebKey", args{"testdata/host-key-cert.pub", &JSONWebKey{Key: otherKey}}, "", true},
		{"fail validate OpaqueSigner", args{"testdata/host-key-cert.pub", NewOpaqueSigner(otherKey)}, "", true},
		{"fail bad key", args{"testdata/host-key-cert.pub", "not a key"}, "", true},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			got, err := ValidateSSHPOP(tt.args.certFile, tt.args.key)
			if (err != nil) != tt.wantErr {
				t.Errorf("ValidateSSHPOP() error = %v, wantErr %v", err, tt.wantErr)
				return
			}
			if got != tt.want {
				t.Errorf("ValidateSSHPOP() = %v, want %v", got, tt.want)
			}
		})
	}
}

func Test_validateX5(t *testing.T) {
	type test struct {
		certs []*x509.Certificate
		key   interface{}
		err   error
	}
	tests := map[string]func() test{
		"fail/empty-certs": func() test {
			return test{
				certs: []*x509.Certificate{},
				key:   nil,
				err:   errors.New("certs cannot be empty"),
			}
		},
		"fail/bad-key": func() test {
			certs, err := pemutil.ReadCertificateBundle(certFile)
			require.NoError(t, err)
			return test{
				certs: certs,
				key:   nil,
				err:   errors.New("error verifying certificate and key"),
			}
		},
		"fail/cert-not-approved-for-digital-signature": func() test {
			certs, err := pemutil.ReadCertificateBundle(badCertFile)
			require.NoError(t, err)
			k, err := pemutil.Read(badKeyFile)
			require.NoError(t, err)
			return test{
				certs: certs,
				key:   k,
				err: errors.New("certificate/private-key pair used to sign " +
					"token is not approved for digital signature"),
			}
		},
		"ok": func() test {
			certs, err := pemutil.ReadCertificateBundle(certFile)
			require.NoError(t, err)
			k, err := pemutil.Read(keyFile)
			require.NoError(t, err)
			return test{
				certs: certs,
				key:   k,
			}
		},
	}
	for name, run := range tests {
		t.Run(name, func(t *testing.T) {
			tc := run()
			if err := validateX5(tc.certs, tc.key); err != nil {
				if assert.NotNil(t, tc.err) {
					assert.Contains(t, err.Error(), tc.err.Error())
				}
			} else {
				assert.Nil(t, tc.err)
			}
		})
	}
}

func TestValidateX5T(t *testing.T) {
	type test struct {
		certs []*x509.Certificate
		key   interface{}
		fp    string
		err   error
	}
	tests := map[string]func() test{
		"fail/validateX5-error": func() test {
			return test{
				certs: []*x509.Certificate{},
				key:   nil,
				err:   errors.New("ValidateX5T: certs cannot be empty"),
			}
		},
		"ok": func() test {
			certs, err := pemutil.ReadCertificateBundle(certFile)
			require.NoError(t, err)
			k, err := pemutil.Read(keyFile)
			require.NoError(t, err)
			cert, err := pemutil.ReadCertificate(certFile)
			require.NoError(t, err)
			// x5t is the base64 URL encoded SHA1 thumbprint
			// (see https://tools.ietf.org/html/rfc7515#section-4.1.7)
			// nolint:gosec // RFC 7515 - X.509 Certificate SHA-1 Thumbprint
			fp := sha1.Sum(cert.Raw)
			return test{
				certs: certs,
				key:   k,
				fp:    base64.URLEncoding.EncodeToString(fp[:]),
			}
		},
		"ok/opaque": func() test {
			certs, err := pemutil.ReadCertificateBundle(certFile)
			require.NoError(t, err)
			k, err := pemutil.Read(keyFile)
			require.NoError(t, err)
			sig, ok := k.(crypto.Signer)
			assert.True(t, ok)
			op := NewOpaqueSigner(sig)
			cert, err := pemutil.ReadCertificate(certFile)
			require.NoError(t, err)
			// x5t is the base64 URL encoded SHA1 thumbprint
			// (see https://tools.ietf.org/html/rfc7515#section-4.1.7)
			// nolint:gosec // RFC 7515 - X.509 Certificate SHA-1 Thumbprint
			fp := sha1.Sum(cert.Raw)
			return test{
				certs: certs,
				key:   op,
				fp:    base64.URLEncoding.EncodeToString(fp[:]),
			}
		},
	}
	for name, run := range tests {
		t.Run(name, func(t *testing.T) {
			tc := run()
			if fingerprint, err := ValidateX5T(tc.certs, tc.key); err != nil {
				if assert.NotNil(t, tc.err) {
					assert.Contains(t, err.Error(), tc.err.Error())
				}
			} else {
				assert.Nil(t, tc.err)
				assert.Equal(t, tc.fp, fingerprint)
			}
		})
	}
}

func TestValidateX5C(t *testing.T) {
	type test struct {
		certs []*x509.Certificate
		key   interface{}
		err   error
	}
	tests := map[string]func() test{
		"fail/validateX5-error": func() test {
			return test{
				certs: []*x509.Certificate{},
				key:   nil,
				err:   errors.New("ValidateX5C: certs cannot be empty"),
			}
		},
		"ok": func() test {
			certs, err := pemutil.ReadCertificateBundle(certFile)
			require.NoError(t, err)
			k, err := pemutil.Read(keyFile)
			require.NoError(t, err)
			return test{
				certs: certs,
				key:   k,
			}
		},
		"ok/opaque": func() test {
			certs, err := pemutil.ReadCertificateBundle(certFile)
			require.NoError(t, err)
			k, err := pemutil.Read(keyFile)
			require.NoError(t, err)
			sig, ok := k.(crypto.Signer)
			assert.True(t, ok)
			op := NewOpaqueSigner(sig)
			return test{
				certs: certs,
				key:   op,
			}
		},
	}
	for name, run := range tests {
		t.Run(name, func(t *testing.T) {
			tc := run()
			if certs, err := ValidateX5C(tc.certs, tc.key); err != nil {
				if assert.NotNil(t, tc.err) {
					assert.Contains(t, err.Error(), tc.err.Error())
				}
			} else {
				assert.Nil(t, tc.err)
				assert.Equal(t, []string{`MIIDCTCCAfGgAwIBAgIQIdY8a5pFZ/FGUowvuGdJvTANBgkqhkiG9w0BAQsFADAPMQ0wCwYDVQQDEwR0ZXN0MB4XDTIwMDQyMTA0MDg0MFoXDTIwMDQyMjA0MDg0MFowDzENMAsGA1UEAxMEdGVzdDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAL0zsTneONS0eNto7LMlD8GtcUYXqILSEWD07v0HgbIguBT+yC8BozpAT3lyB+oBBZkFLzfEHAteULngPwlq0R5hsEZJ6lcL1Z9WXwyLE4nkEndIPMA+zQmHnOzoqgKy7pIqUnFqSGXtGp384fFF3Y0/qjeFciLnmf+Wn0PneaToY1rDj2Eb9sFf5UDiVaSLT1NzpSyXOS5uGbGplPe+WE8uEb3u3Vg2VGbEPau2l5MPYroCwSyxqlpKsmzJ558uvjQ7KpRExSNdb6f0iRfdRMbw3LahrxhbKV1mmM6GD5onmbgBCZpw5htOJj1MzVFZOdnoTHmMl/Y/IUdMjv0jG/UCAwEAAaNhMF8wDgYDVR0PAQH/BAQDAgWgMB0GA1UdJQQWMBQGCCsGAQUFBwMBBggrBgEFBQcDAjAdBgNVHQ4EFgQUXlQCQL6RymQZnvqY15F/GlE3H4UwDwYDVR0RBAgwBoIEdGVzdDANBgkqhkiG9w0BAQsFAAOCAQEArVmOL5L+NVsnCcUWfOVXQYg/6P8AGdPJBECk+BE5pbtMg0GuH5Ml/vCTBqD+diWC4O0TZDxPMhXH5Ehl+67hcqeu4riwB2WvvKOlAqHqIuqVDRHtxwknvS1efstBKVdDC6aAfIa5f2dmCSxvd8elpcnufEefLGALTSPxg4uMVvpfWUkkmpmvOUpI3gNrlvP2H4KZk7hKYz+J4x2jv2pdPWUAtt1U4M8oQ4BCPrrHSxznw2Q5mdCMIB64ZeYnZ+rAMQS6WnZy1fTC3d0pCs0UCXH5JefBpha1clqHDUkxHA6/1EYYsSlKGFaPEmfv2uw7MFz0o+yntG34KVdsC8HO3g==`}, certs)
			}
		})
	}
}

func TestValidateJWK_sig(t *testing.T) {
	mustSigner := func(kty, crv string, size int) crypto.Signer {
		signer, err := keyutil.GenerateSigner(kty, crv, size)
		if err != nil {
			t.Fatal(err)
		}
		return signer
	}

	rsaKey := mustSigner("RSA", "", 2048)
	p256Key := mustSigner("EC", "P-256", 0)
	p384key := mustSigner("EC", "P-384", 0)
	p521Key := mustSigner("EC", "P-521", 0)
	edKey := mustSigner("OKP", "Ed25519", 0)

	type args struct {
		jwk *JSONWebKey
	}
	tests := []struct {
		name    string
		args    args
		wantErr bool
	}{
		{"ok ES256", args{&JSONWebKey{Use: "sig", Algorithm: ES256, Key: p256Key}}, false},
		{"ok ES384", args{&JSONWebKey{Use: "sig", Algorithm: ES384, Key: p384key}}, false},
		{"ok ES512", args{&JSONWebKey{Use: "sig", Algorithm: ES512, Key: p521Key}}, false},
		{"ok ES256 pub", args{&JSONWebKey{Use: "sig", Algorithm: ES256, Key: p256Key.Public()}}, false},
		{"ok ES384 pub", args{&JSONWebKey{Use: "sig", Algorithm: ES384, Key: p384key.Public()}}, false},
		{"ok ES512 pub", args{&JSONWebKey{Use: "sig", Algorithm: ES512, Key: p521Key.Public()}}, false},
		{"ok RS256", args{&JSONWebKey{Use: "sig", Algorithm: RS256, Key: rsaKey}}, false},
		{"ok RS384", args{&JSONWebKey{Use: "sig", Algorithm: RS384, Key: rsaKey.Public()}}, false},
		{"ok RS512", args{&JSONWebKey{Use: "sig", Algorithm: RS512, Key: rsaKey}}, false},
		{"ok PS256", args{&JSONWebKey{Use: "sig", Algorithm: PS256, Key: rsaKey.Public()}}, false},
		{"ok PS384", args{&JSONWebKey{Use: "sig", Algorithm: PS384, Key: rsaKey}}, false},
		{"ok PS512", args{&JSONWebKey{Use: "sig", Algorithm: PS512, Key: rsaKey.Public()}}, false},
		{"ok EdDSA", args{&JSONWebKey{Use: "sig", Algorithm: EdDSA, Key: edKey}}, false},
		{"ok EdDSA pub", args{&JSONWebKey{Use: "sig", Algorithm: EdDSA, Key: edKey.Public()}}, false},
		{"ok HS256", args{&JSONWebKey{Use: "sig", Algorithm: HS256, Key: []byte("raw-key")}}, false},
		{"ok HS384", args{&JSONWebKey{Use: "sig", Algorithm: HS384, Key: []byte("raw-key")}}, false},
		{"ok HS512", args{&JSONWebKey{Use: "sig", Algorithm: HS512, Key: []byte("raw-key")}}, false},
		{"ok OpaqueSigner", args{&JSONWebKey{Use: "sig", Algorithm: ES256, Key: NewOpaqueSigner(p256Key)}}, false},
		{"fail alg empty", args{&JSONWebKey{Use: "sig", Key: p256Key}}, true},
		{"fail ECDSA", args{&JSONWebKey{Use: "sig", Algorithm: ES384, Key: p256Key}}, true},
		{"fail ECDSA pub", args{&JSONWebKey{Use: "sig", Algorithm: ES384, Key: p256Key.Public()}}, true},
		{"fail RSA", args{&JSONWebKey{Use: "sig", Algorithm: ES256, Key: rsaKey}}, true},
		{"fail Ed25519", args{&JSONWebKey{Use: "sig", Algorithm: ES256, Key: edKey}}, true},
		{"fail bytes", args{&JSONWebKey{Use: "sig", Algorithm: ES256, Key: []byte("raw-key")}}, true},
		{"fail OpaqueSigner", args{&JSONWebKey{Use: "sig", Algorithm: RS256, Key: p256Key}}, true},
		{"fail unknown", args{&JSONWebKey{Use: "sig", Algorithm: HS256, Key: "raw-key"}}, true},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			if err := ValidateJWK(tt.args.jwk); (err != nil) != tt.wantErr {
				t.Errorf("ValidateJWK() error = %v, wantErr %v", err, tt.wantErr)
			}
		})
	}
}