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