pmeth_gn.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /*
  2. * Copyright 2006-2016 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (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/bn.h"
  15. #include "crypto/asn1.h"
  16. #include "crypto/evp.h"
  17. #include "evp_local.h"
  18. static int fromdata_init(EVP_PKEY_CTX *ctx, int operation)
  19. {
  20. if (ctx == NULL || ctx->algorithm == NULL)
  21. goto not_supported;
  22. evp_pkey_ctx_free_old_ops(ctx);
  23. ctx->operation = operation;
  24. if (ctx->keymgmt == NULL)
  25. ctx->keymgmt = EVP_KEYMGMT_fetch(NULL, ctx->algorithm, ctx->propquery);
  26. if (ctx->keymgmt == NULL)
  27. goto not_supported;
  28. return 1;
  29. not_supported:
  30. ctx->operation = EVP_PKEY_OP_UNDEFINED;
  31. ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  32. return -2;
  33. }
  34. int EVP_PKEY_param_fromdata_init(EVP_PKEY_CTX *ctx)
  35. {
  36. return fromdata_init(ctx, EVP_PKEY_OP_PARAMFROMDATA);
  37. }
  38. int EVP_PKEY_key_fromdata_init(EVP_PKEY_CTX *ctx)
  39. {
  40. return fromdata_init(ctx, EVP_PKEY_OP_KEYFROMDATA);
  41. }
  42. int EVP_PKEY_fromdata(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey, OSSL_PARAM params[])
  43. {
  44. void *provdata = NULL;
  45. if (ctx == NULL || (ctx->operation & EVP_PKEY_OP_TYPE_FROMDATA) == 0) {
  46. ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  47. return -2;
  48. }
  49. if (ppkey == NULL)
  50. return -1;
  51. if (*ppkey == NULL)
  52. *ppkey = EVP_PKEY_new();
  53. if (*ppkey == NULL) {
  54. ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
  55. return -1;
  56. }
  57. provdata =
  58. evp_keymgmt_fromdata(*ppkey, ctx->keymgmt, params,
  59. ctx->operation == EVP_PKEY_OP_PARAMFROMDATA);
  60. if (provdata == NULL)
  61. return 0;
  62. /* provdata is cached in *ppkey, so we need not bother with it further */
  63. return 1;
  64. }
  65. /*
  66. * TODO(3.0) Re-evaluate the names, it's possible that we find these to be
  67. * better:
  68. *
  69. * EVP_PKEY_param_settable()
  70. * EVP_PKEY_param_gettable()
  71. */
  72. const OSSL_PARAM *EVP_PKEY_param_fromdata_settable(EVP_PKEY_CTX *ctx)
  73. {
  74. /* We call fromdata_init to get ctx->keymgmt populated */
  75. if (fromdata_init(ctx, EVP_PKEY_OP_UNDEFINED))
  76. return evp_keymgmt_importdomparam_types(ctx->keymgmt);
  77. return NULL;
  78. }
  79. const OSSL_PARAM *EVP_PKEY_key_fromdata_settable(EVP_PKEY_CTX *ctx)
  80. {
  81. /* We call fromdata_init to get ctx->keymgmt populated */
  82. if (fromdata_init(ctx, EVP_PKEY_OP_UNDEFINED))
  83. return evp_keymgmt_importdomparam_types(ctx->keymgmt);
  84. return NULL;
  85. }
  86. int EVP_PKEY_paramgen_init(EVP_PKEY_CTX *ctx)
  87. {
  88. int ret;
  89. if (!ctx || !ctx->pmeth || !ctx->pmeth->paramgen) {
  90. EVPerr(EVP_F_EVP_PKEY_PARAMGEN_INIT,
  91. EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  92. return -2;
  93. }
  94. ctx->operation = EVP_PKEY_OP_PARAMGEN;
  95. if (!ctx->pmeth->paramgen_init)
  96. return 1;
  97. ret = ctx->pmeth->paramgen_init(ctx);
  98. if (ret <= 0)
  99. ctx->operation = EVP_PKEY_OP_UNDEFINED;
  100. return ret;
  101. }
  102. int EVP_PKEY_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey)
  103. {
  104. int ret;
  105. if (!ctx || !ctx->pmeth || !ctx->pmeth->paramgen) {
  106. EVPerr(EVP_F_EVP_PKEY_PARAMGEN,
  107. EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  108. return -2;
  109. }
  110. if (ctx->operation != EVP_PKEY_OP_PARAMGEN) {
  111. EVPerr(EVP_F_EVP_PKEY_PARAMGEN, EVP_R_OPERATON_NOT_INITIALIZED);
  112. return -1;
  113. }
  114. if (ppkey == NULL)
  115. return -1;
  116. if (*ppkey == NULL)
  117. *ppkey = EVP_PKEY_new();
  118. if (*ppkey == NULL) {
  119. EVPerr(EVP_F_EVP_PKEY_PARAMGEN, ERR_R_MALLOC_FAILURE);
  120. return -1;
  121. }
  122. ret = ctx->pmeth->paramgen(ctx, *ppkey);
  123. if (ret <= 0) {
  124. EVP_PKEY_free(*ppkey);
  125. *ppkey = NULL;
  126. }
  127. return ret;
  128. }
  129. int EVP_PKEY_keygen_init(EVP_PKEY_CTX *ctx)
  130. {
  131. int ret;
  132. if (!ctx || !ctx->pmeth || !ctx->pmeth->keygen) {
  133. EVPerr(EVP_F_EVP_PKEY_KEYGEN_INIT,
  134. EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  135. return -2;
  136. }
  137. ctx->operation = EVP_PKEY_OP_KEYGEN;
  138. if (!ctx->pmeth->keygen_init)
  139. return 1;
  140. ret = ctx->pmeth->keygen_init(ctx);
  141. if (ret <= 0)
  142. ctx->operation = EVP_PKEY_OP_UNDEFINED;
  143. return ret;
  144. }
  145. int EVP_PKEY_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey)
  146. {
  147. int ret;
  148. if (!ctx || !ctx->pmeth || !ctx->pmeth->keygen) {
  149. EVPerr(EVP_F_EVP_PKEY_KEYGEN,
  150. EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  151. return -2;
  152. }
  153. if (ctx->operation != EVP_PKEY_OP_KEYGEN) {
  154. EVPerr(EVP_F_EVP_PKEY_KEYGEN, EVP_R_OPERATON_NOT_INITIALIZED);
  155. return -1;
  156. }
  157. if (ppkey == NULL)
  158. return -1;
  159. if (*ppkey == NULL)
  160. *ppkey = EVP_PKEY_new();
  161. if (*ppkey == NULL)
  162. return -1;
  163. ret = ctx->pmeth->keygen(ctx, *ppkey);
  164. if (ret <= 0) {
  165. EVP_PKEY_free(*ppkey);
  166. *ppkey = NULL;
  167. }
  168. return ret;
  169. }
  170. void EVP_PKEY_CTX_set_cb(EVP_PKEY_CTX *ctx, EVP_PKEY_gen_cb *cb)
  171. {
  172. ctx->pkey_gencb = cb;
  173. }
  174. EVP_PKEY_gen_cb *EVP_PKEY_CTX_get_cb(EVP_PKEY_CTX *ctx)
  175. {
  176. return ctx->pkey_gencb;
  177. }
  178. /*
  179. * "translation callback" to call EVP_PKEY_CTX callbacks using BN_GENCB style
  180. * callbacks.
  181. */
  182. static int trans_cb(int a, int b, BN_GENCB *gcb)
  183. {
  184. EVP_PKEY_CTX *ctx = BN_GENCB_get_arg(gcb);
  185. ctx->keygen_info[0] = a;
  186. ctx->keygen_info[1] = b;
  187. return ctx->pkey_gencb(ctx);
  188. }
  189. void evp_pkey_set_cb_translate(BN_GENCB *cb, EVP_PKEY_CTX *ctx)
  190. {
  191. BN_GENCB_set(cb, trans_cb, ctx);
  192. }
  193. int EVP_PKEY_CTX_get_keygen_info(EVP_PKEY_CTX *ctx, int idx)
  194. {
  195. if (idx == -1)
  196. return ctx->keygen_info_count;
  197. if (idx < 0 || idx > ctx->keygen_info_count)
  198. return 0;
  199. return ctx->keygen_info[idx];
  200. }
  201. EVP_PKEY *EVP_PKEY_new_mac_key(int type, ENGINE *e,
  202. const unsigned char *key, int keylen)
  203. {
  204. EVP_PKEY_CTX *mac_ctx = NULL;
  205. EVP_PKEY *mac_key = NULL;
  206. mac_ctx = EVP_PKEY_CTX_new_id(type, e);
  207. if (!mac_ctx)
  208. return NULL;
  209. if (EVP_PKEY_keygen_init(mac_ctx) <= 0)
  210. goto merr;
  211. if (EVP_PKEY_CTX_set_mac_key(mac_ctx, key, keylen) <= 0)
  212. goto merr;
  213. if (EVP_PKEY_keygen(mac_ctx, &mac_key) <= 0)
  214. goto merr;
  215. merr:
  216. EVP_PKEY_CTX_free(mac_ctx);
  217. return mac_key;
  218. }
  219. int EVP_PKEY_check(EVP_PKEY_CTX *ctx)
  220. {
  221. EVP_PKEY *pkey = ctx->pkey;
  222. if (pkey == NULL) {
  223. EVPerr(EVP_F_EVP_PKEY_CHECK, EVP_R_NO_KEY_SET);
  224. return 0;
  225. }
  226. /* call customized check function first */
  227. if (ctx->pmeth->check != NULL)
  228. return ctx->pmeth->check(pkey);
  229. /* use default check function in ameth */
  230. if (pkey->ameth == NULL || pkey->ameth->pkey_check == NULL) {
  231. EVPerr(EVP_F_EVP_PKEY_CHECK,
  232. EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  233. return -2;
  234. }
  235. return pkey->ameth->pkey_check(pkey);
  236. }
  237. int EVP_PKEY_public_check(EVP_PKEY_CTX *ctx)
  238. {
  239. EVP_PKEY *pkey = ctx->pkey;
  240. if (pkey == NULL) {
  241. EVPerr(EVP_F_EVP_PKEY_PUBLIC_CHECK, EVP_R_NO_KEY_SET);
  242. return 0;
  243. }
  244. /* call customized public key check function first */
  245. if (ctx->pmeth->public_check != NULL)
  246. return ctx->pmeth->public_check(pkey);
  247. /* use default public key check function in ameth */
  248. if (pkey->ameth == NULL || pkey->ameth->pkey_public_check == NULL) {
  249. EVPerr(EVP_F_EVP_PKEY_PUBLIC_CHECK,
  250. EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  251. return -2;
  252. }
  253. return pkey->ameth->pkey_public_check(pkey);
  254. }
  255. int EVP_PKEY_param_check(EVP_PKEY_CTX *ctx)
  256. {
  257. EVP_PKEY *pkey = ctx->pkey;
  258. if (pkey == NULL) {
  259. EVPerr(EVP_F_EVP_PKEY_PARAM_CHECK, EVP_R_NO_KEY_SET);
  260. return 0;
  261. }
  262. /* call customized param check function first */
  263. if (ctx->pmeth->param_check != NULL)
  264. return ctx->pmeth->param_check(pkey);
  265. /* use default param check function in ameth */
  266. if (pkey->ameth == NULL || pkey->ameth->pkey_param_check == NULL) {
  267. EVPerr(EVP_F_EVP_PKEY_PARAM_CHECK,
  268. EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  269. return -2;
  270. }
  271. return pkey->ameth->pkey_param_check(pkey);
  272. }