fake_rsaprov.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  1. /*
  2. * Copyright 2021-2025 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. * https://www.openssl.org/source/license.html
  8. * or in the file LICENSE in the source distribution.
  9. */
  10. #include <string.h>
  11. #include <openssl/core_names.h>
  12. #include <openssl/core_object.h>
  13. #include <openssl/rand.h>
  14. #include <openssl/provider.h>
  15. #include "testutil.h"
  16. #include "fake_rsaprov.h"
  17. static OSSL_FUNC_keymgmt_new_fn fake_rsa_keymgmt_new;
  18. static OSSL_FUNC_keymgmt_free_fn fake_rsa_keymgmt_free;
  19. static OSSL_FUNC_keymgmt_has_fn fake_rsa_keymgmt_has;
  20. static OSSL_FUNC_keymgmt_query_operation_name_fn fake_rsa_keymgmt_query;
  21. static OSSL_FUNC_keymgmt_import_fn fake_rsa_keymgmt_import;
  22. static OSSL_FUNC_keymgmt_import_types_fn fake_rsa_keymgmt_imptypes;
  23. static OSSL_FUNC_keymgmt_export_fn fake_rsa_keymgmt_export;
  24. static OSSL_FUNC_keymgmt_export_types_fn fake_rsa_keymgmt_exptypes;
  25. static OSSL_FUNC_keymgmt_load_fn fake_rsa_keymgmt_load;
  26. static int has_selection;
  27. static int imptypes_selection;
  28. static int exptypes_selection;
  29. static int query_id;
  30. static int key_deleted;
  31. unsigned fake_rsa_query_operation_name = 0;
  32. struct fake_rsa_keydata {
  33. int selection;
  34. int status;
  35. };
  36. void fake_rsa_restore_store_state(void)
  37. {
  38. key_deleted = 0;
  39. }
  40. static void *fake_rsa_keymgmt_new(void *provctx)
  41. {
  42. struct fake_rsa_keydata *key;
  43. if (!TEST_ptr(key = OPENSSL_zalloc(sizeof(struct fake_rsa_keydata))))
  44. return NULL;
  45. /* clear test globals */
  46. has_selection = 0;
  47. imptypes_selection = 0;
  48. exptypes_selection = 0;
  49. query_id = 0;
  50. return key;
  51. }
  52. static void fake_rsa_keymgmt_free(void *keydata)
  53. {
  54. OPENSSL_free(keydata);
  55. }
  56. static int fake_rsa_keymgmt_has(const void *key, int selection)
  57. {
  58. /* record global for checking */
  59. has_selection = selection;
  60. return 1;
  61. }
  62. static const char *fake_rsa_keymgmt_query(int id)
  63. {
  64. /* record global for checking */
  65. query_id = id;
  66. return fake_rsa_query_operation_name ? NULL: "RSA";
  67. }
  68. static int fake_rsa_keymgmt_import(void *keydata, int selection,
  69. const OSSL_PARAM *p)
  70. {
  71. struct fake_rsa_keydata *fake_rsa_key = keydata;
  72. /* key was imported */
  73. fake_rsa_key->status = 1;
  74. return 1;
  75. }
  76. static unsigned char fake_rsa_n[] =
  77. "\x00\xAA\x36\xAB\xCE\x88\xAC\xFD\xFF\x55\x52\x3C\x7F\xC4\x52\x3F"
  78. "\x90\xEF\xA0\x0D\xF3\x77\x4A\x25\x9F\x2E\x62\xB4\xC5\xD9\x9C\xB5"
  79. "\xAD\xB3\x00\xA0\x28\x5E\x53\x01\x93\x0E\x0C\x70\xFB\x68\x76\x93"
  80. "\x9C\xE6\x16\xCE\x62\x4A\x11\xE0\x08\x6D\x34\x1E\xBC\xAC\xA0\xA1"
  81. "\xF5";
  82. static unsigned char fake_rsa_e[] = "\x11";
  83. static unsigned char fake_rsa_d[] =
  84. "\x0A\x03\x37\x48\x62\x64\x87\x69\x5F\x5F\x30\xBC\x38\xB9\x8B\x44"
  85. "\xC2\xCD\x2D\xFF\x43\x40\x98\xCD\x20\xD8\xA1\x38\xD0\x90\xBF\x64"
  86. "\x79\x7C\x3F\xA7\xA2\xCD\xCB\x3C\xD1\xE0\xBD\xBA\x26\x54\xB4\xF9"
  87. "\xDF\x8E\x8A\xE5\x9D\x73\x3D\x9F\x33\xB3\x01\x62\x4A\xFD\x1D\x51";
  88. static unsigned char fake_rsa_p[] =
  89. "\x00\xD8\x40\xB4\x16\x66\xB4\x2E\x92\xEA\x0D\xA3\xB4\x32\x04\xB5"
  90. "\xCF\xCE\x33\x52\x52\x4D\x04\x16\xA5\xA4\x41\xE7\x00\xAF\x46\x12"
  91. "\x0D";
  92. static unsigned char fake_rsa_q[] =
  93. "\x00\xC9\x7F\xB1\xF0\x27\xF4\x53\xF6\x34\x12\x33\xEA\xAA\xD1\xD9"
  94. "\x35\x3F\x6C\x42\xD0\x88\x66\xB1\xD0\x5A\x0F\x20\x35\x02\x8B\x9D"
  95. "\x89";
  96. static unsigned char fake_rsa_dmp1[] =
  97. "\x59\x0B\x95\x72\xA2\xC2\xA9\xC4\x06\x05\x9D\xC2\xAB\x2F\x1D\xAF"
  98. "\xEB\x7E\x8B\x4F\x10\xA7\x54\x9E\x8E\xED\xF5\xB4\xFC\xE0\x9E\x05";
  99. static unsigned char fake_rsa_dmq1[] =
  100. "\x00\x8E\x3C\x05\x21\xFE\x15\xE0\xEA\x06\xA3\x6F\xF0\xF1\x0C\x99"
  101. "\x52\xC3\x5B\x7A\x75\x14\xFD\x32\x38\xB8\x0A\xAD\x52\x98\x62\x8D"
  102. "\x51";
  103. static unsigned char fake_rsa_iqmp[] =
  104. "\x36\x3F\xF7\x18\x9D\xA8\xE9\x0B\x1D\x34\x1F\x71\xD0\x9B\x76\xA8"
  105. "\xA9\x43\xE1\x1D\x10\xB2\x4D\x24\x9F\x2D\xEA\xFE\xF8\x0C\x18\x26";
  106. OSSL_PARAM *fake_rsa_key_params(int priv)
  107. {
  108. if (priv) {
  109. OSSL_PARAM params[] = {
  110. OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_N, fake_rsa_n,
  111. sizeof(fake_rsa_n) -1),
  112. OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_E, fake_rsa_e,
  113. sizeof(fake_rsa_e) -1),
  114. OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_D, fake_rsa_d,
  115. sizeof(fake_rsa_d) -1),
  116. OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR1, fake_rsa_p,
  117. sizeof(fake_rsa_p) -1),
  118. OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR2, fake_rsa_q,
  119. sizeof(fake_rsa_q) -1),
  120. OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT1, fake_rsa_dmp1,
  121. sizeof(fake_rsa_dmp1) -1),
  122. OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT2, fake_rsa_dmq1,
  123. sizeof(fake_rsa_dmq1) -1),
  124. OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT1, fake_rsa_iqmp,
  125. sizeof(fake_rsa_iqmp) -1),
  126. OSSL_PARAM_END
  127. };
  128. return OSSL_PARAM_dup(params);
  129. } else {
  130. OSSL_PARAM params[] = {
  131. OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_N, fake_rsa_n,
  132. sizeof(fake_rsa_n) -1),
  133. OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_E, fake_rsa_e,
  134. sizeof(fake_rsa_e) -1),
  135. OSSL_PARAM_END
  136. };
  137. return OSSL_PARAM_dup(params);
  138. }
  139. }
  140. static int fake_rsa_keymgmt_export(void *keydata, int selection,
  141. OSSL_CALLBACK *param_callback, void *cbarg)
  142. {
  143. OSSL_PARAM *params = NULL;
  144. int ret;
  145. if (selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY)
  146. return 0;
  147. if (!TEST_ptr(params = fake_rsa_key_params(0)))
  148. return 0;
  149. ret = param_callback(params, cbarg);
  150. OSSL_PARAM_free(params);
  151. return ret;
  152. }
  153. static const OSSL_PARAM fake_rsa_import_key_types[] = {
  154. OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_N, NULL, 0),
  155. OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_E, NULL, 0),
  156. OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_D, NULL, 0),
  157. OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR1, NULL, 0),
  158. OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR2, NULL, 0),
  159. OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT1, NULL, 0),
  160. OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT2, NULL, 0),
  161. OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT1, NULL, 0),
  162. OSSL_PARAM_END
  163. };
  164. static const OSSL_PARAM *fake_rsa_keymgmt_imptypes(int selection)
  165. {
  166. /* record global for checking */
  167. imptypes_selection = selection;
  168. return fake_rsa_import_key_types;
  169. }
  170. static const OSSL_PARAM fake_rsa_export_key_types[] = {
  171. OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_N, NULL, 0),
  172. OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_E, NULL, 0),
  173. OSSL_PARAM_END
  174. };
  175. static const OSSL_PARAM *fake_rsa_keymgmt_exptypes(int selection)
  176. {
  177. /* record global for checking */
  178. exptypes_selection = selection;
  179. return fake_rsa_export_key_types;
  180. }
  181. static void *fake_rsa_keymgmt_load(const void *reference, size_t reference_sz)
  182. {
  183. struct fake_rsa_keydata *key = NULL;
  184. if (reference_sz != sizeof(*key))
  185. return NULL;
  186. key = *(struct fake_rsa_keydata **)reference;
  187. if (key->status != 1)
  188. return NULL;
  189. /* detach the reference */
  190. *(struct fake_rsa_keydata **)reference = NULL;
  191. return key;
  192. }
  193. static void *fake_rsa_gen_init(void *provctx, int selection,
  194. const OSSL_PARAM params[])
  195. {
  196. unsigned char *gctx = NULL;
  197. if (!TEST_ptr(gctx = OPENSSL_malloc(1)))
  198. return NULL;
  199. *gctx = 1;
  200. return gctx;
  201. }
  202. static void *fake_rsa_gen(void *genctx, OSSL_CALLBACK *osslcb, void *cbarg)
  203. {
  204. unsigned char *gctx = genctx;
  205. static const unsigned char inited[] = { 1 };
  206. struct fake_rsa_keydata *keydata;
  207. if (!TEST_ptr(gctx)
  208. || !TEST_mem_eq(gctx, sizeof(*gctx), inited, sizeof(inited)))
  209. return NULL;
  210. if (!TEST_ptr(keydata = fake_rsa_keymgmt_new(NULL)))
  211. return NULL;
  212. keydata->status = 2;
  213. return keydata;
  214. }
  215. static void fake_rsa_gen_cleanup(void *genctx)
  216. {
  217. OPENSSL_free(genctx);
  218. }
  219. static const OSSL_DISPATCH fake_rsa_keymgmt_funcs[] = {
  220. { OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))fake_rsa_keymgmt_new },
  221. { OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))fake_rsa_keymgmt_free} ,
  222. { OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))fake_rsa_keymgmt_has },
  223. { OSSL_FUNC_KEYMGMT_QUERY_OPERATION_NAME,
  224. (void (*)(void))fake_rsa_keymgmt_query },
  225. { OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))fake_rsa_keymgmt_import },
  226. { OSSL_FUNC_KEYMGMT_IMPORT_TYPES,
  227. (void (*)(void))fake_rsa_keymgmt_imptypes },
  228. { OSSL_FUNC_KEYMGMT_EXPORT, (void (*)(void))fake_rsa_keymgmt_export },
  229. { OSSL_FUNC_KEYMGMT_EXPORT_TYPES,
  230. (void (*)(void))fake_rsa_keymgmt_exptypes },
  231. { OSSL_FUNC_KEYMGMT_LOAD, (void (*)(void))fake_rsa_keymgmt_load },
  232. { OSSL_FUNC_KEYMGMT_GEN_INIT, (void (*)(void))fake_rsa_gen_init },
  233. { OSSL_FUNC_KEYMGMT_GEN, (void (*)(void))fake_rsa_gen },
  234. { OSSL_FUNC_KEYMGMT_GEN_CLEANUP, (void (*)(void))fake_rsa_gen_cleanup },
  235. OSSL_DISPATCH_END
  236. };
  237. static const OSSL_ALGORITHM fake_rsa_keymgmt_algs[] = {
  238. { "RSA:rsaEncryption", "provider=fake-rsa", fake_rsa_keymgmt_funcs, "Fake RSA Key Management" },
  239. { NULL, NULL, NULL, NULL }
  240. };
  241. static OSSL_FUNC_signature_newctx_fn fake_rsa_sig_newctx;
  242. static OSSL_FUNC_signature_freectx_fn fake_rsa_sig_freectx;
  243. static OSSL_FUNC_signature_sign_init_fn fake_rsa_sig_sign_init;
  244. static OSSL_FUNC_signature_sign_fn fake_rsa_sig_sign;
  245. static void *fake_rsa_sig_newctx(void *provctx, const char *propq)
  246. {
  247. unsigned char *sigctx = OPENSSL_zalloc(1);
  248. TEST_ptr(sigctx);
  249. return sigctx;
  250. }
  251. static void fake_rsa_sig_freectx(void *sigctx)
  252. {
  253. OPENSSL_free(sigctx);
  254. }
  255. static int fake_rsa_sig_sign_init(void *ctx, void *provkey,
  256. const OSSL_PARAM params[])
  257. {
  258. unsigned char *sigctx = ctx;
  259. struct fake_rsa_keydata *keydata = provkey;
  260. /* we must have a ctx */
  261. if (!TEST_ptr(sigctx))
  262. return 0;
  263. /* we must have some initialized key */
  264. if (!TEST_ptr(keydata) || !TEST_int_gt(keydata->status, 0))
  265. return 0;
  266. /* record that sign init was called */
  267. *sigctx = 1;
  268. return 1;
  269. }
  270. static int fake_rsa_sig_sign(void *ctx, unsigned char *sig,
  271. size_t *siglen, size_t sigsize,
  272. const unsigned char *tbs, size_t tbslen)
  273. {
  274. unsigned char *sigctx = ctx;
  275. /* we must have a ctx and init was called upon it */
  276. if (!TEST_ptr(sigctx) || !TEST_int_eq(*sigctx, 1))
  277. return 0;
  278. *siglen = 256;
  279. /* record that the real sign operation was called */
  280. if (sig != NULL) {
  281. if (!TEST_int_ge(sigsize, *siglen))
  282. return 0;
  283. *sigctx = 2;
  284. /* produce a fake signature */
  285. memset(sig, 'a', *siglen);
  286. }
  287. return 1;
  288. }
  289. #define FAKE_DGSTSGN_SIGN 0x01
  290. #define FAKE_DGSTSGN_VERIFY 0x02
  291. #define FAKE_DGSTSGN_UPDATED 0x04
  292. #define FAKE_DGSTSGN_FINALISED 0x08
  293. #define FAKE_DGSTSGN_NO_DUP 0xA0
  294. static void *fake_rsa_sig_dupctx(void *ctx)
  295. {
  296. unsigned char *sigctx = ctx;
  297. unsigned char *newctx;
  298. if ((*sigctx & FAKE_DGSTSGN_NO_DUP) != 0)
  299. return NULL;
  300. if (!TEST_ptr(newctx = OPENSSL_zalloc(1)))
  301. return NULL;
  302. *newctx = *sigctx;
  303. return newctx;
  304. }
  305. static int fake_rsa_dgstsgnvfy_init(void *ctx, unsigned char type,
  306. void *provkey, const OSSL_PARAM params[])
  307. {
  308. unsigned char *sigctx = ctx;
  309. struct fake_rsa_keydata *keydata = provkey;
  310. /* we must have a ctx */
  311. if (!TEST_ptr(sigctx))
  312. return 0;
  313. /* we must have some initialized key */
  314. if (!TEST_ptr(keydata) || !TEST_int_gt(keydata->status, 0))
  315. return 0;
  316. /* record that sign/verify init was called */
  317. *sigctx = type;
  318. if (params) {
  319. const OSSL_PARAM *p;
  320. int dup;
  321. p = OSSL_PARAM_locate_const(params, "NO_DUP");
  322. if (p != NULL) {
  323. if (OSSL_PARAM_get_int(p, &dup)) {
  324. *sigctx |= FAKE_DGSTSGN_NO_DUP;
  325. }
  326. }
  327. }
  328. return 1;
  329. }
  330. static int fake_rsa_dgstsgn_init(void *ctx, const char *mdname,
  331. void *provkey, const OSSL_PARAM params[])
  332. {
  333. return fake_rsa_dgstsgnvfy_init(ctx, FAKE_DGSTSGN_SIGN, provkey, params);
  334. }
  335. static int fake_rsa_dgstvfy_init(void *ctx, const char *mdname,
  336. void *provkey, const OSSL_PARAM params[])
  337. {
  338. return fake_rsa_dgstsgnvfy_init(ctx, FAKE_DGSTSGN_VERIFY, provkey, params);
  339. }
  340. static int fake_rsa_dgstsgnvfy_update(void *ctx, const unsigned char *data,
  341. size_t datalen)
  342. {
  343. unsigned char *sigctx = ctx;
  344. /* we must have a ctx */
  345. if (!TEST_ptr(sigctx))
  346. return 0;
  347. if (*sigctx == 0 || (*sigctx & FAKE_DGSTSGN_FINALISED) != 0)
  348. return 0;
  349. *sigctx |= FAKE_DGSTSGN_UPDATED;
  350. return 1;
  351. }
  352. static int fake_rsa_dgstsgnvfy_final(void *ctx, unsigned char *sig,
  353. size_t *siglen, size_t sigsize)
  354. {
  355. unsigned char *sigctx = ctx;
  356. /* we must have a ctx */
  357. if (!TEST_ptr(sigctx))
  358. return 0;
  359. if (*sigctx == 0 || (*sigctx & FAKE_DGSTSGN_FINALISED) != 0)
  360. return 0;
  361. if ((*sigctx & FAKE_DGSTSGN_SIGN) != 0 && (siglen == NULL))
  362. return 0;
  363. if ((*sigctx & FAKE_DGSTSGN_VERIFY) != 0 && (siglen != NULL))
  364. return 0;
  365. /* this is sign op */
  366. if (siglen) {
  367. *siglen = 256;
  368. /* record that the real sign operation was called */
  369. if (sig != NULL) {
  370. if (!TEST_int_ge(sigsize, *siglen))
  371. return 0;
  372. /* produce a fake signature */
  373. memset(sig, 'a', *siglen);
  374. }
  375. }
  376. /* simulate inability to duplicate context and finalise it */
  377. if ((*sigctx & FAKE_DGSTSGN_NO_DUP) != 0) {
  378. *sigctx |= FAKE_DGSTSGN_FINALISED;
  379. }
  380. return 1;
  381. }
  382. static int fake_rsa_dgstvfy_final(void *ctx, unsigned char *sig,
  383. size_t siglen)
  384. {
  385. return fake_rsa_dgstsgnvfy_final(ctx, sig, NULL, siglen);
  386. }
  387. static int fake_rsa_dgstsgn(void *ctx, unsigned char *sig, size_t *siglen,
  388. size_t sigsize, const unsigned char *tbs,
  389. size_t tbslen)
  390. {
  391. if (!fake_rsa_dgstsgnvfy_update(ctx, tbs, tbslen))
  392. return 0;
  393. return fake_rsa_dgstsgnvfy_final(ctx, sig, siglen, sigsize);
  394. }
  395. static int fake_rsa_dgstvfy(void *ctx, unsigned char *sig, size_t siglen,
  396. const unsigned char *tbv, size_t tbvlen)
  397. {
  398. if (!fake_rsa_dgstsgnvfy_update(ctx, tbv, tbvlen))
  399. return 0;
  400. return fake_rsa_dgstvfy_final(ctx, sig, siglen);
  401. }
  402. static const OSSL_DISPATCH fake_rsa_sig_funcs[] = {
  403. { OSSL_FUNC_SIGNATURE_NEWCTX, (void (*)(void))fake_rsa_sig_newctx },
  404. { OSSL_FUNC_SIGNATURE_FREECTX, (void (*)(void))fake_rsa_sig_freectx },
  405. { OSSL_FUNC_SIGNATURE_SIGN_INIT, (void (*)(void))fake_rsa_sig_sign_init },
  406. { OSSL_FUNC_SIGNATURE_SIGN, (void (*)(void))fake_rsa_sig_sign },
  407. { OSSL_FUNC_SIGNATURE_DUPCTX, (void (*)(void))fake_rsa_sig_dupctx },
  408. { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_INIT,
  409. (void (*)(void))fake_rsa_dgstsgn_init },
  410. { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_UPDATE,
  411. (void (*)(void))fake_rsa_dgstsgnvfy_update },
  412. { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_FINAL,
  413. (void (*)(void))fake_rsa_dgstsgnvfy_final },
  414. { OSSL_FUNC_SIGNATURE_DIGEST_SIGN,
  415. (void (*)(void))fake_rsa_dgstsgn },
  416. { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_INIT,
  417. (void (*)(void))fake_rsa_dgstvfy_init },
  418. { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_UPDATE,
  419. (void (*)(void))fake_rsa_dgstsgnvfy_update },
  420. { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_FINAL,
  421. (void (*)(void))fake_rsa_dgstvfy_final },
  422. { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY,
  423. (void (*)(void))fake_rsa_dgstvfy },
  424. OSSL_DISPATCH_END
  425. };
  426. static const OSSL_ALGORITHM fake_rsa_sig_algs[] = {
  427. { "RSA:rsaEncryption", "provider=fake-rsa", fake_rsa_sig_funcs, "Fake RSA Signature" },
  428. { NULL, NULL, NULL, NULL }
  429. };
  430. static OSSL_FUNC_store_open_fn fake_rsa_st_open;
  431. static OSSL_FUNC_store_open_ex_fn fake_rsa_st_open_ex;
  432. static OSSL_FUNC_store_settable_ctx_params_fn fake_rsa_st_settable_ctx_params;
  433. static OSSL_FUNC_store_set_ctx_params_fn fake_rsa_st_set_ctx_params;
  434. static OSSL_FUNC_store_load_fn fake_rsa_st_load;
  435. static OSSL_FUNC_store_eof_fn fake_rsa_st_eof;
  436. static OSSL_FUNC_store_close_fn fake_rsa_st_close;
  437. static OSSL_FUNC_store_delete_fn fake_rsa_st_delete;
  438. static const char fake_rsa_scheme[] = "fake_rsa:";
  439. static const char fake_rsa_openpwtest[] = "fake_rsa:openpwtest";
  440. static const char fake_rsa_prompt[] = "Fake Prompt Info";
  441. static void *fake_rsa_st_open_ex(void *provctx, const char *uri,
  442. const OSSL_PARAM params[],
  443. OSSL_PASSPHRASE_CALLBACK *pw_cb,
  444. void *pw_cbarg)
  445. {
  446. unsigned char *storectx = NULL;
  447. /* First check whether the uri is ours */
  448. if (strncmp(uri, fake_rsa_scheme, sizeof(fake_rsa_scheme) - 1) != 0)
  449. return NULL;
  450. if (strncmp(uri, fake_rsa_openpwtest,
  451. sizeof(fake_rsa_openpwtest) - 1) == 0) {
  452. const char *pw_check = FAKE_PASSPHRASE;
  453. char fakepw[sizeof(FAKE_PASSPHRASE) + 1] = { 0 };
  454. size_t fakepw_len = 0;
  455. OSSL_PARAM pw_params[2] = {
  456. OSSL_PARAM_utf8_string(OSSL_PASSPHRASE_PARAM_INFO,
  457. (void *)fake_rsa_prompt,
  458. sizeof(fake_rsa_prompt) - 1),
  459. OSSL_PARAM_END,
  460. };
  461. if (pw_cb == NULL) {
  462. return NULL;
  463. }
  464. if (!pw_cb(fakepw, sizeof(fakepw), &fakepw_len, pw_params, pw_cbarg)) {
  465. TEST_info("fake_rsa_open_ex failed passphrase callback");
  466. return NULL;
  467. }
  468. if (strncmp(pw_check, fakepw, sizeof(pw_check) - 1) != 0) {
  469. TEST_info("fake_rsa_open_ex failed passphrase check");
  470. return NULL;
  471. }
  472. }
  473. storectx = OPENSSL_zalloc(1);
  474. if (!TEST_ptr(storectx))
  475. return NULL;
  476. TEST_info("fake_rsa_open_ex called");
  477. return storectx;
  478. }
  479. static void *fake_rsa_st_open(void *provctx, const char *uri)
  480. {
  481. unsigned char *storectx = NULL;
  482. storectx = fake_rsa_st_open_ex(provctx, uri, NULL, NULL, NULL);
  483. TEST_info("fake_rsa_open called");
  484. return storectx;
  485. }
  486. static const OSSL_PARAM *fake_rsa_st_settable_ctx_params(void *provctx)
  487. {
  488. static const OSSL_PARAM known_settable_ctx_params[] = {
  489. OSSL_PARAM_END
  490. };
  491. return known_settable_ctx_params;
  492. }
  493. static int fake_rsa_st_set_ctx_params(void *loaderctx,
  494. const OSSL_PARAM params[])
  495. {
  496. return 1;
  497. }
  498. static int fake_rsa_st_load(void *loaderctx,
  499. OSSL_CALLBACK *object_cb, void *object_cbarg,
  500. OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg)
  501. {
  502. unsigned char *storectx = loaderctx;
  503. OSSL_PARAM params[4];
  504. int object_type = OSSL_OBJECT_PKEY;
  505. struct fake_rsa_keydata *key = NULL;
  506. int rv = 0;
  507. switch (*storectx) {
  508. case 0:
  509. if (key_deleted == 1) {
  510. *storectx = 1;
  511. break;
  512. }
  513. /* Construct a new key using our keymgmt functions */
  514. if (!TEST_ptr(key = fake_rsa_keymgmt_new(NULL)))
  515. break;
  516. if (!TEST_int_gt(fake_rsa_keymgmt_import(key, 0, NULL), 0))
  517. break;
  518. params[0] =
  519. OSSL_PARAM_construct_int(OSSL_OBJECT_PARAM_TYPE, &object_type);
  520. params[1] =
  521. OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_TYPE,
  522. "RSA", 0);
  523. /* The address of the key becomes the octet string */
  524. params[2] =
  525. OSSL_PARAM_construct_octet_string(OSSL_OBJECT_PARAM_REFERENCE,
  526. &key, sizeof(*key));
  527. params[3] = OSSL_PARAM_construct_end();
  528. rv = object_cb(params, object_cbarg);
  529. *storectx = 1;
  530. break;
  531. case 2:
  532. TEST_info("fake_rsa_load() called in error state");
  533. break;
  534. default:
  535. TEST_info("fake_rsa_load() called in eof state");
  536. break;
  537. }
  538. TEST_info("fake_rsa_load called - rv: %d", rv);
  539. if (rv == 0 && key_deleted == 0) {
  540. fake_rsa_keymgmt_free(key);
  541. *storectx = 2;
  542. }
  543. return rv;
  544. }
  545. static int fake_rsa_st_delete(void *loaderctx, const char *uri,
  546. const OSSL_PARAM params[],
  547. OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg)
  548. {
  549. key_deleted = 1;
  550. return 1;
  551. }
  552. static int fake_rsa_st_eof(void *loaderctx)
  553. {
  554. unsigned char *storectx = loaderctx;
  555. /* just one key for now in the fake_rsa store */
  556. return *storectx != 0;
  557. }
  558. static int fake_rsa_st_close(void *loaderctx)
  559. {
  560. OPENSSL_free(loaderctx);
  561. return 1;
  562. }
  563. static const OSSL_DISPATCH fake_rsa_store_funcs[] = {
  564. { OSSL_FUNC_STORE_OPEN, (void (*)(void))fake_rsa_st_open },
  565. { OSSL_FUNC_STORE_OPEN_EX, (void (*)(void))fake_rsa_st_open_ex },
  566. { OSSL_FUNC_STORE_SETTABLE_CTX_PARAMS,
  567. (void (*)(void))fake_rsa_st_settable_ctx_params },
  568. { OSSL_FUNC_STORE_SET_CTX_PARAMS, (void (*)(void))fake_rsa_st_set_ctx_params },
  569. { OSSL_FUNC_STORE_LOAD, (void (*)(void))fake_rsa_st_load },
  570. { OSSL_FUNC_STORE_EOF, (void (*)(void))fake_rsa_st_eof },
  571. { OSSL_FUNC_STORE_CLOSE, (void (*)(void))fake_rsa_st_close },
  572. { OSSL_FUNC_STORE_DELETE, (void (*)(void))fake_rsa_st_delete },
  573. OSSL_DISPATCH_END,
  574. };
  575. static const OSSL_ALGORITHM fake_rsa_store_algs[] = {
  576. { "fake_rsa", "provider=fake-rsa", fake_rsa_store_funcs },
  577. { NULL, NULL, NULL }
  578. };
  579. static const OSSL_ALGORITHM *fake_rsa_query(void *provctx,
  580. int operation_id,
  581. int *no_cache)
  582. {
  583. *no_cache = 0;
  584. switch (operation_id) {
  585. case OSSL_OP_SIGNATURE:
  586. return fake_rsa_sig_algs;
  587. case OSSL_OP_KEYMGMT:
  588. return fake_rsa_keymgmt_algs;
  589. case OSSL_OP_STORE:
  590. return fake_rsa_store_algs;
  591. }
  592. return NULL;
  593. }
  594. /* Functions we provide to the core */
  595. static const OSSL_DISPATCH fake_rsa_method[] = {
  596. { OSSL_FUNC_PROVIDER_TEARDOWN, (void (*)(void))OSSL_LIB_CTX_free },
  597. { OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))fake_rsa_query },
  598. OSSL_DISPATCH_END
  599. };
  600. static int fake_rsa_provider_init(const OSSL_CORE_HANDLE *handle,
  601. const OSSL_DISPATCH *in,
  602. const OSSL_DISPATCH **out, void **provctx)
  603. {
  604. if (!TEST_ptr(*provctx = OSSL_LIB_CTX_new()))
  605. return 0;
  606. *out = fake_rsa_method;
  607. return 1;
  608. }
  609. OSSL_PROVIDER *fake_rsa_start(OSSL_LIB_CTX *libctx)
  610. {
  611. OSSL_PROVIDER *p;
  612. if (!TEST_true(OSSL_PROVIDER_add_builtin(libctx, "fake-rsa",
  613. fake_rsa_provider_init))
  614. || !TEST_ptr(p = OSSL_PROVIDER_try_load(libctx, "fake-rsa", 1)))
  615. return NULL;
  616. return p;
  617. }
  618. void fake_rsa_finish(OSSL_PROVIDER *p)
  619. {
  620. OSSL_PROVIDER_unload(p);
  621. }