params_dup.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*
  2. * Copyright 2021 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. #include "e_os.h" /* strcasecmp */
  14. #define OSSL_PARAM_ALLOCATED_END 127
  15. #define OSSL_PARAM_MERGE_LIST_MAX 128
  16. #define OSSL_PARAM_BUF_PUBLIC 0
  17. #define OSSL_PARAM_BUF_SECURE 1
  18. #define OSSL_PARAM_BUF_MAX (OSSL_PARAM_BUF_SECURE + 1)
  19. typedef struct {
  20. OSSL_PARAM_ALIGNED_BLOCK *alloc; /* The allocated buffer */
  21. OSSL_PARAM_ALIGNED_BLOCK *cur; /* Current position in the allocated buf */
  22. size_t blocks; /* Number of aligned blocks */
  23. size_t alloc_sz; /* The size of the allocated buffer (in bytes) */
  24. } OSSL_PARAM_BUF;
  25. size_t ossl_param_bytes_to_blocks(size_t bytes)
  26. {
  27. return (bytes + OSSL_PARAM_ALIGN_SIZE - 1) / OSSL_PARAM_ALIGN_SIZE;
  28. }
  29. static int ossl_param_buf_alloc(OSSL_PARAM_BUF *out, size_t extra_blocks,
  30. int is_secure)
  31. {
  32. size_t sz = OSSL_PARAM_ALIGN_SIZE * (extra_blocks + out->blocks);
  33. out->alloc = is_secure ? OPENSSL_secure_zalloc(sz) : OPENSSL_zalloc(sz);
  34. if (out->alloc == NULL) {
  35. ERR_raise(ERR_LIB_CRYPTO, is_secure ? CRYPTO_R_SECURE_MALLOC_FAILURE
  36. : ERR_R_MALLOC_FAILURE);
  37. return 0;
  38. }
  39. out->alloc_sz = sz;
  40. out->cur = out->alloc + extra_blocks;
  41. return 1;
  42. }
  43. void ossl_param_set_secure_block(OSSL_PARAM *last, void *secure_buffer,
  44. size_t secure_buffer_sz)
  45. {
  46. last->key = NULL;
  47. last->data_size = secure_buffer_sz;
  48. last->data = secure_buffer;
  49. last->data_type = OSSL_PARAM_ALLOCATED_END;
  50. }
  51. static OSSL_PARAM *ossl_param_dup(const OSSL_PARAM *src, OSSL_PARAM *dst,
  52. OSSL_PARAM_BUF buf[OSSL_PARAM_BUF_MAX],
  53. int *param_count)
  54. {
  55. const OSSL_PARAM *in;
  56. int has_dst = (dst != NULL);
  57. int is_secure;
  58. size_t param_sz, blks;
  59. for (in = src; in->key != NULL; in++) {
  60. is_secure = CRYPTO_secure_allocated(in->data);
  61. if (has_dst) {
  62. *dst = *in;
  63. dst->data = buf[is_secure].cur;
  64. }
  65. if (in->data_type == OSSL_PARAM_OCTET_PTR
  66. || in->data_type == OSSL_PARAM_UTF8_PTR) {
  67. param_sz = sizeof(in->data);
  68. if (has_dst)
  69. *((const void **)dst->data) = *(const void **)in->data;
  70. } else {
  71. param_sz = in->data_size;
  72. if (has_dst)
  73. memcpy(dst->data, in->data, param_sz);
  74. }
  75. if (in->data_type == OSSL_PARAM_UTF8_STRING)
  76. param_sz++; /* NULL terminator */
  77. blks = ossl_param_bytes_to_blocks(param_sz);
  78. if (has_dst) {
  79. dst++;
  80. buf[is_secure].cur += blks;
  81. } else {
  82. buf[is_secure].blocks += blks;
  83. }
  84. if (param_count != NULL)
  85. ++*param_count;
  86. }
  87. return dst;
  88. }
  89. OSSL_PARAM *OSSL_PARAM_dup(const OSSL_PARAM *src)
  90. {
  91. size_t param_blocks;
  92. OSSL_PARAM_BUF buf[OSSL_PARAM_BUF_MAX];
  93. OSSL_PARAM *last, *dst;
  94. int param_count = 1; /* Include terminator in the count */
  95. if (src == NULL)
  96. return NULL;
  97. memset(buf, 0, sizeof(buf));
  98. /* First Pass: get the param_count and block sizes required */
  99. (void)ossl_param_dup(src, NULL, buf, &param_count);
  100. param_blocks = ossl_param_bytes_to_blocks(param_count * sizeof(*src));
  101. /*
  102. * The allocated buffer consists of an array of OSSL_PARAM followed by
  103. * aligned data bytes that the array elements will point to.
  104. */
  105. if (!ossl_param_buf_alloc(&buf[OSSL_PARAM_BUF_PUBLIC], param_blocks, 0))
  106. return NULL;
  107. /* Allocate a secure memory buffer if required */
  108. if (buf[OSSL_PARAM_BUF_SECURE].blocks > 0
  109. && !ossl_param_buf_alloc(&buf[OSSL_PARAM_BUF_SECURE], 0, 1)) {
  110. OPENSSL_free(buf[OSSL_PARAM_BUF_PUBLIC].alloc);
  111. return NULL;
  112. }
  113. dst = (OSSL_PARAM *)buf[OSSL_PARAM_BUF_PUBLIC].alloc;
  114. last = ossl_param_dup(src, dst, buf, NULL);
  115. /* Store the allocated secure memory buffer in the last param block */
  116. ossl_param_set_secure_block(last, buf[OSSL_PARAM_BUF_SECURE].alloc,
  117. buf[OSSL_PARAM_BUF_SECURE].alloc_sz);
  118. return dst;
  119. }
  120. static int compare_params(const void *left, const void *right)
  121. {
  122. const OSSL_PARAM *l = *(const OSSL_PARAM **)left;
  123. const OSSL_PARAM *r = *(const OSSL_PARAM **)right;
  124. return strcasecmp(l->key, r->key);
  125. }
  126. OSSL_PARAM *OSSL_PARAM_merge(const OSSL_PARAM *p1, const OSSL_PARAM *p2)
  127. {
  128. const OSSL_PARAM *list1[OSSL_PARAM_MERGE_LIST_MAX + 1];
  129. const OSSL_PARAM *list2[OSSL_PARAM_MERGE_LIST_MAX + 1];
  130. const OSSL_PARAM *p = NULL;
  131. const OSSL_PARAM **p1cur, **p2cur;
  132. OSSL_PARAM *params, *dst;
  133. size_t list1_sz = 0, list2_sz = 0;
  134. int diff;
  135. if (p1 == NULL && p2 == NULL)
  136. return NULL;
  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. return NULL;
  151. /* Sort the 2 lists */
  152. qsort(list1, list1_sz, sizeof(OSSL_PARAM *), compare_params);
  153. qsort(list2, list2_sz, sizeof(OSSL_PARAM *), compare_params);
  154. /* Allocate enough space to store the merged parameters */
  155. params = OPENSSL_zalloc((list1_sz + list2_sz + 1) * sizeof(*p1));
  156. if (params == NULL) {
  157. ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
  158. return NULL;
  159. }
  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 = 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. }