2
0

rsa_pmeth.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. /* crypto/rsa/rsa_pmeth.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 <openssl/evp.h>
  64. #include "evp_locl.h"
  65. extern int int_rsa_verify(int dtype, const unsigned char *m, unsigned int m_len,
  66. unsigned char *rm, unsigned int *prm_len,
  67. const unsigned char *sigbuf, unsigned int siglen,
  68. RSA *rsa);
  69. /* RSA pkey context structure */
  70. typedef struct
  71. {
  72. /* Key gen parameters */
  73. int nbits;
  74. BIGNUM *pub_exp;
  75. /* Keygen callback info */
  76. int gentmp[2];
  77. /* RSA padding mode */
  78. int pad_mode;
  79. /* message digest */
  80. const EVP_MD *md;
  81. /* PSS/OAEP salt length */
  82. int saltlen;
  83. /* Temp buffer */
  84. unsigned char *tbuf;
  85. } RSA_PKEY_CTX;
  86. static int pkey_rsa_init(EVP_PKEY_CTX *ctx)
  87. {
  88. RSA_PKEY_CTX *rctx;
  89. rctx = OPENSSL_malloc(sizeof(RSA_PKEY_CTX));
  90. if (!rctx)
  91. return 0;
  92. rctx->nbits = 1024;
  93. rctx->pub_exp = NULL;
  94. rctx->pad_mode = RSA_PKCS1_PADDING;
  95. rctx->md = NULL;
  96. rctx->tbuf = NULL;
  97. rctx->saltlen = -2;
  98. ctx->data = rctx;
  99. ctx->keygen_info = rctx->gentmp;
  100. ctx->keygen_info_count = 2;
  101. return 1;
  102. }
  103. static int setup_tbuf(RSA_PKEY_CTX *ctx, EVP_PKEY_CTX *pk)
  104. {
  105. if (ctx->tbuf)
  106. return 1;
  107. ctx->tbuf = OPENSSL_malloc(EVP_PKEY_size(pk->pkey));
  108. if (!ctx->tbuf)
  109. return 0;
  110. return 1;
  111. }
  112. static void pkey_rsa_cleanup(EVP_PKEY_CTX *ctx)
  113. {
  114. RSA_PKEY_CTX *rctx = ctx->data;
  115. if (rctx)
  116. {
  117. if (rctx->pub_exp)
  118. BN_free(rctx->pub_exp);
  119. if (rctx->tbuf)
  120. OPENSSL_free(rctx->tbuf);
  121. OPENSSL_free(rctx);
  122. }
  123. }
  124. static int pkey_rsa_sign(EVP_PKEY_CTX *ctx, unsigned char *sig, int *siglen,
  125. const unsigned char *tbs, int tbslen)
  126. {
  127. int ret;
  128. RSA_PKEY_CTX *rctx = ctx->data;
  129. RSA *rsa = ctx->pkey->pkey.rsa;
  130. if (rctx->md)
  131. {
  132. if (tbslen != EVP_MD_size(rctx->md))
  133. {
  134. RSAerr(RSA_F_PKEY_RSA_SIGN,
  135. RSA_R_INVALID_DIGEST_LENGTH);
  136. return -1;
  137. }
  138. if (rctx->pad_mode == RSA_X931_PADDING)
  139. {
  140. if (!setup_tbuf(rctx, ctx))
  141. return -1;
  142. memcpy(rctx->tbuf, tbs, tbslen);
  143. rctx->tbuf[tbslen] =
  144. RSA_X931_hash_id(EVP_MD_type(rctx->md));
  145. ret = RSA_private_encrypt(tbslen + 1, rctx->tbuf,
  146. sig, rsa, RSA_X931_PADDING);
  147. }
  148. else if (rctx->pad_mode == RSA_PKCS1_PADDING)
  149. {
  150. unsigned int sltmp;
  151. ret = RSA_sign(EVP_MD_type(rctx->md),
  152. tbs, tbslen, sig, &sltmp, rsa);
  153. if (ret <= 0)
  154. return ret;
  155. ret = sltmp;
  156. }
  157. else if (rctx->pad_mode == RSA_PKCS1_PSS_PADDING)
  158. {
  159. if (!setup_tbuf(rctx, ctx))
  160. return -1;
  161. if (!RSA_padding_add_PKCS1_PSS(rsa, rctx->tbuf, tbs,
  162. rctx->md, rctx->saltlen))
  163. return -1;
  164. ret = RSA_private_encrypt(RSA_size(rsa), rctx->tbuf,
  165. sig, rsa, RSA_NO_PADDING);
  166. }
  167. else
  168. return -1;
  169. }
  170. else
  171. ret = RSA_private_encrypt(tbslen, tbs, sig, ctx->pkey->pkey.rsa,
  172. rctx->pad_mode);
  173. if (ret < 0)
  174. return ret;
  175. *siglen = ret;
  176. return 1;
  177. }
  178. static int pkey_rsa_verifyrecover(EVP_PKEY_CTX *ctx,
  179. unsigned char *rout, int *routlen,
  180. const unsigned char *sig, int siglen)
  181. {
  182. int ret;
  183. RSA_PKEY_CTX *rctx = ctx->data;
  184. if (rctx->md)
  185. {
  186. if (rctx->pad_mode == RSA_X931_PADDING)
  187. {
  188. if (!setup_tbuf(rctx, ctx))
  189. return -1;
  190. ret = RSA_public_decrypt(siglen, sig,
  191. rctx->tbuf, ctx->pkey->pkey.rsa,
  192. RSA_X931_PADDING);
  193. if (ret < 1)
  194. return 0;
  195. ret--;
  196. if (rctx->tbuf[ret] !=
  197. RSA_X931_hash_id(EVP_MD_type(rctx->md)))
  198. {
  199. RSAerr(RSA_F_PKEY_RSA_VERIFYRECOVER,
  200. RSA_R_ALGORITHM_MISMATCH);
  201. return 0;
  202. }
  203. if (ret != EVP_MD_size(rctx->md))
  204. {
  205. RSAerr(RSA_F_PKEY_RSA_VERIFYRECOVER,
  206. RSA_R_INVALID_DIGEST_LENGTH);
  207. return 0;
  208. }
  209. if (rout)
  210. memcpy(rout, rctx->tbuf, ret);
  211. }
  212. else if (rctx->pad_mode == RSA_PKCS1_PADDING)
  213. {
  214. unsigned int sltmp;
  215. ret = int_rsa_verify(EVP_MD_type(rctx->md),
  216. NULL, 0, rout, &sltmp,
  217. sig, siglen, ctx->pkey->pkey.rsa);
  218. ret = sltmp;
  219. }
  220. else
  221. return -1;
  222. }
  223. else
  224. ret = RSA_public_decrypt(siglen, sig, rout, ctx->pkey->pkey.rsa,
  225. rctx->pad_mode);
  226. if (ret < 0)
  227. return ret;
  228. *routlen = ret;
  229. return 1;
  230. }
  231. static int pkey_rsa_verify(EVP_PKEY_CTX *ctx,
  232. const unsigned char *sig, int siglen,
  233. const unsigned char *tbs, int tbslen)
  234. {
  235. RSA_PKEY_CTX *rctx = ctx->data;
  236. RSA *rsa = ctx->pkey->pkey.rsa;
  237. int rslen;
  238. if (rctx->md)
  239. {
  240. if (rctx->pad_mode == RSA_PKCS1_PADDING)
  241. return RSA_verify(EVP_MD_type(rctx->md), tbs, tbslen,
  242. sig, siglen, rsa);
  243. if (rctx->pad_mode == RSA_X931_PADDING)
  244. {
  245. if (pkey_rsa_verifyrecover(ctx, NULL, &rslen,
  246. sig, siglen) <= 0)
  247. return 0;
  248. }
  249. else if (rctx->pad_mode == RSA_PKCS1_PSS_PADDING)
  250. {
  251. int ret;
  252. if (!setup_tbuf(rctx, ctx))
  253. return -1;
  254. ret = RSA_public_decrypt(siglen, sig, rctx->tbuf,
  255. rsa, RSA_NO_PADDING);
  256. if (ret <= 0)
  257. return 0;
  258. ret = RSA_verify_PKCS1_PSS(rsa, tbs, rctx->md,
  259. rctx->tbuf, rctx->saltlen);
  260. if (ret <= 0)
  261. return 0;
  262. return 1;
  263. }
  264. else
  265. return -1;
  266. }
  267. else
  268. {
  269. if (!setup_tbuf(rctx, ctx))
  270. return -1;
  271. rslen = RSA_public_decrypt(siglen, sig, rctx->tbuf,
  272. rsa, rctx->pad_mode);
  273. if (rslen <= 0)
  274. return 0;
  275. }
  276. if ((rslen != tbslen) || memcmp(tbs, rctx->tbuf, rslen))
  277. return 0;
  278. return 1;
  279. }
  280. static int pkey_rsa_encrypt(EVP_PKEY_CTX *ctx, unsigned char *out, int *outlen,
  281. const unsigned char *in, int inlen)
  282. {
  283. int ret;
  284. RSA_PKEY_CTX *rctx = ctx->data;
  285. ret = RSA_public_encrypt(inlen, in, out, ctx->pkey->pkey.rsa,
  286. rctx->pad_mode);
  287. if (ret < 0)
  288. return ret;
  289. *outlen = ret;
  290. return 1;
  291. }
  292. static int pkey_rsa_decrypt(EVP_PKEY_CTX *ctx, unsigned char *out, int *outlen,
  293. const unsigned char *in, int inlen)
  294. {
  295. int ret;
  296. RSA_PKEY_CTX *rctx = ctx->data;
  297. ret = RSA_private_decrypt(inlen, in, out, ctx->pkey->pkey.rsa,
  298. rctx->pad_mode);
  299. if (ret < 0)
  300. return ret;
  301. *outlen = ret;
  302. return 1;
  303. }
  304. static int check_padding_md(const EVP_MD *md, int padding)
  305. {
  306. if (!md)
  307. return 1;
  308. if (padding == RSA_NO_PADDING)
  309. {
  310. RSAerr(RSA_F_CHECK_PADDING_MD, RSA_R_INVALID_PADDING_MODE);
  311. return 0;
  312. }
  313. if (padding == RSA_X931_PADDING)
  314. {
  315. if (RSA_X931_hash_id(EVP_MD_type(md)) == -1)
  316. {
  317. RSAerr(RSA_F_CHECK_PADDING_MD,
  318. RSA_R_INVALID_X931_DIGEST);
  319. return 0;
  320. }
  321. return 1;
  322. }
  323. return 1;
  324. }
  325. static int pkey_rsa_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
  326. {
  327. RSA_PKEY_CTX *rctx = ctx->data;
  328. switch (type)
  329. {
  330. case EVP_PKEY_CTRL_RSA_PADDING:
  331. if ((p1 >= RSA_PKCS1_PADDING) && (p1 <= RSA_PKCS1_PSS_PADDING))
  332. {
  333. if (!check_padding_md(rctx->md, p1))
  334. return 0;
  335. if (p1 == RSA_PKCS1_PSS_PADDING)
  336. {
  337. if (!(ctx->operation &
  338. (EVP_PKEY_OP_SIGN | EVP_PKEY_OP_VERIFY)))
  339. goto bad_pad;
  340. if (!rctx->md)
  341. rctx->md = EVP_sha1();
  342. }
  343. if (p1 == RSA_PKCS1_OAEP_PADDING)
  344. {
  345. if (!(ctx->operation & EVP_PKEY_OP_TYPE_CRYPT))
  346. goto bad_pad;
  347. if (!rctx->md)
  348. rctx->md = EVP_sha1();
  349. }
  350. rctx->pad_mode = p1;
  351. return 1;
  352. }
  353. bad_pad:
  354. RSAerr(RSA_F_PKEY_RSA_CTRL,
  355. RSA_R_ILLEGAL_OR_UNSUPPORTED_PADDING_MODE);
  356. return -2;
  357. case EVP_PKEY_CTRL_RSA_PSS_SALTLEN:
  358. if (p1 < -2)
  359. return -2;
  360. if (rctx->pad_mode != RSA_PKCS1_PSS_PADDING)
  361. {
  362. RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_PSS_SALTLEN);
  363. return -2;
  364. }
  365. rctx->saltlen = p1;
  366. return 1;
  367. case EVP_PKEY_CTRL_RSA_KEYGEN_BITS:
  368. if (p1 < 256)
  369. {
  370. RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_KEYBITS);
  371. return -2;
  372. }
  373. rctx->nbits = p1;
  374. return 1;
  375. case EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP:
  376. if (!p2)
  377. return -2;
  378. rctx->pub_exp = p2;
  379. return 1;
  380. case EVP_PKEY_CTRL_MD:
  381. if (!check_padding_md(p2, rctx->pad_mode))
  382. return 0;
  383. rctx->md = p2;
  384. return 1;
  385. case EVP_PKEY_CTRL_PKCS7_ENCRYPT:
  386. case EVP_PKEY_CTRL_PKCS7_DECRYPT:
  387. return 1;
  388. default:
  389. return -2;
  390. }
  391. }
  392. static int pkey_rsa_ctrl_str(EVP_PKEY_CTX *ctx,
  393. const char *type, const char *value)
  394. {
  395. if (!value)
  396. {
  397. RSAerr(RSA_F_PKEY_RSA_CTRL_STR, RSA_R_VALUE_MISSING);
  398. return 0;
  399. }
  400. if (!strcmp(type, "rsa_padding_mode"))
  401. {
  402. int pm;
  403. if (!strcmp(value, "pkcs1"))
  404. pm = RSA_PKCS1_PADDING;
  405. else if (!strcmp(value, "sslv23"))
  406. pm = RSA_SSLV23_PADDING;
  407. else if (!strcmp(value, "none"))
  408. pm = RSA_NO_PADDING;
  409. else if (!strcmp(value, "oeap"))
  410. pm = RSA_PKCS1_OAEP_PADDING;
  411. else if (!strcmp(value, "x931"))
  412. pm = RSA_X931_PADDING;
  413. else if (!strcmp(value, "pss"))
  414. pm = RSA_PKCS1_PSS_PADDING;
  415. else
  416. {
  417. RSAerr(RSA_F_PKEY_RSA_CTRL_STR,
  418. RSA_R_UNKNOWN_PADDING_TYPE);
  419. return -2;
  420. }
  421. return EVP_PKEY_CTX_set_rsa_padding(ctx, pm);
  422. }
  423. if (!strcmp(type, "rsa_pss_saltlen"))
  424. {
  425. int saltlen;
  426. saltlen = atoi(value);
  427. return EVP_PKEY_CTX_set_rsa_pss_saltlen(ctx, saltlen);
  428. }
  429. if (!strcmp(type, "rsa_keygen_bits"))
  430. {
  431. int nbits;
  432. nbits = atoi(value);
  433. return EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, nbits);
  434. }
  435. if (!strcmp(type, "rsa_keygen_pubexp"))
  436. {
  437. int ret;
  438. BIGNUM *pubexp = NULL;
  439. if (!BN_asc2bn(&pubexp, value))
  440. return 0;
  441. ret = EVP_PKEY_CTX_set_rsa_keygen_pubexp(ctx, pubexp);
  442. if (ret <= 0)
  443. BN_free(pubexp);
  444. return ret;
  445. }
  446. return -2;
  447. }
  448. static int pkey_rsa_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
  449. {
  450. RSA *rsa = NULL;
  451. RSA_PKEY_CTX *rctx = ctx->data;
  452. BN_GENCB *pcb, cb;
  453. int ret;
  454. if (!rctx->pub_exp)
  455. {
  456. rctx->pub_exp = BN_new();
  457. if (!rctx->pub_exp || !BN_set_word(rctx->pub_exp, RSA_F4))
  458. return 0;
  459. }
  460. rsa = RSA_new();
  461. if (!rsa)
  462. return 0;
  463. if (ctx->pkey_gencb)
  464. {
  465. pcb = &cb;
  466. evp_pkey_set_cb_translate(pcb, ctx);
  467. }
  468. else
  469. pcb = NULL;
  470. ret = RSA_generate_key_ex(rsa, rctx->nbits, rctx->pub_exp, pcb);
  471. if (ret > 0)
  472. EVP_PKEY_assign_RSA(pkey, rsa);
  473. else
  474. RSA_free(rsa);
  475. return ret;
  476. }
  477. const EVP_PKEY_METHOD rsa_pkey_meth =
  478. {
  479. EVP_PKEY_RSA,
  480. EVP_PKEY_FLAG_AUTOARGLEN,
  481. pkey_rsa_init,
  482. pkey_rsa_cleanup,
  483. 0,0,
  484. 0,
  485. pkey_rsa_keygen,
  486. 0,
  487. pkey_rsa_sign,
  488. 0,
  489. pkey_rsa_verify,
  490. 0,
  491. pkey_rsa_verifyrecover,
  492. 0,0,0,0,
  493. 0,
  494. pkey_rsa_encrypt,
  495. 0,
  496. pkey_rsa_decrypt,
  497. 0,0,
  498. pkey_rsa_ctrl,
  499. pkey_rsa_ctrl_str
  500. };