evp_ctx.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young (eay@cryptsoft.com).
  6. * The implementation was written so as to conform with Netscapes SSL.
  7. *
  8. * This library is free for commercial and non-commercial use as long as
  9. * the following conditions are aheared to. The following conditions
  10. * apply to all code found in this distribution, be it the RC4, RSA,
  11. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  12. * included with this distribution is covered by the same copyright terms
  13. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  14. *
  15. * Copyright remains Eric Young's, and as such any Copyright notices in
  16. * the code are not to be removed.
  17. * If this package is used in a product, Eric Young should be given attribution
  18. * as the author of the parts of the library used.
  19. * This can be in the form of a textual message at program startup or
  20. * in documentation (online or textual) provided with the package.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. All advertising materials mentioning features or use of this software
  31. * must display the following acknowledgement:
  32. * "This product includes cryptographic software written by
  33. * Eric Young (eay@cryptsoft.com)"
  34. * The word 'cryptographic' can be left out if the rouines from the library
  35. * being used are not cryptographic related :-).
  36. * 4. If you include any Windows specific code (or a derivative thereof) from
  37. * the apps directory (application code) you must include an acknowledgement:
  38. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  41. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. * The licence and distribution terms for any publically available version or
  53. * derivative of this code cannot be changed. i.e. this code cannot simply be
  54. * copied and put under another distribution licence
  55. * [including the GNU Public Licence.] */
  56. #include <openssl/evp.h>
  57. #include <string.h>
  58. #include <openssl/err.h>
  59. #include <openssl/mem.h>
  60. #include "../internal.h"
  61. #include "internal.h"
  62. static const EVP_PKEY_METHOD *const evp_methods[] = {
  63. &rsa_pkey_meth,
  64. &ec_pkey_meth,
  65. };
  66. static const EVP_PKEY_METHOD *evp_pkey_meth_find(int type) {
  67. unsigned i;
  68. for (i = 0; i < sizeof(evp_methods)/sizeof(EVP_PKEY_METHOD*); i++) {
  69. if (evp_methods[i]->pkey_id == type) {
  70. return evp_methods[i];
  71. }
  72. }
  73. return NULL;
  74. }
  75. static EVP_PKEY_CTX *evp_pkey_ctx_new(EVP_PKEY *pkey, ENGINE *e, int id) {
  76. EVP_PKEY_CTX *ret;
  77. const EVP_PKEY_METHOD *pmeth;
  78. if (id == -1) {
  79. if (!pkey || !pkey->ameth) {
  80. return NULL;
  81. }
  82. id = pkey->ameth->pkey_id;
  83. }
  84. pmeth = evp_pkey_meth_find(id);
  85. if (pmeth == NULL) {
  86. OPENSSL_PUT_ERROR(EVP, EVP_R_UNSUPPORTED_ALGORITHM);
  87. ERR_add_error_dataf("algorithm %d", id);
  88. return NULL;
  89. }
  90. ret = OPENSSL_malloc(sizeof(EVP_PKEY_CTX));
  91. if (!ret) {
  92. OPENSSL_PUT_ERROR(EVP, ERR_R_MALLOC_FAILURE);
  93. return NULL;
  94. }
  95. OPENSSL_memset(ret, 0, sizeof(EVP_PKEY_CTX));
  96. ret->engine = e;
  97. ret->pmeth = pmeth;
  98. ret->operation = EVP_PKEY_OP_UNDEFINED;
  99. if (pkey) {
  100. EVP_PKEY_up_ref(pkey);
  101. ret->pkey = pkey;
  102. }
  103. if (pmeth->init) {
  104. if (pmeth->init(ret) <= 0) {
  105. EVP_PKEY_free(ret->pkey);
  106. OPENSSL_free(ret);
  107. return NULL;
  108. }
  109. }
  110. return ret;
  111. }
  112. EVP_PKEY_CTX *EVP_PKEY_CTX_new(EVP_PKEY *pkey, ENGINE *e) {
  113. return evp_pkey_ctx_new(pkey, e, -1);
  114. }
  115. EVP_PKEY_CTX *EVP_PKEY_CTX_new_id(int id, ENGINE *e) {
  116. return evp_pkey_ctx_new(NULL, e, id);
  117. }
  118. void EVP_PKEY_CTX_free(EVP_PKEY_CTX *ctx) {
  119. if (ctx == NULL) {
  120. return;
  121. }
  122. if (ctx->pmeth && ctx->pmeth->cleanup) {
  123. ctx->pmeth->cleanup(ctx);
  124. }
  125. EVP_PKEY_free(ctx->pkey);
  126. EVP_PKEY_free(ctx->peerkey);
  127. OPENSSL_free(ctx);
  128. }
  129. EVP_PKEY_CTX *EVP_PKEY_CTX_dup(EVP_PKEY_CTX *ctx) {
  130. if (!ctx->pmeth || !ctx->pmeth->copy) {
  131. return NULL;
  132. }
  133. EVP_PKEY_CTX *ret = OPENSSL_malloc(sizeof(EVP_PKEY_CTX));
  134. if (!ret) {
  135. return NULL;
  136. }
  137. OPENSSL_memset(ret, 0, sizeof(EVP_PKEY_CTX));
  138. ret->pmeth = ctx->pmeth;
  139. ret->engine = ctx->engine;
  140. ret->operation = ctx->operation;
  141. if (ctx->pkey != NULL) {
  142. EVP_PKEY_up_ref(ctx->pkey);
  143. ret->pkey = ctx->pkey;
  144. }
  145. if (ctx->peerkey != NULL) {
  146. EVP_PKEY_up_ref(ctx->peerkey);
  147. ret->peerkey = ctx->peerkey;
  148. }
  149. if (ctx->pmeth->copy(ret, ctx) <= 0) {
  150. ret->pmeth = NULL;
  151. EVP_PKEY_CTX_free(ret);
  152. OPENSSL_PUT_ERROR(EVP, ERR_LIB_EVP);
  153. return NULL;
  154. }
  155. return ret;
  156. }
  157. EVP_PKEY *EVP_PKEY_CTX_get0_pkey(EVP_PKEY_CTX *ctx) { return ctx->pkey; }
  158. int EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX *ctx, int keytype, int optype, int cmd,
  159. int p1, void *p2) {
  160. if (!ctx || !ctx->pmeth || !ctx->pmeth->ctrl) {
  161. OPENSSL_PUT_ERROR(EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  162. return 0;
  163. }
  164. if (keytype != -1 && ctx->pmeth->pkey_id != keytype) {
  165. OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  166. return 0;
  167. }
  168. if (ctx->operation == EVP_PKEY_OP_UNDEFINED) {
  169. OPENSSL_PUT_ERROR(EVP, EVP_R_NO_OPERATION_SET);
  170. return 0;
  171. }
  172. if (optype != -1 && !(ctx->operation & optype)) {
  173. OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_OPERATION);
  174. return 0;
  175. }
  176. return ctx->pmeth->ctrl(ctx, cmd, p1, p2);
  177. }
  178. int EVP_PKEY_sign_init(EVP_PKEY_CTX *ctx) {
  179. if (!ctx || !ctx->pmeth || !ctx->pmeth->sign) {
  180. OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  181. return 0;
  182. }
  183. ctx->operation = EVP_PKEY_OP_SIGN;
  184. return 1;
  185. }
  186. int EVP_PKEY_sign(EVP_PKEY_CTX *ctx, uint8_t *sig, size_t *sig_len,
  187. const uint8_t *data, size_t data_len) {
  188. if (!ctx || !ctx->pmeth || !ctx->pmeth->sign) {
  189. OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  190. return 0;
  191. }
  192. if (ctx->operation != EVP_PKEY_OP_SIGN) {
  193. OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATON_NOT_INITIALIZED);
  194. return 0;
  195. }
  196. return ctx->pmeth->sign(ctx, sig, sig_len, data, data_len);
  197. }
  198. int EVP_PKEY_verify_init(EVP_PKEY_CTX *ctx) {
  199. if (!ctx || !ctx->pmeth || !ctx->pmeth->verify) {
  200. OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  201. return 0;
  202. }
  203. ctx->operation = EVP_PKEY_OP_VERIFY;
  204. return 1;
  205. }
  206. int EVP_PKEY_verify(EVP_PKEY_CTX *ctx, const uint8_t *sig, size_t sig_len,
  207. const uint8_t *data, size_t data_len) {
  208. if (!ctx || !ctx->pmeth || !ctx->pmeth->verify) {
  209. OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  210. return 0;
  211. }
  212. if (ctx->operation != EVP_PKEY_OP_VERIFY) {
  213. OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATON_NOT_INITIALIZED);
  214. return 0;
  215. }
  216. return ctx->pmeth->verify(ctx, sig, sig_len, data, data_len);
  217. }
  218. int EVP_PKEY_encrypt_init(EVP_PKEY_CTX *ctx) {
  219. if (!ctx || !ctx->pmeth || !ctx->pmeth->encrypt) {
  220. OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  221. return 0;
  222. }
  223. ctx->operation = EVP_PKEY_OP_ENCRYPT;
  224. return 1;
  225. }
  226. int EVP_PKEY_encrypt(EVP_PKEY_CTX *ctx, uint8_t *out, size_t *outlen,
  227. const uint8_t *in, size_t inlen) {
  228. if (!ctx || !ctx->pmeth || !ctx->pmeth->encrypt) {
  229. OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  230. return 0;
  231. }
  232. if (ctx->operation != EVP_PKEY_OP_ENCRYPT) {
  233. OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATON_NOT_INITIALIZED);
  234. return 0;
  235. }
  236. return ctx->pmeth->encrypt(ctx, out, outlen, in, inlen);
  237. }
  238. int EVP_PKEY_decrypt_init(EVP_PKEY_CTX *ctx) {
  239. if (!ctx || !ctx->pmeth || !ctx->pmeth->decrypt) {
  240. OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  241. return 0;
  242. }
  243. ctx->operation = EVP_PKEY_OP_DECRYPT;
  244. return 1;
  245. }
  246. int EVP_PKEY_decrypt(EVP_PKEY_CTX *ctx, uint8_t *out, size_t *outlen,
  247. const uint8_t *in, size_t inlen) {
  248. if (!ctx || !ctx->pmeth || !ctx->pmeth->decrypt) {
  249. OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  250. return 0;
  251. }
  252. if (ctx->operation != EVP_PKEY_OP_DECRYPT) {
  253. OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATON_NOT_INITIALIZED);
  254. return 0;
  255. }
  256. return ctx->pmeth->decrypt(ctx, out, outlen, in, inlen);
  257. }
  258. int EVP_PKEY_verify_recover_init(EVP_PKEY_CTX *ctx) {
  259. if (!ctx || !ctx->pmeth || !ctx->pmeth->verify_recover) {
  260. OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  261. return 0;
  262. }
  263. ctx->operation = EVP_PKEY_OP_VERIFYRECOVER;
  264. return 1;
  265. }
  266. int EVP_PKEY_verify_recover(EVP_PKEY_CTX *ctx, uint8_t *out, size_t *out_len,
  267. const uint8_t *sig, size_t sig_len) {
  268. if (!ctx || !ctx->pmeth || !ctx->pmeth->verify_recover) {
  269. OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  270. return 0;
  271. }
  272. if (ctx->operation != EVP_PKEY_OP_VERIFYRECOVER) {
  273. OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATON_NOT_INITIALIZED);
  274. return 0;
  275. }
  276. return ctx->pmeth->verify_recover(ctx, out, out_len, sig, sig_len);
  277. }
  278. int EVP_PKEY_derive_init(EVP_PKEY_CTX *ctx) {
  279. if (!ctx || !ctx->pmeth || !ctx->pmeth->derive) {
  280. OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  281. return 0;
  282. }
  283. ctx->operation = EVP_PKEY_OP_DERIVE;
  284. return 1;
  285. }
  286. int EVP_PKEY_derive_set_peer(EVP_PKEY_CTX *ctx, EVP_PKEY *peer) {
  287. int ret;
  288. if (!ctx || !ctx->pmeth ||
  289. !(ctx->pmeth->derive || ctx->pmeth->encrypt || ctx->pmeth->decrypt) ||
  290. !ctx->pmeth->ctrl) {
  291. OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  292. return 0;
  293. }
  294. if (ctx->operation != EVP_PKEY_OP_DERIVE &&
  295. ctx->operation != EVP_PKEY_OP_ENCRYPT &&
  296. ctx->operation != EVP_PKEY_OP_DECRYPT) {
  297. OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATON_NOT_INITIALIZED);
  298. return 0;
  299. }
  300. ret = ctx->pmeth->ctrl(ctx, EVP_PKEY_CTRL_PEER_KEY, 0, peer);
  301. if (ret <= 0) {
  302. return 0;
  303. }
  304. if (ret == 2) {
  305. return 1;
  306. }
  307. if (!ctx->pkey) {
  308. OPENSSL_PUT_ERROR(EVP, EVP_R_NO_KEY_SET);
  309. return 0;
  310. }
  311. if (ctx->pkey->type != peer->type) {
  312. OPENSSL_PUT_ERROR(EVP, EVP_R_DIFFERENT_KEY_TYPES);
  313. return 0;
  314. }
  315. /* ran@cryptocom.ru: For clarity. The error is if parameters in peer are
  316. * present (!missing) but don't match. EVP_PKEY_cmp_parameters may return
  317. * 1 (match), 0 (don't match) and -2 (comparison is not defined). -1
  318. * (different key types) is impossible here because it is checked earlier.
  319. * -2 is OK for us here, as well as 1, so we can check for 0 only. */
  320. if (!EVP_PKEY_missing_parameters(peer) &&
  321. !EVP_PKEY_cmp_parameters(ctx->pkey, peer)) {
  322. OPENSSL_PUT_ERROR(EVP, EVP_R_DIFFERENT_PARAMETERS);
  323. return 0;
  324. }
  325. EVP_PKEY_free(ctx->peerkey);
  326. ctx->peerkey = peer;
  327. ret = ctx->pmeth->ctrl(ctx, EVP_PKEY_CTRL_PEER_KEY, 1, peer);
  328. if (ret <= 0) {
  329. ctx->peerkey = NULL;
  330. return 0;
  331. }
  332. EVP_PKEY_up_ref(peer);
  333. return 1;
  334. }
  335. int EVP_PKEY_derive(EVP_PKEY_CTX *ctx, uint8_t *key, size_t *out_key_len) {
  336. if (!ctx || !ctx->pmeth || !ctx->pmeth->derive) {
  337. OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  338. return 0;
  339. }
  340. if (ctx->operation != EVP_PKEY_OP_DERIVE) {
  341. OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATON_NOT_INITIALIZED);
  342. return 0;
  343. }
  344. return ctx->pmeth->derive(ctx, key, out_key_len);
  345. }
  346. int EVP_PKEY_keygen_init(EVP_PKEY_CTX *ctx) {
  347. if (!ctx || !ctx->pmeth || !ctx->pmeth->keygen) {
  348. OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  349. return 0;
  350. }
  351. ctx->operation = EVP_PKEY_OP_KEYGEN;
  352. return 1;
  353. }
  354. int EVP_PKEY_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey) {
  355. if (!ctx || !ctx->pmeth || !ctx->pmeth->keygen) {
  356. OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  357. return 0;
  358. }
  359. if (ctx->operation != EVP_PKEY_OP_KEYGEN) {
  360. OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATON_NOT_INITIALIZED);
  361. return 0;
  362. }
  363. if (!ppkey) {
  364. return 0;
  365. }
  366. if (!*ppkey) {
  367. *ppkey = EVP_PKEY_new();
  368. if (!*ppkey) {
  369. OPENSSL_PUT_ERROR(EVP, ERR_LIB_EVP);
  370. return 0;
  371. }
  372. }
  373. if (!ctx->pmeth->keygen(ctx, *ppkey)) {
  374. EVP_PKEY_free(*ppkey);
  375. *ppkey = NULL;
  376. return 0;
  377. }
  378. return 1;
  379. }