| 
									
										
										
										
											2013-07-17 21:36:39 +08:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2025-02-11 22:27:50 +08:00
										 |  |  |  * Copyright 2013-2025 The OpenSSL Project Authors. All Rights Reserved. | 
					
						
							| 
									
										
										
										
											2013-07-17 21:36:39 +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
 | 
					
						
							| 
									
										
										
										
											2013-07-17 21:36:39 +08:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-13 09:00:57 +08:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Low level key APIs (DH etc) are deprecated for public use, but still ok for | 
					
						
							|  |  |  |  * internal use. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | #include "internal/deprecated.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-14 22:56:48 +08:00
										 |  |  | #include "internal/cryptlib.h"
 | 
					
						
							| 
									
										
										
										
											2013-07-17 21:36:39 +08:00
										 |  |  | #include <openssl/asn1t.h>
 | 
					
						
							|  |  |  | #include <openssl/pem.h>
 | 
					
						
							|  |  |  | #include <openssl/x509v3.h>
 | 
					
						
							|  |  |  | #include <openssl/err.h>
 | 
					
						
							|  |  |  | #include <openssl/cms.h>
 | 
					
						
							|  |  |  | #include <openssl/aes.h>
 | 
					
						
							| 
									
										
										
										
											2019-09-28 06:45:40 +08:00
										 |  |  | #include "cms_local.h"
 | 
					
						
							| 
									
										
										
										
											2019-09-28 06:45:33 +08:00
										 |  |  | #include "crypto/asn1.h"
 | 
					
						
							| 
									
										
										
										
											2013-07-17 21:36:39 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* Key Agreement Recipient Info (KARI) routines */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int CMS_RecipientInfo_kari_get0_alg(CMS_RecipientInfo *ri, | 
					
						
							|  |  |  |                                     X509_ALGOR **palg, | 
					
						
							|  |  |  |                                     ASN1_OCTET_STRING **pukm) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (ri->type != CMS_RECIPINFO_AGREE) { | 
					
						
							| 
									
										
										
										
											2020-11-04 19:23:19 +08:00
										 |  |  |         ERR_raise(ERR_LIB_CMS, CMS_R_NOT_KEY_AGREEMENT); | 
					
						
							| 
									
										
										
										
											2013-07-17 21:36:39 +08:00
										 |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (palg) | 
					
						
							|  |  |  |         *palg = ri->d.kari->keyEncryptionAlgorithm; | 
					
						
							|  |  |  |     if (pukm) | 
					
						
							|  |  |  |         *pukm = ri->d.kari->ukm; | 
					
						
							|  |  |  |     return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Retrieve recipient encrypted keys from a kari */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | STACK_OF(CMS_RecipientEncryptedKey) | 
					
						
							|  |  |  | *CMS_RecipientInfo_kari_get0_reks(CMS_RecipientInfo *ri) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (ri->type != CMS_RECIPINFO_AGREE) { | 
					
						
							| 
									
										
										
										
											2020-11-04 19:23:19 +08:00
										 |  |  |         ERR_raise(ERR_LIB_CMS, CMS_R_NOT_KEY_AGREEMENT); | 
					
						
							| 
									
										
										
										
											2013-07-17 21:36:39 +08:00
										 |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return ri->d.kari->recipientEncryptedKeys; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int CMS_RecipientInfo_kari_get0_orig_id(CMS_RecipientInfo *ri, | 
					
						
							|  |  |  |                                         X509_ALGOR **pubalg, | 
					
						
							|  |  |  |                                         ASN1_BIT_STRING **pubkey, | 
					
						
							|  |  |  |                                         ASN1_OCTET_STRING **keyid, | 
					
						
							|  |  |  |                                         X509_NAME **issuer, | 
					
						
							|  |  |  |                                         ASN1_INTEGER **sno) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     CMS_OriginatorIdentifierOrKey *oik; | 
					
						
							| 
									
										
										
										
											2020-07-25 16:04:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-17 21:36:39 +08:00
										 |  |  |     if (ri->type != CMS_RECIPINFO_AGREE) { | 
					
						
							| 
									
										
										
										
											2020-11-04 19:23:19 +08:00
										 |  |  |         ERR_raise(ERR_LIB_CMS, CMS_R_NOT_KEY_AGREEMENT); | 
					
						
							| 
									
										
										
										
											2013-07-17 21:36:39 +08:00
										 |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     oik = ri->d.kari->originator; | 
					
						
							|  |  |  |     if (issuer) | 
					
						
							|  |  |  |         *issuer = NULL; | 
					
						
							|  |  |  |     if (sno) | 
					
						
							|  |  |  |         *sno = NULL; | 
					
						
							|  |  |  |     if (keyid) | 
					
						
							|  |  |  |         *keyid = NULL; | 
					
						
							|  |  |  |     if (pubalg) | 
					
						
							|  |  |  |         *pubalg = NULL; | 
					
						
							|  |  |  |     if (pubkey) | 
					
						
							|  |  |  |         *pubkey = NULL; | 
					
						
							|  |  |  |     if (oik->type == CMS_OIK_ISSUER_SERIAL) { | 
					
						
							|  |  |  |         if (issuer) | 
					
						
							|  |  |  |             *issuer = oik->d.issuerAndSerialNumber->issuer; | 
					
						
							|  |  |  |         if (sno) | 
					
						
							|  |  |  |             *sno = oik->d.issuerAndSerialNumber->serialNumber; | 
					
						
							|  |  |  |     } else if (oik->type == CMS_OIK_KEYIDENTIFIER) { | 
					
						
							|  |  |  |         if (keyid) | 
					
						
							|  |  |  |             *keyid = oik->d.subjectKeyIdentifier; | 
					
						
							|  |  |  |     } else if (oik->type == CMS_OIK_PUBKEY) { | 
					
						
							|  |  |  |         if (pubalg) | 
					
						
							|  |  |  |             *pubalg = oik->d.originatorKey->algorithm; | 
					
						
							|  |  |  |         if (pubkey) | 
					
						
							|  |  |  |             *pubkey = oik->d.originatorKey->publicKey; | 
					
						
							|  |  |  |     } else | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int CMS_RecipientInfo_kari_orig_id_cmp(CMS_RecipientInfo *ri, X509 *cert) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     CMS_OriginatorIdentifierOrKey *oik; | 
					
						
							| 
									
										
										
										
											2020-07-25 16:04:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-17 21:36:39 +08:00
										 |  |  |     if (ri->type != CMS_RECIPINFO_AGREE) { | 
					
						
							| 
									
										
										
										
											2020-11-04 19:23:19 +08:00
										 |  |  |         ERR_raise(ERR_LIB_CMS, CMS_R_NOT_KEY_AGREEMENT); | 
					
						
							| 
									
										
										
										
											2013-07-17 21:36:39 +08:00
										 |  |  |         return -2; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     oik = ri->d.kari->originator; | 
					
						
							|  |  |  |     if (oik->type == CMS_OIK_ISSUER_SERIAL) | 
					
						
							| 
									
										
										
										
											2021-02-18 12:03:25 +08:00
										 |  |  |         return ossl_cms_ias_cert_cmp(oik->d.issuerAndSerialNumber, cert); | 
					
						
							| 
									
										
										
										
											2013-07-17 21:36:39 +08:00
										 |  |  |     else if (oik->type == CMS_OIK_KEYIDENTIFIER) | 
					
						
							| 
									
										
										
										
											2021-02-18 12:03:25 +08:00
										 |  |  |         return ossl_cms_keyid_cert_cmp(oik->d.subjectKeyIdentifier, cert); | 
					
						
							| 
									
										
										
										
											2013-07-17 21:36:39 +08:00
										 |  |  |     return -1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int CMS_RecipientEncryptedKey_get0_id(CMS_RecipientEncryptedKey *rek, | 
					
						
							|  |  |  |                                       ASN1_OCTET_STRING **keyid, | 
					
						
							|  |  |  |                                       ASN1_GENERALIZEDTIME **tm, | 
					
						
							|  |  |  |                                       CMS_OtherKeyAttribute **other, | 
					
						
							|  |  |  |                                       X509_NAME **issuer, ASN1_INTEGER **sno) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     CMS_KeyAgreeRecipientIdentifier *rid = rek->rid; | 
					
						
							| 
									
										
										
										
											2020-07-25 16:04:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-17 21:36:39 +08:00
										 |  |  |     if (rid->type == CMS_REK_ISSUER_SERIAL) { | 
					
						
							|  |  |  |         if (issuer) | 
					
						
							|  |  |  |             *issuer = rid->d.issuerAndSerialNumber->issuer; | 
					
						
							|  |  |  |         if (sno) | 
					
						
							|  |  |  |             *sno = rid->d.issuerAndSerialNumber->serialNumber; | 
					
						
							|  |  |  |         if (keyid) | 
					
						
							|  |  |  |             *keyid = NULL; | 
					
						
							|  |  |  |         if (tm) | 
					
						
							|  |  |  |             *tm = NULL; | 
					
						
							|  |  |  |         if (other) | 
					
						
							|  |  |  |             *other = NULL; | 
					
						
							|  |  |  |     } else if (rid->type == CMS_REK_KEYIDENTIFIER) { | 
					
						
							|  |  |  |         if (keyid) | 
					
						
							|  |  |  |             *keyid = rid->d.rKeyId->subjectKeyIdentifier; | 
					
						
							|  |  |  |         if (tm) | 
					
						
							|  |  |  |             *tm = rid->d.rKeyId->date; | 
					
						
							|  |  |  |         if (other) | 
					
						
							|  |  |  |             *other = rid->d.rKeyId->other; | 
					
						
							|  |  |  |         if (issuer) | 
					
						
							|  |  |  |             *issuer = NULL; | 
					
						
							|  |  |  |         if (sno) | 
					
						
							|  |  |  |             *sno = NULL; | 
					
						
							|  |  |  |     } else | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int CMS_RecipientEncryptedKey_cert_cmp(CMS_RecipientEncryptedKey *rek, | 
					
						
							|  |  |  |                                        X509 *cert) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     CMS_KeyAgreeRecipientIdentifier *rid = rek->rid; | 
					
						
							| 
									
										
										
										
											2020-07-25 16:04:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-17 21:36:39 +08:00
										 |  |  |     if (rid->type == CMS_REK_ISSUER_SERIAL) | 
					
						
							| 
									
										
										
										
											2021-02-18 12:03:25 +08:00
										 |  |  |         return ossl_cms_ias_cert_cmp(rid->d.issuerAndSerialNumber, cert); | 
					
						
							| 
									
										
										
										
											2013-07-17 21:36:39 +08:00
										 |  |  |     else if (rid->type == CMS_REK_KEYIDENTIFIER) | 
					
						
							| 
									
										
										
										
											2021-02-18 12:03:25 +08:00
										 |  |  |         return ossl_cms_keyid_cert_cmp(rid->d.rKeyId->subjectKeyIdentifier, | 
					
						
							|  |  |  |                                        cert); | 
					
						
							| 
									
										
										
										
											2013-07-17 21:36:39 +08:00
										 |  |  |     else | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-18 12:03:25 +08:00
										 |  |  | int CMS_RecipientInfo_kari_set0_pkey_and_peer(CMS_RecipientInfo *ri, | 
					
						
							|  |  |  |                                               EVP_PKEY *pk, X509 *peer) | 
					
						
							| 
									
										
										
										
											2013-07-17 21:36:39 +08:00
										 |  |  | { | 
					
						
							|  |  |  |     EVP_PKEY_CTX *pctx; | 
					
						
							|  |  |  |     CMS_KeyAgreeRecipientInfo *kari = ri->d.kari; | 
					
						
							| 
									
										
										
										
											2015-05-02 02:37:16 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     EVP_PKEY_CTX_free(kari->pctx); | 
					
						
							|  |  |  |     kari->pctx = NULL; | 
					
						
							| 
									
										
										
										
											2019-09-17 03:28:57 +08:00
										 |  |  |     if (pk == NULL) | 
					
						
							| 
									
										
										
										
											2013-07-17 21:36:39 +08:00
										 |  |  |         return 1; | 
					
						
							| 
									
										
										
										
											2020-01-20 23:17:44 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-18 12:03:25 +08:00
										 |  |  |     pctx = EVP_PKEY_CTX_new_from_pkey(ossl_cms_ctx_get0_libctx(kari->cms_ctx), | 
					
						
							|  |  |  |                                       pk, | 
					
						
							|  |  |  |                                       ossl_cms_ctx_get0_propq(kari->cms_ctx)); | 
					
						
							| 
									
										
										
										
											2019-09-17 03:28:57 +08:00
										 |  |  |     if (pctx == NULL || EVP_PKEY_derive_init(pctx) <= 0) | 
					
						
							| 
									
										
										
										
											2013-07-17 21:36:39 +08:00
										 |  |  |         goto err; | 
					
						
							| 
									
										
										
										
											2020-01-20 23:17:44 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (peer != NULL) { | 
					
						
							|  |  |  |         EVP_PKEY *pub_pkey = X509_get0_pubkey(peer); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (EVP_PKEY_derive_set_peer(pctx, pub_pkey) <= 0) | 
					
						
							|  |  |  |             goto err; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-17 21:36:39 +08:00
										 |  |  |     kari->pctx = pctx; | 
					
						
							|  |  |  |     return 1; | 
					
						
							|  |  |  |  err: | 
					
						
							| 
									
										
										
										
											2015-03-28 22:54:15 +08:00
										 |  |  |     EVP_PKEY_CTX_free(pctx); | 
					
						
							| 
									
										
										
										
											2013-07-17 21:36:39 +08:00
										 |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-20 23:17:44 +08:00
										 |  |  | int CMS_RecipientInfo_kari_set0_pkey(CMS_RecipientInfo *ri, EVP_PKEY *pk) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return CMS_RecipientInfo_kari_set0_pkey_and_peer(ri, pk, NULL); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-17 21:36:39 +08:00
										 |  |  | EVP_CIPHER_CTX *CMS_RecipientInfo_kari_get0_ctx(CMS_RecipientInfo *ri) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (ri->type == CMS_RECIPINFO_AGREE) | 
					
						
							| 
									
										
										
										
											2015-12-14 05:08:41 +08:00
										 |  |  |         return ri->d.kari->ctx; | 
					
						
							| 
									
										
										
										
											2013-07-17 21:36:39 +08:00
										 |  |  |     return NULL; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-17 21:36:39 +08:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Derive KEK and decrypt/encrypt with it to produce either the original CEK | 
					
						
							|  |  |  |  * or the encrypted CEK. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int cms_kek_cipher(unsigned char **pout, size_t *poutlen, | 
					
						
							|  |  |  |                           const unsigned char *in, size_t inlen, | 
					
						
							|  |  |  |                           CMS_KeyAgreeRecipientInfo *kari, int enc) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     /* Key encryption key */ | 
					
						
							|  |  |  |     unsigned char kek[EVP_MAX_KEY_LENGTH]; | 
					
						
							|  |  |  |     size_t keklen; | 
					
						
							|  |  |  |     int rv = 0; | 
					
						
							|  |  |  |     unsigned char *out = NULL; | 
					
						
							|  |  |  |     int outlen; | 
					
						
							| 
									
										
										
										
											2020-07-25 16:04: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
										 |  |  |     keklen = EVP_CIPHER_CTX_get_key_length(kari->ctx); | 
					
						
							| 
									
										
										
										
											2013-07-17 21:36:39 +08:00
										 |  |  |     if (keklen > EVP_MAX_KEY_LENGTH) | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     /* Derive KEK */ | 
					
						
							|  |  |  |     if (EVP_PKEY_derive(kari->pctx, kek, &keklen) <= 0) | 
					
						
							|  |  |  |         goto err; | 
					
						
							|  |  |  |     /* Set KEK in context */ | 
					
						
							| 
									
										
										
										
											2015-12-14 05:08:41 +08:00
										 |  |  |     if (!EVP_CipherInit_ex(kari->ctx, NULL, NULL, kek, NULL, enc)) | 
					
						
							| 
									
										
										
										
											2013-07-17 21:36:39 +08:00
										 |  |  |         goto err; | 
					
						
							|  |  |  |     /* obtain output length of ciphered key */ | 
					
						
							| 
									
										
										
										
											2015-12-14 05:08:41 +08:00
										 |  |  |     if (!EVP_CipherUpdate(kari->ctx, NULL, &outlen, in, inlen)) | 
					
						
							| 
									
										
										
										
											2013-07-17 21:36:39 +08:00
										 |  |  |         goto err; | 
					
						
							|  |  |  |     out = OPENSSL_malloc(outlen); | 
					
						
							| 
									
										
										
										
											2015-10-30 19:12:26 +08:00
										 |  |  |     if (out == NULL) | 
					
						
							| 
									
										
										
										
											2013-07-17 21:36:39 +08:00
										 |  |  |         goto err; | 
					
						
							| 
									
										
										
										
											2015-12-14 05:08:41 +08:00
										 |  |  |     if (!EVP_CipherUpdate(kari->ctx, out, &outlen, in, inlen)) | 
					
						
							| 
									
										
										
										
											2013-07-17 21:36:39 +08:00
										 |  |  |         goto err; | 
					
						
							|  |  |  |     *pout = out; | 
					
						
							|  |  |  |     *poutlen = (size_t)outlen; | 
					
						
							|  |  |  |     rv = 1; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-17 21:36:39 +08:00
										 |  |  |  err: | 
					
						
							|  |  |  |     OPENSSL_cleanse(kek, keklen); | 
					
						
							| 
									
										
										
										
											2015-05-01 22:02:07 +08:00
										 |  |  |     if (!rv) | 
					
						
							| 
									
										
										
										
											2013-07-17 21:36:39 +08:00
										 |  |  |         OPENSSL_free(out); | 
					
						
							| 
									
										
										
										
											2015-12-14 05:08:41 +08:00
										 |  |  |     EVP_CIPHER_CTX_reset(kari->ctx); | 
					
						
							|  |  |  |     /* FIXME: WHY IS kari->pctx freed here?  /RL */ | 
					
						
							| 
									
										
										
										
											2013-07-17 21:36:39 +08:00
										 |  |  |     EVP_PKEY_CTX_free(kari->pctx); | 
					
						
							|  |  |  |     kari->pctx = NULL; | 
					
						
							|  |  |  |     return rv; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-17 21:36:39 +08:00
										 |  |  | int CMS_RecipientInfo_kari_decrypt(CMS_ContentInfo *cms, | 
					
						
							|  |  |  |                                    CMS_RecipientInfo *ri, | 
					
						
							|  |  |  |                                    CMS_RecipientEncryptedKey *rek) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     int rv = 0; | 
					
						
							|  |  |  |     unsigned char *enckey = NULL, *cek = NULL; | 
					
						
							|  |  |  |     size_t enckeylen; | 
					
						
							|  |  |  |     size_t ceklen; | 
					
						
							|  |  |  |     CMS_EncryptedContentInfo *ec; | 
					
						
							| 
									
										
										
										
											2020-04-08 21:41:05 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-17 21:36:39 +08:00
										 |  |  |     enckeylen = rek->encryptedKey->length; | 
					
						
							|  |  |  |     enckey = rek->encryptedKey->data; | 
					
						
							|  |  |  |     /* Setup all parameters to derive KEK */ | 
					
						
							| 
									
										
										
										
											2021-02-18 12:03:25 +08:00
										 |  |  |     if (!ossl_cms_env_asn1_ctrl(ri, 1)) | 
					
						
							| 
									
										
										
										
											2013-07-17 21:36:39 +08:00
										 |  |  |         goto err; | 
					
						
							|  |  |  |     /* Attempt to decrypt CEK */ | 
					
						
							|  |  |  |     if (!cms_kek_cipher(&cek, &ceklen, enckey, enckeylen, ri->d.kari, 0)) | 
					
						
							|  |  |  |         goto err; | 
					
						
							| 
									
										
										
										
											2021-02-18 12:03:25 +08:00
										 |  |  |     ec = ossl_cms_get0_env_enc_content(cms); | 
					
						
							| 
									
										
										
										
											2015-05-01 05:57:32 +08:00
										 |  |  |     OPENSSL_clear_free(ec->key, ec->keylen); | 
					
						
							| 
									
										
										
										
											2013-07-17 21:36:39 +08:00
										 |  |  |     ec->key = cek; | 
					
						
							|  |  |  |     ec->keylen = ceklen; | 
					
						
							|  |  |  |     cek = NULL; | 
					
						
							|  |  |  |     rv = 1; | 
					
						
							|  |  |  |  err: | 
					
						
							| 
									
										
										
										
											2015-05-01 22:02:07 +08:00
										 |  |  |     OPENSSL_free(cek); | 
					
						
							| 
									
										
										
										
											2013-07-17 21:36:39 +08:00
										 |  |  |     return rv; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Create ephemeral key and initialise context based on it */ | 
					
						
							|  |  |  | static int cms_kari_create_ephemeral_key(CMS_KeyAgreeRecipientInfo *kari, | 
					
						
							|  |  |  |                                          EVP_PKEY *pk) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     EVP_PKEY_CTX *pctx = NULL; | 
					
						
							|  |  |  |     EVP_PKEY *ekey = NULL; | 
					
						
							|  |  |  |     int rv = 0; | 
					
						
							| 
									
										
										
										
											2020-07-25 16:04:55 +08:00
										 |  |  |     const CMS_CTX *ctx = kari->cms_ctx; | 
					
						
							| 
									
										
										
										
											2021-02-18 12:03:25 +08:00
										 |  |  |     OSSL_LIB_CTX *libctx = ossl_cms_ctx_get0_libctx(ctx); | 
					
						
							|  |  |  |     const char *propq = ossl_cms_ctx_get0_propq(ctx); | 
					
						
							| 
									
										
										
										
											2019-09-17 03:28:57 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-11 17:19:37 +08:00
										 |  |  |     pctx = EVP_PKEY_CTX_new_from_pkey(libctx, pk, propq); | 
					
						
							| 
									
										
										
										
											2019-09-17 03:28:57 +08:00
										 |  |  |     if (pctx == NULL) | 
					
						
							| 
									
										
										
										
											2013-07-17 21:36:39 +08:00
										 |  |  |         goto err; | 
					
						
							|  |  |  |     if (EVP_PKEY_keygen_init(pctx) <= 0) | 
					
						
							|  |  |  |         goto err; | 
					
						
							|  |  |  |     if (EVP_PKEY_keygen(pctx, &ekey) <= 0) | 
					
						
							|  |  |  |         goto err; | 
					
						
							|  |  |  |     EVP_PKEY_CTX_free(pctx); | 
					
						
							| 
									
										
										
										
											2020-12-11 17:19:37 +08:00
										 |  |  |     pctx = EVP_PKEY_CTX_new_from_pkey(libctx, ekey, propq); | 
					
						
							| 
									
										
										
										
											2019-09-17 03:28:57 +08:00
										 |  |  |     if (pctx == NULL) | 
					
						
							| 
									
										
										
										
											2013-07-17 21:36:39 +08:00
										 |  |  |         goto err; | 
					
						
							|  |  |  |     if (EVP_PKEY_derive_init(pctx) <= 0) | 
					
						
							|  |  |  |         goto err; | 
					
						
							|  |  |  |     kari->pctx = pctx; | 
					
						
							|  |  |  |     rv = 1; | 
					
						
							|  |  |  |  err: | 
					
						
							| 
									
										
										
										
											2015-03-28 22:54:15 +08:00
										 |  |  |     if (!rv) | 
					
						
							| 
									
										
										
										
											2013-07-17 21:36:39 +08:00
										 |  |  |         EVP_PKEY_CTX_free(pctx); | 
					
						
							| 
									
										
										
										
											2015-03-28 22:54:15 +08:00
										 |  |  |     EVP_PKEY_free(ekey); | 
					
						
							| 
									
										
										
										
											2013-07-17 21:36:39 +08:00
										 |  |  |     return rv; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-20 23:17:44 +08:00
										 |  |  | /* Set originator private key and initialise context based on it */ | 
					
						
							| 
									
										
										
										
											2020-07-25 16:04:55 +08:00
										 |  |  | static int cms_kari_set_originator_private_key(CMS_KeyAgreeRecipientInfo *kari, | 
					
						
							|  |  |  |                                                EVP_PKEY *originatorPrivKey ) | 
					
						
							| 
									
										
										
										
											2020-01-20 23:17:44 +08:00
										 |  |  | { | 
					
						
							|  |  |  |     EVP_PKEY_CTX *pctx = NULL; | 
					
						
							|  |  |  |     int rv = 0; | 
					
						
							| 
									
										
										
										
											2020-07-25 16:04:55 +08:00
										 |  |  |     const CMS_CTX *ctx = kari->cms_ctx; | 
					
						
							| 
									
										
										
										
											2020-01-20 23:17:44 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-18 12:03:25 +08:00
										 |  |  |     pctx = EVP_PKEY_CTX_new_from_pkey(ossl_cms_ctx_get0_libctx(ctx), | 
					
						
							| 
									
										
										
										
											2020-12-11 17:19:37 +08:00
										 |  |  |                                       originatorPrivKey, | 
					
						
							| 
									
										
										
										
											2021-02-18 12:03:25 +08:00
										 |  |  |                                       ossl_cms_ctx_get0_propq(ctx)); | 
					
						
							| 
									
										
										
										
											2020-01-20 23:17:44 +08:00
										 |  |  |     if (pctx == NULL) | 
					
						
							|  |  |  |         goto err; | 
					
						
							|  |  |  |     if (EVP_PKEY_derive_init(pctx) <= 0) | 
					
						
							|  |  |  |          goto err; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     kari->pctx = pctx; | 
					
						
							|  |  |  |     rv = 1; | 
					
						
							|  |  |  |  err: | 
					
						
							|  |  |  |     if (rv == 0) | 
					
						
							|  |  |  |         EVP_PKEY_CTX_free(pctx); | 
					
						
							|  |  |  |     return rv; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-13 14:26:14 +08:00
										 |  |  | /* Initialise a kari based on passed certificate and key */ | 
					
						
							| 
									
										
										
										
											2013-07-17 21:36:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-18 12:03:25 +08:00
										 |  |  | int ossl_cms_RecipientInfo_kari_init(CMS_RecipientInfo *ri,  X509 *recip, | 
					
						
							|  |  |  |                                      EVP_PKEY *recipPubKey, X509 *originator, | 
					
						
							|  |  |  |                                      EVP_PKEY *originatorPrivKey, | 
					
						
							|  |  |  |                                      unsigned int flags, const CMS_CTX *ctx) | 
					
						
							| 
									
										
										
										
											2013-07-17 21:36:39 +08:00
										 |  |  | { | 
					
						
							|  |  |  |     CMS_KeyAgreeRecipientInfo *kari; | 
					
						
							|  |  |  |     CMS_RecipientEncryptedKey *rek = NULL; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-17 21:36:39 +08:00
										 |  |  |     ri->d.kari = M_ASN1_new_of(CMS_KeyAgreeRecipientInfo); | 
					
						
							| 
									
										
										
										
											2020-07-25 16:04:55 +08:00
										 |  |  |     if (ri->d.kari == NULL) | 
					
						
							| 
									
										
										
										
											2013-07-17 21:36:39 +08:00
										 |  |  |         return 0; | 
					
						
							|  |  |  |     ri->type = CMS_RECIPINFO_AGREE; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-17 21:36:39 +08:00
										 |  |  |     kari = ri->d.kari; | 
					
						
							|  |  |  |     kari->version = 3; | 
					
						
							| 
									
										
										
										
											2020-07-25 16:04:55 +08:00
										 |  |  |     kari->cms_ctx = ctx; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-17 21:36:39 +08:00
										 |  |  |     rek = M_ASN1_new_of(CMS_RecipientEncryptedKey); | 
					
						
							| 
									
										
										
										
											2019-02-13 14:26:14 +08:00
										 |  |  |     if (rek == NULL) | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-17 21:36:39 +08:00
										 |  |  |     if (!sk_CMS_RecipientEncryptedKey_push(kari->recipientEncryptedKeys, rek)) { | 
					
						
							|  |  |  |         M_ASN1_free_of(rek, CMS_RecipientEncryptedKey); | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-17 21:36:39 +08:00
										 |  |  |     if (flags & CMS_USE_KEYID) { | 
					
						
							|  |  |  |         rek->rid->type = CMS_REK_KEYIDENTIFIER; | 
					
						
							| 
									
										
										
										
											2015-04-10 09:33:44 +08:00
										 |  |  |         rek->rid->d.rKeyId = M_ASN1_new_of(CMS_RecipientKeyIdentifier); | 
					
						
							|  |  |  |         if (rek->rid->d.rKeyId == NULL) | 
					
						
							|  |  |  |             return 0; | 
					
						
							| 
									
										
										
										
											2021-02-18 12:03:25 +08:00
										 |  |  |         if (!ossl_cms_set1_keyid(&rek->rid->d.rKeyId->subjectKeyIdentifier, recip)) | 
					
						
							| 
									
										
										
										
											2013-07-17 21:36:39 +08:00
										 |  |  |             return 0; | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         rek->rid->type = CMS_REK_ISSUER_SERIAL; | 
					
						
							| 
									
										
										
										
											2021-02-18 12:03:25 +08:00
										 |  |  |         if (!ossl_cms_set1_ias(&rek->rid->d.issuerAndSerialNumber, recip)) | 
					
						
							| 
									
										
										
										
											2013-07-17 21:36:39 +08:00
										 |  |  |             return 0; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-20 23:17:44 +08:00
										 |  |  |     if (originatorPrivKey == NULL && originator == NULL) { | 
					
						
							|  |  |  |         /* Create ephemeral key */ | 
					
						
							|  |  |  |         if (!cms_kari_create_ephemeral_key(kari, recipPubKey)) | 
					
						
							|  |  |  |             return 0; | 
					
						
							|  |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2020-04-27 06:05:23 +08:00
										 |  |  |         /* Use originator key */ | 
					
						
							|  |  |  |         CMS_OriginatorIdentifierOrKey *oik = ri->d.kari->originator; | 
					
						
							| 
									
										
										
										
											2020-01-20 23:17:44 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-27 06:05:23 +08:00
										 |  |  |         if (originatorPrivKey == NULL || originator == NULL) | 
					
						
							| 
									
										
										
										
											2020-01-20 23:17:44 +08:00
										 |  |  |             return 0; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-27 06:05:23 +08:00
										 |  |  |         if (flags & CMS_USE_ORIGINATOR_KEYID) { | 
					
						
							|  |  |  |              oik->type = CMS_OIK_KEYIDENTIFIER; | 
					
						
							|  |  |  |              oik->d.subjectKeyIdentifier = ASN1_OCTET_STRING_new(); | 
					
						
							|  |  |  |              if (oik->d.subjectKeyIdentifier == NULL) | 
					
						
							|  |  |  |                   return 0; | 
					
						
							| 
									
										
										
										
											2021-02-18 12:03:25 +08:00
										 |  |  |              if (!ossl_cms_set1_keyid(&oik->d.subjectKeyIdentifier, originator)) | 
					
						
							| 
									
										
										
										
											2020-04-27 06:05:23 +08:00
										 |  |  |                   return 0; | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |              oik->type = CMS_REK_ISSUER_SERIAL; | 
					
						
							| 
									
										
										
										
											2021-02-18 12:03:25 +08:00
										 |  |  |              if (!ossl_cms_set1_ias(&oik->d.issuerAndSerialNumber, originator)) | 
					
						
							| 
									
										
										
										
											2020-04-27 06:05:23 +08:00
										 |  |  |                   return 0; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (!cms_kari_set_originator_private_key(kari, originatorPrivKey)) | 
					
						
							|  |  |  |             return 0; | 
					
						
							| 
									
										
										
										
											2020-01-20 23:17:44 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     EVP_PKEY_up_ref(recipPubKey); | 
					
						
							|  |  |  |     rek->pkey = recipPubKey; | 
					
						
							| 
									
										
										
										
											2013-07-17 21:36:39 +08:00
										 |  |  |     return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int cms_wrap_init(CMS_KeyAgreeRecipientInfo *kari, | 
					
						
							|  |  |  |                          const EVP_CIPHER *cipher) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2020-07-25 16:04:55 +08:00
										 |  |  |     const CMS_CTX *cms_ctx = kari->cms_ctx; | 
					
						
							| 
									
										
										
										
											2015-12-14 05:08:41 +08:00
										 |  |  |     EVP_CIPHER_CTX *ctx = kari->ctx; | 
					
						
							| 
									
										
										
										
											2013-07-17 21:36:39 +08:00
										 |  |  |     const EVP_CIPHER *kekcipher; | 
					
						
							| 
									
										
										
										
											2020-07-25 16:04:55 +08:00
										 |  |  |     EVP_CIPHER *fetched_kekcipher; | 
					
						
							|  |  |  |     const char *kekcipher_name; | 
					
						
							| 
									
										
										
										
											2020-04-27 05:48:32 +08:00
										 |  |  |     int keylen; | 
					
						
							| 
									
										
										
										
											2020-01-20 23:17:44 +08:00
										 |  |  |     int ret; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-17 21:36:39 +08:00
										 |  |  |     /* If a suitable wrap algorithm is already set nothing to do */ | 
					
						
							| 
									
										
											  
											
												Add "origin" field to EVP_CIPHER, EVP_MD
Add a "where did this EVP_{CIPHER,MD} come from" flag: global, via fetch,
or via EVP_{CIPHER,MD}_meth_new.  Update EVP_{CIPHER,MD}_free to handle all
three origins. The flag is deliberately right before some function pointers,
so that compile-time failures (int/pointer) will occur, as opposed to
taking a bit in the existing "flags" field.  The "global variable" flag
is non-zero, so the default case of using OPENSSL_zalloc (for provider
ciphers), will do the right thing. Ref-counting is a no-op for
Make up_ref no-op for global MD and CIPHER objects
Deprecate EVP_MD_CTX_md().  Added EVP_MD_CTX_get0_md() (same semantics as
the deprecated function) and EVP_MD_CTX_get1_md().  Likewise, deprecate
EVP_CIPHER_CTX_cipher() in favor of EVP_CIPHER_CTX_get0_cipher(), and add
EVP_CIPHER_CTX_get1_CIPHER().
Refactor EVP_MD_free() and EVP_MD_meth_free() to call new common
evp_md_free_int() function.
Refactor EVP_CIPHER_free() and EVP_CIPHER_meth_free() to call new common
evp_cipher_free_int() function.
Also change some flags tests to explicit test == or != zero. E.g.,
        if (flags & x) --> if ((flags & x) != 0)
        if (!(flags & x)) --> if ((flags & x) == 0)
Only done for those lines where "get0_cipher" calls were made.
Reviewed-by: Matt Caswell <matt@openssl.org>
Reviewed-by: Richard Levitte <levitte@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/14193)
											
										 
											2021-02-17 06:51:56 +08:00
										 |  |  |     kekcipher = EVP_CIPHER_CTX_get0_cipher(ctx); | 
					
						
							| 
									
										
										
										
											2020-01-20 23:17:44 +08:00
										 |  |  |     if (kekcipher != 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
										 |  |  |         if (EVP_CIPHER_CTX_get_mode(ctx) != EVP_CIPH_WRAP_MODE) | 
					
						
							| 
									
										
										
										
											2013-07-17 21:36:39 +08:00
										 |  |  |             return 0; | 
					
						
							|  |  |  |         return 1; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-04-27 05:48:32 +08:00
										 |  |  |     if (cipher == NULL) | 
					
						
							|  |  |  |         return 0; | 
					
						
							| 
									
										
										
											
												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
										 |  |  |     keylen = EVP_CIPHER_get_key_length(cipher); | 
					
						
							|  |  |  |     if ((EVP_CIPHER_get_flags(cipher) & EVP_CIPH_FLAG_GET_WRAP_CIPHER) != 0) { | 
					
						
							| 
									
										
										
										
											2020-01-20 23:17:44 +08:00
										 |  |  |         ret = EVP_CIPHER_meth_get_ctrl(cipher)(NULL, EVP_CTRL_GET_WRAP_CIPHER, | 
					
						
							|  |  |  |                                                0, &kekcipher); | 
					
						
							|  |  |  |         if (ret <= 0) | 
					
						
							|  |  |  |              return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (kekcipher != 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
										 |  |  |              if (EVP_CIPHER_get_mode(kekcipher) != EVP_CIPH_WRAP_MODE) | 
					
						
							| 
									
										
										
										
											2020-01-20 23:17:44 +08:00
										 |  |  |                  return 0; | 
					
						
							| 
									
										
										
											
												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
										 |  |  |              kekcipher_name = EVP_CIPHER_get0_name(kekcipher); | 
					
						
							| 
									
										
										
										
											2020-07-25 16:04:55 +08:00
										 |  |  |              goto enc; | 
					
						
							| 
									
										
										
										
											2020-01-20 23:17:44 +08:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-17 21:36:39 +08:00
										 |  |  |     /*
 | 
					
						
							|  |  |  |      * Pick a cipher based on content encryption cipher. If it is DES3 use | 
					
						
							|  |  |  |      * DES3 wrap otherwise use AES wrap similar to key size. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-03-18 23:56:06 +08:00
										 |  |  | #ifndef OPENSSL_NO_DES
 | 
					
						
							| 
									
										
										
											
												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_type(cipher) == NID_des_ede3_cbc) | 
					
						
							| 
									
										
										
										
											2020-07-25 16:04:55 +08:00
										 |  |  |         kekcipher_name = SN_id_smime_alg_CMS3DESwrap; | 
					
						
							| 
									
										
										
										
											2016-03-18 23:56:06 +08:00
										 |  |  |     else | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  |     if (keylen <= 16) | 
					
						
							| 
									
										
										
										
											2020-07-25 16:04:55 +08:00
										 |  |  |         kekcipher_name = SN_id_aes128_wrap; | 
					
						
							| 
									
										
										
										
											2013-07-17 21:36:39 +08:00
										 |  |  |     else if (keylen <= 24) | 
					
						
							| 
									
										
										
										
											2020-07-25 16:04:55 +08:00
										 |  |  |         kekcipher_name = SN_id_aes192_wrap; | 
					
						
							| 
									
										
										
										
											2013-07-17 21:36:39 +08:00
										 |  |  |     else | 
					
						
							| 
									
										
										
										
											2020-07-25 16:04:55 +08:00
										 |  |  |         kekcipher_name = SN_id_aes256_wrap; | 
					
						
							|  |  |  | enc: | 
					
						
							| 
									
										
										
										
											2021-02-18 12:03:25 +08:00
										 |  |  |     fetched_kekcipher = EVP_CIPHER_fetch(ossl_cms_ctx_get0_libctx(cms_ctx), | 
					
						
							| 
									
										
										
										
											2020-12-11 17:19:37 +08:00
										 |  |  |                                          kekcipher_name, | 
					
						
							| 
									
										
										
										
											2021-02-18 12:03:25 +08:00
										 |  |  |                                          ossl_cms_ctx_get0_propq(cms_ctx)); | 
					
						
							| 
									
										
										
										
											2020-07-25 16:04:55 +08:00
										 |  |  |     if (fetched_kekcipher == NULL) | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     ret = EVP_EncryptInit_ex(ctx, fetched_kekcipher, NULL, NULL, NULL); | 
					
						
							|  |  |  |     EVP_CIPHER_free(fetched_kekcipher); | 
					
						
							|  |  |  |     return ret; | 
					
						
							| 
									
										
										
										
											2013-07-17 21:36:39 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Encrypt content key in key agreement recipient info */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-18 12:03:25 +08:00
										 |  |  | int ossl_cms_RecipientInfo_kari_encrypt(const CMS_ContentInfo *cms, | 
					
						
							|  |  |  |                                         CMS_RecipientInfo *ri) | 
					
						
							| 
									
										
										
										
											2013-07-17 21:36:39 +08:00
										 |  |  | { | 
					
						
							|  |  |  |     CMS_KeyAgreeRecipientInfo *kari; | 
					
						
							|  |  |  |     CMS_EncryptedContentInfo *ec; | 
					
						
							|  |  |  |     CMS_RecipientEncryptedKey *rek; | 
					
						
							|  |  |  |     STACK_OF(CMS_RecipientEncryptedKey) *reks; | 
					
						
							|  |  |  |     int i; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-17 21:36:39 +08:00
										 |  |  |     if (ri->type != CMS_RECIPINFO_AGREE) { | 
					
						
							| 
									
										
										
										
											2020-11-04 19:23:19 +08:00
										 |  |  |         ERR_raise(ERR_LIB_CMS, CMS_R_NOT_KEY_AGREEMENT); | 
					
						
							| 
									
										
										
										
											2013-07-17 21:36:39 +08:00
										 |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     kari = ri->d.kari; | 
					
						
							|  |  |  |     reks = kari->recipientEncryptedKeys; | 
					
						
							| 
									
										
										
										
											2021-02-18 12:03:25 +08:00
										 |  |  |     ec = ossl_cms_get0_env_enc_content(cms); | 
					
						
							| 
									
										
										
										
											2013-07-17 21:36:39 +08:00
										 |  |  |     /* Initialise wrap algorithm parameters */ | 
					
						
							|  |  |  |     if (!cms_wrap_init(kari, ec->cipher)) | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     /*
 | 
					
						
							| 
									
										
										
										
											2016-02-06 04:23:54 +08:00
										 |  |  |      * If no originator key set up initialise for ephemeral key the public key | 
					
						
							| 
									
										
										
										
											2013-07-17 21:36:39 +08:00
										 |  |  |      * ASN1 structure will set the actual public key value. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     if (kari->originator->type == -1) { | 
					
						
							|  |  |  |         CMS_OriginatorIdentifierOrKey *oik = kari->originator; | 
					
						
							|  |  |  |         oik->type = CMS_OIK_PUBKEY; | 
					
						
							|  |  |  |         oik->d.originatorKey = M_ASN1_new_of(CMS_OriginatorPublicKey); | 
					
						
							|  |  |  |         if (!oik->d.originatorKey) | 
					
						
							|  |  |  |             return 0; | 
					
						
							| 
									
										
										
										
											2024-12-13 20:48:23 +08:00
										 |  |  |     } else { | 
					
						
							|  |  |  |         /*
 | 
					
						
							|  |  |  |          * Currently it is not possible to get public key as it is not stored | 
					
						
							|  |  |  |          * during kari initialization. | 
					
						
							|  |  |  |          */ | 
					
						
							|  |  |  |         ERR_raise(ERR_LIB_CMS, CMS_R_ERROR_UNSUPPORTED_STATIC_KEY_AGREEMENT); | 
					
						
							|  |  |  |         return 0; | 
					
						
							| 
									
										
										
										
											2013-07-17 21:36:39 +08:00
										 |  |  |     } | 
					
						
							|  |  |  |     /* Initialise KDF algorithm */ | 
					
						
							| 
									
										
										
										
											2021-02-18 12:03:25 +08:00
										 |  |  |     if (!ossl_cms_env_asn1_ctrl(ri, 0)) | 
					
						
							| 
									
										
										
										
											2013-07-17 21:36:39 +08:00
										 |  |  |         return 0; | 
					
						
							|  |  |  |     /* For each rek, derive KEK, encrypt CEK */ | 
					
						
							|  |  |  |     for (i = 0; i < sk_CMS_RecipientEncryptedKey_num(reks); i++) { | 
					
						
							|  |  |  |         unsigned char *enckey; | 
					
						
							|  |  |  |         size_t enckeylen; | 
					
						
							|  |  |  |         rek = sk_CMS_RecipientEncryptedKey_value(reks, i); | 
					
						
							|  |  |  |         if (EVP_PKEY_derive_set_peer(kari->pctx, rek->pkey) <= 0) | 
					
						
							|  |  |  |             return 0; | 
					
						
							|  |  |  |         if (!cms_kek_cipher(&enckey, &enckeylen, ec->key, ec->keylen, | 
					
						
							|  |  |  |                             kari, 1)) | 
					
						
							|  |  |  |             return 0; | 
					
						
							|  |  |  |         ASN1_STRING_set0(rek->encryptedKey, enckey, enckeylen); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-17 21:36:39 +08:00
										 |  |  |     return 1; | 
					
						
							|  |  |  | } |