asymcipher.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. /*
  2. * Copyright 2006-2021 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <openssl/objects.h>
  12. #include <openssl/evp.h>
  13. #include "internal/cryptlib.h"
  14. #include "internal/provider.h"
  15. #include "internal/core.h"
  16. #include "crypto/evp.h"
  17. #include "evp_local.h"
  18. static int evp_pkey_asym_cipher_init(EVP_PKEY_CTX *ctx, int operation,
  19. const OSSL_PARAM params[])
  20. {
  21. int ret = 0;
  22. void *provkey = NULL;
  23. EVP_ASYM_CIPHER *cipher = NULL;
  24. EVP_KEYMGMT *tmp_keymgmt = NULL;
  25. const char *supported_ciph = NULL;
  26. if (ctx == NULL) {
  27. ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  28. return -2;
  29. }
  30. evp_pkey_ctx_free_old_ops(ctx);
  31. ctx->operation = operation;
  32. ERR_set_mark();
  33. if (evp_pkey_ctx_is_legacy(ctx))
  34. goto legacy;
  35. /*
  36. * Ensure that the key is provided, either natively, or as a cached export.
  37. * If not, go legacy
  38. */
  39. tmp_keymgmt = ctx->keymgmt;
  40. provkey = evp_pkey_export_to_provider(ctx->pkey, ctx->libctx,
  41. &tmp_keymgmt, ctx->propquery);
  42. if (provkey == NULL)
  43. goto legacy;
  44. if (!EVP_KEYMGMT_up_ref(tmp_keymgmt)) {
  45. ERR_clear_last_mark();
  46. ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
  47. goto err;
  48. }
  49. EVP_KEYMGMT_free(ctx->keymgmt);
  50. ctx->keymgmt = tmp_keymgmt;
  51. if (ctx->keymgmt->query_operation_name != NULL)
  52. supported_ciph =
  53. ctx->keymgmt->query_operation_name(OSSL_OP_ASYM_CIPHER);
  54. /*
  55. * If we didn't get a supported ciph, assume there is one with the
  56. * same name as the key type.
  57. */
  58. if (supported_ciph == NULL)
  59. supported_ciph = ctx->keytype;
  60. /*
  61. * Because we cleared out old ops, we shouldn't need to worry about
  62. * checking if cipher is already there.
  63. */
  64. cipher =
  65. EVP_ASYM_CIPHER_fetch(ctx->libctx, supported_ciph, ctx->propquery);
  66. if (cipher == NULL
  67. || (EVP_KEYMGMT_get0_provider(ctx->keymgmt)
  68. != EVP_ASYM_CIPHER_get0_provider(cipher))) {
  69. /*
  70. * We don't need to free ctx->keymgmt here, as it's not necessarily
  71. * tied to this operation. It will be freed by EVP_PKEY_CTX_free().
  72. */
  73. EVP_ASYM_CIPHER_free(cipher);
  74. goto legacy;
  75. }
  76. /*
  77. * If we don't have the full support we need with provided methods,
  78. * let's go see if legacy does.
  79. */
  80. ERR_pop_to_mark();
  81. /* No more legacy from here down to legacy: */
  82. ctx->op.ciph.cipher = cipher;
  83. ctx->op.ciph.algctx = cipher->newctx(ossl_provider_ctx(cipher->prov));
  84. if (ctx->op.ciph.algctx == NULL) {
  85. /* The provider key can stay in the cache */
  86. ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
  87. goto err;
  88. }
  89. switch (operation) {
  90. case EVP_PKEY_OP_ENCRYPT:
  91. if (cipher->encrypt_init == NULL) {
  92. ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  93. ret = -2;
  94. goto err;
  95. }
  96. ret = cipher->encrypt_init(ctx->op.ciph.algctx, provkey, params);
  97. break;
  98. case EVP_PKEY_OP_DECRYPT:
  99. if (cipher->decrypt_init == NULL) {
  100. ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  101. ret = -2;
  102. goto err;
  103. }
  104. ret = cipher->decrypt_init(ctx->op.ciph.algctx, provkey, params);
  105. break;
  106. default:
  107. ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
  108. goto err;
  109. }
  110. if (ret <= 0)
  111. goto err;
  112. return 1;
  113. legacy:
  114. /*
  115. * If we don't have the full support we need with provided methods,
  116. * let's go see if legacy does.
  117. */
  118. ERR_pop_to_mark();
  119. if (ctx->pmeth == NULL || ctx->pmeth->encrypt == NULL) {
  120. ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  121. return -2;
  122. }
  123. switch(ctx->operation) {
  124. case EVP_PKEY_OP_ENCRYPT:
  125. if (ctx->pmeth->encrypt_init == NULL)
  126. return 1;
  127. ret = ctx->pmeth->encrypt_init(ctx);
  128. break;
  129. case EVP_PKEY_OP_DECRYPT:
  130. if (ctx->pmeth->decrypt_init == NULL)
  131. return 1;
  132. ret = ctx->pmeth->decrypt_init(ctx);
  133. break;
  134. default:
  135. ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
  136. ret = -1;
  137. }
  138. err:
  139. if (ret <= 0) {
  140. evp_pkey_ctx_free_old_ops(ctx);
  141. ctx->operation = EVP_PKEY_OP_UNDEFINED;
  142. }
  143. return ret;
  144. }
  145. int EVP_PKEY_encrypt_init(EVP_PKEY_CTX *ctx)
  146. {
  147. return evp_pkey_asym_cipher_init(ctx, EVP_PKEY_OP_ENCRYPT, NULL);
  148. }
  149. int EVP_PKEY_encrypt_init_ex(EVP_PKEY_CTX *ctx, const OSSL_PARAM params[])
  150. {
  151. return evp_pkey_asym_cipher_init(ctx, EVP_PKEY_OP_ENCRYPT, params);
  152. }
  153. int EVP_PKEY_encrypt(EVP_PKEY_CTX *ctx,
  154. unsigned char *out, size_t *outlen,
  155. const unsigned char *in, size_t inlen)
  156. {
  157. int ret;
  158. if (ctx == NULL) {
  159. ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  160. return -2;
  161. }
  162. if (ctx->operation != EVP_PKEY_OP_ENCRYPT) {
  163. ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_INITIALIZED);
  164. return -1;
  165. }
  166. if (ctx->op.ciph.algctx == NULL)
  167. goto legacy;
  168. ret = ctx->op.ciph.cipher->encrypt(ctx->op.ciph.algctx, out, outlen,
  169. (out == NULL ? 0 : *outlen), in, inlen);
  170. return ret;
  171. legacy:
  172. if (ctx->pmeth == NULL || ctx->pmeth->encrypt == NULL) {
  173. ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  174. return -2;
  175. }
  176. M_check_autoarg(ctx, out, outlen, EVP_F_EVP_PKEY_ENCRYPT)
  177. return ctx->pmeth->encrypt(ctx, out, outlen, in, inlen);
  178. }
  179. int EVP_PKEY_decrypt_init(EVP_PKEY_CTX *ctx)
  180. {
  181. return evp_pkey_asym_cipher_init(ctx, EVP_PKEY_OP_DECRYPT, NULL);
  182. }
  183. int EVP_PKEY_decrypt_init_ex(EVP_PKEY_CTX *ctx, const OSSL_PARAM params[])
  184. {
  185. return evp_pkey_asym_cipher_init(ctx, EVP_PKEY_OP_DECRYPT, params);
  186. }
  187. int EVP_PKEY_decrypt(EVP_PKEY_CTX *ctx,
  188. unsigned char *out, size_t *outlen,
  189. const unsigned char *in, size_t inlen)
  190. {
  191. int ret;
  192. if (ctx == NULL) {
  193. ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  194. return -2;
  195. }
  196. if (ctx->operation != EVP_PKEY_OP_DECRYPT) {
  197. ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_INITIALIZED);
  198. return -1;
  199. }
  200. if (ctx->op.ciph.algctx == NULL)
  201. goto legacy;
  202. ret = ctx->op.ciph.cipher->decrypt(ctx->op.ciph.algctx, out, outlen,
  203. (out == NULL ? 0 : *outlen), in, inlen);
  204. return ret;
  205. legacy:
  206. if (ctx->pmeth == NULL || ctx->pmeth->decrypt == NULL) {
  207. ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  208. return -2;
  209. }
  210. M_check_autoarg(ctx, out, outlen, EVP_F_EVP_PKEY_DECRYPT)
  211. return ctx->pmeth->decrypt(ctx, out, outlen, in, inlen);
  212. }
  213. static EVP_ASYM_CIPHER *evp_asym_cipher_new(OSSL_PROVIDER *prov)
  214. {
  215. EVP_ASYM_CIPHER *cipher = OPENSSL_zalloc(sizeof(EVP_ASYM_CIPHER));
  216. if (cipher == NULL) {
  217. ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
  218. return NULL;
  219. }
  220. cipher->lock = CRYPTO_THREAD_lock_new();
  221. if (cipher->lock == NULL) {
  222. ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
  223. OPENSSL_free(cipher);
  224. return NULL;
  225. }
  226. cipher->prov = prov;
  227. ossl_provider_up_ref(prov);
  228. cipher->refcnt = 1;
  229. return cipher;
  230. }
  231. static void *evp_asym_cipher_from_algorithm(int name_id,
  232. const OSSL_ALGORITHM *algodef,
  233. OSSL_PROVIDER *prov)
  234. {
  235. const OSSL_DISPATCH *fns = algodef->implementation;
  236. EVP_ASYM_CIPHER *cipher = NULL;
  237. int ctxfncnt = 0, encfncnt = 0, decfncnt = 0;
  238. int gparamfncnt = 0, sparamfncnt = 0;
  239. if ((cipher = evp_asym_cipher_new(prov)) == NULL) {
  240. ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
  241. goto err;
  242. }
  243. cipher->name_id = name_id;
  244. if ((cipher->type_name = ossl_algorithm_get1_first_name(algodef)) == NULL)
  245. goto err;
  246. cipher->description = algodef->algorithm_description;
  247. for (; fns->function_id != 0; fns++) {
  248. switch (fns->function_id) {
  249. case OSSL_FUNC_ASYM_CIPHER_NEWCTX:
  250. if (cipher->newctx != NULL)
  251. break;
  252. cipher->newctx = OSSL_FUNC_asym_cipher_newctx(fns);
  253. ctxfncnt++;
  254. break;
  255. case OSSL_FUNC_ASYM_CIPHER_ENCRYPT_INIT:
  256. if (cipher->encrypt_init != NULL)
  257. break;
  258. cipher->encrypt_init = OSSL_FUNC_asym_cipher_encrypt_init(fns);
  259. encfncnt++;
  260. break;
  261. case OSSL_FUNC_ASYM_CIPHER_ENCRYPT:
  262. if (cipher->encrypt != NULL)
  263. break;
  264. cipher->encrypt = OSSL_FUNC_asym_cipher_encrypt(fns);
  265. encfncnt++;
  266. break;
  267. case OSSL_FUNC_ASYM_CIPHER_DECRYPT_INIT:
  268. if (cipher->decrypt_init != NULL)
  269. break;
  270. cipher->decrypt_init = OSSL_FUNC_asym_cipher_decrypt_init(fns);
  271. decfncnt++;
  272. break;
  273. case OSSL_FUNC_ASYM_CIPHER_DECRYPT:
  274. if (cipher->decrypt != NULL)
  275. break;
  276. cipher->decrypt = OSSL_FUNC_asym_cipher_decrypt(fns);
  277. decfncnt++;
  278. break;
  279. case OSSL_FUNC_ASYM_CIPHER_FREECTX:
  280. if (cipher->freectx != NULL)
  281. break;
  282. cipher->freectx = OSSL_FUNC_asym_cipher_freectx(fns);
  283. ctxfncnt++;
  284. break;
  285. case OSSL_FUNC_ASYM_CIPHER_DUPCTX:
  286. if (cipher->dupctx != NULL)
  287. break;
  288. cipher->dupctx = OSSL_FUNC_asym_cipher_dupctx(fns);
  289. break;
  290. case OSSL_FUNC_ASYM_CIPHER_GET_CTX_PARAMS:
  291. if (cipher->get_ctx_params != NULL)
  292. break;
  293. cipher->get_ctx_params
  294. = OSSL_FUNC_asym_cipher_get_ctx_params(fns);
  295. gparamfncnt++;
  296. break;
  297. case OSSL_FUNC_ASYM_CIPHER_GETTABLE_CTX_PARAMS:
  298. if (cipher->gettable_ctx_params != NULL)
  299. break;
  300. cipher->gettable_ctx_params
  301. = OSSL_FUNC_asym_cipher_gettable_ctx_params(fns);
  302. gparamfncnt++;
  303. break;
  304. case OSSL_FUNC_ASYM_CIPHER_SET_CTX_PARAMS:
  305. if (cipher->set_ctx_params != NULL)
  306. break;
  307. cipher->set_ctx_params
  308. = OSSL_FUNC_asym_cipher_set_ctx_params(fns);
  309. sparamfncnt++;
  310. break;
  311. case OSSL_FUNC_ASYM_CIPHER_SETTABLE_CTX_PARAMS:
  312. if (cipher->settable_ctx_params != NULL)
  313. break;
  314. cipher->settable_ctx_params
  315. = OSSL_FUNC_asym_cipher_settable_ctx_params(fns);
  316. sparamfncnt++;
  317. break;
  318. }
  319. }
  320. if (ctxfncnt != 2
  321. || (encfncnt != 0 && encfncnt != 2)
  322. || (decfncnt != 0 && decfncnt != 2)
  323. || (encfncnt != 2 && decfncnt != 2)
  324. || (gparamfncnt != 0 && gparamfncnt != 2)
  325. || (sparamfncnt != 0 && sparamfncnt != 2)) {
  326. /*
  327. * In order to be a consistent set of functions we must have at least
  328. * a set of context functions (newctx and freectx) as well as a pair of
  329. * "cipher" functions: (encrypt_init, encrypt) or
  330. * (decrypt_init decrypt). set_ctx_params and settable_ctx_params are
  331. * optional, but if one of them is present then the other one must also
  332. * be present. The same applies to get_ctx_params and
  333. * gettable_ctx_params. The dupctx function is optional.
  334. */
  335. ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS);
  336. goto err;
  337. }
  338. return cipher;
  339. err:
  340. EVP_ASYM_CIPHER_free(cipher);
  341. return NULL;
  342. }
  343. void EVP_ASYM_CIPHER_free(EVP_ASYM_CIPHER *cipher)
  344. {
  345. int i;
  346. if (cipher == NULL)
  347. return;
  348. CRYPTO_DOWN_REF(&cipher->refcnt, &i, cipher->lock);
  349. if (i > 0)
  350. return;
  351. OPENSSL_free(cipher->type_name);
  352. ossl_provider_free(cipher->prov);
  353. CRYPTO_THREAD_lock_free(cipher->lock);
  354. OPENSSL_free(cipher);
  355. }
  356. int EVP_ASYM_CIPHER_up_ref(EVP_ASYM_CIPHER *cipher)
  357. {
  358. int ref = 0;
  359. CRYPTO_UP_REF(&cipher->refcnt, &ref, cipher->lock);
  360. return 1;
  361. }
  362. OSSL_PROVIDER *EVP_ASYM_CIPHER_get0_provider(const EVP_ASYM_CIPHER *cipher)
  363. {
  364. return cipher->prov;
  365. }
  366. EVP_ASYM_CIPHER *EVP_ASYM_CIPHER_fetch(OSSL_LIB_CTX *ctx, const char *algorithm,
  367. const char *properties)
  368. {
  369. return evp_generic_fetch(ctx, OSSL_OP_ASYM_CIPHER, algorithm, properties,
  370. evp_asym_cipher_from_algorithm,
  371. (int (*)(void *))EVP_ASYM_CIPHER_up_ref,
  372. (void (*)(void *))EVP_ASYM_CIPHER_free);
  373. }
  374. int EVP_ASYM_CIPHER_is_a(const EVP_ASYM_CIPHER *cipher, const char *name)
  375. {
  376. return evp_is_a(cipher->prov, cipher->name_id, NULL, name);
  377. }
  378. int evp_asym_cipher_get_number(const EVP_ASYM_CIPHER *cipher)
  379. {
  380. return cipher->name_id;
  381. }
  382. const char *EVP_ASYM_CIPHER_get0_name(const EVP_ASYM_CIPHER *cipher)
  383. {
  384. return cipher->type_name;
  385. }
  386. const char *EVP_ASYM_CIPHER_get0_description(const EVP_ASYM_CIPHER *cipher)
  387. {
  388. return cipher->description;
  389. }
  390. void EVP_ASYM_CIPHER_do_all_provided(OSSL_LIB_CTX *libctx,
  391. void (*fn)(EVP_ASYM_CIPHER *cipher,
  392. void *arg),
  393. void *arg)
  394. {
  395. evp_generic_do_all(libctx, OSSL_OP_ASYM_CIPHER,
  396. (void (*)(void *, void *))fn, arg,
  397. evp_asym_cipher_from_algorithm,
  398. (int (*)(void *))EVP_ASYM_CIPHER_up_ref,
  399. (void (*)(void *))EVP_ASYM_CIPHER_free);
  400. }
  401. int EVP_ASYM_CIPHER_names_do_all(const EVP_ASYM_CIPHER *cipher,
  402. void (*fn)(const char *name, void *data),
  403. void *data)
  404. {
  405. if (cipher->prov != NULL)
  406. return evp_names_do_all(cipher->prov, cipher->name_id, fn, data);
  407. return 1;
  408. }
  409. const OSSL_PARAM *EVP_ASYM_CIPHER_gettable_ctx_params(const EVP_ASYM_CIPHER *cip)
  410. {
  411. void *provctx;
  412. if (cip == NULL || cip->gettable_ctx_params == NULL)
  413. return NULL;
  414. provctx = ossl_provider_ctx(EVP_ASYM_CIPHER_get0_provider(cip));
  415. return cip->gettable_ctx_params(NULL, provctx);
  416. }
  417. const OSSL_PARAM *EVP_ASYM_CIPHER_settable_ctx_params(const EVP_ASYM_CIPHER *cip)
  418. {
  419. void *provctx;
  420. if (cip == NULL || cip->settable_ctx_params == NULL)
  421. return NULL;
  422. provctx = ossl_provider_ctx(EVP_ASYM_CIPHER_get0_provider(cip));
  423. return cip->settable_ctx_params(NULL, provctx);
  424. }