1 | /*
|
---|
2 | * Copyright 2007-2021 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, fixture->fail_info, NULL));
|
---|
111 | }
|
---|
112 |
|
---|
113 | static int execute_genm_create_test(CMP_MSG_TEST_FIXTURE *fixture)
|
---|
114 | {
|
---|
115 | EXECUTE_MSG_CREATION_TEST(ossl_cmp_genm_new(fixture->cmp_ctx));
|
---|
116 | }
|
---|
117 |
|
---|
118 | static int execute_pollreq_create_test(CMP_MSG_TEST_FIXTURE *fixture)
|
---|
119 | {
|
---|
120 | EXECUTE_MSG_CREATION_TEST(ossl_cmp_pollReq_new(fixture->cmp_ctx, 4711));
|
---|
121 | }
|
---|
122 |
|
---|
123 | static int execute_pkimessage_create_test(CMP_MSG_TEST_FIXTURE *fixture)
|
---|
124 | {
|
---|
125 | EXECUTE_MSG_CREATION_TEST(ossl_cmp_msg_create
|
---|
126 | (fixture->cmp_ctx, fixture->bodytype));
|
---|
127 | }
|
---|
128 |
|
---|
129 | static int set1_newPkey(OSSL_CMP_CTX *ctx, EVP_PKEY *pkey)
|
---|
130 | {
|
---|
131 | if (!EVP_PKEY_up_ref(pkey))
|
---|
132 | return 0;
|
---|
133 |
|
---|
134 | if (!OSSL_CMP_CTX_set0_newPkey(ctx, 1, pkey)) {
|
---|
135 | EVP_PKEY_free(pkey);
|
---|
136 | return 0;
|
---|
137 | }
|
---|
138 | return 1;
|
---|
139 | }
|
---|
140 |
|
---|
141 | static int test_cmp_create_ir_protection_set(void)
|
---|
142 | {
|
---|
143 | OSSL_CMP_CTX *ctx;
|
---|
144 | unsigned char secret[16];
|
---|
145 |
|
---|
146 | SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
|
---|
147 |
|
---|
148 | ctx = fixture->cmp_ctx;
|
---|
149 | fixture->bodytype = OSSL_CMP_PKIBODY_IR;
|
---|
150 | fixture->err_code = -1;
|
---|
151 | fixture->expected = 1;
|
---|
152 | if (!TEST_int_eq(1, RAND_bytes_ex(libctx, secret, sizeof(secret), 0))
|
---|
153 | || !TEST_true(SET_OPT_UNPROTECTED_SEND(ctx, 0))
|
---|
154 | || !TEST_true(set1_newPkey(ctx, newkey))
|
---|
155 | || !TEST_true(OSSL_CMP_CTX_set1_secretValue(ctx, secret,
|
---|
156 | sizeof(secret)))) {
|
---|
157 | tear_down(fixture);
|
---|
158 | fixture = NULL;
|
---|
159 | }
|
---|
160 | EXECUTE_TEST(execute_certreq_create_test, tear_down);
|
---|
161 | return result;
|
---|
162 | }
|
---|
163 |
|
---|
164 | static int test_cmp_create_ir_protection_fails(void)
|
---|
165 | {
|
---|
166 | SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
|
---|
167 | fixture->bodytype = OSSL_CMP_PKIBODY_IR;
|
---|
168 | fixture->err_code = -1;
|
---|
169 | fixture->expected = 0;
|
---|
170 | if (!TEST_true(OSSL_CMP_CTX_set1_pkey(fixture->cmp_ctx, newkey))
|
---|
171 | || !TEST_true(SET_OPT_UNPROTECTED_SEND(fixture->cmp_ctx, 0))
|
---|
172 | /* newkey used by default for signing does not match cert: */
|
---|
173 | || !TEST_true(OSSL_CMP_CTX_set1_cert(fixture->cmp_ctx, cert))) {
|
---|
174 | tear_down(fixture);
|
---|
175 | fixture = NULL;
|
---|
176 | }
|
---|
177 | EXECUTE_TEST(execute_certreq_create_test, tear_down);
|
---|
178 | return result;
|
---|
179 | }
|
---|
180 |
|
---|
181 | static int test_cmp_create_cr_without_key(void)
|
---|
182 | {
|
---|
183 | SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
|
---|
184 | fixture->bodytype = OSSL_CMP_PKIBODY_CR;
|
---|
185 | fixture->err_code = -1;
|
---|
186 | fixture->expected = 0;
|
---|
187 | EXECUTE_TEST(execute_certreq_create_test, tear_down);
|
---|
188 | return result;
|
---|
189 | }
|
---|
190 |
|
---|
191 | static int test_cmp_create_cr(void)
|
---|
192 | {
|
---|
193 | SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
|
---|
194 | fixture->bodytype = OSSL_CMP_PKIBODY_CR;
|
---|
195 | fixture->err_code = -1;
|
---|
196 | fixture->expected = 1;
|
---|
197 | if (!TEST_true(set1_newPkey(fixture->cmp_ctx, newkey))) {
|
---|
198 | tear_down(fixture);
|
---|
199 | fixture = NULL;
|
---|
200 | }
|
---|
201 | EXECUTE_TEST(execute_certreq_create_test, tear_down);
|
---|
202 | return result;
|
---|
203 | }
|
---|
204 |
|
---|
205 | static int test_cmp_create_certreq_with_invalid_bodytype(void)
|
---|
206 | {
|
---|
207 | SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
|
---|
208 | fixture->bodytype = OSSL_CMP_PKIBODY_RR;
|
---|
209 | fixture->err_code = -1;
|
---|
210 | fixture->expected = 0;
|
---|
211 | if (!TEST_true(set1_newPkey(fixture->cmp_ctx, newkey))) {
|
---|
212 | tear_down(fixture);
|
---|
213 | fixture = NULL;
|
---|
214 | }
|
---|
215 | EXECUTE_TEST(execute_certreq_create_test, tear_down);
|
---|
216 | return result;
|
---|
217 | }
|
---|
218 |
|
---|
219 | static int test_cmp_create_p10cr(void)
|
---|
220 | {
|
---|
221 | OSSL_CMP_CTX *ctx;
|
---|
222 | X509_REQ *p10cr = NULL;
|
---|
223 |
|
---|
224 | SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
|
---|
225 | ctx = fixture->cmp_ctx;
|
---|
226 | fixture->bodytype = OSSL_CMP_PKIBODY_P10CR;
|
---|
227 | fixture->err_code = CMP_R_ERROR_CREATING_CERTREQ;
|
---|
228 | fixture->expected = 1;
|
---|
229 | if (!TEST_ptr(p10cr = load_csr_der(pkcs10_f, libctx))
|
---|
230 | || !TEST_true(set1_newPkey(ctx, newkey))
|
---|
231 | || !TEST_true(OSSL_CMP_CTX_set1_p10CSR(ctx, p10cr))) {
|
---|
232 | tear_down(fixture);
|
---|
233 | fixture = NULL;
|
---|
234 | }
|
---|
235 | X509_REQ_free(p10cr);
|
---|
236 | EXECUTE_TEST(execute_certreq_create_test, tear_down);
|
---|
237 | return result;
|
---|
238 | }
|
---|
239 |
|
---|
240 | static int test_cmp_create_p10cr_null(void)
|
---|
241 | {
|
---|
242 | SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
|
---|
243 | fixture->bodytype = OSSL_CMP_PKIBODY_P10CR;
|
---|
244 | fixture->err_code = CMP_R_ERROR_CREATING_CERTREQ;
|
---|
245 | fixture->expected = 0;
|
---|
246 | if (!TEST_true(set1_newPkey(fixture->cmp_ctx, newkey))) {
|
---|
247 | tear_down(fixture);
|
---|
248 | fixture = NULL;
|
---|
249 | }
|
---|
250 | EXECUTE_TEST(execute_certreq_create_test, tear_down);
|
---|
251 | return result;
|
---|
252 | }
|
---|
253 |
|
---|
254 | static int test_cmp_create_kur(void)
|
---|
255 | {
|
---|
256 | SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
|
---|
257 | fixture->bodytype = OSSL_CMP_PKIBODY_KUR;
|
---|
258 | fixture->err_code = -1;
|
---|
259 | fixture->expected = 1;
|
---|
260 | if (!TEST_true(set1_newPkey(fixture->cmp_ctx, newkey))
|
---|
261 | || !TEST_true(OSSL_CMP_CTX_set1_oldCert(fixture->cmp_ctx, cert))) {
|
---|
262 | tear_down(fixture);
|
---|
263 | fixture = NULL;
|
---|
264 | }
|
---|
265 | EXECUTE_TEST(execute_certreq_create_test, tear_down);
|
---|
266 | return result;
|
---|
267 | }
|
---|
268 |
|
---|
269 | static int test_cmp_create_kur_without_oldcert(void)
|
---|
270 | {
|
---|
271 | SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
|
---|
272 | fixture->bodytype = OSSL_CMP_PKIBODY_KUR;
|
---|
273 | fixture->err_code = -1;
|
---|
274 | fixture->expected = 0;
|
---|
275 | if (!TEST_true(set1_newPkey(fixture->cmp_ctx, newkey))) {
|
---|
276 | tear_down(fixture);
|
---|
277 | fixture = NULL;
|
---|
278 | }
|
---|
279 | EXECUTE_TEST(execute_certreq_create_test, tear_down);
|
---|
280 | return result;
|
---|
281 | }
|
---|
282 |
|
---|
283 | static int test_cmp_create_certconf(void)
|
---|
284 | {
|
---|
285 | SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
|
---|
286 | fixture->fail_info = 0;
|
---|
287 | fixture->expected = 1;
|
---|
288 | if (!TEST_true(ossl_cmp_ctx_set0_newCert(fixture->cmp_ctx,
|
---|
289 | X509_dup(cert)))) {
|
---|
290 | tear_down(fixture);
|
---|
291 | fixture = NULL;
|
---|
292 | }
|
---|
293 | EXECUTE_TEST(execute_certconf_create_test, tear_down);
|
---|
294 | return result;
|
---|
295 | }
|
---|
296 |
|
---|
297 | static int test_cmp_create_certconf_badAlg(void)
|
---|
298 | {
|
---|
299 | SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
|
---|
300 | fixture->fail_info = 1 << OSSL_CMP_PKIFAILUREINFO_badAlg;
|
---|
301 | fixture->expected = 1;
|
---|
302 | if (!TEST_true(ossl_cmp_ctx_set0_newCert(fixture->cmp_ctx,
|
---|
303 | X509_dup(cert)))) {
|
---|
304 | tear_down(fixture);
|
---|
305 | fixture = NULL;
|
---|
306 | }
|
---|
307 | EXECUTE_TEST(execute_certconf_create_test, tear_down);
|
---|
308 | return result;
|
---|
309 | }
|
---|
310 |
|
---|
311 | static int test_cmp_create_certconf_fail_info_max(void)
|
---|
312 | {
|
---|
313 | SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
|
---|
314 | fixture->fail_info = 1 << OSSL_CMP_PKIFAILUREINFO_MAX;
|
---|
315 | fixture->expected = 1;
|
---|
316 | if (!TEST_true(ossl_cmp_ctx_set0_newCert(fixture->cmp_ctx,
|
---|
317 | X509_dup(cert)))) {
|
---|
318 | tear_down(fixture);
|
---|
319 | fixture = NULL;
|
---|
320 | }
|
---|
321 | EXECUTE_TEST(execute_certconf_create_test, tear_down);
|
---|
322 | return result;
|
---|
323 | }
|
---|
324 |
|
---|
325 | static int test_cmp_create_error_msg(void)
|
---|
326 | {
|
---|
327 | SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
|
---|
328 | fixture->si = OSSL_CMP_STATUSINFO_new(OSSL_CMP_PKISTATUS_rejection,
|
---|
329 | OSSL_CMP_PKIFAILUREINFO_systemFailure,
|
---|
330 | NULL);
|
---|
331 | fixture->err_code = -1;
|
---|
332 | fixture->expected = 1; /* expected: message creation is successful */
|
---|
333 | if (!TEST_true(set1_newPkey(fixture->cmp_ctx, newkey))) {
|
---|
334 | tear_down(fixture);
|
---|
335 | fixture = NULL;
|
---|
336 | }
|
---|
337 | EXECUTE_TEST(execute_errormsg_create_test, tear_down);
|
---|
338 | return result;
|
---|
339 | }
|
---|
340 |
|
---|
341 |
|
---|
342 | static int test_cmp_create_pollreq(void)
|
---|
343 | {
|
---|
344 | SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
|
---|
345 | fixture->expected = 1;
|
---|
346 | EXECUTE_TEST(execute_pollreq_create_test, tear_down);
|
---|
347 | return result;
|
---|
348 | }
|
---|
349 |
|
---|
350 | static int test_cmp_create_rr(void)
|
---|
351 | {
|
---|
352 | SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
|
---|
353 | fixture->expected = 1;
|
---|
354 | if (!TEST_true(OSSL_CMP_CTX_set1_oldCert(fixture->cmp_ctx, cert))) {
|
---|
355 | tear_down(fixture);
|
---|
356 | fixture = NULL;
|
---|
357 | }
|
---|
358 | EXECUTE_TEST(execute_rr_create_test, tear_down);
|
---|
359 | return result;
|
---|
360 | }
|
---|
361 |
|
---|
362 | static int test_cmp_create_genm(void)
|
---|
363 | {
|
---|
364 | OSSL_CMP_ITAV *iv = NULL;
|
---|
365 |
|
---|
366 | SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
|
---|
367 | fixture->expected = 1;
|
---|
368 | iv = OSSL_CMP_ITAV_create(OBJ_nid2obj(NID_id_it_implicitConfirm), NULL);
|
---|
369 | if (!TEST_ptr(iv)
|
---|
370 | || !TEST_true(OSSL_CMP_CTX_push0_genm_ITAV(fixture->cmp_ctx, iv))) {
|
---|
371 | OSSL_CMP_ITAV_free(iv);
|
---|
372 | tear_down(fixture);
|
---|
373 | fixture = NULL;
|
---|
374 | }
|
---|
375 |
|
---|
376 | EXECUTE_TEST(execute_genm_create_test, tear_down);
|
---|
377 | return result;
|
---|
378 | }
|
---|
379 |
|
---|
380 | static int execute_certrep_create(CMP_MSG_TEST_FIXTURE *fixture)
|
---|
381 | {
|
---|
382 | OSSL_CMP_CTX *ctx = fixture->cmp_ctx;
|
---|
383 | OSSL_CMP_CERTREPMESSAGE *crepmsg = OSSL_CMP_CERTREPMESSAGE_new();
|
---|
384 | OSSL_CMP_CERTRESPONSE *read_cresp, *cresp = OSSL_CMP_CERTRESPONSE_new();
|
---|
385 | EVP_PKEY *privkey;
|
---|
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 | privkey = OSSL_CMP_CTX_get0_newPkey(ctx, 1); /* may be NULL */
|
---|
408 | certfromresp = ossl_cmp_certresponse_get1_cert(read_cresp, ctx, privkey);
|
---|
409 | if (certfromresp == NULL || !TEST_int_eq(X509_cmp(cert, certfromresp), 0))
|
---|
410 | goto err;
|
---|
411 |
|
---|
412 | res = 1;
|
---|
413 | err:
|
---|
414 | X509_free(certfromresp);
|
---|
415 | OSSL_CMP_CERTRESPONSE_free(cresp);
|
---|
416 | OSSL_CMP_CERTREPMESSAGE_free(crepmsg);
|
---|
417 | return res;
|
---|
418 | }
|
---|
419 |
|
---|
420 | static int test_cmp_create_certrep(void)
|
---|
421 | {
|
---|
422 | SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
|
---|
423 | EXECUTE_TEST(execute_certrep_create, tear_down);
|
---|
424 | return result;
|
---|
425 | }
|
---|
426 |
|
---|
427 |
|
---|
428 | static int execute_rp_create(CMP_MSG_TEST_FIXTURE *fixture)
|
---|
429 | {
|
---|
430 | OSSL_CMP_PKISI *si = OSSL_CMP_STATUSINFO_new(33, 44, "a text");
|
---|
431 | X509_NAME *issuer = X509_NAME_new();
|
---|
432 | ASN1_INTEGER *serial = ASN1_INTEGER_new();
|
---|
433 | OSSL_CRMF_CERTID *cid = NULL;
|
---|
434 | OSSL_CMP_MSG *rpmsg = NULL;
|
---|
435 | int res = 0;
|
---|
436 |
|
---|
437 | if (si == NULL || issuer == NULL || serial == NULL)
|
---|
438 | goto err;
|
---|
439 |
|
---|
440 | if (!X509_NAME_add_entry_by_txt(issuer, "CN", MBSTRING_ASC,
|
---|
441 | (unsigned char *)"The Issuer", -1, -1, 0)
|
---|
442 | || !ASN1_INTEGER_set(serial, 99)
|
---|
443 | || (cid = OSSL_CRMF_CERTID_gen(issuer, serial)) == NULL
|
---|
444 | || (rpmsg = ossl_cmp_rp_new(fixture->cmp_ctx, si, cid, 1)) == NULL)
|
---|
445 | goto err;
|
---|
446 |
|
---|
447 | if (!TEST_ptr(ossl_cmp_revrepcontent_get_CertId(rpmsg->body->value.rp, 0)))
|
---|
448 | goto err;
|
---|
449 |
|
---|
450 | if (!TEST_ptr(ossl_cmp_revrepcontent_get_pkisi(rpmsg->body->value.rp, 0)))
|
---|
451 | goto err;
|
---|
452 |
|
---|
453 | res = 1;
|
---|
454 | err:
|
---|
455 | ASN1_INTEGER_free(serial);
|
---|
456 | X509_NAME_free(issuer);
|
---|
457 | OSSL_CRMF_CERTID_free(cid);
|
---|
458 | OSSL_CMP_PKISI_free(si);
|
---|
459 | OSSL_CMP_MSG_free(rpmsg);
|
---|
460 | return res;
|
---|
461 | }
|
---|
462 |
|
---|
463 | static int test_cmp_create_rp(void)
|
---|
464 | {
|
---|
465 | SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
|
---|
466 | EXECUTE_TEST(execute_rp_create, tear_down);
|
---|
467 | return result;
|
---|
468 | }
|
---|
469 |
|
---|
470 | static int execute_pollrep_create(CMP_MSG_TEST_FIXTURE *fixture)
|
---|
471 | {
|
---|
472 | OSSL_CMP_MSG *pollrep;
|
---|
473 | int res = 0;
|
---|
474 |
|
---|
475 | pollrep = ossl_cmp_pollRep_new(fixture->cmp_ctx, 77, 2000);
|
---|
476 | if (!TEST_ptr(pollrep))
|
---|
477 | return 0;
|
---|
478 | if (!TEST_ptr(ossl_cmp_pollrepcontent_get0_pollrep(pollrep->body->
|
---|
479 | value.pollRep, 77)))
|
---|
480 | goto err;
|
---|
481 | if (!TEST_ptr_null(ossl_cmp_pollrepcontent_get0_pollrep(pollrep->body->
|
---|
482 | value.pollRep, 88)))
|
---|
483 | goto err;
|
---|
484 |
|
---|
485 | res = 1;
|
---|
486 | err:
|
---|
487 | OSSL_CMP_MSG_free(pollrep);
|
---|
488 | return res;
|
---|
489 | }
|
---|
490 |
|
---|
491 | static int test_cmp_create_pollrep(void)
|
---|
492 | {
|
---|
493 | SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
|
---|
494 | EXECUTE_TEST(execute_pollrep_create, tear_down);
|
---|
495 | return result;
|
---|
496 | }
|
---|
497 |
|
---|
498 | static int test_cmp_pkimessage_create(int bodytype)
|
---|
499 | {
|
---|
500 | X509_REQ *p10cr = NULL;
|
---|
501 |
|
---|
502 | SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
|
---|
503 |
|
---|
504 | switch (fixture->bodytype = bodytype) {
|
---|
505 | case OSSL_CMP_PKIBODY_P10CR:
|
---|
506 | fixture->expected = 1;
|
---|
507 | p10cr = load_csr_der(pkcs10_f, libctx);
|
---|
508 | if (!TEST_true(OSSL_CMP_CTX_set1_p10CSR(fixture->cmp_ctx, p10cr))) {
|
---|
509 | tear_down(fixture);
|
---|
510 | fixture = NULL;
|
---|
511 | }
|
---|
512 | X509_REQ_free(p10cr);
|
---|
513 | break;
|
---|
514 | case OSSL_CMP_PKIBODY_IR:
|
---|
515 | case OSSL_CMP_PKIBODY_IP:
|
---|
516 | case OSSL_CMP_PKIBODY_CR:
|
---|
517 | case OSSL_CMP_PKIBODY_CP:
|
---|
518 | case OSSL_CMP_PKIBODY_KUR:
|
---|
519 | case OSSL_CMP_PKIBODY_KUP:
|
---|
520 | case OSSL_CMP_PKIBODY_RR:
|
---|
521 | case OSSL_CMP_PKIBODY_RP:
|
---|
522 | case OSSL_CMP_PKIBODY_PKICONF:
|
---|
523 | case OSSL_CMP_PKIBODY_GENM:
|
---|
524 | case OSSL_CMP_PKIBODY_GENP:
|
---|
525 | case OSSL_CMP_PKIBODY_ERROR:
|
---|
526 | case OSSL_CMP_PKIBODY_CERTCONF:
|
---|
527 | case OSSL_CMP_PKIBODY_POLLREQ:
|
---|
528 | case OSSL_CMP_PKIBODY_POLLREP:
|
---|
529 | fixture->expected = 1;
|
---|
530 | break;
|
---|
531 | default:
|
---|
532 | fixture->expected = 0;
|
---|
533 | break;
|
---|
534 | }
|
---|
535 |
|
---|
536 | EXECUTE_TEST(execute_pkimessage_create_test, tear_down);
|
---|
537 | return result;
|
---|
538 | }
|
---|
539 |
|
---|
540 | void cleanup_tests(void)
|
---|
541 | {
|
---|
542 | EVP_PKEY_free(newkey);
|
---|
543 | X509_free(cert);
|
---|
544 | OSSL_LIB_CTX_free(libctx);
|
---|
545 | }
|
---|
546 |
|
---|
547 | #define USAGE "new.key server.crt pkcs10.der module_name [module_conf_file]\n"
|
---|
548 | OPT_TEST_DECLARE_USAGE(USAGE)
|
---|
549 |
|
---|
550 | int setup_tests(void)
|
---|
551 | {
|
---|
552 | if (!test_skip_common_options()) {
|
---|
553 | TEST_error("Error parsing test options\n");
|
---|
554 | return 0;
|
---|
555 | }
|
---|
556 |
|
---|
557 | if (!TEST_ptr(newkey_f = test_get_argument(0))
|
---|
558 | || !TEST_ptr(server_cert_f = test_get_argument(1))
|
---|
559 | || !TEST_ptr(pkcs10_f = test_get_argument(2))) {
|
---|
560 | TEST_error("usage: cmp_msg_test %s", USAGE);
|
---|
561 | return 0;
|
---|
562 | }
|
---|
563 |
|
---|
564 | if (!test_arg_libctx(&libctx, &default_null_provider, &provider, 3, USAGE))
|
---|
565 | return 0;
|
---|
566 |
|
---|
567 | if (!TEST_ptr(newkey = load_pkey_pem(newkey_f, libctx))
|
---|
568 | || !TEST_ptr(cert = load_cert_pem(server_cert_f, libctx))
|
---|
569 | || !TEST_int_eq(1, RAND_bytes_ex(libctx, ref, sizeof(ref), 0))) {
|
---|
570 | cleanup_tests();
|
---|
571 | return 0;
|
---|
572 | }
|
---|
573 |
|
---|
574 | /* Message creation tests */
|
---|
575 | ADD_TEST(test_cmp_create_certreq_with_invalid_bodytype);
|
---|
576 | ADD_TEST(test_cmp_create_ir_protection_fails);
|
---|
577 | ADD_TEST(test_cmp_create_ir_protection_set);
|
---|
578 | ADD_TEST(test_cmp_create_error_msg);
|
---|
579 | ADD_TEST(test_cmp_create_certconf);
|
---|
580 | ADD_TEST(test_cmp_create_certconf_badAlg);
|
---|
581 | ADD_TEST(test_cmp_create_certconf_fail_info_max);
|
---|
582 | ADD_TEST(test_cmp_create_kur);
|
---|
583 | ADD_TEST(test_cmp_create_kur_without_oldcert);
|
---|
584 | ADD_TEST(test_cmp_create_cr);
|
---|
585 | ADD_TEST(test_cmp_create_cr_without_key);
|
---|
586 | ADD_TEST(test_cmp_create_p10cr);
|
---|
587 | ADD_TEST(test_cmp_create_p10cr_null);
|
---|
588 | ADD_TEST(test_cmp_create_pollreq);
|
---|
589 | ADD_TEST(test_cmp_create_rr);
|
---|
590 | ADD_TEST(test_cmp_create_rp);
|
---|
591 | ADD_TEST(test_cmp_create_genm);
|
---|
592 | ADD_TEST(test_cmp_create_certrep);
|
---|
593 | ADD_TEST(test_cmp_create_pollrep);
|
---|
594 | ADD_ALL_TESTS_NOSUBTEST(test_cmp_pkimessage_create,
|
---|
595 | OSSL_CMP_PKIBODY_POLLREP + 1);
|
---|
596 | return 1;
|
---|
597 | }
|
---|