Implement Provider side Key Management for X25519 and X448
[openssl.git] / test / cmp_protect_test.c
1 /*
2  * Copyright 2007-2019 The OpenSSL Project Authors. All Rights Reserved.
3  * Copyright Nokia 2007-2019
4  * Copyright Siemens AG 2015-2019
5  *
6  * Licensed under the Apache License 2.0 (the "License").  You may not use
7  * this file except in compliance with the License.  You can obtain a copy
8  * in the file LICENSE in the source distribution or at
9  * https://www.openssl.org/source/license.html
10  */
11
12 #include "cmp_testlib.h"
13
14 static const char *ir_protected_f;
15 static const char *ir_unprotected_f;
16 static const char *ip_PBM_f;
17
18 typedef struct test_fixture {
19     const char *test_case_name;
20     OSSL_CMP_CTX *cmp_ctx;
21     /* for protection tests */
22     OSSL_CMP_MSG *msg;
23     OSSL_CMP_PKISI *si;      /* for error and response messages */
24     ASN1_OCTET_STRING *secret;
25     EVP_PKEY *privkey;
26     EVP_PKEY *pubkey;
27     unsigned char *mem;
28     int memlen;
29     X509 *cert;
30     STACK_OF(X509) *certs;
31     STACK_OF(X509) *chain;
32     int callback_arg;
33     int expected;
34 } CMP_PROTECT_TEST_FIXTURE;
35
36 static void tear_down(CMP_PROTECT_TEST_FIXTURE *fixture)
37 {
38     OSSL_CMP_CTX_free(fixture->cmp_ctx);
39     OSSL_CMP_MSG_free(fixture->msg);
40     ASN1_OCTET_STRING_free(fixture->secret);
41     OSSL_CMP_PKISI_free(fixture->si);
42
43     OPENSSL_free(fixture->mem);
44     sk_X509_free(fixture->certs);
45     sk_X509_free(fixture->chain);
46
47     OPENSSL_free(fixture);
48 }
49
50 static CMP_PROTECT_TEST_FIXTURE *set_up(const char *const test_case_name)
51 {
52     CMP_PROTECT_TEST_FIXTURE *fixture;
53
54     if (!TEST_ptr(fixture = OPENSSL_zalloc(sizeof(*fixture))))
55         return NULL;
56     fixture->test_case_name = test_case_name;
57     if (!TEST_ptr(fixture->cmp_ctx = OSSL_CMP_CTX_new())) {
58         tear_down(fixture);
59         return NULL;
60     }
61     return fixture;
62 }
63
64 static EVP_PKEY *loadedprivkey = NULL;
65 static EVP_PKEY *loadedpubkey = NULL;
66 static EVP_PKEY *loadedkey = NULL;
67 static X509 *cert = NULL;
68 static unsigned char rand_data[OSSL_CMP_TRANSACTIONID_LENGTH];
69 static OSSL_CMP_MSG *ir_unprotected, *ir_protected;
70 static X509 *endentity1 = NULL, *endentity2 = NULL,
71     *root = NULL, *intermediate = NULL;
72
73 static int execute_calc_protection_fails_test(CMP_PROTECT_TEST_FIXTURE *fixture)
74 {
75     ASN1_BIT_STRING *protection =
76         ossl_cmp_calc_protection(fixture->msg, fixture->secret,
77                                  fixture->privkey);
78     int res = TEST_ptr_null(protection);
79
80     ASN1_BIT_STRING_free(protection);
81     return res;
82 }
83
84 static int execute_calc_protection_pbmac_test(CMP_PROTECT_TEST_FIXTURE *fixture)
85 {
86     ASN1_BIT_STRING *protection =
87         ossl_cmp_calc_protection(fixture->msg, fixture->secret, NULL);
88     int res = TEST_ptr(protection)
89         && TEST_true(ASN1_STRING_cmp(protection, fixture->msg->protection) == 0);
90
91     ASN1_BIT_STRING_free(protection);
92     return res;
93 }
94
95 /*
96  * This function works similarly to parts of CMP_verify_signature in cmp_vfy.c,
97  * but without the need for a OSSL_CMP_CTX or a X509 certificate
98  */
99 static int verify_signature(OSSL_CMP_MSG *msg,
100                             ASN1_BIT_STRING *protection,
101                             EVP_PKEY *pkey, int digest_nid)
102 {
103     CMP_PROTECTEDPART prot_part;
104     unsigned char *prot_part_der = NULL;
105     int len;
106     EVP_MD_CTX *ctx = NULL;
107     const EVP_MD *digest = EVP_get_digestbynid(digest_nid);
108     int res;
109
110     prot_part.header = OSSL_CMP_MSG_get0_header(msg);
111     prot_part.body = msg->body;
112     res =
113         TEST_int_ge(len = i2d_CMP_PROTECTEDPART(&prot_part, &prot_part_der), 0)
114         && TEST_ptr(ctx = EVP_MD_CTX_new())
115         && TEST_true(EVP_DigestVerifyInit(ctx, NULL, digest, NULL, pkey))
116         && TEST_int_eq(EVP_DigestVerify(ctx, protection->data,
117                                         protection->length,
118                                         prot_part_der, len), 1);
119     /* cleanup */
120     EVP_MD_CTX_free(ctx);
121     OPENSSL_free(prot_part_der);
122     return res;
123 }
124
125 /* Calls OSSL_CMP_calc_protection and compares and verifies signature */
126 static int execute_calc_protection_signature_test(CMP_PROTECT_TEST_FIXTURE *
127                                                   fixture)
128 {
129     ASN1_BIT_STRING *protection =
130         ossl_cmp_calc_protection(fixture->msg, NULL, fixture->privkey);
131     int ret = (TEST_ptr(protection)
132                    && TEST_true(ASN1_STRING_cmp(protection,
133                                                 fixture->msg->protection) == 0)
134                    && TEST_true(verify_signature(fixture->msg, protection,
135                                                  fixture->pubkey,
136                                                  fixture->cmp_ctx->digest)));
137
138     ASN1_BIT_STRING_free(protection);
139     return ret;
140 }
141
142 static int test_cmp_calc_protection_no_key_no_secret(void)
143 {
144     SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up);
145     if (!TEST_ptr(fixture->msg = load_pkimsg(ir_unprotected_f))
146             || !TEST_ptr(fixture->msg->header->protectionAlg =
147                          X509_ALGOR_new() /* no specific alg needed here */)) {
148         tear_down(fixture);
149         fixture = NULL;
150     }
151
152     EXECUTE_TEST(execute_calc_protection_fails_test, tear_down);
153     return result;
154 }
155
156 static int test_cmp_calc_protection_pkey(void)
157 {
158     SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up);
159     fixture->pubkey = loadedpubkey;
160     fixture->privkey = loadedprivkey;
161     if (!TEST_ptr(fixture->msg = load_pkimsg(ir_protected_f))) {
162         tear_down(fixture);
163         fixture = NULL;
164     }
165     EXECUTE_TEST(execute_calc_protection_signature_test, tear_down);
166     return result;
167 }
168
169 static int test_cmp_calc_protection_pbmac(void)
170 {
171     unsigned char sec_insta[] = { 'i', 'n', 's', 't', 'a' };
172
173     SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up);
174     if (!TEST_ptr(fixture->secret = ASN1_OCTET_STRING_new())
175             || !TEST_true(ASN1_OCTET_STRING_set
176                           (fixture->secret, sec_insta, sizeof(sec_insta)))
177             || !TEST_ptr(fixture->msg = load_pkimsg(ip_PBM_f))) {
178         tear_down(fixture);
179         fixture = NULL;
180     }
181     EXECUTE_TEST(execute_calc_protection_pbmac_test, tear_down);
182     return result;
183 }
184 static int execute_MSG_protect_test(CMP_PROTECT_TEST_FIXTURE *fixture)
185 {
186     return TEST_int_eq(fixture->expected,
187                        ossl_cmp_msg_protect(fixture->cmp_ctx, fixture->msg));
188 }
189
190 #define SET_OPT_UNPROTECTED_SEND(ctx, val) \
191     OSSL_CMP_CTX_set_option((ctx), OSSL_CMP_OPT_UNPROTECTED_SEND, (val))
192 static int test_MSG_protect_unprotected_request(void)
193 {
194     SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up);
195
196     fixture->expected = 1;
197     if (!TEST_ptr(fixture->msg = OSSL_CMP_MSG_dup(ir_unprotected))
198             || !TEST_true(SET_OPT_UNPROTECTED_SEND(fixture->cmp_ctx, 1))) {
199         tear_down(fixture);
200         fixture = NULL;
201     }
202     EXECUTE_TEST(execute_MSG_protect_test, tear_down);
203     return result;
204 }
205
206 static int test_MSG_protect_with_msg_sig_alg_protection_plus_rsa_key(void)
207 {
208     const size_t size = sizeof(rand_data) / 2;
209
210     SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up);
211     fixture->expected = 1;
212
213     if (!TEST_ptr(fixture->msg =
214                   OSSL_CMP_MSG_dup(ir_unprotected))
215             || !TEST_true(SET_OPT_UNPROTECTED_SEND(fixture->cmp_ctx, 0))
216             /*
217              * Use half of the 16 bytes of random input
218              * for each reference and secret value
219              */
220             || !TEST_true(OSSL_CMP_CTX_set1_referenceValue(fixture->cmp_ctx,
221                                                            rand_data, size))
222             || !TEST_true(OSSL_CMP_CTX_set1_secretValue(fixture->cmp_ctx,
223                                                         rand_data + size,
224                                                         size))) {
225         tear_down(fixture);
226         fixture = NULL;
227     }
228     EXECUTE_TEST(execute_MSG_protect_test, tear_down);
229     return result;
230 }
231
232 static int test_MSG_protect_with_certificate_and_key(void)
233 {
234     SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up);
235     fixture->expected = 1;
236
237     if (!TEST_ptr(fixture->msg =
238                   OSSL_CMP_MSG_dup(ir_unprotected))
239             || !TEST_true(SET_OPT_UNPROTECTED_SEND(fixture->cmp_ctx, 0))
240             || !TEST_true(OSSL_CMP_CTX_set1_pkey(fixture->cmp_ctx, loadedkey))
241             || !TEST_true(OSSL_CMP_CTX_set1_clCert(fixture->cmp_ctx, cert))) {
242         tear_down(fixture);
243         fixture = NULL;
244     }
245     EXECUTE_TEST(execute_MSG_protect_test, tear_down);
246     return result;
247 }
248
249 static int test_MSG_protect_certificate_based_without_cert(void)
250 {
251     SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up);
252     OSSL_CMP_CTX *ctx = fixture->cmp_ctx;
253
254     fixture->expected = 0;
255     if (!TEST_ptr(fixture->msg =
256                   OSSL_CMP_MSG_dup(ir_unprotected))
257             || !TEST_true(SET_OPT_UNPROTECTED_SEND(ctx, 0))
258             || !TEST_true(OSSL_CMP_CTX_set0_newPkey(ctx, 1, loadedkey))) {
259         tear_down(fixture);
260         fixture = NULL;
261     }
262     EVP_PKEY_up_ref(loadedkey);
263     EXECUTE_TEST(execute_MSG_protect_test, tear_down);
264     return result;
265 }
266
267 static int test_MSG_protect_no_key_no_secret(void)
268 {
269     SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up);
270     fixture->expected = 0;
271     if (!TEST_ptr(fixture->msg = OSSL_CMP_MSG_dup(ir_unprotected))
272             || !TEST_true(SET_OPT_UNPROTECTED_SEND(fixture->cmp_ctx, 0))) {
273         tear_down(fixture);
274         fixture = NULL;
275     }
276     EXECUTE_TEST(execute_MSG_protect_test, tear_down);
277     return result;
278 }
279
280 static int execute_MSG_add_extraCerts_test(CMP_PROTECT_TEST_FIXTURE *fixture)
281 {
282     return TEST_true(ossl_cmp_msg_add_extraCerts(fixture->cmp_ctx,
283                                                  fixture->msg));
284 }
285
286 static int test_MSG_add_extraCerts(void)
287 {
288     SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up);
289     if (!TEST_ptr(fixture->msg = OSSL_CMP_MSG_dup(ir_protected))) {
290         tear_down(fixture);
291         fixture = NULL;
292     }
293     EXECUTE_TEST(execute_MSG_add_extraCerts_test, tear_down);
294     return result;
295 }
296
297 #ifndef OPENSSL_NO_EC
298 /* The cert chain tests use EC certs so we skip them in no-ec builds */
299 static int execute_cmp_build_cert_chain_test(CMP_PROTECT_TEST_FIXTURE *fixture)
300 {
301     STACK_OF(X509) *result = NULL;
302     int ret = 0;
303
304     if (TEST_ptr(result = ossl_cmp_build_cert_chain(fixture->certs,
305                                                     fixture->cert))) {
306         /* Check whether chain built is equal to the expected one */
307         ret = TEST_int_eq(0, STACK_OF_X509_cmp(result, fixture->chain));
308         sk_X509_pop_free(result, X509_free);
309     }
310     return ret;
311 }
312
313 static int test_cmp_build_cert_chain(void)
314 {
315     SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up);
316     fixture->cert = endentity2;
317     if (!TEST_ptr(fixture->certs = sk_X509_new_null())
318             || !TEST_ptr(fixture->chain = sk_X509_new_null())
319             || !TEST_true(sk_X509_push(fixture->certs, endentity1))
320             || !TEST_true(sk_X509_push(fixture->certs, root))
321             || !TEST_true(sk_X509_push(fixture->certs, intermediate))
322             || !TEST_true(sk_X509_push(fixture->chain, endentity2))
323             || !TEST_true(sk_X509_push(fixture->chain, intermediate))) {
324         tear_down(fixture);
325         fixture = NULL;
326     }
327     EXECUTE_TEST(execute_cmp_build_cert_chain_test, tear_down);
328     return result;
329 }
330
331 static int test_cmp_build_cert_chain_missing_intermediate(void)
332 {
333     SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up);
334     fixture->cert = endentity2;
335     if (!TEST_ptr(fixture->certs = sk_X509_new_null())
336             || !TEST_ptr(fixture->chain = sk_X509_new_null())
337             || !TEST_true(sk_X509_push(fixture->certs, endentity1))
338             || !TEST_true(sk_X509_push(fixture->certs, root))
339             || !TEST_true(sk_X509_push(fixture->chain, endentity2))) {
340         tear_down(fixture);
341         fixture = NULL;
342     }
343     EXECUTE_TEST(execute_cmp_build_cert_chain_test, tear_down);
344     return result;
345 }
346
347 static int test_cmp_build_cert_chain_missing_root(void)
348 {
349     SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up);
350     fixture->cert = endentity2;
351     if (!TEST_ptr(fixture->certs = sk_X509_new_null())
352             || !TEST_ptr(fixture->chain = sk_X509_new_null())
353             || !TEST_true(sk_X509_push(fixture->certs, endentity1))
354             || !TEST_true(sk_X509_push(fixture->certs, intermediate))
355             || !TEST_true(sk_X509_push(fixture->chain, endentity2))
356             || !TEST_true(sk_X509_push(fixture->chain, intermediate))) {
357         tear_down(fixture);
358         fixture = NULL;
359     }
360     EXECUTE_TEST(execute_cmp_build_cert_chain_test, tear_down);
361     return result;
362 }
363
364 static int test_cmp_build_cert_chain_no_certs(void)
365 {
366     SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up);
367     fixture->cert = endentity2;
368     if (!TEST_ptr(fixture->certs = sk_X509_new_null())
369             || !TEST_ptr(fixture->chain = sk_X509_new_null())
370             || !TEST_true(sk_X509_push(fixture->chain, endentity2))) {
371         tear_down(fixture);
372         fixture = NULL;
373     }
374     EXECUTE_TEST(execute_cmp_build_cert_chain_test, tear_down);
375     return result;
376 }
377 #endif /* OPENSSL_NO_EC */
378
379 static int execute_X509_STORE_test(CMP_PROTECT_TEST_FIXTURE *fixture)
380 {
381     X509_STORE *store = X509_STORE_new();
382     STACK_OF(X509) *sk = NULL;
383     int res = 0;
384
385     if (!TEST_true(ossl_cmp_X509_STORE_add1_certs(store,
386                                                   fixture->certs,
387                                                   fixture->callback_arg)))
388         goto err;
389     sk = ossl_cmp_X509_STORE_get1_certs(store);
390     if (!TEST_int_eq(0, STACK_OF_X509_cmp(sk, fixture->chain)))
391         goto err;
392     res = 1;
393  err:
394     X509_STORE_free(store);
395     sk_X509_pop_free(sk, X509_free);
396     return res;
397
398 }
399
400 static int test_X509_STORE(void)
401 {
402     SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up);
403     fixture->callback_arg = 0;  /* self-signed allowed */
404     if (!TEST_ptr(fixture->certs = sk_X509_new_null())
405             || !sk_X509_push(fixture->certs, endentity1)
406             || !sk_X509_push(fixture->certs, endentity2)
407             || !sk_X509_push(fixture->certs, root)
408             || !sk_X509_push(fixture->certs, intermediate)
409             || !TEST_ptr(fixture->chain = sk_X509_dup(fixture->certs))) {
410         tear_down(fixture);
411         fixture = NULL;
412     }
413     EXECUTE_TEST(execute_X509_STORE_test, tear_down);
414     return result;
415 }
416
417 static int test_X509_STORE_only_self_signed(void)
418 {
419     SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up);
420     fixture->certs = sk_X509_new_null();
421     fixture->chain = sk_X509_new_null();
422     fixture->callback_arg = 1;  /* only self-signed */
423     if (!TEST_true(sk_X509_push(fixture->certs, endentity1))
424             || !TEST_true(sk_X509_push(fixture->certs, endentity2))
425             || !TEST_true(sk_X509_push(fixture->certs, root))
426             || !TEST_true(sk_X509_push(fixture->certs, intermediate))
427             || !TEST_true(sk_X509_push(fixture->chain, root))) {
428         tear_down(fixture);
429         fixture = NULL;
430     }
431     EXECUTE_TEST(execute_X509_STORE_test, tear_down);
432     return result;
433 }
434
435
436 void cleanup_tests(void)
437 {
438     EVP_PKEY_free(loadedprivkey);
439     EVP_PKEY_free(loadedpubkey);
440     EVP_PKEY_free(loadedkey);
441     X509_free(cert);
442     X509_free(endentity1);
443     X509_free(endentity2);
444     X509_free(root);
445     X509_free(intermediate);
446     OSSL_CMP_MSG_free(ir_protected);
447     OSSL_CMP_MSG_free(ir_unprotected);
448
449 }
450
451 int setup_tests(void)
452 {
453     char *server_f;
454     char *server_key_f;
455     char *server_cert_f;
456     char *endentity1_f;
457     char *endentity2_f;
458     char *root_f;
459     char *intermediate_f;
460
461     if (!test_skip_common_options()) {
462         TEST_error("Error parsing test options\n");
463         return 0;
464     }
465
466     RAND_bytes(rand_data, OSSL_CMP_TRANSACTIONID_LENGTH);
467     if (!TEST_ptr(server_f = test_get_argument(0))
468             || !TEST_ptr(ir_protected_f = test_get_argument(1))
469             || !TEST_ptr(ir_unprotected_f = test_get_argument(2))
470             || !TEST_ptr(ip_PBM_f = test_get_argument(3))
471             || !TEST_ptr(server_cert_f = test_get_argument(4))
472             || !TEST_ptr(server_key_f = test_get_argument(5))
473             || !TEST_ptr(endentity1_f = test_get_argument(6))
474             || !TEST_ptr(endentity2_f = test_get_argument(7))
475             || !TEST_ptr(root_f = test_get_argument(8))
476             || !TEST_ptr(intermediate_f = test_get_argument(9))) {
477         TEST_error("usage: cmp_protect_test server.pem "
478                    "IR_protected.der IR_unprotected.der IP_PBM.der "
479                    "server.crt server.pem"
480                    "EndEntity1.crt EndEntity2.crt "
481                    "Root_CA.crt Intermediate_CA.crt\n");
482         return 0;
483     }
484     if (!TEST_ptr(loadedkey = load_pem_key(server_key_f))
485             || !TEST_ptr(cert = load_pem_cert(server_cert_f)))
486         return 0;
487
488     if (!TEST_ptr(loadedprivkey = load_pem_key(server_f)))
489         return 0;
490     if (TEST_true(EVP_PKEY_up_ref(loadedprivkey)))
491         loadedpubkey = loadedprivkey;
492     if (!TEST_ptr(ir_protected = load_pkimsg(ir_protected_f))
493             || !TEST_ptr(ir_unprotected = load_pkimsg(ir_unprotected_f)))
494         return 0;
495     if (!TEST_ptr(endentity1 = load_pem_cert(endentity1_f))
496             || !TEST_ptr(endentity2 = load_pem_cert(endentity2_f))
497             || !TEST_ptr(root = load_pem_cert(root_f))
498             || !TEST_ptr(intermediate = load_pem_cert(intermediate_f)))
499         return 0;
500     if (!TEST_int_eq(1, RAND_bytes(rand_data, OSSL_CMP_TRANSACTIONID_LENGTH)))
501         return 0;
502
503     /* Message protection tests */
504     ADD_TEST(test_cmp_calc_protection_no_key_no_secret);
505     ADD_TEST(test_cmp_calc_protection_pkey);
506     ADD_TEST(test_cmp_calc_protection_pbmac);
507
508     ADD_TEST(test_MSG_protect_with_msg_sig_alg_protection_plus_rsa_key);
509     ADD_TEST(test_MSG_protect_with_certificate_and_key);
510     ADD_TEST(test_MSG_protect_certificate_based_without_cert);
511     ADD_TEST(test_MSG_protect_unprotected_request);
512     ADD_TEST(test_MSG_protect_no_key_no_secret);
513
514     ADD_TEST(test_MSG_add_extraCerts);
515
516 #ifndef OPENSSL_NO_EC
517     ADD_TEST(test_cmp_build_cert_chain);
518     ADD_TEST(test_cmp_build_cert_chain_missing_root);
519     ADD_TEST(test_cmp_build_cert_chain_missing_intermediate);
520     ADD_TEST(test_cmp_build_cert_chain_no_certs);
521 #endif
522
523     ADD_TEST(test_X509_STORE);
524     ADD_TEST(test_X509_STORE_only_self_signed);
525
526     return 1;
527 }