pmeth_fn.c 12 KB

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