| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2021-01-28 20:54:57 +08:00
										 |  |  |  * Copyright 2008-2021 The OpenSSL Project Authors. All Rights Reserved. | 
					
						
							| 
									
										
										
										
											2008-03-14 08:58:43 +08:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2018-12-06 20:32:50 +08:00
										 |  |  |  * Licensed under the Apache License 2.0 (the "License").  You may not use | 
					
						
							| 
									
										
										
										
											2016-05-18 02:52:22 +08:00
										 |  |  |  * this file except in compliance with the License.  You can obtain a copy | 
					
						
							|  |  |  |  * in the file LICENSE in the source distribution or at | 
					
						
							|  |  |  |  * https://www.openssl.org/source/license.html
 | 
					
						
							| 
									
										
										
										
											2008-03-14 08:58:43 +08:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-14 22:56:48 +08:00
										 |  |  | #include "internal/cryptlib.h"
 | 
					
						
							| 
									
										
										
										
											2008-03-14 08:58:43 +08:00
										 |  |  | #include <openssl/asn1t.h>
 | 
					
						
							|  |  |  | #include <openssl/pem.h>
 | 
					
						
							|  |  |  | #include <openssl/x509v3.h>
 | 
					
						
							|  |  |  | #include <openssl/err.h>
 | 
					
						
							|  |  |  | #include <openssl/cms.h>
 | 
					
						
							|  |  |  | #include <openssl/rand.h>
 | 
					
						
							| 
									
										
										
										
											2020-09-07 02:11:34 +08:00
										 |  |  | #include "crypto/evp.h"
 | 
					
						
							| 
									
										
										
										
											2019-09-28 06:45:40 +08:00
										 |  |  | #include "cms_local.h"
 | 
					
						
							| 
									
										
										
										
											2008-03-14 08:58:43 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* CMS EncryptedData Utilities */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-15 03:37:56 +08:00
										 |  |  | /* Return BIO based on EncryptedContentInfo and key */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-18 12:03:25 +08:00
										 |  |  | BIO *ossl_cms_EncryptedContent_init_bio(CMS_EncryptedContentInfo *ec, | 
					
						
							|  |  |  |                                         const CMS_CTX *cms_ctx) | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | { | 
					
						
							|  |  |  |     BIO *b; | 
					
						
							|  |  |  |     EVP_CIPHER_CTX *ctx; | 
					
						
							| 
									
										
										
										
											2020-07-25 16:04:55 +08:00
										 |  |  |     EVP_CIPHER *fetched_ciph = NULL; | 
					
						
							|  |  |  |     const EVP_CIPHER *cipher = NULL; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |     X509_ALGOR *calg = ec->contentEncryptionAlgorithm; | 
					
						
							| 
									
										
										
										
											2020-09-07 02:11:34 +08:00
										 |  |  |     evp_cipher_aead_asn1_params aparams; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |     unsigned char iv[EVP_MAX_IV_LENGTH], *piv = NULL; | 
					
						
							|  |  |  |     unsigned char *tkey = NULL; | 
					
						
							| 
									
										
										
										
											2020-07-07 07:46:37 +08:00
										 |  |  |     int len; | 
					
						
							| 
									
										
										
										
											2020-09-07 02:11:34 +08:00
										 |  |  |     int ivlen = 0; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |     size_t tkeylen = 0; | 
					
						
							|  |  |  |     int ok = 0; | 
					
						
							|  |  |  |     int enc, keep_key = 0; | 
					
						
							| 
									
										
										
										
											2021-02-18 12:03:25 +08:00
										 |  |  |     OSSL_LIB_CTX *libctx = ossl_cms_ctx_get0_libctx(cms_ctx); | 
					
						
							|  |  |  |     const char *propq = ossl_cms_ctx_get0_propq(cms_ctx); | 
					
						
							| 
									
										
										
										
											2008-03-14 08:58:43 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |     enc = ec->cipher ? 1 : 0; | 
					
						
							| 
									
										
										
										
											2008-03-14 08:58:43 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |     b = BIO_new(BIO_f_cipher()); | 
					
						
							| 
									
										
										
										
											2015-10-30 19:12:26 +08:00
										 |  |  |     if (b == NULL) { | 
					
						
							| 
									
										
										
										
											2020-11-04 19:23:19 +08:00
										 |  |  |         ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE); | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2008-03-14 08:58:43 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |     BIO_get_cipher_ctx(b, &ctx); | 
					
						
							| 
									
										
										
										
											2008-03-14 08:58:43 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-20 11:28:11 +08:00
										 |  |  |     (void)ERR_set_mark(); | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |     if (enc) { | 
					
						
							| 
									
										
										
										
											2020-07-25 16:04:55 +08:00
										 |  |  |         cipher = ec->cipher; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |         /*
 | 
					
						
							|  |  |  |          * If not keeping key set cipher to NULL so subsequent calls decrypt. | 
					
						
							|  |  |  |          */ | 
					
						
							| 
									
										
										
										
											2020-07-25 16:04:55 +08:00
										 |  |  |         if (ec->key != NULL) | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |             ec->cipher = NULL; | 
					
						
							|  |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2020-07-25 16:04:55 +08:00
										 |  |  |         cipher = EVP_get_cipherbyobj(calg->algorithm); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (cipher != NULL) { | 
					
						
							| 
									
										
										
											
												Rename all getters to use get/get0 in name
For functions that exist in 1.1.1 provide a simple aliases via #define.
Fixes #15236
Functions with OSSL_DECODER_, OSSL_ENCODER_, OSSL_STORE_LOADER_,
EVP_KEYEXCH_, EVP_KEM_, EVP_ASYM_CIPHER_, EVP_SIGNATURE_,
EVP_KEYMGMT_, EVP_RAND_, EVP_MAC_, EVP_KDF_, EVP_PKEY_,
EVP_MD_, and EVP_CIPHER_ prefixes are renamed.
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/15405)
											
										 
											2021-05-21 22:58:08 +08:00
										 |  |  |         fetched_ciph = EVP_CIPHER_fetch(libctx, EVP_CIPHER_get0_name(cipher), | 
					
						
							|  |  |  |                                         propq); | 
					
						
							| 
									
										
										
										
											2020-08-20 11:28:11 +08:00
										 |  |  |         if (fetched_ciph != NULL) | 
					
						
							|  |  |  |             cipher = fetched_ciph; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (cipher == NULL) { | 
					
						
							|  |  |  |         (void)ERR_clear_last_mark(); | 
					
						
							| 
									
										
										
										
											2020-11-04 19:23:19 +08:00
										 |  |  |         ERR_raise(ERR_LIB_CMS, CMS_R_UNKNOWN_CIPHER); | 
					
						
							| 
									
										
										
										
											2020-08-20 11:28:11 +08:00
										 |  |  |         goto err; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-08-20 11:28:11 +08:00
										 |  |  |     (void)ERR_pop_to_mark(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (EVP_CipherInit_ex(ctx, cipher, NULL, NULL, NULL, enc) <= 0) { | 
					
						
							| 
									
										
										
										
											2020-11-04 19:23:19 +08:00
										 |  |  |         ERR_raise(ERR_LIB_CMS, CMS_R_CIPHER_INITIALISATION_ERROR); | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |         goto err; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2008-03-14 08:58:43 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |     if (enc) { | 
					
						
							| 
									
										
										
											
												Rename all getters to use get/get0 in name
For functions that exist in 1.1.1 provide a simple aliases via #define.
Fixes #15236
Functions with OSSL_DECODER_, OSSL_ENCODER_, OSSL_STORE_LOADER_,
EVP_KEYEXCH_, EVP_KEM_, EVP_ASYM_CIPHER_, EVP_SIGNATURE_,
EVP_KEYMGMT_, EVP_RAND_, EVP_MAC_, EVP_KDF_, EVP_PKEY_,
EVP_MD_, and EVP_CIPHER_ prefixes are renamed.
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/15405)
											
										 
											2021-05-21 22:58:08 +08:00
										 |  |  |         calg->algorithm = OBJ_nid2obj(EVP_CIPHER_CTX_get_type(ctx)); | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |         /* Generate a random IV if we need one */ | 
					
						
							| 
									
										
										
											
												Rename all getters to use get/get0 in name
For functions that exist in 1.1.1 provide a simple aliases via #define.
Fixes #15236
Functions with OSSL_DECODER_, OSSL_ENCODER_, OSSL_STORE_LOADER_,
EVP_KEYEXCH_, EVP_KEM_, EVP_ASYM_CIPHER_, EVP_SIGNATURE_,
EVP_KEYMGMT_, EVP_RAND_, EVP_MAC_, EVP_KDF_, EVP_PKEY_,
EVP_MD_, and EVP_CIPHER_ prefixes are renamed.
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/15405)
											
										 
											2021-05-21 22:58:08 +08:00
										 |  |  |         ivlen = EVP_CIPHER_CTX_get_iv_length(ctx); | 
					
						
							| 
									
										
										
										
											2022-07-27 18:15:07 +08:00
										 |  |  |         if (ivlen < 0) { | 
					
						
							|  |  |  |             ERR_raise(ERR_LIB_CMS, ERR_R_EVP_LIB); | 
					
						
							|  |  |  |             goto err; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |         if (ivlen > 0) { | 
					
						
							| 
									
										
										
										
											2021-05-28 12:46:40 +08:00
										 |  |  |             if (RAND_bytes_ex(libctx, iv, ivlen, 0) <= 0) | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |                 goto err; | 
					
						
							|  |  |  |             piv = iv; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-09-07 02:11:34 +08:00
										 |  |  |     } else { | 
					
						
							|  |  |  |         if (evp_cipher_asn1_to_param_ex(ctx, calg->parameter, &aparams) <= 0) { | 
					
						
							| 
									
										
										
										
											2020-11-04 19:23:19 +08:00
										 |  |  |             ERR_raise(ERR_LIB_CMS, CMS_R_CIPHER_PARAMETER_INITIALISATION_ERROR); | 
					
						
							| 
									
										
										
										
											2020-09-07 02:11:34 +08:00
										 |  |  |             goto err; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
											
												Rename all getters to use get/get0 in name
For functions that exist in 1.1.1 provide a simple aliases via #define.
Fixes #15236
Functions with OSSL_DECODER_, OSSL_ENCODER_, OSSL_STORE_LOADER_,
EVP_KEYEXCH_, EVP_KEM_, EVP_ASYM_CIPHER_, EVP_SIGNATURE_,
EVP_KEYMGMT_, EVP_RAND_, EVP_MAC_, EVP_KDF_, EVP_PKEY_,
EVP_MD_, and EVP_CIPHER_ prefixes are renamed.
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/15405)
											
										 
											2021-05-21 22:58:08 +08:00
										 |  |  |         if ((EVP_CIPHER_get_flags(cipher) & EVP_CIPH_FLAG_AEAD_CIPHER)) { | 
					
						
							| 
									
										
										
										
											2020-09-07 02:11:34 +08:00
										 |  |  |             piv = aparams.iv; | 
					
						
							|  |  |  |             if (ec->taglen > 0 | 
					
						
							|  |  |  |                     && EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, | 
					
						
							|  |  |  |                                            ec->taglen, ec->tag) <= 0) { | 
					
						
							| 
									
										
										
										
											2020-11-04 19:23:19 +08:00
										 |  |  |                 ERR_raise(ERR_LIB_CMS, CMS_R_CIPHER_AEAD_SET_TAG_ERROR); | 
					
						
							| 
									
										
										
										
											2020-09-07 02:11:34 +08:00
										 |  |  |                 goto err; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
											
												Rename all getters to use get/get0 in name
For functions that exist in 1.1.1 provide a simple aliases via #define.
Fixes #15236
Functions with OSSL_DECODER_, OSSL_ENCODER_, OSSL_STORE_LOADER_,
EVP_KEYEXCH_, EVP_KEM_, EVP_ASYM_CIPHER_, EVP_SIGNATURE_,
EVP_KEYMGMT_, EVP_RAND_, EVP_MAC_, EVP_KDF_, EVP_PKEY_,
EVP_MD_, and EVP_CIPHER_ prefixes are renamed.
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/15405)
											
										 
											2021-05-21 22:58:08 +08:00
										 |  |  |     len = EVP_CIPHER_CTX_get_key_length(ctx); | 
					
						
							| 
									
										
										
										
											2020-07-07 07:46:37 +08:00
										 |  |  |     if (len <= 0) | 
					
						
							|  |  |  |         goto err; | 
					
						
							|  |  |  |     tkeylen = (size_t)len; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |     /* Generate random session key */ | 
					
						
							|  |  |  |     if (!enc || !ec->key) { | 
					
						
							|  |  |  |         tkey = OPENSSL_malloc(tkeylen); | 
					
						
							| 
									
										
										
										
											2015-10-30 19:12:26 +08:00
										 |  |  |         if (tkey == NULL) { | 
					
						
							| 
									
										
										
										
											2020-11-04 19:23:19 +08:00
										 |  |  |             ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE); | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |             goto err; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (EVP_CIPHER_CTX_rand_key(ctx, tkey) <= 0) | 
					
						
							|  |  |  |             goto err; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2012-03-13 00:31:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |     if (!ec->key) { | 
					
						
							|  |  |  |         ec->key = tkey; | 
					
						
							|  |  |  |         ec->keylen = tkeylen; | 
					
						
							|  |  |  |         tkey = NULL; | 
					
						
							|  |  |  |         if (enc) | 
					
						
							|  |  |  |             keep_key = 1; | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |             ERR_clear_error(); | 
					
						
							| 
									
										
										
										
											2012-03-13 00:31:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2008-03-15 21:37:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |     if (ec->keylen != tkeylen) { | 
					
						
							|  |  |  |         /* If necessary set key length */ | 
					
						
							|  |  |  |         if (EVP_CIPHER_CTX_set_key_length(ctx, ec->keylen) <= 0) { | 
					
						
							|  |  |  |             /*
 | 
					
						
							|  |  |  |              * Only reveal failure if debugging so we don't leak information | 
					
						
							|  |  |  |              * which may be useful in MMA. | 
					
						
							|  |  |  |              */ | 
					
						
							|  |  |  |             if (enc || ec->debug) { | 
					
						
							| 
									
										
										
										
											2020-11-04 19:23:19 +08:00
										 |  |  |                 ERR_raise(ERR_LIB_CMS, CMS_R_INVALID_KEY_LENGTH); | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |                 goto err; | 
					
						
							|  |  |  |             } else { | 
					
						
							|  |  |  |                 /* Use random key */ | 
					
						
							| 
									
										
										
										
											2015-05-01 05:57:32 +08:00
										 |  |  |                 OPENSSL_clear_free(ec->key, ec->keylen); | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |                 ec->key = tkey; | 
					
						
							|  |  |  |                 ec->keylen = tkeylen; | 
					
						
							|  |  |  |                 tkey = NULL; | 
					
						
							|  |  |  |                 ERR_clear_error(); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2008-03-14 08:58:43 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |     if (EVP_CipherInit_ex(ctx, NULL, NULL, ec->key, piv, enc) <= 0) { | 
					
						
							| 
									
										
										
										
											2020-11-04 19:23:19 +08:00
										 |  |  |         ERR_raise(ERR_LIB_CMS, CMS_R_CIPHER_INITIALISATION_ERROR); | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |         goto err; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-05-07 02:27:49 +08:00
										 |  |  |     if (enc) { | 
					
						
							|  |  |  |         calg->parameter = ASN1_TYPE_new(); | 
					
						
							|  |  |  |         if (calg->parameter == NULL) { | 
					
						
							| 
									
										
										
										
											2020-11-04 19:23:19 +08:00
										 |  |  |             ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE); | 
					
						
							| 
									
										
										
										
											2016-05-07 02:27:49 +08:00
										 |  |  |             goto err; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
											
												Rename all getters to use get/get0 in name
For functions that exist in 1.1.1 provide a simple aliases via #define.
Fixes #15236
Functions with OSSL_DECODER_, OSSL_ENCODER_, OSSL_STORE_LOADER_,
EVP_KEYEXCH_, EVP_KEM_, EVP_ASYM_CIPHER_, EVP_SIGNATURE_,
EVP_KEYMGMT_, EVP_RAND_, EVP_MAC_, EVP_KDF_, EVP_PKEY_,
EVP_MD_, and EVP_CIPHER_ prefixes are renamed.
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/15405)
											
										 
											2021-05-21 22:58:08 +08:00
										 |  |  |         if ((EVP_CIPHER_get_flags(cipher) & EVP_CIPH_FLAG_AEAD_CIPHER)) { | 
					
						
							| 
									
										
										
										
											2020-09-07 02:11:34 +08:00
										 |  |  |             memcpy(aparams.iv, piv, ivlen); | 
					
						
							|  |  |  |             aparams.iv_len = ivlen; | 
					
						
							| 
									
										
										
											
												Rename all getters to use get/get0 in name
For functions that exist in 1.1.1 provide a simple aliases via #define.
Fixes #15236
Functions with OSSL_DECODER_, OSSL_ENCODER_, OSSL_STORE_LOADER_,
EVP_KEYEXCH_, EVP_KEM_, EVP_ASYM_CIPHER_, EVP_SIGNATURE_,
EVP_KEYMGMT_, EVP_RAND_, EVP_MAC_, EVP_KDF_, EVP_PKEY_,
EVP_MD_, and EVP_CIPHER_ prefixes are renamed.
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/15405)
											
										 
											2021-05-21 22:58:08 +08:00
										 |  |  |             aparams.tag_len = EVP_CIPHER_CTX_get_tag_length(ctx); | 
					
						
							| 
									
										
										
										
											2020-09-07 02:11:34 +08:00
										 |  |  |             if (aparams.tag_len <= 0) | 
					
						
							|  |  |  |                 goto err; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (evp_cipher_param_to_asn1_ex(ctx, calg->parameter, &aparams) <= 0) { | 
					
						
							| 
									
										
										
										
											2020-11-04 19:23:19 +08:00
										 |  |  |             ERR_raise(ERR_LIB_CMS, CMS_R_CIPHER_PARAMETER_INITIALISATION_ERROR); | 
					
						
							| 
									
										
										
										
											2016-05-07 02:27:49 +08:00
										 |  |  |             goto err; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         /* If parameter type not set omit parameter */ | 
					
						
							|  |  |  |         if (calg->parameter->type == V_ASN1_UNDEF) { | 
					
						
							|  |  |  |             ASN1_TYPE_free(calg->parameter); | 
					
						
							|  |  |  |             calg->parameter = NULL; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |     } | 
					
						
							|  |  |  |     ok = 1; | 
					
						
							| 
									
										
										
										
											2008-03-14 08:58:43 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |  err: | 
					
						
							| 
									
										
										
										
											2020-08-20 11:28:11 +08:00
										 |  |  |     EVP_CIPHER_free(fetched_ciph); | 
					
						
							| 
									
										
										
										
											2015-08-01 22:37:44 +08:00
										 |  |  |     if (!keep_key || !ok) { | 
					
						
							| 
									
										
										
										
											2015-05-01 05:57:32 +08:00
										 |  |  |         OPENSSL_clear_free(ec->key, ec->keylen); | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |         ec->key = NULL; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-05-01 05:57:32 +08:00
										 |  |  |     OPENSSL_clear_free(tkey, tkeylen); | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |     if (ok) | 
					
						
							|  |  |  |         return b; | 
					
						
							|  |  |  |     BIO_free(b); | 
					
						
							|  |  |  |     return NULL; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-18 12:03:25 +08:00
										 |  |  | int ossl_cms_EncryptedContent_init(CMS_EncryptedContentInfo *ec, | 
					
						
							|  |  |  |                                    const EVP_CIPHER *cipher, | 
					
						
							|  |  |  |                                    const unsigned char *key, size_t keylen, | 
					
						
							|  |  |  |                                    const CMS_CTX *cms_ctx) | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | { | 
					
						
							|  |  |  |     ec->cipher = cipher; | 
					
						
							|  |  |  |     if (key) { | 
					
						
							| 
									
										
										
										
											2018-04-03 23:31:16 +08:00
										 |  |  |         if ((ec->key = OPENSSL_malloc(keylen)) == NULL) { | 
					
						
							| 
									
										
										
										
											2020-11-04 19:23:19 +08:00
										 |  |  |             ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE); | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |             return 0; | 
					
						
							| 
									
										
										
										
											2018-04-03 23:31:16 +08:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |         memcpy(ec->key, key, keylen); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     ec->keylen = keylen; | 
					
						
							| 
									
										
										
										
											2020-07-25 16:04:55 +08:00
										 |  |  |     if (cipher != NULL) | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |         ec->contentType = OBJ_nid2obj(NID_pkcs7_data); | 
					
						
							|  |  |  |     return 1; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2008-03-15 03:37:56 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | int CMS_EncryptedData_set1_key(CMS_ContentInfo *cms, const EVP_CIPHER *ciph, | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |                                const unsigned char *key, size_t keylen) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     CMS_EncryptedContentInfo *ec; | 
					
						
							| 
									
										
										
										
											2020-07-25 16:04:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |     if (!key || !keylen) { | 
					
						
							| 
									
										
										
										
											2020-11-04 19:23:19 +08:00
										 |  |  |         ERR_raise(ERR_LIB_CMS, CMS_R_NO_KEY); | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (ciph) { | 
					
						
							|  |  |  |         cms->d.encryptedData = M_ASN1_new_of(CMS_EncryptedData); | 
					
						
							|  |  |  |         if (!cms->d.encryptedData) { | 
					
						
							| 
									
										
										
										
											2020-11-04 19:23:19 +08:00
										 |  |  |             ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE); | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |             return 0; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         cms->contentType = OBJ_nid2obj(NID_pkcs7_encrypted); | 
					
						
							|  |  |  |         cms->d.encryptedData->version = 0; | 
					
						
							|  |  |  |     } else if (OBJ_obj2nid(cms->contentType) != NID_pkcs7_encrypted) { | 
					
						
							| 
									
										
										
										
											2020-11-04 19:23:19 +08:00
										 |  |  |         ERR_raise(ERR_LIB_CMS, CMS_R_NOT_ENCRYPTED_DATA); | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     ec = cms->d.encryptedData->encryptedContentInfo; | 
					
						
							| 
									
										
										
										
											2021-02-18 12:03:25 +08:00
										 |  |  |     return ossl_cms_EncryptedContent_init(ec, ciph, key, keylen, | 
					
						
							|  |  |  |                                           ossl_cms_get0_cmsctx(cms)); | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2008-03-15 03:37:56 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-18 12:03:25 +08:00
										 |  |  | BIO *ossl_cms_EncryptedData_init_bio(const CMS_ContentInfo *cms) | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | { | 
					
						
							|  |  |  |     CMS_EncryptedData *enc = cms->d.encryptedData; | 
					
						
							|  |  |  |     if (enc->encryptedContentInfo->cipher && enc->unprotectedAttrs) | 
					
						
							|  |  |  |         enc->version = 2; | 
					
						
							| 
									
										
										
										
											2021-02-18 12:03:25 +08:00
										 |  |  |     return ossl_cms_EncryptedContent_init_bio(enc->encryptedContentInfo, | 
					
						
							|  |  |  |                                               ossl_cms_get0_cmsctx(cms)); | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | } |