exchange.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /*
  2. * Copyright 2019 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 <openssl/crypto.h>
  10. #include <openssl/evp.h>
  11. #include <openssl/err.h>
  12. #include "internal/refcount.h"
  13. #include "internal/evp_int.h"
  14. #include "internal/provider.h"
  15. #include "evp_locl.h"
  16. static EVP_KEYEXCH *evp_keyexch_new(OSSL_PROVIDER *prov)
  17. {
  18. EVP_KEYEXCH *exchange = OPENSSL_zalloc(sizeof(EVP_KEYEXCH));
  19. exchange->lock = CRYPTO_THREAD_lock_new();
  20. if (exchange->lock == NULL) {
  21. OPENSSL_free(exchange);
  22. return NULL;
  23. }
  24. exchange->prov = prov;
  25. ossl_provider_up_ref(prov);
  26. exchange->refcnt = 1;
  27. return exchange;
  28. }
  29. static void *evp_keyexch_from_dispatch(const OSSL_DISPATCH *fns,
  30. OSSL_PROVIDER *prov)
  31. {
  32. EVP_KEYEXCH *exchange = NULL;
  33. int fncnt = 0;
  34. if ((exchange = evp_keyexch_new(prov)) == NULL)
  35. return NULL;
  36. for (; fns->function_id != 0; fns++) {
  37. switch (fns->function_id) {
  38. case OSSL_FUNC_KEYEXCH_NEWCTX:
  39. if (exchange->newctx != NULL)
  40. break;
  41. exchange->newctx = OSSL_get_OP_keyexch_newctx(fns);
  42. fncnt++;
  43. break;
  44. case OSSL_FUNC_KEYEXCH_INIT:
  45. if (exchange->init != NULL)
  46. break;
  47. exchange->init = OSSL_get_OP_keyexch_init(fns);
  48. fncnt++;
  49. break;
  50. case OSSL_FUNC_KEYEXCH_SET_PEER:
  51. if (exchange->set_peer != NULL)
  52. break;
  53. exchange->set_peer = OSSL_get_OP_keyexch_set_peer(fns);
  54. break;
  55. case OSSL_FUNC_KEYEXCH_DERIVE:
  56. if (exchange->derive != NULL)
  57. break;
  58. exchange->derive = OSSL_get_OP_keyexch_derive(fns);
  59. fncnt++;
  60. break;
  61. case OSSL_FUNC_KEYEXCH_FREECTX:
  62. if (exchange->freectx != NULL)
  63. break;
  64. exchange->freectx = OSSL_get_OP_keyexch_freectx(fns);
  65. fncnt++;
  66. break;
  67. case OSSL_FUNC_KEYEXCH_DUPCTX:
  68. if (exchange->dupctx != NULL)
  69. break;
  70. exchange->dupctx = OSSL_get_OP_keyexch_dupctx(fns);
  71. break;
  72. case OSSL_FUNC_KEYEXCH_SET_PARAMS:
  73. if (exchange->set_params != NULL)
  74. break;
  75. exchange->set_params = OSSL_get_OP_keyexch_set_params(fns);
  76. break;
  77. }
  78. }
  79. if (fncnt != 4) {
  80. /*
  81. * In order to be a consistent set of functions we must have at least
  82. * a complete set of "exchange" functions: init, derive, newctx,
  83. * and freectx. The dupctx, set_peer and set_params functions are
  84. * optional.
  85. */
  86. EVP_KEYEXCH_free(exchange);
  87. EVPerr(EVP_F_EVP_KEYEXCH_FROM_DISPATCH,
  88. EVP_R_INVALID_PROVIDER_FUNCTIONS);
  89. return NULL;
  90. }
  91. return exchange;
  92. }
  93. void EVP_KEYEXCH_free(EVP_KEYEXCH *exchange)
  94. {
  95. if (exchange != NULL) {
  96. int i;
  97. CRYPTO_DOWN_REF(&exchange->refcnt, &i, exchange->lock);
  98. if (i > 0)
  99. return;
  100. ossl_provider_free(exchange->prov);
  101. CRYPTO_THREAD_lock_free(exchange->lock);
  102. OPENSSL_free(exchange);
  103. }
  104. }
  105. int EVP_KEYEXCH_up_ref(EVP_KEYEXCH *exchange)
  106. {
  107. int ref = 0;
  108. CRYPTO_UP_REF(&exchange->refcnt, &ref, exchange->lock);
  109. return 1;
  110. }
  111. EVP_KEYEXCH *EVP_KEYEXCH_fetch(OPENSSL_CTX *ctx, const char *algorithm,
  112. const char *properties)
  113. {
  114. return evp_generic_fetch(ctx, OSSL_OP_KEYEXCH, algorithm, properties,
  115. evp_keyexch_from_dispatch,
  116. (int (*)(void *))EVP_KEYEXCH_up_ref,
  117. (void (*)(void *))EVP_KEYEXCH_free);
  118. }
  119. int EVP_PKEY_derive_init_ex(EVP_PKEY_CTX *ctx, EVP_KEYEXCH *exchange)
  120. {
  121. int ret;
  122. OSSL_PARAM *param = NULL;
  123. size_t paramsz = 0;
  124. ctx->operation = EVP_PKEY_OP_DERIVE;
  125. if (ctx->engine != NULL)
  126. goto legacy;
  127. if (exchange != NULL) {
  128. if (!EVP_KEYEXCH_up_ref(exchange))
  129. goto err;
  130. } else {
  131. int nid = ctx->pkey != NULL ? ctx->pkey->type : ctx->pmeth->pkey_id;
  132. /*
  133. * TODO(3.0): Check for legacy handling. Remove this once all all
  134. * algorithms are moved to providers.
  135. */
  136. if (ctx->pkey != NULL) {
  137. switch (ctx->pkey->type) {
  138. case EVP_PKEY_DH:
  139. break;
  140. default:
  141. goto legacy;
  142. }
  143. exchange = EVP_KEYEXCH_fetch(NULL, OBJ_nid2sn(nid), NULL);
  144. } else {
  145. goto legacy;
  146. }
  147. if (exchange == NULL) {
  148. EVPerr(EVP_F_EVP_PKEY_DERIVE_INIT_EX, EVP_R_INITIALIZATION_ERROR);
  149. goto err;
  150. }
  151. }
  152. if (ctx->exchprovctx != NULL && ctx->exchange != NULL)
  153. ctx->exchange->freectx(ctx->exchprovctx);
  154. EVP_KEYEXCH_free(ctx->exchange);
  155. ctx->exchange = exchange;
  156. if (ctx->pkey != NULL) {
  157. param = evp_pkey_to_param(ctx->pkey, &paramsz);
  158. if (param == NULL) {
  159. EVPerr(EVP_F_EVP_PKEY_DERIVE_INIT_EX, EVP_R_INITIALIZATION_ERROR);
  160. goto err;
  161. }
  162. }
  163. ctx->exchprovctx = exchange->newctx(ossl_provider_ctx(exchange->prov));
  164. if (ctx->exchprovctx == NULL) {
  165. OPENSSL_secure_clear_free(param, paramsz);
  166. EVPerr(EVP_F_EVP_PKEY_DERIVE_INIT_EX, EVP_R_INITIALIZATION_ERROR);
  167. goto err;
  168. }
  169. ret = exchange->init(ctx->exchprovctx, param);
  170. /*
  171. * TODO(3.0): Really we should detect whether to call OPENSSL_free or
  172. * OPENSSL_secure_clear_free based on the presence of a private key or not.
  173. * Since we always expect a private key to be present we just call
  174. * OPENSSL_secure_clear_free for now.
  175. */
  176. OPENSSL_secure_clear_free(param, paramsz);
  177. return ret ? 1 : 0;
  178. err:
  179. ctx->operation = EVP_PKEY_OP_UNDEFINED;
  180. return 0;
  181. legacy:
  182. if (ctx == NULL || ctx->pmeth == NULL || ctx->pmeth->derive == NULL) {
  183. EVPerr(EVP_F_EVP_PKEY_DERIVE_INIT_EX,
  184. EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  185. return -2;
  186. }
  187. if (ctx->pmeth->derive_init == NULL)
  188. return 1;
  189. ret = ctx->pmeth->derive_init(ctx);
  190. if (ret <= 0)
  191. ctx->operation = EVP_PKEY_OP_UNDEFINED;
  192. return ret;
  193. }
  194. int EVP_PKEY_derive_init(EVP_PKEY_CTX *ctx)
  195. {
  196. return EVP_PKEY_derive_init_ex(ctx, NULL);
  197. }
  198. int EVP_PKEY_derive_set_peer(EVP_PKEY_CTX *ctx, EVP_PKEY *peer)
  199. {
  200. int ret;
  201. OSSL_PARAM *param = NULL;
  202. if (ctx == NULL) {
  203. EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER,
  204. EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  205. return -2;
  206. }
  207. if (ctx->exchprovctx == NULL)
  208. goto legacy;
  209. if (ctx->operation != EVP_PKEY_OP_DERIVE) {
  210. EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER,
  211. EVP_R_OPERATON_NOT_INITIALIZED);
  212. return -1;
  213. }
  214. if (ctx->exchange->set_peer == NULL) {
  215. EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER,
  216. EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  217. return -2;
  218. }
  219. param = evp_pkey_to_param(peer, NULL);
  220. if (param == NULL) {
  221. EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER, ERR_R_INTERNAL_ERROR);
  222. return 0;
  223. }
  224. ret = ctx->exchange->set_peer(ctx->exchprovctx, param);
  225. /*
  226. * TODO(3.0): Really we should detect whether to call OPENSSL_free or
  227. * OPENSSL_secure_clear_free based on the presence of a private key or not.
  228. * Since we always expect a public key to be present we just call
  229. * OPENSSL_free for now.
  230. */
  231. OPENSSL_free(param);
  232. return ret;
  233. legacy:
  234. if (ctx->pmeth == NULL
  235. || !(ctx->pmeth->derive != NULL
  236. || ctx->pmeth->encrypt != NULL
  237. || ctx->pmeth->decrypt != NULL)
  238. || ctx->pmeth->ctrl == NULL) {
  239. EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER,
  240. EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  241. return -2;
  242. }
  243. if (ctx->operation != EVP_PKEY_OP_DERIVE
  244. && ctx->operation != EVP_PKEY_OP_ENCRYPT
  245. && ctx->operation != EVP_PKEY_OP_DECRYPT) {
  246. EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER,
  247. EVP_R_OPERATON_NOT_INITIALIZED);
  248. return -1;
  249. }
  250. ret = ctx->pmeth->ctrl(ctx, EVP_PKEY_CTRL_PEER_KEY, 0, peer);
  251. if (ret <= 0)
  252. return ret;
  253. if (ret == 2)
  254. return 1;
  255. if (ctx->pkey == NULL) {
  256. EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER, EVP_R_NO_KEY_SET);
  257. return -1;
  258. }
  259. if (ctx->pkey->type != peer->type) {
  260. EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER, EVP_R_DIFFERENT_KEY_TYPES);
  261. return -1;
  262. }
  263. /*
  264. * For clarity. The error is if parameters in peer are
  265. * present (!missing) but don't match. EVP_PKEY_cmp_parameters may return
  266. * 1 (match), 0 (don't match) and -2 (comparison is not defined). -1
  267. * (different key types) is impossible here because it is checked earlier.
  268. * -2 is OK for us here, as well as 1, so we can check for 0 only.
  269. */
  270. if (!EVP_PKEY_missing_parameters(peer) &&
  271. !EVP_PKEY_cmp_parameters(ctx->pkey, peer)) {
  272. EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER, EVP_R_DIFFERENT_PARAMETERS);
  273. return -1;
  274. }
  275. EVP_PKEY_free(ctx->peerkey);
  276. ctx->peerkey = peer;
  277. ret = ctx->pmeth->ctrl(ctx, EVP_PKEY_CTRL_PEER_KEY, 1, peer);
  278. if (ret <= 0) {
  279. ctx->peerkey = NULL;
  280. return ret;
  281. }
  282. EVP_PKEY_up_ref(peer);
  283. return 1;
  284. }
  285. int EVP_PKEY_derive(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *pkeylen)
  286. {
  287. int ret;
  288. if (ctx == NULL) {
  289. EVPerr(EVP_F_EVP_PKEY_DERIVE,
  290. EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  291. return -2;
  292. }
  293. if (ctx->operation != EVP_PKEY_OP_DERIVE) {
  294. EVPerr(EVP_F_EVP_PKEY_DERIVE, EVP_R_OPERATON_NOT_INITIALIZED);
  295. return -1;
  296. }
  297. if (ctx->exchprovctx == NULL)
  298. goto legacy;
  299. ret = ctx->exchange->derive(ctx->exchprovctx, key, pkeylen, SIZE_MAX);
  300. return ret;
  301. legacy:
  302. if (ctx == NULL || ctx->pmeth == NULL || ctx->pmeth->derive == NULL) {
  303. EVPerr(EVP_F_EVP_PKEY_DERIVE,
  304. EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  305. return -2;
  306. }
  307. M_check_autoarg(ctx, key, pkeylen, EVP_F_EVP_PKEY_DERIVE)
  308. return ctx->pmeth->derive(ctx, key, pkeylen);
  309. }