2 * Copyright 2007-2020 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright Nokia 2007-2018
4 * Copyright Siemens AG 2015-2019
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
11 * CRMF implementation by Martin Peylo, Miikka Viljanen, and David von Oheimb.
15 * This file contains the functions that handle the individual items inside
22 * The 0 functions use the supplied structure pointer directly in the parent and
23 * it will be freed up when the parent is freed.
25 * The 1 functions use a copy of the supplied structure pointer (or in some
26 * cases increases its link count) in the parent and so both should be freed up.
29 #include <openssl/asn1t.h>
31 #include "crmf_local.h"
32 #include "internal/constant_time.h"
34 /* explicit #includes not strictly needed since implied by the above: */
35 #include <openssl/crmf.h>
36 #include <openssl/err.h>
37 #include <openssl/evp.h>
39 DEFINE_STACK_OF(OSSL_CRMF_MSG)
42 * atyp = Attribute Type
44 * ctrlinf = "regCtrl" or "regInfo"
46 #define IMPLEMENT_CRMF_CTRL_FUNC(atyp, valt, ctrlinf) \
47 int OSSL_CRMF_MSG_set1_##ctrlinf##_##atyp(OSSL_CRMF_MSG *msg, \
50 OSSL_CRMF_ATTRIBUTETYPEANDVALUE *atav = NULL; \
52 if (msg == NULL || in == NULL) \
54 if ((atav = OSSL_CRMF_ATTRIBUTETYPEANDVALUE_new()) == NULL) \
56 if ((atav->type = OBJ_nid2obj(NID_id_##ctrlinf##_##atyp)) == NULL) \
58 if ((atav->value.atyp = valt##_dup(in)) == NULL) \
60 if (!OSSL_CRMF_MSG_push0_##ctrlinf(msg, atav)) \
64 OSSL_CRMF_ATTRIBUTETYPEANDVALUE_free(atav); \
70 * Pushes the given control attribute into the controls stack of a CertRequest
72 * returns 1 on success, 0 on error
74 static int OSSL_CRMF_MSG_push0_regCtrl(OSSL_CRMF_MSG *crm,
75 OSSL_CRMF_ATTRIBUTETYPEANDVALUE *ctrl)
79 if (crm == NULL || crm->certReq == NULL || ctrl == NULL) {
80 CRMFerr(CRMF_F_OSSL_CRMF_MSG_PUSH0_REGCTRL, CRMF_R_NULL_ARGUMENT);
84 if (crm->certReq->controls == NULL) {
85 crm->certReq->controls = sk_OSSL_CRMF_ATTRIBUTETYPEANDVALUE_new_null();
86 if (crm->certReq->controls == NULL)
90 if (!sk_OSSL_CRMF_ATTRIBUTETYPEANDVALUE_push(crm->certReq->controls, ctrl))
96 sk_OSSL_CRMF_ATTRIBUTETYPEANDVALUE_free(crm->certReq->controls);
97 crm->certReq->controls = NULL;
102 /* id-regCtrl-regToken Control (section 6.1) */
103 IMPLEMENT_CRMF_CTRL_FUNC(regToken, ASN1_STRING, regCtrl)
105 /* id-regCtrl-authenticator Control (section 6.2) */
106 #define ASN1_UTF8STRING_dup ASN1_STRING_dup
107 IMPLEMENT_CRMF_CTRL_FUNC(authenticator, ASN1_UTF8STRING, regCtrl)
109 int OSSL_CRMF_MSG_set0_SinglePubInfo(OSSL_CRMF_SINGLEPUBINFO *spi,
110 int method, GENERAL_NAME *nm)
113 || method < OSSL_CRMF_PUB_METHOD_DONTCARE
114 || method > OSSL_CRMF_PUB_METHOD_LDAP) {
115 CRMFerr(CRMF_F_OSSL_CRMF_MSG_SET0_SINGLEPUBINFO,
116 ERR_R_PASSED_INVALID_ARGUMENT);
120 if (!ASN1_INTEGER_set(spi->pubMethod, method))
122 GENERAL_NAME_free(spi->pubLocation);
123 spi->pubLocation = nm;
128 OSSL_CRMF_MSG_PKIPublicationInfo_push0_SinglePubInfo(OSSL_CRMF_PKIPUBLICATIONINFO *pi,
129 OSSL_CRMF_SINGLEPUBINFO *spi)
131 if (pi == NULL || spi == NULL) {
132 CRMFerr(CRMF_F_OSSL_CRMF_MSG_PKIPUBLICATIONINFO_PUSH0_SINGLEPUBINFO,
133 CRMF_R_NULL_ARGUMENT);
136 if (pi->pubInfos == NULL)
137 pi->pubInfos = sk_OSSL_CRMF_SINGLEPUBINFO_new_null();
138 if (pi->pubInfos == NULL)
141 return sk_OSSL_CRMF_SINGLEPUBINFO_push(pi->pubInfos, spi);
144 int OSSL_CRMF_MSG_set_PKIPublicationInfo_action(OSSL_CRMF_PKIPUBLICATIONINFO *pi,
148 || action < OSSL_CRMF_PUB_ACTION_DONTPUBLISH
149 || action > OSSL_CRMF_PUB_ACTION_PLEASEPUBLISH) {
150 CRMFerr(CRMF_F_OSSL_CRMF_MSG_SET_PKIPUBLICATIONINFO_ACTION,
151 ERR_R_PASSED_INVALID_ARGUMENT);
155 return ASN1_INTEGER_set(pi->action, action);
158 /* id-regCtrl-pkiPublicationInfo Control (section 6.3) */
159 IMPLEMENT_CRMF_CTRL_FUNC(pkiPublicationInfo, OSSL_CRMF_PKIPUBLICATIONINFO,
162 /* id-regCtrl-oldCertID Control (section 6.5) from the given */
163 IMPLEMENT_CRMF_CTRL_FUNC(oldCertID, OSSL_CRMF_CERTID, regCtrl)
165 OSSL_CRMF_CERTID *OSSL_CRMF_CERTID_gen(const X509_NAME *issuer,
166 const ASN1_INTEGER *serial)
168 OSSL_CRMF_CERTID *cid = NULL;
170 if (issuer == NULL || serial == NULL) {
171 CRMFerr(CRMF_F_OSSL_CRMF_CERTID_GEN, CRMF_R_NULL_ARGUMENT);
175 if ((cid = OSSL_CRMF_CERTID_new()) == NULL)
178 if (!X509_NAME_set(&cid->issuer->d.directoryName, issuer))
180 cid->issuer->type = GEN_DIRNAME;
182 ASN1_INTEGER_free(cid->serialNumber);
183 if ((cid->serialNumber = ASN1_INTEGER_dup(serial)) == NULL)
189 OSSL_CRMF_CERTID_free(cid);
194 * id-regCtrl-protocolEncrKey Control (section 6.6)
196 IMPLEMENT_CRMF_CTRL_FUNC(protocolEncrKey, X509_PUBKEY, regCtrl)
199 * Pushes the attribute given in regInfo in to the CertReqMsg->regInfo stack.
201 * returns 1 on success, 0 on error
203 static int OSSL_CRMF_MSG_push0_regInfo(OSSL_CRMF_MSG *crm,
204 OSSL_CRMF_ATTRIBUTETYPEANDVALUE *ri)
206 STACK_OF(OSSL_CRMF_ATTRIBUTETYPEANDVALUE) *info = NULL;
208 if (crm == NULL || ri == NULL) {
209 CRMFerr(CRMF_F_OSSL_CRMF_MSG_PUSH0_REGINFO, CRMF_R_NULL_ARGUMENT);
213 if (crm->regInfo == NULL)
214 crm->regInfo = info = sk_OSSL_CRMF_ATTRIBUTETYPEANDVALUE_new_null();
215 if (crm->regInfo == NULL)
217 if (!sk_OSSL_CRMF_ATTRIBUTETYPEANDVALUE_push(crm->regInfo, ri))
224 sk_OSSL_CRMF_ATTRIBUTETYPEANDVALUE_free(info);
228 /* id-regInfo-utf8Pairs to regInfo (section 7.1) */
229 IMPLEMENT_CRMF_CTRL_FUNC(utf8Pairs, ASN1_UTF8STRING, regInfo)
231 /* id-regInfo-certReq to regInfo (section 7.2) */
232 IMPLEMENT_CRMF_CTRL_FUNC(certReq, OSSL_CRMF_CERTREQUEST, regInfo)
235 /* retrieves the certificate template of crm */
236 OSSL_CRMF_CERTTEMPLATE *OSSL_CRMF_MSG_get0_tmpl(const OSSL_CRMF_MSG *crm)
238 if (crm == NULL || crm->certReq == NULL) {
239 CRMFerr(CRMF_F_OSSL_CRMF_MSG_GET0_TMPL, CRMF_R_NULL_ARGUMENT);
242 return crm->certReq->certTemplate;
246 int OSSL_CRMF_MSG_set0_validity(OSSL_CRMF_MSG *crm,
247 ASN1_TIME *notBefore, ASN1_TIME *notAfter)
249 OSSL_CRMF_OPTIONALVALIDITY *vld;
250 OSSL_CRMF_CERTTEMPLATE *tmpl = OSSL_CRMF_MSG_get0_tmpl(crm);
252 if (tmpl == NULL) { /* also crm == NULL implies this */
253 CRMFerr(CRMF_F_OSSL_CRMF_MSG_SET0_VALIDITY, CRMF_R_NULL_ARGUMENT);
257 if ((vld = OSSL_CRMF_OPTIONALVALIDITY_new()) == NULL)
259 vld->notBefore = notBefore;
260 vld->notAfter = notAfter;
261 tmpl->validity = vld;
266 int OSSL_CRMF_MSG_set_certReqId(OSSL_CRMF_MSG *crm, int rid)
268 if (crm == NULL || crm->certReq == NULL || crm->certReq->certReqId == NULL) {
269 CRMFerr(CRMF_F_OSSL_CRMF_MSG_SET_CERTREQID, CRMF_R_NULL_ARGUMENT);
273 return ASN1_INTEGER_set(crm->certReq->certReqId, rid);
276 /* get ASN.1 encoded integer, return -1 on error */
277 static int crmf_asn1_get_int(const ASN1_INTEGER *a)
281 if (!ASN1_INTEGER_get_int64(&res, a)) {
282 CRMFerr(0, ASN1_R_INVALID_NUMBER);
286 CRMFerr(0, ASN1_R_TOO_SMALL);
290 CRMFerr(0, ASN1_R_TOO_LARGE);
296 int OSSL_CRMF_MSG_get_certReqId(const OSSL_CRMF_MSG *crm)
298 if (crm == NULL || /* not really needed: */ crm->certReq == NULL) {
299 CRMFerr(CRMF_F_OSSL_CRMF_MSG_GET_CERTREQID, CRMF_R_NULL_ARGUMENT);
302 return crmf_asn1_get_int(crm->certReq->certReqId);
306 int OSSL_CRMF_MSG_set0_extensions(OSSL_CRMF_MSG *crm,
307 X509_EXTENSIONS *exts)
309 OSSL_CRMF_CERTTEMPLATE *tmpl = OSSL_CRMF_MSG_get0_tmpl(crm);
311 if (tmpl == NULL) { /* also crm == NULL implies this */
312 CRMFerr(CRMF_F_OSSL_CRMF_MSG_SET0_EXTENSIONS, CRMF_R_NULL_ARGUMENT);
316 if (sk_X509_EXTENSION_num(exts) == 0) {
317 sk_X509_EXTENSION_free(exts);
318 exts = NULL; /* do not include empty extensions list */
321 sk_X509_EXTENSION_pop_free(tmpl->extensions, X509_EXTENSION_free);
322 tmpl->extensions = exts;
327 int OSSL_CRMF_MSG_push0_extension(OSSL_CRMF_MSG *crm,
331 OSSL_CRMF_CERTTEMPLATE *tmpl = OSSL_CRMF_MSG_get0_tmpl(crm);
333 if (tmpl == NULL || ext == NULL) { /* also crm == NULL implies this */
334 CRMFerr(CRMF_F_OSSL_CRMF_MSG_PUSH0_EXTENSION, CRMF_R_NULL_ARGUMENT);
338 if (tmpl->extensions == NULL) {
339 if ((tmpl->extensions = sk_X509_EXTENSION_new_null()) == NULL)
344 if (!sk_X509_EXTENSION_push(tmpl->extensions, ext))
349 sk_X509_EXTENSION_free(tmpl->extensions);
350 tmpl->extensions = NULL;
355 static int create_popo_signature(OSSL_CRMF_POPOSIGNINGKEY *ps,
356 const OSSL_CRMF_CERTREQUEST *cr,
357 EVP_PKEY *pkey, const EVP_MD *digest,
358 OPENSSL_CTX *libctx, const char *propq)
360 if (ps == NULL || cr == NULL || pkey == NULL) {
361 CRMFerr(0, CRMF_R_NULL_ARGUMENT);
364 if (ps->poposkInput != NULL) {
365 /* TODO: support cases 1+2 defined in RFC 4211, section 4.1 */
366 CRMFerr(0, CRMF_R_POPOSKINPUT_NOT_SUPPORTED);
370 return ASN1_item_sign_with_libctx(ASN1_ITEM_rptr(OSSL_CRMF_CERTREQUEST),
371 ps->algorithmIdentifier, NULL,
372 ps->signature, cr, NULL, pkey, digest,
377 int OSSL_CRMF_MSG_create_popo(int meth, OSSL_CRMF_MSG *crm,
378 EVP_PKEY *pkey, const EVP_MD *digest,
379 OPENSSL_CTX *libctx, const char *propq)
381 OSSL_CRMF_POPO *pp = NULL;
382 ASN1_INTEGER *tag = NULL;
384 if (crm == NULL || (meth == OSSL_CRMF_POPO_SIGNATURE && pkey == NULL)) {
385 CRMFerr(CRMF_F_OSSL_CRMF_MSG_CREATE_POPO, CRMF_R_NULL_ARGUMENT);
389 if (meth == OSSL_CRMF_POPO_NONE)
391 if ((pp = OSSL_CRMF_POPO_new()) == NULL)
396 case OSSL_CRMF_POPO_RAVERIFIED:
397 if ((pp->value.raVerified = ASN1_NULL_new()) == NULL)
401 case OSSL_CRMF_POPO_SIGNATURE:
403 OSSL_CRMF_POPOSIGNINGKEY *ps = OSSL_CRMF_POPOSIGNINGKEY_new();
407 if (!create_popo_signature(ps, crm->certReq, pkey, digest,
409 OSSL_CRMF_POPOSIGNINGKEY_free(ps);
412 pp->value.signature = ps;
416 case OSSL_CRMF_POPO_KEYENC:
417 if ((pp->value.keyEncipherment = OSSL_CRMF_POPOPRIVKEY_new()) == NULL)
419 tag = ASN1_INTEGER_new();
420 pp->value.keyEncipherment->type =
421 OSSL_CRMF_POPOPRIVKEY_SUBSEQUENTMESSAGE;
422 pp->value.keyEncipherment->value.subsequentMessage = tag;
424 || !ASN1_INTEGER_set(tag, OSSL_CRMF_SUBSEQUENTMESSAGE_ENCRCERT))
429 CRMFerr(CRMF_F_OSSL_CRMF_MSG_CREATE_POPO,
430 CRMF_R_UNSUPPORTED_METHOD_FOR_CREATING_POPO);
435 OSSL_CRMF_POPO_free(crm->popo);
440 OSSL_CRMF_POPO_free(pp);
444 /* verifies the Proof-of-Possession of the request with the given rid in reqs */
445 int OSSL_CRMF_MSGS_verify_popo(const OSSL_CRMF_MSGS *reqs,
446 int rid, int acceptRAVerified,
447 OPENSSL_CTX *libctx, const char *propq)
449 OSSL_CRMF_MSG *req = NULL;
450 X509_PUBKEY *pubkey = NULL;
451 OSSL_CRMF_POPOSIGNINGKEY *sig = NULL;
455 if (reqs == NULL || (req = sk_OSSL_CRMF_MSG_value(reqs, rid)) == NULL) {
456 CRMFerr(CRMF_F_OSSL_CRMF_MSGS_VERIFY_POPO, CRMF_R_NULL_ARGUMENT);
460 if (req->popo == NULL) {
461 CRMFerr(0, CRMF_R_POPO_MISSING);
465 switch (req->popo->type) {
466 case OSSL_CRMF_POPO_RAVERIFIED:
467 if (!acceptRAVerified) {
468 CRMFerr(0, CRMF_R_POPO_RAVERIFIED_NOT_ACCEPTED);
472 case OSSL_CRMF_POPO_SIGNATURE:
473 pubkey = req->certReq->certTemplate->publicKey;
474 if (pubkey == NULL) {
475 CRMFerr(0, CRMF_R_POPO_MISSING_PUBLIC_KEY);
478 sig = req->popo->value.signature;
479 if (sig->poposkInput != NULL) {
481 * According to RFC 4211: publicKey contains a copy of
482 * the public key from the certificate template. This MUST be
483 * exactly the same value as contained in the certificate template.
485 if (sig->poposkInput->publicKey == NULL) {
486 CRMFerr(0, CRMF_R_POPO_MISSING_PUBLIC_KEY);
489 if (X509_PUBKEY_eq(pubkey, sig->poposkInput->publicKey) != 1) {
490 CRMFerr(0, CRMF_R_POPO_INCONSISTENT_PUBLIC_KEY);
494 * TODO check the contents of the authInfo sub-field,
495 * see RFC 4211 https://tools.ietf.org/html/rfc4211#section-4.1
497 it = ASN1_ITEM_rptr(OSSL_CRMF_POPOSIGNINGKEYINPUT);
498 asn = sig->poposkInput;
500 if (req->certReq->certTemplate->subject == NULL) {
501 CRMFerr(0, CRMF_R_POPO_MISSING_SUBJECT);
504 it = ASN1_ITEM_rptr(OSSL_CRMF_CERTREQUEST);
507 if (ASN1_item_verify_with_libctx(it, sig->algorithmIdentifier,
508 sig->signature, asn, NULL,
509 X509_PUBKEY_get0(pubkey),
513 case OSSL_CRMF_POPO_KEYENC:
515 * TODO: when OSSL_CMP_certrep_new() supports encrypted certs,
516 * return 1 if the type of req->popo->value.keyEncipherment
517 * is OSSL_CRMF_POPOPRIVKEY_SUBSEQUENTMESSAGE and
518 * its value.subsequentMessage == OSSL_CRMF_SUBSEQUENTMESSAGE_ENCRCERT
520 case OSSL_CRMF_POPO_KEYAGREE:
522 CRMFerr(CRMF_F_OSSL_CRMF_MSGS_VERIFY_POPO,
523 CRMF_R_UNSUPPORTED_POPO_METHOD);
529 /* retrieves the serialNumber of the given cert template or NULL on error */
531 *OSSL_CRMF_CERTTEMPLATE_get0_serialNumber(const OSSL_CRMF_CERTTEMPLATE *tmpl)
533 return tmpl != NULL ? tmpl->serialNumber : NULL;
536 /* retrieves the issuer name of the given cert template or NULL on error */
538 *OSSL_CRMF_CERTTEMPLATE_get0_issuer(const OSSL_CRMF_CERTTEMPLATE *tmpl)
540 return tmpl != NULL ? tmpl->issuer : NULL;
543 /* retrieves the issuer name of the given CertId or NULL on error */
544 const X509_NAME *OSSL_CRMF_CERTID_get0_issuer(const OSSL_CRMF_CERTID *cid)
546 return cid != NULL && cid->issuer->type == GEN_DIRNAME ?
547 cid->issuer->d.directoryName : NULL;
550 /* retrieves the serialNumber of the given CertId or NULL on error */
551 ASN1_INTEGER *OSSL_CRMF_CERTID_get0_serialNumber(const OSSL_CRMF_CERTID *cid)
553 return cid != NULL ? cid->serialNumber : NULL;
557 * fill in certificate template.
558 * Any value argument that is NULL will leave the respective field unchanged.
560 int OSSL_CRMF_CERTTEMPLATE_fill(OSSL_CRMF_CERTTEMPLATE *tmpl,
562 const X509_NAME *subject,
563 const X509_NAME *issuer,
564 const ASN1_INTEGER *serial)
567 CRMFerr(CRMF_F_OSSL_CRMF_CERTTEMPLATE_FILL, CRMF_R_NULL_ARGUMENT);
570 if (subject != NULL && !X509_NAME_set((X509_NAME **)&tmpl->subject, subject))
572 if (issuer != NULL && !X509_NAME_set((X509_NAME **)&tmpl->issuer, issuer))
574 if (serial != NULL) {
575 ASN1_INTEGER_free(tmpl->serialNumber);
576 if ((tmpl->serialNumber = ASN1_INTEGER_dup(serial)) == NULL)
579 if (pubkey != NULL && !X509_PUBKEY_set(&tmpl->publicKey, pubkey))
586 * Decrypts the certificate in the given encryptedValue using private key pkey.
587 * This is needed for the indirect PoP method as in RFC 4210 section 5.2.8.2.
589 * returns a pointer to the decrypted certificate
590 * returns NULL on error or if no certificate available
593 *OSSL_CRMF_ENCRYPTEDVALUE_get1_encCert(const OSSL_CRMF_ENCRYPTEDVALUE *ecert,
594 OPENSSL_CTX *libctx, const char *propq,
597 X509 *cert = NULL; /* decrypted certificate */
598 EVP_CIPHER_CTX *evp_ctx = NULL; /* context for symmetric encryption */
599 unsigned char *ek = NULL; /* decrypted symmetric encryption key */
600 size_t eksize = 0; /* size of decrypted symmetric encryption key */
601 const EVP_CIPHER *cipher = NULL; /* used cipher */
602 int cikeysize = 0; /* key size from cipher */
603 unsigned char *iv = NULL; /* initial vector for symmetric encryption */
604 unsigned char *outbuf = NULL; /* decryption output buffer */
605 const unsigned char *p = NULL; /* needed for decoding ASN1 */
606 int symmAlg = 0; /* NIDs for symmetric algorithm */
608 EVP_PKEY_CTX *pkctx = NULL; /* private key context */
610 if (ecert == NULL || ecert->symmAlg == NULL || ecert->encSymmKey == NULL
611 || ecert->encValue == NULL || pkey == NULL) {
612 CRMFerr(CRMF_F_OSSL_CRMF_ENCRYPTEDVALUE_GET1_ENCCERT,
613 CRMF_R_NULL_ARGUMENT);
616 if ((symmAlg = OBJ_obj2nid(ecert->symmAlg->algorithm)) == 0) {
617 CRMFerr(CRMF_F_OSSL_CRMF_ENCRYPTEDVALUE_GET1_ENCCERT,
618 CRMF_R_UNSUPPORTED_CIPHER);
621 /* select symmetric cipher based on algorithm given in message */
622 if ((cipher = EVP_get_cipherbynid(symmAlg)) == NULL) {
623 CRMFerr(CRMF_F_OSSL_CRMF_ENCRYPTEDVALUE_GET1_ENCCERT,
624 CRMF_R_UNSUPPORTED_CIPHER);
627 cikeysize = EVP_CIPHER_key_length(cipher);
628 /* first the symmetric key needs to be decrypted */
629 pkctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, propq);
630 if (pkctx != NULL && EVP_PKEY_decrypt_init(pkctx)) {
631 ASN1_BIT_STRING *encKey = ecert->encSymmKey;
635 if (EVP_PKEY_decrypt(pkctx, NULL, &eksize,
636 encKey->data, encKey->length) <= 0
637 || (ek = OPENSSL_malloc(eksize)) == NULL)
639 retval = EVP_PKEY_decrypt(pkctx, ek, &eksize,
640 encKey->data, encKey->length);
641 ERR_clear_error(); /* error state may have sensitive information */
642 failure = ~constant_time_is_zero_s(constant_time_msb(retval)
643 | constant_time_is_zero(retval));
644 failure |= ~constant_time_eq_s(eksize, (size_t)cikeysize);
646 CRMFerr(CRMF_F_OSSL_CRMF_ENCRYPTEDVALUE_GET1_ENCCERT,
647 CRMF_R_ERROR_DECRYPTING_SYMMETRIC_KEY);
653 if ((iv = OPENSSL_malloc(EVP_CIPHER_iv_length(cipher))) == NULL)
655 if (ASN1_TYPE_get_octetstring(ecert->symmAlg->parameter, iv,
656 EVP_CIPHER_iv_length(cipher))
657 != EVP_CIPHER_iv_length(cipher)) {
658 CRMFerr(CRMF_F_OSSL_CRMF_ENCRYPTEDVALUE_GET1_ENCCERT,
659 CRMF_R_MALFORMED_IV);
664 * d2i_X509 changes the given pointer, so use p for decoding the message and
665 * keep the original pointer in outbuf so the memory can be freed later
667 if ((p = outbuf = OPENSSL_malloc(ecert->encValue->length +
668 EVP_CIPHER_block_size(cipher))) == NULL
669 || (evp_ctx = EVP_CIPHER_CTX_new()) == NULL)
671 EVP_CIPHER_CTX_set_padding(evp_ctx, 0);
673 if (!EVP_DecryptInit(evp_ctx, cipher, ek, iv)
674 || !EVP_DecryptUpdate(evp_ctx, outbuf, &outlen,
675 ecert->encValue->data,
676 ecert->encValue->length)
677 || !EVP_DecryptFinal(evp_ctx, outbuf + outlen, &n)) {
678 CRMFerr(CRMF_F_OSSL_CRMF_ENCRYPTEDVALUE_GET1_ENCCERT,
679 CRMF_R_ERROR_DECRYPTING_CERTIFICATE);
684 /* convert decrypted certificate from DER to internal ASN.1 structure */
685 if ((cert = X509_new_with_libctx(libctx, propq)) == NULL)
687 if (d2i_X509(&cert, &p, outlen) == NULL)
688 CRMFerr(CRMF_F_OSSL_CRMF_ENCRYPTEDVALUE_GET1_ENCCERT,
689 CRMF_R_ERROR_DECODING_CERTIFICATE);
691 EVP_PKEY_CTX_free(pkctx);
692 OPENSSL_free(outbuf);
693 EVP_CIPHER_CTX_free(evp_ctx);
694 OPENSSL_clear_free(ek, eksize);