2 * Copyright 2007-2019 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>
40 * atyp = Attribute Type
42 * ctrlinf = "regCtrl" or "regInfo"
44 #define IMPLEMENT_CRMF_CTRL_FUNC(atyp, valt, ctrlinf) \
45 int OSSL_CRMF_MSG_set1_##ctrlinf##_##atyp(OSSL_CRMF_MSG *msg, \
48 OSSL_CRMF_ATTRIBUTETYPEANDVALUE *atav = NULL; \
50 if (msg == NULL || in == NULL) \
52 if ((atav = OSSL_CRMF_ATTRIBUTETYPEANDVALUE_new()) == NULL) \
54 if ((atav->type = OBJ_nid2obj(NID_id_##ctrlinf##_##atyp)) == NULL) \
56 if ((atav->value.atyp = valt##_dup(in)) == NULL) \
58 if (!OSSL_CRMF_MSG_push0_##ctrlinf(msg, atav)) \
62 OSSL_CRMF_ATTRIBUTETYPEANDVALUE_free(atav); \
68 * Pushes the given control attribute into the controls stack of a CertRequest
70 * returns 1 on success, 0 on error
72 static int OSSL_CRMF_MSG_push0_regCtrl(OSSL_CRMF_MSG
*crm
,
73 OSSL_CRMF_ATTRIBUTETYPEANDVALUE
*ctrl
)
77 if (crm
== NULL
|| crm
->certReq
== NULL
|| ctrl
== NULL
) {
78 CRMFerr(CRMF_F_OSSL_CRMF_MSG_PUSH0_REGCTRL
, CRMF_R_NULL_ARGUMENT
);
82 if (crm
->certReq
->controls
== NULL
) {
83 crm
->certReq
->controls
= sk_OSSL_CRMF_ATTRIBUTETYPEANDVALUE_new_null();
84 if (crm
->certReq
->controls
== NULL
)
88 if (!sk_OSSL_CRMF_ATTRIBUTETYPEANDVALUE_push(crm
->certReq
->controls
, ctrl
))
94 sk_OSSL_CRMF_ATTRIBUTETYPEANDVALUE_free(crm
->certReq
->controls
);
95 crm
->certReq
->controls
= NULL
;
100 /* id-regCtrl-regToken Control (section 6.1) */
101 IMPLEMENT_CRMF_CTRL_FUNC(regToken
, ASN1_STRING
, regCtrl
)
103 /* id-regCtrl-authenticator Control (section 6.2) */
104 #define ASN1_UTF8STRING_dup ASN1_STRING_dup
105 IMPLEMENT_CRMF_CTRL_FUNC(authenticator
, ASN1_UTF8STRING
, regCtrl
)
107 int OSSL_CRMF_MSG_set0_SinglePubInfo(OSSL_CRMF_SINGLEPUBINFO
*spi
,
108 int method
, GENERAL_NAME
*nm
)
111 || method
< OSSL_CRMF_PUB_METHOD_DONTCARE
112 || method
> OSSL_CRMF_PUB_METHOD_LDAP
) {
113 CRMFerr(CRMF_F_OSSL_CRMF_MSG_SET0_SINGLEPUBINFO
,
114 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 CRMFerr(CRMF_F_OSSL_CRMF_MSG_PKIPUBLICATIONINFO_PUSH0_SINGLEPUBINFO
,
131 CRMF_R_NULL_ARGUMENT
);
134 if (pi
->pubInfos
== NULL
)
135 pi
->pubInfos
= sk_OSSL_CRMF_SINGLEPUBINFO_new_null();
136 if (pi
->pubInfos
== NULL
)
139 return sk_OSSL_CRMF_SINGLEPUBINFO_push(pi
->pubInfos
, spi
);
142 int OSSL_CRMF_MSG_set_PKIPublicationInfo_action(OSSL_CRMF_PKIPUBLICATIONINFO
*pi
,
146 || action
< OSSL_CRMF_PUB_ACTION_DONTPUBLISH
147 || action
> OSSL_CRMF_PUB_ACTION_PLEASEPUBLISH
) {
148 CRMFerr(CRMF_F_OSSL_CRMF_MSG_SET_PKIPUBLICATIONINFO_ACTION
,
149 ERR_R_PASSED_INVALID_ARGUMENT
);
153 return ASN1_INTEGER_set(pi
->action
, action
);
156 /* id-regCtrl-pkiPublicationInfo Control (section 6.3) */
157 IMPLEMENT_CRMF_CTRL_FUNC(pkiPublicationInfo
, OSSL_CRMF_PKIPUBLICATIONINFO
,
160 /* id-regCtrl-oldCertID Control (section 6.5) from the given */
161 IMPLEMENT_CRMF_CTRL_FUNC(oldCertID
, OSSL_CRMF_CERTID
, regCtrl
)
163 OSSL_CRMF_CERTID
*OSSL_CRMF_CERTID_gen(const X509_NAME
*issuer
,
164 const ASN1_INTEGER
*serial
)
166 OSSL_CRMF_CERTID
*cid
= NULL
;
168 if (issuer
== NULL
|| serial
== NULL
) {
169 CRMFerr(CRMF_F_OSSL_CRMF_CERTID_GEN
, CRMF_R_NULL_ARGUMENT
);
173 if ((cid
= OSSL_CRMF_CERTID_new()) == NULL
)
176 if (!X509_NAME_set(&cid
->issuer
->d
.directoryName
, issuer
))
178 cid
->issuer
->type
= GEN_DIRNAME
;
180 ASN1_INTEGER_free(cid
->serialNumber
);
181 if ((cid
->serialNumber
= ASN1_INTEGER_dup(serial
)) == NULL
)
187 OSSL_CRMF_CERTID_free(cid
);
192 * id-regCtrl-protocolEncrKey Control (section 6.6)
194 IMPLEMENT_CRMF_CTRL_FUNC(protocolEncrKey
, X509_PUBKEY
, regCtrl
)
197 * Pushes the attribute given in regInfo in to the CertReqMsg->regInfo stack.
199 * returns 1 on success, 0 on error
201 static int OSSL_CRMF_MSG_push0_regInfo(OSSL_CRMF_MSG
*crm
,
202 OSSL_CRMF_ATTRIBUTETYPEANDVALUE
*ri
)
204 STACK_OF(OSSL_CRMF_ATTRIBUTETYPEANDVALUE
) *info
= NULL
;
206 if (crm
== NULL
|| ri
== NULL
) {
207 CRMFerr(CRMF_F_OSSL_CRMF_MSG_PUSH0_REGINFO
, CRMF_R_NULL_ARGUMENT
);
211 if (crm
->regInfo
== NULL
)
212 crm
->regInfo
= info
= sk_OSSL_CRMF_ATTRIBUTETYPEANDVALUE_new_null();
213 if (crm
->regInfo
== NULL
)
215 if (!sk_OSSL_CRMF_ATTRIBUTETYPEANDVALUE_push(crm
->regInfo
, ri
))
222 sk_OSSL_CRMF_ATTRIBUTETYPEANDVALUE_free(info
);
226 /* id-regInfo-utf8Pairs to regInfo (section 7.1) */
227 IMPLEMENT_CRMF_CTRL_FUNC(utf8Pairs
, ASN1_UTF8STRING
, regInfo
)
229 /* id-regInfo-certReq to regInfo (section 7.2) */
230 IMPLEMENT_CRMF_CTRL_FUNC(certReq
, OSSL_CRMF_CERTREQUEST
, regInfo
)
233 /* retrieves the certificate template of crm */
234 OSSL_CRMF_CERTTEMPLATE
*OSSL_CRMF_MSG_get0_tmpl(const OSSL_CRMF_MSG
*crm
)
236 if (crm
== NULL
|| crm
->certReq
== NULL
) {
237 CRMFerr(CRMF_F_OSSL_CRMF_MSG_GET0_TMPL
, CRMF_R_NULL_ARGUMENT
);
240 return crm
->certReq
->certTemplate
;
244 int OSSL_CRMF_MSG_set_validity(OSSL_CRMF_MSG
*crm
, time_t from
, time_t to
)
246 OSSL_CRMF_OPTIONALVALIDITY
*vld
= NULL
;
247 ASN1_TIME
*from_asn
= NULL
;
248 ASN1_TIME
*to_asn
= NULL
;
249 OSSL_CRMF_CERTTEMPLATE
*tmpl
= OSSL_CRMF_MSG_get0_tmpl(crm
);
251 if (tmpl
== NULL
) { /* also crm == NULL implies this */
252 CRMFerr(CRMF_F_OSSL_CRMF_MSG_SET_VALIDITY
, CRMF_R_NULL_ARGUMENT
);
256 if (from
!= 0 && ((from_asn
= ASN1_TIME_set(NULL
, from
)) == NULL
))
258 if (to
!= 0 && ((to_asn
= ASN1_TIME_set(NULL
, to
)) == NULL
))
260 if ((vld
= OSSL_CRMF_OPTIONALVALIDITY_new()) == NULL
)
263 vld
->notBefore
= from_asn
;
264 vld
->notAfter
= to_asn
;
266 tmpl
->validity
= vld
;
270 ASN1_TIME_free(from_asn
);
271 ASN1_TIME_free(to_asn
);
276 int OSSL_CRMF_MSG_set_certReqId(OSSL_CRMF_MSG
*crm
, int rid
)
278 if (crm
== NULL
|| crm
->certReq
== NULL
|| crm
->certReq
->certReqId
== NULL
) {
279 CRMFerr(CRMF_F_OSSL_CRMF_MSG_SET_CERTREQID
, CRMF_R_NULL_ARGUMENT
);
283 return ASN1_INTEGER_set(crm
->certReq
->certReqId
, rid
);
286 /* get ASN.1 encoded integer, return -1 on error */
287 static int crmf_asn1_get_int(const ASN1_INTEGER
*a
)
291 if (!ASN1_INTEGER_get_int64(&res
, a
)) {
292 CRMFerr(0, ASN1_R_INVALID_NUMBER
);
296 CRMFerr(0, ASN1_R_TOO_SMALL
);
300 CRMFerr(0, ASN1_R_TOO_LARGE
);
306 int OSSL_CRMF_MSG_get_certReqId(OSSL_CRMF_MSG
*crm
)
308 if (crm
== NULL
|| /* not really needed: */ crm
->certReq
== NULL
) {
309 CRMFerr(CRMF_F_OSSL_CRMF_MSG_GET_CERTREQID
, CRMF_R_NULL_ARGUMENT
);
312 return crmf_asn1_get_int(crm
->certReq
->certReqId
);
316 int OSSL_CRMF_MSG_set0_extensions(OSSL_CRMF_MSG
*crm
,
317 X509_EXTENSIONS
*exts
)
319 OSSL_CRMF_CERTTEMPLATE
*tmpl
= OSSL_CRMF_MSG_get0_tmpl(crm
);
321 if (tmpl
== NULL
) { /* also crm == NULL implies this */
322 CRMFerr(CRMF_F_OSSL_CRMF_MSG_SET0_EXTENSIONS
, CRMF_R_NULL_ARGUMENT
);
326 if (sk_X509_EXTENSION_num(exts
) == 0) {
327 sk_X509_EXTENSION_free(exts
);
328 exts
= NULL
; /* do not include empty extensions list */
331 sk_X509_EXTENSION_pop_free(tmpl
->extensions
, X509_EXTENSION_free
);
332 tmpl
->extensions
= exts
;
337 int OSSL_CRMF_MSG_push0_extension(OSSL_CRMF_MSG
*crm
,
341 OSSL_CRMF_CERTTEMPLATE
*tmpl
= OSSL_CRMF_MSG_get0_tmpl(crm
);
343 if (tmpl
== NULL
|| ext
== NULL
) { /* also crm == NULL implies this */
344 CRMFerr(CRMF_F_OSSL_CRMF_MSG_PUSH0_EXTENSION
, CRMF_R_NULL_ARGUMENT
);
348 if (tmpl
->extensions
== NULL
) {
349 if ((tmpl
->extensions
= sk_X509_EXTENSION_new_null()) == NULL
)
354 if (!sk_X509_EXTENSION_push(tmpl
->extensions
, ext
))
359 sk_X509_EXTENSION_free(tmpl
->extensions
);
360 tmpl
->extensions
= NULL
;
365 /* TODO: support cases 1+2 (besides case 3) defined in RFC 4211, section 4.1. */
366 static int CRMF_poposigningkey_init(OSSL_CRMF_POPOSIGNINGKEY
*ps
,
367 OSSL_CRMF_CERTREQUEST
*cr
,
368 EVP_PKEY
*pkey
, int dgst
)
371 EVP_MD
*fetched_md
= NULL
;
372 const EVP_MD
*md
= EVP_get_digestbynid(dgst
);
374 if (ps
== NULL
|| cr
== NULL
|| pkey
== NULL
) {
375 CRMFerr(CRMF_F_CRMF_POPOSIGNINGKEY_INIT
, CRMF_R_NULL_ARGUMENT
);
379 /* If we didn't find legacy MD, we try an implicit fetch */
381 md
= fetched_md
= EVP_MD_fetch(NULL
, OBJ_nid2sn(dgst
), NULL
);
384 CRMFerr(CRMF_F_CRMF_POPOSIGNINGKEY_INIT
,
385 CRMF_R_UNSUPPORTED_ALG_FOR_POPSIGNINGKEY
);
389 ret
= ASN1_item_sign(ASN1_ITEM_rptr(OSSL_CRMF_CERTREQUEST
),
390 ps
->algorithmIdentifier
, NULL
, ps
->signature
,
393 EVP_MD_free(fetched_md
);
398 int OSSL_CRMF_MSG_create_popo(OSSL_CRMF_MSG
*crm
, EVP_PKEY
*pkey
,
401 OSSL_CRMF_POPO
*pp
= NULL
;
402 ASN1_INTEGER
*tag
= NULL
;
404 if (crm
== NULL
|| (ppmtd
== OSSL_CRMF_POPO_SIGNATURE
&& pkey
== NULL
)) {
405 CRMFerr(CRMF_F_OSSL_CRMF_MSG_CREATE_POPO
, CRMF_R_NULL_ARGUMENT
);
409 if (ppmtd
== OSSL_CRMF_POPO_NONE
)
411 if ((pp
= OSSL_CRMF_POPO_new()) == NULL
)
416 case OSSL_CRMF_POPO_RAVERIFIED
:
417 if ((pp
->value
.raVerified
= ASN1_NULL_new()) == NULL
)
421 case OSSL_CRMF_POPO_SIGNATURE
:
423 OSSL_CRMF_POPOSIGNINGKEY
*ps
= OSSL_CRMF_POPOSIGNINGKEY_new();
425 || !CRMF_poposigningkey_init(ps
, crm
->certReq
, pkey
, dgst
)) {
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 CRMFerr(CRMF_F_OSSL_CRMF_MSG_CREATE_POPO
,
447 CRMF_R_UNSUPPORTED_METHOD_FOR_CREATING_POPO
);
452 OSSL_CRMF_POPO_free(crm
->popo
);
457 OSSL_CRMF_POPO_free(pp
);
461 /* returns 0 for equal, -1 for a < b or error on a, 1 for a > b or error on b */
462 static int X509_PUBKEY_cmp(X509_PUBKEY
*a
, X509_PUBKEY
*b
)
464 X509_ALGOR
*algA
= NULL
, *algB
= NULL
;
469 if (a
== NULL
|| !X509_PUBKEY_get0_param(NULL
, NULL
, NULL
, &algA
, a
)
472 if (b
== NULL
|| !X509_PUBKEY_get0_param(NULL
, NULL
, NULL
, &algB
, b
)
475 if ((res
= X509_ALGOR_cmp(algA
, algB
)) != 0)
477 return EVP_PKEY_cmp(X509_PUBKEY_get0(a
), X509_PUBKEY_get0(b
));
480 /* verifies the Proof-of-Possession of the request with the given rid in reqs */
481 int OSSL_CRMF_MSGS_verify_popo(const OSSL_CRMF_MSGS
*reqs
,
482 int rid
, int acceptRAVerified
)
484 OSSL_CRMF_MSG
*req
= NULL
;
485 X509_PUBKEY
*pubkey
= NULL
;
486 OSSL_CRMF_POPOSIGNINGKEY
*sig
= NULL
;
489 || (req
= sk_OSSL_CRMF_MSG_value(reqs
, rid
)) == NULL
490 || req
->popo
== NULL
) {
491 CRMFerr(CRMF_F_OSSL_CRMF_MSGS_VERIFY_POPO
,
492 CRMF_R_NULL_ARGUMENT
);
496 switch (req
->popo
->type
) {
497 case OSSL_CRMF_POPO_RAVERIFIED
:
498 if (acceptRAVerified
)
501 case OSSL_CRMF_POPO_SIGNATURE
:
502 pubkey
= req
->certReq
->certTemplate
->publicKey
;
503 sig
= req
->popo
->value
.signature
;
504 if (sig
->poposkInput
!= NULL
) {
506 * According to RFC 4211: publicKey contains a copy of
507 * the public key from the certificate template. This MUST be
508 * exactly the same value as contained in the certificate template.
510 const ASN1_ITEM
*rptr
= ASN1_ITEM_rptr(OSSL_CRMF_POPOSIGNINGKEYINPUT
);
513 || sig
->poposkInput
->publicKey
== NULL
514 || X509_PUBKEY_cmp(pubkey
, sig
->poposkInput
->publicKey
)
515 || ASN1_item_verify(rptr
, sig
->algorithmIdentifier
,
516 sig
->signature
, sig
->poposkInput
,
517 X509_PUBKEY_get0(pubkey
)) < 1)
521 || req
->certReq
->certTemplate
->subject
== NULL
522 || ASN1_item_verify(ASN1_ITEM_rptr(OSSL_CRMF_CERTREQUEST
),
523 sig
->algorithmIdentifier
,
526 X509_PUBKEY_get0(pubkey
)) < 1)
530 case OSSL_CRMF_POPO_KEYENC
:
532 * TODO: when OSSL_CMP_certrep_new() supports encrypted certs,
533 * return 1 if the type of req->popo->value.keyEncipherment
534 * is OSSL_CRMF_POPOPRIVKEY_SUBSEQUENTMESSAGE and
535 * its value.subsequentMessage == OSSL_CRMF_SUBSEQUENTMESSAGE_ENCRCERT
537 case OSSL_CRMF_POPO_KEYAGREE
:
539 CRMFerr(CRMF_F_OSSL_CRMF_MSGS_VERIFY_POPO
,
540 CRMF_R_UNSUPPORTED_POPO_METHOD
);
543 CRMFerr(CRMF_F_OSSL_CRMF_MSGS_VERIFY_POPO
,
544 CRMF_R_UNSUPPORTED_POPO_NOT_ACCEPTED
);
548 /* retrieves the serialNumber of the given cert template or NULL on error */
549 ASN1_INTEGER
*OSSL_CRMF_CERTTEMPLATE_get0_serialNumber(OSSL_CRMF_CERTTEMPLATE
*tmpl
)
551 return tmpl
!= NULL
? tmpl
->serialNumber
: NULL
;
554 /* retrieves the issuer name of the given cert template or NULL on error */
555 X509_NAME
*OSSL_CRMF_CERTTEMPLATE_get0_issuer(OSSL_CRMF_CERTTEMPLATE
*tmpl
)
557 return tmpl
!= NULL
? tmpl
->issuer
: NULL
;
560 /* retrieves the issuer name of the given CertId or NULL on error */
561 X509_NAME
*OSSL_CRMF_CERTID_get0_issuer(const OSSL_CRMF_CERTID
*cid
)
563 return cid
!= NULL
&& cid
->issuer
->type
== GEN_DIRNAME
?
564 cid
->issuer
->d
.directoryName
: NULL
;
567 /* retrieves the serialNumber of the given CertId or NULL on error */
568 ASN1_INTEGER
*OSSL_CRMF_CERTID_get0_serialNumber(const OSSL_CRMF_CERTID
*cid
)
570 return cid
!= NULL
? cid
->serialNumber
: NULL
;
574 * fill in certificate template.
575 * Any value argument that is NULL will leave the respective field unchanged.
577 int OSSL_CRMF_CERTTEMPLATE_fill(OSSL_CRMF_CERTTEMPLATE
*tmpl
,
579 const X509_NAME
*subject
,
580 const X509_NAME
*issuer
,
581 const ASN1_INTEGER
*serial
)
584 CRMFerr(CRMF_F_OSSL_CRMF_CERTTEMPLATE_FILL
, CRMF_R_NULL_ARGUMENT
);
587 if (subject
!= NULL
&& !X509_NAME_set(&tmpl
->subject
, subject
))
589 if (issuer
!= NULL
&& !X509_NAME_set(&tmpl
->issuer
, issuer
))
591 if (serial
!= NULL
) {
592 ASN1_INTEGER_free(tmpl
->serialNumber
);
593 if ((tmpl
->serialNumber
= ASN1_INTEGER_dup(serial
)) == NULL
)
596 if (pubkey
!= NULL
&& !X509_PUBKEY_set(&tmpl
->publicKey
, pubkey
))
603 * Decrypts the certificate in the given encryptedValue using private key pkey.
604 * This is needed for the indirect PoP method as in RFC 4210 section 5.2.8.2.
606 * returns a pointer to the decrypted certificate
607 * returns NULL on error or if no certificate available
609 X509
*OSSL_CRMF_ENCRYPTEDVALUE_get1_encCert(OSSL_CRMF_ENCRYPTEDVALUE
*ecert
,
612 X509
*cert
= NULL
; /* decrypted certificate */
613 EVP_CIPHER_CTX
*evp_ctx
= NULL
; /* context for symmetric encryption */
614 unsigned char *ek
= NULL
; /* decrypted symmetric encryption key */
615 size_t eksize
= 0; /* size of decrypted symmetric encryption key */
616 const EVP_CIPHER
*cipher
= NULL
; /* used cipher */
617 int cikeysize
= 0; /* key size from cipher */
618 unsigned char *iv
= NULL
; /* initial vector for symmetric encryption */
619 unsigned char *outbuf
= NULL
; /* decryption output buffer */
620 const unsigned char *p
= NULL
; /* needed for decoding ASN1 */
621 int symmAlg
= 0; /* NIDs for symmetric algorithm */
623 EVP_PKEY_CTX
*pkctx
= NULL
; /* private key context */
625 if (ecert
== NULL
|| ecert
->symmAlg
== NULL
|| ecert
->encSymmKey
== NULL
626 || ecert
->encValue
== NULL
|| pkey
== NULL
) {
627 CRMFerr(CRMF_F_OSSL_CRMF_ENCRYPTEDVALUE_GET1_ENCCERT
,
628 CRMF_R_NULL_ARGUMENT
);
631 if ((symmAlg
= OBJ_obj2nid(ecert
->symmAlg
->algorithm
)) == 0) {
632 CRMFerr(CRMF_F_OSSL_CRMF_ENCRYPTEDVALUE_GET1_ENCCERT
,
633 CRMF_R_UNSUPPORTED_CIPHER
);
636 /* select symmetric cipher based on algorithm given in message */
637 if ((cipher
= EVP_get_cipherbynid(symmAlg
)) == NULL
) {
638 CRMFerr(CRMF_F_OSSL_CRMF_ENCRYPTEDVALUE_GET1_ENCCERT
,
639 CRMF_R_UNSUPPORTED_CIPHER
);
642 cikeysize
= EVP_CIPHER_key_length(cipher
);
643 /* first the symmetric key needs to be decrypted */
644 pkctx
= EVP_PKEY_CTX_new(pkey
, NULL
);
645 if (pkctx
!= NULL
&& EVP_PKEY_decrypt_init(pkctx
)) {
646 ASN1_BIT_STRING
*encKey
= ecert
->encSymmKey
;
650 if (EVP_PKEY_decrypt(pkctx
, NULL
, &eksize
,
651 encKey
->data
, encKey
->length
) <= 0
652 || (ek
= OPENSSL_malloc(eksize
)) == NULL
)
654 retval
= EVP_PKEY_decrypt(pkctx
, ek
, &eksize
,
655 encKey
->data
, encKey
->length
);
656 ERR_clear_error(); /* error state may have sensitive information */
657 failure
= ~constant_time_is_zero_s(constant_time_msb(retval
)
658 | constant_time_is_zero(retval
));
659 failure
|= ~constant_time_eq_s(eksize
, (size_t)cikeysize
);
661 CRMFerr(CRMF_F_OSSL_CRMF_ENCRYPTEDVALUE_GET1_ENCCERT
,
662 CRMF_R_ERROR_DECRYPTING_SYMMETRIC_KEY
);
668 if ((iv
= OPENSSL_malloc(EVP_CIPHER_iv_length(cipher
))) == NULL
)
670 if (ASN1_TYPE_get_octetstring(ecert
->symmAlg
->parameter
, iv
,
671 EVP_CIPHER_iv_length(cipher
))
672 != EVP_CIPHER_iv_length(cipher
)) {
673 CRMFerr(CRMF_F_OSSL_CRMF_ENCRYPTEDVALUE_GET1_ENCCERT
,
674 CRMF_R_MALFORMED_IV
);
679 * d2i_X509 changes the given pointer, so use p for decoding the message and
680 * keep the original pointer in outbuf so the memory can be freed later
682 if ((p
= outbuf
= OPENSSL_malloc(ecert
->encValue
->length
+
683 EVP_CIPHER_block_size(cipher
))) == NULL
684 || (evp_ctx
= EVP_CIPHER_CTX_new()) == NULL
)
686 EVP_CIPHER_CTX_set_padding(evp_ctx
, 0);
688 if (!EVP_DecryptInit(evp_ctx
, cipher
, ek
, iv
)
689 || !EVP_DecryptUpdate(evp_ctx
, outbuf
, &outlen
,
690 ecert
->encValue
->data
,
691 ecert
->encValue
->length
)
692 || !EVP_DecryptFinal(evp_ctx
, outbuf
+ outlen
, &n
)) {
693 CRMFerr(CRMF_F_OSSL_CRMF_ENCRYPTEDVALUE_GET1_ENCCERT
,
694 CRMF_R_ERROR_DECRYPTING_CERTIFICATE
);
699 /* convert decrypted certificate from DER to internal ASN.1 structure */
700 if ((cert
= d2i_X509(NULL
, &p
, outlen
)) == NULL
) {
701 CRMFerr(CRMF_F_OSSL_CRMF_ENCRYPTEDVALUE_GET1_ENCCERT
,
702 CRMF_R_ERROR_DECODING_CERTIFICATE
);
705 EVP_PKEY_CTX_free(pkctx
);
706 OPENSSL_free(outbuf
);
707 EVP_CIPHER_CTX_free(evp_ctx
);
708 OPENSSL_clear_free(ek
, eksize
);