| 
									
										
										
										
											2019-07-17 14:59:09 +08:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2020-04-23 20:55:52 +08:00
										 |  |  |  * Copyright 2019-2020 The OpenSSL Project Authors. All Rights Reserved. | 
					
						
							| 
									
										
										
										
											2019-07-17 14:59:09 +08:00
										 |  |  |  * Copyright (c) 2019, Oracle and/or its affiliates.  All rights reserved. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Licensed under the Apache License 2.0 (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
 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <string.h>
 | 
					
						
							|  |  |  | #include <openssl/err.h>
 | 
					
						
							|  |  |  | #include <openssl/cryptoerr.h>
 | 
					
						
							|  |  |  | #include <openssl/params.h>
 | 
					
						
							| 
									
										
										
										
											2020-03-26 07:28:01 +08:00
										 |  |  | #include <openssl/types.h>
 | 
					
						
							| 
									
										
										
										
											2020-03-27 08:33:46 +08:00
										 |  |  | #include <openssl/safestack.h>
 | 
					
						
							| 
									
										
										
										
											2019-07-17 14:59:09 +08:00
										 |  |  | #include "internal/cryptlib.h"
 | 
					
						
							| 
									
										
										
										
											2020-03-24 12:58:57 +08:00
										 |  |  | #include "openssl/param_build.h"
 | 
					
						
							| 
									
										
										
										
											2019-07-17 14:59:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-26 07:28:01 +08:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Special internal param type to indicate the end of an allocate OSSL_PARAM | 
					
						
							|  |  |  |  * array. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2019-07-18 23:14:07 +08:00
										 |  |  | #define OSSL_PARAM_ALLOCATED_END    127
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-26 07:28:01 +08:00
										 |  |  | typedef struct { | 
					
						
							|  |  |  |     const char *key; | 
					
						
							|  |  |  |     int type; | 
					
						
							|  |  |  |     int secure; | 
					
						
							|  |  |  |     size_t size; | 
					
						
							|  |  |  |     size_t alloc_blocks; | 
					
						
							|  |  |  |     const BIGNUM *bn; | 
					
						
							|  |  |  |     const void *string; | 
					
						
							|  |  |  |     union { | 
					
						
							|  |  |  |         /*
 | 
					
						
							|  |  |  |          * These fields are never directly addressed, but their sizes are | 
					
						
							|  |  |  |          * imporant so that all native types can be copied here without overrun. | 
					
						
							|  |  |  |          */ | 
					
						
							|  |  |  |         ossl_intmax_t i; | 
					
						
							|  |  |  |         ossl_uintmax_t u; | 
					
						
							|  |  |  |         double d; | 
					
						
							|  |  |  |     } num; | 
					
						
							|  |  |  | } OSSL_PARAM_BLD_DEF; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-27 08:33:46 +08:00
										 |  |  | DEFINE_STACK_OF(OSSL_PARAM_BLD_DEF) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-26 07:28:01 +08:00
										 |  |  | struct ossl_param_bld_st { | 
					
						
							|  |  |  |     size_t total_blocks; | 
					
						
							|  |  |  |     size_t secure_blocks; | 
					
						
							| 
									
										
										
										
											2020-03-27 08:33:46 +08:00
										 |  |  |     STACK_OF(OSSL_PARAM_BLD_DEF) *params; | 
					
						
							| 
									
										
										
										
											2020-03-26 07:28:01 +08:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-17 14:59:09 +08:00
										 |  |  | typedef union { | 
					
						
							|  |  |  |     OSSL_UNION_ALIGN; | 
					
						
							|  |  |  | } OSSL_PARAM_BLD_BLOCK; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #define ALIGN_SIZE  sizeof(OSSL_PARAM_BLD_BLOCK)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static size_t bytes_to_blocks(size_t bytes) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return (bytes + ALIGN_SIZE - 1) / ALIGN_SIZE; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static OSSL_PARAM_BLD_DEF *param_push(OSSL_PARAM_BLD *bld, const char *key, | 
					
						
							|  |  |  |                                       int size, size_t alloc, int type, | 
					
						
							|  |  |  |                                       int secure) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2020-03-27 08:33:46 +08:00
										 |  |  |     OSSL_PARAM_BLD_DEF *pd = OPENSSL_zalloc(sizeof(*pd)); | 
					
						
							| 
									
										
										
										
											2019-07-17 14:59:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-27 08:33:46 +08:00
										 |  |  |     if (pd == NULL) { | 
					
						
							| 
									
										
										
										
											2020-11-04 19:23:19 +08:00
										 |  |  |         ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE); | 
					
						
							| 
									
										
										
										
											2019-07-17 14:59:09 +08:00
										 |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     pd->key = key; | 
					
						
							|  |  |  |     pd->type = type; | 
					
						
							|  |  |  |     pd->size = size; | 
					
						
							|  |  |  |     pd->alloc_blocks = bytes_to_blocks(size); | 
					
						
							|  |  |  |     if ((pd->secure = secure) != 0) | 
					
						
							|  |  |  |         bld->secure_blocks += pd->alloc_blocks; | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |         bld->total_blocks += pd->alloc_blocks; | 
					
						
							| 
									
										
										
										
											2020-03-27 08:33:46 +08:00
										 |  |  |     if (sk_OSSL_PARAM_BLD_DEF_push(bld->params, pd) <= 0) { | 
					
						
							|  |  |  |         OPENSSL_free(pd); | 
					
						
							|  |  |  |         pd = NULL; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-07-17 14:59:09 +08:00
										 |  |  |     return pd; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int param_push_num(OSSL_PARAM_BLD *bld, const char *key, | 
					
						
							|  |  |  |                           void *num, size_t size, int type) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     OSSL_PARAM_BLD_DEF *pd = param_push(bld, key, size, size, type, 0); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (pd == NULL) | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     if (size > sizeof(pd->num)) { | 
					
						
							| 
									
										
										
										
											2020-11-04 19:23:19 +08:00
										 |  |  |         ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_TOO_MANY_BYTES); | 
					
						
							| 
									
										
										
										
											2019-07-17 14:59:09 +08:00
										 |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     memcpy(&pd->num, num, size); | 
					
						
							|  |  |  |     return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-26 07:28:01 +08:00
										 |  |  | OSSL_PARAM_BLD *OSSL_PARAM_BLD_new(void) | 
					
						
							| 
									
										
										
										
											2019-07-17 14:59:09 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-03-27 08:33:46 +08:00
										 |  |  |     OSSL_PARAM_BLD *r = OPENSSL_zalloc(sizeof(OSSL_PARAM_BLD)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (r != NULL) { | 
					
						
							|  |  |  |         r->params = sk_OSSL_PARAM_BLD_DEF_new_null(); | 
					
						
							|  |  |  |         if (r->params == NULL) { | 
					
						
							|  |  |  |             OPENSSL_free(r); | 
					
						
							|  |  |  |             r = NULL; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return r; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void free_all_params(OSSL_PARAM_BLD *bld) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     int i, n = sk_OSSL_PARAM_BLD_DEF_num(bld->params); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for (i = 0; i < n; i++) | 
					
						
							|  |  |  |         OPENSSL_free(sk_OSSL_PARAM_BLD_DEF_pop(bld->params)); | 
					
						
							| 
									
										
										
										
											2020-03-26 07:28:01 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void OSSL_PARAM_BLD_free(OSSL_PARAM_BLD *bld) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2020-04-24 18:44:15 +08:00
										 |  |  |     if (bld == NULL) | 
					
						
							|  |  |  |         return; | 
					
						
							| 
									
										
										
										
											2020-03-27 08:33:46 +08:00
										 |  |  |     free_all_params(bld); | 
					
						
							|  |  |  |     sk_OSSL_PARAM_BLD_DEF_free(bld->params); | 
					
						
							| 
									
										
										
										
											2020-03-26 07:28:01 +08:00
										 |  |  |     OPENSSL_free(bld); | 
					
						
							| 
									
										
										
										
											2019-07-17 14:59:09 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-24 12:58:57 +08:00
										 |  |  | int OSSL_PARAM_BLD_push_int(OSSL_PARAM_BLD *bld, const char *key, int num) | 
					
						
							| 
									
										
										
										
											2019-07-17 14:59:09 +08:00
										 |  |  | { | 
					
						
							|  |  |  |     return param_push_num(bld, key, &num, sizeof(num), OSSL_PARAM_INTEGER); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-24 12:58:57 +08:00
										 |  |  | int OSSL_PARAM_BLD_push_uint(OSSL_PARAM_BLD *bld, const char *key, | 
					
						
							| 
									
										
										
										
											2019-07-17 14:59:09 +08:00
										 |  |  |                              unsigned int num) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return param_push_num(bld, key, &num, sizeof(num), | 
					
						
							|  |  |  |                           OSSL_PARAM_UNSIGNED_INTEGER); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-24 12:58:57 +08:00
										 |  |  | int OSSL_PARAM_BLD_push_long(OSSL_PARAM_BLD *bld, const char *key, | 
					
						
							| 
									
										
										
										
											2019-07-17 14:59:09 +08:00
										 |  |  |                              long int num) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return param_push_num(bld, key, &num, sizeof(num), OSSL_PARAM_INTEGER); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-24 12:58:57 +08:00
										 |  |  | int OSSL_PARAM_BLD_push_ulong(OSSL_PARAM_BLD *bld, const char *key, | 
					
						
							| 
									
										
										
										
											2019-07-17 14:59:09 +08:00
										 |  |  |                               unsigned long int num) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return param_push_num(bld, key, &num, sizeof(num), | 
					
						
							|  |  |  |                           OSSL_PARAM_UNSIGNED_INTEGER); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-24 12:58:57 +08:00
										 |  |  | int OSSL_PARAM_BLD_push_int32(OSSL_PARAM_BLD *bld, const char *key, | 
					
						
							| 
									
										
										
										
											2019-07-17 14:59:09 +08:00
										 |  |  |                               int32_t num) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return param_push_num(bld, key, &num, sizeof(num), OSSL_PARAM_INTEGER); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-24 12:58:57 +08:00
										 |  |  | int OSSL_PARAM_BLD_push_uint32(OSSL_PARAM_BLD *bld, const char *key, | 
					
						
							| 
									
										
										
										
											2019-07-17 14:59:09 +08:00
										 |  |  |                                uint32_t num) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return param_push_num(bld, key, &num, sizeof(num), | 
					
						
							|  |  |  |                           OSSL_PARAM_UNSIGNED_INTEGER); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-24 12:58:57 +08:00
										 |  |  | int OSSL_PARAM_BLD_push_int64(OSSL_PARAM_BLD *bld, const char *key, | 
					
						
							| 
									
										
										
										
											2019-07-17 14:59:09 +08:00
										 |  |  |                               int64_t num) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return param_push_num(bld, key, &num, sizeof(num), OSSL_PARAM_INTEGER); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-24 12:58:57 +08:00
										 |  |  | int OSSL_PARAM_BLD_push_uint64(OSSL_PARAM_BLD *bld, const char *key, | 
					
						
							| 
									
										
										
										
											2019-07-17 14:59:09 +08:00
										 |  |  |                                uint64_t num) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return param_push_num(bld, key, &num, sizeof(num), | 
					
						
							|  |  |  |                           OSSL_PARAM_UNSIGNED_INTEGER); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-24 12:58:57 +08:00
										 |  |  | int OSSL_PARAM_BLD_push_size_t(OSSL_PARAM_BLD *bld, const char *key, | 
					
						
							| 
									
										
										
										
											2019-07-17 14:59:09 +08:00
										 |  |  |                                size_t num) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return param_push_num(bld, key, &num, sizeof(num), | 
					
						
							|  |  |  |                           OSSL_PARAM_UNSIGNED_INTEGER); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-11 07:37:45 +08:00
										 |  |  | int OSSL_PARAM_BLD_push_time_t(OSSL_PARAM_BLD *bld, const char *key, | 
					
						
							|  |  |  |                                time_t num) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return param_push_num(bld, key, &num, sizeof(num), | 
					
						
							|  |  |  |                           OSSL_PARAM_INTEGER); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-24 12:58:57 +08:00
										 |  |  | int OSSL_PARAM_BLD_push_double(OSSL_PARAM_BLD *bld, const char *key, | 
					
						
							| 
									
										
										
										
											2019-07-17 14:59:09 +08:00
										 |  |  |                                double num) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return param_push_num(bld, key, &num, sizeof(num), OSSL_PARAM_REAL); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-24 12:58:57 +08:00
										 |  |  | int OSSL_PARAM_BLD_push_BN(OSSL_PARAM_BLD *bld, const char *key, | 
					
						
							| 
									
										
										
										
											2019-07-17 14:59:09 +08:00
										 |  |  |                            const BIGNUM *bn) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2020-03-24 12:58:57 +08:00
										 |  |  |     return OSSL_PARAM_BLD_push_BN_pad(bld, key, bn, | 
					
						
							| 
									
										
										
										
											2020-01-14 17:36:39 +08:00
										 |  |  |                                       bn == NULL ? 0 : BN_num_bytes(bn)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-24 12:58:57 +08:00
										 |  |  | int OSSL_PARAM_BLD_push_BN_pad(OSSL_PARAM_BLD *bld, const char *key, | 
					
						
							| 
									
										
										
										
											2020-01-14 17:36:39 +08:00
										 |  |  |                                const BIGNUM *bn, size_t sz) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     int n, secure = 0; | 
					
						
							| 
									
										
										
										
											2019-07-17 14:59:09 +08:00
										 |  |  |     OSSL_PARAM_BLD_DEF *pd; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (bn != NULL) { | 
					
						
							| 
									
										
										
										
											2020-01-14 17:36:39 +08:00
										 |  |  |         n = BN_num_bytes(bn); | 
					
						
							|  |  |  |         if (n < 0) { | 
					
						
							| 
									
										
										
										
											2020-11-04 19:23:19 +08:00
										 |  |  |             ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_ZERO_LENGTH_NUMBER); | 
					
						
							| 
									
										
										
										
											2020-01-14 17:36:39 +08:00
										 |  |  |             return 0; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (sz < (size_t)n) { | 
					
						
							| 
									
										
										
										
											2020-11-04 19:23:19 +08:00
										 |  |  |             ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_TOO_SMALL_BUFFER); | 
					
						
							| 
									
										
										
										
											2019-07-17 14:59:09 +08:00
										 |  |  |             return 0; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (BN_get_flags(bn, BN_FLG_SECURE) == BN_FLG_SECURE) | 
					
						
							|  |  |  |             secure = 1; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-01-14 17:36:39 +08:00
										 |  |  |     pd = param_push(bld, key, sz, sz, OSSL_PARAM_UNSIGNED_INTEGER, secure); | 
					
						
							| 
									
										
										
										
											2019-07-17 14:59:09 +08:00
										 |  |  |     if (pd == NULL) | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     pd->bn = bn; | 
					
						
							|  |  |  |     return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-24 12:58:57 +08:00
										 |  |  | int OSSL_PARAM_BLD_push_utf8_string(OSSL_PARAM_BLD *bld, const char *key, | 
					
						
							| 
									
										
										
										
											2019-08-21 13:28:52 +08:00
										 |  |  |                                     const char *buf, size_t bsize) | 
					
						
							| 
									
										
										
										
											2019-07-17 14:59:09 +08:00
										 |  |  | { | 
					
						
							|  |  |  |     OSSL_PARAM_BLD_DEF *pd; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (bsize == 0) { | 
					
						
							|  |  |  |         bsize = strlen(buf) + 1; | 
					
						
							|  |  |  |     } else if (bsize > INT_MAX) { | 
					
						
							| 
									
										
										
										
											2020-11-04 19:23:19 +08:00
										 |  |  |         ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_STRING_TOO_LONG); | 
					
						
							| 
									
										
										
										
											2019-07-17 14:59:09 +08:00
										 |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     pd = param_push(bld, key, bsize, bsize, OSSL_PARAM_UTF8_STRING, 0); | 
					
						
							|  |  |  |     if (pd == NULL) | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     pd->string = buf; | 
					
						
							|  |  |  |     return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-24 12:58:57 +08:00
										 |  |  | int OSSL_PARAM_BLD_push_utf8_ptr(OSSL_PARAM_BLD *bld, const char *key, | 
					
						
							| 
									
										
										
										
											2019-07-17 14:59:09 +08:00
										 |  |  |                                  char *buf, size_t bsize) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     OSSL_PARAM_BLD_DEF *pd; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (bsize == 0) { | 
					
						
							|  |  |  |         bsize = strlen(buf) + 1; | 
					
						
							|  |  |  |     } else if (bsize > INT_MAX) { | 
					
						
							| 
									
										
										
										
											2020-11-04 19:23:19 +08:00
										 |  |  |         ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_STRING_TOO_LONG); | 
					
						
							| 
									
										
										
										
											2019-07-17 14:59:09 +08:00
										 |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     pd = param_push(bld, key, bsize, sizeof(buf), OSSL_PARAM_UTF8_PTR, 0); | 
					
						
							|  |  |  |     if (pd == NULL) | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     pd->string = buf; | 
					
						
							|  |  |  |     return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-24 12:58:57 +08:00
										 |  |  | int OSSL_PARAM_BLD_push_octet_string(OSSL_PARAM_BLD *bld, const char *key, | 
					
						
							| 
									
										
										
										
											2019-08-21 13:28:52 +08:00
										 |  |  |                                      const void *buf, size_t bsize) | 
					
						
							| 
									
										
										
										
											2019-07-17 14:59:09 +08:00
										 |  |  | { | 
					
						
							|  |  |  |     OSSL_PARAM_BLD_DEF *pd; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (bsize > INT_MAX) { | 
					
						
							| 
									
										
										
										
											2020-11-04 19:23:19 +08:00
										 |  |  |         ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_STRING_TOO_LONG); | 
					
						
							| 
									
										
										
										
											2019-07-17 14:59:09 +08:00
										 |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     pd = param_push(bld, key, bsize, bsize, OSSL_PARAM_OCTET_STRING, 0); | 
					
						
							|  |  |  |     if (pd == NULL) | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     pd->string = buf; | 
					
						
							|  |  |  |     return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-24 12:58:57 +08:00
										 |  |  | int OSSL_PARAM_BLD_push_octet_ptr(OSSL_PARAM_BLD *bld, const char *key, | 
					
						
							| 
									
										
										
										
											2019-07-17 14:59:09 +08:00
										 |  |  |                                   void *buf, size_t bsize) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     OSSL_PARAM_BLD_DEF *pd; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (bsize > INT_MAX) { | 
					
						
							| 
									
										
										
										
											2020-11-04 19:23:19 +08:00
										 |  |  |         ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_STRING_TOO_LONG); | 
					
						
							| 
									
										
										
										
											2019-07-17 14:59:09 +08:00
										 |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     pd = param_push(bld, key, bsize, sizeof(buf), OSSL_PARAM_OCTET_PTR, 0); | 
					
						
							|  |  |  |     if (pd == NULL) | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     pd->string = buf; | 
					
						
							|  |  |  |     return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static OSSL_PARAM *param_bld_convert(OSSL_PARAM_BLD *bld, OSSL_PARAM *param, | 
					
						
							|  |  |  |                                      OSSL_PARAM_BLD_BLOCK *blk, | 
					
						
							|  |  |  |                                      OSSL_PARAM_BLD_BLOCK *secure) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2020-03-27 08:33:46 +08:00
										 |  |  |     int i, num = sk_OSSL_PARAM_BLD_DEF_num(bld->params); | 
					
						
							| 
									
										
										
										
											2019-07-17 14:59:09 +08:00
										 |  |  |     OSSL_PARAM_BLD_DEF *pd; | 
					
						
							|  |  |  |     void *p; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-27 08:33:46 +08:00
										 |  |  |     for (i = 0; i < num; i++) { | 
					
						
							|  |  |  |         pd = sk_OSSL_PARAM_BLD_DEF_value(bld->params, i); | 
					
						
							| 
									
										
										
										
											2019-07-17 14:59:09 +08:00
										 |  |  |         param[i].key = pd->key; | 
					
						
							|  |  |  |         param[i].data_type = pd->type; | 
					
						
							|  |  |  |         param[i].data_size = pd->size; | 
					
						
							| 
									
										
										
										
											2020-04-22 12:10:24 +08:00
										 |  |  |         param[i].return_size = OSSL_PARAM_UNMODIFIED; | 
					
						
							| 
									
										
										
										
											2019-07-17 14:59:09 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (pd->secure) { | 
					
						
							|  |  |  |             p = secure; | 
					
						
							|  |  |  |             secure += pd->alloc_blocks; | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             p = blk; | 
					
						
							|  |  |  |             blk += pd->alloc_blocks; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         param[i].data = p; | 
					
						
							|  |  |  |         if (pd->bn != NULL) { | 
					
						
							|  |  |  |             /* BIGNUM */ | 
					
						
							|  |  |  |             BN_bn2nativepad(pd->bn, (unsigned char *)p, pd->size); | 
					
						
							|  |  |  |         } else if (pd->type == OSSL_PARAM_OCTET_PTR | 
					
						
							|  |  |  |                    || pd->type == OSSL_PARAM_UTF8_PTR) { | 
					
						
							|  |  |  |             /* PTR */ | 
					
						
							| 
									
										
										
										
											2019-08-21 17:08:20 +08:00
										 |  |  |             *(const void **)p = pd->string; | 
					
						
							| 
									
										
										
										
											2019-07-17 14:59:09 +08:00
										 |  |  |         } else if (pd->type == OSSL_PARAM_OCTET_STRING | 
					
						
							|  |  |  |                    || pd->type == OSSL_PARAM_UTF8_STRING) { | 
					
						
							|  |  |  |             if (pd->string != NULL) | 
					
						
							|  |  |  |                 memcpy(p, pd->string, pd->size); | 
					
						
							|  |  |  |             else | 
					
						
							|  |  |  |                 memset(p, 0, pd->size); | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             /* Number, but could also be a NULL BIGNUM */ | 
					
						
							|  |  |  |             if (pd->size > sizeof(pd->num)) | 
					
						
							|  |  |  |                 memset(p, 0, pd->size); | 
					
						
							|  |  |  |             else if (pd->size > 0) | 
					
						
							|  |  |  |                 memcpy(p, &pd->num, pd->size); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     param[i] = OSSL_PARAM_construct_end(); | 
					
						
							| 
									
										
										
										
											2019-07-18 23:14:07 +08:00
										 |  |  |     return param + i; | 
					
						
							| 
									
										
										
										
											2019-07-17 14:59:09 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-24 12:58:57 +08:00
										 |  |  | OSSL_PARAM *OSSL_PARAM_BLD_to_param(OSSL_PARAM_BLD *bld) | 
					
						
							| 
									
										
										
										
											2019-07-17 14:59:09 +08:00
										 |  |  | { | 
					
						
							|  |  |  |     OSSL_PARAM_BLD_BLOCK *blk, *s = NULL; | 
					
						
							| 
									
										
										
										
											2019-07-18 23:14:07 +08:00
										 |  |  |     OSSL_PARAM *params, *last; | 
					
						
							| 
									
										
										
										
											2020-03-27 08:33:46 +08:00
										 |  |  |     const int num = sk_OSSL_PARAM_BLD_DEF_num(bld->params); | 
					
						
							|  |  |  |     const size_t p_blks = bytes_to_blocks((1 + num) * sizeof(*params)); | 
					
						
							| 
									
										
										
										
											2019-07-17 14:59:09 +08:00
										 |  |  |     const size_t total = ALIGN_SIZE * (p_blks + bld->total_blocks); | 
					
						
							| 
									
										
										
										
											2019-07-18 23:14:07 +08:00
										 |  |  |     const size_t ss = ALIGN_SIZE * bld->secure_blocks; | 
					
						
							| 
									
										
										
										
											2019-07-17 14:59:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-18 23:14:07 +08:00
										 |  |  |     if (ss > 0) { | 
					
						
							|  |  |  |         s = OPENSSL_secure_malloc(ss); | 
					
						
							| 
									
										
										
										
											2019-07-17 14:59:09 +08:00
										 |  |  |         if (s == NULL) { | 
					
						
							| 
									
										
										
										
											2020-11-04 19:23:19 +08:00
										 |  |  |             ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_SECURE_MALLOC_FAILURE); | 
					
						
							| 
									
										
										
										
											2019-07-17 14:59:09 +08:00
										 |  |  |             return NULL; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-07-18 23:14:07 +08:00
										 |  |  |     params = OPENSSL_malloc(total); | 
					
						
							|  |  |  |     if (params == NULL) { | 
					
						
							| 
									
										
										
										
											2020-11-04 19:23:19 +08:00
										 |  |  |         ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE); | 
					
						
							| 
									
										
										
										
											2019-07-17 14:59:09 +08:00
										 |  |  |         OPENSSL_secure_free(s); | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-07-18 23:14:07 +08:00
										 |  |  |     blk = p_blks + (OSSL_PARAM_BLD_BLOCK *)(params); | 
					
						
							|  |  |  |     last = param_bld_convert(bld, params, blk, s); | 
					
						
							|  |  |  |     last->data_size = ss; | 
					
						
							|  |  |  |     last->data = s; | 
					
						
							|  |  |  |     last->data_type = OSSL_PARAM_ALLOCATED_END; | 
					
						
							| 
									
										
										
										
											2020-03-26 07:28:01 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-27 08:33:46 +08:00
										 |  |  |     /* Reset builder for reuse */ | 
					
						
							|  |  |  |     bld->total_blocks = 0; | 
					
						
							|  |  |  |     bld->secure_blocks = 0; | 
					
						
							|  |  |  |     free_all_params(bld); | 
					
						
							| 
									
										
										
										
											2019-07-18 23:14:07 +08:00
										 |  |  |     return params; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-26 07:28:01 +08:00
										 |  |  | void OSSL_PARAM_BLD_free_params(OSSL_PARAM *params) | 
					
						
							| 
									
										
										
										
											2019-07-18 23:14:07 +08:00
										 |  |  | { | 
					
						
							|  |  |  |     if (params != NULL) { | 
					
						
							|  |  |  |         OSSL_PARAM *p; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         for (p = params; p->key != NULL; p++) | 
					
						
							|  |  |  |             ; | 
					
						
							|  |  |  |         if (p->data_type == OSSL_PARAM_ALLOCATED_END) | 
					
						
							|  |  |  |             OPENSSL_secure_clear_free(p->data, p->data_size); | 
					
						
							|  |  |  |         OPENSSL_free(params); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-07-17 14:59:09 +08:00
										 |  |  | } |