2 * Copyright 2007-2021 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"
33 #include "internal/sizes.h"
35 /* explicit #includes not strictly needed since implied by the above: */
36 #include <openssl/crmf.h>
37 #include <openssl/err.h>
38 #include <openssl/evp.h>
41 * atyp = Attribute Type
43 * ctrlinf = "regCtrl" or "regInfo"
45 #define IMPLEMENT_CRMF_CTRL_FUNC(atyp, valt, ctrlinf) \
46 int OSSL_CRMF_MSG_set1_##ctrlinf##_##atyp(OSSL_CRMF_MSG *msg, \
49 OSSL_CRMF_ATTRIBUTETYPEANDVALUE *atav = NULL; \
51 if (msg == NULL || in == NULL) \
53 if ((atav = OSSL_CRMF_ATTRIBUTETYPEANDVALUE_new()) == NULL) \
55 if ((atav->type = OBJ_nid2obj(NID_id_##ctrlinf##_##atyp)) == NULL) \
57 if ((atav->value.atyp = valt##_dup(in)) == NULL) \
59 if (!OSSL_CRMF_MSG_push0_##ctrlinf(msg, atav)) \
63 OSSL_CRMF_ATTRIBUTETYPEANDVALUE_free(atav); \
69 * Pushes the given control attribute into the controls stack of a CertRequest
71 * returns 1 on success, 0 on error
73 static int OSSL_CRMF_MSG_push0_regCtrl(OSSL_CRMF_MSG
*crm
,
74 OSSL_CRMF_ATTRIBUTETYPEANDVALUE
*ctrl
)
78 if (crm
== NULL
|| crm
->certReq
== NULL
|| ctrl
== NULL
) {
79 ERR_raise(ERR_LIB_CRMF
, CRMF_R_NULL_ARGUMENT
);
83 if (crm
->certReq
->controls
== NULL
) {
84 crm
->certReq
->controls
= sk_OSSL_CRMF_ATTRIBUTETYPEANDVALUE_new_null();
85 if (crm
->certReq
->controls
== NULL
)
89 if (!sk_OSSL_CRMF_ATTRIBUTETYPEANDVALUE_push(crm
->certReq
->controls
, ctrl
))
95 sk_OSSL_CRMF_ATTRIBUTETYPEANDVALUE_free(crm
->certReq
->controls
);
96 crm
->certReq
->controls
= NULL
;
101 /* id-regCtrl-regToken Control (section 6.1) */
102 IMPLEMENT_CRMF_CTRL_FUNC(regToken
, ASN1_STRING
, regCtrl
)
104 /* id-regCtrl-authenticator Control (section 6.2) */
105 #define ASN1_UTF8STRING_dup ASN1_STRING_dup
106 IMPLEMENT_CRMF_CTRL_FUNC(authenticator
, ASN1_UTF8STRING
, regCtrl
)
108 int OSSL_CRMF_MSG_set0_SinglePubInfo(OSSL_CRMF_SINGLEPUBINFO
*spi
,
109 int method
, GENERAL_NAME
*nm
)
112 || method
< OSSL_CRMF_PUB_METHOD_DONTCARE
113 || method
> OSSL_CRMF_PUB_METHOD_LDAP
) {
114 ERR_raise(ERR_LIB_CRMF
, ERR_R_PASSED_INVALID_ARGUMENT
);
118 if (!ASN1_INTEGER_set(spi
->pubMethod
, method
))
120 GENERAL_NAME_free(spi
->pubLocation
);
121 spi
->pubLocation
= nm
;
126 OSSL_CRMF_MSG_PKIPublicationInfo_push0_SinglePubInfo(OSSL_CRMF_PKIPUBLICATIONINFO
*pi
,
127 OSSL_CRMF_SINGLEPUBINFO
*spi
)
129 if (pi
== NULL
|| spi
== NULL
) {
130 ERR_raise(ERR_LIB_CRMF
, CRMF_R_NULL_ARGUMENT
);
133 if (pi
->pubInfos
== NULL
)
134 pi
->pubInfos
= sk_OSSL_CRMF_SINGLEPUBINFO_new_null();
135 if (pi
->pubInfos
== NULL
)
138 return sk_OSSL_CRMF_SINGLEPUBINFO_push(pi
->pubInfos
, spi
);
141 int OSSL_CRMF_MSG_set_PKIPublicationInfo_action(OSSL_CRMF_PKIPUBLICATIONINFO
*pi
,
145 || action
< OSSL_CRMF_PUB_ACTION_DONTPUBLISH
146 || action
> OSSL_CRMF_PUB_ACTION_PLEASEPUBLISH
) {
147 ERR_raise(ERR_LIB_CRMF
, ERR_R_PASSED_INVALID_ARGUMENT
);
151 return ASN1_INTEGER_set(pi
->action
, action
);
154 /* id-regCtrl-pkiPublicationInfo Control (section 6.3) */
155 IMPLEMENT_CRMF_CTRL_FUNC(pkiPublicationInfo
, OSSL_CRMF_PKIPUBLICATIONINFO
,
158 /* id-regCtrl-oldCertID Control (section 6.5) from the given */
159 IMPLEMENT_CRMF_CTRL_FUNC(oldCertID
, OSSL_CRMF_CERTID
, regCtrl
)
161 OSSL_CRMF_CERTID
*OSSL_CRMF_CERTID_gen(const X509_NAME
*issuer
,
162 const ASN1_INTEGER
*serial
)
164 OSSL_CRMF_CERTID
*cid
= NULL
;
166 if (issuer
== NULL
|| serial
== NULL
) {
167 ERR_raise(ERR_LIB_CRMF
, CRMF_R_NULL_ARGUMENT
);
171 if ((cid
= OSSL_CRMF_CERTID_new()) == NULL
)
174 if (!X509_NAME_set(&cid
->issuer
->d
.directoryName
, issuer
))
176 cid
->issuer
->type
= GEN_DIRNAME
;
178 ASN1_INTEGER_free(cid
->serialNumber
);
179 if ((cid
->serialNumber
= ASN1_INTEGER_dup(serial
)) == NULL
)
185 OSSL_CRMF_CERTID_free(cid
);
190 * id-regCtrl-protocolEncrKey Control (section 6.6)
192 IMPLEMENT_CRMF_CTRL_FUNC(protocolEncrKey
, X509_PUBKEY
, regCtrl
)
195 * Pushes the attribute given in regInfo in to the CertReqMsg->regInfo stack.
197 * returns 1 on success, 0 on error
199 static int OSSL_CRMF_MSG_push0_regInfo(OSSL_CRMF_MSG
*crm
,
200 OSSL_CRMF_ATTRIBUTETYPEANDVALUE
*ri
)
202 STACK_OF(OSSL_CRMF_ATTRIBUTETYPEANDVALUE
) *info
= NULL
;
204 if (crm
== NULL
|| ri
== NULL
) {
205 ERR_raise(ERR_LIB_CRMF
, CRMF_R_NULL_ARGUMENT
);
209 if (crm
->regInfo
== NULL
)
210 crm
->regInfo
= info
= sk_OSSL_CRMF_ATTRIBUTETYPEANDVALUE_new_null();
211 if (crm
->regInfo
== NULL
)
213 if (!sk_OSSL_CRMF_ATTRIBUTETYPEANDVALUE_push(crm
->regInfo
, ri
))
220 sk_OSSL_CRMF_ATTRIBUTETYPEANDVALUE_free(info
);
224 /* id-regInfo-utf8Pairs to regInfo (section 7.1) */
225 IMPLEMENT_CRMF_CTRL_FUNC(utf8Pairs
, ASN1_UTF8STRING
, regInfo
)
227 /* id-regInfo-certReq to regInfo (section 7.2) */
228 IMPLEMENT_CRMF_CTRL_FUNC(certReq
, OSSL_CRMF_CERTREQUEST
, regInfo
)
231 /* retrieves the certificate template of crm */
232 OSSL_CRMF_CERTTEMPLATE
*OSSL_CRMF_MSG_get0_tmpl(const OSSL_CRMF_MSG
*crm
)
234 if (crm
== NULL
|| crm
->certReq
== NULL
) {
235 ERR_raise(ERR_LIB_CRMF
, CRMF_R_NULL_ARGUMENT
);
238 return crm
->certReq
->certTemplate
;
242 int OSSL_CRMF_MSG_set0_validity(OSSL_CRMF_MSG
*crm
,
243 ASN1_TIME
*notBefore
, ASN1_TIME
*notAfter
)
245 OSSL_CRMF_OPTIONALVALIDITY
*vld
;
246 OSSL_CRMF_CERTTEMPLATE
*tmpl
= OSSL_CRMF_MSG_get0_tmpl(crm
);
248 if (tmpl
== NULL
) { /* also crm == NULL implies this */
249 ERR_raise(ERR_LIB_CRMF
, CRMF_R_NULL_ARGUMENT
);
253 if ((vld
= OSSL_CRMF_OPTIONALVALIDITY_new()) == NULL
)
255 vld
->notBefore
= notBefore
;
256 vld
->notAfter
= notAfter
;
257 tmpl
->validity
= vld
;
262 int OSSL_CRMF_MSG_set_certReqId(OSSL_CRMF_MSG
*crm
, int rid
)
264 if (crm
== NULL
|| crm
->certReq
== NULL
|| crm
->certReq
->certReqId
== NULL
) {
265 ERR_raise(ERR_LIB_CRMF
, CRMF_R_NULL_ARGUMENT
);
269 return ASN1_INTEGER_set(crm
->certReq
->certReqId
, rid
);
272 /* get ASN.1 encoded integer, return -1 on error */
273 static int crmf_asn1_get_int(const ASN1_INTEGER
*a
)
277 if (!ASN1_INTEGER_get_int64(&res
, a
)) {
278 ERR_raise(ERR_LIB_CRMF
, ASN1_R_INVALID_NUMBER
);
282 ERR_raise(ERR_LIB_CRMF
, ASN1_R_TOO_SMALL
);
286 ERR_raise(ERR_LIB_CRMF
, ASN1_R_TOO_LARGE
);
292 int OSSL_CRMF_MSG_get_certReqId(const OSSL_CRMF_MSG
*crm
)
294 if (crm
== NULL
|| /* not really needed: */ crm
->certReq
== NULL
) {
295 ERR_raise(ERR_LIB_CRMF
, CRMF_R_NULL_ARGUMENT
);
298 return crmf_asn1_get_int(crm
->certReq
->certReqId
);
302 int OSSL_CRMF_MSG_set0_extensions(OSSL_CRMF_MSG
*crm
,
303 X509_EXTENSIONS
*exts
)
305 OSSL_CRMF_CERTTEMPLATE
*tmpl
= OSSL_CRMF_MSG_get0_tmpl(crm
);
307 if (tmpl
== NULL
) { /* also crm == NULL implies this */
308 ERR_raise(ERR_LIB_CRMF
, CRMF_R_NULL_ARGUMENT
);
312 if (sk_X509_EXTENSION_num(exts
) == 0) {
313 sk_X509_EXTENSION_free(exts
);
314 exts
= NULL
; /* do not include empty extensions list */
317 sk_X509_EXTENSION_pop_free(tmpl
->extensions
, X509_EXTENSION_free
);
318 tmpl
->extensions
= exts
;
323 int OSSL_CRMF_MSG_push0_extension(OSSL_CRMF_MSG
*crm
,
327 OSSL_CRMF_CERTTEMPLATE
*tmpl
= OSSL_CRMF_MSG_get0_tmpl(crm
);
329 if (tmpl
== NULL
|| ext
== NULL
) { /* also crm == NULL implies this */
330 ERR_raise(ERR_LIB_CRMF
, CRMF_R_NULL_ARGUMENT
);
334 if (tmpl
->extensions
== NULL
) {
335 if ((tmpl
->extensions
= sk_X509_EXTENSION_new_null()) == NULL
)
340 if (!sk_X509_EXTENSION_push(tmpl
->extensions
, ext
))
345 sk_X509_EXTENSION_free(tmpl
->extensions
);
346 tmpl
->extensions
= NULL
;
351 static int create_popo_signature(OSSL_CRMF_POPOSIGNINGKEY
*ps
,
352 const OSSL_CRMF_CERTREQUEST
*cr
,
353 EVP_PKEY
*pkey
, const EVP_MD
*digest
,
354 OSSL_LIB_CTX
*libctx
, const char *propq
)
356 if (ps
== NULL
|| cr
== NULL
|| pkey
== NULL
) {
357 ERR_raise(ERR_LIB_CRMF
, CRMF_R_NULL_ARGUMENT
);
360 if (ps
->poposkInput
!= NULL
) {
361 /* We do not support cases 1+2 defined in RFC 4211, section 4.1 */
362 ERR_raise(ERR_LIB_CRMF
, CRMF_R_POPOSKINPUT_NOT_SUPPORTED
);
366 return ASN1_item_sign_ex(ASN1_ITEM_rptr(OSSL_CRMF_CERTREQUEST
),
367 ps
->algorithmIdentifier
, NULL
, ps
->signature
, cr
,
368 NULL
, pkey
, digest
, libctx
, propq
);
372 int OSSL_CRMF_MSG_create_popo(int meth
, OSSL_CRMF_MSG
*crm
,
373 EVP_PKEY
*pkey
, const EVP_MD
*digest
,
374 OSSL_LIB_CTX
*libctx
, const char *propq
)
376 OSSL_CRMF_POPO
*pp
= NULL
;
377 ASN1_INTEGER
*tag
= NULL
;
379 if (crm
== NULL
|| (meth
== OSSL_CRMF_POPO_SIGNATURE
&& pkey
== NULL
)) {
380 ERR_raise(ERR_LIB_CRMF
, CRMF_R_NULL_ARGUMENT
);
384 if (meth
== OSSL_CRMF_POPO_NONE
)
386 if ((pp
= OSSL_CRMF_POPO_new()) == NULL
)
391 case OSSL_CRMF_POPO_RAVERIFIED
:
392 if ((pp
->value
.raVerified
= ASN1_NULL_new()) == NULL
)
396 case OSSL_CRMF_POPO_SIGNATURE
:
398 OSSL_CRMF_POPOSIGNINGKEY
*ps
= OSSL_CRMF_POPOSIGNINGKEY_new();
402 if (!create_popo_signature(ps
, crm
->certReq
, pkey
, digest
,
404 OSSL_CRMF_POPOSIGNINGKEY_free(ps
);
407 pp
->value
.signature
= ps
;
411 case OSSL_CRMF_POPO_KEYENC
:
412 if ((pp
->value
.keyEncipherment
= OSSL_CRMF_POPOPRIVKEY_new()) == NULL
)
414 tag
= ASN1_INTEGER_new();
415 pp
->value
.keyEncipherment
->type
=
416 OSSL_CRMF_POPOPRIVKEY_SUBSEQUENTMESSAGE
;
417 pp
->value
.keyEncipherment
->value
.subsequentMessage
= tag
;
419 || !ASN1_INTEGER_set(tag
, OSSL_CRMF_SUBSEQUENTMESSAGE_ENCRCERT
))
424 ERR_raise(ERR_LIB_CRMF
, CRMF_R_UNSUPPORTED_METHOD_FOR_CREATING_POPO
);
429 OSSL_CRMF_POPO_free(crm
->popo
);
434 OSSL_CRMF_POPO_free(pp
);
438 /* verifies the Proof-of-Possession of the request with the given rid in reqs */
439 int OSSL_CRMF_MSGS_verify_popo(const OSSL_CRMF_MSGS
*reqs
,
440 int rid
, int acceptRAVerified
,
441 OSSL_LIB_CTX
*libctx
, const char *propq
)
443 OSSL_CRMF_MSG
*req
= NULL
;
444 X509_PUBKEY
*pubkey
= NULL
;
445 OSSL_CRMF_POPOSIGNINGKEY
*sig
= NULL
;
449 if (reqs
== NULL
|| (req
= sk_OSSL_CRMF_MSG_value(reqs
, rid
)) == NULL
) {
450 ERR_raise(ERR_LIB_CRMF
, CRMF_R_NULL_ARGUMENT
);
454 if (req
->popo
== NULL
) {
455 ERR_raise(ERR_LIB_CRMF
, CRMF_R_POPO_MISSING
);
459 switch (req
->popo
->type
) {
460 case OSSL_CRMF_POPO_RAVERIFIED
:
461 if (!acceptRAVerified
) {
462 ERR_raise(ERR_LIB_CRMF
, CRMF_R_POPO_RAVERIFIED_NOT_ACCEPTED
);
466 case OSSL_CRMF_POPO_SIGNATURE
:
467 pubkey
= req
->certReq
->certTemplate
->publicKey
;
468 if (pubkey
== NULL
) {
469 ERR_raise(ERR_LIB_CRMF
, CRMF_R_POPO_MISSING_PUBLIC_KEY
);
472 sig
= req
->popo
->value
.signature
;
473 if (sig
->poposkInput
!= NULL
) {
475 * According to RFC 4211: publicKey contains a copy of
476 * the public key from the certificate template. This MUST be
477 * exactly the same value as contained in the certificate template.
479 if (sig
->poposkInput
->publicKey
== NULL
) {
480 ERR_raise(ERR_LIB_CRMF
, CRMF_R_POPO_MISSING_PUBLIC_KEY
);
483 if (X509_PUBKEY_eq(pubkey
, sig
->poposkInput
->publicKey
) != 1) {
484 ERR_raise(ERR_LIB_CRMF
, CRMF_R_POPO_INCONSISTENT_PUBLIC_KEY
);
487 it
= ASN1_ITEM_rptr(OSSL_CRMF_POPOSIGNINGKEYINPUT
);
488 asn
= sig
->poposkInput
;
490 if (req
->certReq
->certTemplate
->subject
== NULL
) {
491 ERR_raise(ERR_LIB_CRMF
, CRMF_R_POPO_MISSING_SUBJECT
);
494 it
= ASN1_ITEM_rptr(OSSL_CRMF_CERTREQUEST
);
497 if (ASN1_item_verify_ex(it
, sig
->algorithmIdentifier
, sig
->signature
,
498 asn
, NULL
, X509_PUBKEY_get0(pubkey
), libctx
,
502 case OSSL_CRMF_POPO_KEYENC
:
503 case OSSL_CRMF_POPO_KEYAGREE
:
505 ERR_raise(ERR_LIB_CRMF
, CRMF_R_UNSUPPORTED_POPO_METHOD
);
511 /* retrieves the serialNumber of the given cert template or NULL on error */
513 *OSSL_CRMF_CERTTEMPLATE_get0_serialNumber(const OSSL_CRMF_CERTTEMPLATE
*tmpl
)
515 return tmpl
!= NULL
? tmpl
->serialNumber
: NULL
;
518 /* retrieves the issuer name of the given cert template or NULL on error */
520 *OSSL_CRMF_CERTTEMPLATE_get0_issuer(const OSSL_CRMF_CERTTEMPLATE
*tmpl
)
522 return tmpl
!= NULL
? tmpl
->issuer
: NULL
;
525 /* retrieves the issuer name of the given CertId or NULL on error */
526 const X509_NAME
*OSSL_CRMF_CERTID_get0_issuer(const OSSL_CRMF_CERTID
*cid
)
528 return cid
!= NULL
&& cid
->issuer
->type
== GEN_DIRNAME
?
529 cid
->issuer
->d
.directoryName
: NULL
;
532 /* retrieves the serialNumber of the given CertId or NULL on error */
533 ASN1_INTEGER
*OSSL_CRMF_CERTID_get0_serialNumber(const OSSL_CRMF_CERTID
*cid
)
535 return cid
!= NULL
? cid
->serialNumber
: NULL
;
539 * fill in certificate template.
540 * Any value argument that is NULL will leave the respective field unchanged.
542 int OSSL_CRMF_CERTTEMPLATE_fill(OSSL_CRMF_CERTTEMPLATE
*tmpl
,
544 const X509_NAME
*subject
,
545 const X509_NAME
*issuer
,
546 const ASN1_INTEGER
*serial
)
549 ERR_raise(ERR_LIB_CRMF
, CRMF_R_NULL_ARGUMENT
);
552 if (subject
!= NULL
&& !X509_NAME_set((X509_NAME
**)&tmpl
->subject
, subject
))
554 if (issuer
!= NULL
&& !X509_NAME_set((X509_NAME
**)&tmpl
->issuer
, issuer
))
556 if (serial
!= NULL
) {
557 ASN1_INTEGER_free(tmpl
->serialNumber
);
558 if ((tmpl
->serialNumber
= ASN1_INTEGER_dup(serial
)) == NULL
)
561 if (pubkey
!= NULL
&& !X509_PUBKEY_set(&tmpl
->publicKey
, pubkey
))
568 * Decrypts the certificate in the given encryptedValue using private key pkey.
569 * This is needed for the indirect PoP method as in RFC 4210 section 5.2.8.2.
571 * returns a pointer to the decrypted certificate
572 * returns NULL on error or if no certificate available
575 *OSSL_CRMF_ENCRYPTEDVALUE_get1_encCert(const OSSL_CRMF_ENCRYPTEDVALUE
*ecert
,
576 OSSL_LIB_CTX
*libctx
, const char *propq
,
579 X509
*cert
= NULL
; /* decrypted certificate */
580 EVP_CIPHER_CTX
*evp_ctx
= NULL
; /* context for symmetric encryption */
581 unsigned char *ek
= NULL
; /* decrypted symmetric encryption key */
582 size_t eksize
= 0; /* size of decrypted symmetric encryption key */
583 EVP_CIPHER
*cipher
= NULL
; /* used cipher */
584 int cikeysize
= 0; /* key size from cipher */
585 unsigned char *iv
= NULL
; /* initial vector for symmetric encryption */
586 unsigned char *outbuf
= NULL
; /* decryption output buffer */
587 const unsigned char *p
= NULL
; /* needed for decoding ASN1 */
589 EVP_PKEY_CTX
*pkctx
= NULL
; /* private key context */
590 char name
[OSSL_MAX_NAME_SIZE
];
592 if (ecert
== NULL
|| ecert
->symmAlg
== NULL
|| ecert
->encSymmKey
== NULL
593 || ecert
->encValue
== NULL
|| pkey
== NULL
) {
594 ERR_raise(ERR_LIB_CRMF
, CRMF_R_NULL_ARGUMENT
);
598 /* select symmetric cipher based on algorithm given in message */
599 OBJ_obj2txt(name
, sizeof(name
), ecert
->symmAlg
->algorithm
, 0);
601 (void)ERR_set_mark();
602 cipher
= EVP_CIPHER_fetch(NULL
, name
, NULL
);
605 cipher
= (EVP_CIPHER
*)EVP_get_cipherbyname(name
);
607 if (cipher
== NULL
) {
608 (void)ERR_clear_last_mark();
609 ERR_raise(ERR_LIB_CRMF
, CRMF_R_UNSUPPORTED_CIPHER
);
612 (void)ERR_pop_to_mark();
614 cikeysize
= EVP_CIPHER_get_key_length(cipher
);
615 /* first the symmetric key needs to be decrypted */
616 pkctx
= EVP_PKEY_CTX_new_from_pkey(libctx
, pkey
, propq
);
617 if (pkctx
!= NULL
&& EVP_PKEY_decrypt_init(pkctx
)) {
618 ASN1_BIT_STRING
*encKey
= ecert
->encSymmKey
;
622 if (EVP_PKEY_decrypt(pkctx
, NULL
, &eksize
,
623 encKey
->data
, encKey
->length
) <= 0
624 || (ek
= OPENSSL_malloc(eksize
)) == NULL
)
626 retval
= EVP_PKEY_decrypt(pkctx
, ek
, &eksize
,
627 encKey
->data
, encKey
->length
);
628 ERR_clear_error(); /* error state may have sensitive information */
629 failure
= ~constant_time_is_zero_s(constant_time_msb(retval
)
630 | constant_time_is_zero(retval
));
631 failure
|= ~constant_time_eq_s(eksize
, (size_t)cikeysize
);
633 ERR_raise(ERR_LIB_CRMF
, CRMF_R_ERROR_DECRYPTING_SYMMETRIC_KEY
);
639 if ((iv
= OPENSSL_malloc(EVP_CIPHER_get_iv_length(cipher
))) == NULL
)
641 if (ASN1_TYPE_get_octetstring(ecert
->symmAlg
->parameter
, iv
,
642 EVP_CIPHER_get_iv_length(cipher
))
643 != EVP_CIPHER_get_iv_length(cipher
)) {
644 ERR_raise(ERR_LIB_CRMF
, CRMF_R_MALFORMED_IV
);
649 * d2i_X509 changes the given pointer, so use p for decoding the message and
650 * keep the original pointer in outbuf so the memory can be freed later
652 if ((p
= outbuf
= OPENSSL_malloc(ecert
->encValue
->length
+
653 EVP_CIPHER_get_block_size(cipher
))) == NULL
654 || (evp_ctx
= EVP_CIPHER_CTX_new()) == NULL
)
656 EVP_CIPHER_CTX_set_padding(evp_ctx
, 0);
658 if (!EVP_DecryptInit(evp_ctx
, cipher
, ek
, iv
)
659 || !EVP_DecryptUpdate(evp_ctx
, outbuf
, &outlen
,
660 ecert
->encValue
->data
,
661 ecert
->encValue
->length
)
662 || !EVP_DecryptFinal(evp_ctx
, outbuf
+ outlen
, &n
)) {
663 ERR_raise(ERR_LIB_CRMF
, CRMF_R_ERROR_DECRYPTING_CERTIFICATE
);
668 /* convert decrypted certificate from DER to internal ASN.1 structure */
669 if ((cert
= X509_new_ex(libctx
, propq
)) == NULL
)
671 if (d2i_X509(&cert
, &p
, outlen
) == NULL
)
672 ERR_raise(ERR_LIB_CRMF
, CRMF_R_ERROR_DECODING_CERTIFICATE
);
674 EVP_PKEY_CTX_free(pkctx
);
675 OPENSSL_free(outbuf
);
676 EVP_CIPHER_CTX_free(evp_ctx
);
677 EVP_CIPHER_free(cipher
);
678 OPENSSL_clear_free(ek
, eksize
);