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(X509_EXTENSION)
40 DEFINE_STACK_OF(OSSL_CRMF_MSG)
43 * atyp = Attribute Type
45 * ctrlinf = "regCtrl" or "regInfo"
47 #define IMPLEMENT_CRMF_CTRL_FUNC(atyp, valt, ctrlinf) \
48 int OSSL_CRMF_MSG_set1_##ctrlinf##_##atyp(OSSL_CRMF_MSG *msg, \
51 OSSL_CRMF_ATTRIBUTETYPEANDVALUE *atav = NULL; \
53 if (msg == NULL || in == NULL) \
55 if ((atav = OSSL_CRMF_ATTRIBUTETYPEANDVALUE_new()) == NULL) \
57 if ((atav->type = OBJ_nid2obj(NID_id_##ctrlinf##_##atyp)) == NULL) \
59 if ((atav->value.atyp = valt##_dup(in)) == NULL) \
61 if (!OSSL_CRMF_MSG_push0_##ctrlinf(msg, atav)) \
65 OSSL_CRMF_ATTRIBUTETYPEANDVALUE_free(atav); \
71 * Pushes the given control attribute into the controls stack of a CertRequest
73 * returns 1 on success, 0 on error
75 static int OSSL_CRMF_MSG_push0_regCtrl(OSSL_CRMF_MSG *crm,
76 OSSL_CRMF_ATTRIBUTETYPEANDVALUE *ctrl)
80 if (crm == NULL || crm->certReq == NULL || ctrl == NULL) {
81 CRMFerr(CRMF_F_OSSL_CRMF_MSG_PUSH0_REGCTRL, CRMF_R_NULL_ARGUMENT);
85 if (crm->certReq->controls == NULL) {
86 crm->certReq->controls = sk_OSSL_CRMF_ATTRIBUTETYPEANDVALUE_new_null();
87 if (crm->certReq->controls == NULL)
91 if (!sk_OSSL_CRMF_ATTRIBUTETYPEANDVALUE_push(crm->certReq->controls, ctrl))
97 sk_OSSL_CRMF_ATTRIBUTETYPEANDVALUE_free(crm->certReq->controls);
98 crm->certReq->controls = NULL;
103 /* id-regCtrl-regToken Control (section 6.1) */
104 IMPLEMENT_CRMF_CTRL_FUNC(regToken, ASN1_STRING, regCtrl)
106 /* id-regCtrl-authenticator Control (section 6.2) */
107 #define ASN1_UTF8STRING_dup ASN1_STRING_dup
108 IMPLEMENT_CRMF_CTRL_FUNC(authenticator, ASN1_UTF8STRING, regCtrl)
110 int OSSL_CRMF_MSG_set0_SinglePubInfo(OSSL_CRMF_SINGLEPUBINFO *spi,
111 int method, GENERAL_NAME *nm)
114 || method < OSSL_CRMF_PUB_METHOD_DONTCARE
115 || method > OSSL_CRMF_PUB_METHOD_LDAP) {
116 CRMFerr(CRMF_F_OSSL_CRMF_MSG_SET0_SINGLEPUBINFO,
117 ERR_R_PASSED_INVALID_ARGUMENT);
121 if (!ASN1_INTEGER_set(spi->pubMethod, method))
123 GENERAL_NAME_free(spi->pubLocation);
124 spi->pubLocation = nm;
129 OSSL_CRMF_MSG_PKIPublicationInfo_push0_SinglePubInfo(OSSL_CRMF_PKIPUBLICATIONINFO *pi,
130 OSSL_CRMF_SINGLEPUBINFO *spi)
132 if (pi == NULL || spi == NULL) {
133 CRMFerr(CRMF_F_OSSL_CRMF_MSG_PKIPUBLICATIONINFO_PUSH0_SINGLEPUBINFO,
134 CRMF_R_NULL_ARGUMENT);
137 if (pi->pubInfos == NULL)
138 pi->pubInfos = sk_OSSL_CRMF_SINGLEPUBINFO_new_null();
139 if (pi->pubInfos == NULL)
142 return sk_OSSL_CRMF_SINGLEPUBINFO_push(pi->pubInfos, spi);
145 int OSSL_CRMF_MSG_set_PKIPublicationInfo_action(OSSL_CRMF_PKIPUBLICATIONINFO *pi,
149 || action < OSSL_CRMF_PUB_ACTION_DONTPUBLISH
150 || action > OSSL_CRMF_PUB_ACTION_PLEASEPUBLISH) {
151 CRMFerr(CRMF_F_OSSL_CRMF_MSG_SET_PKIPUBLICATIONINFO_ACTION,
152 ERR_R_PASSED_INVALID_ARGUMENT);
156 return ASN1_INTEGER_set(pi->action, action);
159 /* id-regCtrl-pkiPublicationInfo Control (section 6.3) */
160 IMPLEMENT_CRMF_CTRL_FUNC(pkiPublicationInfo, OSSL_CRMF_PKIPUBLICATIONINFO,
163 /* id-regCtrl-oldCertID Control (section 6.5) from the given */
164 IMPLEMENT_CRMF_CTRL_FUNC(oldCertID, OSSL_CRMF_CERTID, regCtrl)
166 OSSL_CRMF_CERTID *OSSL_CRMF_CERTID_gen(const X509_NAME *issuer,
167 const ASN1_INTEGER *serial)
169 OSSL_CRMF_CERTID *cid = NULL;
171 if (issuer == NULL || serial == NULL) {
172 CRMFerr(CRMF_F_OSSL_CRMF_CERTID_GEN, CRMF_R_NULL_ARGUMENT);
176 if ((cid = OSSL_CRMF_CERTID_new()) == NULL)
179 if (!X509_NAME_set(&cid->issuer->d.directoryName, issuer))
181 cid->issuer->type = GEN_DIRNAME;
183 ASN1_INTEGER_free(cid->serialNumber);
184 if ((cid->serialNumber = ASN1_INTEGER_dup(serial)) == NULL)
190 OSSL_CRMF_CERTID_free(cid);
195 * id-regCtrl-protocolEncrKey Control (section 6.6)
197 IMPLEMENT_CRMF_CTRL_FUNC(protocolEncrKey, X509_PUBKEY, regCtrl)
200 * Pushes the attribute given in regInfo in to the CertReqMsg->regInfo stack.
202 * returns 1 on success, 0 on error
204 static int OSSL_CRMF_MSG_push0_regInfo(OSSL_CRMF_MSG *crm,
205 OSSL_CRMF_ATTRIBUTETYPEANDVALUE *ri)
207 STACK_OF(OSSL_CRMF_ATTRIBUTETYPEANDVALUE) *info = NULL;
209 if (crm == NULL || ri == NULL) {
210 CRMFerr(CRMF_F_OSSL_CRMF_MSG_PUSH0_REGINFO, CRMF_R_NULL_ARGUMENT);
214 if (crm->regInfo == NULL)
215 crm->regInfo = info = sk_OSSL_CRMF_ATTRIBUTETYPEANDVALUE_new_null();
216 if (crm->regInfo == NULL)
218 if (!sk_OSSL_CRMF_ATTRIBUTETYPEANDVALUE_push(crm->regInfo, ri))
225 sk_OSSL_CRMF_ATTRIBUTETYPEANDVALUE_free(info);
229 /* id-regInfo-utf8Pairs to regInfo (section 7.1) */
230 IMPLEMENT_CRMF_CTRL_FUNC(utf8Pairs, ASN1_UTF8STRING, regInfo)
232 /* id-regInfo-certReq to regInfo (section 7.2) */
233 IMPLEMENT_CRMF_CTRL_FUNC(certReq, OSSL_CRMF_CERTREQUEST, regInfo)
236 /* retrieves the certificate template of crm */
237 OSSL_CRMF_CERTTEMPLATE *OSSL_CRMF_MSG_get0_tmpl(const OSSL_CRMF_MSG *crm)
239 if (crm == NULL || crm->certReq == NULL) {
240 CRMFerr(CRMF_F_OSSL_CRMF_MSG_GET0_TMPL, CRMF_R_NULL_ARGUMENT);
243 return crm->certReq->certTemplate;
247 int OSSL_CRMF_MSG_set0_validity(OSSL_CRMF_MSG *crm,
248 ASN1_TIME *notBefore, ASN1_TIME *notAfter)
250 OSSL_CRMF_OPTIONALVALIDITY *vld;
251 OSSL_CRMF_CERTTEMPLATE *tmpl = OSSL_CRMF_MSG_get0_tmpl(crm);
253 if (tmpl == NULL) { /* also crm == NULL implies this */
254 CRMFerr(CRMF_F_OSSL_CRMF_MSG_SET0_VALIDITY, CRMF_R_NULL_ARGUMENT);
258 if ((vld = OSSL_CRMF_OPTIONALVALIDITY_new()) == NULL)
260 vld->notBefore = notBefore;
261 vld->notAfter = notAfter;
262 tmpl->validity = vld;
267 int OSSL_CRMF_MSG_set_certReqId(OSSL_CRMF_MSG *crm, int rid)
269 if (crm == NULL || crm->certReq == NULL || crm->certReq->certReqId == NULL) {
270 CRMFerr(CRMF_F_OSSL_CRMF_MSG_SET_CERTREQID, CRMF_R_NULL_ARGUMENT);
274 return ASN1_INTEGER_set(crm->certReq->certReqId, rid);
277 /* get ASN.1 encoded integer, return -1 on error */
278 static int crmf_asn1_get_int(const ASN1_INTEGER *a)
282 if (!ASN1_INTEGER_get_int64(&res, a)) {
283 CRMFerr(0, ASN1_R_INVALID_NUMBER);
287 CRMFerr(0, ASN1_R_TOO_SMALL);
291 CRMFerr(0, ASN1_R_TOO_LARGE);
297 int OSSL_CRMF_MSG_get_certReqId(const OSSL_CRMF_MSG *crm)
299 if (crm == NULL || /* not really needed: */ crm->certReq == NULL) {
300 CRMFerr(CRMF_F_OSSL_CRMF_MSG_GET_CERTREQID, CRMF_R_NULL_ARGUMENT);
303 return crmf_asn1_get_int(crm->certReq->certReqId);
307 int OSSL_CRMF_MSG_set0_extensions(OSSL_CRMF_MSG *crm,
308 X509_EXTENSIONS *exts)
310 OSSL_CRMF_CERTTEMPLATE *tmpl = OSSL_CRMF_MSG_get0_tmpl(crm);
312 if (tmpl == NULL) { /* also crm == NULL implies this */
313 CRMFerr(CRMF_F_OSSL_CRMF_MSG_SET0_EXTENSIONS, CRMF_R_NULL_ARGUMENT);
317 if (sk_X509_EXTENSION_num(exts) == 0) {
318 sk_X509_EXTENSION_free(exts);
319 exts = NULL; /* do not include empty extensions list */
322 sk_X509_EXTENSION_pop_free(tmpl->extensions, X509_EXTENSION_free);
323 tmpl->extensions = exts;
328 int OSSL_CRMF_MSG_push0_extension(OSSL_CRMF_MSG *crm,
332 OSSL_CRMF_CERTTEMPLATE *tmpl = OSSL_CRMF_MSG_get0_tmpl(crm);
334 if (tmpl == NULL || ext == NULL) { /* also crm == NULL implies this */
335 CRMFerr(CRMF_F_OSSL_CRMF_MSG_PUSH0_EXTENSION, CRMF_R_NULL_ARGUMENT);
339 if (tmpl->extensions == NULL) {
340 if ((tmpl->extensions = sk_X509_EXTENSION_new_null()) == NULL)
345 if (!sk_X509_EXTENSION_push(tmpl->extensions, ext))
350 sk_X509_EXTENSION_free(tmpl->extensions);
351 tmpl->extensions = NULL;
356 static int create_popo_signature(OSSL_CRMF_POPOSIGNINGKEY *ps,
357 const OSSL_CRMF_CERTREQUEST *cr,
358 EVP_PKEY *pkey, const EVP_MD *digest,
359 OPENSSL_CTX *libctx, const char *propq)
361 if (ps == NULL || cr == NULL || pkey == NULL) {
362 CRMFerr(0, CRMF_R_NULL_ARGUMENT);
365 if (ps->poposkInput != NULL) {
366 /* TODO: support cases 1+2 defined in RFC 4211, section 4.1 */
367 CRMFerr(0, CRMF_R_POPOSKINPUT_NOT_SUPPORTED);
371 return ASN1_item_sign_with_libctx(ASN1_ITEM_rptr(OSSL_CRMF_CERTREQUEST),
372 ps->algorithmIdentifier, NULL,
373 ps->signature, cr, NULL, pkey, digest,
378 int OSSL_CRMF_MSG_create_popo(int meth, OSSL_CRMF_MSG *crm,
379 EVP_PKEY *pkey, const EVP_MD *digest,
380 OPENSSL_CTX *libctx, const char *propq)
382 OSSL_CRMF_POPO *pp = NULL;
383 ASN1_INTEGER *tag = NULL;
385 if (crm == NULL || (meth == OSSL_CRMF_POPO_SIGNATURE && pkey == NULL)) {
386 CRMFerr(CRMF_F_OSSL_CRMF_MSG_CREATE_POPO, CRMF_R_NULL_ARGUMENT);
390 if (meth == OSSL_CRMF_POPO_NONE)
392 if ((pp = OSSL_CRMF_POPO_new()) == NULL)
397 case OSSL_CRMF_POPO_RAVERIFIED:
398 if ((pp->value.raVerified = ASN1_NULL_new()) == NULL)
402 case OSSL_CRMF_POPO_SIGNATURE:
404 OSSL_CRMF_POPOSIGNINGKEY *ps = OSSL_CRMF_POPOSIGNINGKEY_new();
408 if (!create_popo_signature(ps, crm->certReq, pkey, digest,
410 OSSL_CRMF_POPOSIGNINGKEY_free(ps);
413 pp->value.signature = ps;
417 case OSSL_CRMF_POPO_KEYENC:
418 if ((pp->value.keyEncipherment = OSSL_CRMF_POPOPRIVKEY_new()) == NULL)
420 tag = ASN1_INTEGER_new();
421 pp->value.keyEncipherment->type =
422 OSSL_CRMF_POPOPRIVKEY_SUBSEQUENTMESSAGE;
423 pp->value.keyEncipherment->value.subsequentMessage = tag;
425 || !ASN1_INTEGER_set(tag, OSSL_CRMF_SUBSEQUENTMESSAGE_ENCRCERT))
430 CRMFerr(CRMF_F_OSSL_CRMF_MSG_CREATE_POPO,
431 CRMF_R_UNSUPPORTED_METHOD_FOR_CREATING_POPO);
436 OSSL_CRMF_POPO_free(crm->popo);
441 OSSL_CRMF_POPO_free(pp);
445 /* verifies the Proof-of-Possession of the request with the given rid in reqs */
446 int OSSL_CRMF_MSGS_verify_popo(const OSSL_CRMF_MSGS *reqs,
447 int rid, int acceptRAVerified,
448 OPENSSL_CTX *libctx, const char *propq)
450 OSSL_CRMF_MSG *req = NULL;
451 X509_PUBKEY *pubkey = NULL;
452 OSSL_CRMF_POPOSIGNINGKEY *sig = NULL;
456 if (reqs == NULL || (req = sk_OSSL_CRMF_MSG_value(reqs, rid)) == NULL) {
457 CRMFerr(CRMF_F_OSSL_CRMF_MSGS_VERIFY_POPO, CRMF_R_NULL_ARGUMENT);
461 if (req->popo == NULL) {
462 CRMFerr(0, CRMF_R_POPO_MISSING);
466 switch (req->popo->type) {
467 case OSSL_CRMF_POPO_RAVERIFIED:
468 if (!acceptRAVerified) {
469 CRMFerr(0, CRMF_R_POPO_RAVERIFIED_NOT_ACCEPTED);
473 case OSSL_CRMF_POPO_SIGNATURE:
474 pubkey = req->certReq->certTemplate->publicKey;
475 if (pubkey == NULL) {
476 CRMFerr(0, CRMF_R_POPO_MISSING_PUBLIC_KEY);
479 sig = req->popo->value.signature;
480 if (sig->poposkInput != NULL) {
482 * According to RFC 4211: publicKey contains a copy of
483 * the public key from the certificate template. This MUST be
484 * exactly the same value as contained in the certificate template.
486 if (sig->poposkInput->publicKey == NULL) {
487 CRMFerr(0, CRMF_R_POPO_MISSING_PUBLIC_KEY);
490 if (X509_PUBKEY_eq(pubkey, sig->poposkInput->publicKey) != 1) {
491 CRMFerr(0, CRMF_R_POPO_INCONSISTENT_PUBLIC_KEY);
495 * TODO check the contents of the authInfo sub-field,
496 * see RFC 4211 https://tools.ietf.org/html/rfc4211#section-4.1
498 it = ASN1_ITEM_rptr(OSSL_CRMF_POPOSIGNINGKEYINPUT);
499 asn = sig->poposkInput;
501 if (req->certReq->certTemplate->subject == NULL) {
502 CRMFerr(0, CRMF_R_POPO_MISSING_SUBJECT);
505 it = ASN1_ITEM_rptr(OSSL_CRMF_CERTREQUEST);
508 if (ASN1_item_verify_with_libctx(it, sig->algorithmIdentifier,
509 sig->signature, asn, NULL,
510 X509_PUBKEY_get0(pubkey),
514 case OSSL_CRMF_POPO_KEYENC:
516 * TODO: when OSSL_CMP_certrep_new() supports encrypted certs,
517 * return 1 if the type of req->popo->value.keyEncipherment
518 * is OSSL_CRMF_POPOPRIVKEY_SUBSEQUENTMESSAGE and
519 * its value.subsequentMessage == OSSL_CRMF_SUBSEQUENTMESSAGE_ENCRCERT
521 case OSSL_CRMF_POPO_KEYAGREE:
523 CRMFerr(CRMF_F_OSSL_CRMF_MSGS_VERIFY_POPO,
524 CRMF_R_UNSUPPORTED_POPO_METHOD);
530 /* retrieves the serialNumber of the given cert template or NULL on error */
532 *OSSL_CRMF_CERTTEMPLATE_get0_serialNumber(const OSSL_CRMF_CERTTEMPLATE *tmpl)
534 return tmpl != NULL ? tmpl->serialNumber : NULL;
537 /* retrieves the issuer name of the given cert template or NULL on error */
539 *OSSL_CRMF_CERTTEMPLATE_get0_issuer(const OSSL_CRMF_CERTTEMPLATE *tmpl)
541 return tmpl != NULL ? tmpl->issuer : NULL;
544 /* retrieves the issuer name of the given CertId or NULL on error */
545 const X509_NAME *OSSL_CRMF_CERTID_get0_issuer(const OSSL_CRMF_CERTID *cid)
547 return cid != NULL && cid->issuer->type == GEN_DIRNAME ?
548 cid->issuer->d.directoryName : NULL;
551 /* retrieves the serialNumber of the given CertId or NULL on error */
552 ASN1_INTEGER *OSSL_CRMF_CERTID_get0_serialNumber(const OSSL_CRMF_CERTID *cid)
554 return cid != NULL ? cid->serialNumber : NULL;
558 * fill in certificate template.
559 * Any value argument that is NULL will leave the respective field unchanged.
561 int OSSL_CRMF_CERTTEMPLATE_fill(OSSL_CRMF_CERTTEMPLATE *tmpl,
563 const X509_NAME *subject,
564 const X509_NAME *issuer,
565 const ASN1_INTEGER *serial)
568 CRMFerr(CRMF_F_OSSL_CRMF_CERTTEMPLATE_FILL, CRMF_R_NULL_ARGUMENT);
571 if (subject != NULL && !X509_NAME_set((X509_NAME **)&tmpl->subject, subject))
573 if (issuer != NULL && !X509_NAME_set((X509_NAME **)&tmpl->issuer, issuer))
575 if (serial != NULL) {
576 ASN1_INTEGER_free(tmpl->serialNumber);
577 if ((tmpl->serialNumber = ASN1_INTEGER_dup(serial)) == NULL)
580 if (pubkey != NULL && !X509_PUBKEY_set(&tmpl->publicKey, pubkey))
587 * Decrypts the certificate in the given encryptedValue using private key pkey.
588 * This is needed for the indirect PoP method as in RFC 4210 section 5.2.8.2.
590 * returns a pointer to the decrypted certificate
591 * returns NULL on error or if no certificate available
594 *OSSL_CRMF_ENCRYPTEDVALUE_get1_encCert(const OSSL_CRMF_ENCRYPTEDVALUE *ecert,
595 OPENSSL_CTX *libctx, const char *propq,
598 X509 *cert = NULL; /* decrypted certificate */
599 EVP_CIPHER_CTX *evp_ctx = NULL; /* context for symmetric encryption */
600 unsigned char *ek = NULL; /* decrypted symmetric encryption key */
601 size_t eksize = 0; /* size of decrypted symmetric encryption key */
602 const EVP_CIPHER *cipher = NULL; /* used cipher */
603 int cikeysize = 0; /* key size from cipher */
604 unsigned char *iv = NULL; /* initial vector for symmetric encryption */
605 unsigned char *outbuf = NULL; /* decryption output buffer */
606 const unsigned char *p = NULL; /* needed for decoding ASN1 */
607 int symmAlg = 0; /* NIDs for symmetric algorithm */
609 EVP_PKEY_CTX *pkctx = NULL; /* private key context */
611 if (ecert == NULL || ecert->symmAlg == NULL || ecert->encSymmKey == NULL
612 || ecert->encValue == NULL || pkey == NULL) {
613 CRMFerr(CRMF_F_OSSL_CRMF_ENCRYPTEDVALUE_GET1_ENCCERT,
614 CRMF_R_NULL_ARGUMENT);
617 if ((symmAlg = OBJ_obj2nid(ecert->symmAlg->algorithm)) == 0) {
618 CRMFerr(CRMF_F_OSSL_CRMF_ENCRYPTEDVALUE_GET1_ENCCERT,
619 CRMF_R_UNSUPPORTED_CIPHER);
622 /* select symmetric cipher based on algorithm given in message */
623 if ((cipher = EVP_get_cipherbynid(symmAlg)) == NULL) {
624 CRMFerr(CRMF_F_OSSL_CRMF_ENCRYPTEDVALUE_GET1_ENCCERT,
625 CRMF_R_UNSUPPORTED_CIPHER);
628 cikeysize = EVP_CIPHER_key_length(cipher);
629 /* first the symmetric key needs to be decrypted */
630 pkctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, propq);
631 if (pkctx != NULL && EVP_PKEY_decrypt_init(pkctx)) {
632 ASN1_BIT_STRING *encKey = ecert->encSymmKey;
636 if (EVP_PKEY_decrypt(pkctx, NULL, &eksize,
637 encKey->data, encKey->length) <= 0
638 || (ek = OPENSSL_malloc(eksize)) == NULL)
640 retval = EVP_PKEY_decrypt(pkctx, ek, &eksize,
641 encKey->data, encKey->length);
642 ERR_clear_error(); /* error state may have sensitive information */
643 failure = ~constant_time_is_zero_s(constant_time_msb(retval)
644 | constant_time_is_zero(retval));
645 failure |= ~constant_time_eq_s(eksize, (size_t)cikeysize);
647 CRMFerr(CRMF_F_OSSL_CRMF_ENCRYPTEDVALUE_GET1_ENCCERT,
648 CRMF_R_ERROR_DECRYPTING_SYMMETRIC_KEY);
654 if ((iv = OPENSSL_malloc(EVP_CIPHER_iv_length(cipher))) == NULL)
656 if (ASN1_TYPE_get_octetstring(ecert->symmAlg->parameter, iv,
657 EVP_CIPHER_iv_length(cipher))
658 != EVP_CIPHER_iv_length(cipher)) {
659 CRMFerr(CRMF_F_OSSL_CRMF_ENCRYPTEDVALUE_GET1_ENCCERT,
660 CRMF_R_MALFORMED_IV);
665 * d2i_X509 changes the given pointer, so use p for decoding the message and
666 * keep the original pointer in outbuf so the memory can be freed later
668 if ((p = outbuf = OPENSSL_malloc(ecert->encValue->length +
669 EVP_CIPHER_block_size(cipher))) == NULL
670 || (evp_ctx = EVP_CIPHER_CTX_new()) == NULL)
672 EVP_CIPHER_CTX_set_padding(evp_ctx, 0);
674 if (!EVP_DecryptInit(evp_ctx, cipher, ek, iv)
675 || !EVP_DecryptUpdate(evp_ctx, outbuf, &outlen,
676 ecert->encValue->data,
677 ecert->encValue->length)
678 || !EVP_DecryptFinal(evp_ctx, outbuf + outlen, &n)) {
679 CRMFerr(CRMF_F_OSSL_CRMF_ENCRYPTEDVALUE_GET1_ENCCERT,
680 CRMF_R_ERROR_DECRYPTING_CERTIFICATE);
685 /* convert decrypted certificate from DER to internal ASN.1 structure */
686 if ((cert = X509_new_with_libctx(libctx, propq)) == NULL)
688 if (d2i_X509(&cert, &p, outlen) == NULL)
689 CRMFerr(CRMF_F_OSSL_CRMF_ENCRYPTEDVALUE_GET1_ENCCERT,
690 CRMF_R_ERROR_DECODING_CERTIFICATE);
692 EVP_PKEY_CTX_free(pkctx);
693 OPENSSL_free(outbuf);
694 EVP_CIPHER_CTX_free(evp_ctx);
695 OPENSSL_clear_free(ek, eksize);