params_dup.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. * Copyright 2021-2022 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <string.h>
  10. #include <openssl/params.h>
  11. #include <openssl/param_build.h>
  12. #include "internal/param_build_set.h"
  13. #define OSSL_PARAM_ALLOCATED_END 127
  14. #define OSSL_PARAM_MERGE_LIST_MAX 128
  15. #define OSSL_PARAM_BUF_PUBLIC 0
  16. #define OSSL_PARAM_BUF_SECURE 1
  17. #define OSSL_PARAM_BUF_MAX (OSSL_PARAM_BUF_SECURE + 1)
  18. typedef struct {
  19. OSSL_PARAM_ALIGNED_BLOCK *alloc; /* The allocated buffer */
  20. OSSL_PARAM_ALIGNED_BLOCK *cur; /* Current position in the allocated buf */
  21. size_t blocks; /* Number of aligned blocks */
  22. size_t alloc_sz; /* The size of the allocated buffer (in bytes) */
  23. } OSSL_PARAM_BUF;
  24. size_t ossl_param_bytes_to_blocks(size_t bytes)
  25. {
  26. return (bytes + OSSL_PARAM_ALIGN_SIZE - 1) / OSSL_PARAM_ALIGN_SIZE;
  27. }
  28. static int ossl_param_buf_alloc(OSSL_PARAM_BUF *out, size_t extra_blocks,
  29. int is_secure)
  30. {
  31. size_t sz = OSSL_PARAM_ALIGN_SIZE * (extra_blocks + out->blocks);
  32. out->alloc = is_secure ? OPENSSL_secure_zalloc(sz) : OPENSSL_zalloc(sz);
  33. if (out->alloc == NULL) {
  34. ERR_raise(ERR_LIB_CRYPTO, is_secure ? CRYPTO_R_SECURE_MALLOC_FAILURE
  35. : ERR_R_MALLOC_FAILURE);
  36. return 0;
  37. }
  38. out->alloc_sz = sz;
  39. out->cur = out->alloc + extra_blocks;
  40. return 1;
  41. }
  42. void ossl_param_set_secure_block(OSSL_PARAM *last, void *secure_buffer,
  43. size_t secure_buffer_sz)
  44. {
  45. last->key = NULL;
  46. last->data_size = secure_buffer_sz;
  47. last->data = secure_buffer;
  48. last->data_type = OSSL_PARAM_ALLOCATED_END;
  49. }
  50. static OSSL_PARAM *ossl_param_dup(const OSSL_PARAM *src, OSSL_PARAM *dst,
  51. OSSL_PARAM_BUF buf[OSSL_PARAM_BUF_MAX],
  52. int *param_count)
  53. {
  54. const OSSL_PARAM *in;
  55. int has_dst = (dst != NULL);
  56. int is_secure;
  57. size_t param_sz, blks;
  58. for (in = src; in->key != NULL; in++) {
  59. is_secure = CRYPTO_secure_allocated(in->data);
  60. if (has_dst) {
  61. *dst = *in;
  62. dst->data = buf[is_secure].cur;
  63. }
  64. if (in->data_type == OSSL_PARAM_OCTET_PTR
  65. || in->data_type == OSSL_PARAM_UTF8_PTR) {
  66. param_sz = sizeof(in->data);
  67. if (has_dst)
  68. *((const void **)dst->data) = *(const void **)in->data;
  69. } else {
  70. param_sz = in->data_size;
  71. if (has_dst)
  72. memcpy(dst->data, in->data, param_sz);
  73. }
  74. if (in->data_type == OSSL_PARAM_UTF8_STRING)
  75. param_sz++; /* NULL terminator */
  76. blks = ossl_param_bytes_to_blocks(param_sz);
  77. if (has_dst) {
  78. dst++;
  79. buf[is_secure].cur += blks;
  80. } else {
  81. buf[is_secure].blocks += blks;
  82. }
  83. if (param_count != NULL)
  84. ++*param_count;
  85. }
  86. return dst;
  87. }
  88. OSSL_PARAM *OSSL_PARAM_dup(const OSSL_PARAM *src)
  89. {
  90. size_t param_blocks;
  91. OSSL_PARAM_BUF buf[OSSL_PARAM_BUF_MAX];
  92. OSSL_PARAM *last, *dst;
  93. int param_count = 1; /* Include terminator in the count */
  94. if (src == NULL) {
  95. ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER);
  96. return NULL;
  97. }
  98. memset(buf, 0, sizeof(buf));
  99. /* First Pass: get the param_count and block sizes required */
  100. (void)ossl_param_dup(src, NULL, buf, &param_count);
  101. param_blocks = ossl_param_bytes_to_blocks(param_count * sizeof(*src));
  102. /*
  103. * The allocated buffer consists of an array of OSSL_PARAM followed by
  104. * aligned data bytes that the array elements will point to.
  105. */
  106. if (!ossl_param_buf_alloc(&buf[OSSL_PARAM_BUF_PUBLIC], param_blocks, 0))
  107. return NULL;
  108. /* Allocate a secure memory buffer if required */
  109. if (buf[OSSL_PARAM_BUF_SECURE].blocks > 0
  110. && !ossl_param_buf_alloc(&buf[OSSL_PARAM_BUF_SECURE], 0, 1)) {
  111. OPENSSL_free(buf[OSSL_PARAM_BUF_PUBLIC].alloc);
  112. return NULL;
  113. }
  114. dst = (OSSL_PARAM *)buf[OSSL_PARAM_BUF_PUBLIC].alloc;
  115. last = ossl_param_dup(src, dst, buf, NULL);
  116. /* Store the allocated secure memory buffer in the last param block */
  117. ossl_param_set_secure_block(last, buf[OSSL_PARAM_BUF_SECURE].alloc,
  118. buf[OSSL_PARAM_BUF_SECURE].alloc_sz);
  119. return dst;
  120. }
  121. static int compare_params(const void *left, const void *right)
  122. {
  123. const OSSL_PARAM *l = *(const OSSL_PARAM **)left;
  124. const OSSL_PARAM *r = *(const OSSL_PARAM **)right;
  125. return OPENSSL_strcasecmp(l->key, r->key);
  126. }
  127. OSSL_PARAM *OSSL_PARAM_merge(const OSSL_PARAM *p1, const OSSL_PARAM *p2)
  128. {
  129. const OSSL_PARAM *list1[OSSL_PARAM_MERGE_LIST_MAX + 1];
  130. const OSSL_PARAM *list2[OSSL_PARAM_MERGE_LIST_MAX + 1];
  131. const OSSL_PARAM *p = NULL;
  132. const OSSL_PARAM **p1cur, **p2cur;
  133. OSSL_PARAM *params, *dst;
  134. size_t list1_sz = 0, list2_sz = 0;
  135. int diff;
  136. if (p1 == NULL && p2 == NULL) {
  137. ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER);
  138. return NULL;
  139. }
  140. /* Copy p1 to list1 */
  141. if (p1 != NULL) {
  142. for (p = p1; p->key != NULL && list1_sz < OSSL_PARAM_MERGE_LIST_MAX; p++)
  143. list1[list1_sz++] = p;
  144. }
  145. list1[list1_sz] = NULL;
  146. /* copy p2 to a list2 */
  147. if (p2 != NULL) {
  148. for (p = p2; p->key != NULL && list2_sz < OSSL_PARAM_MERGE_LIST_MAX; p++)
  149. list2[list2_sz++] = p;
  150. }
  151. list2[list2_sz] = NULL;
  152. if (list1_sz == 0 && list2_sz == 0) {
  153. ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_NO_PARAMS_TO_MERGE);
  154. return NULL;
  155. }
  156. /* Sort the 2 lists */
  157. qsort(list1, list1_sz, sizeof(OSSL_PARAM *), compare_params);
  158. qsort(list2, list2_sz, sizeof(OSSL_PARAM *), compare_params);
  159. /* Allocate enough space to store the merged parameters */
  160. params = OPENSSL_zalloc((list1_sz + list2_sz + 1) * sizeof(*p1));
  161. if (params == NULL) {
  162. ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
  163. return NULL;
  164. }
  165. dst = params;
  166. p1cur = list1;
  167. p2cur = list2;
  168. while (1) {
  169. /* If list1 is finished just tack list2 onto the end */
  170. if (*p1cur == NULL) {
  171. do {
  172. *dst++ = **p2cur;
  173. p2cur++;
  174. } while (*p2cur != NULL);
  175. break;
  176. }
  177. /* If list2 is finished just tack list1 onto the end */
  178. if (*p2cur == NULL) {
  179. do {
  180. *dst++ = **p1cur;
  181. p1cur++;
  182. } while (*p1cur != NULL);
  183. break;
  184. }
  185. /* consume the list element with the smaller key */
  186. diff = OPENSSL_strcasecmp((*p1cur)->key, (*p2cur)->key);
  187. if (diff == 0) {
  188. /* If the keys are the same then throw away the list1 element */
  189. *dst++ = **p2cur;
  190. p2cur++;
  191. p1cur++;
  192. } else if (diff > 0) {
  193. *dst++ = **p2cur;
  194. p2cur++;
  195. } else {
  196. *dst++ = **p1cur;
  197. p1cur++;
  198. }
  199. }
  200. return params;
  201. }
  202. void OSSL_PARAM_free(OSSL_PARAM *params)
  203. {
  204. if (params != NULL) {
  205. OSSL_PARAM *p;
  206. for (p = params; p->key != NULL; p++)
  207. ;
  208. if (p->data_type == OSSL_PARAM_ALLOCATED_END)
  209. OPENSSL_secure_clear_free(p->data, p->data_size);
  210. OPENSSL_free(params);
  211. }
  212. }