File: bundle_from_file_test.go

package info (click to toggle)
golang-github-cloudflare-cfssl 1.2.0%2Bgit20160825.89.7fb22c8-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 4,916 kB
  • ctags: 2,827
  • sloc: sh: 146; sql: 62; python: 11; makefile: 8
file content (360 lines) | stat: -rw-r--r-- 12,587 bytes parent folder | download | duplicates (2)
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
package bundler

// This test file contains tests on checking the correctness of BundleFromFile and Bundle.
// We simulate various scenarios for Bundle and funnel the tests through BundleFromFile.
import (
	"encoding/json"
	"testing"
)

// A helper structure that defines a BundleFromFile test case.
type fileTest struct {
	// PEM cert file to be bundled
	cert string
	// PEM private key file to be bundled
	key string
	// Root CA bundle
	caBundleFile string
	// Trust intermediate bundle
	intBundleFile string
	// Additional PEM intermediate certificates to be added into the bundler
	extraIntermediates string
	// Bundler creation function
	bundlerConstructor func(*testing.T) (b *Bundler)
	// Error checking function
	errorCallback func(*testing.T, error)
	// Bundle checking function
	bundleChecking func(*testing.T, *Bundle)
}

/* ========== BundleFromFile Test Setup =============

For each pair of crypto algorithm X and key size Y, a CA chain is constructed:
	Test_root_CA -> inter-L1 -> inter-L2--> cfssl-leaf-ecdsa256
	                                    |-> cfssl-leaf-ecdsa384
	                                    |-> cfssl-leaf-ecdsa521
	                                    |-> cfssl-leaf-rsa2048
	                                    |-> cfssl-leaf-rsa3072
	                                    |-> cfssl-leaf-rsa4096

Test_root_CA is a RSA cert, inter-L1 is RSA 4096 cert, inter-L2 is ecdsa-384 cert.

The max path length is set to be 1 for non-root CAs.
Two inter-* certs are assembled in intermediates.crt

There is also an expired L1 cert, sharing the same CSR with inter-L1. Also the
root CA processes the inter-L2 CSR directly to generate inter-L2-direct cert.
*	Test_root_CA--> inter-L1-expired
	            |-> inter-L2-direct
Using inter-L2-direct as additional intermediate cert should shorten the
bundle chain.
*/
const (
	leafECDSA256    = "../../../../../../bundler/testdata/cfssl-leaf-ecdsa256.pem"
	leafECDSA384    = "../../../../../../bundler/testdata/cfssl-leaf-ecdsa384.pem"
	leafECDSA521    = "../../../../../../bundler/testdata/cfssl-leaf-ecdsa521.pem"
	leafRSA2048     = "../../../../../../bundler/testdata/cfssl-leaf-rsa2048.pem"
	leafRSA3072     = "../../../../../../bundler/testdata/cfssl-leaf-rsa3072.pem"
	leafRSA4096     = "../../../../../../bundler/testdata/cfssl-leaf-rsa4096.pem"
	leafKeyECDSA256 = "../../../../../../bundler/testdata/cfssl-leaf-ecdsa256.key"
	leafKeyECDSA384 = "../../../../../../bundler/testdata/cfssl-leaf-ecdsa384.key"
	leafKeyECDSA521 = "../../../../../../bundler/testdata/cfssl-leaf-ecdsa521.key"
	leafKeyRSA2048  = "../../../../../../bundler/testdata/cfssl-leaf-rsa2048.key"
	leafKeyRSA3072  = "../../../../../../bundler/testdata/cfssl-leaf-rsa3072.key"
	leafKeyRSA4096  = "../../../../../../bundler/testdata/cfssl-leaf-rsa4096.key"
	leafletRSA4096  = "../../../../../../bundler/testdata/cfssl-leaflet-rsa4096.pem"
	interL1         = "../../../../../../bundler/testdata/inter-L1.pem"
	interL1Expired  = "../../../../../../bundler/testdata/inter-L1-expired.pem"
	interL1CSR      = "../../../../../../bundler/testdata/inter-L1.csr"
	interL2         = "../../../../../../bundler/testdata/inter-L2.pem"

	interL2Direct = "../../../../../../bundler/testdata/inter-L2-direct.pem"
	partialBundle = "../../../../../../bundler/testdata/partial-bundle.pem"         // partialBundle is a partial cert chain {leaf-ecds256,  inter-L2}
	rpBundle      = "../../../../../../bundler/testdata/reverse-partial-bundle.pem" // partialBundle is a partial cert chain in the reverse order {inter-L2, leaf-ecdsa256}
	badBundle     = "../../../../../../bundler/testdata/bad-bundle.pem"             // badBundle is a non-verifying partial bundle {leaf-ecdsa256, leaf-ecdsa384}
	interL2CSR    = "../../../../../../bundler/testdata/inter-L2.csr"
	certDSA2048   = "../../../../../../bundler/testdata/dsa2048.pem"
	keyDSA2048    = "../../../../../../bundler/testdata/dsa2048.key"
)

