Debian Codegraph @

debian/rust-vendor/openssl-src/openssl/crypto/cmp/cmp_client.c

raw
1 /*
2 * Copyright 2007-2026 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_local.h"
13
14 #define IS_CREP(t) ((t) == OSSL_CMP_PKIBODY_IP || (t) == OSSL_CMP_PKIBODY_CP \
15 || (t) == OSSL_CMP_PKIBODY_KUP)
16
17 /*-
18 * Evaluate whether there's an exception (violating the standard) configured for
19 * handling negative responses without protection or with invalid protection.
20 * Returns 1 on acceptance, 0 on rejection, or -1 on (internal) error.
21 */
22 static int unprotected_exception(const OSSL_CMP_CTX *ctx,
23 const OSSL_CMP_MSG *rep,
24 int invalid_protection,
25 ossl_unused int expected_type)
26 {
27 int rcvd_type = OSSL_CMP_MSG_get_bodytype(rep /* may be NULL */);
28 const char *msg_type = NULL;
29
30 if (!ossl_assert(ctx != NULL && rep != NULL))
31 return -1;
32
33 if (!ctx->unprotectedErrors)
34 return 0;
35
36 switch (rcvd_type) {
37 case OSSL_CMP_PKIBODY_ERROR:
38 msg_type = "error response";
39 break;
40 case OSSL_CMP_PKIBODY_RP: {
41 OSSL_CMP_PKISI *si = ossl_cmp_revrepcontent_get_pkisi(rep->body->value.rp,
42 OSSL_CMP_REVREQSID);
43
44 if (si == NULL)
45 return -1;
46 if (ossl_cmp_pkisi_get_status(si) == OSSL_CMP_PKISTATUS_rejection)
47 msg_type = "revocation response message with rejection status";
48 break;
49 }
50 case OSSL_CMP_PKIBODY_PKICONF:
51 msg_type = "PKI Confirmation message";
52 break;
53 default:
54 if (IS_CREP(rcvd_type)) {
55 int any_rid = OSSL_CMP_CERTREQID_NONE;
56 OSSL_CMP_CERTREPMESSAGE *crepmsg = rep->body->value.ip;
57 OSSL_CMP_CERTRESPONSE *crep = ossl_cmp_certrepmessage_get0_certresponse(crepmsg, any_rid);
58
59 if (sk_OSSL_CMP_CERTRESPONSE_num(crepmsg->response) > 1)
60 return -1;
61 if (crep == NULL)
62 return -1;
63 if (ossl_cmp_pkisi_get_status(crep->status)
64 == OSSL_CMP_PKISTATUS_rejection)
65 msg_type = "CertRepMessage with rejection status";
66 }
67 }
68 if (msg_type == NULL)
69 return 0;
70 ossl_cmp_log2(WARN, ctx, "ignoring %s protection of %s",
71 invalid_protection ? "invalid" : "missing", msg_type);
72 return 1;
73 }
74
75 /* Save error info from PKIStatusInfo field of a certresponse into ctx */
76 static int save_statusInfo(OSSL_CMP_CTX *ctx, OSSL_CMP_PKISI *si)
77 {
78 int i;
79 OSSL_CMP_PKIFREETEXT *ss;
80
81 if (!ossl_assert(ctx != NULL && si != NULL))
82 return 0;
83
84 ctx->status = ossl_cmp_pkisi_get_status(si);
85 if (ctx->status < OSSL_CMP_PKISTATUS_accepted)
86 return 0;
87
88 ctx->failInfoCode = ossl_cmp_pkisi_get_pkifailureinfo(si);
89
90 if (!ossl_cmp_ctx_set0_statusString(ctx, sk_ASN1_UTF8STRING_new_null())
91 || (ctx->statusString == NULL))
92 return 0;
93
94 ss = si->statusString; /* may be NULL */
95 for (i = 0; i < sk_ASN1_UTF8STRING_num(ss); i++) {
96 ASN1_UTF8STRING *str = sk_ASN1_UTF8STRING_value(ss, i);
97 ASN1_UTF8STRING *dup = ASN1_STRING_dup(str);
98
99 if (dup == NULL || !sk_ASN1_UTF8STRING_push(ctx->statusString, dup)) {
100 ASN1_UTF8STRING_free(dup);
101 return 0;
102 }
103 }
104 return 1;
105 }
106
107 static int is_crep_with_waiting(const OSSL_CMP_MSG *resp, int rid)
108 {
109 OSSL_CMP_CERTREPMESSAGE *crepmsg;
110 OSSL_CMP_CERTRESPONSE *crep;
111 int bt = OSSL_CMP_MSG_get_bodytype(resp);
112
113 if (!IS_CREP(bt))
114 return 0;
115
116 crepmsg = resp->body->value.ip; /* same for cp and kup */
117 crep = ossl_cmp_certrepmessage_get0_certresponse(crepmsg, rid);
118
119 return (crep != NULL
120 && ossl_cmp_pkisi_get_status(crep->status)
121 == OSSL_CMP_PKISTATUS_waiting);
122 }
123
124 /*-
125 * Perform the generic aspects of sending a request and receiving a response.
126 * Returns 1 on success and provides the received PKIMESSAGE in *rep.
127 * Returns 0 on error.
128 * Regardless of success, caller is responsible for freeing *rep (unless NULL).
129 */
130 static int send_receive_check(OSSL_CMP_CTX *ctx, const OSSL_CMP_MSG *req,
131 OSSL_CMP_MSG **rep, int expected_type)
132 {
133 int begin_transaction = expected_type != OSSL_CMP_PKIBODY_POLLREP
134 && expected_type != OSSL_CMP_PKIBODY_PKICONF;
135 const char *req_type_str = ossl_cmp_bodytype_to_string(OSSL_CMP_MSG_get_bodytype(req));
136 const char *expected_type_str = ossl_cmp_bodytype_to_string(expected_type);
137 int bak_msg_timeout = ctx->msg_timeout;
138 int bt;
139 time_t now = time(NULL);
140 int time_left;
141 OSSL_CMP_transfer_cb_t transfer_cb = ctx->transfer_cb;
142
143 ctx->status = OSSL_CMP_PKISTATUS_trans;
144 #ifndef OPENSSL_NO_HTTP
145 if (transfer_cb == NULL)
146 transfer_cb = OSSL_CMP_MSG_http_perform;
147 #endif
148 *rep = NULL;
149
150 if (ctx->total_timeout != 0 /* not waiting indefinitely */) {
151 if (begin_transaction)
152 ctx->end_time = now + ctx->total_timeout;
153 if (now >= ctx->end_time) {
154 ERR_raise(ERR_LIB_CMP, CMP_R_TOTAL_TIMEOUT);
155 return 0;
156 }
157 if (!ossl_assert(ctx->end_time - now < INT_MAX)) {
158 /* actually cannot happen due to assignment in initial_certreq() */
159 ERR_raise(ERR_LIB_CMP, CMP_R_INVALID_ARGS);
160 return 0;
161 }
162 time_left = (int)(ctx->end_time - now);
163 if (ctx->msg_timeout == 0 || time_left < ctx->msg_timeout)
164 ctx->msg_timeout = time_left;
165 }
166
167 /* should print error queue since transfer_cb may call ERR_clear_error() */
168 OSSL_CMP_CTX_print_errors(ctx);
169
170 if (ctx->server != NULL || ctx->transfer_cb != NULL)
171 ossl_cmp_log1(INFO, ctx, "sending %s", req_type_str);
172
173 *rep = (*transfer_cb)(ctx, req);
174 ctx->msg_timeout = bak_msg_timeout;
175
176 if (*rep == NULL) {
177 ERR_raise_data(ERR_LIB_CMP,
178 ctx->total_timeout != 0 && time(NULL) >= ctx->end_time ? CMP_R_TOTAL_TIMEOUT : CMP_R_TRANSFER_ERROR,
179 "request sent: %s, expected response: %s",
180 req_type_str, expected_type_str);
181 return 0;
182 }
183
184 ctx->status = OSSL_CMP_PKISTATUS_checking_response;
185 bt = OSSL_CMP_MSG_get_bodytype(*rep);
186 /*
187 * The body type in the 'bt' variable is not yet verified.
188 * Still we use this preliminary value already for a progress report because
189 * the following msg verification may also produce log entries and may fail.
190 */
191 ossl_cmp_log2(INFO, ctx, "received %s%s", ossl_cmp_bodytype_to_string(bt),
192 ossl_cmp_is_error_with_waiting(*rep) ? " (waiting)" : "");
193
194 /* copy received extraCerts to ctx->extraCertsIn so they can be retrieved */
195 if (bt != OSSL_CMP_PKIBODY_POLLREP && bt != OSSL_CMP_PKIBODY_PKICONF
196 && !ossl_cmp_ctx_set1_extraCertsIn(ctx, (*rep)->extraCerts))
197 return 0;
198
199 if (!ossl_cmp_msg_check_update(ctx, *rep, unprotected_exception,
200 expected_type))
201 return 0;
202
203 /*
204 * rep can have the expected response type, which during polling is pollRep.
205 * When polling, also any other non-error response (the final response)
206 * is fine here. When not yet polling, delayed delivery may be initiated
207 * by the server returning an error message with 'waiting' status (or a
208 * response message of expected type ip/cp/kup with 'waiting' status).
209 */
210 if (bt == expected_type
211 || (expected_type == OSSL_CMP_PKIBODY_POLLREP
212 ? bt != OSSL_CMP_PKIBODY_ERROR
213 : ossl_cmp_is_error_with_waiting(*rep)))
214 return 1;
215
216 /* received message type is not one of the expected ones (e.g., error) */
217 ERR_raise(ERR_LIB_CMP, bt == OSSL_CMP_PKIBODY_ERROR ? CMP_R_RECEIVED_ERROR : CMP_R_UNEXPECTED_PKIBODY); /* in next line for mkerr.pl */
218
219 if (bt != OSSL_CMP_PKIBODY_ERROR) {
220 ERR_add_error_data(3, "message type is '",
221 ossl_cmp_bodytype_to_string(bt), "'");
222 } else {
223 OSSL_CMP_ERRORMSGCONTENT *emc = (*rep)->body->value.error;
224 OSSL_CMP_PKISI *si = emc->pKIStatusInfo;
225 char buf[OSSL_CMP_PKISI_BUFLEN];
226
227 if (save_statusInfo(ctx, si)
228 && OSSL_CMP_CTX_snprint_PKIStatus(ctx, buf,
229 sizeof(buf))
230 != NULL)
231 ERR_add_error_data(1, buf);
232 if (emc->errorCode != NULL
233 && BIO_snprintf(buf, sizeof(buf), "; errorCode: %08lX",
234 ASN1_INTEGER_get(emc->errorCode))
235 > 0)
236 ERR_add_error_data(1, buf);
237 if (emc->errorDetails != NULL) {
238 char *text = ossl_sk_ASN1_UTF8STRING2text(emc->errorDetails, ", ",
239 OSSL_CMP_PKISI_BUFLEN - 1);
240
241 if (text != NULL && *text != '\0')
242 ERR_add_error_data(2, "; errorDetails: ", text);
243 OPENSSL_free(text);
244 }
245 if (ctx->status != OSSL_CMP_PKISTATUS_rejection) {
246 ERR_raise(ERR_LIB_CMP, CMP_R_UNEXPECTED_PKISTATUS);
247 if (ctx->status == OSSL_CMP_PKISTATUS_waiting)
248 ctx->status = OSSL_CMP_PKISTATUS_rejection;
249 }
250 }
251 return 0;
252 }
253
254 /*-
255 * When a 'waiting' PKIStatus has been received, this function is used to
256 * poll, which should yield a pollRep or the final response.
257 * On receiving a pollRep, which includes a checkAfter value, it return this
258 * value if sleep == 0, else it sleeps as long as indicated and retries.
259 *
260 * A transaction timeout is enabled if ctx->total_timeout is != 0.
261 * In this case polling will continue until the timeout is reached and then
262 * polling is done a last time even if this is before the "checkAfter" time.
263 *
264 * Returns -1 on receiving pollRep if sleep == 0, setting the checkAfter value.
265 * Returns 1 on success and provides the received PKIMESSAGE in *rep.
266 * In this case the caller is responsible for freeing *rep.
267 * Returns 0 on error (which includes the cases that timeout has been reached
268 * or a response with 'waiting' status has been received).
269 */
270 static int poll_for_response(OSSL_CMP_CTX *ctx, int sleep, int rid,
271 OSSL_CMP_MSG **rep, int *checkAfter)
272 {
273 OSSL_CMP_MSG *preq = NULL;
274 OSSL_CMP_MSG *prep = NULL;
275
276 ossl_cmp_info(ctx,
277 "received 'waiting' PKIStatus, starting to poll for response");
278 *rep = NULL;
279 for (;;) {
280 int bak = ctx->status;
281
282 ctx->status = OSSL_CMP_PKISTATUS_request;
283 if ((preq = ossl_cmp_pollReq_new(ctx, rid)) == NULL)
284 goto err;
285
286 if (!send_receive_check(ctx, preq, &prep, OSSL_CMP_PKIBODY_POLLREP))
287 goto err;
288 ctx->status = bak;
289
290 /* handle potential pollRep */
291 if (OSSL_CMP_MSG_get_bodytype(prep) == OSSL_CMP_PKIBODY_POLLREP) {
292 OSSL_CMP_POLLREPCONTENT *prc = prep->body->value.pollRep;
293 OSSL_CMP_POLLREP *pollRep = NULL;
294 int64_t check_after;
295 char str[OSSL_CMP_PKISI_BUFLEN];
296 int len;
297
298 if (sk_OSSL_CMP_POLLREP_num(prc) > 1) {
299 ERR_raise(ERR_LIB_CMP, CMP_R_MULTIPLE_RESPONSES_NOT_SUPPORTED);
300 goto err;
301 }
302 pollRep = ossl_cmp_pollrepcontent_get0_pollrep(prc, rid);
303 if (pollRep == NULL)
304 goto err;
305
306 if (!ASN1_INTEGER_get_int64(&check_after, pollRep->checkAfter)) {
307 ERR_raise(ERR_LIB_CMP, CMP_R_BAD_CHECKAFTER_IN_POLLREP);
308 goto err;
309 }
310 if (check_after < 0 || (uint64_t)check_after > (sleep ? ULONG_MAX / 1000 : INT_MAX)) {
311 ERR_raise(ERR_LIB_CMP, CMP_R_CHECKAFTER_OUT_OF_RANGE);
312 if (BIO_snprintf(str, OSSL_CMP_PKISI_BUFLEN, "value = %jd",
313 check_after)
314 >= 0)
315 ERR_add_error_data(1, str);
316 goto err;
317 }
318
319 if (pollRep->reason == NULL
320 || (len = BIO_snprintf(str, OSSL_CMP_PKISI_BUFLEN,
321 " with reason = '"))
322 < 0) {
323 *str = '\0';
324 } else {
325 char *text = ossl_sk_ASN1_UTF8STRING2text(pollRep->reason, ", ",
326 sizeof(str) - len - 2);
327
328 if (text == NULL
329 || BIO_snprintf(str + len, sizeof(str) - len,
330 "%s'", text)
331 < 0)
332 *str = '\0';
333 OPENSSL_free(text);
334 }
335 ossl_cmp_log2(INFO, ctx,
336 "received polling response%s; checkAfter = %ld seconds",
337 str, check_after);
338
339 if (ctx->total_timeout != 0) { /* timeout is not infinite */
340 const int exp = OSSL_CMP_EXPECTED_RESP_TIME;
341 int64_t time_left = (int64_t)(ctx->end_time - exp - time(NULL));
342
343 if (time_left <= 0) {
344 ctx->status = OSSL_CMP_PKISTATUS_trans;
345 ERR_raise(ERR_LIB_CMP, CMP_R_TOTAL_TIMEOUT);
346 goto err;
347 }
348 if (time_left < check_after)
349 check_after = time_left;
350 /* poll one last time just when timeout was reached */
351 }
352
353 OSSL_CMP_MSG_free(preq);
354 preq = NULL;
355 OSSL_CMP_MSG_free(prep);
356 prep = NULL;
357 if (sleep) {
358 OSSL_sleep((unsigned long)(1000 * check_after));
359 } else {
360 if (checkAfter != NULL)
361 *checkAfter = (int)check_after;
362 return -1; /* exits the loop */
363 }
364 } else if (is_crep_with_waiting(prep, rid)
365 || ossl_cmp_is_error_with_waiting(prep)) {
366 /* received status must not be 'waiting' */
367 (void)ossl_cmp_exchange_error(ctx, OSSL_CMP_PKISTATUS_rejection,
368 OSSL_CMP_CTX_FAILINFO_badRequest,
369 "polling already started",
370 0 /* errorCode */, NULL);
371 ERR_raise(ERR_LIB_CMP, CMP_R_UNEXPECTED_PKISTATUS);
372 goto err;
373 } else {
374 ossl_cmp_info(ctx, "received final response after polling");
375 if (!ossl_cmp_ctx_set1_first_senderNonce(ctx, NULL))
376 goto err;
377 break;
378 }
379 }
380 if (prep == NULL)
381 goto err;
382
383 OSSL_CMP_MSG_free(preq);
384 *rep = prep;
385
386 return 1;
387 err:
388 (void)ossl_cmp_ctx_set1_first_senderNonce(ctx, NULL);
389 OSSL_CMP_MSG_free(preq);
390 OSSL_CMP_MSG_free(prep);
391 return 0;
392 }
393
394 static int save_senderNonce_if_waiting(OSSL_CMP_CTX *ctx,
395 const OSSL_CMP_MSG *rep, int rid)
396 {
397 /*
398 * Lightweight CMP Profile section 4.4 states: the senderNonce of the
399 * preceding request message because this value will be needed for checking
400 * the recipNonce of the final response to be received after polling.
401 */
402 if ((is_crep_with_waiting(rep, rid)
403 || ossl_cmp_is_error_with_waiting(rep))
404 && !ossl_cmp_ctx_set1_first_senderNonce(ctx, ctx->senderNonce))
405 return 0;
406
407 return 1;
408 }
409
410 /*
411 * Send request and get response possibly with polling initiated by error msg.
412 * Polling for ip/cp/kup/ with 'waiting' status is handled by cert_response().
413 */
414 static int send_receive_also_delayed(OSSL_CMP_CTX *ctx, const OSSL_CMP_MSG *req,
415 OSSL_CMP_MSG **rep, int expected_type)
416 {
417
418 if (!send_receive_check(ctx, req, rep, expected_type))
419 return 0;
420
421 if (ossl_cmp_is_error_with_waiting(*rep)) {
422 if (!save_senderNonce_if_waiting(ctx, *rep, OSSL_CMP_CERTREQID_NONE))
423 return 0;
424 /* not modifying ctx->status during certConf and error exchanges */
425 if (expected_type != OSSL_CMP_PKIBODY_PKICONF
426 && !save_statusInfo(ctx, (*rep)->body->value.error->pKIStatusInfo))
427 return 0;
428
429 OSSL_CMP_MSG_free(*rep);
430 *rep = NULL;
431
432 if (poll_for_response(ctx, 1 /* can sleep */, OSSL_CMP_CERTREQID_NONE,
433 rep, NULL /* checkAfter */)
434 <= 0) {
435 ERR_raise(ERR_LIB_CMP, CMP_R_POLLING_FAILED);
436 return 0;
437 }
438 }
439 if (OSSL_CMP_MSG_get_bodytype(*rep) != expected_type) {
440 ERR_raise(ERR_LIB_CMP, CMP_R_UNEXPECTED_PKIBODY);
441 return 0;
442 }
443
444 return 1;
445 }
446 /*
447 * Send certConf for IR, CR or KUR sequences and check response,
448 * not modifying ctx->status during the certConf exchange
449 */
450 int ossl_cmp_exchange_certConf(OSSL_CMP_CTX *ctx, int certReqId,
451 int fail_info, const char *txt)
452 {
453 OSSL_CMP_MSG *certConf;
454 OSSL_CMP_MSG *PKIconf = NULL;
455 int res = 0;
456 int bak = ctx->status;
457
458 ctx->status = OSSL_CMP_PKISTATUS_request;
459 /* OSSL_CMP_certConf_new() also checks if all necessary options are set */
460 certConf = ossl_cmp_certConf_new(ctx, certReqId, fail_info, txt);
461 if (certConf == NULL)
462 goto err;
463
464 res = send_receive_also_delayed(ctx, certConf, &PKIconf,
465 OSSL_CMP_PKIBODY_PKICONF);
466
467 if (res)
468 ctx->status = bak;
469
470 err:
471 OSSL_CMP_MSG_free(certConf);
472 OSSL_CMP_MSG_free(PKIconf);
473 return res;
474 }
475
476 /* Send given error and check response */
477 int ossl_cmp_exchange_error(OSSL_CMP_CTX *ctx, int status, int fail_info,
478 const char *txt, int errorCode, const char *details)
479 {
480 OSSL_CMP_MSG *error = NULL;
481 OSSL_CMP_PKISI *si = NULL;
482 OSSL_CMP_MSG *PKIconf = NULL;
483 int res = 0;
484
485 ctx->status = OSSL_CMP_PKISTATUS_request;
486 /* not overwriting ctx->status on error exchange */
487 if ((si = OSSL_CMP_STATUSINFO_new(status, fail_info, txt)) == NULL)
488 goto err;
489 /* ossl_cmp_error_new() also checks if all necessary options are set */
490 if ((error = ossl_cmp_error_new(ctx, si, errorCode, details, 0)) == NULL)
491 goto err;
492
493 res = send_receive_also_delayed(ctx, error,
494 &PKIconf, OSSL_CMP_PKIBODY_PKICONF);
495 ctx->status = OSSL_CMP_PKISTATUS_rejected_by_client;
496
497 err:
498 OSSL_CMP_MSG_free(error);
499 OSSL_CMP_PKISI_free(si);
500 OSSL_CMP_MSG_free(PKIconf);
501 return res;
502 }
503
504 /*-
505 * Retrieve a copy of the certificate, if any, from the given CertResponse.
506 * Take into account PKIStatusInfo of CertResponse in ctx, report it on error.
507 * Returns NULL if not found or on error.
508 */
509 static X509 *get1_cert_status(OSSL_CMP_CTX *ctx, int bodytype,
510 OSSL_CMP_CERTRESPONSE *crep)
511 {
512 char buf[OSSL_CMP_PKISI_BUFLEN];
513 X509 *crt = NULL;
514
515 if (!ossl_assert(ctx != NULL && crep != NULL))
516 return NULL;
517
518 switch (ossl_cmp_pkisi_get_status(crep->status)) {
519 case OSSL_CMP_PKISTATUS_waiting:
520 ossl_cmp_err(ctx,
521 "received \"waiting\" status for cert when actually aiming to extract cert");
522 ERR_raise(ERR_LIB_CMP, CMP_R_ENCOUNTERED_WAITING);
523 goto err;
524 case OSSL_CMP_PKISTATUS_grantedWithMods:
525 ossl_cmp_warn(ctx, "received \"grantedWithMods\" for certificate");
526 break;
527 case OSSL_CMP_PKISTATUS_accepted:
528 break;
529 /* get all information in case of a rejection before going to error */
530 case OSSL_CMP_PKISTATUS_rejection:
531 ossl_cmp_err(ctx, "received \"rejection\" status rather than cert");
532 ERR_raise(ERR_LIB_CMP, CMP_R_REQUEST_REJECTED_BY_SERVER);
533 goto err;
534 case OSSL_CMP_PKISTATUS_revocationWarning:
535 ossl_cmp_warn(ctx,
536 "received \"revocationWarning\" - a revocation of the cert is imminent");
537 break;
538 case OSSL_CMP_PKISTATUS_revocationNotification:
539 ossl_cmp_warn(ctx,
540 "received \"revocationNotification\" - a revocation of the cert has occurred");
541 break;
542 case OSSL_CMP_PKISTATUS_keyUpdateWarning:
543 if (bodytype != OSSL_CMP_PKIBODY_KUR) {
544 ERR_raise(ERR_LIB_CMP, CMP_R_ENCOUNTERED_KEYUPDATEWARNING);
545 goto err;
546 }
547 break;
548 default:
549 ossl_cmp_log1(ERROR, ctx,
550 "received unsupported PKIStatus %d for certificate",
551 ctx->status);
552 ERR_raise(ERR_LIB_CMP, CMP_R_UNKNOWN_PKISTATUS);
553 goto err;
554 }
555 crt = ossl_cmp_certresponse_get1_cert(ctx, crep);
556 if (crt == NULL) /* according to PKIStatus, we can expect a cert */
557 ERR_raise(ERR_LIB_CMP, CMP_R_CERTIFICATE_NOT_FOUND);
558
559 return crt;
560
561 err:
562 if (OSSL_CMP_CTX_snprint_PKIStatus(ctx, buf, sizeof(buf)) != NULL)
563 ERR_add_error_data(1, buf);
564 return NULL;
565 }
566
567 /*-
568 * Callback fn validating that the new certificate can be verified, using
569 * ctx->certConf_cb_arg, which has been initialized using opt_out_trusted, and
570 * ctx->untrusted, which at this point already contains msg->extraCerts.
571 * Returns 0 on acceptance, else a bit field reflecting PKIFailureInfo.
572 * Quoting from RFC 9810 section 5.1. Overall PKI Message:
573 * The extraCerts field can contain certificates that may be useful to
574 * the recipient. For example, this can be used by a CA or RA to
575 * present an end entity with certificates that it needs to verify its
576 * own new certificate (for example, if the CA that issued the end
577 * entity's certificate is not a root CA for the end entity). Note that
578 * this field does not necessarily contain a certification path; the
579 * recipient may have to sort, select from, or otherwise process the
580 * extra certificates in order to use them.
581 * Note: While often handy, there is no hard requirement by CMP that
582 * an EE must be able to validate the certificates it gets enrolled.
583 */
584 int OSSL_CMP_certConf_cb(OSSL_CMP_CTX *ctx, X509 *cert, int fail_info,
585 const char **text)
586 {
587 X509_STORE *out_trusted = OSSL_CMP_CTX_get_certConf_cb_arg(ctx);
588 STACK_OF(X509) *chain = NULL;
589
590 (void)text; /* make (artificial) use of var to prevent compiler warning */
591
592 if (fail_info != 0) /* accept any error flagged by CMP core library */
593 return fail_info;
594
595 if (out_trusted == NULL) {
596 ossl_cmp_debug(ctx, "trying to build chain for newly enrolled cert");
597 chain = X509_build_chain(cert, ctx->untrusted, out_trusted,
598 0, ctx->libctx, ctx->propq);
599 } else {
600 X509_STORE_CTX *csc = X509_STORE_CTX_new_ex(ctx->libctx, ctx->propq);
601
602 ossl_cmp_debug(ctx, "validating newly enrolled cert");
603 if (csc == NULL)
604 goto err;
605 if (!X509_STORE_CTX_init(csc, out_trusted, cert, ctx->untrusted))
606 goto err;
607 /* disable any cert status/revocation checking etc. */
608 X509_VERIFY_PARAM_clear_flags(X509_STORE_CTX_get0_param(csc),
609 ~(X509_V_FLAG_USE_CHECK_TIME
610 | X509_V_FLAG_NO_CHECK_TIME
611 | X509_V_FLAG_PARTIAL_CHAIN
612 | X509_V_FLAG_POLICY_CHECK));
613 if (X509_verify_cert(csc) <= 0)
614 goto err;
615
616 if (!ossl_x509_add_certs_new(&chain, X509_STORE_CTX_get0_chain(csc),
617 X509_ADD_FLAG_UP_REF | X509_ADD_FLAG_NO_DUP
618 | X509_ADD_FLAG_NO_SS)) {
619 sk_X509_free(chain);
620 chain = NULL;
621 }
622 err:
623 X509_STORE_CTX_free(csc);
624 }
625
626 if (sk_X509_num(chain) > 0)
627 X509_free(sk_X509_shift(chain)); /* remove leaf (EE) cert */
628 if (out_trusted != NULL) {
629 if (chain == NULL) {
630 ossl_cmp_err(ctx, "failed to validate newly enrolled cert");
631 fail_info = 1 << OSSL_CMP_PKIFAILUREINFO_incorrectData;
632 } else {
633 ossl_cmp_debug(ctx,
634 "success validating newly enrolled cert");
635 }
636 } else if (chain == NULL) {
637 ossl_cmp_warn(ctx, "could not build approximate chain for newly enrolled cert, resorting to received extraCerts");
638 chain = OSSL_CMP_CTX_get1_extraCertsIn(ctx);
639 } else {
640 ossl_cmp_debug(ctx,
641 "success building approximate chain for newly enrolled cert");
642 }
643 (void)ossl_cmp_ctx_set1_newChain(ctx, chain);
644 OSSL_STACK_OF_X509_free(chain);
645
646 return fail_info;
647 }
648
649 /*-
650 * Perform the generic handling of certificate responses for IR/CR/KUR/P10CR.
651 * |rid| must be OSSL_CMP_CERTREQID_NONE if not available, namely for p10cr
652 * Returns -1 on receiving pollRep if sleep == 0, setting the checkAfter value.
653 * Returns 1 on success and provides the received PKIMESSAGE in *resp.
654 * Returns 0 on error (which includes the case that timeout has been reached).
655 * Regardless of success, caller is responsible for freeing *resp (unless NULL).
656 */
657 static int cert_response(OSSL_CMP_CTX *ctx, int sleep, int rid,
658 OSSL_CMP_MSG **resp, int *checkAfter,
659 ossl_unused int req_type,
660 ossl_unused int expected_type)
661 {
662 EVP_PKEY *rkey = NULL;
663 int fail_info = 0; /* no failure */
664 const char *txt = NULL;
665 OSSL_CMP_CERTREPMESSAGE *crepmsg = NULL;
666 OSSL_CMP_CERTRESPONSE *crep = NULL;
667 OSSL_CMP_certConf_cb_t cb;
668 X509 *cert;
669 char *subj = NULL;
670 int ret = 1;
671 int rcvd_type;
672 OSSL_CMP_PKISI *si;
673
674 if (!ossl_assert(ctx != NULL))
675 return 0;
676
677 retry:
678 rcvd_type = OSSL_CMP_MSG_get_bodytype(*resp);
679 if (IS_CREP(rcvd_type)) {
680 crepmsg = (*resp)->body->value.ip; /* same for cp and kup */
681 if (sk_OSSL_CMP_CERTRESPONSE_num(crepmsg->response) > 1) {
682 ERR_raise(ERR_LIB_CMP, CMP_R_MULTIPLE_RESPONSES_NOT_SUPPORTED);
683 return 0;
684 }
685 crep = ossl_cmp_certrepmessage_get0_certresponse(crepmsg, rid);
686 if (crep == NULL)
687 return 0;
688 si = crep->status;
689
690 if (rid == OSSL_CMP_CERTREQID_NONE) {
691 /* for OSSL_CMP_PKIBODY_P10CR learn CertReqId from response */
692 rid = ossl_cmp_asn1_get_int(crep->certReqId);
693 if (rid < OSSL_CMP_CERTREQID_NONE) {
694 ERR_raise(ERR_LIB_CMP, CMP_R_BAD_REQUEST_ID);
695 return 0;
696 }
697 }
698 } else if (rcvd_type == OSSL_CMP_PKIBODY_ERROR) {
699 si = (*resp)->body->value.error->pKIStatusInfo;
700 } else {
701 ERR_raise(ERR_LIB_CMP, CMP_R_UNEXPECTED_PKIBODY);
702 return 0;
703 }
704
705 if (!save_statusInfo(ctx, si))
706 return 0;
707
708 if (ossl_cmp_pkisi_get_status(si) == OSSL_CMP_PKISTATUS_waiting) {
709 /*
710 * Here we allow both and error message with waiting indication
711 * as well as a certificate response with waiting indication, where
712 * its flavor (ip, cp, or kup) may not strictly match ir/cr/p10cr/kur.
713 */
714 OSSL_CMP_MSG_free(*resp);
715 *resp = NULL;
716 if ((ret = poll_for_response(ctx, sleep, rid, resp, checkAfter)) != 0) {
717 if (ret == -1) /* at this point implies sleep == 0 */
718 return ret; /* waiting */
719 goto retry; /* got some response other than pollRep */
720 } else {
721 ERR_raise(ERR_LIB_CMP, CMP_R_POLLING_FAILED);
722 return 0;
723 }
724 }
725
726 /* at this point, we have received ip/cp/kup/error without waiting */
727 if (rcvd_type == OSSL_CMP_PKIBODY_ERROR) {
728 ERR_raise(ERR_LIB_CMP, CMP_R_RECEIVED_ERROR);
729 return 0;
730 }
731 /* here we are strict on the flavor of ip/cp/kup: must match request */
732 if (rcvd_type != expected_type) {
733 ERR_raise(ERR_LIB_CMP, CMP_R_UNEXPECTED_PKIBODY);
734 return 0;
735 }
736
737 cert = get1_cert_status(ctx, (*resp)->body->type, crep);
738 if (cert == NULL) {
739 ERR_add_error_data(1, "; cannot extract certificate from response");
740 return 0;
741 }
742 if (!ossl_cmp_ctx_set0_newCert(ctx, cert)) {
743 X509_free(cert);
744 return 0;
745 }
746
747 /*
748 * if the CMP server returned certificates in the caPubs field, copy them
749 * to the context so that they can be retrieved if necessary
750 */
751 if (crepmsg != NULL && crepmsg->caPubs != NULL
752 && !ossl_cmp_ctx_set1_caPubs(ctx, crepmsg->caPubs))
753 return 0;
754
755 subj = X509_NAME_oneline(X509_get_subject_name(cert), NULL, 0);
756 rkey = ossl_cmp_ctx_get0_newPubkey(ctx);
757 if (rkey != NULL
758 /* X509_check_private_key() also works if rkey is just public key */
759 && !(X509_check_private_key(ctx->newCert, rkey))) {
760 fail_info = 1 << OSSL_CMP_PKIFAILUREINFO_incorrectData;
761 txt = "public key in new certificate does not match our enrollment key";
762 /*-
763 * not calling (void)ossl_cmp_exchange_error(ctx,
764 * OSSL_CMP_PKISTATUS_rejection, fail_info, txt)
765 * not throwing CMP_R_CERTIFICATE_NOT_ACCEPTED with txt
766 * not returning 0
767 * since we better leave this for the certConf_cb to decide
768 */
769 }
770
771 /*
772 * Execute the certification checking callback function,
773 * which can determine whether to accept a newly enrolled certificate.
774 * It may overrule the pre-decision reflected in 'fail_info' and '*txt'.
775 */
776 cb = ctx->certConf_cb != NULL ? ctx->certConf_cb : OSSL_CMP_certConf_cb;
777 if ((fail_info = cb(ctx, ctx->newCert, fail_info, &txt)) != 0
778 && txt == NULL)
779 txt = "CMP client did not accept it";
780 if (fail_info != 0) /* immediately log error before any certConf exchange */
781 ossl_cmp_log1(ERROR, ctx,
782 "rejecting newly enrolled cert with subject: %s", subj);
783 /*
784 * certConf exchange should better be moved to do_certreq_seq() such that
785 * also more low-level errors with CertReqMessages get reported to server
786 */
787 if (!ctx->disableConfirm
788 && !ossl_cmp_hdr_has_implicitConfirm((*resp)->header)) {
789 if (!ossl_cmp_exchange_certConf(ctx, rid, fail_info, txt))
790 ret = 0;
791 }
792
793 /* not throwing failure earlier as transfer_cb may call ERR_clear_error() */
794 if (fail_info != 0) {
795 ERR_raise_data(ERR_LIB_CMP, CMP_R_CERTIFICATE_NOT_ACCEPTED,
796 "rejecting newly enrolled cert with subject: %s; %s",
797 subj, txt);
798 ctx->status = OSSL_CMP_PKISTATUS_rejected_by_client;
799 ret = 0;
800 }
801 OPENSSL_free(subj);
802 return ret;
803 }
804
805 static int initial_certreq(OSSL_CMP_CTX *ctx,
806 int req_type, const OSSL_CRMF_MSG *crm,
807 OSSL_CMP_MSG **p_rep, int rep_type)
808 {
809 OSSL_CMP_MSG *req;
810 int res;
811
812 ctx->status = OSSL_CMP_PKISTATUS_request;
813 if (!ossl_cmp_ctx_set0_newCert(ctx, NULL))
814 return 0;
815
816 /* also checks if all necessary options are set */
817 if ((req = ossl_cmp_certreq_new(ctx, req_type, crm)) == NULL)
818 return 0;
819
820 res = send_receive_check(ctx, req, p_rep, rep_type);
821 OSSL_CMP_MSG_free(req);
822 return res;
823 }
824
825 int OSSL_CMP_try_certreq(OSSL_CMP_CTX *ctx, int req_type,
826 const OSSL_CRMF_MSG *crm, int *checkAfter)
827 {
828 OSSL_CMP_MSG *rep = NULL;
829 int is_p10 = req_type == OSSL_CMP_PKIBODY_P10CR;
830 int rid = is_p10 ? OSSL_CMP_CERTREQID_NONE : OSSL_CMP_CERTREQID;
831 int rep_type = is_p10 ? OSSL_CMP_PKIBODY_CP : req_type + 1;
832 int res = 0;
833
834 if (ctx == NULL) {
835 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
836 return 0;
837 }
838
839 if (ctx->status != OSSL_CMP_PKISTATUS_waiting) { /* not polling already */
840 if (!initial_certreq(ctx, req_type, crm, &rep, rep_type))
841 goto err;
842
843 if (!save_senderNonce_if_waiting(ctx, rep, rid))
844 goto err;
845 } else {
846 if (req_type < 0)
847 return ossl_cmp_exchange_error(ctx, OSSL_CMP_PKISTATUS_rejection,
848 0, "polling aborted",
849 0 /* errorCode */, "by application");
850 res = poll_for_response(ctx, 0 /* no sleep */, rid, &rep, checkAfter);
851 if (res <= 0) /* waiting or error */
852 return res;
853 }
854 res = cert_response(ctx, 0 /* no sleep */, rid, &rep, checkAfter,
855 req_type, rep_type);
856
857 err:
858 OSSL_CMP_MSG_free(rep);
859 return res;
860 }
861
862 /*-
863 * Do the full sequence CR/IR/KUR/P10CR, CP/IP/KUP/CP,
864 * certConf, PKIconf, and polling if required.
865 * Will sleep as long as indicated by the server (according to checkAfter).
866 * All enrollment options need to be present in the context.
867 * Returns pointer to received certificate, or NULL if none was received.
868 */
869 X509 *OSSL_CMP_exec_certreq(OSSL_CMP_CTX *ctx, int req_type,
870 const OSSL_CRMF_MSG *crm)
871 {
872 OSSL_CMP_MSG *rep = NULL;
873 int is_p10 = req_type == OSSL_CMP_PKIBODY_P10CR;
874 int rid = is_p10 ? OSSL_CMP_CERTREQID_NONE : OSSL_CMP_CERTREQID;
875 int rep_type = is_p10 ? OSSL_CMP_PKIBODY_CP : req_type + 1;
876 X509 *result = NULL;
877
878 if (ctx == NULL) {
879 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
880 return NULL;
881 }
882
883 if (!initial_certreq(ctx, req_type, crm, &rep, rep_type))
884 goto err;
885
886 if (!save_senderNonce_if_waiting(ctx, rep, rid))
887 goto err;
888
889 if (cert_response(ctx, 1 /* sleep */, rid, &rep, NULL, req_type, rep_type)
890 <= 0)
891 goto err;
892
893 result = ctx->newCert;
894 err:
895 OSSL_CMP_MSG_free(rep);
896 return result;
897 }
898
899 int OSSL_CMP_exec_RR_ses(OSSL_CMP_CTX *ctx)
900 {
901 OSSL_CMP_MSG *rr = NULL;
902 OSSL_CMP_MSG *rp = NULL;
903 const int num_RevDetails = 1;
904 const int rsid = OSSL_CMP_REVREQSID;
905 OSSL_CMP_REVREPCONTENT *rrep = NULL;
906 OSSL_CMP_PKISI *si = NULL;
907 char buf[OSSL_CMP_PKISI_BUFLEN];
908 int ret = 0;
909
910 if (ctx == NULL) {
911 ERR_raise(ERR_LIB_CMP, CMP_R_INVALID_ARGS);
912 return 0;
913 }
914 ctx->status = OSSL_CMP_PKISTATUS_request;
915 if (ctx->oldCert == NULL && ctx->p10CSR == NULL
916 && (ctx->serialNumber == NULL || ctx->issuer == NULL)) {
917 ERR_raise(ERR_LIB_CMP, CMP_R_MISSING_REFERENCE_CERT);
918 return 0;
919 }
920
921 /* OSSL_CMP_rr_new() also checks if all necessary options are set */
922 if ((rr = ossl_cmp_rr_new(ctx)) == NULL)
923 goto end;
924
925 if (!send_receive_also_delayed(ctx, rr, &rp, OSSL_CMP_PKIBODY_RP))
926 goto end;
927
928 rrep = rp->body->value.rp;
929 #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
930 if (sk_OSSL_CMP_PKISI_num(rrep->status) != num_RevDetails) {
931 ERR_raise(ERR_LIB_CMP, CMP_R_WRONG_RP_COMPONENT_COUNT);
932 goto end;
933 }
934 #else
935 if (sk_OSSL_CMP_PKISI_num(rrep->status) < 1) {
936 ERR_raise(ERR_LIB_CMP, CMP_R_WRONG_RP_COMPONENT_COUNT);
937 goto end;
938 }
939 #endif
940
941 /* evaluate PKIStatus field */
942 si = ossl_cmp_revrepcontent_get_pkisi(rrep, rsid);
943 if (!save_statusInfo(ctx, si))
944 goto err;
945 switch (ossl_cmp_pkisi_get_status(si)) {
946 case OSSL_CMP_PKISTATUS_accepted:
947 ossl_cmp_info(ctx, "revocation accepted (PKIStatus=accepted)");
948 ret = 1;
949 break;
950 case OSSL_CMP_PKISTATUS_grantedWithMods:
951 ossl_cmp_info(ctx, "revocation accepted (PKIStatus=grantedWithMods)");
952 ret = 1;
953 break;
954 case OSSL_CMP_PKISTATUS_rejection:
955 ERR_raise(ERR_LIB_CMP, CMP_R_REQUEST_REJECTED_BY_SERVER);
956 goto err;
957 case OSSL_CMP_PKISTATUS_revocationWarning:
958 ossl_cmp_info(ctx, "revocation accepted (PKIStatus=revocationWarning)");
959 ret = 1;
960 break;
961 case OSSL_CMP_PKISTATUS_revocationNotification:
962 /* interpretation as warning or error depends on CA */
963 ossl_cmp_warn(ctx,
964 "revocation accepted (PKIStatus=revocationNotification)");
965 ret = 1;
966 break;
967 case OSSL_CMP_PKISTATUS_waiting:
968 case OSSL_CMP_PKISTATUS_keyUpdateWarning:
969 ERR_raise(ERR_LIB_CMP, CMP_R_UNEXPECTED_PKISTATUS);
970 goto err;
971 default:
972 ERR_raise(ERR_LIB_CMP, CMP_R_UNKNOWN_PKISTATUS);
973 goto err;
974 }
975
976 /* check any present CertId in optional revCerts field */
977 if (sk_OSSL_CRMF_CERTID_num(rrep->revCerts) >= 1) {
978 OSSL_CRMF_CERTID *cid;
979 OSSL_CRMF_CERTTEMPLATE *tmpl = sk_OSSL_CMP_REVDETAILS_value(rr->body->value.rr, rsid)->certDetails;
980 const X509_NAME *issuer = OSSL_CRMF_CERTTEMPLATE_get0_issuer(tmpl);
981 const ASN1_INTEGER *serial = OSSL_CRMF_CERTTEMPLATE_get0_serialNumber(tmpl);
982
983 if (sk_OSSL_CRMF_CERTID_num(rrep->revCerts) != num_RevDetails) {
984 ERR_raise(ERR_LIB_CMP, CMP_R_WRONG_RP_COMPONENT_COUNT);
985 ret = 0;
986 goto err;
987 }
988 if ((cid = ossl_cmp_revrepcontent_get_CertId(rrep, rsid)) == NULL) {
989 ERR_raise(ERR_LIB_CMP, CMP_R_MISSING_CERTID);
990 ret = 0;
991 goto err;
992 }
993 if (X509_NAME_cmp(issuer, OSSL_CRMF_CERTID_get0_issuer(cid)) != 0) {
994 #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
995 ERR_raise(ERR_LIB_CMP, CMP_R_WRONG_CERTID_IN_RP);
996 ret = 0;
997 goto err;
998 #endif
999 }
1000 if (ASN1_INTEGER_cmp(serial,
1001 OSSL_CRMF_CERTID_get0_serialNumber(cid))
1002 != 0) {
1003 #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
1004 ERR_raise(ERR_LIB_CMP, CMP_R_WRONG_SERIAL_IN_RP);
1005 ret = 0;
1006 goto err;
1007 #endif
1008 }
1009 }
1010
1011 /* check number of any optionally present crls */
1012 if (rrep->crls != NULL && sk_X509_CRL_num(rrep->crls) != num_RevDetails) {
1013 ERR_raise(ERR_LIB_CMP, CMP_R_WRONG_RP_COMPONENT_COUNT);
1014 ret = 0;
1015 goto err;
1016 }
1017
1018 err:
1019 if (ret == 0
1020 && OSSL_CMP_CTX_snprint_PKIStatus(ctx, buf, sizeof(buf)) != NULL)
1021 ERR_add_error_data(1, buf);
1022
1023 end:
1024 OSSL_CMP_MSG_free(rr);
1025 OSSL_CMP_MSG_free(rp);
1026 return ret;
1027 }
1028
1029 STACK_OF(OSSL_CMP_ITAV) *OSSL_CMP_exec_GENM_ses(OSSL_CMP_CTX *ctx)
1030 {
1031 OSSL_CMP_MSG *genm;
1032 OSSL_CMP_MSG *genp = NULL;
1033 STACK_OF(OSSL_CMP_ITAV) *itavs = NULL;
1034
1035 if (ctx == NULL) {
1036 ERR_raise(ERR_LIB_CMP, CMP_R_INVALID_ARGS);
1037 return NULL;
1038 }
1039 ctx->status = OSSL_CMP_PKISTATUS_request;
1040
1041 if ((genm = ossl_cmp_genm_new(ctx)) == NULL)
1042 goto err;
1043
1044 if (!send_receive_also_delayed(ctx, genm, &genp, OSSL_CMP_PKIBODY_GENP))
1045 goto err;
1046 ctx->status = OSSL_CMP_PKISTATUS_accepted;
1047
1048 itavs = genp->body->value.genp;
1049 if (itavs == NULL)
1050 itavs = sk_OSSL_CMP_ITAV_new_null();
1051 /* received stack of itavs not to be freed with the genp */
1052 genp->body->value.genp = NULL;
1053
1054 err:
1055 OSSL_CMP_MSG_free(genm);
1056 OSSL_CMP_MSG_free(genp);
1057
1058 return itavs; /* NULL indicates error case */
1059 }