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.
 
 
 
 
 
 

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