params.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295
  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 (ret == 0)
  1033. return 0;
  1034. if (data_length >= max_len)
  1035. data_length = OPENSSL_strnlen(p->data, data_length);
  1036. if (data_length >= max_len)
  1037. return 0; /* No space for a terminating NUL byte */
  1038. (*val)[data_length] = '\0';
  1039. return ret;
  1040. }
  1041. int OSSL_PARAM_get_octet_string(const OSSL_PARAM *p, void **val, size_t max_len,
  1042. size_t *used_len)
  1043. {
  1044. return get_string_internal(p, val, &max_len, used_len,
  1045. OSSL_PARAM_OCTET_STRING);
  1046. }
  1047. static int set_string_internal(OSSL_PARAM *p, const void *val, size_t len,
  1048. unsigned int type)
  1049. {
  1050. p->return_size = len;
  1051. if (p->data == NULL)
  1052. return 1;
  1053. if (p->data_type != type || p->data_size < len)
  1054. return 0;
  1055. memcpy(p->data, val, len);
  1056. /* If possible within the size of p->data, add a NUL terminator byte */
  1057. if (type == OSSL_PARAM_UTF8_STRING && p->data_size > len)
  1058. ((char *)p->data)[len] = '\0';
  1059. return 1;
  1060. }
  1061. int OSSL_PARAM_set_utf8_string(OSSL_PARAM *p, const char *val)
  1062. {
  1063. if (p == NULL)
  1064. return 0;
  1065. p->return_size = 0;
  1066. if (val == NULL)
  1067. return 0;
  1068. return set_string_internal(p, val, strlen(val), OSSL_PARAM_UTF8_STRING);
  1069. }
  1070. int OSSL_PARAM_set_octet_string(OSSL_PARAM *p, const void *val,
  1071. size_t len)
  1072. {
  1073. if (p == NULL)
  1074. return 0;
  1075. p->return_size = 0;
  1076. if (val == NULL)
  1077. return 0;
  1078. return set_string_internal(p, val, len, OSSL_PARAM_OCTET_STRING);
  1079. }
  1080. OSSL_PARAM OSSL_PARAM_construct_utf8_string(const char *key, char *buf,
  1081. size_t bsize)
  1082. {
  1083. if (buf != NULL && bsize == 0)
  1084. bsize = strlen(buf);
  1085. return ossl_param_construct(key, OSSL_PARAM_UTF8_STRING, buf, bsize);
  1086. }
  1087. OSSL_PARAM OSSL_PARAM_construct_octet_string(const char *key, void *buf,
  1088. size_t bsize)
  1089. {
  1090. return ossl_param_construct(key, OSSL_PARAM_OCTET_STRING, buf, bsize);
  1091. }
  1092. static int get_ptr_internal(const OSSL_PARAM *p, const void **val,
  1093. size_t *used_len, unsigned int type)
  1094. {
  1095. if (val == NULL || p == NULL || p->data_type != type)
  1096. return 0;
  1097. if (used_len != NULL)
  1098. *used_len = p->data_size;
  1099. *val = *(const void **)p->data;
  1100. return 1;
  1101. }
  1102. int OSSL_PARAM_get_utf8_ptr(const OSSL_PARAM *p, const char **val)
  1103. {
  1104. return get_ptr_internal(p, (const void **)val, NULL, OSSL_PARAM_UTF8_PTR);
  1105. }
  1106. int OSSL_PARAM_get_octet_ptr(const OSSL_PARAM *p, const void **val,
  1107. size_t *used_len)
  1108. {
  1109. return get_ptr_internal(p, val, used_len, OSSL_PARAM_OCTET_PTR);
  1110. }
  1111. static int set_ptr_internal(OSSL_PARAM *p, const void *val,
  1112. unsigned int type, size_t len)
  1113. {
  1114. p->return_size = len;
  1115. if (p->data_type != type)
  1116. return 0;
  1117. if (p->data != NULL)
  1118. *(const void **)p->data = val;
  1119. return 1;
  1120. }
  1121. int OSSL_PARAM_set_utf8_ptr(OSSL_PARAM *p, const char *val)
  1122. {
  1123. if (p == NULL)
  1124. return 0;
  1125. p->return_size = 0;
  1126. return set_ptr_internal(p, val, OSSL_PARAM_UTF8_PTR,
  1127. val == NULL ? 0 : strlen(val));
  1128. }
  1129. int OSSL_PARAM_set_octet_ptr(OSSL_PARAM *p, const void *val,
  1130. size_t used_len)
  1131. {
  1132. if (p == NULL)
  1133. return 0;
  1134. p->return_size = 0;
  1135. return set_ptr_internal(p, val, OSSL_PARAM_OCTET_PTR, used_len);
  1136. }
  1137. OSSL_PARAM OSSL_PARAM_construct_utf8_ptr(const char *key, char **buf,
  1138. size_t bsize)
  1139. {
  1140. return ossl_param_construct(key, OSSL_PARAM_UTF8_PTR, buf, bsize);
  1141. }
  1142. OSSL_PARAM OSSL_PARAM_construct_octet_ptr(const char *key, void **buf,
  1143. size_t bsize)
  1144. {
  1145. return ossl_param_construct(key, OSSL_PARAM_OCTET_PTR, buf, bsize);
  1146. }
  1147. OSSL_PARAM OSSL_PARAM_construct_end(void)
  1148. {
  1149. OSSL_PARAM end = OSSL_PARAM_END;
  1150. return end;
  1151. }
  1152. static int get_string_ptr_internal(const OSSL_PARAM *p, const void **val,
  1153. size_t *used_len, unsigned int type)
  1154. {
  1155. if (val == NULL || p == NULL || p->data_type != type)
  1156. return 0;
  1157. if (used_len != NULL)
  1158. *used_len = p->data_size;
  1159. *val = p->data;
  1160. return 1;
  1161. }
  1162. int OSSL_PARAM_get_utf8_string_ptr(const OSSL_PARAM *p, const char **val)
  1163. {
  1164. return OSSL_PARAM_get_utf8_ptr(p, val)
  1165. || get_string_ptr_internal(p, (const void **)val, NULL,
  1166. OSSL_PARAM_UTF8_STRING);
  1167. }
  1168. int OSSL_PARAM_get_octet_string_ptr(const OSSL_PARAM *p, const void **val,
  1169. size_t *used_len)
  1170. {
  1171. return OSSL_PARAM_get_octet_ptr(p, val, used_len)
  1172. || get_string_ptr_internal(p, val, used_len, OSSL_PARAM_OCTET_STRING);
  1173. }