You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

507 lines
14 KiB

  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 <stdio.h>
  58. #include <string.h>
  59. #include <openssl/err.h>
  60. #include <openssl/mem.h>
  61. #include <openssl/obj.h>
  62. #include "internal.h"
  63. static const EVP_PKEY_METHOD *const evp_methods[] = {
  64. &rsa_pkey_meth,
  65. &ec_pkey_meth,
  66. };
  67. static const EVP_PKEY_METHOD *evp_pkey_meth_find(int type) {
  68. unsigned i;
  69. for (i = 0; i < sizeof(evp_methods)/sizeof(EVP_PKEY_METHOD*); i++) {
  70. if (evp_methods[i]->pkey_id == type) {
  71. return evp_methods[i];
  72. }
  73. }
  74. return NULL;
  75. }
  76. static EVP_PKEY_CTX *evp_pkey_ctx_new(EVP_PKEY *pkey, ENGINE *e, int id) {
  77. EVP_PKEY_CTX *ret;
  78. const EVP_PKEY_METHOD *pmeth;
  79. if (id == -1) {
  80. if (!pkey || !pkey->ameth) {
  81. return NULL;
  82. }
  83. id = pkey->ameth->pkey_id;
  84. }
  85. pmeth = evp_pkey_meth_find(id);
  86. if (pmeth == NULL) {
  87. OPENSSL_PUT_ERROR(EVP, EVP_R_UNSUPPORTED_ALGORITHM);
  88. const char *name = OBJ_nid2sn(id);
  89. ERR_add_error_dataf("algorithm %d (%s)", id, name);
  90. return NULL;
  91. }
  92. ret = OPENSSL_malloc(sizeof(EVP_PKEY_CTX));
  93. if (!ret) {
  94. OPENSSL_PUT_ERROR(EVP, ERR_R_MALLOC_FAILURE);
  95. return NULL;
  96. }
  97. memset(ret, 0, sizeof(EVP_PKEY_CTX));
  98. ret->engine = e;
  99. ret->pmeth = pmeth;
  100. ret->operation = EVP_PKEY_OP_UNDEFINED;
  101. if (pkey) {
  102. ret->pkey = EVP_PKEY_up_ref(pkey);
  103. }
  104. if (pmeth->init) {
  105. if (pmeth->init(ret) <= 0) {
  106. EVP_PKEY_free(ret->pkey);
  107. OPENSSL_free(ret);
  108. return NULL;
  109. }
  110. }
  111. return ret;
  112. }
  113. EVP_PKEY_CTX *EVP_PKEY_CTX_new(EVP_PKEY *pkey, ENGINE *e) {
  114. return evp_pkey_ctx_new(pkey, e, -1);
  115. }
  116. EVP_PKEY_CTX *EVP_PKEY_CTX_new_id(int id, ENGINE *e) {
  117. return evp_pkey_ctx_new(NULL, e, id);
  118. }
  119. void EVP_PKEY_CTX_free(EVP_PKEY_CTX *ctx) {
  120. if (ctx == NULL) {
  121. return;
  122. }
  123. if (ctx->pmeth && ctx->pmeth->cleanup) {
  124. ctx->pmeth->cleanup(ctx);
  125. }
  126. EVP_PKEY_free(ctx->pkey);
  127. EVP_PKEY_free(ctx->peerkey);
  128. OPENSSL_free(ctx);
  129. }
  130. EVP_PKEY_CTX *EVP_PKEY_CTX_dup(EVP_PKEY_CTX *pctx) {
  131. EVP_PKEY_CTX *rctx;
  132. if (!pctx->pmeth || !pctx->pmeth->copy) {
  133. return NULL;
  134. }
  135. rctx = OPENSSL_malloc(sizeof(EVP_PKEY_CTX));
  136. if (!rctx) {
  137. return NULL;
  138. }
  139. memset(rctx, 0, sizeof(EVP_PKEY_CTX));
  140. rctx->pmeth = pctx->pmeth;
  141. rctx->engine = pctx->engine;
  142. rctx->operation = pctx->operation;
  143. if (pctx->pkey) {
  144. rctx->pkey = EVP_PKEY_up_ref(pctx->pkey);
  145. if (rctx->pkey == NULL) {
  146. goto err;
  147. }
  148. }
  149. if (pctx->peerkey) {
  150. rctx->peerkey = EVP_PKEY_up_ref(pctx->peerkey);
  151. if (rctx->peerkey == NULL) {
  152. goto err;
  153. }
  154. }
  155. if (pctx->pmeth->copy(rctx, pctx) > 0) {
  156. return rctx;
  157. }
  158. err:
  159. EVP_PKEY_CTX_free(rctx);
  160. OPENSSL_PUT_ERROR(EVP, ERR_LIB_EVP);
  161. return NULL;
  162. }
  163. EVP_PKEY *EVP_PKEY_CTX_get0_pkey(EVP_PKEY_CTX *ctx) { return ctx->pkey; }
  164. void EVP_PKEY_CTX_set_app_data(EVP_PKEY_CTX *ctx, void *data) {
  165. ctx->app_data = data;
  166. }
  167. void *EVP_PKEY_CTX_get_app_data(EVP_PKEY_CTX *ctx) { return ctx->app_data; }
  168. int EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX *ctx, int keytype, int optype, int cmd,
  169. int p1, void *p2) {
  170. if (!ctx || !ctx->pmeth || !ctx->pmeth->ctrl) {
  171. OPENSSL_PUT_ERROR(EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  172. return 0;
  173. }
  174. if (keytype != -1 && ctx->pmeth->pkey_id != keytype) {
  175. return 0;
  176. }
  177. if (ctx->operation == EVP_PKEY_OP_UNDEFINED) {
  178. OPENSSL_PUT_ERROR(EVP, EVP_R_NO_OPERATION_SET);
  179. return 0;
  180. }
  181. if (optype != -1 && !(ctx->operation & optype)) {
  182. OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_OPERATION);
  183. return 0;
  184. }
  185. return ctx->pmeth->ctrl(ctx, cmd, p1, p2);
  186. }
  187. int EVP_PKEY_sign_init(EVP_PKEY_CTX *ctx) {
  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. ctx->operation = EVP_PKEY_OP_SIGN;
  193. if (!ctx->pmeth->sign_init) {
  194. return 1;
  195. }
  196. if (!ctx->pmeth->sign_init(ctx)) {
  197. ctx->operation = EVP_PKEY_OP_UNDEFINED;
  198. return 0;
  199. }
  200. return 1;
  201. }
  202. int EVP_PKEY_sign(EVP_PKEY_CTX *ctx, uint8_t *sig, size_t *sig_len,
  203. const uint8_t *data, size_t data_len) {
  204. if (!ctx || !ctx->pmeth || !ctx->pmeth->sign) {
  205. OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  206. return 0;
  207. }
  208. if (ctx->operation != EVP_PKEY_OP_SIGN) {
  209. OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATON_NOT_INITIALIZED);
  210. return 0;
  211. }
  212. return ctx->pmeth->sign(ctx, sig, sig_len, data, data_len);
  213. }
  214. int EVP_PKEY_verify_init(EVP_PKEY_CTX *ctx) {
  215. if (!ctx || !ctx->pmeth || !ctx->pmeth->verify) {
  216. OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  217. return 0;
  218. }
  219. ctx->operation = EVP_PKEY_OP_VERIFY;
  220. if (!ctx->pmeth->verify_init) {
  221. return 1;
  222. }
  223. if (!ctx->pmeth->verify_init(ctx)) {
  224. ctx->operation = EVP_PKEY_OP_UNDEFINED;
  225. return 0;
  226. }
  227. return 1;
  228. }
  229. int EVP_PKEY_verify(EVP_PKEY_CTX *ctx, const uint8_t *sig, size_t sig_len,
  230. const uint8_t *data, size_t data_len) {
  231. if (!ctx || !ctx->pmeth || !ctx->pmeth->verify) {
  232. OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  233. return 0;
  234. }
  235. if (ctx->operation != EVP_PKEY_OP_VERIFY) {
  236. OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATON_NOT_INITIALIZED);
  237. return 0;
  238. }
  239. return ctx->pmeth->verify(ctx, sig, sig_len, data, data_len);
  240. }
  241. int EVP_PKEY_encrypt_init(EVP_PKEY_CTX *ctx) {
  242. if (!ctx || !ctx->pmeth || !ctx->pmeth->encrypt) {
  243. OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  244. return 0;
  245. }
  246. ctx->operation = EVP_PKEY_OP_ENCRYPT;
  247. if (!ctx->pmeth->encrypt_init) {
  248. return 1;
  249. }
  250. if (!ctx->pmeth->encrypt_init(ctx)) {
  251. ctx->operation = EVP_PKEY_OP_UNDEFINED;
  252. return 0;
  253. }
  254. return 1;
  255. }
  256. int EVP_PKEY_encrypt(EVP_PKEY_CTX *ctx, uint8_t *out, size_t *outlen,
  257. const uint8_t *in, size_t inlen) {
  258. if (!ctx || !ctx->pmeth || !ctx->pmeth->encrypt) {
  259. OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  260. return 0;
  261. }
  262. if (ctx->operation != EVP_PKEY_OP_ENCRYPT) {
  263. OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATON_NOT_INITIALIZED);
  264. return 0;
  265. }
  266. return ctx->pmeth->encrypt(ctx, out, outlen, in, inlen);
  267. }
  268. int EVP_PKEY_decrypt_init(EVP_PKEY_CTX *ctx) {
  269. if (!ctx || !ctx->pmeth || !ctx->pmeth->decrypt) {
  270. OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  271. return 0;
  272. }
  273. ctx->operation = EVP_PKEY_OP_DECRYPT;
  274. if (!ctx->pmeth->decrypt_init) {
  275. return 1;
  276. }
  277. if (!ctx->pmeth->decrypt_init(ctx)) {
  278. ctx->operation = EVP_PKEY_OP_UNDEFINED;
  279. return 0;
  280. }
  281. return 1;
  282. }
  283. int EVP_PKEY_decrypt(EVP_PKEY_CTX *ctx, uint8_t *out, size_t *outlen,
  284. const uint8_t *in, size_t inlen) {
  285. if (!ctx || !ctx->pmeth || !ctx->pmeth->decrypt) {
  286. OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  287. return 0;
  288. }
  289. if (ctx->operation != EVP_PKEY_OP_DECRYPT) {
  290. OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATON_NOT_INITIALIZED);
  291. return 0;
  292. }
  293. return ctx->pmeth->decrypt(ctx, out, outlen, in, inlen);
  294. }
  295. int EVP_PKEY_verify_recover_init(EVP_PKEY_CTX *ctx) {
  296. if (!ctx || !ctx->pmeth || !ctx->pmeth->verify_recover) {
  297. OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  298. return 0;
  299. }
  300. ctx->operation = EVP_PKEY_OP_VERIFYRECOVER;
  301. if (!ctx->pmeth->verify_recover_init) {
  302. return 1;
  303. }
  304. if (!ctx->pmeth->verify_recover_init(ctx)) {
  305. ctx->operation = EVP_PKEY_OP_UNDEFINED;
  306. return 0;
  307. }
  308. return 1;
  309. }
  310. int EVP_PKEY_verify_recover(EVP_PKEY_CTX *ctx, uint8_t *out, size_t *out_len,
  311. const uint8_t *sig, size_t sig_len) {
  312. if (!ctx || !ctx->pmeth || !ctx->pmeth->verify_recover) {
  313. OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  314. return 0;
  315. }
  316. if (ctx->operation != EVP_PKEY_OP_VERIFYRECOVER) {
  317. OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATON_NOT_INITIALIZED);
  318. return 0;
  319. }
  320. return ctx->pmeth->verify_recover(ctx, out, out_len, sig, sig_len);
  321. }
  322. int EVP_PKEY_derive_init(EVP_PKEY_CTX *ctx) {
  323. if (!ctx || !ctx->pmeth || !ctx->pmeth->derive) {
  324. OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  325. return 0;
  326. }
  327. ctx->operation = EVP_PKEY_OP_DERIVE;
  328. if (!ctx->pmeth->derive_init) {
  329. return 1;
  330. }
  331. if (!ctx->pmeth->derive_init(ctx)) {
  332. ctx->operation = EVP_PKEY_OP_UNDEFINED;
  333. return 0;
  334. }
  335. return 1;
  336. }
  337. int EVP_PKEY_derive_set_peer(EVP_PKEY_CTX *ctx, EVP_PKEY *peer) {
  338. int ret;
  339. if (!ctx || !ctx->pmeth ||
  340. !(ctx->pmeth->derive || ctx->pmeth->encrypt || ctx->pmeth->decrypt) ||
  341. !ctx->pmeth->ctrl) {
  342. OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  343. return 0;
  344. }
  345. if (ctx->operation != EVP_PKEY_OP_DERIVE &&
  346. ctx->operation != EVP_PKEY_OP_ENCRYPT &&
  347. ctx->operation != EVP_PKEY_OP_DECRYPT) {
  348. OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATON_NOT_INITIALIZED);
  349. return 0;
  350. }
  351. ret = ctx->pmeth->ctrl(ctx, EVP_PKEY_CTRL_PEER_KEY, 0, peer);
  352. if (ret <= 0) {
  353. return 0;
  354. }
  355. if (ret == 2) {
  356. return 1;
  357. }
  358. if (!ctx->pkey) {
  359. OPENSSL_PUT_ERROR(EVP, EVP_R_NO_KEY_SET);
  360. return 0;
  361. }
  362. if (ctx->pkey->type != peer->type) {
  363. OPENSSL_PUT_ERROR(EVP, EVP_R_DIFFERENT_KEY_TYPES);
  364. return 0;
  365. }
  366. /* ran@cryptocom.ru: For clarity. The error is if parameters in peer are
  367. * present (!missing) but don't match. EVP_PKEY_cmp_parameters may return
  368. * 1 (match), 0 (don't match) and -2 (comparison is not defined). -1
  369. * (different key types) is impossible here because it is checked earlier.
  370. * -2 is OK for us here, as well as 1, so we can check for 0 only. */
  371. if (!EVP_PKEY_missing_parameters(peer) &&
  372. !EVP_PKEY_cmp_parameters(ctx->pkey, peer)) {
  373. OPENSSL_PUT_ERROR(EVP, EVP_R_DIFFERENT_PARAMETERS);
  374. return 0;
  375. }
  376. EVP_PKEY_free(ctx->peerkey);
  377. ctx->peerkey = peer;
  378. ret = ctx->pmeth->ctrl(ctx, EVP_PKEY_CTRL_PEER_KEY, 1, peer);
  379. if (ret <= 0) {
  380. ctx->peerkey = NULL;
  381. return 0;
  382. }
  383. EVP_PKEY_up_ref(peer);
  384. return 1;
  385. }
  386. int EVP_PKEY_derive(EVP_PKEY_CTX *ctx, uint8_t *key, size_t *out_key_len) {
  387. if (!ctx || !ctx->pmeth || !ctx->pmeth->derive) {
  388. OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  389. return 0;
  390. }
  391. if (ctx->operation != EVP_PKEY_OP_DERIVE) {
  392. OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATON_NOT_INITIALIZED);
  393. return 0;
  394. }
  395. return ctx->pmeth->derive(ctx, key, out_key_len);
  396. }
  397. int EVP_PKEY_keygen_init(EVP_PKEY_CTX *ctx) {
  398. if (!ctx || !ctx->pmeth || !ctx->pmeth->keygen) {
  399. OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  400. return 0;
  401. }
  402. ctx->operation = EVP_PKEY_OP_KEYGEN;
  403. if (!ctx->pmeth->keygen_init) {
  404. return 1;
  405. }
  406. if (!ctx->pmeth->keygen_init(ctx)) {
  407. ctx->operation = EVP_PKEY_OP_UNDEFINED;
  408. return 0;
  409. }
  410. return 1;
  411. }
  412. int EVP_PKEY_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey) {
  413. if (!ctx || !ctx->pmeth || !ctx->pmeth->keygen) {
  414. OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  415. return 0;
  416. }
  417. if (ctx->operation != EVP_PKEY_OP_KEYGEN) {
  418. OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATON_NOT_INITIALIZED);
  419. return 0;
  420. }
  421. if (!ppkey) {
  422. return 0;
  423. }
  424. if (!*ppkey) {
  425. *ppkey = EVP_PKEY_new();
  426. if (!*ppkey) {
  427. OPENSSL_PUT_ERROR(EVP, ERR_LIB_EVP);
  428. return 0;
  429. }
  430. }
  431. if (!ctx->pmeth->keygen(ctx, *ppkey)) {
  432. EVP_PKEY_free(*ppkey);
  433. *ppkey = NULL;
  434. return 0;
  435. }
  436. return 1;
  437. }