rsa_ameth.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /* crypto/rsa/rsa_ameth.c */
  2. /* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
  3. * project 2006.
  4. */
  5. /* ====================================================================
  6. * Copyright (c) 2006 The OpenSSL Project. All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. All advertising materials mentioning features or use of this
  21. * software must display the following acknowledgment:
  22. * "This product includes software developed by the OpenSSL Project
  23. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  24. *
  25. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  26. * endorse or promote products derived from this software without
  27. * prior written permission. For written permission, please contact
  28. * licensing@OpenSSL.org.
  29. *
  30. * 5. Products derived from this software may not be called "OpenSSL"
  31. * nor may "OpenSSL" appear in their names without prior written
  32. * permission of the OpenSSL Project.
  33. *
  34. * 6. Redistributions of any form whatsoever must retain the following
  35. * acknowledgment:
  36. * "This product includes software developed by the OpenSSL Project
  37. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  38. *
  39. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  40. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  41. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  42. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  43. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  44. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  45. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  46. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  48. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  49. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  50. * OF THE POSSIBILITY OF SUCH DAMAGE.
  51. * ====================================================================
  52. *
  53. * This product includes cryptographic software written by Eric Young
  54. * (eay@cryptsoft.com). This product includes software written by Tim
  55. * Hudson (tjh@cryptsoft.com).
  56. *
  57. */
  58. #include <stdio.h>
  59. #include "cryptlib.h"
  60. #include <openssl/asn1t.h>
  61. #include <openssl/x509.h>
  62. #include <openssl/rsa.h>
  63. #include "asn1_locl.h"
  64. static int rsa_pub_encode(X509_PUBKEY *pk, const EVP_PKEY *pkey)
  65. {
  66. unsigned char *penc = NULL;
  67. int penclen;
  68. penclen = i2d_RSAPublicKey(pkey->pkey.rsa, &penc);
  69. if (penclen <= 0)
  70. return 0;
  71. if (X509_PUBKEY_set0_param(pk, OBJ_nid2obj(EVP_PKEY_RSA),
  72. V_ASN1_NULL, NULL, penc, penclen))
  73. return 1;
  74. OPENSSL_free(penc);
  75. return 0;
  76. }
  77. static int rsa_pub_decode(EVP_PKEY *pkey, X509_PUBKEY *pubkey)
  78. {
  79. const unsigned char *p;
  80. int pklen;
  81. RSA *rsa = NULL;
  82. if (!X509_PUBKEY_get0_param(NULL, &p, &pklen, NULL, pubkey))
  83. return 0;
  84. if (!(rsa = d2i_RSAPublicKey(NULL, &p, pklen)))
  85. {
  86. RSAerr(RSA_F_RSA_PUB_DECODE, ERR_R_RSA_LIB);
  87. return 0;
  88. }
  89. EVP_PKEY_assign_RSA (pkey, rsa);
  90. return 1;
  91. }
  92. static int rsa_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
  93. {
  94. if (BN_cmp(b->pkey.rsa->n,a->pkey.rsa->n) != 0
  95. || BN_cmp(b->pkey.rsa->e,a->pkey.rsa->e) != 0)
  96. return 0;
  97. return 1;
  98. }
  99. static int old_rsa_priv_decode(EVP_PKEY *pkey,
  100. const unsigned char **pder, int derlen)
  101. {
  102. RSA *rsa;
  103. if (!(rsa = d2i_RSAPrivateKey (NULL, pder, derlen)))
  104. {
  105. RSAerr(RSA_F_RSA_PRIV_DECODE, ERR_R_RSA_LIB);
  106. return 0;
  107. }
  108. EVP_PKEY_assign_RSA(pkey, rsa);
  109. return 1;
  110. }
  111. static int old_rsa_priv_encode(const EVP_PKEY *pkey, unsigned char **pder)
  112. {
  113. return i2d_RSAPrivateKey(pkey->pkey.rsa, pder);
  114. }
  115. static int rsa_priv_encode(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pkey)
  116. {
  117. unsigned char *rk = NULL;
  118. int rklen;
  119. rklen = i2d_RSAPrivateKey(pkey->pkey.rsa, &rk);
  120. if (rklen <= 0)
  121. {
  122. RSAerr(RSA_F_RSA_PRIV_ENCODE,ERR_R_MALLOC_FAILURE);
  123. return 0;
  124. }
  125. if (!PKCS8_pkey_set0(p8, OBJ_nid2obj(NID_rsaEncryption), 0,
  126. V_ASN1_NULL, NULL, rk, rklen))
  127. {
  128. RSAerr(RSA_F_RSA_PRIV_ENCODE,ERR_R_MALLOC_FAILURE);
  129. return 0;
  130. }
  131. return 1;
  132. }
  133. static int rsa_priv_decode(EVP_PKEY *pkey, PKCS8_PRIV_KEY_INFO *p8)
  134. {
  135. const unsigned char *p;
  136. int pklen;
  137. if (!PKCS8_pkey_get0(NULL, &p, &pklen, NULL, p8))
  138. return 0;
  139. return old_rsa_priv_decode(pkey, &p, pklen);
  140. }
  141. static int int_rsa_size(const EVP_PKEY *pkey)
  142. {
  143. return RSA_size(pkey->pkey.rsa);
  144. }
  145. static int rsa_bits(const EVP_PKEY *pkey)
  146. {
  147. return BN_num_bits(pkey->pkey.rsa->n);
  148. }
  149. static void int_rsa_free(EVP_PKEY *pkey)
  150. {
  151. RSA_free(pkey->pkey.rsa);
  152. }
  153. static void update_buflen(const BIGNUM *b, size_t *pbuflen)
  154. {
  155. size_t i;
  156. if (!b)
  157. return;
  158. if (*pbuflen < (i = (size_t)BN_num_bytes(b)))
  159. *pbuflen = i;
  160. }
  161. static int do_rsa_print(BIO *bp, const RSA *x, int off, int priv)
  162. {
  163. char *str;
  164. const char *s;
  165. unsigned char *m=NULL;
  166. int ret=0, mod_len = 0;
  167. size_t buf_len=0;
  168. update_buflen(x->n, &buf_len);
  169. update_buflen(x->e, &buf_len);
  170. if (priv)
  171. {
  172. update_buflen(x->d, &buf_len);
  173. update_buflen(x->p, &buf_len);
  174. update_buflen(x->q, &buf_len);
  175. update_buflen(x->dmp1, &buf_len);
  176. update_buflen(x->dmq1, &buf_len);
  177. update_buflen(x->iqmp, &buf_len);
  178. }
  179. m=(unsigned char *)OPENSSL_malloc(buf_len+10);
  180. if (m == NULL)
  181. {
  182. RSAerr(RSA_F_RSA_PRINT,ERR_R_MALLOC_FAILURE);
  183. goto err;
  184. }
  185. if (x->n != NULL)
  186. mod_len = BN_num_bits(x->n);
  187. if(!BIO_indent(bp,off,128))
  188. goto err;
  189. if (priv && x->d)
  190. {
  191. if (BIO_printf(bp,"Private-Key: (%d bit)\n", mod_len)
  192. <= 0) goto err;
  193. str = "modulus:";
  194. s = "publicExponent:";
  195. }
  196. else
  197. {
  198. if (BIO_printf(bp,"Public-Key: (%d bit)\n", mod_len)
  199. <= 0) goto err;
  200. str = "Modulus:";
  201. s= "Exponent:";
  202. }
  203. if (!ASN1_bn_print(bp,str,x->n,m,off)) goto err;
  204. if (!ASN1_bn_print(bp,s,x->e,m,off))
  205. goto err;
  206. if (priv)
  207. {
  208. if (!ASN1_bn_print(bp,"privateExponent:",x->d,m,off))
  209. goto err;
  210. if (!ASN1_bn_print(bp,"prime1:",x->p,m,off))
  211. goto err;
  212. if (!ASN1_bn_print(bp,"prime2:",x->q,m,off))
  213. goto err;
  214. if (!ASN1_bn_print(bp,"exponent1:",x->dmp1,m,off))
  215. goto err;
  216. if (!ASN1_bn_print(bp,"exponent2:",x->dmq1,m,off))
  217. goto err;
  218. if (!ASN1_bn_print(bp,"coefficient:",x->iqmp,m,off))
  219. goto err;
  220. }
  221. ret=1;
  222. err:
  223. if (m != NULL) OPENSSL_free(m);
  224. return(ret);
  225. }
  226. static int rsa_pub_print(BIO *bp, const EVP_PKEY *pkey, int indent,
  227. ASN1_PCTX *ctx)
  228. {
  229. return do_rsa_print(bp, pkey->pkey.rsa, indent, 0);
  230. }
  231. static int rsa_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent,
  232. ASN1_PCTX *ctx)
  233. {
  234. return do_rsa_print(bp, pkey->pkey.rsa, indent, 1);
  235. }
  236. static int rsa_pkey_ctrl(EVP_PKEY *pkey, int op, long arg1, void *arg2)
  237. {
  238. switch (op)
  239. {
  240. case ASN1_PKEY_CTRL_PKCS7_SIGN:
  241. if (arg1 == 0)
  242. {
  243. X509_ALGOR *alg;
  244. PKCS7_SIGNER_INFO_get0_algs(arg2, NULL, NULL, &alg);
  245. X509_ALGOR_set0(alg, OBJ_nid2obj(NID_rsaEncryption),
  246. V_ASN1_NULL, 0);
  247. }
  248. return 1;
  249. case ASN1_PKEY_CTRL_PKCS7_ENCRYPT:
  250. if (arg1 == 0)
  251. {
  252. X509_ALGOR *alg;
  253. PKCS7_RECIP_INFO_get0_alg(arg2, &alg);
  254. X509_ALGOR_set0(alg, OBJ_nid2obj(NID_rsaEncryption),
  255. V_ASN1_NULL, 0);
  256. }
  257. return 1;
  258. case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
  259. *(int *)arg2 = NID_sha1;
  260. return 1;
  261. default:
  262. return -2;
  263. }
  264. }
  265. const EVP_PKEY_ASN1_METHOD rsa_asn1_meths[] =
  266. {
  267. {
  268. EVP_PKEY_RSA,
  269. EVP_PKEY_RSA,
  270. ASN1_PKEY_SIGPARAM_NULL,
  271. "RSA",
  272. "OpenSSL RSA method",
  273. rsa_pub_decode,
  274. rsa_pub_encode,
  275. rsa_pub_cmp,
  276. rsa_pub_print,
  277. rsa_priv_decode,
  278. rsa_priv_encode,
  279. rsa_priv_print,
  280. int_rsa_size,
  281. rsa_bits,
  282. 0,0,0,0,0,0,
  283. int_rsa_free,
  284. rsa_pkey_ctrl,
  285. old_rsa_priv_decode,
  286. old_rsa_priv_encode
  287. },
  288. {
  289. EVP_PKEY_RSA2,
  290. EVP_PKEY_RSA,
  291. ASN1_PKEY_ALIAS
  292. }
  293. };