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 valt *OSSL_CRMF_MSG_get0_##ctrlinf##_##atyp(const OSSL_CRMF_MSG *msg) \
49 STACK_OF(OSSL_CRMF_ATTRIBUTETYPEANDVALUE) *controls; \
50 OSSL_CRMF_ATTRIBUTETYPEANDVALUE *atav = NULL; \
52 if (msg == NULL || msg->certReq == NULL) \
54 controls = msg->certReq->controls; \
55 for (i = 0; i < sk_OSSL_CRMF_ATTRIBUTETYPEANDVALUE_num(controls); i++) { \
56 atav = sk_OSSL_CRMF_ATTRIBUTETYPEANDVALUE_value(controls, i); \
57 if (OBJ_obj2nid(atav->type) == NID_id_##ctrlinf##_##atyp) \
58 return atav->value.atyp; \
63 int OSSL_CRMF_MSG_set1_##ctrlinf##_##atyp(OSSL_CRMF_MSG *msg, const valt *in) \
65 OSSL_CRMF_ATTRIBUTETYPEANDVALUE *atav = NULL; \
67 if (msg == NULL || in == NULL) \
69 if ((atav = OSSL_CRMF_ATTRIBUTETYPEANDVALUE_new()) == NULL) \
71 if ((atav->type = OBJ_nid2obj(NID_id_##ctrlinf##_##atyp)) == NULL) \
73 if ((atav->value.atyp = valt##_dup(in)) == NULL) \
75 if (!OSSL_CRMF_MSG_push0_##ctrlinf(msg, atav)) \
79 OSSL_CRMF_ATTRIBUTETYPEANDVALUE_free(atav); \
85 * Pushes the given control attribute into the controls stack of a CertRequest
87 * returns 1 on success, 0 on error
89 static int OSSL_CRMF_MSG_push0_regCtrl(OSSL_CRMF_MSG
*crm
,
90 OSSL_CRMF_ATTRIBUTETYPEANDVALUE
*ctrl
)
94 if (crm
== NULL
|| crm
->certReq
== NULL
|| ctrl
== NULL
) {
95 ERR_raise(ERR_LIB_CRMF
, CRMF_R_NULL_ARGUMENT
);
99 if (crm
->certReq
->controls
== NULL
) {
100 crm
->certReq
->controls
= sk_OSSL_CRMF_ATTRIBUTETYPEANDVALUE_new_null();
101 if (crm
->certReq
->controls
== NULL
)
105 if (!sk_OSSL_CRMF_ATTRIBUTETYPEANDVALUE_push(crm
->certReq
->controls
, ctrl
))
111 sk_OSSL_CRMF_ATTRIBUTETYPEANDVALUE_free(crm
->certReq
->controls
);
112 crm
->certReq
->controls
= NULL
;
117 /* id-regCtrl-regToken Control (section 6.1) */
118 IMPLEMENT_CRMF_CTRL_FUNC(regToken
, ASN1_STRING
, regCtrl
)
120 /* id-regCtrl-authenticator Control (section 6.2) */
121 #define ASN1_UTF8STRING_dup ASN1_STRING_dup
122 IMPLEMENT_CRMF_CTRL_FUNC(authenticator
, ASN1_UTF8STRING
, regCtrl
)
124 int OSSL_CRMF_MSG_set0_SinglePubInfo(OSSL_CRMF_SINGLEPUBINFO
*spi
,
125 int method
, GENERAL_NAME
*nm
)
128 || method
< OSSL_CRMF_PUB_METHOD_DONTCARE
129 || method
> OSSL_CRMF_PUB_METHOD_LDAP
) {
130 ERR_raise(ERR_LIB_CRMF
, ERR_R_PASSED_INVALID_ARGUMENT
);
134 if (!ASN1_INTEGER_set(spi
->pubMethod
, method
))
136 GENERAL_NAME_free(spi
->pubLocation
);
137 spi
->pubLocation
= nm
;
142 OSSL_CRMF_MSG_PKIPublicationInfo_push0_SinglePubInfo(OSSL_CRMF_PKIPUBLICATIONINFO
*pi
,
143 OSSL_CRMF_SINGLEPUBINFO
*spi
)
145 if (pi
== NULL
|| spi
== NULL
) {
146 ERR_raise(ERR_LIB_CRMF
, CRMF_R_NULL_ARGUMENT
);
149 if (pi
->pubInfos
== NULL
)
150 pi
->pubInfos
= sk_OSSL_CRMF_SINGLEPUBINFO_new_null();
151 if (pi
->pubInfos
== NULL
)
154 return sk_OSSL_CRMF_SINGLEPUBINFO_push(pi
->pubInfos
, spi
);
157 int OSSL_CRMF_MSG_set_PKIPublicationInfo_action(OSSL_CRMF_PKIPUBLICATIONINFO
*pi
,
161 || action
< OSSL_CRMF_PUB_ACTION_DONTPUBLISH
162 || action
> OSSL_CRMF_PUB_ACTION_PLEASEPUBLISH
) {
163 ERR_raise(ERR_LIB_CRMF
, ERR_R_PASSED_INVALID_ARGUMENT
);
167 return ASN1_INTEGER_set(pi
->action
, action
);
170 /* id-regCtrl-pkiPublicationInfo Control (section 6.3) */
171 IMPLEMENT_CRMF_CTRL_FUNC(pkiPublicationInfo
, OSSL_CRMF_PKIPUBLICATIONINFO
,
174 /* id-regCtrl-oldCertID Control (section 6.5) from the given */
175 IMPLEMENT_CRMF_CTRL_FUNC(oldCertID
, OSSL_CRMF_CERTID
, regCtrl
)
177 OSSL_CRMF_CERTID
*OSSL_CRMF_CERTID_gen(const X509_NAME
*issuer
,
178 const ASN1_INTEGER
*serial
)
180 OSSL_CRMF_CERTID
*cid
= NULL
;
182 if (issuer
== NULL
|| serial
== NULL
) {
183 ERR_raise(ERR_LIB_CRMF
, CRMF_R_NULL_ARGUMENT
);
187 if ((cid
= OSSL_CRMF_CERTID_new()) == NULL
)
190 if (!X509_NAME_set(&cid
->issuer
->d
.directoryName
, issuer
))
192 cid
->issuer
->type
= GEN_DIRNAME
;
194 ASN1_INTEGER_free(cid
->serialNumber
);
195 if ((cid
->serialNumber
= ASN1_INTEGER_dup(serial
)) == NULL
)
201 OSSL_CRMF_CERTID_free(cid
);
206 * id-regCtrl-protocolEncrKey Control (section 6.6)
208 IMPLEMENT_CRMF_CTRL_FUNC(protocolEncrKey
, X509_PUBKEY
, regCtrl
)
211 * Pushes the attribute given in regInfo in to the CertReqMsg->regInfo stack.
213 * returns 1 on success, 0 on error
215 static int OSSL_CRMF_MSG_push0_regInfo(OSSL_CRMF_MSG
*crm
,
216 OSSL_CRMF_ATTRIBUTETYPEANDVALUE
*ri
)
218 STACK_OF(OSSL_CRMF_ATTRIBUTETYPEANDVALUE
) *info
= NULL
;
220 if (crm
== NULL
|| ri
== NULL
) {
221 ERR_raise(ERR_LIB_CRMF
, CRMF_R_NULL_ARGUMENT
);
225 if (crm
->regInfo
== NULL
)
226 crm
->regInfo
= info
= sk_OSSL_CRMF_ATTRIBUTETYPEANDVALUE_new_null();
227 if (crm
->regInfo
== NULL
)
229 if (!sk_OSSL_CRMF_ATTRIBUTETYPEANDVALUE_push(crm
->regInfo
, ri
))
236 sk_OSSL_CRMF_ATTRIBUTETYPEANDVALUE_free(info
);
240 /* id-regInfo-utf8Pairs to regInfo (section 7.1) */
241 IMPLEMENT_CRMF_CTRL_FUNC(utf8Pairs
, ASN1_UTF8STRING
, regInfo
)
243 /* id-regInfo-certReq to regInfo (section 7.2) */
244 IMPLEMENT_CRMF_CTRL_FUNC(certReq
, OSSL_CRMF_CERTREQUEST
, regInfo
)
247 /* retrieves the certificate template of crm */
248 OSSL_CRMF_CERTTEMPLATE
*OSSL_CRMF_MSG_get0_tmpl(const OSSL_CRMF_MSG
*crm
)
250 if (crm
== NULL
|| crm
->certReq
== NULL
) {
251 ERR_raise(ERR_LIB_CRMF
, CRMF_R_NULL_ARGUMENT
);
254 return crm
->certReq
->certTemplate
;
258 int OSSL_CRMF_MSG_set0_validity(OSSL_CRMF_MSG
*crm
,
259 ASN1_TIME
*notBefore
, ASN1_TIME
*notAfter
)
261 OSSL_CRMF_OPTIONALVALIDITY
*vld
;
262 OSSL_CRMF_CERTTEMPLATE
*tmpl
= OSSL_CRMF_MSG_get0_tmpl(crm
);
264 if (tmpl
== NULL
) { /* also crm == NULL implies this */
265 ERR_raise(ERR_LIB_CRMF
, CRMF_R_NULL_ARGUMENT
);
269 if ((vld
= OSSL_CRMF_OPTIONALVALIDITY_new()) == NULL
)
271 vld
->notBefore
= notBefore
;
272 vld
->notAfter
= notAfter
;
273 tmpl
->validity
= vld
;
278 int OSSL_CRMF_MSG_set_certReqId(OSSL_CRMF_MSG
*crm
, int rid
)
280 if (crm
== NULL
|| crm
->certReq
== NULL
|| crm
->certReq
->certReqId
== NULL
) {
281 ERR_raise(ERR_LIB_CRMF
, CRMF_R_NULL_ARGUMENT
);
285 return ASN1_INTEGER_set(crm
->certReq
->certReqId
, rid
);
288 /* get ASN.1 encoded integer, return -1 on error */
289 static int crmf_asn1_get_int(const ASN1_INTEGER
*a
)
293 if (!ASN1_INTEGER_get_int64(&res
, a
)) {
294 ERR_raise(ERR_LIB_CRMF
, ASN1_R_INVALID_NUMBER
);
298 ERR_raise(ERR_LIB_CRMF
, ASN1_R_TOO_SMALL
);
302 ERR_raise(ERR_LIB_CRMF
, ASN1_R_TOO_LARGE
);
308 int OSSL_CRMF_MSG_get_certReqId(const OSSL_CRMF_MSG
*crm
)
310 if (crm
== NULL
|| /* not really needed: */ crm
->certReq
== NULL
) {
311 ERR_raise(ERR_LIB_CRMF
, CRMF_R_NULL_ARGUMENT
);
314 return crmf_asn1_get_int(crm
->certReq
->certReqId
);
318 int OSSL_CRMF_MSG_set0_extensions(OSSL_CRMF_MSG
*crm
,
319 X509_EXTENSIONS
*exts
)
321 OSSL_CRMF_CERTTEMPLATE
*tmpl
= OSSL_CRMF_MSG_get0_tmpl(crm
);
323 if (tmpl
== NULL
) { /* also crm == NULL implies this */
324 ERR_raise(ERR_LIB_CRMF
, CRMF_R_NULL_ARGUMENT
);
328 if (sk_X509_EXTENSION_num(exts
) == 0) {
329 sk_X509_EXTENSION_free(exts
);
330 exts
= NULL
; /* do not include empty extensions list */
333 sk_X509_EXTENSION_pop_free(tmpl
->extensions
, X509_EXTENSION_free
);
334 tmpl
->extensions
= exts
;
339 int OSSL_CRMF_MSG_push0_extension(OSSL_CRMF_MSG
*crm
,
343 OSSL_CRMF_CERTTEMPLATE
*tmpl
= OSSL_CRMF_MSG_get0_tmpl(crm
);
345 if (tmpl
== NULL
|| ext
== NULL
) { /* also crm == NULL implies this */
346 ERR_raise(ERR_LIB_CRMF
, CRMF_R_NULL_ARGUMENT
);
350 if (tmpl
->extensions
== NULL
) {
351 if ((tmpl
->extensions
= sk_X509_EXTENSION_new_null()) == NULL
)
356 if (!sk_X509_EXTENSION_push(tmpl
->extensions
, ext
))
361 sk_X509_EXTENSION_free(tmpl
->extensions
);
362 tmpl
->extensions
= NULL
;
367 static int create_popo_signature(OSSL_CRMF_POPOSIGNINGKEY
*ps
,
368 const OSSL_CRMF_CERTREQUEST
*cr
,
369 EVP_PKEY
*pkey
, const EVP_MD
*digest
,
370 OSSL_LIB_CTX
*libctx
, const char *propq
)
374 if (ps
== NULL
|| cr
== NULL
|| pkey
== NULL
) {
375 ERR_raise(ERR_LIB_CRMF
, CRMF_R_NULL_ARGUMENT
);
378 if (ps
->poposkInput
!= NULL
) {
379 /* We do not support cases 1+2 defined in RFC 4211, section 4.1 */
380 ERR_raise(ERR_LIB_CRMF
, CRMF_R_POPOSKINPUT_NOT_SUPPORTED
);
384 if (EVP_PKEY_get_default_digest_name(pkey
, name
, sizeof(name
)) > 0
385 && strcmp(name
, "UNDEF") == 0) /* at least for Ed25519, Ed448 */
388 return ASN1_item_sign_ex(ASN1_ITEM_rptr(OSSL_CRMF_CERTREQUEST
),
389 ps
->algorithmIdentifier
, NULL
, ps
->signature
, cr
,
390 NULL
, pkey
, digest
, libctx
, propq
);
394 int OSSL_CRMF_MSG_create_popo(int meth
, OSSL_CRMF_MSG
*crm
,
395 EVP_PKEY
*pkey
, const EVP_MD
*digest
,
396 OSSL_LIB_CTX
*libctx
, const char *propq
)
398 OSSL_CRMF_POPO
*pp
= NULL
;
399 ASN1_INTEGER
*tag
= NULL
;
401 if (crm
== NULL
|| (meth
== OSSL_CRMF_POPO_SIGNATURE
&& pkey
== NULL
)) {
402 ERR_raise(ERR_LIB_CRMF
, CRMF_R_NULL_ARGUMENT
);
406 if (meth
== OSSL_CRMF_POPO_NONE
)
408 if ((pp
= OSSL_CRMF_POPO_new()) == NULL
)
413 case OSSL_CRMF_POPO_RAVERIFIED
:
414 if ((pp
->value
.raVerified
= ASN1_NULL_new()) == NULL
)
418 case OSSL_CRMF_POPO_SIGNATURE
:
420 OSSL_CRMF_POPOSIGNINGKEY
*ps
= OSSL_CRMF_POPOSIGNINGKEY_new();
424 if (!create_popo_signature(ps
, crm
->certReq
, pkey
, digest
,
426 OSSL_CRMF_POPOSIGNINGKEY_free(ps
);
429 pp
->value
.signature
= ps
;
433 case OSSL_CRMF_POPO_KEYENC
:
434 if ((pp
->value
.keyEncipherment
= OSSL_CRMF_POPOPRIVKEY_new()) == NULL
)
436 tag
= ASN1_INTEGER_new();
437 pp
->value
.keyEncipherment
->type
=
438 OSSL_CRMF_POPOPRIVKEY_SUBSEQUENTMESSAGE
;
439 pp
->value
.keyEncipherment
->value
.subsequentMessage
= tag
;
441 || !ASN1_INTEGER_set(tag
, OSSL_CRMF_SUBSEQUENTMESSAGE_ENCRCERT
))
446 ERR_raise(ERR_LIB_CRMF
, CRMF_R_UNSUPPORTED_METHOD_FOR_CREATING_POPO
);
451 OSSL_CRMF_POPO_free(crm
->popo
);
456 OSSL_CRMF_POPO_free(pp
);
460 /* verifies the Proof-of-Possession of the request with the given rid in reqs */
461 int OSSL_CRMF_MSGS_verify_popo(const OSSL_CRMF_MSGS
*reqs
,
462 int rid
, int acceptRAVerified
,
463 OSSL_LIB_CTX
*libctx
, const char *propq
)
465 OSSL_CRMF_MSG
*req
= NULL
;
466 X509_PUBKEY
*pubkey
= NULL
;
467 OSSL_CRMF_POPOSIGNINGKEY
*sig
= NULL
;
471 if (reqs
== NULL
|| (req
= sk_OSSL_CRMF_MSG_value(reqs
, rid
)) == NULL
) {
472 ERR_raise(ERR_LIB_CRMF
, CRMF_R_NULL_ARGUMENT
);
476 if (req
->popo
== NULL
) {
477 ERR_raise(ERR_LIB_CRMF
, CRMF_R_POPO_MISSING
);
481 switch (req
->popo
->type
) {
482 case OSSL_CRMF_POPO_RAVERIFIED
:
483 if (!acceptRAVerified
) {
484 ERR_raise(ERR_LIB_CRMF
, CRMF_R_POPO_RAVERIFIED_NOT_ACCEPTED
);
488 case OSSL_CRMF_POPO_SIGNATURE
:
489 pubkey
= req
->certReq
->certTemplate
->publicKey
;
490 if (pubkey
== NULL
) {
491 ERR_raise(ERR_LIB_CRMF
, CRMF_R_POPO_MISSING_PUBLIC_KEY
);
494 sig
= req
->popo
->value
.signature
;
495 if (sig
->poposkInput
!= NULL
) {
497 * According to RFC 4211: publicKey contains a copy of
498 * the public key from the certificate template. This MUST be
499 * exactly the same value as contained in the certificate template.
501 if (sig
->poposkInput
->publicKey
== NULL
) {
502 ERR_raise(ERR_LIB_CRMF
, CRMF_R_POPO_MISSING_PUBLIC_KEY
);
505 if (X509_PUBKEY_eq(pubkey
, sig
->poposkInput
->publicKey
) != 1) {
506 ERR_raise(ERR_LIB_CRMF
, CRMF_R_POPO_INCONSISTENT_PUBLIC_KEY
);
509 it
= ASN1_ITEM_rptr(OSSL_CRMF_POPOSIGNINGKEYINPUT
);
510 asn
= sig
->poposkInput
;
512 if (req
->certReq
->certTemplate
->subject
== NULL
) {
513 ERR_raise(ERR_LIB_CRMF
, CRMF_R_POPO_MISSING_SUBJECT
);
516 it
= ASN1_ITEM_rptr(OSSL_CRMF_CERTREQUEST
);
519 if (ASN1_item_verify_ex(it
, sig
->algorithmIdentifier
, sig
->signature
,
520 asn
, NULL
, X509_PUBKEY_get0(pubkey
), libctx
,
524 case OSSL_CRMF_POPO_KEYENC
:
525 case OSSL_CRMF_POPO_KEYAGREE
:
527 ERR_raise(ERR_LIB_CRMF
, CRMF_R_UNSUPPORTED_POPO_METHOD
);
533 /* retrieves the serialNumber of the given cert template or NULL on error */
535 *OSSL_CRMF_CERTTEMPLATE_get0_serialNumber(const OSSL_CRMF_CERTTEMPLATE
*tmpl
)
537 return tmpl
!= NULL
? tmpl
->serialNumber
: NULL
;
541 *OSSL_CRMF_CERTTEMPLATE_get0_subject(const OSSL_CRMF_CERTTEMPLATE
*tmpl
)
543 return tmpl
!= NULL
? tmpl
->subject
: NULL
;
546 /* retrieves the issuer name of the given cert template or NULL on error */
548 *OSSL_CRMF_CERTTEMPLATE_get0_issuer(const OSSL_CRMF_CERTTEMPLATE
*tmpl
)
550 return tmpl
!= NULL
? tmpl
->issuer
: NULL
;
554 *OSSL_CRMF_CERTTEMPLATE_get0_extensions(const OSSL_CRMF_CERTTEMPLATE
*tmpl
)
556 return tmpl
!= NULL
? tmpl
->extensions
: NULL
;
559 /* retrieves the issuer name of the given CertId or NULL on error */
560 const X509_NAME
*OSSL_CRMF_CERTID_get0_issuer(const OSSL_CRMF_CERTID
*cid
)
562 return cid
!= NULL
&& cid
->issuer
->type
== GEN_DIRNAME
?
563 cid
->issuer
->d
.directoryName
: NULL
;
566 /* retrieves the serialNumber of the given CertId or NULL on error */
567 const ASN1_INTEGER
*OSSL_CRMF_CERTID_get0_serialNumber(const OSSL_CRMF_CERTID
*cid
)
569 return cid
!= NULL
? cid
->serialNumber
: NULL
;
573 * fill in certificate template.
574 * Any value argument that is NULL will leave the respective field unchanged.
576 int OSSL_CRMF_CERTTEMPLATE_fill(OSSL_CRMF_CERTTEMPLATE
*tmpl
,
578 const X509_NAME
*subject
,
579 const X509_NAME
*issuer
,
580 const ASN1_INTEGER
*serial
)
583 ERR_raise(ERR_LIB_CRMF
, CRMF_R_NULL_ARGUMENT
);
586 if (subject
!= NULL
&& !X509_NAME_set((X509_NAME
**)&tmpl
->subject
, subject
))
588 if (issuer
!= NULL
&& !X509_NAME_set((X509_NAME
**)&tmpl
->issuer
, issuer
))
590 if (serial
!= NULL
) {
591 ASN1_INTEGER_free(tmpl
->serialNumber
);
592 if ((tmpl
->serialNumber
= ASN1_INTEGER_dup(serial
)) == NULL
)
595 if (pubkey
!= NULL
&& !X509_PUBKEY_set(&tmpl
->publicKey
, pubkey
))
602 * Decrypts the certificate in the given encryptedValue using private key pkey.
603 * This is needed for the indirect PoP method as in RFC 4210 section 5.2.8.2.
605 * returns a pointer to the decrypted certificate
606 * returns NULL on error or if no certificate available
609 *OSSL_CRMF_ENCRYPTEDVALUE_get1_encCert(const OSSL_CRMF_ENCRYPTEDVALUE
*ecert
,
610 OSSL_LIB_CTX
*libctx
, const char *propq
,
613 X509
*cert
= NULL
; /* decrypted certificate */
614 EVP_CIPHER_CTX
*evp_ctx
= NULL
; /* context for symmetric encryption */
615 unsigned char *ek
= NULL
; /* decrypted symmetric encryption key */
616 size_t eksize
= 0; /* size of decrypted symmetric encryption key */
617 EVP_CIPHER
*cipher
= NULL
; /* used cipher */
618 int cikeysize
= 0; /* key size from cipher */
619 unsigned char *iv
= NULL
; /* initial vector for symmetric encryption */
620 unsigned char *outbuf
= NULL
; /* decryption output buffer */
621 const unsigned char *p
= NULL
; /* needed for decoding ASN1 */
623 EVP_PKEY_CTX
*pkctx
= NULL
; /* private key context */
624 char name
[OSSL_MAX_NAME_SIZE
];
626 if (ecert
== NULL
|| ecert
->symmAlg
== NULL
|| ecert
->encSymmKey
== NULL
627 || ecert
->encValue
== NULL
|| pkey
== NULL
) {
628 ERR_raise(ERR_LIB_CRMF
, CRMF_R_NULL_ARGUMENT
);
632 /* select symmetric cipher based on algorithm given in message */
633 OBJ_obj2txt(name
, sizeof(name
), ecert
->symmAlg
->algorithm
, 0);
635 (void)ERR_set_mark();
636 cipher
= EVP_CIPHER_fetch(NULL
, name
, NULL
);
639 cipher
= (EVP_CIPHER
*)EVP_get_cipherbyname(name
);
641 if (cipher
== NULL
) {
642 (void)ERR_clear_last_mark();
643 ERR_raise(ERR_LIB_CRMF
, CRMF_R_UNSUPPORTED_CIPHER
);
646 (void)ERR_pop_to_mark();
648 cikeysize
= EVP_CIPHER_get_key_length(cipher
);
649 /* first the symmetric key needs to be decrypted */
650 pkctx
= EVP_PKEY_CTX_new_from_pkey(libctx
, pkey
, propq
);
651 if (pkctx
!= NULL
&& EVP_PKEY_decrypt_init(pkctx
)) {
652 ASN1_BIT_STRING
*encKey
= ecert
->encSymmKey
;
656 if (EVP_PKEY_decrypt(pkctx
, NULL
, &eksize
,
657 encKey
->data
, encKey
->length
) <= 0
658 || (ek
= OPENSSL_malloc(eksize
)) == NULL
)
660 retval
= EVP_PKEY_decrypt(pkctx
, ek
, &eksize
,
661 encKey
->data
, encKey
->length
);
662 ERR_clear_error(); /* error state may have sensitive information */
663 failure
= ~constant_time_is_zero_s(constant_time_msb(retval
)
664 | constant_time_is_zero(retval
));
665 failure
|= ~constant_time_eq_s(eksize
, (size_t)cikeysize
);
667 ERR_raise(ERR_LIB_CRMF
, CRMF_R_ERROR_DECRYPTING_SYMMETRIC_KEY
);
673 if ((iv
= OPENSSL_malloc(EVP_CIPHER_get_iv_length(cipher
))) == NULL
)
675 if (ASN1_TYPE_get_octetstring(ecert
->symmAlg
->parameter
, iv
,
676 EVP_CIPHER_get_iv_length(cipher
))
677 != EVP_CIPHER_get_iv_length(cipher
)) {
678 ERR_raise(ERR_LIB_CRMF
, CRMF_R_MALFORMED_IV
);
683 * d2i_X509 changes the given pointer, so use p for decoding the message and
684 * keep the original pointer in outbuf so the memory can be freed later
686 if ((p
= outbuf
= OPENSSL_malloc(ecert
->encValue
->length
+
687 EVP_CIPHER_get_block_size(cipher
))) == NULL
688 || (evp_ctx
= EVP_CIPHER_CTX_new()) == NULL
)
690 EVP_CIPHER_CTX_set_padding(evp_ctx
, 0);
692 if (!EVP_DecryptInit(evp_ctx
, cipher
, ek
, iv
)
693 || !EVP_DecryptUpdate(evp_ctx
, outbuf
, &outlen
,
694 ecert
->encValue
->data
,
695 ecert
->encValue
->length
)
696 || !EVP_DecryptFinal(evp_ctx
, outbuf
+ outlen
, &n
)) {
697 ERR_raise(ERR_LIB_CRMF
, CRMF_R_ERROR_DECRYPTING_CERTIFICATE
);
702 /* convert decrypted certificate from DER to internal ASN.1 structure */
703 if ((cert
= X509_new_ex(libctx
, propq
)) == NULL
)
705 if (d2i_X509(&cert
, &p
, outlen
) == NULL
)
706 ERR_raise(ERR_LIB_CRMF
, CRMF_R_ERROR_DECODING_CERTIFICATE
);
708 EVP_PKEY_CTX_free(pkctx
);
709 OPENSSL_free(outbuf
);
710 EVP_CIPHER_CTX_free(evp_ctx
);
711 EVP_CIPHER_free(cipher
);
712 OPENSSL_clear_free(ek
, eksize
);