params_dup.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. return 0;
  35. out->alloc_sz = sz;
  36. out->cur = out->alloc + extra_blocks;
  37. return 1;
  38. }
  39. void ossl_param_set_secure_block(OSSL_PARAM *last, void *secure_buffer,
  40. size_t secure_buffer_sz)
  41. {
  42. last->key = NULL;
  43. last->data_size = secure_buffer_sz;
  44. last->data = secure_buffer;
  45. last->data_type = OSSL_PARAM_ALLOCATED_END;
  46. }
  47. static OSSL_PARAM *ossl_param_dup(const OSSL_PARAM *src, OSSL_PARAM *dst,
  48. OSSL_PARAM_BUF buf[OSSL_PARAM_BUF_MAX],
  49. int *param_count)
  50. {
  51. const OSSL_PARAM *in;
  52. int has_dst = (dst != NULL);
  53. int is_secure;
  54. size_t param_sz, blks;
  55. for (in = src; in->key != NULL; in++) {
  56. is_secure = CRYPTO_secure_allocated(in->data);
  57. if (has_dst) {
  58. *dst = *in;
  59. dst->data = buf[is_secure].cur;
  60. }
  61. if (in->data_type == OSSL_PARAM_OCTET_PTR
  62. || in->data_type == OSSL_PARAM_UTF8_PTR) {
  63. param_sz = sizeof(in->data);
  64. if (has_dst)
  65. *((const void **)dst->data) = *(const void **)in->data;
  66. } else {
  67. param_sz = in->data_size;
  68. if (has_dst)
  69. memcpy(dst->data, in->data, param_sz);
  70. }
  71. if (in->data_type == OSSL_PARAM_UTF8_STRING)
  72. param_sz++; /* NULL terminator */
  73. blks = ossl_param_bytes_to_blocks(param_sz);
  74. if (has_dst) {
  75. dst++;
  76. buf[is_secure].cur += blks;
  77. } else {
  78. buf[is_secure].blocks += blks;
  79. }
  80. if (param_count != NULL)
  81. ++*param_count;
  82. }
  83. return dst;
  84. }
  85. OSSL_PARAM *OSSL_PARAM_dup(const OSSL_PARAM *src)
  86. {
  87. size_t param_blocks;
  88. OSSL_PARAM_BUF buf[OSSL_PARAM_BUF_MAX];
  89. OSSL_PARAM *last, *dst;
  90. int param_count = 1; /* Include terminator in the count */
  91. if (src == NULL) {
  92. ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER);
  93. return NULL;
  94. }
  95. memset(buf, 0, sizeof(buf));
  96. /* First Pass: get the param_count and block sizes required */
  97. (void)ossl_param_dup(src, NULL, buf, &param_count);
  98. param_blocks = ossl_param_bytes_to_blocks(param_count * sizeof(*src));
  99. /*
  100. * The allocated buffer consists of an array of OSSL_PARAM followed by
  101. * aligned data bytes that the array elements will point to.
  102. */
  103. if (!ossl_param_buf_alloc(&buf[OSSL_PARAM_BUF_PUBLIC], param_blocks, 0))
  104. return NULL;
  105. /* Allocate a secure memory buffer if required */
  106. if (buf[OSSL_PARAM_BUF_SECURE].blocks > 0
  107. && !ossl_param_buf_alloc(&buf[OSSL_PARAM_BUF_SECURE], 0, 1)) {
  108. OPENSSL_free(buf[OSSL_PARAM_BUF_PUBLIC].alloc);
  109. return NULL;
  110. }
  111. dst = (OSSL_PARAM *)buf[OSSL_PARAM_BUF_PUBLIC].alloc;
  112. last = ossl_param_dup(src, dst, buf, NULL);
  113. /* Store the allocated secure memory buffer in the last param block */
  114. ossl_param_set_secure_block(last, buf[OSSL_PARAM_BUF_SECURE].alloc,
  115. buf[OSSL_PARAM_BUF_SECURE].alloc_sz);
  116. return dst;
  117. }
  118. static int compare_params(const void *left, const void *right)
  119. {
  120. const OSSL_PARAM *l = *(const OSSL_PARAM **)left;
  121. const OSSL_PARAM *r = *(const OSSL_PARAM **)right;
  122. return OPENSSL_strcasecmp(l->key, r->key);
  123. }
  124. OSSL_PARAM *OSSL_PARAM_merge(const OSSL_PARAM *p1, const OSSL_PARAM *p2)
  125. {
  126. const OSSL_PARAM *list1[OSSL_PARAM_MERGE_LIST_MAX + 1];
  127. const OSSL_PARAM *list2[OSSL_PARAM_MERGE_LIST_MAX + 1];
  128. const OSSL_PARAM *p = NULL;
  129. const OSSL_PARAM **p1cur, **p2cur;
  130. OSSL_PARAM *params, *dst;
  131. size_t list1_sz = 0, list2_sz = 0;
  132. int diff;
  133. if (p1 == NULL && p2 == NULL) {
  134. ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER);
  135. return NULL;
  136. }
  137. /* Copy p1 to list1 */
  138. if (p1 != NULL) {
  139. for (p = p1; p->key != NULL && list1_sz < OSSL_PARAM_MERGE_LIST_MAX; p++)
  140. list1[list1_sz++] = p;
  141. }
  142. list1[list1_sz] = NULL;
  143. /* copy p2 to a list2 */
  144. if (p2 != NULL) {
  145. for (p = p2; p->key != NULL && list2_sz < OSSL_PARAM_MERGE_LIST_MAX; p++)
  146. list2[list2_sz++] = p;
  147. }
  148. list2[list2_sz] = NULL;
  149. if (list1_sz == 0 && list2_sz == 0) {
  150. ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_NO_PARAMS_TO_MERGE);
  151. return NULL;
  152. }
  153. /* Sort the 2 lists */
  154. qsort(list1, list1_sz, sizeof(OSSL_PARAM *), compare_params);
  155. qsort(list2, list2_sz, sizeof(OSSL_PARAM *), compare_params);
  156. /* Allocate enough space to store the merged parameters */
  157. params = OPENSSL_zalloc((list1_sz + list2_sz + 1) * sizeof(*p1));
  158. if (params == NULL)
  159. return NULL;
  160. dst = params;
  161. p1cur = list1;
  162. p2cur = list2;
  163. while (1) {
  164. /* If list1 is finished just tack list2 onto the end */
  165. if (*p1cur == NULL) {
  166. do {
  167. *dst++ = **p2cur;
  168. p2cur++;
  169. } while (*p2cur != NULL);
  170. break;
  171. }
  172. /* If list2 is finished just tack list1 onto the end */
  173. if (*p2cur == NULL) {
  174. do {
  175. *dst++ = **p1cur;
  176. p1cur++;
  177. } while (*p1cur != NULL);
  178. break;
  179. }
  180. /* consume the list element with the smaller key */
  181. diff = OPENSSL_strcasecmp((*p1cur)->key, (*p2cur)->key);
  182. if (diff == 0) {
  183. /* If the keys are the same then throw away the list1 element */
  184. *dst++ = **p2cur;
  185. p2cur++;
  186. p1cur++;
  187. } else if (diff > 0) {
  188. *dst++ = **p2cur;
  189. p2cur++;
  190. } else {
  191. *dst++ = **p1cur;
  192. p1cur++;
  193. }
  194. }
  195. return params;
  196. }
  197. void OSSL_PARAM_free(OSSL_PARAM *params)
  198. {
  199. if (params != NULL) {
  200. OSSL_PARAM *p;
  201. for (p = params; p->key != NULL; p++)
  202. ;
  203. if (p->data_type == OSSL_PARAM_ALLOCATED_END)
  204. OPENSSL_secure_clear_free(p->data, p->data_size);
  205. OPENSSL_free(params);
  206. }
  207. }