params.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941
  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/params.h>
  12. #include "internal/thread_once.h"
  13. #include "internal/numbers.h"
  14. OSSL_PARAM *OSSL_PARAM_locate(OSSL_PARAM *p, const char *key)
  15. {
  16. if (p != NULL && key != NULL)
  17. for (; p->key != NULL; p++)
  18. if (strcmp(key, p->key) == 0)
  19. return p;
  20. return NULL;
  21. }
  22. const OSSL_PARAM *OSSL_PARAM_locate_const(const OSSL_PARAM *p, const char *key)
  23. {
  24. return OSSL_PARAM_locate((OSSL_PARAM *)p, key);
  25. }
  26. static OSSL_PARAM ossl_param_construct(const char *key, unsigned int data_type,
  27. void *data, size_t data_size)
  28. {
  29. OSSL_PARAM res;
  30. res.key = key;
  31. res.data_type = data_type;
  32. res.data = data;
  33. res.data_size = data_size;
  34. res.return_size = OSSL_PARAM_UNMODIFIED;
  35. return res;
  36. }
  37. int OSSL_PARAM_modified(const OSSL_PARAM *p)
  38. {
  39. return p != NULL && p->return_size != OSSL_PARAM_UNMODIFIED;
  40. }
  41. void OSSL_PARAM_set_unmodified(OSSL_PARAM *p)
  42. {
  43. if (p != NULL)
  44. p->return_size = OSSL_PARAM_UNMODIFIED;
  45. }
  46. int OSSL_PARAM_get_int(const OSSL_PARAM *p, int *val)
  47. {
  48. switch (sizeof(int)) {
  49. case sizeof(int32_t):
  50. return OSSL_PARAM_get_int32(p, (int32_t *)val);
  51. case sizeof(int64_t):
  52. return OSSL_PARAM_get_int64(p, (int64_t *)val);
  53. }
  54. return 0;
  55. }
  56. int OSSL_PARAM_set_int(OSSL_PARAM *p, int val)
  57. {
  58. switch (sizeof(int)) {
  59. case sizeof(int32_t):
  60. return OSSL_PARAM_set_int32(p, (int32_t)val);
  61. case sizeof(int64_t):
  62. return OSSL_PARAM_set_int64(p, (int64_t)val);
  63. }
  64. return 0;
  65. }
  66. OSSL_PARAM OSSL_PARAM_construct_int(const char *key, int *buf)
  67. {
  68. return ossl_param_construct(key, OSSL_PARAM_INTEGER, buf, sizeof(int));
  69. }
  70. int OSSL_PARAM_get_uint(const OSSL_PARAM *p, unsigned int *val)
  71. {
  72. switch (sizeof(unsigned int)) {
  73. case sizeof(uint32_t):
  74. return OSSL_PARAM_get_uint32(p, (uint32_t *)val);
  75. case sizeof(uint64_t):
  76. return OSSL_PARAM_get_uint64(p, (uint64_t *)val);
  77. }
  78. return 0;
  79. }
  80. int OSSL_PARAM_set_uint(OSSL_PARAM *p, unsigned int val)
  81. {
  82. switch (sizeof(unsigned int)) {
  83. case sizeof(uint32_t):
  84. return OSSL_PARAM_set_uint32(p, (uint32_t)val);
  85. case sizeof(uint64_t):
  86. return OSSL_PARAM_set_uint64(p, (uint64_t)val);
  87. }
  88. return 0;
  89. }
  90. OSSL_PARAM OSSL_PARAM_construct_uint(const char *key, unsigned int *buf)
  91. {
  92. return ossl_param_construct(key, OSSL_PARAM_UNSIGNED_INTEGER, buf,
  93. sizeof(unsigned int));
  94. }
  95. int OSSL_PARAM_get_long(const OSSL_PARAM *p, long int *val)
  96. {
  97. switch (sizeof(long int)) {
  98. case sizeof(int32_t):
  99. return OSSL_PARAM_get_int32(p, (int32_t *)val);
  100. case sizeof(int64_t):
  101. return OSSL_PARAM_get_int64(p, (int64_t *)val);
  102. }
  103. return 0;
  104. }
  105. int OSSL_PARAM_set_long(OSSL_PARAM *p, long int val)
  106. {
  107. switch (sizeof(long int)) {
  108. case sizeof(int32_t):
  109. return OSSL_PARAM_set_int32(p, (int32_t)val);
  110. case sizeof(int64_t):
  111. return OSSL_PARAM_set_int64(p, (int64_t)val);
  112. }
  113. return 0;
  114. }
  115. OSSL_PARAM OSSL_PARAM_construct_long(const char *key, long int *buf)
  116. {
  117. return ossl_param_construct(key, OSSL_PARAM_INTEGER, buf, sizeof(long int));
  118. }
  119. int OSSL_PARAM_get_ulong(const OSSL_PARAM *p, unsigned long int *val)
  120. {
  121. switch (sizeof(unsigned long int)) {
  122. case sizeof(uint32_t):
  123. return OSSL_PARAM_get_uint32(p, (uint32_t *)val);
  124. case sizeof(uint64_t):
  125. return OSSL_PARAM_get_uint64(p, (uint64_t *)val);
  126. }
  127. return 0;
  128. }
  129. int OSSL_PARAM_set_ulong(OSSL_PARAM *p, unsigned long int val)
  130. {
  131. switch (sizeof(unsigned long int)) {
  132. case sizeof(uint32_t):
  133. return OSSL_PARAM_set_uint32(p, (uint32_t)val);
  134. case sizeof(uint64_t):
  135. return OSSL_PARAM_set_uint64(p, (uint64_t)val);
  136. }
  137. return 0;
  138. }
  139. OSSL_PARAM OSSL_PARAM_construct_ulong(const char *key, unsigned long int *buf)
  140. {
  141. return ossl_param_construct(key, OSSL_PARAM_UNSIGNED_INTEGER, buf,
  142. sizeof(unsigned long int));
  143. }
  144. int OSSL_PARAM_get_int32(const OSSL_PARAM *p, int32_t *val)
  145. {
  146. int64_t i64;
  147. uint32_t u32;
  148. uint64_t u64;
  149. double d;
  150. if (val == NULL || p == NULL )
  151. return 0;
  152. if (p->data_type == OSSL_PARAM_INTEGER) {
  153. switch (p->data_size) {
  154. case sizeof(int32_t):
  155. *val = *(const int32_t *)p->data;
  156. return 1;
  157. case sizeof(int64_t):
  158. i64 = *(const int64_t *)p->data;
  159. if (i64 >= INT32_MIN && i64 <= INT32_MAX) {
  160. *val = (int32_t)i64;
  161. return 1;
  162. }
  163. break;
  164. }
  165. } else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
  166. switch (p->data_size) {
  167. case sizeof(uint32_t):
  168. u32 = *(const uint32_t *)p->data;
  169. if (u32 <= INT32_MAX) {
  170. *val = (int32_t)u32;
  171. return 1;
  172. }
  173. break;
  174. case sizeof(uint64_t):
  175. u64 = *(const uint64_t *)p->data;
  176. if (u64 <= INT32_MAX) {
  177. *val = (int32_t)u64;
  178. return 1;
  179. }
  180. break;
  181. }
  182. } else if (p->data_type == OSSL_PARAM_REAL) {
  183. switch (p->data_size) {
  184. case sizeof(double):
  185. d = *(const double *)p->data;
  186. if (d >= INT32_MIN && d <= INT32_MAX && d == (int32_t)d) {
  187. *val = (int32_t)d;
  188. return 1;
  189. }
  190. break;
  191. }
  192. }
  193. return 0;
  194. }
  195. int OSSL_PARAM_set_int32(OSSL_PARAM *p, int32_t val)
  196. {
  197. if (p == NULL)
  198. return 0;
  199. p->return_size = 0;
  200. if (p->data_type == OSSL_PARAM_INTEGER) {
  201. p->return_size = sizeof(int32_t); /* Minimum expected size */
  202. if (p->data == NULL)
  203. return 1;
  204. switch (p->data_size) {
  205. case sizeof(int32_t):
  206. *(int32_t *)p->data = val;
  207. return 1;
  208. case sizeof(int64_t):
  209. p->return_size = sizeof(int64_t);
  210. *(int64_t *)p->data = (int64_t)val;
  211. return 1;
  212. }
  213. } else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER && val >= 0) {
  214. p->return_size = sizeof(uint32_t); /* Minimum expected size */
  215. if (p->data == NULL)
  216. return 1;
  217. switch (p->data_size) {
  218. case sizeof(uint32_t):
  219. *(uint32_t *)p->data = (uint32_t)val;
  220. return 1;
  221. case sizeof(uint64_t):
  222. p->return_size = sizeof(uint64_t);
  223. *(uint64_t *)p->data = (uint64_t)val;
  224. return 1;
  225. }
  226. } else if (p->data_type == OSSL_PARAM_REAL) {
  227. p->return_size = sizeof(double);
  228. if (p->data == NULL)
  229. return 1;
  230. switch (p->data_size) {
  231. case sizeof(double):
  232. *(double *)p->data = (double)val;
  233. return 1;
  234. }
  235. }
  236. return 0;
  237. }
  238. OSSL_PARAM OSSL_PARAM_construct_int32(const char *key, int32_t *buf)
  239. {
  240. return ossl_param_construct(key, OSSL_PARAM_INTEGER, buf,
  241. sizeof(int32_t));
  242. }
  243. int OSSL_PARAM_get_uint32(const OSSL_PARAM *p, uint32_t *val)
  244. {
  245. int32_t i32;
  246. int64_t i64;
  247. uint64_t u64;
  248. double d;
  249. if (val == NULL || p == NULL)
  250. return 0;
  251. if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
  252. switch (p->data_size) {
  253. case sizeof(uint32_t):
  254. *val = *(const uint32_t *)p->data;
  255. return 1;
  256. case sizeof(uint64_t):
  257. u64 = *(const uint64_t *)p->data;
  258. if (u64 <= UINT32_MAX) {
  259. *val = (uint32_t)u64;
  260. return 1;
  261. }
  262. break;
  263. }
  264. } else if (p->data_type == OSSL_PARAM_INTEGER) {
  265. switch (p->data_size) {
  266. case sizeof(int32_t):
  267. i32 = *(const int32_t *)p->data;
  268. if (i32 >= 0) {
  269. *val = i32;
  270. return 1;
  271. }
  272. break;
  273. case sizeof(int64_t):
  274. i64 = *(const int64_t *)p->data;
  275. if (i64 >= 0 && i64 <= UINT32_MAX) {
  276. *val = (uint32_t)i64;
  277. return 1;
  278. }
  279. break;
  280. }
  281. } else if (p->data_type == OSSL_PARAM_REAL) {
  282. switch (p->data_size) {
  283. case sizeof(double):
  284. d = *(const double *)p->data;
  285. if (d >= 0 && d <= UINT32_MAX && d == (uint32_t)d) {
  286. *val = (uint32_t)d;
  287. return 1;
  288. }
  289. break;
  290. }
  291. }
  292. return 0;
  293. }
  294. int OSSL_PARAM_set_uint32(OSSL_PARAM *p, uint32_t val)
  295. {
  296. if (p == NULL)
  297. return 0;
  298. p->return_size = 0;
  299. if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
  300. p->return_size = sizeof(uint32_t); /* Minimum expected size */
  301. if (p->data == NULL)
  302. return 1;
  303. switch (p->data_size) {
  304. case sizeof(uint32_t):
  305. *(uint32_t *)p->data = val;
  306. return 1;
  307. case sizeof(uint64_t):
  308. p->return_size = sizeof(uint64_t);
  309. *(uint64_t *)p->data = val;
  310. return 1;
  311. }
  312. } else if (p->data_type == OSSL_PARAM_INTEGER) {
  313. p->return_size = sizeof(int32_t); /* Minimum expected size */
  314. if (p->data == NULL)
  315. return 1;
  316. switch (p->data_size) {
  317. case sizeof(int32_t):
  318. if (val <= INT32_MAX) {
  319. *(int32_t *)p->data = (int32_t)val;
  320. return 1;
  321. }
  322. break;
  323. case sizeof(int64_t):
  324. p->return_size = sizeof(int64_t);
  325. *(int64_t *)p->data = (int64_t)val;
  326. return 1;
  327. }
  328. } else if (p->data_type == OSSL_PARAM_REAL) {
  329. p->return_size = sizeof(double);
  330. if (p->data == NULL)
  331. return 1;
  332. switch (p->data_size) {
  333. case sizeof(double):
  334. *(double *)p->data = (double)val;
  335. return 1;
  336. }
  337. }
  338. return 0;
  339. }
  340. OSSL_PARAM OSSL_PARAM_construct_uint32(const char *key, uint32_t *buf)
  341. {
  342. return ossl_param_construct(key, OSSL_PARAM_UNSIGNED_INTEGER, buf,
  343. sizeof(uint32_t));
  344. }
  345. int OSSL_PARAM_get_int64(const OSSL_PARAM *p, int64_t *val)
  346. {
  347. uint64_t u64;
  348. double d;
  349. if (val == NULL || p == NULL )
  350. return 0;
  351. if (p->data_type == OSSL_PARAM_INTEGER) {
  352. switch (p->data_size) {
  353. case sizeof(int32_t):
  354. *val = *(const int32_t *)p->data;
  355. return 1;
  356. case sizeof(int64_t):
  357. *val = *(const int64_t *)p->data;
  358. return 1;
  359. }
  360. } else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
  361. switch (p->data_size) {
  362. case sizeof(uint32_t):
  363. *val = *(const uint32_t *)p->data;
  364. return 1;
  365. case sizeof(uint64_t):
  366. u64 = *(const uint64_t *)p->data;
  367. if (u64 <= INT64_MAX) {
  368. *val = (int64_t)u64;
  369. return 1;
  370. }
  371. break;
  372. }
  373. } else if (p->data_type == OSSL_PARAM_REAL) {
  374. switch (p->data_size) {
  375. case sizeof(double):
  376. d = *(const double *)p->data;
  377. if (d >= INT64_MIN && d <= INT64_MAX && d == (int64_t)d) {
  378. *val = (int64_t)d;
  379. return 1;
  380. }
  381. break;
  382. }
  383. }
  384. return 0;
  385. }
  386. int OSSL_PARAM_set_int64(OSSL_PARAM *p, int64_t val)
  387. {
  388. uint64_t u64;
  389. if (p == NULL)
  390. return 0;
  391. p->return_size = 0;
  392. if (p->data_type == OSSL_PARAM_INTEGER) {
  393. p->return_size = sizeof(int64_t); /* Expected size */
  394. if (p->data == NULL)
  395. return 1;
  396. switch (p->data_size) {
  397. case sizeof(int32_t):
  398. if (val >= INT32_MIN && val <= INT32_MAX) {
  399. p->return_size = sizeof(int32_t);
  400. *(int32_t *)p->data = (int32_t)val;
  401. return 1;
  402. }
  403. break;
  404. case sizeof(int64_t):
  405. *(int64_t *)p->data = val;
  406. return 1;
  407. }
  408. } else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER && val >= 0) {
  409. p->return_size = sizeof(uint64_t); /* Expected size */
  410. if (p->data == NULL)
  411. return 1;
  412. switch (p->data_size) {
  413. case sizeof(uint32_t):
  414. if (val <= UINT32_MAX) {
  415. p->return_size = sizeof(uint32_t);
  416. *(uint32_t *)p->data = (uint32_t)val;
  417. return 1;
  418. }
  419. break;
  420. case sizeof(uint64_t):
  421. *(uint64_t *)p->data = (uint64_t)val;
  422. return 1;
  423. }
  424. } else if (p->data_type == OSSL_PARAM_REAL) {
  425. p->return_size = sizeof(double);
  426. if (p->data == NULL)
  427. return 1;
  428. switch (p->data_size) {
  429. case sizeof(double):
  430. u64 = val < 0 ? -val : val;
  431. if ((u64 >> 53) == 0) { /* 53 significant bits in the mantissa */
  432. *(double *)p->data = (double)val;
  433. return 1;
  434. }
  435. break;
  436. }
  437. }
  438. return 0;
  439. }
  440. OSSL_PARAM OSSL_PARAM_construct_int64(const char *key, int64_t *buf)
  441. {
  442. return ossl_param_construct(key, OSSL_PARAM_INTEGER, buf, sizeof(int64_t));
  443. }
  444. int OSSL_PARAM_get_uint64(const OSSL_PARAM *p, uint64_t *val)
  445. {
  446. int32_t i32;
  447. int64_t i64;
  448. double d;
  449. if (val == NULL || p == NULL)
  450. return 0;
  451. if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
  452. switch (p->data_size) {
  453. case sizeof(uint32_t):
  454. *val = *(const uint32_t *)p->data;
  455. return 1;
  456. case sizeof(uint64_t):
  457. *val = *(const uint64_t *)p->data;
  458. return 1;
  459. }
  460. } else if (p->data_type == OSSL_PARAM_INTEGER) {
  461. switch (p->data_size) {
  462. case sizeof(int32_t):
  463. i32 = *(const int32_t *)p->data;
  464. if (i32 >= 0) {
  465. *val = (uint64_t)i32;
  466. return 1;
  467. }
  468. break;
  469. case sizeof(int64_t):
  470. i64 = *(const int64_t *)p->data;
  471. if (i64 >= 0) {
  472. *val = (uint64_t)i64;
  473. return 1;
  474. }
  475. break;
  476. }
  477. } else if (p->data_type == OSSL_PARAM_REAL) {
  478. switch (p->data_size) {
  479. case sizeof(double):
  480. d = *(const double *)p->data;
  481. if (d >= 0 && d <= INT64_MAX && d == (uint64_t)d) {
  482. *val = (uint64_t)d;
  483. return 1;
  484. }
  485. break;
  486. }
  487. }
  488. return 0;
  489. }
  490. int OSSL_PARAM_set_uint64(OSSL_PARAM *p, uint64_t val)
  491. {
  492. if (p == NULL)
  493. return 0;
  494. p->return_size = 0;
  495. if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
  496. p->return_size = sizeof(uint64_t); /* Expected size */
  497. if (p->data == NULL)
  498. return 1;
  499. switch (p->data_size) {
  500. case sizeof(uint32_t):
  501. if (val <= UINT32_MAX) {
  502. p->return_size = sizeof(uint32_t);
  503. *(uint32_t *)p->data = (uint32_t)val;
  504. return 1;
  505. }
  506. break;
  507. case sizeof(uint64_t):
  508. *(uint64_t *)p->data = val;
  509. return 1;
  510. }
  511. } else if (p->data_type == OSSL_PARAM_INTEGER) {
  512. p->return_size = sizeof(int64_t); /* Expected size */
  513. if (p->data == NULL)
  514. return 1;
  515. switch (p->data_size) {
  516. case sizeof(int32_t):
  517. if (val <= INT32_MAX) {
  518. p->return_size = sizeof(int32_t);
  519. *(int32_t *)p->data = (int32_t)val;
  520. return 1;
  521. }
  522. break;
  523. case sizeof(int64_t):
  524. if (val <= INT64_MAX) {
  525. *(int64_t *)p->data = (int64_t)val;
  526. return 1;
  527. }
  528. break;
  529. }
  530. } else if (p->data_type == OSSL_PARAM_REAL) {
  531. p->return_size = sizeof(double);
  532. switch (p->data_size) {
  533. case sizeof(double):
  534. if ((val >> 53) == 0) { /* 53 significant bits in the mantissa */
  535. *(double *)p->data = (double)val;
  536. return 1;
  537. }
  538. break;
  539. }
  540. }
  541. return 0;
  542. }
  543. OSSL_PARAM OSSL_PARAM_construct_uint64(const char *key, uint64_t *buf)
  544. {
  545. return ossl_param_construct(key, OSSL_PARAM_UNSIGNED_INTEGER, buf,
  546. sizeof(uint64_t));
  547. }
  548. int OSSL_PARAM_get_size_t(const OSSL_PARAM *p, size_t *val)
  549. {
  550. switch (sizeof(size_t)) {
  551. case sizeof(uint32_t):
  552. return OSSL_PARAM_get_uint32(p, (uint32_t *)val);
  553. case sizeof(uint64_t):
  554. return OSSL_PARAM_get_uint64(p, (uint64_t *)val);
  555. }
  556. return 0;
  557. }
  558. int OSSL_PARAM_set_size_t(OSSL_PARAM *p, size_t val)
  559. {
  560. switch (sizeof(size_t)) {
  561. case sizeof(uint32_t):
  562. return OSSL_PARAM_set_uint32(p, (uint32_t)val);
  563. case sizeof(uint64_t):
  564. return OSSL_PARAM_set_uint64(p, (uint64_t)val);
  565. }
  566. return 0;
  567. }
  568. OSSL_PARAM OSSL_PARAM_construct_size_t(const char *key, size_t *buf)
  569. {
  570. return ossl_param_construct(key, OSSL_PARAM_UNSIGNED_INTEGER, buf,
  571. sizeof(size_t));
  572. }
  573. int OSSL_PARAM_get_BN(const OSSL_PARAM *p, BIGNUM **val)
  574. {
  575. BIGNUM *b;
  576. if (val == NULL
  577. || p == NULL
  578. || p->data_type != OSSL_PARAM_UNSIGNED_INTEGER)
  579. return 0;
  580. b = BN_native2bn(p->data, (int)p->data_size, *val);
  581. if (b != NULL) {
  582. *val = b;
  583. return 1;
  584. }
  585. return 0;
  586. }
  587. int OSSL_PARAM_set_BN(OSSL_PARAM *p, const BIGNUM *val)
  588. {
  589. size_t bytes;
  590. if (p == NULL)
  591. return 0;
  592. p->return_size = 0;
  593. if (val == NULL || p->data_type != OSSL_PARAM_UNSIGNED_INTEGER)
  594. return 0;
  595. /* For the moment, only positive values are permitted */
  596. if (BN_is_negative(val))
  597. return 0;
  598. bytes = (size_t)BN_num_bytes(val);
  599. p->return_size = bytes;
  600. if (p->data == NULL)
  601. return 1;
  602. if (p->data_size >= bytes) {
  603. p->return_size = p->data_size;
  604. return BN_bn2nativepad(val, p->data, p->data_size) >= 0;
  605. }
  606. return 0;
  607. }
  608. OSSL_PARAM OSSL_PARAM_construct_BN(const char *key, unsigned char *buf,
  609. size_t bsize)
  610. {
  611. return ossl_param_construct(key, OSSL_PARAM_UNSIGNED_INTEGER,
  612. buf, bsize);
  613. }
  614. int OSSL_PARAM_get_double(const OSSL_PARAM *p, double *val)
  615. {
  616. int64_t i64;
  617. uint64_t u64;
  618. if (val == NULL || p == NULL)
  619. return 0;
  620. if (p->data_type == OSSL_PARAM_REAL) {
  621. switch (p->data_size) {
  622. case sizeof(double):
  623. *val = *(const double *)p->data;
  624. return 1;
  625. }
  626. } else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
  627. switch (p->data_size) {
  628. case sizeof(uint32_t):
  629. *val = *(const uint32_t *)p->data;
  630. return 1;
  631. case sizeof(uint64_t):
  632. u64 = *(const uint64_t *)p->data;
  633. if ((u64 >> 53) == 0) { /* 53 significant bits in the mantissa */
  634. *val = (double)u64;
  635. return 1;
  636. }
  637. break;
  638. }
  639. } else if (p->data_type == OSSL_PARAM_INTEGER) {
  640. switch (p->data_size) {
  641. case sizeof(int32_t):
  642. *val = *(const int32_t *)p->data;
  643. return 1;
  644. case sizeof(int64_t):
  645. i64 = *(const int64_t *)p->data;
  646. u64 = i64 < 0 ? -i64 : i64;
  647. if ((u64 >> 53) == 0) { /* 53 significant bits in the mantissa */
  648. *val = 0.0 + i64;
  649. return 1;
  650. }
  651. break;
  652. }
  653. }
  654. return 0;
  655. }
  656. int OSSL_PARAM_set_double(OSSL_PARAM *p, double val)
  657. {
  658. if (p == NULL)
  659. return 0;
  660. p->return_size = 0;
  661. if (p->data_type == OSSL_PARAM_REAL) {
  662. p->return_size = sizeof(double);
  663. if (p->data == NULL)
  664. return 1;
  665. switch (p->data_size) {
  666. case sizeof(double):
  667. *(double *)p->data = val;
  668. return 1;
  669. }
  670. } else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER
  671. && val == (ossl_uintmax_t)val) {
  672. p->return_size = sizeof(double);
  673. if (p->data == NULL)
  674. return 1;
  675. switch (p->data_size) {
  676. case sizeof(uint32_t):
  677. if (val >= 0 && val <= UINT32_MAX) {
  678. p->return_size = sizeof(uint32_t);
  679. *(uint32_t *)p->data = (uint32_t)val;
  680. return 1;
  681. }
  682. break;
  683. case sizeof(uint64_t):
  684. if (val >= 0 && val <= UINT64_MAX) {
  685. p->return_size = sizeof(uint64_t);
  686. *(uint64_t *)p->data = (uint64_t)val;
  687. return 1;
  688. }
  689. break; }
  690. } else if (p->data_type == OSSL_PARAM_INTEGER && val == (ossl_intmax_t)val) {
  691. p->return_size = sizeof(double);
  692. if (p->data == NULL)
  693. return 1;
  694. switch (p->data_size) {
  695. case sizeof(int32_t):
  696. if (val >= INT32_MIN && val <= INT32_MAX) {
  697. p->return_size = sizeof(int32_t);
  698. *(int32_t *)p->data = (int32_t)val;
  699. return 1;
  700. }
  701. break;
  702. case sizeof(int64_t):
  703. if (val >= INT64_MIN && val <= INT64_MAX) {
  704. p->return_size = sizeof(int64_t);
  705. *(int64_t *)p->data = (int64_t)val;
  706. return 1;
  707. }
  708. break;
  709. }
  710. }
  711. return 0;
  712. }
  713. OSSL_PARAM OSSL_PARAM_construct_double(const char *key, double *buf)
  714. {
  715. return ossl_param_construct(key, OSSL_PARAM_REAL, buf, sizeof(double));
  716. }
  717. static int get_string_internal(const OSSL_PARAM *p, void **val, size_t max_len,
  718. size_t *used_len, unsigned int type)
  719. {
  720. size_t sz;
  721. if (val == NULL || p == NULL || p->data_type != type)
  722. return 0;
  723. sz = p->data_size;
  724. if (used_len != NULL)
  725. *used_len = sz;
  726. if (sz == 0)
  727. return 1;
  728. if (p->data == NULL)
  729. return 0;
  730. if (*val == NULL) {
  731. char *const q = OPENSSL_malloc(sz);
  732. if (q == NULL)
  733. return 0;
  734. *val = q;
  735. memcpy(q, p->data, sz);
  736. return 1;
  737. }
  738. if (max_len < sz)
  739. return 0;
  740. memcpy(*val, p->data, sz);
  741. return 1;
  742. }
  743. int OSSL_PARAM_get_utf8_string(const OSSL_PARAM *p, char **val, size_t max_len)
  744. {
  745. return get_string_internal(p, (void **)val, max_len, NULL,
  746. OSSL_PARAM_UTF8_STRING);
  747. }
  748. int OSSL_PARAM_get_octet_string(const OSSL_PARAM *p, void **val, size_t max_len,
  749. size_t *used_len)
  750. {
  751. return get_string_internal(p, val, max_len, used_len,
  752. OSSL_PARAM_OCTET_STRING);
  753. }
  754. static int set_string_internal(OSSL_PARAM *p, const void *val, size_t len,
  755. unsigned int type)
  756. {
  757. p->return_size = len;
  758. if (p->data == NULL)
  759. return 1;
  760. if (p->data_type != type || p->data_size < len)
  761. return 0;
  762. memcpy(p->data, val, len);
  763. return 1;
  764. }
  765. int OSSL_PARAM_set_utf8_string(OSSL_PARAM *p, const char *val)
  766. {
  767. if (p == NULL)
  768. return 0;
  769. p->return_size = 0;
  770. if (val == NULL)
  771. return 0;
  772. return set_string_internal(p, val, strlen(val) + 1, OSSL_PARAM_UTF8_STRING);
  773. }
  774. int OSSL_PARAM_set_octet_string(OSSL_PARAM *p, const void *val,
  775. size_t len)
  776. {
  777. if (p == NULL)
  778. return 0;
  779. p->return_size = 0;
  780. if (val == NULL)
  781. return 0;
  782. return set_string_internal(p, val, len, OSSL_PARAM_OCTET_STRING);
  783. }
  784. OSSL_PARAM OSSL_PARAM_construct_utf8_string(const char *key, char *buf,
  785. size_t bsize)
  786. {
  787. if (buf != NULL && bsize == 0)
  788. bsize = strlen(buf) + 1;
  789. return ossl_param_construct(key, OSSL_PARAM_UTF8_STRING, buf, bsize);
  790. }
  791. OSSL_PARAM OSSL_PARAM_construct_octet_string(const char *key, void *buf,
  792. size_t bsize)
  793. {
  794. return ossl_param_construct(key, OSSL_PARAM_OCTET_STRING, buf, bsize);
  795. }
  796. static int get_ptr_internal(const OSSL_PARAM *p, const void **val,
  797. size_t *used_len, unsigned int type)
  798. {
  799. if (val == NULL || p == NULL || p->data_type != type)
  800. return 0;
  801. if (used_len != NULL)
  802. *used_len = p->data_size;
  803. *val = *(const void **)p->data;
  804. return 1;
  805. }
  806. int OSSL_PARAM_get_utf8_ptr(const OSSL_PARAM *p, const char **val)
  807. {
  808. return get_ptr_internal(p, (const void **)val, NULL, OSSL_PARAM_UTF8_PTR);
  809. }
  810. int OSSL_PARAM_get_octet_ptr(const OSSL_PARAM *p, const void **val,
  811. size_t *used_len)
  812. {
  813. return get_ptr_internal(p, val, used_len, OSSL_PARAM_OCTET_PTR);
  814. }
  815. static int set_ptr_internal(OSSL_PARAM *p, const void *val,
  816. unsigned int type, size_t len)
  817. {
  818. p->return_size = len;
  819. if (p->data_type != type)
  820. return 0;
  821. if (p->data != NULL)
  822. *(const void **)p->data = val;
  823. return 1;
  824. }
  825. int OSSL_PARAM_set_utf8_ptr(OSSL_PARAM *p, const char *val)
  826. {
  827. if (p == NULL)
  828. return 0;
  829. p->return_size = 0;
  830. return set_ptr_internal(p, val, OSSL_PARAM_UTF8_PTR,
  831. val == NULL ? 0 : strlen(val) + 1);
  832. }
  833. int OSSL_PARAM_set_octet_ptr(OSSL_PARAM *p, const void *val,
  834. size_t used_len)
  835. {
  836. if (p == NULL)
  837. return 0;
  838. p->return_size = 0;
  839. return set_ptr_internal(p, val, OSSL_PARAM_OCTET_PTR, used_len);
  840. }
  841. OSSL_PARAM OSSL_PARAM_construct_utf8_ptr(const char *key, char **buf,
  842. size_t bsize)
  843. {
  844. return ossl_param_construct(key, OSSL_PARAM_UTF8_PTR, buf, bsize);
  845. }
  846. OSSL_PARAM OSSL_PARAM_construct_octet_ptr(const char *key, void **buf,
  847. size_t bsize)
  848. {
  849. return ossl_param_construct(key, OSSL_PARAM_OCTET_PTR, buf, bsize);
  850. }
  851. OSSL_PARAM OSSL_PARAM_construct_end(void)
  852. {
  853. OSSL_PARAM end = OSSL_PARAM_END;
  854. return end;
  855. }