A local copy of OpenSSL from GitHub
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

275 lines
6.9 KiB

  1. /*
  2. * Copyright 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 <string.h>
  10. #include "rsa_locl.h"
  11. #include <openssl/err.h>
  12. RSA_METHOD *RSA_meth_new(const char *name, int flags)
  13. {
  14. RSA_METHOD *meth = OPENSSL_zalloc(sizeof(RSA_METHOD));
  15. if (meth != NULL) {
  16. meth->name = OPENSSL_strdup(name);
  17. if (meth->name == NULL) {
  18. OPENSSL_free(meth);
  19. RSAerr(RSA_F_RSA_METH_NEW, ERR_R_MALLOC_FAILURE);
  20. return NULL;
  21. }
  22. meth->flags = flags;
  23. }
  24. return meth;
  25. }
  26. void RSA_meth_free(RSA_METHOD *meth)
  27. {
  28. if (meth != NULL) {
  29. if (meth->name != NULL)
  30. OPENSSL_free(meth->name);
  31. OPENSSL_free(meth);
  32. }
  33. }
  34. RSA_METHOD *RSA_meth_dup(const RSA_METHOD *meth)
  35. {
  36. RSA_METHOD *ret;
  37. ret = OPENSSL_malloc(sizeof(RSA_METHOD));
  38. if (ret != NULL) {
  39. memcpy(ret, meth, sizeof(*meth));
  40. ret->name = OPENSSL_strdup(meth->name);
  41. if (ret->name == NULL) {
  42. OPENSSL_free(ret);
  43. RSAerr(RSA_F_RSA_METH_DUP, ERR_R_MALLOC_FAILURE);
  44. return NULL;
  45. }
  46. }
  47. return ret;
  48. }
  49. const char *RSA_meth_get0_name(const RSA_METHOD *meth)
  50. {
  51. return meth->name;
  52. }
  53. int RSA_meth_set1_name(RSA_METHOD *meth, const char *name)
  54. {
  55. char *tmpname;
  56. tmpname = OPENSSL_strdup(name);
  57. if (tmpname == NULL) {
  58. RSAerr(RSA_F_RSA_METH_SET1_NAME, 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(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. }