2
0

rsa_meth.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*
  2. * Copyright 2016-2020 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. /*
  10. * RSA low level APIs are deprecated for public use, but still ok for
  11. * internal use.
  12. */
  13. #include "internal/deprecated.h"
  14. #include <string.h>
  15. #include "rsa_local.h"
  16. #include <openssl/err.h>
  17. RSA_METHOD *RSA_meth_new(const char *name, int flags)
  18. {
  19. RSA_METHOD *meth = OPENSSL_zalloc(sizeof(*meth));
  20. if (meth != NULL) {
  21. meth->flags = flags;
  22. meth->name = OPENSSL_strdup(name);
  23. if (meth->name != NULL)
  24. return meth;
  25. OPENSSL_free(meth);
  26. }
  27. ERR_raise(ERR_LIB_RSA, ERR_R_MALLOC_FAILURE);
  28. return NULL;
  29. }
  30. void RSA_meth_free(RSA_METHOD *meth)
  31. {
  32. if (meth != NULL) {
  33. OPENSSL_free(meth->name);
  34. OPENSSL_free(meth);
  35. }
  36. }
  37. RSA_METHOD *RSA_meth_dup(const RSA_METHOD *meth)
  38. {
  39. RSA_METHOD *ret = OPENSSL_malloc(sizeof(*ret));
  40. if (ret != NULL) {
  41. memcpy(ret, meth, sizeof(*meth));
  42. ret->name = OPENSSL_strdup(meth->name);
  43. if (ret->name != NULL)
  44. return ret;
  45. OPENSSL_free(ret);
  46. }
  47. ERR_raise(ERR_LIB_RSA, ERR_R_MALLOC_FAILURE);
  48. return NULL;
  49. }
  50. const char *RSA_meth_get0_name(const RSA_METHOD *meth)
  51. {
  52. return meth->name;
  53. }
  54. int RSA_meth_set1_name(RSA_METHOD *meth, const char *name)
  55. {
  56. char *tmpname = OPENSSL_strdup(name);
  57. if (tmpname == NULL) {
  58. ERR_raise(ERR_LIB_RSA, ERR_R_MALLOC_FAILURE);
  59. return 0;
  60. }
  61. OPENSSL_free(meth->name);
  62. meth->name = tmpname;
  63. return 1;
  64. }
  65. int RSA_meth_get_flags(const RSA_METHOD *meth)
  66. {
  67. return meth->flags;
  68. }
  69. int RSA_meth_set_flags(RSA_METHOD *meth, int flags)
  70. {
  71. meth->flags = flags;
  72. return 1;
  73. }
  74. void *RSA_meth_get0_app_data(const RSA_METHOD *meth)
  75. {
  76. return meth->app_data;
  77. }
  78. int RSA_meth_set0_app_data(RSA_METHOD *meth, void *app_data)
  79. {
  80. meth->app_data = app_data;
  81. return 1;
  82. }
  83. int (*RSA_meth_get_pub_enc(const RSA_METHOD *meth))
  84. (int flen, const unsigned char *from,
  85. unsigned char *to, RSA *rsa, int padding)
  86. {
  87. return meth->rsa_pub_enc;
  88. }
  89. int RSA_meth_set_pub_enc(RSA_METHOD *meth,
  90. int (*pub_enc) (int flen, const unsigned char *from,
  91. unsigned char *to, RSA *rsa,
  92. int padding))
  93. {
  94. meth->rsa_pub_enc = pub_enc;
  95. return 1;
  96. }
  97. int (*RSA_meth_get_pub_dec(const RSA_METHOD *meth))
  98. (int flen, const unsigned char *from,
  99. unsigned char *to, RSA *rsa, int padding)
  100. {
  101. return meth->rsa_pub_dec;
  102. }
  103. int RSA_meth_set_pub_dec(RSA_METHOD *meth,
  104. int (*pub_dec) (int flen, const unsigned char *from,
  105. unsigned char *to, RSA *rsa,
  106. int padding))
  107. {
  108. meth->rsa_pub_dec = pub_dec;
  109. return 1;
  110. }
  111. int (*RSA_meth_get_priv_enc(const RSA_METHOD *meth))
  112. (int flen, const unsigned char *from,
  113. unsigned char *to, RSA *rsa, int padding)
  114. {
  115. return meth->rsa_priv_enc;
  116. }
  117. int RSA_meth_set_priv_enc(RSA_METHOD *meth,
  118. int (*priv_enc) (int flen, const unsigned char *from,
  119. unsigned char *to, RSA *rsa,
  120. int padding))
  121. {
  122. meth->rsa_priv_enc = priv_enc;
  123. return 1;
  124. }
  125. int (*RSA_meth_get_priv_dec(const RSA_METHOD *meth))
  126. (int flen, const unsigned char *from,
  127. unsigned char *to, RSA *rsa, int padding)
  128. {
  129. return meth->rsa_priv_dec;
  130. }
  131. int RSA_meth_set_priv_dec(RSA_METHOD *meth,
  132. int (*priv_dec) (int flen, const unsigned char *from,
  133. unsigned char *to, RSA *rsa,
  134. int padding))
  135. {
  136. meth->rsa_priv_dec = priv_dec;
  137. return 1;
  138. }
  139. /* Can be null */
  140. int (*RSA_meth_get_mod_exp(const RSA_METHOD *meth))
  141. (BIGNUM *r0, const BIGNUM *i, RSA *rsa, BN_CTX *ctx)
  142. {
  143. return meth->rsa_mod_exp;
  144. }
  145. int RSA_meth_set_mod_exp(RSA_METHOD *meth,
  146. int (*mod_exp) (BIGNUM *r0, const BIGNUM *i, RSA *rsa,
  147. BN_CTX *ctx))
  148. {
  149. meth->rsa_mod_exp = mod_exp;
  150. return 1;
  151. }
  152. /* Can be null */
  153. int (*RSA_meth_get_bn_mod_exp(const RSA_METHOD *meth))
  154. (BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
  155. const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
  156. {
  157. return meth->bn_mod_exp;
  158. }
  159. int RSA_meth_set_bn_mod_exp(RSA_METHOD *meth,
  160. int (*bn_mod_exp) (BIGNUM *r,
  161. const BIGNUM *a,
  162. const BIGNUM *p,
  163. const BIGNUM *m,
  164. BN_CTX *ctx,
  165. BN_MONT_CTX *m_ctx))
  166. {
  167. meth->bn_mod_exp = bn_mod_exp;
  168. return 1;
  169. }
  170. /* called at new */
  171. int (*RSA_meth_get_init(const RSA_METHOD *meth)) (RSA *rsa)
  172. {
  173. return meth->init;
  174. }
  175. int RSA_meth_set_init(RSA_METHOD *meth, int (*init) (RSA *rsa))
  176. {
  177. meth->init = init;
  178. return 1;
  179. }
  180. /* called at free */
  181. int (*RSA_meth_get_finish(const RSA_METHOD *meth)) (RSA *rsa)
  182. {
  183. return meth->finish;
  184. }
  185. int RSA_meth_set_finish(RSA_METHOD *meth, int (*finish) (RSA *rsa))
  186. {
  187. meth->finish = finish;
  188. return 1;
  189. }
  190. int (*RSA_meth_get_sign(const RSA_METHOD *meth))
  191. (int type,
  192. const unsigned char *m, unsigned int m_length,
  193. unsigned char *sigret, unsigned int *siglen,
  194. const RSA *rsa)
  195. {
  196. return meth->rsa_sign;
  197. }
  198. int RSA_meth_set_sign(RSA_METHOD *meth,
  199. int (*sign) (int type, const unsigned char *m,
  200. unsigned int m_length,
  201. unsigned char *sigret, unsigned int *siglen,
  202. const RSA *rsa))
  203. {
  204. meth->rsa_sign = sign;
  205. return 1;
  206. }
  207. int (*RSA_meth_get_verify(const RSA_METHOD *meth))
  208. (int dtype, const unsigned char *m,
  209. unsigned int m_length, const unsigned char *sigbuf,
  210. unsigned int siglen, const RSA *rsa)
  211. {
  212. return meth->rsa_verify;
  213. }
  214. int RSA_meth_set_verify(RSA_METHOD *meth,
  215. int (*verify) (int dtype, const unsigned char *m,
  216. unsigned int m_length,
  217. const unsigned char *sigbuf,
  218. unsigned int siglen, const RSA *rsa))
  219. {
  220. meth->rsa_verify = verify;
  221. return 1;
  222. }
  223. int (*RSA_meth_get_keygen(const RSA_METHOD *meth))
  224. (RSA *rsa, int bits, BIGNUM *e, BN_GENCB *cb)
  225. {
  226. return meth->rsa_keygen;
  227. }
  228. int RSA_meth_set_keygen(RSA_METHOD *meth,
  229. int (*keygen) (RSA *rsa, int bits, BIGNUM *e,
  230. BN_GENCB *cb))
  231. {
  232. meth->rsa_keygen = keygen;
  233. return 1;
  234. }
  235. int (*RSA_meth_get_multi_prime_keygen(const RSA_METHOD *meth))
  236. (RSA *rsa, int bits, int primes, BIGNUM *e, BN_GENCB *cb)
  237. {
  238. return meth->rsa_multi_prime_keygen;
  239. }
  240. int RSA_meth_set_multi_prime_keygen(RSA_METHOD *meth,
  241. int (*keygen) (RSA *rsa, int bits,
  242. int primes, BIGNUM *e,
  243. BN_GENCB *cb))
  244. {
  245. meth->rsa_multi_prime_keygen = keygen;
  246. return 1;
  247. }