CMP client: fix error response on -csr without private key, also in docs
[openssl.git] / crypto / cmp / cmp_msg.c
1 /*
2  * Copyright 2007-2022 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 /* CMP functions for PKIMessage construction */
13
14 #include "cmp_local.h"
15
16 /* explicit #includes not strictly needed since implied by the above: */
17 #include <openssl/asn1t.h>
18 #include <openssl/cmp.h>
19 #include <openssl/crmf.h>
20 #include <openssl/err.h>
21 #include <openssl/x509.h>
22
23 OSSL_CMP_MSG *OSSL_CMP_MSG_new(OSSL_LIB_CTX *libctx, const char *propq)
24 {
25     OSSL_CMP_MSG *msg = NULL;
26
27     msg = (OSSL_CMP_MSG *)ASN1_item_new_ex(ASN1_ITEM_rptr(OSSL_CMP_MSG),
28                                            libctx, propq);
29     if (!ossl_cmp_msg_set0_libctx(msg, libctx, propq)) {
30         OSSL_CMP_MSG_free(msg);
31         msg = NULL;
32     }
33     return msg;
34 }
35
36 void OSSL_CMP_MSG_free(OSSL_CMP_MSG *msg)
37 {
38     ASN1_item_free((ASN1_VALUE *)msg, ASN1_ITEM_rptr(OSSL_CMP_MSG));
39 }
40
41 /*
42  * This should only be used if the X509 object was embedded inside another
43  * asn1 object and it needs a libctx to operate.
44  * Use OSSL_CMP_MSG_new() instead if possible.
45  */
46 int ossl_cmp_msg_set0_libctx(OSSL_CMP_MSG *msg, OSSL_LIB_CTX *libctx,
47                              const char *propq)
48 {
49     if (msg != NULL) {
50         msg->libctx = libctx;
51         OPENSSL_free(msg->propq);
52         msg->propq = NULL;
53         if (propq != NULL) {
54             msg->propq = OPENSSL_strdup(propq);
55             if (msg->propq == NULL)
56                 return 0;
57         }
58     }
59     return 1;
60 }
61
62 OSSL_CMP_PKIHEADER *OSSL_CMP_MSG_get0_header(const OSSL_CMP_MSG *msg)
63 {
64     if (msg == NULL) {
65         ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
66         return NULL;
67     }
68     return msg->header;
69 }
70
71 const char *ossl_cmp_bodytype_to_string(int type)
72 {
73     static const char *type_names[] = {
74         "IR", "IP", "CR", "CP", "P10CR",
75         "POPDECC", "POPDECR", "KUR", "KUP",
76         "KRR", "KRP", "RR", "RP", "CCR", "CCP",
77         "CKUANN", "CANN", "RANN", "CRLANN", "PKICONF", "NESTED",
78         "GENM", "GENP", "ERROR", "CERTCONF", "POLLREQ", "POLLREP",
79     };
80
81     if (type < 0 || type > OSSL_CMP_PKIBODY_TYPE_MAX)
82         return "illegal body type";
83     return type_names[type];
84 }
85
86 int ossl_cmp_msg_set_bodytype(OSSL_CMP_MSG *msg, int type)
87 {
88     if (!ossl_assert(msg != NULL && msg->body != NULL))
89         return 0;
90
91     msg->body->type = type;
92     return 1;
93 }
94
95 int OSSL_CMP_MSG_get_bodytype(const OSSL_CMP_MSG *msg)
96 {
97     if (!ossl_assert(msg != NULL && msg->body != NULL))
98         return -1;
99
100     return msg->body->type;
101 }
102
103 /* Add an extension to the referenced extension stack, which may be NULL */
104 static int add1_extension(X509_EXTENSIONS **pexts, int nid, int crit, void *ex)
105 {
106     X509_EXTENSION *ext;
107     int res;
108
109     if (!ossl_assert(pexts != NULL)) /* pointer to var must not be NULL */
110         return 0;
111
112     if ((ext = X509V3_EXT_i2d(nid, crit, ex)) == NULL)
113         return 0;
114
115     res = X509v3_add_ext(pexts, ext, 0) != NULL;
116     X509_EXTENSION_free(ext);
117     return res;
118 }
119
120 /* Add extension list to the referenced extension stack, which may be NULL */
121 static int add_extensions(STACK_OF(X509_EXTENSION) **target,
122                           const STACK_OF(X509_EXTENSION) *exts)
123 {
124     int i;
125
126     if (target == NULL)
127         return 0;
128
129     for (i = 0; i < sk_X509_EXTENSION_num(exts); i++) {
130         X509_EXTENSION *ext = sk_X509_EXTENSION_value(exts, i);
131         ASN1_OBJECT *obj = X509_EXTENSION_get_object(ext);
132         int idx = X509v3_get_ext_by_OBJ(*target, obj, -1);
133
134         /* Does extension exist in target? */
135         if (idx != -1) {
136             /* Delete all extensions of same type */
137             do {
138                 X509_EXTENSION_free(sk_X509_EXTENSION_delete(*target, idx));
139                 idx = X509v3_get_ext_by_OBJ(*target, obj, -1);
140             } while (idx != -1);
141         }
142         if (!X509v3_add_ext(target, ext, -1))
143             return 0;
144     }
145     return 1;
146 }
147
148 /* Add a CRL revocation reason code to extension stack, which may be NULL */
149 static int add_crl_reason_extension(X509_EXTENSIONS **pexts, int reason_code)
150 {
151     ASN1_ENUMERATED *val = ASN1_ENUMERATED_new();
152     int res = 0;
153
154     if (val != NULL && ASN1_ENUMERATED_set(val, reason_code))
155         res = add1_extension(pexts, NID_crl_reason, 0 /* non-critical */, val);
156     ASN1_ENUMERATED_free(val);
157     return res;
158 }
159
160 OSSL_CMP_MSG *ossl_cmp_msg_create(OSSL_CMP_CTX *ctx, int bodytype)
161 {
162     OSSL_CMP_MSG *msg = NULL;
163
164     if (!ossl_assert(ctx != NULL))
165         return NULL;
166
167     if ((msg = OSSL_CMP_MSG_new(ctx->libctx, ctx->propq)) == NULL)
168         return NULL;
169     if (!ossl_cmp_hdr_init(ctx, msg->header)
170             || !ossl_cmp_msg_set_bodytype(msg, bodytype))
171         goto err;
172     if (ctx->geninfo_ITAVs != NULL
173             && !ossl_cmp_hdr_generalInfo_push1_items(msg->header,
174                                                      ctx->geninfo_ITAVs))
175         goto err;
176
177     switch (bodytype) {
178     case OSSL_CMP_PKIBODY_IR:
179     case OSSL_CMP_PKIBODY_CR:
180     case OSSL_CMP_PKIBODY_KUR:
181         if ((msg->body->value.ir = OSSL_CRMF_MSGS_new()) == NULL)
182             goto err;
183         return msg;
184
185     case OSSL_CMP_PKIBODY_P10CR:
186         if (ctx->p10CSR == NULL) {
187             ERR_raise(ERR_LIB_CMP, CMP_R_MISSING_P10CSR);
188             goto err;
189         }
190         if ((msg->body->value.p10cr = X509_REQ_dup(ctx->p10CSR)) == NULL)
191             goto err;
192         return msg;
193
194     case OSSL_CMP_PKIBODY_IP:
195     case OSSL_CMP_PKIBODY_CP:
196     case OSSL_CMP_PKIBODY_KUP:
197         if ((msg->body->value.ip = OSSL_CMP_CERTREPMESSAGE_new()) == NULL)
198             goto err;
199         return msg;
200
201     case OSSL_CMP_PKIBODY_RR:
202         if ((msg->body->value.rr = sk_OSSL_CMP_REVDETAILS_new_null()) == NULL)
203             goto err;
204         return msg;
205     case OSSL_CMP_PKIBODY_RP:
206         if ((msg->body->value.rp = OSSL_CMP_REVREPCONTENT_new()) == NULL)
207             goto err;
208         return msg;
209
210     case OSSL_CMP_PKIBODY_CERTCONF:
211         if ((msg->body->value.certConf =
212              sk_OSSL_CMP_CERTSTATUS_new_null()) == NULL)
213             goto err;
214         return msg;
215     case OSSL_CMP_PKIBODY_PKICONF:
216         if ((msg->body->value.pkiconf = ASN1_TYPE_new()) == NULL)
217             goto err;
218         ASN1_TYPE_set(msg->body->value.pkiconf, V_ASN1_NULL, NULL);
219         return msg;
220
221     case OSSL_CMP_PKIBODY_POLLREQ:
222         if ((msg->body->value.pollReq = sk_OSSL_CMP_POLLREQ_new_null()) == NULL)
223             goto err;
224         return msg;
225     case OSSL_CMP_PKIBODY_POLLREP:
226         if ((msg->body->value.pollRep = sk_OSSL_CMP_POLLREP_new_null()) == NULL)
227             goto err;
228         return msg;
229
230     case OSSL_CMP_PKIBODY_GENM:
231     case OSSL_CMP_PKIBODY_GENP:
232         if ((msg->body->value.genm = sk_OSSL_CMP_ITAV_new_null()) == NULL)
233             goto err;
234         return msg;
235
236     case OSSL_CMP_PKIBODY_ERROR:
237         if ((msg->body->value.error = OSSL_CMP_ERRORMSGCONTENT_new()) == NULL)
238             goto err;
239         return msg;
240
241     default:
242         ERR_raise(ERR_LIB_CMP, CMP_R_UNEXPECTED_PKIBODY);
243         goto err;
244     }
245
246  err:
247     OSSL_CMP_MSG_free(msg);
248     return NULL;
249 }
250
251 #define HAS_SAN(ctx) \
252     (sk_GENERAL_NAME_num((ctx)->subjectAltNames) > 0 \
253          || OSSL_CMP_CTX_reqExtensions_have_SAN(ctx) == 1)
254
255 static const X509_NAME *determine_subj(OSSL_CMP_CTX *ctx, int for_KUR,
256                                        const X509_NAME *ref_subj)
257 {
258     if (ctx->subjectName != NULL)
259         return IS_NULL_DN(ctx->subjectName) ? NULL : ctx->subjectName;
260     if (ctx->p10CSR != NULL) /* first default is from any given CSR */
261         return X509_REQ_get_subject_name(ctx->p10CSR);
262     if (for_KUR || !HAS_SAN(ctx))
263         /*
264          * For KUR, copy subject from any reference cert as fallback.
265          * For IR or CR, do the same only if there is no subjectAltName.
266          */
267         return ref_subj;
268     return NULL;
269 }
270
271 OSSL_CRMF_MSG *OSSL_CMP_CTX_setup_CRM(OSSL_CMP_CTX *ctx, int for_KUR, int rid)
272 {
273     OSSL_CRMF_MSG *crm = NULL;
274     X509 *refcert = ctx->oldCert != NULL ? ctx->oldCert : ctx->cert;
275     /* refcert defaults to current client cert */
276     EVP_PKEY *rkey = OSSL_CMP_CTX_get0_newPkey(ctx, 0);
277     STACK_OF(GENERAL_NAME) *default_sans = NULL;
278     const X509_NAME *ref_subj =
279         refcert != NULL ? X509_get_subject_name(refcert) : NULL;
280     const X509_NAME *subject = determine_subj(ctx, for_KUR, ref_subj);
281     const X509_NAME *issuer = ctx->issuer != NULL || refcert == NULL
282         ? (IS_NULL_DN(ctx->issuer) ? NULL : ctx->issuer)
283         : X509_get_issuer_name(refcert);
284     int crit = ctx->setSubjectAltNameCritical || subject == NULL;
285     /* RFC5280: subjectAltName MUST be critical if subject is null */
286     X509_EXTENSIONS *exts = NULL;
287
288     if (rkey == NULL && ctx->p10CSR != NULL)
289         rkey = X509_REQ_get0_pubkey(ctx->p10CSR);
290     if (rkey == NULL && refcert != NULL)
291         rkey = X509_get0_pubkey(refcert);
292     if (rkey == NULL)
293         rkey = ctx->pkey; /* default is independent of ctx->oldCert */
294     if (rkey == NULL) {
295 #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
296         ERR_raise(ERR_LIB_CMP, CMP_R_MISSING_PUBLIC_KEY);
297         return NULL;
298 #endif
299     }
300     if (for_KUR && refcert == NULL && ctx->p10CSR == NULL) {
301         ERR_raise(ERR_LIB_CMP, CMP_R_MISSING_REFERENCE_CERT);
302         return NULL;
303     }
304     if ((crm = OSSL_CRMF_MSG_new()) == NULL)
305         return NULL;
306     if (!OSSL_CRMF_MSG_set_certReqId(crm, rid)
307             /*
308              * fill certTemplate, corresponding to CertificationRequestInfo
309              * of PKCS#10. The rkey param cannot be NULL so far -
310              * it could be NULL if centralized key creation was supported
311              */
312             || !OSSL_CRMF_CERTTEMPLATE_fill(OSSL_CRMF_MSG_get0_tmpl(crm), rkey,
313                                             subject, issuer, NULL /* serial */))
314         goto err;
315     if (ctx->days != 0) {
316         time_t now = time(NULL);
317         ASN1_TIME *notBefore = ASN1_TIME_adj(NULL, now, 0, 0);
318         ASN1_TIME *notAfter = ASN1_TIME_adj(NULL, now, ctx->days, 0);
319
320         if (notBefore == NULL
321                 || notAfter == NULL
322                 || !OSSL_CRMF_MSG_set0_validity(crm, notBefore, notAfter)) {
323             ASN1_TIME_free(notBefore);
324             ASN1_TIME_free(notAfter);
325             goto err;
326         }
327     }
328
329     /* extensions */
330     if (ctx->p10CSR != NULL
331             && (exts = X509_REQ_get_extensions(ctx->p10CSR)) == NULL)
332         goto err;
333     if (!ctx->SubjectAltName_nodefault && !HAS_SAN(ctx) && refcert != NULL
334         && (default_sans = X509V3_get_d2i(X509_get0_extensions(refcert),
335                                           NID_subject_alt_name, NULL, NULL))
336         != NULL
337             && !add1_extension(&exts, NID_subject_alt_name, crit, default_sans))
338         goto err;
339     if (ctx->reqExtensions != NULL /* augment/override existing ones */
340             && !add_extensions(&exts, ctx->reqExtensions))
341         goto err;
342     if (sk_GENERAL_NAME_num(ctx->subjectAltNames) > 0
343             && !add1_extension(&exts, NID_subject_alt_name,
344                                crit, ctx->subjectAltNames))
345         goto err;
346     if (ctx->policies != NULL
347             && !add1_extension(&exts, NID_certificate_policies,
348                                ctx->setPoliciesCritical, ctx->policies))
349         goto err;
350     if (!OSSL_CRMF_MSG_set0_extensions(crm, exts))
351         goto err;
352     exts = NULL;
353     /* end fill certTemplate, now set any controls */
354
355     /* for KUR, set OldCertId according to D.6 */
356     if (for_KUR && refcert != NULL) {
357         OSSL_CRMF_CERTID *cid =
358             OSSL_CRMF_CERTID_gen(X509_get_issuer_name(refcert),
359                                  X509_get0_serialNumber(refcert));
360         int ret;
361
362         if (cid == NULL)
363             goto err;
364         ret = OSSL_CRMF_MSG_set1_regCtrl_oldCertID(crm, cid);
365         OSSL_CRMF_CERTID_free(cid);
366         if (ret == 0)
367             goto err;
368     }
369
370     goto end;
371
372  err:
373     OSSL_CRMF_MSG_free(crm);
374     crm = NULL;
375
376  end:
377     sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
378     sk_GENERAL_NAME_pop_free(default_sans, GENERAL_NAME_free);
379     return crm;
380 }
381
382 OSSL_CMP_MSG *ossl_cmp_certreq_new(OSSL_CMP_CTX *ctx, int type,
383                                    const OSSL_CRMF_MSG *crm)
384 {
385     OSSL_CMP_MSG *msg;
386     OSSL_CRMF_MSG *local_crm = NULL;
387
388     if (!ossl_assert(ctx != NULL))
389         return NULL;
390
391     if (type != OSSL_CMP_PKIBODY_IR && type != OSSL_CMP_PKIBODY_CR
392             && type != OSSL_CMP_PKIBODY_KUR && type != OSSL_CMP_PKIBODY_P10CR) {
393         ERR_raise(ERR_LIB_CMP, CMP_R_INVALID_ARGS);
394         return NULL;
395     }
396     if (type == OSSL_CMP_PKIBODY_P10CR && crm != NULL) {
397         ERR_raise(ERR_LIB_CMP, CMP_R_INVALID_ARGS);
398         return NULL;
399     }
400
401     if ((msg = ossl_cmp_msg_create(ctx, type)) == NULL)
402         goto err;
403
404     /* header */
405     if (ctx->implicitConfirm && !ossl_cmp_hdr_set_implicitConfirm(msg->header))
406         goto err;
407
408     /* body */
409     /* For P10CR the content has already been set in OSSL_CMP_MSG_create */
410     if (type != OSSL_CMP_PKIBODY_P10CR) {
411         EVP_PKEY *privkey = OSSL_CMP_CTX_get0_newPkey(ctx, 1);
412
413         /*
414          * privkey is NULL in case ctx->newPkey does not include a private key.
415          * We then may try to use ctx->pkey as fallback/default, but only
416          * if ctx-> newPkey does not include a (non-matching) public key:
417          */
418         if (privkey == NULL && OSSL_CMP_CTX_get0_newPkey(ctx, 0) == NULL)
419             privkey = ctx->pkey; /* default is independent of ctx->oldCert */
420         if (ctx->popoMethod >= OSSL_CRMF_POPO_SIGNATURE && privkey == NULL) {
421             ERR_raise(ERR_LIB_CMP, CMP_R_MISSING_PRIVATE_KEY_FOR_POPO);
422             goto err;
423         }
424         if (crm == NULL) {
425             local_crm = OSSL_CMP_CTX_setup_CRM(ctx,
426                                                type == OSSL_CMP_PKIBODY_KUR,
427                                                OSSL_CMP_CERTREQID);
428             if (local_crm == NULL
429                 || !OSSL_CRMF_MSG_create_popo(ctx->popoMethod, local_crm,
430                                               privkey, ctx->digest,
431                                               ctx->libctx, ctx->propq))
432                 goto err;
433         } else {
434             if ((local_crm = OSSL_CRMF_MSG_dup(crm)) == NULL)
435                 goto err;
436         }
437
438         /* value.ir is same for cr and kur */
439         if (!sk_OSSL_CRMF_MSG_push(msg->body->value.ir, local_crm))
440             goto err;
441         local_crm = NULL;
442     }
443
444     if (!ossl_cmp_msg_protect(ctx, msg))
445         goto err;
446
447     return msg;
448
449  err:
450     ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_CERTREQ);
451     OSSL_CRMF_MSG_free(local_crm);
452     OSSL_CMP_MSG_free(msg);
453     return NULL;
454 }
455
456 OSSL_CMP_MSG *ossl_cmp_certrep_new(OSSL_CMP_CTX *ctx, int bodytype,
457                                    int certReqId, const OSSL_CMP_PKISI *si,
458                                    X509 *cert, const X509 *encryption_recip,
459                                    STACK_OF(X509) *chain, STACK_OF(X509) *caPubs,
460                                    int unprotectedErrors)
461 {
462     OSSL_CMP_MSG *msg = NULL;
463     OSSL_CMP_CERTREPMESSAGE *repMsg = NULL;
464     OSSL_CMP_CERTRESPONSE *resp = NULL;
465     int status = OSSL_CMP_PKISTATUS_unspecified;
466
467     if (!ossl_assert(ctx != NULL && si != NULL))
468         return NULL;
469
470     if ((msg = ossl_cmp_msg_create(ctx, bodytype)) == NULL)
471         goto err;
472     repMsg = msg->body->value.ip; /* value.ip is same for cp and kup */
473
474     /* header */
475     if (ctx->implicitConfirm && !ossl_cmp_hdr_set_implicitConfirm(msg->header))
476         goto err;
477
478     /* body */
479     if ((resp = OSSL_CMP_CERTRESPONSE_new()) == NULL)
480         goto err;
481     OSSL_CMP_PKISI_free(resp->status);
482     if ((resp->status = OSSL_CMP_PKISI_dup(si)) == NULL
483             || !ASN1_INTEGER_set(resp->certReqId, certReqId))
484         goto err;
485
486     status = ossl_cmp_pkisi_get_status(resp->status);
487     if (status != OSSL_CMP_PKISTATUS_rejection
488             && status != OSSL_CMP_PKISTATUS_waiting && cert != NULL) {
489         if (encryption_recip != NULL) {
490             ERR_raise(ERR_LIB_CMP, ERR_R_UNSUPPORTED);
491             goto err;
492         }
493
494         if ((resp->certifiedKeyPair = OSSL_CMP_CERTIFIEDKEYPAIR_new())
495             == NULL)
496             goto err;
497         resp->certifiedKeyPair->certOrEncCert->type =
498             OSSL_CMP_CERTORENCCERT_CERTIFICATE;
499         if (!X509_up_ref(cert))
500             goto err;
501         resp->certifiedKeyPair->certOrEncCert->value.certificate = cert;
502     }
503
504     if (!sk_OSSL_CMP_CERTRESPONSE_push(repMsg->response, resp))
505         goto err;
506     resp = NULL;
507
508     if (bodytype == OSSL_CMP_PKIBODY_IP && caPubs != NULL
509             && (repMsg->caPubs = X509_chain_up_ref(caPubs)) == NULL)
510         goto err;
511     if (sk_X509_num(chain) > 0
512         && !ossl_x509_add_certs_new(&msg->extraCerts, chain,
513                                     X509_ADD_FLAG_UP_REF | X509_ADD_FLAG_NO_DUP))
514         goto err;
515
516     if (!unprotectedErrors
517             || ossl_cmp_pkisi_get_status(si) != OSSL_CMP_PKISTATUS_rejection)
518         if (!ossl_cmp_msg_protect(ctx, msg))
519             goto err;
520
521     return msg;
522
523  err:
524     ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_CERTREP);
525     OSSL_CMP_CERTRESPONSE_free(resp);
526     OSSL_CMP_MSG_free(msg);
527     return NULL;
528 }
529
530 OSSL_CMP_MSG *ossl_cmp_rr_new(OSSL_CMP_CTX *ctx)
531 {
532     OSSL_CMP_MSG *msg = NULL;
533     OSSL_CMP_REVDETAILS *rd;
534     int ret;
535
536     if (!ossl_assert(ctx != NULL && (ctx->oldCert != NULL
537                                      || ctx->p10CSR != NULL)))
538         return NULL;
539
540     if ((rd = OSSL_CMP_REVDETAILS_new()) == NULL)
541         goto err;
542
543     /* Fill the template from the contents of the certificate to be revoked */
544     ret = ctx->oldCert != NULL
545         ? OSSL_CRMF_CERTTEMPLATE_fill(rd->certDetails,
546                                       NULL /* pubkey would be redundant */,
547                                       NULL /* subject would be redundant */,
548                                       X509_get_issuer_name(ctx->oldCert),
549                                       X509_get0_serialNumber(ctx->oldCert))
550         : OSSL_CRMF_CERTTEMPLATE_fill(rd->certDetails,
551                                       X509_REQ_get0_pubkey(ctx->p10CSR),
552                                       X509_REQ_get_subject_name(ctx->p10CSR),
553                                       NULL, NULL);
554     if (!ret)
555         goto err;
556
557     /* revocation reason code is optional */
558     if (ctx->revocationReason != CRL_REASON_NONE
559             && !add_crl_reason_extension(&rd->crlEntryDetails,
560                                          ctx->revocationReason))
561         goto err;
562
563     if ((msg = ossl_cmp_msg_create(ctx, OSSL_CMP_PKIBODY_RR)) == NULL)
564         goto err;
565
566     if (!sk_OSSL_CMP_REVDETAILS_push(msg->body->value.rr, rd))
567         goto err;
568     rd = NULL;
569     /* Revocation Passphrase according to section 5.3.19.9 could be set here */
570
571     if (!ossl_cmp_msg_protect(ctx, msg))
572         goto err;
573
574     return msg;
575
576  err:
577     ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_RR);
578     OSSL_CMP_MSG_free(msg);
579     OSSL_CMP_REVDETAILS_free(rd);
580     return NULL;
581 }
582
583 OSSL_CMP_MSG *ossl_cmp_rp_new(OSSL_CMP_CTX *ctx, const OSSL_CMP_PKISI *si,
584                               const OSSL_CRMF_CERTID *cid, int unprotectedErrors)
585 {
586     OSSL_CMP_REVREPCONTENT *rep = NULL;
587     OSSL_CMP_PKISI *si1 = NULL;
588     OSSL_CRMF_CERTID *cid_copy = NULL;
589     OSSL_CMP_MSG *msg = NULL;
590
591     if (!ossl_assert(ctx != NULL && si != NULL))
592         return NULL;
593
594     if ((msg = ossl_cmp_msg_create(ctx, OSSL_CMP_PKIBODY_RP)) == NULL)
595         goto err;
596     rep = msg->body->value.rp;
597
598     if ((si1 = OSSL_CMP_PKISI_dup(si)) == NULL)
599         goto err;
600
601     if (!sk_OSSL_CMP_PKISI_push(rep->status, si1)) {
602         OSSL_CMP_PKISI_free(si1);
603         goto err;
604     }
605
606     if ((rep->revCerts = sk_OSSL_CRMF_CERTID_new_null()) == NULL)
607         goto err;
608     if (cid != NULL) {
609         if ((cid_copy = OSSL_CRMF_CERTID_dup(cid)) == NULL)
610             goto err;
611         if (!sk_OSSL_CRMF_CERTID_push(rep->revCerts, cid_copy)) {
612             OSSL_CRMF_CERTID_free(cid_copy);
613             goto err;
614         }
615     }
616
617     if (!unprotectedErrors
618             || ossl_cmp_pkisi_get_status(si) != OSSL_CMP_PKISTATUS_rejection)
619         if (!ossl_cmp_msg_protect(ctx, msg))
620             goto err;
621
622     return msg;
623
624  err:
625     ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_RP);
626     OSSL_CMP_MSG_free(msg);
627     return NULL;
628 }
629
630 OSSL_CMP_MSG *ossl_cmp_pkiconf_new(OSSL_CMP_CTX *ctx)
631 {
632     OSSL_CMP_MSG *msg;
633
634     if (!ossl_assert(ctx != NULL))
635         return NULL;
636
637     if ((msg = ossl_cmp_msg_create(ctx, OSSL_CMP_PKIBODY_PKICONF)) == NULL)
638         goto err;
639     if (ossl_cmp_msg_protect(ctx, msg))
640         return msg;
641
642  err:
643     ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_PKICONF);
644     OSSL_CMP_MSG_free(msg);
645     return NULL;
646 }
647
648 int ossl_cmp_msg_gen_push0_ITAV(OSSL_CMP_MSG *msg, OSSL_CMP_ITAV *itav)
649 {
650     int bodytype;
651
652     if (!ossl_assert(msg != NULL && itav != NULL))
653         return 0;
654
655     bodytype = OSSL_CMP_MSG_get_bodytype(msg);
656     if (bodytype != OSSL_CMP_PKIBODY_GENM
657             && bodytype != OSSL_CMP_PKIBODY_GENP) {
658         ERR_raise(ERR_LIB_CMP, CMP_R_INVALID_ARGS);
659         return 0;
660     }
661
662     /* value.genp has the same structure, so this works for genp as well */
663     return OSSL_CMP_ITAV_push0_stack_item(&msg->body->value.genm, itav);
664 }
665
666 int ossl_cmp_msg_gen_push1_ITAVs(OSSL_CMP_MSG *msg,
667                                  const STACK_OF(OSSL_CMP_ITAV) *itavs)
668 {
669     int i;
670     OSSL_CMP_ITAV *itav = NULL;
671
672     if (!ossl_assert(msg != NULL))
673         return 0;
674
675     for (i = 0; i < sk_OSSL_CMP_ITAV_num(itavs); i++) {
676         itav = OSSL_CMP_ITAV_dup(sk_OSSL_CMP_ITAV_value(itavs, i));
677         if (itav == NULL
678                 || !ossl_cmp_msg_gen_push0_ITAV(msg, itav)) {
679             OSSL_CMP_ITAV_free(itav);
680             return 0;
681         }
682     }
683     return 1;
684 }
685
686 /*
687  * Creates a new General Message/Response with an empty itav stack
688  * returns a pointer to the PKIMessage on success, NULL on error
689  */
690 static OSSL_CMP_MSG *gen_new(OSSL_CMP_CTX *ctx,
691                              const STACK_OF(OSSL_CMP_ITAV) *itavs,
692                              int body_type, int err_code)
693 {
694     OSSL_CMP_MSG *msg = NULL;
695
696     if (!ossl_assert(ctx != NULL))
697         return NULL;
698
699     if ((msg = ossl_cmp_msg_create(ctx, body_type)) == NULL)
700         return NULL;
701
702     if (itavs != NULL && !ossl_cmp_msg_gen_push1_ITAVs(msg, itavs))
703         goto err;
704
705     if (!ossl_cmp_msg_protect(ctx, msg))
706         goto err;
707
708     return msg;
709
710  err:
711     ERR_raise(ERR_LIB_CMP, err_code);
712     OSSL_CMP_MSG_free(msg);
713     return NULL;
714 }
715
716 OSSL_CMP_MSG *ossl_cmp_genm_new(OSSL_CMP_CTX *ctx)
717 {
718     return gen_new(ctx, ctx->genm_ITAVs,
719                    OSSL_CMP_PKIBODY_GENM, CMP_R_ERROR_CREATING_GENM);
720 }
721
722 OSSL_CMP_MSG *ossl_cmp_genp_new(OSSL_CMP_CTX *ctx,
723                                 const STACK_OF(OSSL_CMP_ITAV) *itavs)
724 {
725     return gen_new(ctx, itavs,
726                    OSSL_CMP_PKIBODY_GENP, CMP_R_ERROR_CREATING_GENP);
727 }
728
729 OSSL_CMP_MSG *ossl_cmp_error_new(OSSL_CMP_CTX *ctx, const OSSL_CMP_PKISI *si,
730                                  int64_t errorCode, const char *details,
731                                  int unprotected)
732 {
733     OSSL_CMP_MSG *msg = NULL;
734     const char *lib = NULL, *reason = NULL;
735     OSSL_CMP_PKIFREETEXT *ft;
736
737     if (!ossl_assert(ctx != NULL && si != NULL))
738         return NULL;
739
740     if ((msg = ossl_cmp_msg_create(ctx, OSSL_CMP_PKIBODY_ERROR)) == NULL)
741         goto err;
742
743     OSSL_CMP_PKISI_free(msg->body->value.error->pKIStatusInfo);
744     if ((msg->body->value.error->pKIStatusInfo = OSSL_CMP_PKISI_dup(si))
745         == NULL)
746         goto err;
747     if ((msg->body->value.error->errorCode = ASN1_INTEGER_new()) == NULL)
748         goto err;
749     if (!ASN1_INTEGER_set_int64(msg->body->value.error->errorCode, errorCode))
750         goto err;
751     if (errorCode > 0
752             && (uint64_t)errorCode < ((uint64_t)ERR_SYSTEM_FLAG << 1)) {
753         lib = ERR_lib_error_string((unsigned long)errorCode);
754         reason = ERR_reason_error_string((unsigned long)errorCode);
755     }
756     if (lib != NULL || reason != NULL || details != NULL) {
757         if ((ft = sk_ASN1_UTF8STRING_new_null()) == NULL)
758             goto err;
759         msg->body->value.error->errorDetails = ft;
760         if (lib != NULL && *lib != '\0'
761                 && !ossl_cmp_sk_ASN1_UTF8STRING_push_str(ft, lib, -1))
762             goto err;
763         if (reason != NULL && *reason != '\0'
764                 && !ossl_cmp_sk_ASN1_UTF8STRING_push_str(ft, reason, -1))
765             goto err;
766         if (details != NULL
767                 && !ossl_cmp_sk_ASN1_UTF8STRING_push_str(ft, details, -1))
768             goto err;
769     }
770
771     if (!unprotected && !ossl_cmp_msg_protect(ctx, msg))
772         goto err;
773     return msg;
774
775  err:
776     ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_ERROR);
777     OSSL_CMP_MSG_free(msg);
778     return NULL;
779 }
780
781 /*
782  * Set the certHash field of a OSSL_CMP_CERTSTATUS structure.
783  * This is used in the certConf message, for example,
784  * to confirm that the certificate was received successfully.
785  */
786 int ossl_cmp_certstatus_set0_certHash(OSSL_CMP_CERTSTATUS *certStatus,
787                                       ASN1_OCTET_STRING *hash)
788 {
789     if (!ossl_assert(certStatus != NULL))
790         return 0;
791     ASN1_OCTET_STRING_free(certStatus->certHash);
792     certStatus->certHash = hash;
793     return 1;
794 }
795
796 OSSL_CMP_MSG *ossl_cmp_certConf_new(OSSL_CMP_CTX *ctx, int certReqId,
797                                     int fail_info, const char *text)
798 {
799     OSSL_CMP_MSG *msg = NULL;
800     OSSL_CMP_CERTSTATUS *certStatus = NULL;
801     EVP_MD *md;
802     int is_fallback;
803     ASN1_OCTET_STRING *certHash = NULL;
804     OSSL_CMP_PKISI *sinfo;
805
806     if (!ossl_assert(ctx != NULL && ctx->newCert != NULL
807                      && (certReqId == OSSL_CMP_CERTREQID
808                          || certReqId == OSSL_CMP_CERTREQID_NONE)))
809         return NULL;
810
811     if ((unsigned)fail_info > OSSL_CMP_PKIFAILUREINFO_MAX_BIT_PATTERN) {
812         ERR_raise(ERR_LIB_CMP, CMP_R_FAIL_INFO_OUT_OF_RANGE);
813         return NULL;
814     }
815
816     if ((msg = ossl_cmp_msg_create(ctx, OSSL_CMP_PKIBODY_CERTCONF)) == NULL)
817         goto err;
818
819     if ((certStatus = OSSL_CMP_CERTSTATUS_new()) == NULL)
820         goto err;
821     /* consume certStatus into msg right away so it gets deallocated with msg */
822     if (sk_OSSL_CMP_CERTSTATUS_push(msg->body->value.certConf, certStatus) < 1) {
823         OSSL_CMP_CERTSTATUS_free(certStatus);
824         goto err;
825     }
826
827     /* set the ID of the certReq */
828     if (!ASN1_INTEGER_set(certStatus->certReqId, certReqId))
829         goto err;
830
831     certStatus->hashAlg = NULL;
832     /*
833      * The hash of the certificate, using the same hash algorithm
834      * as is used to create and verify the certificate signature.
835      * If not available, a fallback hash algorithm is used.
836      */
837     if ((certHash = X509_digest_sig(ctx->newCert, &md, &is_fallback)) == NULL)
838         goto err;
839     if (is_fallback) {
840         if (!ossl_cmp_hdr_set_pvno(msg->header, OSSL_CMP_PVNO_3))
841             goto err;
842         if ((certStatus->hashAlg = X509_ALGOR_new()) == NULL)
843             goto err;
844         X509_ALGOR_set_md(certStatus->hashAlg, md);
845     }
846     EVP_MD_free(md);
847
848     if (!ossl_cmp_certstatus_set0_certHash(certStatus, certHash))
849         goto err;
850     certHash = NULL;
851     /*
852      * For any particular CertStatus, omission of the statusInfo field
853      * indicates ACCEPTANCE of the specified certificate.  Alternatively,
854      * explicit status details (with respect to acceptance or rejection) MAY
855      * be provided in the statusInfo field, perhaps for auditing purposes at
856      * the CA/RA.
857      */
858     sinfo = fail_info != 0 ?
859         OSSL_CMP_STATUSINFO_new(OSSL_CMP_PKISTATUS_rejection, fail_info, text) :
860         OSSL_CMP_STATUSINFO_new(OSSL_CMP_PKISTATUS_accepted, 0, text);
861     if (sinfo == NULL)
862         goto err;
863     certStatus->statusInfo = sinfo;
864
865     if (!ossl_cmp_msg_protect(ctx, msg))
866         goto err;
867
868     return msg;
869
870  err:
871     ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_CERTCONF);
872     OSSL_CMP_MSG_free(msg);
873     ASN1_OCTET_STRING_free(certHash);
874     return NULL;
875 }
876
877 OSSL_CMP_MSG *ossl_cmp_pollReq_new(OSSL_CMP_CTX *ctx, int crid)
878 {
879     OSSL_CMP_MSG *msg = NULL;
880     OSSL_CMP_POLLREQ *preq = NULL;
881
882     if (!ossl_assert(ctx != NULL))
883         return NULL;
884
885     if ((msg = ossl_cmp_msg_create(ctx, OSSL_CMP_PKIBODY_POLLREQ)) == NULL)
886         goto err;
887
888     if ((preq = OSSL_CMP_POLLREQ_new()) == NULL
889             || !ASN1_INTEGER_set(preq->certReqId, crid)
890             || !sk_OSSL_CMP_POLLREQ_push(msg->body->value.pollReq, preq))
891         goto err;
892
893     preq = NULL;
894     if (!ossl_cmp_msg_protect(ctx, msg))
895         goto err;
896
897     return msg;
898
899  err:
900     ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_POLLREQ);
901     OSSL_CMP_POLLREQ_free(preq);
902     OSSL_CMP_MSG_free(msg);
903     return NULL;
904 }
905
906 OSSL_CMP_MSG *ossl_cmp_pollRep_new(OSSL_CMP_CTX *ctx, int crid,
907                                    int64_t poll_after)
908 {
909     OSSL_CMP_MSG *msg;
910     OSSL_CMP_POLLREP *prep;
911
912     if (!ossl_assert(ctx != NULL))
913         return NULL;
914
915     if ((msg = ossl_cmp_msg_create(ctx, OSSL_CMP_PKIBODY_POLLREP)) == NULL)
916         goto err;
917     if ((prep = OSSL_CMP_POLLREP_new()) == NULL)
918         goto err;
919     if (!sk_OSSL_CMP_POLLREP_push(msg->body->value.pollRep, prep))
920         goto err;
921     if (!ASN1_INTEGER_set(prep->certReqId, crid))
922         goto err;
923     if (!ASN1_INTEGER_set_int64(prep->checkAfter, poll_after))
924         goto err;
925
926     if (!ossl_cmp_msg_protect(ctx, msg))
927         goto err;
928     return msg;
929
930  err:
931     ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_POLLREP);
932     OSSL_CMP_MSG_free(msg);
933     return NULL;
934 }
935
936 /*-
937  * returns the status field of the RevRepContent with the given
938  * request/sequence id inside a revocation response.
939  * RevRepContent has the revocation statuses in same order as they were sent in
940  * RevReqContent.
941  * returns NULL on error
942  */
943 OSSL_CMP_PKISI *
944 ossl_cmp_revrepcontent_get_pkisi(OSSL_CMP_REVREPCONTENT *rrep, int rsid)
945 {
946     OSSL_CMP_PKISI *status;
947
948     if (!ossl_assert(rrep != NULL))
949         return NULL;
950
951     if ((status = sk_OSSL_CMP_PKISI_value(rrep->status, rsid)) != NULL)
952         return status;
953
954     ERR_raise(ERR_LIB_CMP, CMP_R_PKISTATUSINFO_NOT_FOUND);
955     return NULL;
956 }
957
958 /*
959  * returns the CertId field in the revCerts part of the RevRepContent
960  * with the given request/sequence id inside a revocation response.
961  * RevRepContent has the CertIds in same order as they were sent in
962  * RevReqContent.
963  * returns NULL on error
964  */
965 OSSL_CRMF_CERTID *
966 ossl_cmp_revrepcontent_get_CertId(OSSL_CMP_REVREPCONTENT *rrep, int rsid)
967 {
968     OSSL_CRMF_CERTID *cid = NULL;
969
970     if (!ossl_assert(rrep != NULL))
971         return NULL;
972
973     if ((cid = sk_OSSL_CRMF_CERTID_value(rrep->revCerts, rsid)) != NULL)
974         return cid;
975
976     ERR_raise(ERR_LIB_CMP, CMP_R_CERTID_NOT_FOUND);
977     return NULL;
978 }
979
980 static int suitable_rid(const ASN1_INTEGER *certReqId, int rid)
981 {
982     int trid;
983
984     if (rid == OSSL_CMP_CERTREQID_NONE)
985         return 1;
986
987     trid = ossl_cmp_asn1_get_int(certReqId);
988
989     if (trid == OSSL_CMP_CERTREQID_NONE) {
990         ERR_raise(ERR_LIB_CMP, CMP_R_BAD_REQUEST_ID);
991         return 0;
992     }
993     return rid == trid;
994 }
995
996 /*
997  * returns a pointer to the PollResponse with the given CertReqId
998  * (or the first one in case -1) inside a PollRepContent
999  * returns NULL on error or if no suitable PollResponse available
1000  */
1001 OSSL_CMP_POLLREP *
1002 ossl_cmp_pollrepcontent_get0_pollrep(const OSSL_CMP_POLLREPCONTENT *prc,
1003                                      int rid)
1004 {
1005     OSSL_CMP_POLLREP *pollRep = NULL;
1006     int i;
1007
1008     if (!ossl_assert(prc != NULL))
1009         return NULL;
1010
1011     for (i = 0; i < sk_OSSL_CMP_POLLREP_num(prc); i++) {
1012         pollRep = sk_OSSL_CMP_POLLREP_value(prc, i);
1013         if (suitable_rid(pollRep->certReqId, rid))
1014             return pollRep;
1015     }
1016
1017     ERR_raise_data(ERR_LIB_CMP, CMP_R_CERTRESPONSE_NOT_FOUND,
1018                    "expected certReqId = %d", rid);
1019     return NULL;
1020 }
1021
1022 /*
1023  * returns a pointer to the CertResponse with the given CertReqId
1024  * (or the first one in case -1) inside a CertRepMessage
1025  * returns NULL on error or if no suitable CertResponse available
1026  */
1027 OSSL_CMP_CERTRESPONSE *
1028 ossl_cmp_certrepmessage_get0_certresponse(const OSSL_CMP_CERTREPMESSAGE *crm,
1029                                           int rid)
1030 {
1031     OSSL_CMP_CERTRESPONSE *crep = NULL;
1032     int i;
1033
1034     if (!ossl_assert(crm != NULL && crm->response != NULL))
1035         return NULL;
1036
1037     for (i = 0; i < sk_OSSL_CMP_CERTRESPONSE_num(crm->response); i++) {
1038         crep = sk_OSSL_CMP_CERTRESPONSE_value(crm->response, i);
1039         if (suitable_rid(crep->certReqId, rid))
1040             return crep;
1041     }
1042
1043     ERR_raise_data(ERR_LIB_CMP, CMP_R_CERTRESPONSE_NOT_FOUND,
1044                    "expected certReqId = %d", rid);
1045     return NULL;
1046 }
1047
1048 /*-
1049  * Retrieve the newly enrolled certificate from the given certResponse crep.
1050  * In case of indirect POPO uses the libctx and propq from ctx and private key.
1051  * Returns a pointer to a copy of the found certificate, or NULL if not found.
1052  */
1053 X509 *ossl_cmp_certresponse_get1_cert(const OSSL_CMP_CERTRESPONSE *crep,
1054                                       const OSSL_CMP_CTX *ctx, EVP_PKEY *pkey)
1055 {
1056     OSSL_CMP_CERTORENCCERT *coec;
1057     X509 *crt = NULL;
1058
1059     if (!ossl_assert(crep != NULL && ctx != NULL))
1060         return NULL;
1061
1062     if (crep->certifiedKeyPair
1063             && (coec = crep->certifiedKeyPair->certOrEncCert) != NULL) {
1064         switch (coec->type) {
1065         case OSSL_CMP_CERTORENCCERT_CERTIFICATE:
1066             crt = X509_dup(coec->value.certificate);
1067             break;
1068         case OSSL_CMP_CERTORENCCERT_ENCRYPTEDCERT:
1069             /* cert encrypted for indirect PoP; RFC 4210, 5.2.8.2 */
1070             if (pkey == NULL) {
1071                 ERR_raise(ERR_LIB_CMP, CMP_R_MISSING_PRIVATE_KEY);
1072                 return NULL;
1073             }
1074             crt =
1075                 OSSL_CRMF_ENCRYPTEDVALUE_get1_encCert(coec->value.encryptedCert,
1076                                                       ctx->libctx, ctx->propq,
1077                                                       pkey);
1078             break;
1079         default:
1080             ERR_raise(ERR_LIB_CMP, CMP_R_UNKNOWN_CERT_TYPE);
1081             return NULL;
1082         }
1083     }
1084     if (crt == NULL)
1085         ERR_raise(ERR_LIB_CMP, CMP_R_CERTIFICATE_NOT_FOUND);
1086     else
1087         (void)ossl_x509_set0_libctx(crt, ctx->libctx, ctx->propq);
1088     return crt;
1089 }
1090
1091 int OSSL_CMP_MSG_update_transactionID(OSSL_CMP_CTX *ctx, OSSL_CMP_MSG *msg)
1092 {
1093     if (ctx == NULL || msg == NULL) {
1094         ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
1095         return 0;
1096     }
1097     if (!ossl_cmp_hdr_set_transactionID(ctx, msg->header))
1098         return 0;
1099     return msg->header->protectionAlg == NULL
1100             || ossl_cmp_msg_protect(ctx, msg);
1101 }
1102
1103 int OSSL_CMP_MSG_update_recipNonce(OSSL_CMP_CTX *ctx, OSSL_CMP_MSG *msg)
1104 {
1105     if (ctx == NULL || msg == NULL || msg->header == NULL) {
1106         ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
1107         return 0;
1108     }
1109     if (ctx->recipNonce == NULL) /* nothing to do for 1st msg in transaction */
1110         return 1;
1111     if (!ossl_cmp_asn1_octet_string_set1(&msg->header->recipNonce,
1112                                          ctx->recipNonce))
1113         return 0;
1114     return msg->header->protectionAlg == NULL || ossl_cmp_msg_protect(ctx, msg);
1115 }
1116
1117 OSSL_CMP_MSG *OSSL_CMP_MSG_read(const char *file, OSSL_LIB_CTX *libctx,
1118                                 const char *propq)
1119 {
1120     OSSL_CMP_MSG *msg;
1121     BIO *bio = NULL;
1122
1123     if (file == NULL) {
1124         ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
1125         return NULL;
1126     }
1127
1128     msg = OSSL_CMP_MSG_new(libctx, propq);
1129     if (msg == NULL) {
1130         ERR_raise(ERR_LIB_CMP, ERR_R_CMP_LIB);
1131         return NULL;
1132     }
1133
1134     if ((bio = BIO_new_file(file, "rb")) == NULL
1135             || d2i_OSSL_CMP_MSG_bio(bio, &msg) == NULL) {
1136         OSSL_CMP_MSG_free(msg);
1137         msg = NULL;
1138     }
1139     BIO_free(bio);
1140     return msg;
1141 }
1142
1143 int OSSL_CMP_MSG_write(const char *file, const OSSL_CMP_MSG *msg)
1144 {
1145     BIO *bio;
1146     int res;
1147
1148     if (file == NULL || msg == NULL) {
1149         ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
1150         return -1;
1151     }
1152
1153     bio = BIO_new_file(file, "wb");
1154     if (bio == NULL)
1155         return -2;
1156     res = i2d_OSSL_CMP_MSG_bio(bio, msg);
1157     BIO_free(bio);
1158     return res;
1159 }
1160
1161 OSSL_CMP_MSG *d2i_OSSL_CMP_MSG(OSSL_CMP_MSG **msg, const unsigned char **in,
1162                                long len)
1163 {
1164     OSSL_LIB_CTX *libctx = NULL;
1165     const char *propq = NULL;
1166
1167     if (msg != NULL && *msg != NULL) {
1168         libctx  = (*msg)->libctx;
1169         propq = (*msg)->propq;
1170     }
1171
1172     return (OSSL_CMP_MSG *)ASN1_item_d2i_ex((ASN1_VALUE **)msg, in, len,
1173                                             ASN1_ITEM_rptr(OSSL_CMP_MSG),
1174                                             libctx, propq);
1175 }
1176
1177 int i2d_OSSL_CMP_MSG(const OSSL_CMP_MSG *msg, unsigned char **out)
1178 {
1179     return ASN1_item_i2d((const ASN1_VALUE *)msg, out,
1180                          ASN1_ITEM_rptr(OSSL_CMP_MSG));
1181 }
1182
1183 OSSL_CMP_MSG *d2i_OSSL_CMP_MSG_bio(BIO *bio, OSSL_CMP_MSG **msg)
1184 {
1185     OSSL_LIB_CTX *libctx = NULL;
1186     const char *propq = NULL;
1187
1188     if (msg != NULL && *msg != NULL) {
1189         libctx  = (*msg)->libctx;
1190         propq = (*msg)->propq;
1191     }
1192
1193     return ASN1_item_d2i_bio_ex(ASN1_ITEM_rptr(OSSL_CMP_MSG), bio, msg, libctx,
1194                                 propq);
1195 }
1196
1197 int i2d_OSSL_CMP_MSG_bio(BIO *bio, const OSSL_CMP_MSG *msg)
1198 {
1199     return ASN1_i2d_bio_of(OSSL_CMP_MSG, i2d_OSSL_CMP_MSG, bio, msg);
1200 }