Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

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