pmeth_gn.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 "internal/bn_int.h"
  15. #include "internal/asn1_int.h"
  16. #include "internal/evp_int.h"
  17. int EVP_PKEY_paramgen_init(EVP_PKEY_CTX *ctx)
  18. {
  19. int ret;
  20. if (!ctx || !ctx->pmeth || !ctx->pmeth->paramgen) {
  21. EVPerr(EVP_F_EVP_PKEY_PARAMGEN_INIT,
  22. EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  23. return -2;
  24. }
  25. ctx->operation = EVP_PKEY_OP_PARAMGEN;
  26. if (!ctx->pmeth->paramgen_init)
  27. return 1;
  28. ret = ctx->pmeth->paramgen_init(ctx);
  29. if (ret <= 0)
  30. ctx->operation = EVP_PKEY_OP_UNDEFINED;
  31. return ret;
  32. }
  33. int EVP_PKEY_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey)
  34. {
  35. int ret;
  36. if (!ctx || !ctx->pmeth || !ctx->pmeth->paramgen) {
  37. EVPerr(EVP_F_EVP_PKEY_PARAMGEN,
  38. EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  39. return -2;
  40. }
  41. if (ctx->operation != EVP_PKEY_OP_PARAMGEN) {
  42. EVPerr(EVP_F_EVP_PKEY_PARAMGEN, EVP_R_OPERATON_NOT_INITIALIZED);
  43. return -1;
  44. }
  45. if (ppkey == NULL)
  46. return -1;
  47. if (*ppkey == NULL)
  48. *ppkey = EVP_PKEY_new();
  49. if (*ppkey == NULL) {
  50. EVPerr(EVP_F_EVP_PKEY_PARAMGEN, ERR_R_MALLOC_FAILURE);
  51. return -1;
  52. }
  53. ret = ctx->pmeth->paramgen(ctx, *ppkey);
  54. if (ret <= 0) {
  55. EVP_PKEY_free(*ppkey);
  56. *ppkey = NULL;
  57. }
  58. return ret;
  59. }
  60. int EVP_PKEY_keygen_init(EVP_PKEY_CTX *ctx)
  61. {
  62. int ret;
  63. if (!ctx || !ctx->pmeth || !ctx->pmeth->keygen) {
  64. EVPerr(EVP_F_EVP_PKEY_KEYGEN_INIT,
  65. EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  66. return -2;
  67. }
  68. ctx->operation = EVP_PKEY_OP_KEYGEN;
  69. if (!ctx->pmeth->keygen_init)
  70. return 1;
  71. ret = ctx->pmeth->keygen_init(ctx);
  72. if (ret <= 0)
  73. ctx->operation = EVP_PKEY_OP_UNDEFINED;
  74. return ret;
  75. }
  76. int EVP_PKEY_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey)
  77. {
  78. int ret;
  79. if (!ctx || !ctx->pmeth || !ctx->pmeth->keygen) {
  80. EVPerr(EVP_F_EVP_PKEY_KEYGEN,
  81. EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  82. return -2;
  83. }
  84. if (ctx->operation != EVP_PKEY_OP_KEYGEN) {
  85. EVPerr(EVP_F_EVP_PKEY_KEYGEN, EVP_R_OPERATON_NOT_INITIALIZED);
  86. return -1;
  87. }
  88. if (ppkey == NULL)
  89. return -1;
  90. if (*ppkey == NULL)
  91. *ppkey = EVP_PKEY_new();
  92. if (*ppkey == NULL)
  93. return -1;
  94. ret = ctx->pmeth->keygen(ctx, *ppkey);
  95. if (ret <= 0) {
  96. EVP_PKEY_free(*ppkey);
  97. *ppkey = NULL;
  98. }
  99. return ret;
  100. }
  101. void EVP_PKEY_CTX_set_cb(EVP_PKEY_CTX *ctx, EVP_PKEY_gen_cb *cb)
  102. {
  103. ctx->pkey_gencb = cb;
  104. }
  105. EVP_PKEY_gen_cb *EVP_PKEY_CTX_get_cb(EVP_PKEY_CTX *ctx)
  106. {
  107. return ctx->pkey_gencb;
  108. }
  109. /*
  110. * "translation callback" to call EVP_PKEY_CTX callbacks using BN_GENCB style
  111. * callbacks.
  112. */
  113. static int trans_cb(int a, int b, BN_GENCB *gcb)
  114. {
  115. EVP_PKEY_CTX *ctx = BN_GENCB_get_arg(gcb);
  116. ctx->keygen_info[0] = a;
  117. ctx->keygen_info[1] = b;
  118. return ctx->pkey_gencb(ctx);
  119. }
  120. void evp_pkey_set_cb_translate(BN_GENCB *cb, EVP_PKEY_CTX *ctx)
  121. {
  122. BN_GENCB_set(cb, trans_cb, ctx);
  123. }
  124. int EVP_PKEY_CTX_get_keygen_info(EVP_PKEY_CTX *ctx, int idx)
  125. {
  126. if (idx == -1)
  127. return ctx->keygen_info_count;
  128. if (idx < 0 || idx > ctx->keygen_info_count)
  129. return 0;
  130. return ctx->keygen_info[idx];
  131. }
  132. EVP_PKEY *EVP_PKEY_new_mac_key(int type, ENGINE *e,
  133. const unsigned char *key, int keylen)
  134. {
  135. EVP_PKEY_CTX *mac_ctx = NULL;
  136. EVP_PKEY *mac_key = NULL;
  137. mac_ctx = EVP_PKEY_CTX_new_id(type, e);
  138. if (!mac_ctx)
  139. return NULL;
  140. if (EVP_PKEY_keygen_init(mac_ctx) <= 0)
  141. goto merr;
  142. if (EVP_PKEY_CTX_set_mac_key(mac_ctx, key, keylen) <= 0)
  143. goto merr;
  144. if (EVP_PKEY_keygen(mac_ctx, &mac_key) <= 0)
  145. goto merr;
  146. merr:
  147. EVP_PKEY_CTX_free(mac_ctx);
  148. return mac_key;
  149. }
  150. int EVP_PKEY_check(EVP_PKEY_CTX *ctx)
  151. {
  152. EVP_PKEY *pkey = ctx->pkey;
  153. if (pkey == NULL) {
  154. EVPerr(EVP_F_EVP_PKEY_CHECK, EVP_R_NO_KEY_SET);
  155. return 0;
  156. }
  157. /* call customized check function first */
  158. if (ctx->pmeth->check != NULL)
  159. return ctx->pmeth->check(pkey);
  160. /* use default check function in ameth */
  161. if (pkey->ameth == NULL || pkey->ameth->pkey_check == NULL) {
  162. EVPerr(EVP_F_EVP_PKEY_CHECK,
  163. EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  164. return -2;
  165. }
  166. return pkey->ameth->pkey_check(pkey);
  167. }
  168. int EVP_PKEY_public_check(EVP_PKEY_CTX *ctx)
  169. {
  170. EVP_PKEY *pkey = ctx->pkey;
  171. if (pkey == NULL) {
  172. EVPerr(EVP_F_EVP_PKEY_PUBLIC_CHECK, EVP_R_NO_KEY_SET);
  173. return 0;
  174. }
  175. /* call customized public key check function first */
  176. if (ctx->pmeth->public_check != NULL)
  177. return ctx->pmeth->public_check(pkey);
  178. /* use default public key check function in ameth */
  179. if (pkey->ameth == NULL || pkey->ameth->pkey_public_check == NULL) {
  180. EVPerr(EVP_F_EVP_PKEY_PUBLIC_CHECK,
  181. EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  182. return -2;
  183. }
  184. return pkey->ameth->pkey_public_check(pkey);
  185. }
  186. int EVP_PKEY_param_check(EVP_PKEY_CTX *ctx)
  187. {
  188. EVP_PKEY *pkey = ctx->pkey;
  189. if (pkey == NULL) {
  190. EVPerr(EVP_F_EVP_PKEY_PARAM_CHECK, EVP_R_NO_KEY_SET);
  191. return 0;
  192. }
  193. /* call customized param check function first */
  194. if (ctx->pmeth->param_check != NULL)
  195. return ctx->pmeth->param_check(pkey);
  196. /* use default param check function in ameth */
  197. if (pkey->ameth == NULL || pkey->ameth->pkey_param_check == NULL) {
  198. EVPerr(EVP_F_EVP_PKEY_PARAM_CHECK,
  199. EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  200. return -2;
  201. }
  202. return pkey->ameth->pkey_param_check(pkey);
  203. }