pmeth_fn.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. /* pmeth_fn.c */
  2. /* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
  3. * project 2006.
  4. */
  5. /* ====================================================================
  6. * Copyright (c) 2006 The OpenSSL Project. All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. All advertising materials mentioning features or use of this
  21. * software must display the following acknowledgment:
  22. * "This product includes software developed by the OpenSSL Project
  23. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  24. *
  25. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  26. * endorse or promote products derived from this software without
  27. * prior written permission. For written permission, please contact
  28. * licensing@OpenSSL.org.
  29. *
  30. * 5. Products derived from this software may not be called "OpenSSL"
  31. * nor may "OpenSSL" appear in their names without prior written
  32. * permission of the OpenSSL Project.
  33. *
  34. * 6. Redistributions of any form whatsoever must retain the following
  35. * acknowledgment:
  36. * "This product includes software developed by the OpenSSL Project
  37. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  38. *
  39. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  40. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  41. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  42. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  43. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  44. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  45. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  46. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  48. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  49. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  50. * OF THE POSSIBILITY OF SUCH DAMAGE.
  51. * ====================================================================
  52. *
  53. * This product includes cryptographic software written by Eric Young
  54. * (eay@cryptsoft.com). This product includes software written by Tim
  55. * Hudson (tjh@cryptsoft.com).
  56. *
  57. */
  58. #include <stdio.h>
  59. #include <stdlib.h>
  60. #include "cryptlib.h"
  61. #include <openssl/objects.h>
  62. #include <openssl/evp.h>
  63. #include "evp_locl.h"
  64. #define M_check_autoarg(ctx, arg, arglen, err) \
  65. if (ctx->pmeth->flags & EVP_PKEY_FLAG_AUTOARGLEN) \
  66. { \
  67. int pksize = EVP_PKEY_size(ctx->pkey); \
  68. if (!arg) \
  69. { \
  70. *arglen = pksize; \
  71. return 1; \
  72. } \
  73. else if (*arglen < pksize) \
  74. { \
  75. EVPerr(err, EVP_R_BUFFER_TOO_SMALL); \
  76. return 0; \
  77. } \
  78. }
  79. int EVP_PKEY_sign_init(EVP_PKEY_CTX *ctx)
  80. {
  81. int ret;
  82. if (!ctx || !ctx->pmeth || !ctx->pmeth->sign)
  83. {
  84. EVPerr(EVP_F_EVP_PKEY_SIGN_INIT,
  85. EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  86. return -2;
  87. }
  88. ctx->operation = EVP_PKEY_OP_SIGN;
  89. if (!ctx->pmeth->sign_init)
  90. return 1;
  91. ret = ctx->pmeth->sign_init(ctx);
  92. if (ret <= 0)
  93. ctx->operation = EVP_PKEY_OP_UNDEFINED;
  94. return ret;
  95. }
  96. int EVP_PKEY_sign(EVP_PKEY_CTX *ctx,
  97. unsigned char *sig, int *siglen,
  98. const unsigned char *tbs, int tbslen)
  99. {
  100. if (!ctx || !ctx->pmeth || !ctx->pmeth->sign)
  101. {
  102. EVPerr(EVP_F_EVP_PKEY_SIGN,
  103. EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  104. return -2;
  105. }
  106. if (ctx->operation != EVP_PKEY_OP_SIGN)
  107. {
  108. EVPerr(EVP_F_EVP_PKEY_SIGN, EVP_R_OPERATON_NOT_INITIALIZED);
  109. return -1;
  110. }
  111. M_check_autoarg(ctx, sig, siglen, EVP_F_EVP_PKEY_SIGN)
  112. return ctx->pmeth->sign(ctx, sig, siglen, tbs, tbslen);
  113. }
  114. int EVP_PKEY_verify_init(EVP_PKEY_CTX *ctx)
  115. {
  116. int ret;
  117. if (!ctx || !ctx->pmeth || !ctx->pmeth->verify)
  118. {
  119. EVPerr(EVP_F_EVP_PKEY_VERIFY_INIT,
  120. EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  121. return -2;
  122. }
  123. ctx->operation = EVP_PKEY_OP_VERIFY;
  124. if (!ctx->pmeth->verify_init)
  125. return 1;
  126. ret = ctx->pmeth->verify_init(ctx);
  127. if (ret <= 0)
  128. ctx->operation = EVP_PKEY_OP_UNDEFINED;
  129. return ret;
  130. }
  131. int EVP_PKEY_verify(EVP_PKEY_CTX *ctx,
  132. const unsigned char *sig, int siglen,
  133. const unsigned char *tbs, int tbslen)
  134. {
  135. if (!ctx || !ctx->pmeth || !ctx->pmeth->verify)
  136. {
  137. EVPerr(EVP_F_EVP_PKEY_VERIFY,
  138. EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  139. return -2;
  140. }
  141. if (ctx->operation != EVP_PKEY_OP_VERIFY)
  142. {
  143. EVPerr(EVP_F_EVP_PKEY_VERIFY, EVP_R_OPERATON_NOT_INITIALIZED);
  144. return -1;
  145. }
  146. return ctx->pmeth->verify(ctx, sig, siglen, tbs, tbslen);
  147. }
  148. int EVP_PKEY_verify_recover_init(EVP_PKEY_CTX *ctx)
  149. {
  150. int ret;
  151. if (!ctx || !ctx->pmeth || !ctx->pmeth->verify_recover)
  152. {
  153. EVPerr(EVP_F_EVP_PKEY_VERIFY_RECOVER_INIT,
  154. EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  155. return -2;
  156. }
  157. ctx->operation = EVP_PKEY_OP_VERIFYRECOVER;
  158. if (!ctx->pmeth->verify_recover_init)
  159. return 1;
  160. ret = ctx->pmeth->verify_recover_init(ctx);
  161. if (ret <= 0)
  162. ctx->operation = EVP_PKEY_OP_UNDEFINED;
  163. return ret;
  164. }
  165. int EVP_PKEY_verify_recover(EVP_PKEY_CTX *ctx,
  166. unsigned char *rout, int *routlen,
  167. const unsigned char *sig, int siglen)
  168. {
  169. if (!ctx || !ctx->pmeth || !ctx->pmeth->verify_recover)
  170. {
  171. EVPerr(EVP_F_EVP_PKEY_VERIFY_RECOVER,
  172. EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  173. return -2;
  174. }
  175. if (ctx->operation != EVP_PKEY_OP_VERIFYRECOVER)
  176. {
  177. EVPerr(EVP_F_EVP_PKEY_VERIFY_RECOVER, EVP_R_OPERATON_NOT_INITIALIZED);
  178. return -1;
  179. }
  180. M_check_autoarg(ctx, rout, routlen, EVP_F_EVP_PKEY_VERIFY_RECOVER)
  181. return ctx->pmeth->verify_recover(ctx, rout, routlen, sig, siglen);
  182. }
  183. int EVP_PKEY_encrypt_init(EVP_PKEY_CTX *ctx)
  184. {
  185. int ret;
  186. if (!ctx || !ctx->pmeth || !ctx->pmeth->encrypt)
  187. {
  188. EVPerr(EVP_F_EVP_PKEY_ENCRYPT_INIT,
  189. EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  190. return -2;
  191. }
  192. ctx->operation = EVP_PKEY_OP_ENCRYPT;
  193. if (!ctx->pmeth->encrypt_init)
  194. return 1;
  195. ret = ctx->pmeth->encrypt_init(ctx);
  196. if (ret <= 0)
  197. ctx->operation = EVP_PKEY_OP_UNDEFINED;
  198. return ret;
  199. }
  200. int EVP_PKEY_encrypt(EVP_PKEY_CTX *ctx,
  201. unsigned char *out, int *outlen,
  202. const unsigned char *in, int inlen)
  203. {
  204. if (!ctx || !ctx->pmeth || !ctx->pmeth->encrypt)
  205. {
  206. EVPerr(EVP_F_EVP_PKEY_ENCRYPT,
  207. EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  208. return -2;
  209. }
  210. if (ctx->operation != EVP_PKEY_OP_ENCRYPT)
  211. {
  212. EVPerr(EVP_F_EVP_PKEY_ENCRYPT, EVP_R_OPERATON_NOT_INITIALIZED);
  213. return -1;
  214. }
  215. M_check_autoarg(ctx, out, outlen, EVP_F_EVP_PKEY_ENCRYPT)
  216. return ctx->pmeth->encrypt(ctx, out, outlen, in, inlen);
  217. }
  218. int EVP_PKEY_decrypt_init(EVP_PKEY_CTX *ctx)
  219. {
  220. int ret;
  221. if (!ctx || !ctx->pmeth || !ctx->pmeth->decrypt)
  222. {
  223. EVPerr(EVP_F_EVP_PKEY_DECRYPT_INIT,
  224. EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  225. return -2;
  226. }
  227. ctx->operation = EVP_PKEY_OP_DECRYPT;
  228. if (!ctx->pmeth->decrypt_init)
  229. return 1;
  230. ret = ctx->pmeth->decrypt_init(ctx);
  231. if (ret <= 0)
  232. ctx->operation = EVP_PKEY_OP_UNDEFINED;
  233. return ret;
  234. }
  235. int EVP_PKEY_decrypt(EVP_PKEY_CTX *ctx,
  236. unsigned char *out, int *outlen,
  237. const unsigned char *in, int inlen)
  238. {
  239. if (!ctx || !ctx->pmeth || !ctx->pmeth->decrypt)
  240. {
  241. EVPerr(EVP_F_EVP_PKEY_DECRYPT,
  242. EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  243. return -2;
  244. }
  245. if (ctx->operation != EVP_PKEY_OP_DECRYPT)
  246. {
  247. EVPerr(EVP_F_EVP_PKEY_DECRYPT, EVP_R_OPERATON_NOT_INITIALIZED);
  248. return -1;
  249. }
  250. M_check_autoarg(ctx, out, outlen, EVP_F_EVP_PKEY_DECRYPT)
  251. return ctx->pmeth->decrypt(ctx, out, outlen, in, inlen);
  252. }
  253. int EVP_PKEY_derive_init(EVP_PKEY_CTX *ctx)
  254. {
  255. int ret;
  256. if (!ctx || !ctx->pmeth || !ctx->pmeth->derive)
  257. {
  258. EVPerr(EVP_F_EVP_PKEY_DERIVE_INIT,
  259. EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  260. return -2;
  261. }
  262. ctx->operation = EVP_PKEY_OP_DERIVE;
  263. if (!ctx->pmeth->derive_init)
  264. return 1;
  265. ret = ctx->pmeth->derive_init(ctx);
  266. if (ret <= 0)
  267. ctx->operation = EVP_PKEY_OP_UNDEFINED;
  268. return ret;
  269. }
  270. int EVP_PKEY_derive_set_peer(EVP_PKEY_CTX *ctx, EVP_PKEY *peer)
  271. {
  272. int ret;
  273. if (!ctx || !ctx->pmeth || !ctx->pmeth->derive || !ctx->pmeth->ctrl)
  274. {
  275. EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER,
  276. EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  277. return -2;
  278. }
  279. if (ctx->operation != EVP_PKEY_OP_DERIVE)
  280. {
  281. EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER,
  282. EVP_R_OPERATON_NOT_INITIALIZED);
  283. return -1;
  284. }
  285. ret = ctx->pmeth->ctrl(ctx, EVP_PKEY_CTRL_PEER_KEY, 0, peer);
  286. if (ret <= 0)
  287. return ret;
  288. if (ret == 2)
  289. return 1;
  290. if (!ctx->pkey)
  291. {
  292. EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER, EVP_R_NO_KEY_SET);
  293. return -1;
  294. }
  295. if (ctx->pkey->type != peer->type)
  296. {
  297. EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER,
  298. EVP_R_DIFFERENT_KEY_TYPES);
  299. return -1;
  300. }
  301. if (!EVP_PKEY_missing_parameters(peer) &&
  302. !EVP_PKEY_cmp_parameters(ctx->pkey, peer))
  303. {
  304. EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER,
  305. EVP_R_DIFFERENT_PARAMETERS);
  306. return -1;
  307. }
  308. ctx->peerkey = peer;
  309. ret = ctx->pmeth->ctrl(ctx, EVP_PKEY_CTRL_PEER_KEY, 1, peer);
  310. if (ret <= 0)
  311. {
  312. ctx->peerkey = NULL;
  313. return ret;
  314. }
  315. CRYPTO_add(&peer->references,1,CRYPTO_LOCK_EVP_PKEY);
  316. return 1;
  317. }
  318. int EVP_PKEY_derive(EVP_PKEY_CTX *ctx, unsigned char *key, int *pkeylen)
  319. {
  320. if (!ctx || !ctx->pmeth || !ctx->pmeth->derive)
  321. {
  322. EVPerr(EVP_F_EVP_PKEY_DERIVE,
  323. EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  324. return -2;
  325. }
  326. if (ctx->operation != EVP_PKEY_OP_DERIVE)
  327. {
  328. EVPerr(EVP_F_EVP_PKEY_DERIVE, EVP_R_OPERATON_NOT_INITIALIZED);
  329. return -1;
  330. }
  331. M_check_autoarg(ctx, key, pkeylen, EVP_F_EVP_PKEY_DERIVE)
  332. return ctx->pmeth->derive(ctx, key, pkeylen);
  333. }