@@ -162,8 +162,8 @@ let invalid_keys_tests =
162
162
)
163
163
invalid_private_keys
164
164
165
- let test_valid_cert ~ kind cert time pkey =
166
- match validate_certificate kind cert time pkey with
165
+ let test_valid_leaf_cert pem_leaf time pkey () =
166
+ match validate_pem_chain ~pem_leaf ~pem_chain: None time pkey with
167
167
| Ok _ ->
168
168
()
169
169
| Error (`Msg (_ , msg )) ->
@@ -173,8 +173,8 @@ let test_valid_cert ~kind cert time pkey =
173
173
msg
174
174
)
175
175
176
- let test_invalid_cert ~ kind cert time pkey error reason =
177
- match validate_certificate kind cert time pkey with
176
+ let test_invalid_cert pem_leaf time pkey error reason =
177
+ match validate_pem_chain ~pem_leaf ~pem_chain: None time pkey with
178
178
| Ok _ ->
179
179
Alcotest. fail " Invalid certificate was validated without errors"
180
180
| Error (`Msg msg ) ->
@@ -203,9 +203,6 @@ let sign_leaf_cert host_name digest pkey_leaf =
203
203
>> | Cstruct. to_string
204
204
205
205
let valid_leaf_cert_tests =
206
- let test_valid_leaf_cert cert time pkey () =
207
- test_valid_cert ~kind: Leaf cert time pkey
208
- in
209
206
List. map
210
207
(fun (name , pkey_leaf_name , time , digest ) ->
211
208
let cert_test =
@@ -222,15 +219,15 @@ let test_corrupt_leaf_cert (cert_name, pkey_name, time, error, reason) =
222
219
let time = time_of_rfc3339 time in
223
220
let test_cert =
224
221
load_pkcs8 pkey_name >> | fun pkey ->
225
- let test () = test_invalid_cert ~kind: Leaf cert time pkey error reason in
222
+ let test () = test_invalid_cert cert time pkey error reason in
226
223
test
227
224
in
228
225
(" Validation of a corrupted certificate" , `Quick , test_cert)
229
226
230
227
let test_invalid_leaf_cert
231
228
(name , pkey_leaf_name , pkey_expected_name , time , digest , error , reason ) =
232
229
let test_invalid_leaf_cert cert time pkey error reason () =
233
- test_invalid_cert ~kind: Leaf cert time pkey error reason
230
+ test_invalid_cert cert time pkey error reason
234
231
in
235
232
let test_cert =
236
233
load_pkcs8 pkey_leaf_name >> = fun pkey_leaf ->
@@ -245,17 +242,30 @@ let invalid_leaf_cert_tests =
245
242
List. map test_corrupt_leaf_cert corrupt_certificates
246
243
@ List. map test_invalid_leaf_cert invalid_leaf_certificates
247
244
248
- let test_valid_cert_chain chain time pkey () =
249
- test_valid_cert ~kind: Chain chain time pkey
245
+ let test_valid_cert_chain ~pem_leaf ~pem_chain time pkey () =
246
+ match validate_pem_chain ~pem_leaf ~pem_chain: (Some pem_chain) time pkey with
247
+ | Ok _ ->
248
+ ()
249
+ | Error (`Msg (_ , msg )) ->
250
+ Alcotest. fail
251
+ (Format. asprintf " Valid certificate chain could not be validated: %a"
252
+ Fmt. (Dump. list string )
253
+ msg
254
+ )
250
255
251
- let test_invalid_cert_chain cert time pkey error reason () =
252
- test_invalid_cert ~kind: Chain cert time pkey error reason
256
+ let test_invalid_cert_chain pem_leaf pem_chain time pkey error reason () =
257
+ match validate_pem_chain ~pem_leaf ~pem_chain: (Some pem_chain) time pkey with
258
+ | Ok _ ->
259
+ Alcotest. fail " Invalid certificate chain was validated without errors"
260
+ | Error (`Msg msg ) ->
261
+ Alcotest. (check @@ pair string @@ list string )
262
+ " Error must match" (error, reason) msg
253
263
254
264
let valid_chain_cert_tests =
255
265
let time = time_of_rfc3339 " 2020-02-01T00:00:00Z" in
256
266
let test_cert =
257
267
load_pkcs8 " pkey_rsa_4096" >> = fun pkey_root ->
258
- let pkey , chain =
268
+ let pkey_leaf , chain =
259
269
List. fold_left
260
270
(fun (pkey_sign , chain_result ) pkey ->
261
271
let result =
@@ -267,8 +277,10 @@ let valid_chain_cert_tests =
267
277
)
268
278
(pkey_root, Ok [] ) key_chain
269
279
in
280
+ sign_leaf_cert host_name `SHA256 pkey_leaf >> = fun pem_leaf ->
270
281
chain >> | X509.Certificate. encode_pem_multiple >> | Cstruct. to_string
271
- >> | fun chain -> test_valid_cert_chain chain time pkey
282
+ >> | fun pem_chain ->
283
+ test_valid_cert_chain ~pem_leaf ~pem_chain time pkey_leaf
272
284
in
273
285
[(" Validation of a supported certificate chain" , `Quick , test_cert)]
274
286
@@ -277,8 +289,11 @@ let invalid_chain_cert_tests =
277
289
(fun (chain_name , pkey_name , time , error , reason ) ->
278
290
let chain = load_test_data chain_name in
279
291
let test_cert =
280
- load_pkcs8 pkey_name >> | fun pkey ->
281
- test_invalid_cert_chain chain (time_of_rfc3339 time) pkey error reason
292
+ (* Need to load a valid key and leaf cert *)
293
+ load_pkcs8 pkey_name >> = fun pkey ->
294
+ sign_leaf_cert host_name `SHA256 pkey >> | fun cert ->
295
+ test_invalid_cert_chain cert chain (time_of_rfc3339 time) pkey error
296
+ reason
282
297
in
283
298
(" Validation of an unsupported certificate chain" , `Quick , test_cert)
284
299
)
0 commit comments