asymcipher.c 15 KB

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