-
Notifications
You must be signed in to change notification settings - Fork 2
/
responder_test.go
420 lines (372 loc) · 10.6 KB
/
responder_test.go
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
package dyocsp
import (
"math/big"
"os"
"reflect"
"testing"
"time"
"github.com/yuxki/dyocsp/pkg/cache"
"github.com/yuxki/dyocsp/pkg/db"
"golang.org/x/crypto/ocsp"
)
func cnvSerialStr2BigInt(serialStr string) *big.Int {
bigInt := new(big.Int)
serial, _ := bigInt.SetString(serialStr, db.SerialBase)
return serial
}
func TestBuildResponder(t *testing.T) {
t.Parallel()
data := []struct {
testCase string
// test data
rCertFile string
rCertSerialStr string
rPrivKeyFile string
issuerCertFile string
// want
issuerCertSerialStr string
responderType AuthorizedType
errMsg string
}{
{
"format: openssl x509 certificate, rsa pkcs8 key",
"sub-ocsp-rsa.crt",
"8ca7b3fe5d7f007673c18ccc6a1f818085cdc5f5",
"sub-ocsp-rsa-pkcs8.key",
"sub-ca-rsa.crt",
"1ca7b3fe5d7f007673c18ccc6a1f818085cdc5f6",
Delegation,
"",
},
{
"format: openssl x509 certificate, ec pkcs8 key",
"sub-ocsp-ecparam.crt",
"8ca7b3fe5d7f007673c18ccc6a1f818085cdc5f6",
"sub-ocsp-ecparam-pkcs8.key",
"sub-ca-rsa.crt",
"1ca7b3fe5d7f007673c18ccc6a1f818085cdc5f6",
Delegation,
"",
},
{
"RFC 6960,4.2.2.2: sign the OCSP responses itself (OK)",
"sub-ca-rsa.crt",
"1ca7b3fe5d7f007673c18ccc6a1f818085cdc5f6",
"sub-ca-rsa-pkcs8.key",
"root-ca-rsa.crt",
"1ca7b3fe5d7f007673c18ccc6a1f818085cdc5f5",
Itself,
"",
},
{
"RFC 6960,4.2.2.2: explicitly designate this authority to another entity (OK)",
"sub-ocsp-ecparam.crt",
"8ca7b3fe5d7f007673c18ccc6a1f818085cdc5f6",
"sub-ocsp-ecparam-pkcs8.key",
"sub-ca-rsa.crt",
"1ca7b3fe5d7f007673c18ccc6a1f818085cdc5f6",
Delegation,
"",
},
{
"RFC 6960,4.2.2.2: explicitly designate this authority to another entity " +
"(NG: Not includes a value of id-kp-OCSPSigning)",
"sub-no-ocsp-rsa.crt",
"1f8acd3265e5ba098dec495eece41c11ba093463",
"sub-no-ocsp-rsa-pkcs8.key",
"sub-ca-rsa.crt",
"1ca7b3fe5d7f007673c18ccc6a1f818085cdc5f6",
Delegation,
"invalid responder certificate: authorized reponder certificate does not include a value of id-kp-OCSPSigning.",
},
{
"RFC 6960,4.2.2.2: explicitly designate this authority to another entity " +
"(NG: Not Is the certificate of the CA that issued the certificate)",
"sub-ocsp-ecparam.crt",
"8ca7b3fe5d7f007673c18ccc6a1f818085cdc5f6",
"sub-ocsp-ecparam-pkcs8.key",
"root-ca-rsa.crt",
"1ca7b3fe5d7f007673c18ccc6a1f818085cdc5f5",
Delegation,
"invalid issuer certificate: keyIdentifier is not matched the responder certificate.",
},
{
"configuration: Not After date is past",
"sub-expired-ocsp-rsa.crt",
"8ca7b3fe5d7f007673c18ccc6a1f818085cdc5f7",
"sub-expired-ocsp-rsa-pkcs8.key",
"sub-ca-rsa.crt",
"1ca7b3fe5d7f007673c18ccc6a1f818085cdc5f6",
Delegation,
"invalid responder certificate: date of Not After is past.",
},
{
"configuration: Not Before date is future",
"sub-future-ocsp-rsa.crt",
"8ca7b3fe5d7f007673c18ccc6a1f818085cdc5f8",
"sub-future-ocsp-rsa-pkcs8.key",
"sub-ca-rsa.crt",
"1ca7b3fe5d7f007673c18ccc6a1f818085cdc5f6",
Delegation,
"invalid responder certificate: date of Not Before is future.",
},
{
"key pair: Bad pair of public key and private key",
"sub-ocsp-rsa.crt",
"8ca7b3fe5d7f007673c18ccc6a1f818085cdc5f5",
"sub-ocsp-ecparam-pkcs8.key",
"sub-ca-rsa.crt",
"1ca7b3fe5d7f007673c18ccc6a1f818085cdc5f6",
Delegation,
"invalid responder certificate: algorithm of private key does not matche the public key.",
},
}
for _, d := range data {
d := d
t.Run(d.testCase, func(t *testing.T) {
t.Parallel()
rCertFilePem, err := os.ReadFile("testdata/" + d.rCertFile)
if err != nil {
t.Fatal(err)
}
rPrivKeyPem, err := os.ReadFile("testdata/" + d.rPrivKeyFile)
if err != nil {
t.Fatal(err)
}
issuerCertPem, err := os.ReadFile("testdata/" + d.issuerCertFile)
if err != nil {
t.Fatal(err)
}
responder, err := BuildResponder(
rCertFilePem, rPrivKeyPem, issuerCertPem, time.Date(2024, 8, 9, 12, 30, 0, 0, time.UTC),
)
if d.errMsg == "" {
if err != nil {
t.Fatal(err)
}
} else {
if err.Error() != d.errMsg {
t.Fatalf("Expected '%#v' error msg but got: %#v", d.errMsg, err.Error())
}
return
}
if responder.rCert.SerialNumber.Text(db.SerialBase) !=
cnvSerialStr2BigInt(d.rCertSerialStr).Text(db.SerialBase) {
t.Errorf("Type of rCert is not x509.Certificate: %#v", reflect.TypeOf(responder.rCert))
}
if responder.rPrivKey == nil {
t.Errorf("Issuer Private key is not set on Responder")
}
if responder.issuerCert.SerialNumber.Text(db.SerialBase) !=
cnvSerialStr2BigInt(d.issuerCertSerialStr).Text(db.SerialBase) {
t.Errorf("Type of rCert is not x509.Certificate: %#v", reflect.TypeOf(responder.rCert))
}
if responder.AuthType != d.responderType {
t.Fatalf("Expected ResponderType '%#v' but got: %#v", d.responderType, responder.AuthType)
}
})
}
}
func TestBuildResponder_IssuerKeyNameHashes(t *testing.T) {
t.Parallel()
data := []struct {
testCase string
// test data
rCertFile string
rPrivKeyFile string
issuerCertFile string
// want
issuerKeyHashSHA1 []byte
issuerNameHashSHA1 []byte
}{
{
"issuer name & key hashs: rsa",
"sub-ocsp-rsa.crt",
"sub-ocsp-rsa-pkcs8.key",
"sub-ca-rsa.crt",
[]byte{0xe9, 0x61, 0xfd, 0x3d, 0x66, 0x22, 0xec, 0x5e, 0x5a, 0xe7, 0xc9, 0x52, 0x61, 0xc4, 0xaf, 0x8c, 0x6d, 0x66, 0x70, 0x5d},
[]byte{19, 227, 235, 142, 71, 110, 197, 189, 239, 55, 92, 120, 139, 166, 80, 251, 251, 72, 70, 129},
},
{
"issuer name & key hashs: ecparam",
"self-ecparam.crt",
"self-ecparam.key",
"self-ecparam.crt",
[]byte{0xeb, 0xb9, 0xa8, 0x8d, 0x29, 0xf4, 0x52, 0x57, 0xde, 0x5d, 0x6, 0xea, 0x3d, 0xe4, 0x58, 0x6b, 0x31, 0xa9, 0xf5, 0x56},
[]byte{43, 136, 163, 228, 65, 65, 77, 120, 171, 90, 36, 51, 134, 91, 254, 179, 168, 144, 173, 161},
},
}
for _, d := range data {
d := d
t.Run(d.testCase, func(t *testing.T) {
t.Parallel()
rCertFilePem, err := os.ReadFile("testdata/" + d.rCertFile)
if err != nil {
t.Fatal(err)
}
rPrivKeyPem, err := os.ReadFile("testdata/" + d.rPrivKeyFile)
if err != nil {
t.Fatal(err)
}
issuerCertPem, err := os.ReadFile("testdata/" + d.issuerCertFile)
if err != nil {
t.Fatal(err)
}
responder, err := BuildResponder(
rCertFilePem, rPrivKeyPem, issuerCertPem, time.Date(2024, 8, 9, 12, 30, 0, 0, time.UTC),
)
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(d.issuerKeyHashSHA1, responder.IssuerKeyHash.SHA1) {
t.Errorf(
"Expected IssuerKeyHash.SHA1 %#v but got : %#v",
d.issuerKeyHashSHA1,
responder.IssuerKeyHash.SHA1,
)
}
if !reflect.DeepEqual(d.issuerNameHashSHA1, responder.IssuerNameHash.SHA1) {
t.Errorf(
"Expected IssuerNameHash.SHA1 %#v but got : %#v",
d.issuerNameHashSHA1,
responder.IssuerNameHash.SHA1,
)
}
})
}
}
func testCreateDirectResponder(t *testing.T) *Responder {
t.Helper()
rCertFilePem, err := os.ReadFile("testdata/sub-ca-rsa.crt")
if err != nil {
t.Fatal(err)
}
rPrivKeyPem, err := os.ReadFile("testdata/sub-ca-rsa-pkcs8.key")
if err != nil {
t.Fatal(err)
}
issuerCertPem, err := os.ReadFile("testdata/root-ca-rsa.crt")
if err != nil {
t.Fatal(err)
}
responder, err := BuildResponder(
rCertFilePem, rPrivKeyPem, issuerCertPem, time.Date(2024, 8, 9, 12, 30, 0, 0, time.UTC),
)
if err != nil {
t.Fatal(err)
}
return responder
}
func testCreateDelegatedResponder(t *testing.T) *Responder {
t.Helper()
rCertFilePem, err := os.ReadFile("testdata/sub-ocsp-rsa.crt")
if err != nil {
t.Fatal(err)
}
rPrivKeyPem, err := os.ReadFile("testdata/sub-ocsp-rsa-pkcs8.key")
if err != nil {
t.Fatal(err)
}
issuerCertPem, err := os.ReadFile("testdata/sub-ca-rsa.crt")
if err != nil {
t.Fatal(err)
}
responder, err := BuildResponder(
rCertFilePem, rPrivKeyPem, issuerCertPem, time.Date(2024, 8, 9, 12, 30, 0, 0, time.UTC),
)
if err != nil {
t.Fatal(err)
}
return responder
}
func TestResponder_SignCacheResponse(t *testing.T) {
t.Parallel()
responder := testCreateDelegatedResponder(t)
serial, ok := new(big.Int).SetString("72344BF34067BBA31EF44587CBFB16631332CD23", db.SerialBase)
if !ok {
t.Fatal("String could not be *big.Int.")
}
data := []struct {
testCase string
// test data
responder *Responder
entry db.CertificateEntry
// want
errMsg string
}{
{
"OK: status good",
responder,
db.CertificateEntry{
Ca: "ca",
Serial: serial,
RevType: "V",
ExpDate: time.Date(2033, 8, 9, 12, 30, 0, 0, time.UTC),
RevDate: time.Time{},
CRLReason: db.NotRevoked,
Errors: nil,
},
"",
},
{
"OK: status revoked",
responder,
db.CertificateEntry{
Ca: "ca",
Serial: serial,
RevType: "R",
ExpDate: time.Date(2033, 8, 9, 12, 30, 0, 0, time.UTC),
RevDate: time.Date(2033, 8, 9, 11, 30, 0, 0, time.UTC),
CRLReason: db.Unspecified,
Errors: nil,
},
"",
},
}
for _, d := range data {
d := d
t.Run(d.testCase, func(t *testing.T) {
t.Parallel()
resCache, err := cache.CreatePreSignedResponseCache(
d.entry, time.Date(2023, 8, 9, 12, 30, 0, 0, time.UTC), time.Second*120,
)
if err != nil {
t.Fatal(err)
}
resCache, err = responder.SignCacheResponse(resCache)
if d.errMsg == "" {
if err != nil {
t.Fatal(err)
}
} else {
if err.Error() != d.errMsg {
t.Fatalf("Expected '%#v' error msg but got: %#v", d.errMsg, err.Error())
}
return
}
res, err := ocsp.ParseResponse(resCache.Response(), responder.rCert)
if err != nil {
t.Fatal(err)
}
if len(resCache.SHA1Hash()) != 20 {
t.Errorf(
"resCache.sha1Hash is not may not be a SHA1 hash of the OCSPResponse: %#v", resCache.SHA1Hash())
}
if res.Status != resCache.Template().Status {
t.Errorf("State %#v is changed: %#v", resCache.Template().Status, res.Status)
}
if !reflect.DeepEqual(res.SerialNumber, resCache.Template().SerialNumber) {
t.Errorf("State %#v is changed: %#v", resCache.Template().SerialNumber, res.SerialNumber)
}
if !reflect.DeepEqual(res.ThisUpdate, resCache.Template().ThisUpdate) {
t.Errorf("State %#v is changed: %#v", resCache.Template().ThisUpdate, res.ThisUpdate)
}
if !reflect.DeepEqual(res.NextUpdate, resCache.Template().NextUpdate) {
t.Errorf("State %#v is changed: %#v", resCache.Template().NextUpdate, res.NextUpdate)
}
if !reflect.DeepEqual(res.RevocationReason, resCache.Template().RevocationReason) {
t.Errorf("State %#v is changed: %#v", resCache.Template().RevocationReason, res.RevocationReason)
}
})
}
}