param_build.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. /*
  2. * Copyright 2019-2022 The OpenSSL Project Authors. All Rights Reserved.
  3. * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
  4. *
  5. * Licensed under the Apache License 2.0 (the "License"). You may not use
  6. * this file except in compliance with the License. You can obtain a copy
  7. * in the file LICENSE in the source distribution or at
  8. * https://www.openssl.org/source/license.html
  9. */
  10. #include <string.h>
  11. #include <openssl/err.h>
  12. #include <openssl/cryptoerr.h>
  13. #include <openssl/params.h>
  14. #include <openssl/types.h>
  15. #include <openssl/safestack.h>
  16. #include "internal/param_build_set.h"
  17. /*
  18. * Special internal param type to indicate the end of an allocate OSSL_PARAM
  19. * array.
  20. */
  21. typedef struct {
  22. const char *key;
  23. int type;
  24. int secure;
  25. size_t size;
  26. size_t alloc_blocks;
  27. const BIGNUM *bn;
  28. const void *string;
  29. union {
  30. /*
  31. * These fields are never directly addressed, but their sizes are
  32. * imporant so that all native types can be copied here without overrun.
  33. */
  34. ossl_intmax_t i;
  35. ossl_uintmax_t u;
  36. double d;
  37. } num;
  38. } OSSL_PARAM_BLD_DEF;
  39. DEFINE_STACK_OF(OSSL_PARAM_BLD_DEF)
  40. struct ossl_param_bld_st {
  41. size_t total_blocks;
  42. size_t secure_blocks;
  43. STACK_OF(OSSL_PARAM_BLD_DEF) *params;
  44. };
  45. static OSSL_PARAM_BLD_DEF *param_push(OSSL_PARAM_BLD *bld, const char *key,
  46. int size, size_t alloc, int type,
  47. int secure)
  48. {
  49. OSSL_PARAM_BLD_DEF *pd = OPENSSL_zalloc(sizeof(*pd));
  50. if (pd == NULL) {
  51. ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
  52. return NULL;
  53. }
  54. pd->key = key;
  55. pd->type = type;
  56. pd->size = size;
  57. pd->alloc_blocks = ossl_param_bytes_to_blocks(alloc);
  58. if ((pd->secure = secure) != 0)
  59. bld->secure_blocks += pd->alloc_blocks;
  60. else
  61. bld->total_blocks += pd->alloc_blocks;
  62. if (sk_OSSL_PARAM_BLD_DEF_push(bld->params, pd) <= 0) {
  63. OPENSSL_free(pd);
  64. pd = NULL;
  65. }
  66. return pd;
  67. }
  68. static int param_push_num(OSSL_PARAM_BLD *bld, const char *key,
  69. void *num, size_t size, int type)
  70. {
  71. OSSL_PARAM_BLD_DEF *pd = param_push(bld, key, size, size, type, 0);
  72. if (pd == NULL) {
  73. ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER);
  74. return 0;
  75. }
  76. if (size > sizeof(pd->num)) {
  77. ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_TOO_MANY_BYTES);
  78. return 0;
  79. }
  80. memcpy(&pd->num, num, size);
  81. return 1;
  82. }
  83. OSSL_PARAM_BLD *OSSL_PARAM_BLD_new(void)
  84. {
  85. OSSL_PARAM_BLD *r = OPENSSL_zalloc(sizeof(OSSL_PARAM_BLD));
  86. if (r != NULL) {
  87. r->params = sk_OSSL_PARAM_BLD_DEF_new_null();
  88. if (r->params == NULL) {
  89. OPENSSL_free(r);
  90. r = NULL;
  91. }
  92. }
  93. return r;
  94. }
  95. static void free_all_params(OSSL_PARAM_BLD *bld)
  96. {
  97. int i, n = sk_OSSL_PARAM_BLD_DEF_num(bld->params);
  98. for (i = 0; i < n; i++)
  99. OPENSSL_free(sk_OSSL_PARAM_BLD_DEF_pop(bld->params));
  100. }
  101. void OSSL_PARAM_BLD_free(OSSL_PARAM_BLD *bld)
  102. {
  103. if (bld == NULL)
  104. return;
  105. free_all_params(bld);
  106. sk_OSSL_PARAM_BLD_DEF_free(bld->params);
  107. OPENSSL_free(bld);
  108. }
  109. int OSSL_PARAM_BLD_push_int(OSSL_PARAM_BLD *bld, const char *key, int num)
  110. {
  111. return param_push_num(bld, key, &num, sizeof(num), OSSL_PARAM_INTEGER);
  112. }
  113. int OSSL_PARAM_BLD_push_uint(OSSL_PARAM_BLD *bld, const char *key,
  114. unsigned int num)
  115. {
  116. return param_push_num(bld, key, &num, sizeof(num),
  117. OSSL_PARAM_UNSIGNED_INTEGER);
  118. }
  119. int OSSL_PARAM_BLD_push_long(OSSL_PARAM_BLD *bld, const char *key,
  120. long int num)
  121. {
  122. return param_push_num(bld, key, &num, sizeof(num), OSSL_PARAM_INTEGER);
  123. }
  124. int OSSL_PARAM_BLD_push_ulong(OSSL_PARAM_BLD *bld, const char *key,
  125. unsigned long int num)
  126. {
  127. return param_push_num(bld, key, &num, sizeof(num),
  128. OSSL_PARAM_UNSIGNED_INTEGER);
  129. }
  130. int OSSL_PARAM_BLD_push_int32(OSSL_PARAM_BLD *bld, const char *key,
  131. int32_t num)
  132. {
  133. return param_push_num(bld, key, &num, sizeof(num), OSSL_PARAM_INTEGER);
  134. }
  135. int OSSL_PARAM_BLD_push_uint32(OSSL_PARAM_BLD *bld, const char *key,
  136. uint32_t num)
  137. {
  138. return param_push_num(bld, key, &num, sizeof(num),
  139. OSSL_PARAM_UNSIGNED_INTEGER);
  140. }
  141. int OSSL_PARAM_BLD_push_int64(OSSL_PARAM_BLD *bld, const char *key,
  142. int64_t num)
  143. {
  144. return param_push_num(bld, key, &num, sizeof(num), OSSL_PARAM_INTEGER);
  145. }
  146. int OSSL_PARAM_BLD_push_uint64(OSSL_PARAM_BLD *bld, const char *key,
  147. uint64_t num)
  148. {
  149. return param_push_num(bld, key, &num, sizeof(num),
  150. OSSL_PARAM_UNSIGNED_INTEGER);
  151. }
  152. int OSSL_PARAM_BLD_push_size_t(OSSL_PARAM_BLD *bld, const char *key,
  153. size_t num)
  154. {
  155. return param_push_num(bld, key, &num, sizeof(num),
  156. OSSL_PARAM_UNSIGNED_INTEGER);
  157. }
  158. int OSSL_PARAM_BLD_push_time_t(OSSL_PARAM_BLD *bld, const char *key,
  159. time_t num)
  160. {
  161. return param_push_num(bld, key, &num, sizeof(num),
  162. OSSL_PARAM_INTEGER);
  163. }
  164. int OSSL_PARAM_BLD_push_double(OSSL_PARAM_BLD *bld, const char *key,
  165. double num)
  166. {
  167. return param_push_num(bld, key, &num, sizeof(num), OSSL_PARAM_REAL);
  168. }
  169. static int push_BN(OSSL_PARAM_BLD *bld, const char *key,
  170. const BIGNUM *bn, size_t sz, int type)
  171. {
  172. int n, secure = 0;
  173. OSSL_PARAM_BLD_DEF *pd;
  174. if (!ossl_assert(type == OSSL_PARAM_UNSIGNED_INTEGER
  175. || type == OSSL_PARAM_INTEGER))
  176. return 0;
  177. if (bn != NULL) {
  178. if (type == OSSL_PARAM_UNSIGNED_INTEGER && BN_is_negative(bn)) {
  179. ERR_raise_data(ERR_LIB_CRYPTO, ERR_R_UNSUPPORTED,
  180. "Negative big numbers are unsupported for OSSL_PARAM_UNSIGNED_INTEGER");
  181. return 0;
  182. }
  183. n = BN_num_bytes(bn);
  184. if (n < 0) {
  185. ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_ZERO_LENGTH_NUMBER);
  186. return 0;
  187. }
  188. if (sz < (size_t)n) {
  189. ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_TOO_SMALL_BUFFER);
  190. return 0;
  191. }
  192. if (BN_get_flags(bn, BN_FLG_SECURE) == BN_FLG_SECURE)
  193. secure = 1;
  194. }
  195. pd = param_push(bld, key, sz, sz, type, secure);
  196. if (pd == NULL)
  197. return 0;
  198. pd->bn = bn;
  199. return 1;
  200. }
  201. int OSSL_PARAM_BLD_push_BN(OSSL_PARAM_BLD *bld, const char *key,
  202. const BIGNUM *bn)
  203. {
  204. if (BN_is_negative(bn))
  205. return push_BN(bld, key, bn, bn == NULL ? 0 : BN_num_bytes(bn) + 1,
  206. OSSL_PARAM_INTEGER);
  207. return push_BN(bld, key, bn, bn == NULL ? 0 : BN_num_bytes(bn),
  208. OSSL_PARAM_UNSIGNED_INTEGER);
  209. }
  210. int OSSL_PARAM_BLD_push_BN_pad(OSSL_PARAM_BLD *bld, const char *key,
  211. const BIGNUM *bn, size_t sz)
  212. {
  213. if (BN_is_negative(bn))
  214. return push_BN(bld, key, bn, bn == NULL ? 0 : BN_num_bytes(bn),
  215. OSSL_PARAM_INTEGER);
  216. return push_BN(bld, key, bn, sz, OSSL_PARAM_UNSIGNED_INTEGER);
  217. }
  218. int OSSL_PARAM_BLD_push_utf8_string(OSSL_PARAM_BLD *bld, const char *key,
  219. const char *buf, size_t bsize)
  220. {
  221. OSSL_PARAM_BLD_DEF *pd;
  222. int secure;
  223. if (bsize == 0) {
  224. bsize = strlen(buf);
  225. } else if (bsize > INT_MAX) {
  226. ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_STRING_TOO_LONG);
  227. return 0;
  228. }
  229. secure = CRYPTO_secure_allocated(buf);
  230. pd = param_push(bld, key, bsize, bsize + 1, OSSL_PARAM_UTF8_STRING, secure);
  231. if (pd == NULL)
  232. return 0;
  233. pd->string = buf;
  234. return 1;
  235. }
  236. int OSSL_PARAM_BLD_push_utf8_ptr(OSSL_PARAM_BLD *bld, const char *key,
  237. char *buf, size_t bsize)
  238. {
  239. OSSL_PARAM_BLD_DEF *pd;
  240. if (bsize == 0) {
  241. bsize = strlen(buf);
  242. } else if (bsize > INT_MAX) {
  243. ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_STRING_TOO_LONG);
  244. return 0;
  245. }
  246. pd = param_push(bld, key, bsize, sizeof(buf), OSSL_PARAM_UTF8_PTR, 0);
  247. if (pd == NULL)
  248. return 0;
  249. pd->string = buf;
  250. return 1;
  251. }
  252. int OSSL_PARAM_BLD_push_octet_string(OSSL_PARAM_BLD *bld, const char *key,
  253. const void *buf, size_t bsize)
  254. {
  255. OSSL_PARAM_BLD_DEF *pd;
  256. int secure;
  257. if (bsize > INT_MAX) {
  258. ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_STRING_TOO_LONG);
  259. return 0;
  260. }
  261. secure = CRYPTO_secure_allocated(buf);
  262. pd = param_push(bld, key, bsize, bsize, OSSL_PARAM_OCTET_STRING, secure);
  263. if (pd == NULL)
  264. return 0;
  265. pd->string = buf;
  266. return 1;
  267. }
  268. int OSSL_PARAM_BLD_push_octet_ptr(OSSL_PARAM_BLD *bld, const char *key,
  269. void *buf, size_t bsize)
  270. {
  271. OSSL_PARAM_BLD_DEF *pd;
  272. if (bsize > INT_MAX) {
  273. ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_STRING_TOO_LONG);
  274. return 0;
  275. }
  276. pd = param_push(bld, key, bsize, sizeof(buf), OSSL_PARAM_OCTET_PTR, 0);
  277. if (pd == NULL)
  278. return 0;
  279. pd->string = buf;
  280. return 1;
  281. }
  282. static OSSL_PARAM *param_bld_convert(OSSL_PARAM_BLD *bld, OSSL_PARAM *param,
  283. OSSL_PARAM_ALIGNED_BLOCK *blk,
  284. OSSL_PARAM_ALIGNED_BLOCK *secure)
  285. {
  286. int i, num = sk_OSSL_PARAM_BLD_DEF_num(bld->params);
  287. OSSL_PARAM_BLD_DEF *pd;
  288. void *p;
  289. for (i = 0; i < num; i++) {
  290. pd = sk_OSSL_PARAM_BLD_DEF_value(bld->params, i);
  291. param[i].key = pd->key;
  292. param[i].data_type = pd->type;
  293. param[i].data_size = pd->size;
  294. param[i].return_size = OSSL_PARAM_UNMODIFIED;
  295. if (pd->secure) {
  296. p = secure;
  297. secure += pd->alloc_blocks;
  298. } else {
  299. p = blk;
  300. blk += pd->alloc_blocks;
  301. }
  302. param[i].data = p;
  303. if (pd->bn != NULL) {
  304. /* BIGNUM */
  305. if (pd->type == OSSL_PARAM_UNSIGNED_INTEGER)
  306. BN_bn2nativepad(pd->bn, (unsigned char *)p, pd->size);
  307. else
  308. BN_signed_bn2native(pd->bn, (unsigned char *)p, pd->size);
  309. } else if (pd->type == OSSL_PARAM_OCTET_PTR
  310. || pd->type == OSSL_PARAM_UTF8_PTR) {
  311. /* PTR */
  312. *(const void **)p = pd->string;
  313. } else if (pd->type == OSSL_PARAM_OCTET_STRING
  314. || pd->type == OSSL_PARAM_UTF8_STRING) {
  315. if (pd->string != NULL)
  316. memcpy(p, pd->string, pd->size);
  317. else
  318. memset(p, 0, pd->size);
  319. if (pd->type == OSSL_PARAM_UTF8_STRING)
  320. ((char *)p)[pd->size] = '\0';
  321. } else {
  322. /* Number, but could also be a NULL BIGNUM */
  323. if (pd->size > sizeof(pd->num))
  324. memset(p, 0, pd->size);
  325. else if (pd->size > 0)
  326. memcpy(p, &pd->num, pd->size);
  327. }
  328. }
  329. param[i] = OSSL_PARAM_construct_end();
  330. return param + i;
  331. }
  332. OSSL_PARAM *OSSL_PARAM_BLD_to_param(OSSL_PARAM_BLD *bld)
  333. {
  334. OSSL_PARAM_ALIGNED_BLOCK *blk, *s = NULL;
  335. OSSL_PARAM *params, *last;
  336. const int num = sk_OSSL_PARAM_BLD_DEF_num(bld->params);
  337. const size_t p_blks = ossl_param_bytes_to_blocks((1 + num) * sizeof(*params));
  338. const size_t total = OSSL_PARAM_ALIGN_SIZE * (p_blks + bld->total_blocks);
  339. const size_t ss = OSSL_PARAM_ALIGN_SIZE * bld->secure_blocks;
  340. if (ss > 0) {
  341. s = OPENSSL_secure_malloc(ss);
  342. if (s == NULL) {
  343. ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_SECURE_MALLOC_FAILURE);
  344. return NULL;
  345. }
  346. }
  347. params = OPENSSL_malloc(total);
  348. if (params == NULL) {
  349. ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
  350. OPENSSL_secure_free(s);
  351. return NULL;
  352. }
  353. blk = p_blks + (OSSL_PARAM_ALIGNED_BLOCK *)(params);
  354. last = param_bld_convert(bld, params, blk, s);
  355. ossl_param_set_secure_block(last, s, ss);
  356. /* Reset builder for reuse */
  357. bld->total_blocks = 0;
  358. bld->secure_blocks = 0;
  359. free_all_params(bld);
  360. return params;
  361. }