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