params.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293
  1. /*
  2. * Copyright 2019-2021 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/params.h>
  12. #include "internal/thread_once.h"
  13. #include "internal/numbers.h"
  14. #include "internal/endian.h"
  15. /*
  16. * Return the number of bits in the mantissa of a double. This is used to
  17. * shift a larger integral value to determine if it will exactly fit into a
  18. * double.
  19. */
  20. static unsigned int real_shift(void)
  21. {
  22. return sizeof(double) == 4 ? 24 : 53;
  23. }
  24. OSSL_PARAM *OSSL_PARAM_locate(OSSL_PARAM *p, const char *key)
  25. {
  26. if (p != NULL && key != NULL)
  27. for (; p->key != NULL; p++)
  28. if (strcmp(key, p->key) == 0)
  29. return p;
  30. return NULL;
  31. }
  32. const OSSL_PARAM *OSSL_PARAM_locate_const(const OSSL_PARAM *p, const char *key)
  33. {
  34. return OSSL_PARAM_locate((OSSL_PARAM *)p, key);
  35. }
  36. static OSSL_PARAM ossl_param_construct(const char *key, unsigned int data_type,
  37. void *data, size_t data_size)
  38. {
  39. OSSL_PARAM res;
  40. res.key = key;
  41. res.data_type = data_type;
  42. res.data = data;
  43. res.data_size = data_size;
  44. res.return_size = OSSL_PARAM_UNMODIFIED;
  45. return res;
  46. }
  47. int OSSL_PARAM_modified(const OSSL_PARAM *p)
  48. {
  49. return p != NULL && p->return_size != OSSL_PARAM_UNMODIFIED;
  50. }
  51. void OSSL_PARAM_set_all_unmodified(OSSL_PARAM *p)
  52. {
  53. if (p != NULL)
  54. while (p->key != NULL)
  55. p++->return_size = OSSL_PARAM_UNMODIFIED;
  56. }
  57. /* Return non-zero if the signed number is negative */
  58. static int is_negative(const void *number, size_t s)
  59. {
  60. const unsigned char *n = number;
  61. DECLARE_IS_ENDIAN;
  62. return 0x80 & (IS_BIG_ENDIAN ? n[0] : n[s - 1]);
  63. }
  64. /* Check that all the bytes specified match the expected sign byte */
  65. static int check_sign_bytes(const unsigned char *p, size_t n, unsigned char s)
  66. {
  67. size_t i;
  68. for (i = 0; i < n; i++)
  69. if (p[i] != s)
  70. return 0;
  71. return 1;
  72. }
  73. /*
  74. * Copy an integer to another integer.
  75. * Handle different length integers and signed and unsigned integers.
  76. * Both integers are in native byte ordering.
  77. */
  78. static int copy_integer(unsigned char *dest, size_t dest_len,
  79. const unsigned char *src, size_t src_len,
  80. unsigned char pad, int signed_int)
  81. {
  82. size_t n;
  83. DECLARE_IS_ENDIAN;
  84. if (IS_BIG_ENDIAN) {
  85. if (src_len < dest_len) {
  86. n = dest_len - src_len;
  87. memset(dest, pad, n);
  88. memcpy(dest + n, src, src_len);
  89. } else {
  90. n = src_len - dest_len;
  91. if (!check_sign_bytes(src, n, pad)
  92. /*
  93. * Shortening a signed value must retain the correct sign.
  94. * Avoiding this kind of thing: -253 = 0xff03 -> 0x03 = 3
  95. */
  96. || (signed_int && ((pad ^ src[n]) & 0x80) != 0))
  97. return 0;
  98. memcpy(dest, src + n, dest_len);
  99. }
  100. } else /* IS_LITTLE_ENDIAN */ {
  101. if (src_len < dest_len) {
  102. n = dest_len - src_len;
  103. memset(dest + src_len, pad, n);
  104. memcpy(dest, src, src_len);
  105. } else {
  106. n = src_len - dest_len;
  107. if (!check_sign_bytes(src + dest_len, n, pad)
  108. /*
  109. * Shortening a signed value must retain the correct sign.
  110. * Avoiding this kind of thing: 130 = 0x0082 -> 0x82 = -126
  111. */
  112. || (signed_int && ((pad ^ src[dest_len - 1]) & 0x80) != 0))
  113. return 0;
  114. memcpy(dest, src, dest_len);
  115. }
  116. }
  117. return 1;
  118. }
  119. /* Copy a signed number to a signed number of possibly different length */
  120. static int signed_from_signed(void *dest, size_t dest_len,
  121. const void *src, size_t src_len)
  122. {
  123. return copy_integer(dest, dest_len, src, src_len,
  124. is_negative(src, src_len) ? 0xff : 0, 1);
  125. }
  126. /* Copy an unsigned number to a signed number of possibly different length */
  127. static int signed_from_unsigned(void *dest, size_t dest_len,
  128. const void *src, size_t src_len)
  129. {
  130. return copy_integer(dest, dest_len, src, src_len, 0, 1);
  131. }
  132. /* Copy a signed number to an unsigned number of possibly different length */
  133. static int unsigned_from_signed(void *dest, size_t dest_len,
  134. const void *src, size_t src_len)
  135. {
  136. if (is_negative(src, src_len))
  137. return 0;
  138. return copy_integer(dest, dest_len, src, src_len, 0, 0);
  139. }
  140. /* Copy an unsigned number to an unsigned number of possibly different length */
  141. static int unsigned_from_unsigned(void *dest, size_t dest_len,
  142. const void *src, size_t src_len)
  143. {
  144. return copy_integer(dest, dest_len, src, src_len, 0, 0);
  145. }
  146. /* General purpose get integer parameter call that handles odd sizes */
  147. static int general_get_int(const OSSL_PARAM *p, void *val, size_t val_size)
  148. {
  149. if (p->data_type == OSSL_PARAM_INTEGER)
  150. return signed_from_signed(val, val_size, p->data, p->data_size);
  151. if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER)
  152. return signed_from_unsigned(val, val_size, p->data, p->data_size);
  153. return 0;
  154. }
  155. /* General purpose set integer parameter call that handles odd sizes */
  156. static int general_set_int(OSSL_PARAM *p, void *val, size_t val_size)
  157. {
  158. int r = 0;
  159. p->return_size = val_size; /* Expected size */
  160. if (p->data == NULL)
  161. return 1;
  162. if (p->data_type == OSSL_PARAM_INTEGER)
  163. r = signed_from_signed(p->data, p->data_size, val, val_size);
  164. else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER)
  165. r = unsigned_from_signed(p->data, p->data_size, val, val_size);
  166. p->return_size = r ? p->data_size : val_size;
  167. return r;
  168. }
  169. /* General purpose get unsigned integer parameter call that handles odd sizes */
  170. static int general_get_uint(const OSSL_PARAM *p, void *val, size_t val_size)
  171. {
  172. if (p->data_type == OSSL_PARAM_INTEGER)
  173. return unsigned_from_signed(val, val_size, p->data, p->data_size);
  174. if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER)
  175. return unsigned_from_unsigned(val, val_size, p->data, p->data_size);
  176. return 0;
  177. }
  178. /* General purpose set unsigned integer parameter call that handles odd sizes */
  179. static int general_set_uint(OSSL_PARAM *p, void *val, size_t val_size)
  180. {
  181. int r = 0;
  182. p->return_size = val_size; /* Expected size */
  183. if (p->data == NULL)
  184. return 1;
  185. if (p->data_type == OSSL_PARAM_INTEGER)
  186. r = signed_from_unsigned(p->data, p->data_size, val, val_size);
  187. else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER)
  188. r = unsigned_from_unsigned(p->data, p->data_size, val, val_size);
  189. p->return_size = r ? p->data_size : val_size;
  190. return r;
  191. }
  192. int OSSL_PARAM_get_int(const OSSL_PARAM *p, int *val)
  193. {
  194. #ifndef OPENSSL_SMALL_FOOTPRINT
  195. switch (sizeof(int)) {
  196. case sizeof(int32_t):
  197. return OSSL_PARAM_get_int32(p, (int32_t *)val);
  198. case sizeof(int64_t):
  199. return OSSL_PARAM_get_int64(p, (int64_t *)val);
  200. }
  201. #endif
  202. return general_get_int(p, val, sizeof(*val));
  203. }
  204. int OSSL_PARAM_set_int(OSSL_PARAM *p, int val)
  205. {
  206. #ifndef OPENSSL_SMALL_FOOTPRINT
  207. switch (sizeof(int)) {
  208. case sizeof(int32_t):
  209. return OSSL_PARAM_set_int32(p, (int32_t)val);
  210. case sizeof(int64_t):
  211. return OSSL_PARAM_set_int64(p, (int64_t)val);
  212. }
  213. #endif
  214. return general_set_int(p, &val, sizeof(val));
  215. }
  216. OSSL_PARAM OSSL_PARAM_construct_int(const char *key, int *buf)
  217. {
  218. return ossl_param_construct(key, OSSL_PARAM_INTEGER, buf, sizeof(int));
  219. }
  220. int OSSL_PARAM_get_uint(const OSSL_PARAM *p, unsigned int *val)
  221. {
  222. #ifndef OPENSSL_SMALL_FOOTPRINT
  223. switch (sizeof(unsigned int)) {
  224. case sizeof(uint32_t):
  225. return OSSL_PARAM_get_uint32(p, (uint32_t *)val);
  226. case sizeof(uint64_t):
  227. return OSSL_PARAM_get_uint64(p, (uint64_t *)val);
  228. }
  229. #endif
  230. return general_get_uint(p, val, sizeof(*val));
  231. }
  232. int OSSL_PARAM_set_uint(OSSL_PARAM *p, unsigned int val)
  233. {
  234. #ifndef OPENSSL_SMALL_FOOTPRINT
  235. switch (sizeof(unsigned int)) {
  236. case sizeof(uint32_t):
  237. return OSSL_PARAM_set_uint32(p, (uint32_t)val);
  238. case sizeof(uint64_t):
  239. return OSSL_PARAM_set_uint64(p, (uint64_t)val);
  240. }
  241. #endif
  242. return general_set_uint(p, &val, sizeof(val));
  243. }
  244. OSSL_PARAM OSSL_PARAM_construct_uint(const char *key, unsigned int *buf)
  245. {
  246. return ossl_param_construct(key, OSSL_PARAM_UNSIGNED_INTEGER, buf,
  247. sizeof(unsigned int));
  248. }
  249. int OSSL_PARAM_get_long(const OSSL_PARAM *p, long int *val)
  250. {
  251. #ifndef OPENSSL_SMALL_FOOTPRINT
  252. switch (sizeof(long int)) {
  253. case sizeof(int32_t):
  254. return OSSL_PARAM_get_int32(p, (int32_t *)val);
  255. case sizeof(int64_t):
  256. return OSSL_PARAM_get_int64(p, (int64_t *)val);
  257. }
  258. #endif
  259. return general_get_int(p, val, sizeof(*val));
  260. }
  261. int OSSL_PARAM_set_long(OSSL_PARAM *p, long int val)
  262. {
  263. #ifndef OPENSSL_SMALL_FOOTPRINT
  264. switch (sizeof(long int)) {
  265. case sizeof(int32_t):
  266. return OSSL_PARAM_set_int32(p, (int32_t)val);
  267. case sizeof(int64_t):
  268. return OSSL_PARAM_set_int64(p, (int64_t)val);
  269. }
  270. #endif
  271. return general_set_int(p, &val, sizeof(val));
  272. }
  273. OSSL_PARAM OSSL_PARAM_construct_long(const char *key, long int *buf)
  274. {
  275. return ossl_param_construct(key, OSSL_PARAM_INTEGER, buf, sizeof(long int));
  276. }
  277. int OSSL_PARAM_get_ulong(const OSSL_PARAM *p, unsigned long int *val)
  278. {
  279. #ifndef OPENSSL_SMALL_FOOTPRINT
  280. switch (sizeof(unsigned long int)) {
  281. case sizeof(uint32_t):
  282. return OSSL_PARAM_get_uint32(p, (uint32_t *)val);
  283. case sizeof(uint64_t):
  284. return OSSL_PARAM_get_uint64(p, (uint64_t *)val);
  285. }
  286. #endif
  287. return general_get_uint(p, val, sizeof(*val));
  288. }
  289. int OSSL_PARAM_set_ulong(OSSL_PARAM *p, unsigned long int val)
  290. {
  291. #ifndef OPENSSL_SMALL_FOOTPRINT
  292. switch (sizeof(unsigned long int)) {
  293. case sizeof(uint32_t):
  294. return OSSL_PARAM_set_uint32(p, (uint32_t)val);
  295. case sizeof(uint64_t):
  296. return OSSL_PARAM_set_uint64(p, (uint64_t)val);
  297. }
  298. #endif
  299. return general_set_uint(p, &val, sizeof(val));
  300. }
  301. OSSL_PARAM OSSL_PARAM_construct_ulong(const char *key, unsigned long int *buf)
  302. {
  303. return ossl_param_construct(key, OSSL_PARAM_UNSIGNED_INTEGER, buf,
  304. sizeof(unsigned long int));
  305. }
  306. int OSSL_PARAM_get_int32(const OSSL_PARAM *p, int32_t *val)
  307. {
  308. double d;
  309. if (val == NULL || p == NULL )
  310. return 0;
  311. if (p->data_type == OSSL_PARAM_INTEGER) {
  312. #ifndef OPENSSL_SMALL_FOOTPRINT
  313. int64_t i64;
  314. switch (p->data_size) {
  315. case sizeof(int32_t):
  316. *val = *(const int32_t *)p->data;
  317. return 1;
  318. case sizeof(int64_t):
  319. i64 = *(const int64_t *)p->data;
  320. if (i64 >= INT32_MIN && i64 <= INT32_MAX) {
  321. *val = (int32_t)i64;
  322. return 1;
  323. }
  324. return 0;
  325. }
  326. #endif
  327. return general_get_int(p, val, sizeof(*val));
  328. } else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
  329. #ifndef OPENSSL_SMALL_FOOTPRINT
  330. uint32_t u32;
  331. uint64_t u64;
  332. switch (p->data_size) {
  333. case sizeof(uint32_t):
  334. u32 = *(const uint32_t *)p->data;
  335. if (u32 <= INT32_MAX) {
  336. *val = (int32_t)u32;
  337. return 1;
  338. }
  339. return 0;
  340. case sizeof(uint64_t):
  341. u64 = *(const uint64_t *)p->data;
  342. if (u64 <= INT32_MAX) {
  343. *val = (int32_t)u64;
  344. return 1;
  345. }
  346. return 0;
  347. }
  348. #endif
  349. return general_get_int(p, val, sizeof(*val));
  350. } else if (p->data_type == OSSL_PARAM_REAL) {
  351. switch (p->data_size) {
  352. case sizeof(double):
  353. d = *(const double *)p->data;
  354. if (d >= INT32_MIN && d <= INT32_MAX && d == (int32_t)d) {
  355. *val = (int32_t)d;
  356. return 1;
  357. }
  358. break;
  359. }
  360. }
  361. return 0;
  362. }
  363. int OSSL_PARAM_set_int32(OSSL_PARAM *p, int32_t val)
  364. {
  365. if (p == NULL)
  366. return 0;
  367. p->return_size = 0;
  368. if (p->data_type == OSSL_PARAM_INTEGER) {
  369. #ifndef OPENSSL_SMALL_FOOTPRINT
  370. p->return_size = sizeof(int32_t); /* Minimum expected size */
  371. if (p->data == NULL)
  372. return 1;
  373. switch (p->data_size) {
  374. case sizeof(int32_t):
  375. *(int32_t *)p->data = val;
  376. return 1;
  377. case sizeof(int64_t):
  378. p->return_size = sizeof(int64_t);
  379. *(int64_t *)p->data = (int64_t)val;
  380. return 1;
  381. }
  382. #endif
  383. return general_set_int(p, &val, sizeof(val));
  384. } else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER && val >= 0) {
  385. #ifndef OPENSSL_SMALL_FOOTPRINT
  386. p->return_size = sizeof(uint32_t); /* Minimum expected size */
  387. if (p->data == NULL)
  388. return 1;
  389. switch (p->data_size) {
  390. case sizeof(uint32_t):
  391. *(uint32_t *)p->data = (uint32_t)val;
  392. return 1;
  393. case sizeof(uint64_t):
  394. p->return_size = sizeof(uint64_t);
  395. *(uint64_t *)p->data = (uint64_t)val;
  396. return 1;
  397. }
  398. #endif
  399. return general_set_int(p, &val, sizeof(val));
  400. } else if (p->data_type == OSSL_PARAM_REAL) {
  401. p->return_size = sizeof(double);
  402. if (p->data == NULL)
  403. return 1;
  404. switch (p->data_size) {
  405. case sizeof(double):
  406. *(double *)p->data = (double)val;
  407. return 1;
  408. }
  409. }
  410. return 0;
  411. }
  412. OSSL_PARAM OSSL_PARAM_construct_int32(const char *key, int32_t *buf)
  413. {
  414. return ossl_param_construct(key, OSSL_PARAM_INTEGER, buf,
  415. sizeof(int32_t));
  416. }
  417. int OSSL_PARAM_get_uint32(const OSSL_PARAM *p, uint32_t *val)
  418. {
  419. double d;
  420. if (val == NULL || p == NULL)
  421. return 0;
  422. if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
  423. #ifndef OPENSSL_SMALL_FOOTPRINT
  424. uint64_t u64;
  425. switch (p->data_size) {
  426. case sizeof(uint32_t):
  427. *val = *(const uint32_t *)p->data;
  428. return 1;
  429. case sizeof(uint64_t):
  430. u64 = *(const uint64_t *)p->data;
  431. if (u64 <= UINT32_MAX) {
  432. *val = (uint32_t)u64;
  433. return 1;
  434. }
  435. return 0;
  436. }
  437. #endif
  438. return general_get_uint(p, val, sizeof(*val));
  439. } else if (p->data_type == OSSL_PARAM_INTEGER) {
  440. #ifndef OPENSSL_SMALL_FOOTPRINT
  441. int32_t i32;
  442. int64_t i64;
  443. switch (p->data_size) {
  444. case sizeof(int32_t):
  445. i32 = *(const int32_t *)p->data;
  446. if (i32 >= 0) {
  447. *val = i32;
  448. return 1;
  449. }
  450. return 0;
  451. case sizeof(int64_t):
  452. i64 = *(const int64_t *)p->data;
  453. if (i64 >= 0 && i64 <= UINT32_MAX) {
  454. *val = (uint32_t)i64;
  455. return 1;
  456. }
  457. return 0;
  458. }
  459. #endif
  460. return general_get_uint(p, val, sizeof(*val));
  461. } else if (p->data_type == OSSL_PARAM_REAL) {
  462. switch (p->data_size) {
  463. case sizeof(double):
  464. d = *(const double *)p->data;
  465. if (d >= 0 && d <= UINT32_MAX && d == (uint32_t)d) {
  466. *val = (uint32_t)d;
  467. return 1;
  468. }
  469. break;
  470. }
  471. }
  472. return 0;
  473. }
  474. int OSSL_PARAM_set_uint32(OSSL_PARAM *p, uint32_t val)
  475. {
  476. if (p == NULL)
  477. return 0;
  478. p->return_size = 0;
  479. if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
  480. #ifndef OPENSSL_SMALL_FOOTPRINT
  481. p->return_size = sizeof(uint32_t); /* Minimum expected size */
  482. if (p->data == NULL)
  483. return 1;
  484. switch (p->data_size) {
  485. case sizeof(uint32_t):
  486. *(uint32_t *)p->data = val;
  487. return 1;
  488. case sizeof(uint64_t):
  489. p->return_size = sizeof(uint64_t);
  490. *(uint64_t *)p->data = val;
  491. return 1;
  492. }
  493. #endif
  494. return general_set_uint(p, &val, sizeof(val));
  495. } else if (p->data_type == OSSL_PARAM_INTEGER) {
  496. #ifndef OPENSSL_SMALL_FOOTPRINT
  497. p->return_size = sizeof(int32_t); /* Minimum expected size */
  498. if (p->data == NULL)
  499. return 1;
  500. switch (p->data_size) {
  501. case sizeof(int32_t):
  502. if (val <= INT32_MAX) {
  503. *(int32_t *)p->data = (int32_t)val;
  504. return 1;
  505. }
  506. return 0;
  507. case sizeof(int64_t):
  508. p->return_size = sizeof(int64_t);
  509. *(int64_t *)p->data = (int64_t)val;
  510. return 1;
  511. }
  512. #endif
  513. return general_set_uint(p, &val, sizeof(val));
  514. } else if (p->data_type == OSSL_PARAM_REAL) {
  515. p->return_size = sizeof(double);
  516. if (p->data == NULL)
  517. return 1;
  518. switch (p->data_size) {
  519. case sizeof(double):
  520. *(double *)p->data = (double)val;
  521. return 1;
  522. }
  523. }
  524. return 0;
  525. }
  526. OSSL_PARAM OSSL_PARAM_construct_uint32(const char *key, uint32_t *buf)
  527. {
  528. return ossl_param_construct(key, OSSL_PARAM_UNSIGNED_INTEGER, buf,
  529. sizeof(uint32_t));
  530. }
  531. int OSSL_PARAM_get_int64(const OSSL_PARAM *p, int64_t *val)
  532. {
  533. double d;
  534. if (val == NULL || p == NULL )
  535. return 0;
  536. if (p->data_type == OSSL_PARAM_INTEGER) {
  537. #ifndef OPENSSL_SMALL_FOOTPRINT
  538. switch (p->data_size) {
  539. case sizeof(int32_t):
  540. *val = *(const int32_t *)p->data;
  541. return 1;
  542. case sizeof(int64_t):
  543. *val = *(const int64_t *)p->data;
  544. return 1;
  545. }
  546. #endif
  547. return general_get_int(p, val, sizeof(*val));
  548. } else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
  549. #ifndef OPENSSL_SMALL_FOOTPRINT
  550. uint64_t u64;
  551. switch (p->data_size) {
  552. case sizeof(uint32_t):
  553. *val = *(const uint32_t *)p->data;
  554. return 1;
  555. case sizeof(uint64_t):
  556. u64 = *(const uint64_t *)p->data;
  557. if (u64 <= INT64_MAX) {
  558. *val = (int64_t)u64;
  559. return 1;
  560. }
  561. return 0;
  562. }
  563. #endif
  564. return general_get_int(p, val, sizeof(*val));
  565. } else if (p->data_type == OSSL_PARAM_REAL) {
  566. switch (p->data_size) {
  567. case sizeof(double):
  568. d = *(const double *)p->data;
  569. if (d >= INT64_MIN
  570. /*
  571. * By subtracting 65535 (2^16-1) we cancel the low order
  572. * 15 bits of INT64_MAX to avoid using imprecise floating
  573. * point values.
  574. */
  575. && d < (double)(INT64_MAX - 65535) + 65536.0
  576. && d == (int64_t)d) {
  577. *val = (int64_t)d;
  578. return 1;
  579. }
  580. break;
  581. }
  582. }
  583. return 0;
  584. }
  585. int OSSL_PARAM_set_int64(OSSL_PARAM *p, int64_t val)
  586. {
  587. uint64_t u64;
  588. if (p == NULL)
  589. return 0;
  590. p->return_size = 0;
  591. if (p->data_type == OSSL_PARAM_INTEGER) {
  592. #ifndef OPENSSL_SMALL_FOOTPRINT
  593. p->return_size = sizeof(int64_t); /* Expected size */
  594. if (p->data == NULL)
  595. return 1;
  596. switch (p->data_size) {
  597. case sizeof(int32_t):
  598. if (val >= INT32_MIN && val <= INT32_MAX) {
  599. p->return_size = sizeof(int32_t);
  600. *(int32_t *)p->data = (int32_t)val;
  601. return 1;
  602. }
  603. return 0;
  604. case sizeof(int64_t):
  605. *(int64_t *)p->data = val;
  606. return 1;
  607. }
  608. #endif
  609. return general_set_int(p, &val, sizeof(val));
  610. } else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER && val >= 0) {
  611. #ifndef OPENSSL_SMALL_FOOTPRINT
  612. p->return_size = sizeof(uint64_t); /* Expected size */
  613. if (p->data == NULL)
  614. return 1;
  615. switch (p->data_size) {
  616. case sizeof(uint32_t):
  617. if (val <= UINT32_MAX) {
  618. p->return_size = sizeof(uint32_t);
  619. *(uint32_t *)p->data = (uint32_t)val;
  620. return 1;
  621. }
  622. return 0;
  623. case sizeof(uint64_t):
  624. *(uint64_t *)p->data = (uint64_t)val;
  625. return 1;
  626. }
  627. #endif
  628. return general_set_int(p, &val, sizeof(val));
  629. } else if (p->data_type == OSSL_PARAM_REAL) {
  630. p->return_size = sizeof(double);
  631. if (p->data == NULL)
  632. return 1;
  633. switch (p->data_size) {
  634. case sizeof(double):
  635. u64 = val < 0 ? -val : val;
  636. if ((u64 >> real_shift()) == 0) {
  637. *(double *)p->data = (double)val;
  638. return 1;
  639. }
  640. break;
  641. }
  642. }
  643. return 0;
  644. }
  645. OSSL_PARAM OSSL_PARAM_construct_int64(const char *key, int64_t *buf)
  646. {
  647. return ossl_param_construct(key, OSSL_PARAM_INTEGER, buf, sizeof(int64_t));
  648. }
  649. int OSSL_PARAM_get_uint64(const OSSL_PARAM *p, uint64_t *val)
  650. {
  651. double d;
  652. if (val == NULL || p == NULL)
  653. return 0;
  654. if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
  655. #ifndef OPENSSL_SMALL_FOOTPRINT
  656. switch (p->data_size) {
  657. case sizeof(uint32_t):
  658. *val = *(const uint32_t *)p->data;
  659. return 1;
  660. case sizeof(uint64_t):
  661. *val = *(const uint64_t *)p->data;
  662. return 1;
  663. }
  664. #endif
  665. return general_get_uint(p, val, sizeof(*val));
  666. } else if (p->data_type == OSSL_PARAM_INTEGER) {
  667. #ifndef OPENSSL_SMALL_FOOTPRINT
  668. int32_t i32;
  669. int64_t i64;
  670. switch (p->data_size) {
  671. case sizeof(int32_t):
  672. i32 = *(const int32_t *)p->data;
  673. if (i32 >= 0) {
  674. *val = (uint64_t)i32;
  675. return 1;
  676. }
  677. return 0;
  678. case sizeof(int64_t):
  679. i64 = *(const int64_t *)p->data;
  680. if (i64 >= 0) {
  681. *val = (uint64_t)i64;
  682. return 1;
  683. }
  684. return 0;
  685. }
  686. #endif
  687. return general_get_uint(p, val, sizeof(*val));
  688. } else if (p->data_type == OSSL_PARAM_REAL) {
  689. switch (p->data_size) {
  690. case sizeof(double):
  691. d = *(const double *)p->data;
  692. if (d >= 0
  693. /*
  694. * By subtracting 65535 (2^16-1) we cancel the low order
  695. * 15 bits of UINT64_MAX to avoid using imprecise floating
  696. * point values.
  697. */
  698. && d < (double)(UINT64_MAX - 65535) + 65536.0
  699. && d == (uint64_t)d) {
  700. *val = (uint64_t)d;
  701. return 1;
  702. }
  703. break;
  704. }
  705. }
  706. return 0;
  707. }
  708. int OSSL_PARAM_set_uint64(OSSL_PARAM *p, uint64_t val)
  709. {
  710. if (p == NULL)
  711. return 0;
  712. p->return_size = 0;
  713. if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
  714. #ifndef OPENSSL_SMALL_FOOTPRINT
  715. p->return_size = sizeof(uint64_t); /* Expected size */
  716. if (p->data == NULL)
  717. return 1;
  718. switch (p->data_size) {
  719. case sizeof(uint32_t):
  720. if (val <= UINT32_MAX) {
  721. p->return_size = sizeof(uint32_t);
  722. *(uint32_t *)p->data = (uint32_t)val;
  723. return 1;
  724. }
  725. return 0;
  726. case sizeof(uint64_t):
  727. *(uint64_t *)p->data = val;
  728. return 1;
  729. }
  730. #endif
  731. return general_set_uint(p, &val, sizeof(val));
  732. } else if (p->data_type == OSSL_PARAM_INTEGER) {
  733. #ifndef OPENSSL_SMALL_FOOTPRINT
  734. p->return_size = sizeof(int64_t); /* Expected size */
  735. if (p->data == NULL)
  736. return 1;
  737. switch (p->data_size) {
  738. case sizeof(int32_t):
  739. if (val <= INT32_MAX) {
  740. p->return_size = sizeof(int32_t);
  741. *(int32_t *)p->data = (int32_t)val;
  742. return 1;
  743. }
  744. return 0;
  745. case sizeof(int64_t):
  746. if (val <= INT64_MAX) {
  747. *(int64_t *)p->data = (int64_t)val;
  748. return 1;
  749. }
  750. return 0;
  751. }
  752. #endif
  753. return general_set_uint(p, &val, sizeof(val));
  754. } else if (p->data_type == OSSL_PARAM_REAL) {
  755. p->return_size = sizeof(double);
  756. switch (p->data_size) {
  757. case sizeof(double):
  758. if ((val >> real_shift()) == 0) {
  759. *(double *)p->data = (double)val;
  760. return 1;
  761. }
  762. break;
  763. }
  764. }
  765. return 0;
  766. }
  767. OSSL_PARAM OSSL_PARAM_construct_uint64(const char *key, uint64_t *buf)
  768. {
  769. return ossl_param_construct(key, OSSL_PARAM_UNSIGNED_INTEGER, buf,
  770. sizeof(uint64_t));
  771. }
  772. int OSSL_PARAM_get_size_t(const OSSL_PARAM *p, size_t *val)
  773. {
  774. #ifndef OPENSSL_SMALL_FOOTPRINT
  775. switch (sizeof(size_t)) {
  776. case sizeof(uint32_t):
  777. return OSSL_PARAM_get_uint32(p, (uint32_t *)val);
  778. case sizeof(uint64_t):
  779. return OSSL_PARAM_get_uint64(p, (uint64_t *)val);
  780. }
  781. #endif
  782. return general_get_uint(p, val, sizeof(*val));
  783. }
  784. int OSSL_PARAM_set_size_t(OSSL_PARAM *p, size_t val)
  785. {
  786. #ifndef OPENSSL_SMALL_FOOTPRINT
  787. switch (sizeof(size_t)) {
  788. case sizeof(uint32_t):
  789. return OSSL_PARAM_set_uint32(p, (uint32_t)val);
  790. case sizeof(uint64_t):
  791. return OSSL_PARAM_set_uint64(p, (uint64_t)val);
  792. }
  793. #endif
  794. return general_set_uint(p, &val, sizeof(val));
  795. }
  796. OSSL_PARAM OSSL_PARAM_construct_size_t(const char *key, size_t *buf)
  797. {
  798. return ossl_param_construct(key, OSSL_PARAM_UNSIGNED_INTEGER, buf,
  799. sizeof(size_t));
  800. }
  801. int OSSL_PARAM_get_time_t(const OSSL_PARAM *p, time_t *val)
  802. {
  803. #ifndef OPENSSL_SMALL_FOOTPRINT
  804. switch (sizeof(time_t)) {
  805. case sizeof(int32_t):
  806. return OSSL_PARAM_get_int32(p, (int32_t *)val);
  807. case sizeof(int64_t):
  808. return OSSL_PARAM_get_int64(p, (int64_t *)val);
  809. }
  810. #endif
  811. return general_get_int(p, val, sizeof(*val));
  812. }
  813. int OSSL_PARAM_set_time_t(OSSL_PARAM *p, time_t val)
  814. {
  815. #ifndef OPENSSL_SMALL_FOOTPRINT
  816. switch (sizeof(time_t)) {
  817. case sizeof(int32_t):
  818. return OSSL_PARAM_set_int32(p, (int32_t)val);
  819. case sizeof(int64_t):
  820. return OSSL_PARAM_set_int64(p, (int64_t)val);
  821. }
  822. #endif
  823. return general_set_int(p, &val, sizeof(val));
  824. }
  825. OSSL_PARAM OSSL_PARAM_construct_time_t(const char *key, time_t *buf)
  826. {
  827. return ossl_param_construct(key, OSSL_PARAM_INTEGER, buf, sizeof(time_t));
  828. }
  829. int OSSL_PARAM_get_BN(const OSSL_PARAM *p, BIGNUM **val)
  830. {
  831. BIGNUM *b;
  832. if (val == NULL
  833. || p == NULL
  834. || p->data_type != OSSL_PARAM_UNSIGNED_INTEGER)
  835. return 0;
  836. b = BN_native2bn(p->data, (int)p->data_size, *val);
  837. if (b != NULL) {
  838. *val = b;
  839. return 1;
  840. }
  841. return 0;
  842. }
  843. int OSSL_PARAM_set_BN(OSSL_PARAM *p, const BIGNUM *val)
  844. {
  845. size_t bytes;
  846. if (p == NULL)
  847. return 0;
  848. p->return_size = 0;
  849. if (val == NULL || p->data_type != OSSL_PARAM_UNSIGNED_INTEGER)
  850. return 0;
  851. /* For the moment, only positive values are permitted */
  852. if (BN_is_negative(val))
  853. return 0;
  854. bytes = (size_t)BN_num_bytes(val);
  855. p->return_size = bytes;
  856. if (p->data == NULL)
  857. return 1;
  858. if (p->data_size >= bytes) {
  859. p->return_size = p->data_size;
  860. return BN_bn2nativepad(val, p->data, p->data_size) >= 0;
  861. }
  862. return 0;
  863. }
  864. OSSL_PARAM OSSL_PARAM_construct_BN(const char *key, unsigned char *buf,
  865. size_t bsize)
  866. {
  867. return ossl_param_construct(key, OSSL_PARAM_UNSIGNED_INTEGER,
  868. buf, bsize);
  869. }
  870. int OSSL_PARAM_get_double(const OSSL_PARAM *p, double *val)
  871. {
  872. int64_t i64;
  873. uint64_t u64;
  874. if (val == NULL || p == NULL)
  875. return 0;
  876. if (p->data_type == OSSL_PARAM_REAL) {
  877. switch (p->data_size) {
  878. case sizeof(double):
  879. *val = *(const double *)p->data;
  880. return 1;
  881. }
  882. } else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
  883. switch (p->data_size) {
  884. case sizeof(uint32_t):
  885. *val = *(const uint32_t *)p->data;
  886. return 1;
  887. case sizeof(uint64_t):
  888. u64 = *(const uint64_t *)p->data;
  889. if ((u64 >> real_shift()) == 0) {
  890. *val = (double)u64;
  891. return 1;
  892. }
  893. break;
  894. }
  895. } else if (p->data_type == OSSL_PARAM_INTEGER) {
  896. switch (p->data_size) {
  897. case sizeof(int32_t):
  898. *val = *(const int32_t *)p->data;
  899. return 1;
  900. case sizeof(int64_t):
  901. i64 = *(const int64_t *)p->data;
  902. u64 = i64 < 0 ? -i64 : i64;
  903. if ((u64 >> real_shift()) == 0) {
  904. *val = 0.0 + i64;
  905. return 1;
  906. }
  907. break;
  908. }
  909. }
  910. return 0;
  911. }
  912. int OSSL_PARAM_set_double(OSSL_PARAM *p, double val)
  913. {
  914. if (p == NULL)
  915. return 0;
  916. p->return_size = 0;
  917. if (p->data_type == OSSL_PARAM_REAL) {
  918. p->return_size = sizeof(double);
  919. if (p->data == NULL)
  920. return 1;
  921. switch (p->data_size) {
  922. case sizeof(double):
  923. *(double *)p->data = val;
  924. return 1;
  925. }
  926. } else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER
  927. && val == (ossl_uintmax_t)val) {
  928. p->return_size = sizeof(double);
  929. if (p->data == NULL)
  930. return 1;
  931. switch (p->data_size) {
  932. case sizeof(uint32_t):
  933. if (val >= 0 && val <= UINT32_MAX) {
  934. p->return_size = sizeof(uint32_t);
  935. *(uint32_t *)p->data = (uint32_t)val;
  936. return 1;
  937. }
  938. break;
  939. case sizeof(uint64_t):
  940. if (val >= 0
  941. /*
  942. * By subtracting 65535 (2^16-1) we cancel the low order
  943. * 15 bits of UINT64_MAX to avoid using imprecise floating
  944. * point values.
  945. */
  946. && (double)(UINT64_MAX - 65535) + 65536.0) {
  947. p->return_size = sizeof(uint64_t);
  948. *(uint64_t *)p->data = (uint64_t)val;
  949. return 1;
  950. }
  951. break; }
  952. } else if (p->data_type == OSSL_PARAM_INTEGER && val == (ossl_intmax_t)val) {
  953. p->return_size = sizeof(double);
  954. if (p->data == NULL)
  955. return 1;
  956. switch (p->data_size) {
  957. case sizeof(int32_t):
  958. if (val >= INT32_MIN && val <= INT32_MAX) {
  959. p->return_size = sizeof(int32_t);
  960. *(int32_t *)p->data = (int32_t)val;
  961. return 1;
  962. }
  963. break;
  964. case sizeof(int64_t):
  965. if (val >= INT64_MIN
  966. /*
  967. * By subtracting 65535 (2^16-1) we cancel the low order
  968. * 15 bits of INT64_MAX to avoid using imprecise floating
  969. * point values.
  970. */
  971. && val < (double)(INT64_MAX - 65535) + 65536.0) {
  972. p->return_size = sizeof(int64_t);
  973. *(int64_t *)p->data = (int64_t)val;
  974. return 1;
  975. }
  976. break;
  977. }
  978. }
  979. return 0;
  980. }
  981. OSSL_PARAM OSSL_PARAM_construct_double(const char *key, double *buf)
  982. {
  983. return ossl_param_construct(key, OSSL_PARAM_REAL, buf, sizeof(double));
  984. }
  985. static int get_string_internal(const OSSL_PARAM *p, void **val,
  986. size_t *max_len, size_t *used_len,
  987. unsigned int type)
  988. {
  989. size_t sz, alloc_sz;
  990. if ((val == NULL && used_len == NULL) || p == NULL || p->data_type != type)
  991. return 0;
  992. sz = p->data_size;
  993. /*
  994. * If the input size is 0, or the input string needs NUL byte
  995. * termination, allocate an extra byte.
  996. */
  997. alloc_sz = sz + (type == OSSL_PARAM_UTF8_STRING || sz == 0);
  998. if (used_len != NULL)
  999. *used_len = sz;
  1000. if (p->data == NULL)
  1001. return 0;
  1002. if (val == NULL)
  1003. return 1;
  1004. if (*val == NULL) {
  1005. char *const q = OPENSSL_malloc(alloc_sz);
  1006. if (q == NULL)
  1007. return 0;
  1008. *val = q;
  1009. *max_len = alloc_sz;
  1010. }
  1011. if (*max_len < sz)
  1012. return 0;
  1013. memcpy(*val, p->data, sz);
  1014. return 1;
  1015. }
  1016. int OSSL_PARAM_get_utf8_string(const OSSL_PARAM *p, char **val, size_t max_len)
  1017. {
  1018. int ret = get_string_internal(p, (void **)val, &max_len, NULL,
  1019. OSSL_PARAM_UTF8_STRING);
  1020. /*
  1021. * We try to ensure that the copied string is terminated with a
  1022. * NUL byte. That should be easy, just place a NUL byte at
  1023. * |((char*)*val)[p->data_size]|.
  1024. * Unfortunately, we have seen cases where |p->data_size| doesn't
  1025. * correctly reflect the length of the string, and just happens
  1026. * to be out of bounds according to |max_len|, so in that case, we
  1027. * make the extra step of trying to find the true length of the
  1028. * string that |p->data| points at, and use that as an index to
  1029. * place the NUL byte in |*val|.
  1030. */
  1031. size_t data_length = p->data_size;
  1032. if (data_length >= max_len)
  1033. data_length = OPENSSL_strnlen(p->data, data_length);
  1034. if (data_length >= max_len)
  1035. return 0; /* No space for a terminating NUL byte */
  1036. ((char *)*val)[data_length] = '\0';
  1037. return ret;
  1038. }
  1039. int OSSL_PARAM_get_octet_string(const OSSL_PARAM *p, void **val, size_t max_len,
  1040. size_t *used_len)
  1041. {
  1042. return get_string_internal(p, val, &max_len, used_len,
  1043. OSSL_PARAM_OCTET_STRING);
  1044. }
  1045. static int set_string_internal(OSSL_PARAM *p, const void *val, size_t len,
  1046. unsigned int type)
  1047. {
  1048. p->return_size = len;
  1049. if (p->data == NULL)
  1050. return 1;
  1051. if (p->data_type != type || p->data_size < len)
  1052. return 0;
  1053. memcpy(p->data, val, len);
  1054. /* If possible within the size of p->data, add a NUL terminator byte */
  1055. if (type == OSSL_PARAM_UTF8_STRING && p->data_size > len)
  1056. ((char *)p->data)[len] = '\0';
  1057. return 1;
  1058. }
  1059. int OSSL_PARAM_set_utf8_string(OSSL_PARAM *p, const char *val)
  1060. {
  1061. if (p == NULL)
  1062. return 0;
  1063. p->return_size = 0;
  1064. if (val == NULL)
  1065. return 0;
  1066. return set_string_internal(p, val, strlen(val), OSSL_PARAM_UTF8_STRING);
  1067. }
  1068. int OSSL_PARAM_set_octet_string(OSSL_PARAM *p, const void *val,
  1069. size_t len)
  1070. {
  1071. if (p == NULL)
  1072. return 0;
  1073. p->return_size = 0;
  1074. if (val == NULL)
  1075. return 0;
  1076. return set_string_internal(p, val, len, OSSL_PARAM_OCTET_STRING);
  1077. }
  1078. OSSL_PARAM OSSL_PARAM_construct_utf8_string(const char *key, char *buf,
  1079. size_t bsize)
  1080. {
  1081. if (buf != NULL && bsize == 0)
  1082. bsize = strlen(buf);
  1083. return ossl_param_construct(key, OSSL_PARAM_UTF8_STRING, buf, bsize);
  1084. }
  1085. OSSL_PARAM OSSL_PARAM_construct_octet_string(const char *key, void *buf,
  1086. size_t bsize)
  1087. {
  1088. return ossl_param_construct(key, OSSL_PARAM_OCTET_STRING, buf, bsize);
  1089. }
  1090. static int get_ptr_internal(const OSSL_PARAM *p, const void **val,
  1091. size_t *used_len, unsigned int type)
  1092. {
  1093. if (val == NULL || p == NULL || p->data_type != type)
  1094. return 0;
  1095. if (used_len != NULL)
  1096. *used_len = p->data_size;
  1097. *val = *(const void **)p->data;
  1098. return 1;
  1099. }
  1100. int OSSL_PARAM_get_utf8_ptr(const OSSL_PARAM *p, const char **val)
  1101. {
  1102. return get_ptr_internal(p, (const void **)val, NULL, OSSL_PARAM_UTF8_PTR);
  1103. }
  1104. int OSSL_PARAM_get_octet_ptr(const OSSL_PARAM *p, const void **val,
  1105. size_t *used_len)
  1106. {
  1107. return get_ptr_internal(p, val, used_len, OSSL_PARAM_OCTET_PTR);
  1108. }
  1109. static int set_ptr_internal(OSSL_PARAM *p, const void *val,
  1110. unsigned int type, size_t len)
  1111. {
  1112. p->return_size = len;
  1113. if (p->data_type != type)
  1114. return 0;
  1115. if (p->data != NULL)
  1116. *(const void **)p->data = val;
  1117. return 1;
  1118. }
  1119. int OSSL_PARAM_set_utf8_ptr(OSSL_PARAM *p, const char *val)
  1120. {
  1121. if (p == NULL)
  1122. return 0;
  1123. p->return_size = 0;
  1124. return set_ptr_internal(p, val, OSSL_PARAM_UTF8_PTR,
  1125. val == NULL ? 0 : strlen(val));
  1126. }
  1127. int OSSL_PARAM_set_octet_ptr(OSSL_PARAM *p, const void *val,
  1128. size_t used_len)
  1129. {
  1130. if (p == NULL)
  1131. return 0;
  1132. p->return_size = 0;
  1133. return set_ptr_internal(p, val, OSSL_PARAM_OCTET_PTR, used_len);
  1134. }
  1135. OSSL_PARAM OSSL_PARAM_construct_utf8_ptr(const char *key, char **buf,
  1136. size_t bsize)
  1137. {
  1138. return ossl_param_construct(key, OSSL_PARAM_UTF8_PTR, buf, bsize);
  1139. }
  1140. OSSL_PARAM OSSL_PARAM_construct_octet_ptr(const char *key, void **buf,
  1141. size_t bsize)
  1142. {
  1143. return ossl_param_construct(key, OSSL_PARAM_OCTET_PTR, buf, bsize);
  1144. }
  1145. OSSL_PARAM OSSL_PARAM_construct_end(void)
  1146. {
  1147. OSSL_PARAM end = OSSL_PARAM_END;
  1148. return end;
  1149. }
  1150. static int get_string_ptr_internal(const OSSL_PARAM *p, const void **val,
  1151. size_t *used_len, unsigned int type)
  1152. {
  1153. if (val == NULL || p == NULL || p->data_type != type)
  1154. return 0;
  1155. if (used_len != NULL)
  1156. *used_len = p->data_size;
  1157. *val = p->data;
  1158. return 1;
  1159. }
  1160. int OSSL_PARAM_get_utf8_string_ptr(const OSSL_PARAM *p, const char **val)
  1161. {
  1162. return OSSL_PARAM_get_utf8_ptr(p, val)
  1163. || get_string_ptr_internal(p, (const void **)val, NULL,
  1164. OSSL_PARAM_UTF8_STRING);
  1165. }
  1166. int OSSL_PARAM_get_octet_string_ptr(const OSSL_PARAM *p, const void **val,
  1167. size_t *used_len)
  1168. {
  1169. return OSSL_PARAM_get_octet_ptr(p, val, used_len)
  1170. || get_string_ptr_internal(p, val, used_len, OSSL_PARAM_OCTET_STRING);
  1171. }