| 
									
										
										
										
											2016-05-18 03:38:09 +08:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2018-09-11 20:22:14 +08:00
										 |  |  |  * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved. | 
					
						
							| 
									
										
										
										
											1998-12-21 18:52:47 +08:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2016-05-18 03:38:09 +08:00
										 |  |  |  * Licensed under the OpenSSL license (the "License").  You may not use | 
					
						
							|  |  |  |  * 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
 | 
					
						
							| 
									
										
										
										
											1998-12-21 18:52:47 +08:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <stdio.h>
 | 
					
						
							| 
									
										
										
										
											2015-05-14 22:56:48 +08:00
										 |  |  | #include "internal/cryptlib.h"
 | 
					
						
							| 
									
										
										
										
											1999-04-24 06:13:45 +08:00
										 |  |  | #include <openssl/rand.h>
 | 
					
						
							| 
									
										
										
										
											2016-03-19 02:30:20 +08:00
										 |  |  | #include <openssl/rsa.h>
 | 
					
						
							| 
									
										
										
										
											1999-04-24 06:13:45 +08:00
										 |  |  | #include <openssl/evp.h>
 | 
					
						
							|  |  |  | #include <openssl/objects.h>
 | 
					
						
							|  |  |  | #include <openssl/x509.h>
 | 
					
						
							| 
									
										
										
										
											1998-12-21 18:52:47 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | int EVP_SealInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type, | 
					
						
							|  |  |  |                  unsigned char **ek, int *ekl, unsigned char *iv, | 
					
						
							|  |  |  |                  EVP_PKEY **pubk, int npubk) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     unsigned char key[EVP_MAX_KEY_LENGTH]; | 
					
						
							|  |  |  |     int i; | 
					
						
							| 
									
										
										
										
											2018-09-06 06:34:45 +08:00
										 |  |  |     int rv = 0; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (type) { | 
					
						
							| 
									
										
										
										
											2015-12-14 05:08:41 +08:00
										 |  |  |         EVP_CIPHER_CTX_reset(ctx); | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |         if (!EVP_EncryptInit_ex(ctx, type, NULL, NULL, NULL)) | 
					
						
							|  |  |  |             return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if ((npubk <= 0) || !pubk) | 
					
						
							|  |  |  |         return 1; | 
					
						
							|  |  |  |     if (EVP_CIPHER_CTX_rand_key(ctx, key) <= 0) | 
					
						
							|  |  |  |         return 0; | 
					
						
							| 
									
										
										
										
											2018-09-06 06:34:45 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-09 05:30:28 +08:00
										 |  |  |     if (EVP_CIPHER_CTX_iv_length(ctx) | 
					
						
							| 
									
										
										
										
											2018-09-06 06:34:45 +08:00
										 |  |  |             && RAND_bytes(iv, EVP_CIPHER_CTX_iv_length(ctx)) <= 0) | 
					
						
							|  |  |  |         goto err; | 
					
						
							| 
									
										
										
										
											1998-12-21 18:52:47 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |     if (!EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv)) | 
					
						
							| 
									
										
										
										
											2018-09-06 06:34:45 +08:00
										 |  |  |         goto err; | 
					
						
							| 
									
										
										
										
											1998-12-21 18:56:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |     for (i = 0; i < npubk; i++) { | 
					
						
							|  |  |  |         ekl[i] = | 
					
						
							|  |  |  |             EVP_PKEY_encrypt_old(ek[i], key, EVP_CIPHER_CTX_key_length(ctx), | 
					
						
							|  |  |  |                                  pubk[i]); | 
					
						
							| 
									
										
										
										
											2018-09-06 06:34:45 +08:00
										 |  |  |         if (ekl[i] <= 0) { | 
					
						
							|  |  |  |             rv = -1; | 
					
						
							|  |  |  |             goto err; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-09-06 06:34:45 +08:00
										 |  |  |     rv = npubk; | 
					
						
							|  |  |  | err: | 
					
						
							|  |  |  |     OPENSSL_cleanse(key, sizeof(key)); | 
					
						
							|  |  |  |     return rv; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											1998-12-21 18:52:47 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-05-12 01:37:08 +08:00
										 |  |  | int EVP_SealFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl) | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | { | 
					
						
							|  |  |  |     int i; | 
					
						
							|  |  |  |     i = EVP_EncryptFinal_ex(ctx, out, outl); | 
					
						
							|  |  |  |     if (i) | 
					
						
							|  |  |  |         i = EVP_EncryptInit_ex(ctx, NULL, NULL, NULL, NULL); | 
					
						
							|  |  |  |     return i; | 
					
						
							|  |  |  | } |