pmeth_fn.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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 "cryptlib.h"
  62. #include <openssl/objects.h>
  63. #include <openssl/evp.h>
  64. #include "evp_locl.h"
  65. #define M_check_autoarg(ctx, arg, arglen, err) \
  66. if (ctx->pmeth->flags & EVP_PKEY_FLAG_AUTOARGLEN) { \
  67. size_t pksize = (size_t)EVP_PKEY_size(ctx->pkey); \
  68. \
  69. if (pksize == 0) { \
  70. EVPerr(err, EVP_R_INVALID_KEY); /*ckerr_ignore*/ \
  71. return 0; \
  72. } \
  73. if (!arg) { \
  74. *arglen = pksize; \
  75. return 1; \
  76. } \
  77. if (*arglen < pksize) { \
  78. EVPerr(err, EVP_R_BUFFER_TOO_SMALL); /*ckerr_ignore*/ \
  79. return 0; \
  80. } \
  81. }
  82. int EVP_PKEY_sign_init(EVP_PKEY_CTX *ctx)
  83. {
  84. int ret;
  85. if (!ctx || !ctx->pmeth || !ctx->pmeth->sign) {
  86. EVPerr(EVP_F_EVP_PKEY_SIGN_INIT,
  87. EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  88. return -2;
  89. }
  90. ctx->operation = EVP_PKEY_OP_SIGN;
  91. if (!ctx->pmeth->sign_init)
  92. return 1;
  93. ret = ctx->pmeth->sign_init(ctx);
  94. if (ret <= 0)
  95. ctx->operation = EVP_PKEY_OP_UNDEFINED;
  96. return ret;
  97. }
  98. int EVP_PKEY_sign(EVP_PKEY_CTX *ctx,
  99. unsigned char *sig, size_t *siglen,
  100. const unsigned char *tbs, size_t tbslen)
  101. {
  102. if (!ctx || !ctx->pmeth || !ctx->pmeth->sign) {
  103. EVPerr(EVP_F_EVP_PKEY_SIGN,
  104. EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  105. return -2;
  106. }
  107. if (ctx->operation != EVP_PKEY_OP_SIGN) {
  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. EVPerr(EVP_F_EVP_PKEY_VERIFY_INIT,
  119. EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  120. return -2;
  121. }
  122. ctx->operation = EVP_PKEY_OP_VERIFY;
  123. if (!ctx->pmeth->verify_init)
  124. return 1;
  125. ret = ctx->pmeth->verify_init(ctx);
  126. if (ret <= 0)
  127. ctx->operation = EVP_PKEY_OP_UNDEFINED;
  128. return ret;
  129. }
  130. int EVP_PKEY_verify(EVP_PKEY_CTX *ctx,
  131. const unsigned char *sig, size_t siglen,
  132. const unsigned char *tbs, size_t tbslen)
  133. {
  134. if (!ctx || !ctx->pmeth || !ctx->pmeth->verify) {
  135. EVPerr(EVP_F_EVP_PKEY_VERIFY,
  136. EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  137. return -2;
  138. }
  139. if (ctx->operation != EVP_PKEY_OP_VERIFY) {
  140. EVPerr(EVP_F_EVP_PKEY_VERIFY, EVP_R_OPERATON_NOT_INITIALIZED);
  141. return -1;
  142. }
  143. return ctx->pmeth->verify(ctx, sig, siglen, tbs, tbslen);
  144. }
  145. int EVP_PKEY_verify_recover_init(EVP_PKEY_CTX *ctx)
  146. {
  147. int ret;
  148. if (!ctx || !ctx->pmeth || !ctx->pmeth->verify_recover) {
  149. EVPerr(EVP_F_EVP_PKEY_VERIFY_RECOVER_INIT,
  150. EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  151. return -2;
  152. }
  153. ctx->operation = EVP_PKEY_OP_VERIFYRECOVER;
  154. if (!ctx->pmeth->verify_recover_init)
  155. return 1;
  156. ret = ctx->pmeth->verify_recover_init(ctx);
  157. if (ret <= 0)
  158. ctx->operation = EVP_PKEY_OP_UNDEFINED;
  159. return ret;
  160. }
  161. int EVP_PKEY_verify_recover(EVP_PKEY_CTX *ctx,
  162. unsigned char *rout, size_t *routlen,
  163. const unsigned char *sig, size_t siglen)
  164. {
  165. if (!ctx || !ctx->pmeth || !ctx->pmeth->verify_recover) {
  166. EVPerr(EVP_F_EVP_PKEY_VERIFY_RECOVER,
  167. EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  168. return -2;
  169. }
  170. if (ctx->operation != EVP_PKEY_OP_VERIFYRECOVER) {
  171. EVPerr(EVP_F_EVP_PKEY_VERIFY_RECOVER, EVP_R_OPERATON_NOT_INITIALIZED);
  172. return -1;
  173. }
  174. M_check_autoarg(ctx, rout, routlen, EVP_F_EVP_PKEY_VERIFY_RECOVER)
  175. return ctx->pmeth->verify_recover(ctx, rout, routlen, sig, siglen);
  176. }
  177. int EVP_PKEY_encrypt_init(EVP_PKEY_CTX *ctx)
  178. {
  179. int ret;
  180. if (!ctx || !ctx->pmeth || !ctx->pmeth->encrypt) {
  181. EVPerr(EVP_F_EVP_PKEY_ENCRYPT_INIT,
  182. EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  183. return -2;
  184. }
  185. ctx->operation = EVP_PKEY_OP_ENCRYPT;
  186. if (!ctx->pmeth->encrypt_init)
  187. return 1;
  188. ret = ctx->pmeth->encrypt_init(ctx);
  189. if (ret <= 0)
  190. ctx->operation = EVP_PKEY_OP_UNDEFINED;
  191. return ret;
  192. }
  193. int EVP_PKEY_encrypt(EVP_PKEY_CTX *ctx,
  194. unsigned char *out, size_t *outlen,
  195. const unsigned char *in, size_t inlen)
  196. {
  197. if (!ctx || !ctx->pmeth || !ctx->pmeth->encrypt) {
  198. EVPerr(EVP_F_EVP_PKEY_ENCRYPT,
  199. EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  200. return -2;
  201. }
  202. if (ctx->operation != EVP_PKEY_OP_ENCRYPT) {
  203. EVPerr(EVP_F_EVP_PKEY_ENCRYPT, EVP_R_OPERATON_NOT_INITIALIZED);
  204. return -1;
  205. }
  206. M_check_autoarg(ctx, out, outlen, EVP_F_EVP_PKEY_ENCRYPT)
  207. return ctx->pmeth->encrypt(ctx, out, outlen, in, inlen);
  208. }
  209. int EVP_PKEY_decrypt_init(EVP_PKEY_CTX *ctx)
  210. {
  211. int ret;
  212. if (!ctx || !ctx->pmeth || !ctx->pmeth->decrypt) {
  213. EVPerr(EVP_F_EVP_PKEY_DECRYPT_INIT,
  214. EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  215. return -2;
  216. }
  217. ctx->operation = EVP_PKEY_OP_DECRYPT;
  218. if (!ctx->pmeth->decrypt_init)
  219. return 1;
  220. ret = ctx->pmeth->decrypt_init(ctx);
  221. if (ret <= 0)
  222. ctx->operation = EVP_PKEY_OP_UNDEFINED;
  223. return ret;
  224. }
  225. int EVP_PKEY_decrypt(EVP_PKEY_CTX *ctx,
  226. unsigned char *out, size_t *outlen,
  227. const unsigned char *in, size_t inlen)
  228. {
  229. if (!ctx || !ctx->pmeth || !ctx->pmeth->decrypt) {
  230. EVPerr(EVP_F_EVP_PKEY_DECRYPT,
  231. EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  232. return -2;
  233. }
  234. if (ctx->operation != EVP_PKEY_OP_DECRYPT) {
  235. EVPerr(EVP_F_EVP_PKEY_DECRYPT, EVP_R_OPERATON_NOT_INITIALIZED);
  236. return -1;
  237. }
  238. M_check_autoarg(ctx, out, outlen, EVP_F_EVP_PKEY_DECRYPT)
  239. return ctx->pmeth->decrypt(ctx, out, outlen, in, inlen);
  240. }
  241. int EVP_PKEY_derive_init(EVP_PKEY_CTX *ctx)
  242. {
  243. int ret;
  244. if (!ctx || !ctx->pmeth || !ctx->pmeth->derive) {
  245. EVPerr(EVP_F_EVP_PKEY_DERIVE_INIT,
  246. EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  247. return -2;
  248. }
  249. ctx->operation = EVP_PKEY_OP_DERIVE;
  250. if (!ctx->pmeth->derive_init)
  251. return 1;
  252. ret = ctx->pmeth->derive_init(ctx);
  253. if (ret <= 0)
  254. ctx->operation = EVP_PKEY_OP_UNDEFINED;
  255. return ret;
  256. }
  257. int EVP_PKEY_derive_set_peer(EVP_PKEY_CTX *ctx, EVP_PKEY *peer)
  258. {
  259. int ret;
  260. if (!ctx || !ctx->pmeth
  261. || !(ctx->pmeth->derive || ctx->pmeth->encrypt || ctx->pmeth->decrypt)
  262. || !ctx->pmeth->ctrl) {
  263. EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER,
  264. EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  265. return -2;
  266. }
  267. if (ctx->operation != EVP_PKEY_OP_DERIVE
  268. && ctx->operation != EVP_PKEY_OP_ENCRYPT
  269. && ctx->operation != EVP_PKEY_OP_DECRYPT) {
  270. EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER,
  271. EVP_R_OPERATON_NOT_INITIALIZED);
  272. return -1;
  273. }
  274. ret = ctx->pmeth->ctrl(ctx, EVP_PKEY_CTRL_PEER_KEY, 0, peer);
  275. if (ret <= 0)
  276. return ret;
  277. if (ret == 2)
  278. return 1;
  279. if (!ctx->pkey) {
  280. EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER, EVP_R_NO_KEY_SET);
  281. return -1;
  282. }
  283. if (ctx->pkey->type != peer->type) {
  284. EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER, EVP_R_DIFFERENT_KEY_TYPES);
  285. return -1;
  286. }
  287. /*
  288. * ran@cryptocom.ru: For clarity. The error is if parameters in peer are
  289. * present (!missing) but don't match. EVP_PKEY_cmp_parameters may return
  290. * 1 (match), 0 (don't match) and -2 (comparison is not defined). -1
  291. * (different key types) is impossible here because it is checked earlier.
  292. * -2 is OK for us here, as well as 1, so we can check for 0 only.
  293. */
  294. if (!EVP_PKEY_missing_parameters(peer) &&
  295. !EVP_PKEY_cmp_parameters(ctx->pkey, peer)) {
  296. EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER, EVP_R_DIFFERENT_PARAMETERS);
  297. return -1;
  298. }
  299. if (ctx->peerkey)
  300. EVP_PKEY_free(ctx->peerkey);
  301. ctx->peerkey = peer;
  302. ret = ctx->pmeth->ctrl(ctx, EVP_PKEY_CTRL_PEER_KEY, 1, peer);
  303. if (ret <= 0) {
  304. ctx->peerkey = NULL;
  305. return ret;
  306. }
  307. CRYPTO_add(&peer->references, 1, CRYPTO_LOCK_EVP_PKEY);
  308. return 1;
  309. }
  310. int EVP_PKEY_derive(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *pkeylen)
  311. {
  312. if (!ctx || !ctx->pmeth || !ctx->pmeth->derive) {
  313. EVPerr(EVP_F_EVP_PKEY_DERIVE,
  314. EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  315. return -2;
  316. }
  317. if (ctx->operation != EVP_PKEY_OP_DERIVE) {
  318. EVPerr(EVP_F_EVP_PKEY_DERIVE, EVP_R_OPERATON_NOT_INITIALIZED);
  319. return -1;
  320. }
  321. M_check_autoarg(ctx, key, pkeylen, EVP_F_EVP_PKEY_DERIVE)
  322. return ctx->pmeth->derive(ctx, key, pkeylen);
  323. }