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.
 
 
 
 
 
 

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