// BundleFromFile test cases.
var fileTests = []fileTest{
	// Input verification
	{
		cert:          "not_such_cert.pem",
		caBundleFile:  testCFSSLRootBundle,
		intBundleFile: testCFSSLIntBundle,
		errorCallback: ExpectErrorMessage(`"code":1001`),
	},
	{
		cert:          emptyPEM,
		caBundleFile:  testCFSSLRootBundle,
		intBundleFile: testCFSSLIntBundle,
		errorCallback: ExpectErrorMessage(`"code":1002`),
	},

	// Normal Keyless bundling for all supported public key types
	{
		cert:           leafECDSA256,
		caBundleFile:   testCFSSLRootBundle,
		intBundleFile:  testCFSSLIntBundle,
		errorCallback:  nil,
		bundleChecking: ExpectBundleLength(3),
	},
	{
		cert:           leafECDSA384,
		caBundleFile:   testCFSSLRootBundle,
		intBundleFile:  testCFSSLIntBundle,
		errorCallback:  nil,
		bundleChecking: ExpectBundleLength(3),
	},
	{
		cert:           leafECDSA521,
		caBundleFile:   testCFSSLRootBundle,
		intBundleFile:  testCFSSLIntBundle,
		errorCallback:  nil,
		bundleChecking: ExpectBundleLength(3),
	},
	{
		cert:           leafRSA2048,
		caBundleFile:   testCFSSLRootBundle,
		intBundleFile:  testCFSSLIntBundle,
		errorCallback:  nil,
		bundleChecking: ExpectBundleLength(3),
	},
	{
		cert:           leafRSA3072,
		caBundleFile:   testCFSSLRootBundle,
		intBundleFile:  testCFSSLIntBundle,
		errorCallback:  nil,
		bundleChecking: ExpectBundleLength(3),
	},
	{
		cert:           leafRSA4096,
		caBundleFile:   testCFSSLRootBundle,
		intBundleFile:  testCFSSLIntBundle,
		errorCallback:  nil,
		bundleChecking: ExpectBundleLength(3),
	},

	// Normal bundling with private key for all supported key types
	{
		cert:           leafECDSA256,
		key:            leafKeyECDSA256,
		caBundleFile:   testCFSSLRootBundle,
		intBundleFile:  testCFSSLIntBundle,
		errorCallback:  nil,
		bundleChecking: ExpectBundleLength(3),
	},
	{
		cert:           leafECDSA384,
		key:            leafKeyECDSA384,
		caBundleFile:   testCFSSLRootBundle,
		intBundleFile:  testCFSSLIntBundle,
		errorCallback:  nil,
		bundleChecking: ExpectBundleLength(3),
	},
	{
		cert:           leafECDSA521,
		key:            leafKeyECDSA521,
		caBundleFile:   testCFSSLRootBundle,
		intBundleFile:  testCFSSLIntBundle,
		errorCallback:  nil,
		bundleChecking: ExpectBundleLength(3),
	},
	{
		cert:           leafRSA2048,
		key:            leafKeyRSA2048,
		caBundleFile:   testCFSSLRootBundle,
		intBundleFile:  testCFSSLIntBundle,
		errorCallback:  nil,
		bundleChecking: ExpectBundleLength(3),
	},
	{
		cert:           leafRSA3072,
		key:            leafKeyRSA3072,
		caBundleFile:   testCFSSLRootBundle,
		intBundleFile:  testCFSSLIntBundle,
		errorCallback:  nil,
		bundleChecking: ExpectBundleLength(3),
	},
	{
		cert:           leafRSA4096,
		key:            leafKeyRSA4096,
		caBundleFile:   testCFSSLRootBundle,
		intBundleFile:  testCFSSLIntBundle,
		errorCallback:  nil,
		bundleChecking: ExpectBundleLength(3),
	},

	// Bundling with errors

	// leaflet cert is signed by a leaf cert which is not included the intermediate bundle.
	// So an UnknownAuthority error is expected.
	{
		cert:          leafletRSA4096,
		caBundleFile:  testCFSSLRootBundle,
		intBundleFile: testCFSSLIntBundle,
		errorCallback: ExpectErrorMessage(`"code":1220`),
	},
	// Expect TooManyIntermediates error because max path length is 1 for
	// inter-L1 but the leaflet cert is 2 CA away from inter-L1.
	{
		cert:               leafletRSA4096,
		extraIntermediates: leafRSA4096,
		caBundleFile:       testCFSSLRootBundle,
		intBundleFile:      testCFSSLIntBundle,
		errorCallback:      ExpectErrorMessage(`"code":1213`),
	},
	// Bundle with expired inter-L1 intermediate cert only, expect error 1211 VerifyFailed:Expired.
	{
		cert:               interL2,
		extraIntermediates: interL1Expired,
		caBundleFile:       testCFSSLRootBundle,
		intBundleFile:      emptyPEM,
		errorCallback:      ExpectErrorMessage(`"code":1211`),
	},

	// Bundle with private key mismatch
	// RSA cert, ECC private key
	{
		cert:          leafRSA4096,
		key:           leafKeyECDSA256,
		caBundleFile:  testCFSSLRootBundle,
		intBundleFile: testCFSSLIntBundle,
		errorCallback: ExpectErrorMessages([]string{`"code":2300,`, `"message":"Private key does not match public key"`}),
	},
	// ECC cert, RSA private key
	{
		cert:          leafECDSA256,
		key:           leafKeyRSA4096,
		caBundleFile:  testCFSSLRootBundle,
		intBundleFile: testCFSSLIntBundle,
		errorCallback: ExpectErrorMessages([]string{`"code":2300,`, `"message":"Private key does not match public key"`}),
	},
	// RSA 2048 cert, RSA 4096  private key
	{
		cert:          leafRSA2048,
		key:           leafKeyRSA4096,
		caBundleFile:  testCFSSLRootBundle,
		intBundleFile: testCFSSLIntBundle,
		errorCallback: ExpectErrorMessages([]string{`"code":2300,`, `"message":"Private key does not match public key"`}),
	},
	// ECDSA 256 cert, ECDSA 384  private key
	{
		cert:          leafECDSA256,
		key:           leafKeyECDSA384,
		caBundleFile:  testCFSSLRootBundle,
		intBundleFile: testCFSSLIntBundle,
		errorCallback: ExpectErrorMessages([]string{`"code":2300,`, `"message":"Private key does not match public key"`}),
	},

	// DSA is NOT supported.
	// Keyless bundling, expect private key error "NotRSAOrECC"
	{
		cert:          certDSA2048,
		caBundleFile:  testCFSSLRootBundle,
		intBundleFile: testCFSSLIntBundle,
		errorCallback: ExpectErrorMessages([]string{`"code":2200,`, `"message":"Private key algorithm is not RSA or ECC"`}),
	},
	// Bundling with DSA private key, expect error "Failed to parse private key"
	{
		cert:          certDSA2048,
		key:           keyDSA2048,
		caBundleFile:  testCFSSLRootBundle,
		intBundleFile: testCFSSLIntBundle,
		errorCallback: ExpectErrorMessages([]string{`"code":2003,`, `"message":"Failed to parse private key"`}),
	},

	// Bundle with partial chain less some intermediates, expected error 1220: UnknownAuthority
	{
		cert:          badBundle,
		caBundleFile:  testCFSSLRootBundle,
		intBundleFile: interL1,
		errorCallback: ExpectErrorMessage(`"code":1220`),
	},

	// Bundle with misplaced key as cert
	{
		cert:          leafKeyECDSA256,
		caBundleFile:  testCFSSLRootBundle,
		intBundleFile: testCFSSLIntBundle,
		errorCallback: ExpectErrorMessages([]string{`"code":1003,`, `"message":"Failed to parse certificate"`}),
	},

	// Bundle with misplaced cert as key
	{
		cert:          leafECDSA256,
		key:           leafECDSA256,
		caBundleFile:  testCFSSLRootBundle,
		intBundleFile: testCFSSLIntBundle,
		errorCallback: ExpectErrorMessages([]string{`"code":2003,`, `"message":"Failed to parse private key"`}),
	},

	// Smart Bundling
	// Bundling with a partial bundle should work the same as bundling the leaf.
	{
		cert:           partialBundle,
		caBundleFile:   testCFSSLRootBundle,
		intBundleFile:  testCFSSLIntBundle,
		errorCallback:  nil,
		bundleChecking: ExpectBundleLength(3),
	},

	// Bundle with a partial bundle such that the intermediate provided in the
	// partial bundle is verify by an intermediate. Yet itself is not in the intermediate
	// pool. In such cases, the bundling should be able to store the new intermediate
	// and return a correct bundle.
	{
		cert:           partialBundle,
		caBundleFile:   testCFSSLRootBundle,
		intBundleFile:  interL1,
		errorCallback:  nil,
		bundleChecking: ExpectBundleLength(3),
	},

	// Bundle with a reverse-ordered partial bundle.
	// Bundler should be able to detect it and return a correct bundle.
	{
		cert:           rpBundle,
		caBundleFile:   testCFSSLRootBundle,
		intBundleFile:  interL1,
		errorCallback:  nil,
		bundleChecking: ExpectBundleLength(3),
	},

	// Bundle with a L2 cert direct signed by root, expect a shorter chain of length 2.
	{
		cert:               leafECDSA256,
		extraIntermediates: interL2Direct,
		caBundleFile:       testCFSSLRootBundle,
		intBundleFile:      testCFSSLIntBundle,
		errorCallback:      nil,
		bundleChecking:     ExpectBundleLength(2),
	},
}

// TestBundleFromFile goes through test cases defined in fileTests. See below for test cases definition and details.
func TestBundleFromFile(t *testing.T) {
	for _, test := range fileTests {
		b := newCustomizedBundlerFromFile(t, test.caBundleFile, test.intBundleFile, test.extraIntermediates)
		bundle, err := b.BundleFromFile(test.cert, test.key, Optimal, "")
		if test.errorCallback != nil {
			test.errorCallback(t, err)
		} else {
			if err != nil {
				t.Fatalf("expected no error. but an error occurred: %v", err)
			}
			if test.bundleChecking != nil {
				test.bundleChecking(t, bundle)
			}
		}

		if bundle != nil {
			bundle.Cert = nil
			if _, err = json.Marshal(bundle); err == nil {
				t.Fatal("bundle should fail with no cert")
			}
		}
	}
}