1 | /*
|
---|
2 | * Copyright 2007-2023 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 "helpers/cmp_testlib.h"
|
---|
13 |
|
---|
14 | static const char *newkey_f;
|
---|
15 | static const char *server_cert_f;
|
---|
16 | static const char *pkcs10_f;
|
---|
17 |
|
---|
18 | typedef struct test_fixture {
|
---|
19 | const char *test_case_name;
|
---|
20 | OSSL_CMP_CTX *cmp_ctx;
|
---|
21 | /* for msg create tests */
|
---|
22 | int bodytype;
|
---|
23 | int err_code;
|
---|
24 | /* for certConf */
|
---|
25 | int fail_info;
|
---|
26 | /* for protection tests */
|
---|
27 | OSSL_CMP_MSG *msg;
|
---|
28 | int expected;
|
---|
29 | /* for error and response messages */
|
---|
30 | OSSL_CMP_PKISI *si;
|
---|
31 | } CMP_MSG_TEST_FIXTURE;
|
---|
32 |
|
---|
33 | static OSSL_LIB_CTX *libctx = NULL;
|
---|
34 | static OSSL_PROVIDER *default_null_provider = NULL, *provider = NULL;
|
---|
35 |
|
---|
36 | static unsigned char ref[CMP_TEST_REFVALUE_LENGTH];
|
---|
37 |
|
---|
38 | static void tear_down(CMP_MSG_TEST_FIXTURE *fixture)
|
---|
39 | {
|
---|
40 | OSSL_CMP_CTX_free(fixture->cmp_ctx);
|
---|
41 | OSSL_CMP_MSG_free(fixture->msg);
|
---|
42 | OSSL_CMP_PKISI_free(fixture->si);
|
---|
43 | OPENSSL_free(fixture);
|
---|
44 | }
|
---|
45 |
|
---|
46 | #define SET_OPT_UNPROTECTED_SEND(ctx, val) \
|
---|
47 | OSSL_CMP_CTX_set_option((ctx), OSSL_CMP_OPT_UNPROTECTED_SEND, (val))
|
---|
48 | static CMP_MSG_TEST_FIXTURE *set_up(const char *const test_case_name)
|
---|
49 | {
|
---|
50 | CMP_MSG_TEST_FIXTURE *fixture;
|
---|
51 |
|
---|
52 | if (!TEST_ptr(fixture = OPENSSL_zalloc(sizeof(*fixture))))
|
---|
53 | return NULL;
|
---|
54 | fixture->test_case_name = test_case_name;
|
---|
55 |
|
---|
56 | if (!TEST_ptr(fixture->cmp_ctx = OSSL_CMP_CTX_new(libctx, NULL))
|
---|
57 | || !TEST_true(SET_OPT_UNPROTECTED_SEND(fixture->cmp_ctx, 1))
|
---|
58 | || !TEST_true(OSSL_CMP_CTX_set1_referenceValue(fixture->cmp_ctx,
|
---|
59 | ref, sizeof(ref)))) {
|
---|
60 | tear_down(fixture);
|
---|
61 | return NULL;
|
---|
62 | }
|
---|
63 | return fixture;
|
---|
64 | }
|
---|
65 |
|
---|
66 | static EVP_PKEY *newkey = NULL;
|
---|
67 | static X509 *cert = NULL;
|
---|
68 |
|
---|
69 | #define EXECUTE_MSG_CREATION_TEST(expr) \
|
---|
70 | do { \
|
---|
71 | OSSL_CMP_MSG *msg = NULL; \
|
---|
72 | int good = fixture->expected != 0 ? \
|
---|
73 | TEST_ptr(msg = (expr)) && TEST_true(valid_asn1_encoding(msg)) : \
|
---|
74 | TEST_ptr_null(msg = (expr)); \
|
---|
75 | \
|
---|
76 | OSSL_CMP_MSG_free(msg); \
|
---|
77 | ERR_print_errors_fp(stderr); \
|
---|
78 | return good; \
|
---|
79 | } while (0)
|
---|
80 |
|
---|
81 | /*-
|
---|
82 | * The following tests call a cmp message creation function.
|
---|
83 | * if fixture->expected != 0:
|
---|
84 | * returns 1 if the message is created and syntactically correct.
|
---|
85 | * if fixture->expected == 0
|
---|
86 | * returns 1 if message creation returns NULL
|
---|
87 | */
|
---|
88 | static int execute_certreq_create_test(CMP_MSG_TEST_FIXTURE *fixture)
|
---|
89 | {
|
---|
90 | EXECUTE_MSG_CREATION_TEST(ossl_cmp_certreq_new(fixture->cmp_ctx,
|
---|
91 | fixture->bodytype,
|
---|
92 | NULL));
|
---|
93 | }
|
---|
94 |
|
---|
95 | static int execute_errormsg_create_test(CMP_MSG_TEST_FIXTURE *fixture)
|
---|
96 | {
|
---|
97 | EXECUTE_MSG_CREATION_TEST(ossl_cmp_error_new(fixture->cmp_ctx, fixture->si,
|
---|
98 | fixture->err_code,
|
---|
99 | "details", 0));
|
---|
100 | }
|
---|
101 |
|
---|
102 | static int execute_rr_create_test(CMP_MSG_TEST_FIXTURE *fixture)
|
---|
103 | {
|
---|
104 | EXECUTE_MSG_CREATION_TEST(ossl_cmp_rr_new(fixture->cmp_ctx));
|
---|
105 | }
|
---|
106 |
|
---|
107 | static int execute_certconf_create_test(CMP_MSG_TEST_FIXTURE *fixture)
|
---|
108 | {
|
---|
109 | EXECUTE_MSG_CREATION_TEST(ossl_cmp_certConf_new
|
---|
110 | (fixture->cmp_ctx, OSSL_CMP_CERTREQID,
|
---|
111 | fixture->fail_info, NULL));
|
---|
112 | }
|
---|
113 |
|
---|
114 | static int execute_genm_create_test(CMP_MSG_TEST_FIXTURE *fixture)
|
---|
115 | {
|
---|
116 | EXECUTE_MSG_CREATION_TEST(ossl_cmp_genm_new(fixture->cmp_ctx));
|
---|
117 | }
|
---|
118 |
|
---|
119 | static int execute_pollreq_create_test(CMP_MSG_TEST_FIXTURE *fixture)
|
---|
120 | {
|
---|
121 | EXECUTE_MSG_CREATION_TEST(ossl_cmp_pollReq_new(fixture->cmp_ctx, 4711));
|
---|
122 | }
|
---|
123 |
|
---|
124 | static int execute_pkimessage_create_test(CMP_MSG_TEST_FIXTURE *fixture)
|
---|
125 | {
|
---|
126 | EXECUTE_MSG_CREATION_TEST(ossl_cmp_msg_create
|
---|
127 | (fixture->cmp_ctx, fixture->bodytype));
|
---|
128 | }
|
---|
129 |
|
---|
130 | static int set1_newPkey(OSSL_CMP_CTX *ctx, EVP_PKEY *pkey)
|
---|
131 | {
|
---|
132 | if (!EVP_PKEY_up_ref(pkey))
|
---|
133 | return 0;
|
---|
134 |
|
---|
135 | if (!OSSL_CMP_CTX_set0_newPkey(ctx, 1, pkey)) {
|
---|
136 | EVP_PKEY_free(pkey);
|
---|
137 | return 0;
|
---|
138 | }
|
---|
139 | return 1;
|
---|
140 | }
|
---|
141 |
|
---|
142 | static int test_cmp_create_ir_protection_set(void)
|
---|
143 | {
|
---|
144 | OSSL_CMP_CTX *ctx;
|
---|
145 | unsigned char secret[16];
|
---|
146 |
|
---|
147 | SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
|
---|
148 |
|
---|
149 | ctx = fixture->cmp_ctx;
|
---|
150 | fixture->bodytype = OSSL_CMP_PKIBODY_IR;
|
---|
151 | fixture->err_code = -1;
|
---|
152 | fixture->expected = 1;
|
---|
153 | if (!TEST_int_eq(1, RAND_bytes_ex(libctx, secret, sizeof(secret), 0))
|
---|
154 | || !TEST_true(SET_OPT_UNPROTECTED_SEND(ctx, 0))
|
---|
155 | || !TEST_true(set1_newPkey(ctx, newkey))
|
---|
156 | || !TEST_true(OSSL_CMP_CTX_set1_secretValue(ctx, secret,
|
---|
157 | sizeof(secret)))) {
|
---|
158 | tear_down(fixture);
|
---|
159 | fixture = NULL;
|
---|
160 | }
|
---|
161 | EXECUTE_TEST(execute_certreq_create_test, tear_down);
|
---|
162 | return result;
|
---|
163 | }
|
---|
164 |
|
---|
165 | static int test_cmp_create_ir_protection_fails(void)
|
---|
166 | {
|
---|
167 | SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
|
---|
168 | fixture->bodytype = OSSL_CMP_PKIBODY_IR;
|
---|
169 | fixture->err_code = -1;
|
---|
170 | fixture->expected = 0;
|
---|
171 | if (!TEST_true(OSSL_CMP_CTX_set1_pkey(fixture->cmp_ctx, newkey))
|
---|
172 | || !TEST_true(SET_OPT_UNPROTECTED_SEND(fixture->cmp_ctx, 0))
|
---|
173 | /* newkey used by default for signing does not match cert: */
|
---|
174 | || !TEST_true(OSSL_CMP_CTX_set1_cert(fixture->cmp_ctx, cert))) {
|
---|
175 | tear_down(fixture);
|
---|
176 | fixture = NULL;
|
---|
177 | }
|
---|
178 | EXECUTE_TEST(execute_certreq_create_test, tear_down);
|
---|
179 | return result;
|
---|
180 | }
|
---|
181 |
|
---|
182 | static int test_cmp_create_cr_without_key(void)
|
---|
183 | {
|
---|
184 | SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
|
---|
185 | fixture->bodytype = OSSL_CMP_PKIBODY_CR;
|
---|
186 | fixture->err_code = -1;
|
---|
187 | fixture->expected = 0;
|
---|
188 | EXECUTE_TEST(execute_certreq_create_test, tear_down);
|
---|
189 | return result;
|
---|
190 | }
|
---|
191 |
|
---|
192 | static int test_cmp_create_cr(void)
|
---|
193 | {
|
---|
194 | SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
|
---|
195 | fixture->bodytype = OSSL_CMP_PKIBODY_CR;
|
---|
196 | fixture->err_code = -1;
|
---|
197 | fixture->expected = 1;
|
---|
198 | if (!TEST_true(set1_newPkey(fixture->cmp_ctx, newkey))) {
|
---|
199 | tear_down(fixture);
|
---|
200 | fixture = NULL;
|
---|
201 | }
|
---|
202 | EXECUTE_TEST(execute_certreq_create_test, tear_down);
|
---|
203 | return result;
|
---|
204 | }
|
---|
205 |
|
---|
206 | static int test_cmp_create_certreq_with_invalid_bodytype(void)
|
---|
207 | {
|
---|
208 | SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
|
---|
209 | fixture->bodytype = OSSL_CMP_PKIBODY_RR;
|
---|
210 | fixture->err_code = -1;
|
---|
211 | fixture->expected = 0;
|
---|
212 | if (!TEST_true(set1_newPkey(fixture->cmp_ctx, newkey))) {
|
---|
213 | tear_down(fixture);
|
---|
214 | fixture = NULL;
|
---|
215 | }
|
---|
216 | EXECUTE_TEST(execute_certreq_create_test, tear_down);
|
---|
217 | return result;
|
---|
218 | }
|
---|
219 |
|
---|
220 | static int test_cmp_create_p10cr(void)
|
---|
221 | {
|
---|
222 | OSSL_CMP_CTX *ctx;
|
---|
223 | X509_REQ *p10cr = NULL;
|
---|
224 |
|
---|
225 | SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
|
---|
226 | ctx = fixture->cmp_ctx;
|
---|
227 | fixture->bodytype = OSSL_CMP_PKIBODY_P10CR;
|
---|
228 | fixture->err_code = CMP_R_ERROR_CREATING_CERTREQ;
|
---|
229 | fixture->expected = 1;
|
---|
230 | if (!TEST_ptr(p10cr = load_csr_der(pkcs10_f, libctx))
|
---|
231 | || !TEST_true(set1_newPkey(ctx, newkey))
|
---|
232 | || !TEST_true(OSSL_CMP_CTX_set1_p10CSR(ctx, p10cr))) {
|
---|
233 | tear_down(fixture);
|
---|
234 | fixture = NULL;
|
---|
235 | }
|
---|
236 | X509_REQ_free(p10cr);
|
---|
237 | EXECUTE_TEST(execute_certreq_create_test, tear_down);
|
---|
238 | return result;
|
---|
239 | }
|
---|
240 |
|
---|
241 | static int test_cmp_create_p10cr_null(void)
|
---|
242 | {
|
---|
243 | SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
|
---|
244 | fixture->bodytype = OSSL_CMP_PKIBODY_P10CR;
|
---|
245 | fixture->err_code = CMP_R_ERROR_CREATING_CERTREQ;
|
---|
246 | fixture->expected = 0;
|
---|
247 | if (!TEST_true(set1_newPkey(fixture->cmp_ctx, newkey))) {
|
---|
248 | tear_down(fixture);
|
---|
249 | fixture = NULL;
|
---|
250 | }
|
---|
251 | EXECUTE_TEST(execute_certreq_create_test, tear_down);
|
---|
252 | return result;
|
---|
253 | }
|
---|
254 |
|
---|
255 | static int test_cmp_create_kur(void)
|
---|
256 | {
|
---|
257 | SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
|
---|
258 | fixture->bodytype = OSSL_CMP_PKIBODY_KUR;
|
---|
259 | fixture->err_code = -1;
|
---|
260 | fixture->expected = 1;
|
---|
261 | if (!TEST_true(set1_newPkey(fixture->cmp_ctx, newkey))
|
---|
262 | || !TEST_true(OSSL_CMP_CTX_set1_oldCert(fixture->cmp_ctx, cert))) {
|
---|
263 | tear_down(fixture);
|
---|
264 | fixture = NULL;
|
---|
265 | }
|
---|
266 | EXECUTE_TEST(execute_certreq_create_test, tear_down);
|
---|
267 | return result;
|
---|
268 | }
|
---|
269 |
|
---|
270 | static int test_cmp_create_kur_without_oldcert(void)
|
---|
271 | {
|
---|
272 | SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
|
---|
273 | fixture->bodytype = OSSL_CMP_PKIBODY_KUR;
|
---|
274 | fixture->err_code = -1;
|
---|
275 | fixture->expected = 0;
|
---|
276 | if (!TEST_true(set1_newPkey(fixture->cmp_ctx, newkey))) {
|
---|
277 | tear_down(fixture);
|
---|
278 | fixture = NULL;
|
---|
279 | }
|
---|
280 | EXECUTE_TEST(execute_certreq_create_test, tear_down);
|
---|
281 | return result;
|
---|
282 | }
|
---|
283 |
|
---|
284 | static int test_cmp_create_certconf(void)
|
---|
285 | {
|
---|
286 | SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
|
---|
287 | fixture->fail_info = 0;
|
---|
288 | fixture->expected = 1;
|
---|
289 | if (!TEST_true(ossl_cmp_ctx_set0_newCert(fixture->cmp_ctx,
|
---|
290 | X509_dup(cert)))) {
|
---|
291 | tear_down(fixture);
|
---|
292 | fixture = NULL;
|
---|
293 | }
|
---|
294 | EXECUTE_TEST(execute_certconf_create_test, tear_down);
|
---|
295 | return result;
|
---|
296 | }
|
---|
297 |
|
---|
298 | static int test_cmp_create_certconf_badAlg(void)
|
---|
299 | {
|
---|
300 | SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
|
---|
301 | fixture->fail_info = 1 << OSSL_CMP_PKIFAILUREINFO_badAlg;
|
---|
302 | fixture->expected = 1;
|
---|
303 | if (!TEST_true(ossl_cmp_ctx_set0_newCert(fixture->cmp_ctx,
|
---|
304 | X509_dup(cert)))) {
|
---|
305 | tear_down(fixture);
|
---|
306 | fixture = NULL;
|
---|
307 | }
|
---|
308 | EXECUTE_TEST(execute_certconf_create_test, tear_down);
|
---|
309 | return result;
|
---|
310 | }
|
---|
311 |
|
---|
312 | static int test_cmp_create_certconf_fail_info_max(void)
|
---|
313 | {
|
---|
314 | SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
|
---|
315 | fixture->fail_info = 1 << OSSL_CMP_PKIFAILUREINFO_MAX;
|
---|
316 | fixture->expected = 1;
|
---|
317 | if (!TEST_true(ossl_cmp_ctx_set0_newCert(fixture->cmp_ctx,
|
---|
318 | X509_dup(cert)))) {
|
---|
319 | tear_down(fixture);
|
---|
320 | fixture = NULL;
|
---|
321 | }
|
---|
322 | EXECUTE_TEST(execute_certconf_create_test, tear_down);
|
---|
323 | return result;
|
---|
324 | }
|
---|
325 |
|
---|
326 | static int test_cmp_create_error_msg(void)
|
---|
327 | {
|
---|
328 | SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
|
---|
329 | fixture->si = OSSL_CMP_STATUSINFO_new(OSSL_CMP_PKISTATUS_rejection,
|
---|
330 | OSSL_CMP_PKIFAILUREINFO_systemFailure,
|
---|
331 | NULL);
|
---|
332 | fixture->err_code = -1;
|
---|
333 | fixture->expected = 1; /* expected: message creation is successful */
|
---|
334 | if (!TEST_true(set1_newPkey(fixture->cmp_ctx, newkey))) {
|
---|
335 | tear_down(fixture);
|
---|
336 | fixture = NULL;
|
---|
337 | }
|
---|
338 | EXECUTE_TEST(execute_errormsg_create_test, tear_down);
|
---|
339 | return result;
|
---|
340 | }
|
---|
341 |
|
---|
342 |
|
---|
343 | static int test_cmp_create_pollreq(void)
|
---|
344 | {
|
---|
345 | SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
|
---|
346 | fixture->expected = 1;
|
---|
347 | EXECUTE_TEST(execute_pollreq_create_test, tear_down);
|
---|
348 | return result;
|
---|
349 | }
|
---|
350 |
|
---|
351 | static int test_cmp_create_rr(void)
|
---|
352 | {
|
---|
353 | SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
|
---|
354 | fixture->expected = 1;
|
---|
355 | if (!TEST_true(OSSL_CMP_CTX_set1_oldCert(fixture->cmp_ctx, cert))) {
|
---|
356 | tear_down(fixture);
|
---|
357 | fixture = NULL;
|
---|
358 | }
|
---|
359 | EXECUTE_TEST(execute_rr_create_test, tear_down);
|
---|
360 | return result;
|
---|
361 | }
|
---|
362 |
|
---|
363 | static int test_cmp_create_genm(void)
|
---|
364 | {
|
---|
365 | OSSL_CMP_ITAV *iv = NULL;
|
---|
366 |
|
---|
367 | SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
|
---|
368 | fixture->expected = 1;
|
---|
369 | iv = OSSL_CMP_ITAV_create(OBJ_nid2obj(NID_id_it_implicitConfirm), NULL);
|
---|
370 | if (!TEST_ptr(iv)
|
---|
371 | || !TEST_true(OSSL_CMP_CTX_push0_genm_ITAV(fixture->cmp_ctx, iv))) {
|
---|
372 | OSSL_CMP_ITAV_free(iv);
|
---|
373 | tear_down(fixture);
|
---|
374 | fixture = NULL;
|
---|
375 | }
|
---|
376 |
|
---|
377 | EXECUTE_TEST(execute_genm_create_test, tear_down);
|
---|
378 | return result;
|
---|
379 | }
|
---|
380 |
|
---|
381 | static int execute_certrep_create(CMP_MSG_TEST_FIXTURE *fixture)
|
---|
382 | {
|
---|
383 | OSSL_CMP_CTX *ctx = fixture->cmp_ctx;
|
---|
384 | OSSL_CMP_CERTREPMESSAGE *crepmsg = OSSL_CMP_CERTREPMESSAGE_new();
|
---|
385 | OSSL_CMP_CERTRESPONSE *read_cresp, *cresp = OSSL_CMP_CERTRESPONSE_new();
|
---|
386 | X509 *certfromresp = NULL;
|
---|
387 | int res = 0;
|
---|
388 |
|
---|
389 | if (crepmsg == NULL || cresp == NULL)
|
---|
390 | goto err;
|
---|
391 | if (!ASN1_INTEGER_set(cresp->certReqId, 99))
|
---|
392 | goto err;
|
---|
393 | if ((cresp->certifiedKeyPair = OSSL_CMP_CERTIFIEDKEYPAIR_new()) == NULL)
|
---|
394 | goto err;
|
---|
395 | cresp->certifiedKeyPair->certOrEncCert->type =
|
---|
396 | OSSL_CMP_CERTORENCCERT_CERTIFICATE;
|
---|
397 | if ((cresp->certifiedKeyPair->certOrEncCert->value.certificate =
|
---|
398 | X509_dup(cert)) == NULL
|
---|
399 | || !sk_OSSL_CMP_CERTRESPONSE_push(crepmsg->response, cresp))
|
---|
400 | goto err;
|
---|
401 | cresp = NULL;
|
---|
402 | read_cresp = ossl_cmp_certrepmessage_get0_certresponse(crepmsg, 99);
|
---|
403 | if (!TEST_ptr(read_cresp))
|
---|
404 | goto err;
|
---|
405 | if (!TEST_ptr_null(ossl_cmp_certrepmessage_get0_certresponse(crepmsg, 88)))
|
---|
406 | goto err;
|
---|
407 | certfromresp = ossl_cmp_certresponse_get1_cert(ctx, read_cresp);
|
---|
408 | if (certfromresp == NULL || !TEST_int_eq(X509_cmp(cert, certfromresp), 0))
|
---|
409 | goto err;
|
---|
410 |
|
---|
411 | res = 1;
|
---|
412 | err:
|
---|
413 | X509_free(certfromresp);
|
---|
414 | OSSL_CMP_CERTRESPONSE_free(cresp);
|
---|
415 | OSSL_CMP_CERTREPMESSAGE_free(crepmsg);
|
---|
416 | return res;
|
---|
417 | }
|
---|
418 |
|
---|
419 | static int test_cmp_create_certrep(void)
|
---|
420 | {
|
---|
421 | SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
|
---|
422 | EXECUTE_TEST(execute_certrep_create, tear_down);
|
---|
423 | return result;
|
---|
424 | }
|
---|
425 |
|
---|
426 |
|
---|
427 | static int execute_rp_create(CMP_MSG_TEST_FIXTURE *fixture)
|
---|
428 | {
|
---|
429 | OSSL_CMP_PKISI *si = OSSL_CMP_STATUSINFO_new(33, 44, "a text");
|
---|
430 | X509_NAME *issuer = X509_NAME_new();
|
---|
431 | ASN1_INTEGER *serial = ASN1_INTEGER_new();
|
---|
432 | OSSL_CRMF_CERTID *cid = NULL;
|
---|
433 | OSSL_CMP_MSG *rpmsg = NULL;
|
---|
434 | int res = 0;
|
---|
435 |
|
---|
436 | if (si == NULL || issuer == NULL || serial == NULL)
|
---|
437 | goto err;
|
---|
438 |
|
---|
439 | if (!X509_NAME_add_entry_by_txt(issuer, "CN", MBSTRING_ASC,
|
---|
440 | (unsigned char *)"The Issuer", -1, -1, 0)
|
---|
441 | || !ASN1_INTEGER_set(serial, 99)
|
---|
442 | || (cid = OSSL_CRMF_CERTID_gen(issuer, serial)) == NULL
|
---|
443 | || (rpmsg = ossl_cmp_rp_new(fixture->cmp_ctx, si, cid, 1)) == NULL)
|
---|
444 | goto err;
|
---|
445 |
|
---|
446 | if (!TEST_ptr(ossl_cmp_revrepcontent_get_CertId(rpmsg->body->value.rp, 0)))
|
---|
447 | goto err;
|
---|
448 |
|
---|
449 | if (!TEST_ptr(ossl_cmp_revrepcontent_get_pkisi(rpmsg->body->value.rp, 0)))
|
---|
450 | goto err;
|
---|
451 |
|
---|
452 | res = 1;
|
---|
453 | err:
|
---|
454 | ASN1_INTEGER_free(serial);
|
---|
455 | X509_NAME_free(issuer);
|
---|
456 | OSSL_CRMF_CERTID_free(cid);
|
---|
457 | OSSL_CMP_PKISI_free(si);
|
---|
458 | OSSL_CMP_MSG_free(rpmsg);
|
---|
459 | return res;
|
---|
460 | }
|
---|
461 |
|
---|
462 | static int test_cmp_create_rp(void)
|
---|
463 | {
|
---|
464 | SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
|
---|
465 | EXECUTE_TEST(execute_rp_create, tear_down);
|
---|
466 | return result;
|
---|
467 | }
|
---|
468 |
|
---|
469 | static int execute_pollrep_create(CMP_MSG_TEST_FIXTURE *fixture)
|
---|
470 | {
|
---|
471 | OSSL_CMP_MSG *pollrep;
|
---|
472 | int res = 0;
|
---|
473 |
|
---|
474 | pollrep = ossl_cmp_pollRep_new(fixture->cmp_ctx, 77, 2000);
|
---|
475 | if (!TEST_ptr(pollrep))
|
---|
476 | return 0;
|
---|
477 | if (!TEST_ptr(ossl_cmp_pollrepcontent_get0_pollrep(pollrep->body->
|
---|
478 | value.pollRep, 77)))
|
---|
479 | goto err;
|
---|
480 | if (!TEST_ptr_null(ossl_cmp_pollrepcontent_get0_pollrep(pollrep->body->
|
---|
481 | value.pollRep, 88)))
|
---|
482 | goto err;
|
---|
483 |
|
---|
484 | res = 1;
|
---|
485 | err:
|
---|
486 | OSSL_CMP_MSG_free(pollrep);
|
---|
487 | return res;
|
---|
488 | }
|
---|
489 |
|
---|
490 | static int test_cmp_create_pollrep(void)
|
---|
491 | {
|
---|
492 | SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
|
---|
493 | EXECUTE_TEST(execute_pollrep_create, tear_down);
|
---|
494 | return result;
|
---|
495 | }
|
---|
496 |
|
---|
497 | static int test_cmp_pkimessage_create(int bodytype)
|
---|
498 | {
|
---|
499 | X509_REQ *p10cr = NULL;
|
---|
500 |
|
---|
501 | SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
|
---|
502 |
|
---|
503 | switch (fixture->bodytype = bodytype) {
|
---|
504 | case OSSL_CMP_PKIBODY_P10CR:
|
---|
505 | fixture->expected = 1;
|
---|
506 | p10cr = load_csr_der(pkcs10_f, libctx);
|
---|
507 | if (!TEST_true(OSSL_CMP_CTX_set1_p10CSR(fixture->cmp_ctx, p10cr))) {
|
---|
508 | tear_down(fixture);
|
---|
509 | fixture = NULL;
|
---|
510 | }
|
---|
511 | X509_REQ_free(p10cr);
|
---|
512 | break;
|
---|
513 | case OSSL_CMP_PKIBODY_IR:
|
---|
514 | case OSSL_CMP_PKIBODY_IP:
|
---|
515 | case OSSL_CMP_PKIBODY_CR:
|
---|
516 | case OSSL_CMP_PKIBODY_CP:
|
---|
517 | case OSSL_CMP_PKIBODY_KUR:
|
---|
518 | case OSSL_CMP_PKIBODY_KUP:
|
---|
519 | case OSSL_CMP_PKIBODY_RR:
|
---|
520 | case OSSL_CMP_PKIBODY_RP:
|
---|
521 | case OSSL_CMP_PKIBODY_PKICONF:
|
---|
522 | case OSSL_CMP_PKIBODY_GENM:
|
---|
523 | case OSSL_CMP_PKIBODY_GENP:
|
---|
524 | case OSSL_CMP_PKIBODY_ERROR:
|
---|
525 | case OSSL_CMP_PKIBODY_CERTCONF:
|
---|
526 | case OSSL_CMP_PKIBODY_POLLREQ:
|
---|
527 | case OSSL_CMP_PKIBODY_POLLREP:
|
---|
528 | fixture->expected = 1;
|
---|
529 | break;
|
---|
530 | default:
|
---|
531 | fixture->expected = 0;
|
---|
532 | break;
|
---|
533 | }
|
---|
534 |
|
---|
535 | EXECUTE_TEST(execute_pkimessage_create_test, tear_down);
|
---|
536 | return result;
|
---|
537 | }
|
---|
538 |
|
---|
539 | void cleanup_tests(void)
|
---|
540 | {
|
---|
541 | EVP_PKEY_free(newkey);
|
---|
542 | X509_free(cert);
|
---|
543 | OSSL_PROVIDER_unload(default_null_provider);
|
---|
544 | OSSL_PROVIDER_unload(provider);
|
---|
545 | OSSL_LIB_CTX_free(libctx);
|
---|
546 | }
|
---|
547 |
|
---|
548 | #define USAGE "new.key server.crt pkcs10.der module_name [module_conf_file]\n"
|
---|
549 | OPT_TEST_DECLARE_USAGE(USAGE)
|
---|
550 |
|
---|
551 | int setup_tests(void)
|
---|
552 | {
|
---|
553 | if (!test_skip_common_options()) {
|
---|
554 | TEST_error("Error parsing test options\n");
|
---|
555 | return 0;
|
---|
556 | }
|
---|
557 |
|
---|
558 | if (!TEST_ptr(newkey_f = test_get_argument(0))
|
---|
559 | || !TEST_ptr(server_cert_f = test_get_argument(1))
|
---|
560 | || !TEST_ptr(pkcs10_f = test_get_argument(2))) {
|
---|
561 | TEST_error("usage: cmp_msg_test %s", USAGE);
|
---|
562 | return 0;
|
---|
563 | }
|
---|
564 |
|
---|
565 | if (!test_arg_libctx(&libctx, &default_null_provider, &provider, 3, USAGE))
|
---|
566 | return 0;
|
---|
567 |
|
---|
568 | if (!TEST_ptr(newkey = load_pkey_pem(newkey_f, libctx))
|
---|
569 | || !TEST_ptr(cert = load_cert_pem(server_cert_f, libctx))
|
---|
570 | || !TEST_int_eq(1, RAND_bytes_ex(libctx, ref, sizeof(ref), 0))) {
|
---|
571 | cleanup_tests();
|
---|
572 | return 0;
|
---|
573 | }
|
---|
574 |
|
---|
575 | /* Message creation tests */
|
---|
576 | ADD_TEST(test_cmp_create_certreq_with_invalid_bodytype);
|
---|
577 | ADD_TEST(test_cmp_create_ir_protection_fails);
|
---|
578 | ADD_TEST(test_cmp_create_ir_protection_set);
|
---|
579 | ADD_TEST(test_cmp_create_error_msg);
|
---|
580 | ADD_TEST(test_cmp_create_certconf);
|
---|
581 | ADD_TEST(test_cmp_create_certconf_badAlg);
|
---|
582 | ADD_TEST(test_cmp_create_certconf_fail_info_max);
|
---|
583 | ADD_TEST(test_cmp_create_kur);
|
---|
584 | ADD_TEST(test_cmp_create_kur_without_oldcert);
|
---|
585 | ADD_TEST(test_cmp_create_cr);
|
---|
586 | ADD_TEST(test_cmp_create_cr_without_key);
|
---|
587 | ADD_TEST(test_cmp_create_p10cr);
|
---|
588 | ADD_TEST(test_cmp_create_p10cr_null);
|
---|
589 | ADD_TEST(test_cmp_create_pollreq);
|
---|
590 | ADD_TEST(test_cmp_create_rr);
|
---|
591 | ADD_TEST(test_cmp_create_rp);
|
---|
592 | ADD_TEST(test_cmp_create_genm);
|
---|
593 | ADD_TEST(test_cmp_create_certrep);
|
---|
594 | ADD_TEST(test_cmp_create_pollrep);
|
---|
595 | ADD_ALL_TESTS_NOSUBTEST(test_cmp_pkimessage_create,
|
---|
596 | OSSL_CMP_PKIBODY_POLLREP + 1);
|
---|
597 | return 1;
|
---|
598 | }
|
---|