param_build.c 11 KB

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