Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

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