pmeth_fn.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /*
  2. * Copyright 2006-2016 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include "internal/cryptlib.h"
  12. #include <openssl/objects.h>
  13. #include <openssl/evp.h>
  14. #include "crypto/evp.h"
  15. #define M_check_autoarg(ctx, arg, arglen, err) \
  16. if (ctx->pmeth->flags & EVP_PKEY_FLAG_AUTOARGLEN) { \
  17. size_t pksize = (size_t)EVP_PKEY_size(ctx->pkey); \
  18. \
  19. if (pksize == 0) { \
  20. EVPerr(err, EVP_R_INVALID_KEY); /*ckerr_ignore*/ \
  21. return 0; \
  22. } \
  23. if (!arg) { \
  24. *arglen = pksize; \
  25. return 1; \
  26. } \
  27. if (*arglen < pksize) { \
  28. EVPerr(err, EVP_R_BUFFER_TOO_SMALL); /*ckerr_ignore*/ \
  29. return 0; \
  30. } \
  31. }
  32. int EVP_PKEY_sign_init(EVP_PKEY_CTX *ctx)
  33. {
  34. int ret;
  35. if (!ctx || !ctx->pmeth || !ctx->pmeth->sign) {
  36. EVPerr(EVP_F_EVP_PKEY_SIGN_INIT,
  37. EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  38. return -2;
  39. }
  40. ctx->operation = EVP_PKEY_OP_SIGN;
  41. if (!ctx->pmeth->sign_init)
  42. return 1;
  43. ret = ctx->pmeth->sign_init(ctx);
  44. if (ret <= 0)
  45. ctx->operation = EVP_PKEY_OP_UNDEFINED;
  46. return ret;
  47. }
  48. int EVP_PKEY_sign(EVP_PKEY_CTX *ctx,
  49. unsigned char *sig, size_t *siglen,
  50. const unsigned char *tbs, size_t tbslen)
  51. {
  52. if (!ctx || !ctx->pmeth || !ctx->pmeth->sign) {
  53. EVPerr(EVP_F_EVP_PKEY_SIGN,
  54. EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  55. return -2;
  56. }
  57. if (ctx->operation != EVP_PKEY_OP_SIGN) {
  58. EVPerr(EVP_F_EVP_PKEY_SIGN, EVP_R_OPERATON_NOT_INITIALIZED);
  59. return -1;
  60. }
  61. M_check_autoarg(ctx, sig, siglen, EVP_F_EVP_PKEY_SIGN)
  62. return ctx->pmeth->sign(ctx, sig, siglen, tbs, tbslen);
  63. }
  64. int EVP_PKEY_verify_init(EVP_PKEY_CTX *ctx)
  65. {
  66. int ret;
  67. if (!ctx || !ctx->pmeth || !ctx->pmeth->verify) {
  68. EVPerr(EVP_F_EVP_PKEY_VERIFY_INIT,
  69. EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  70. return -2;
  71. }
  72. ctx->operation = EVP_PKEY_OP_VERIFY;
  73. if (!ctx->pmeth->verify_init)
  74. return 1;
  75. ret = ctx->pmeth->verify_init(ctx);
  76. if (ret <= 0)
  77. ctx->operation = EVP_PKEY_OP_UNDEFINED;
  78. return ret;
  79. }
  80. int EVP_PKEY_verify(EVP_PKEY_CTX *ctx,
  81. const unsigned char *sig, size_t siglen,
  82. const unsigned char *tbs, size_t tbslen)
  83. {
  84. if (!ctx || !ctx->pmeth || !ctx->pmeth->verify) {
  85. EVPerr(EVP_F_EVP_PKEY_VERIFY,
  86. EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  87. return -2;
  88. }
  89. if (ctx->operation != EVP_PKEY_OP_VERIFY) {
  90. EVPerr(EVP_F_EVP_PKEY_VERIFY, EVP_R_OPERATON_NOT_INITIALIZED);
  91. return -1;
  92. }
  93. return ctx->pmeth->verify(ctx, sig, siglen, tbs, tbslen);
  94. }
  95. int EVP_PKEY_verify_recover_init(EVP_PKEY_CTX *ctx)
  96. {
  97. int ret;
  98. if (!ctx || !ctx->pmeth || !ctx->pmeth->verify_recover) {
  99. EVPerr(EVP_F_EVP_PKEY_VERIFY_RECOVER_INIT,
  100. EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  101. return -2;
  102. }
  103. ctx->operation = EVP_PKEY_OP_VERIFYRECOVER;
  104. if (!ctx->pmeth->verify_recover_init)
  105. return 1;
  106. ret = ctx->pmeth->verify_recover_init(ctx);
  107. if (ret <= 0)
  108. ctx->operation = EVP_PKEY_OP_UNDEFINED;
  109. return ret;
  110. }
  111. int EVP_PKEY_verify_recover(EVP_PKEY_CTX *ctx,
  112. unsigned char *rout, size_t *routlen,
  113. const unsigned char *sig, size_t siglen)
  114. {
  115. if (!ctx || !ctx->pmeth || !ctx->pmeth->verify_recover) {
  116. EVPerr(EVP_F_EVP_PKEY_VERIFY_RECOVER,
  117. EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  118. return -2;
  119. }
  120. if (ctx->operation != EVP_PKEY_OP_VERIFYRECOVER) {
  121. EVPerr(EVP_F_EVP_PKEY_VERIFY_RECOVER, EVP_R_OPERATON_NOT_INITIALIZED);
  122. return -1;
  123. }
  124. M_check_autoarg(ctx, rout, routlen, EVP_F_EVP_PKEY_VERIFY_RECOVER)
  125. return ctx->pmeth->verify_recover(ctx, rout, routlen, sig, siglen);
  126. }
  127. int EVP_PKEY_encrypt_init(EVP_PKEY_CTX *ctx)
  128. {
  129. int ret;
  130. if (!ctx || !ctx->pmeth || !ctx->pmeth->encrypt) {
  131. EVPerr(EVP_F_EVP_PKEY_ENCRYPT_INIT,
  132. EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  133. return -2;
  134. }
  135. ctx->operation = EVP_PKEY_OP_ENCRYPT;
  136. if (!ctx->pmeth->encrypt_init)
  137. return 1;
  138. ret = ctx->pmeth->encrypt_init(ctx);
  139. if (ret <= 0)
  140. ctx->operation = EVP_PKEY_OP_UNDEFINED;
  141. return ret;
  142. }
  143. int EVP_PKEY_encrypt(EVP_PKEY_CTX *ctx,
  144. unsigned char *out, size_t *outlen,
  145. const unsigned char *in, size_t inlen)
  146. {
  147. if (!ctx || !ctx->pmeth || !ctx->pmeth->encrypt) {
  148. EVPerr(EVP_F_EVP_PKEY_ENCRYPT,
  149. EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  150. return -2;
  151. }
  152. if (ctx->operation != EVP_PKEY_OP_ENCRYPT) {
  153. EVPerr(EVP_F_EVP_PKEY_ENCRYPT, EVP_R_OPERATON_NOT_INITIALIZED);
  154. return -1;
  155. }
  156. M_check_autoarg(ctx, out, outlen, EVP_F_EVP_PKEY_ENCRYPT)
  157. return ctx->pmeth->encrypt(ctx, out, outlen, in, inlen);
  158. }
  159. int EVP_PKEY_decrypt_init(EVP_PKEY_CTX *ctx)
  160. {
  161. int ret;
  162. if (!ctx || !ctx->pmeth || !ctx->pmeth->decrypt) {
  163. EVPerr(EVP_F_EVP_PKEY_DECRYPT_INIT,
  164. EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  165. return -2;
  166. }
  167. ctx->operation = EVP_PKEY_OP_DECRYPT;
  168. if (!ctx->pmeth->decrypt_init)
  169. return 1;
  170. ret = ctx->pmeth->decrypt_init(ctx);
  171. if (ret <= 0)
  172. ctx->operation = EVP_PKEY_OP_UNDEFINED;
  173. return ret;
  174. }
  175. int EVP_PKEY_decrypt(EVP_PKEY_CTX *ctx,
  176. unsigned char *out, size_t *outlen,
  177. const unsigned char *in, size_t inlen)
  178. {
  179. if (!ctx || !ctx->pmeth || !ctx->pmeth->decrypt) {
  180. EVPerr(EVP_F_EVP_PKEY_DECRYPT,
  181. EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  182. return -2;
  183. }
  184. if (ctx->operation != EVP_PKEY_OP_DECRYPT) {
  185. EVPerr(EVP_F_EVP_PKEY_DECRYPT, EVP_R_OPERATON_NOT_INITIALIZED);
  186. return -1;
  187. }
  188. M_check_autoarg(ctx, out, outlen, EVP_F_EVP_PKEY_DECRYPT)
  189. return ctx->pmeth->decrypt(ctx, out, outlen, in, inlen);
  190. }
  191. int EVP_PKEY_derive_init(EVP_PKEY_CTX *ctx)
  192. {
  193. int ret;
  194. if (!ctx || !ctx->pmeth || !ctx->pmeth->derive) {
  195. EVPerr(EVP_F_EVP_PKEY_DERIVE_INIT,
  196. EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  197. return -2;
  198. }
  199. ctx->operation = EVP_PKEY_OP_DERIVE;
  200. if (!ctx->pmeth->derive_init)
  201. return 1;
  202. ret = ctx->pmeth->derive_init(ctx);
  203. if (ret <= 0)
  204. ctx->operation = EVP_PKEY_OP_UNDEFINED;
  205. return ret;
  206. }
  207. int EVP_PKEY_derive_set_peer(EVP_PKEY_CTX *ctx, EVP_PKEY *peer)
  208. {
  209. int ret;
  210. if (!ctx || !ctx->pmeth
  211. || !(ctx->pmeth->derive || ctx->pmeth->encrypt || ctx->pmeth->decrypt)
  212. || !ctx->pmeth->ctrl) {
  213. EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER,
  214. EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  215. return -2;
  216. }
  217. if (ctx->operation != EVP_PKEY_OP_DERIVE
  218. && ctx->operation != EVP_PKEY_OP_ENCRYPT
  219. && ctx->operation != EVP_PKEY_OP_DECRYPT) {
  220. EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER,
  221. EVP_R_OPERATON_NOT_INITIALIZED);
  222. return -1;
  223. }
  224. ret = ctx->pmeth->ctrl(ctx, EVP_PKEY_CTRL_PEER_KEY, 0, peer);
  225. if (ret <= 0)
  226. return ret;
  227. if (ret == 2)
  228. return 1;
  229. if (!ctx->pkey) {
  230. EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER, EVP_R_NO_KEY_SET);
  231. return -1;
  232. }
  233. if (ctx->pkey->type != peer->type) {
  234. EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER, EVP_R_DIFFERENT_KEY_TYPES);
  235. return -1;
  236. }
  237. /*
  238. * For clarity. The error is if parameters in peer are
  239. * present (!missing) but don't match. EVP_PKEY_cmp_parameters may return
  240. * 1 (match), 0 (don't match) and -2 (comparison is not defined). -1
  241. * (different key types) is impossible here because it is checked earlier.
  242. * -2 is OK for us here, as well as 1, so we can check for 0 only.
  243. */
  244. if (!EVP_PKEY_missing_parameters(peer) &&
  245. !EVP_PKEY_cmp_parameters(ctx->pkey, peer)) {
  246. EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER, EVP_R_DIFFERENT_PARAMETERS);
  247. return -1;
  248. }
  249. EVP_PKEY_free(ctx->peerkey);
  250. ctx->peerkey = peer;
  251. ret = ctx->pmeth->ctrl(ctx, EVP_PKEY_CTRL_PEER_KEY, 1, peer);
  252. if (ret <= 0) {
  253. ctx->peerkey = NULL;
  254. return ret;
  255. }
  256. EVP_PKEY_up_ref(peer);
  257. return 1;
  258. }
  259. int EVP_PKEY_derive(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *pkeylen)
  260. {
  261. if (!ctx || !ctx->pmeth || !ctx->pmeth->derive) {
  262. EVPerr(EVP_F_EVP_PKEY_DERIVE,
  263. EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  264. return -2;
  265. }
  266. if (ctx->operation != EVP_PKEY_OP_DERIVE) {
  267. EVPerr(EVP_F_EVP_PKEY_DERIVE, EVP_R_OPERATON_NOT_INITIALIZED);
  268. return -1;
  269. }
  270. M_check_autoarg(ctx, key, pkeylen, EVP_F_EVP_PKEY_DERIVE)
  271. return ctx->pmeth->derive(ctx, key, pkeylen);
  272. }