Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
  2. * project 2000.
  3. */
  4. /* ====================================================================
  5. * Copyright (c) 2000-2005 The OpenSSL Project. All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * 3. All advertising materials mentioning features or use of this
  20. * software must display the following acknowledgment:
  21. * "This product includes software developed by the OpenSSL Project
  22. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  23. *
  24. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  25. * endorse or promote products derived from this software without
  26. * prior written permission. For written permission, please contact
  27. * licensing@OpenSSL.org.
  28. *
  29. * 5. Products derived from this software may not be called "OpenSSL"
  30. * nor may "OpenSSL" appear in their names without prior written
  31. * permission of the OpenSSL Project.
  32. *
  33. * 6. Redistributions of any form whatsoever must retain the following
  34. * acknowledgment:
  35. * "This product includes software developed by the OpenSSL Project
  36. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  37. *
  38. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  39. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  40. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  41. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  42. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  43. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  44. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  45. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  46. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  47. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  48. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  49. * OF THE POSSIBILITY OF SUCH DAMAGE.
  50. * ====================================================================
  51. *
  52. * This product includes cryptographic software written by Eric Young
  53. * (eay@cryptsoft.com). This product includes software written by Tim
  54. * Hudson (tjh@cryptsoft.com). */
  55. #include <openssl/rsa.h>
  56. #include <assert.h>
  57. #include <limits.h>
  58. #include <string.h>
  59. #include <openssl/asn1.h>
  60. #include <openssl/asn1t.h>
  61. #include <openssl/bn.h>
  62. #include <openssl/bytestring.h>
  63. #include <openssl/err.h>
  64. #include <openssl/mem.h>
  65. #include "internal.h"
  66. static int parse_integer_buggy(CBS *cbs, BIGNUM **out, int buggy) {
  67. assert(*out == NULL);
  68. *out = BN_new();
  69. if (*out == NULL) {
  70. return 0;
  71. }
  72. if (buggy) {
  73. return BN_cbs2unsigned_buggy(cbs, *out);
  74. }
  75. return BN_cbs2unsigned(cbs, *out);
  76. }
  77. static int parse_integer(CBS *cbs, BIGNUM **out) {
  78. return parse_integer_buggy(cbs, out, 0 /* not buggy */);
  79. }
  80. static int marshal_integer(CBB *cbb, BIGNUM *bn) {
  81. if (bn == NULL) {
  82. /* An RSA object may be missing some components. */
  83. OPENSSL_PUT_ERROR(RSA, RSA_R_VALUE_MISSING);
  84. return 0;
  85. }
  86. return BN_bn2cbb(cbb, bn);
  87. }
  88. static RSA *parse_public_key(CBS *cbs, int buggy) {
  89. RSA *ret = RSA_new();
  90. if (ret == NULL) {
  91. return NULL;
  92. }
  93. CBS child;
  94. if (!CBS_get_asn1(cbs, &child, CBS_ASN1_SEQUENCE) ||
  95. !parse_integer_buggy(&child, &ret->n, buggy) ||
  96. !parse_integer(&child, &ret->e) ||
  97. CBS_len(&child) != 0) {
  98. OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_ENCODING);
  99. RSA_free(ret);
  100. return NULL;
  101. }
  102. return ret;
  103. }
  104. RSA *RSA_parse_public_key(CBS *cbs) {
  105. return parse_public_key(cbs, 0 /* not buggy */);
  106. }
  107. RSA *RSA_parse_public_key_buggy(CBS *cbs) {
  108. /* Estonian IDs issued between September 2014 to September 2015 are
  109. * broken. See https://crbug.com/532048 and https://crbug.com/534766.
  110. *
  111. * TODO(davidben): Remove this code and callers in March 2016. */
  112. return parse_public_key(cbs, 1 /* buggy */);
  113. }
  114. RSA *RSA_public_key_from_bytes(const uint8_t *in, size_t in_len) {
  115. CBS cbs;
  116. CBS_init(&cbs, in, in_len);
  117. RSA *ret = RSA_parse_public_key(&cbs);
  118. if (ret == NULL || CBS_len(&cbs) != 0) {
  119. OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_ENCODING);
  120. RSA_free(ret);
  121. return NULL;
  122. }
  123. return ret;
  124. }
  125. int RSA_marshal_public_key(CBB *cbb, const RSA *rsa) {
  126. CBB child;
  127. if (!CBB_add_asn1(cbb, &child, CBS_ASN1_SEQUENCE) ||
  128. !marshal_integer(&child, rsa->n) ||
  129. !marshal_integer(&child, rsa->e) ||
  130. !CBB_flush(cbb)) {
  131. OPENSSL_PUT_ERROR(RSA, RSA_R_ENCODE_ERROR);
  132. return 0;
  133. }
  134. return 1;
  135. }
  136. int RSA_public_key_to_bytes(uint8_t **out_bytes, size_t *out_len,
  137. const RSA *rsa) {
  138. CBB cbb;
  139. CBB_zero(&cbb);
  140. if (!CBB_init(&cbb, 0) ||
  141. !RSA_marshal_public_key(&cbb, rsa) ||
  142. !CBB_finish(&cbb, out_bytes, out_len)) {
  143. OPENSSL_PUT_ERROR(RSA, RSA_R_ENCODE_ERROR);
  144. CBB_cleanup(&cbb);
  145. return 0;
  146. }
  147. return 1;
  148. }
  149. /* kVersionTwoPrime and kVersionMulti are the supported values of the version
  150. * field of an RSAPrivateKey structure (RFC 3447). */
  151. static const uint64_t kVersionTwoPrime = 0;
  152. static const uint64_t kVersionMulti = 1;
  153. /* rsa_parse_additional_prime parses a DER-encoded OtherPrimeInfo from |cbs| and
  154. * advances |cbs|. It returns a newly-allocated |RSA_additional_prime| on
  155. * success or NULL on error. The |r| and |method_mod| fields of the result are
  156. * set to NULL. */
  157. static RSA_additional_prime *rsa_parse_additional_prime(CBS *cbs) {
  158. RSA_additional_prime *ret = OPENSSL_malloc(sizeof(RSA_additional_prime));
  159. if (ret == NULL) {
  160. OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
  161. return 0;
  162. }
  163. memset(ret, 0, sizeof(RSA_additional_prime));
  164. CBS child;
  165. if (!CBS_get_asn1(cbs, &child, CBS_ASN1_SEQUENCE) ||
  166. !parse_integer(&child, &ret->prime) ||
  167. !parse_integer(&child, &ret->exp) ||
  168. !parse_integer(&child, &ret->coeff) ||
  169. CBS_len(&child) != 0) {
  170. OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_ENCODING);
  171. RSA_additional_prime_free(ret);
  172. return NULL;
  173. }
  174. return ret;
  175. }
  176. RSA *RSA_parse_private_key(CBS *cbs) {
  177. BN_CTX *ctx = NULL;
  178. BIGNUM *product_of_primes_so_far = NULL;
  179. RSA *ret = RSA_new();
  180. if (ret == NULL) {
  181. return NULL;
  182. }
  183. CBS child;
  184. uint64_t version;
  185. if (!CBS_get_asn1(cbs, &child, CBS_ASN1_SEQUENCE) ||
  186. !CBS_get_asn1_uint64(&child, &version) ||
  187. (version != kVersionTwoPrime && version != kVersionMulti) ||
  188. !parse_integer(&child, &ret->n) ||
  189. !parse_integer(&child, &ret->e) ||
  190. !parse_integer(&child, &ret->d) ||
  191. !parse_integer(&child, &ret->p) ||
  192. !parse_integer(&child, &ret->q) ||
  193. !parse_integer(&child, &ret->dmp1) ||
  194. !parse_integer(&child, &ret->dmq1) ||
  195. !parse_integer(&child, &ret->iqmp)) {
  196. OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_VERSION);
  197. goto err;
  198. }
  199. /* Multi-prime RSA requires a newer version. */
  200. if (version == kVersionMulti &&
  201. CBS_peek_asn1_tag(&child, CBS_ASN1_SEQUENCE)) {
  202. CBS other_prime_infos;
  203. if (!CBS_get_asn1(&child, &other_prime_infos, CBS_ASN1_SEQUENCE) ||
  204. CBS_len(&other_prime_infos) == 0) {
  205. OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_ENCODING);
  206. goto err;
  207. }
  208. ret->additional_primes = sk_RSA_additional_prime_new_null();
  209. if (ret->additional_primes == NULL) {
  210. OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
  211. goto err;
  212. }
  213. ctx = BN_CTX_new();
  214. product_of_primes_so_far = BN_new();
  215. if (ctx == NULL ||
  216. product_of_primes_so_far == NULL ||
  217. !BN_mul(product_of_primes_so_far, ret->p, ret->q, ctx)) {
  218. goto err;
  219. }
  220. while (CBS_len(&other_prime_infos) > 0) {
  221. RSA_additional_prime *ap = rsa_parse_additional_prime(&other_prime_infos);
  222. if (ap == NULL) {
  223. goto err;
  224. }
  225. if (!sk_RSA_additional_prime_push(ret->additional_primes, ap)) {
  226. OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
  227. RSA_additional_prime_free(ap);
  228. goto err;
  229. }
  230. ap->r = BN_dup(product_of_primes_so_far);
  231. if (ap->r == NULL ||
  232. !BN_mul(product_of_primes_so_far, product_of_primes_so_far,
  233. ap->prime, ctx)) {
  234. goto err;
  235. }
  236. }
  237. }
  238. if (CBS_len(&child) != 0) {
  239. OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_ENCODING);
  240. goto err;
  241. }
  242. BN_CTX_free(ctx);
  243. BN_free(product_of_primes_so_far);
  244. return ret;
  245. err:
  246. BN_CTX_free(ctx);
  247. BN_free(product_of_primes_so_far);
  248. RSA_free(ret);
  249. return NULL;
  250. }
  251. RSA *RSA_private_key_from_bytes(const uint8_t *in, size_t in_len) {
  252. CBS cbs;
  253. CBS_init(&cbs, in, in_len);
  254. RSA *ret = RSA_parse_private_key(&cbs);
  255. if (ret == NULL || CBS_len(&cbs) != 0) {
  256. OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_ENCODING);
  257. RSA_free(ret);
  258. return NULL;
  259. }
  260. return ret;
  261. }
  262. int RSA_marshal_private_key(CBB *cbb, const RSA *rsa) {
  263. const int is_multiprime =
  264. sk_RSA_additional_prime_num(rsa->additional_primes) > 0;
  265. CBB child;
  266. if (!CBB_add_asn1(cbb, &child, CBS_ASN1_SEQUENCE) ||
  267. !CBB_add_asn1_uint64(&child,
  268. is_multiprime ? kVersionMulti : kVersionTwoPrime) ||
  269. !marshal_integer(&child, rsa->n) ||
  270. !marshal_integer(&child, rsa->e) ||
  271. !marshal_integer(&child, rsa->d) ||
  272. !marshal_integer(&child, rsa->p) ||
  273. !marshal_integer(&child, rsa->q) ||
  274. !marshal_integer(&child, rsa->dmp1) ||
  275. !marshal_integer(&child, rsa->dmq1) ||
  276. !marshal_integer(&child, rsa->iqmp)) {
  277. OPENSSL_PUT_ERROR(RSA, RSA_R_ENCODE_ERROR);
  278. return 0;
  279. }
  280. if (is_multiprime) {
  281. CBB other_prime_infos;
  282. if (!CBB_add_asn1(&child, &other_prime_infos, CBS_ASN1_SEQUENCE)) {
  283. OPENSSL_PUT_ERROR(RSA, RSA_R_ENCODE_ERROR);
  284. return 0;
  285. }
  286. size_t i;
  287. for (i = 0; i < sk_RSA_additional_prime_num(rsa->additional_primes); i++) {
  288. RSA_additional_prime *ap =
  289. sk_RSA_additional_prime_value(rsa->additional_primes, i);
  290. CBB other_prime_info;
  291. if (!CBB_add_asn1(&other_prime_infos, &other_prime_info,
  292. CBS_ASN1_SEQUENCE) ||
  293. !marshal_integer(&other_prime_info, ap->prime) ||
  294. !marshal_integer(&other_prime_info, ap->exp) ||
  295. !marshal_integer(&other_prime_info, ap->coeff)) {
  296. OPENSSL_PUT_ERROR(RSA, RSA_R_ENCODE_ERROR);
  297. return 0;
  298. }
  299. }
  300. }
  301. if (!CBB_flush(cbb)) {
  302. OPENSSL_PUT_ERROR(RSA, RSA_R_ENCODE_ERROR);
  303. return 0;
  304. }
  305. return 1;
  306. }
  307. int RSA_private_key_to_bytes(uint8_t **out_bytes, size_t *out_len,
  308. const RSA *rsa) {
  309. CBB cbb;
  310. CBB_zero(&cbb);
  311. if (!CBB_init(&cbb, 0) ||
  312. !RSA_marshal_private_key(&cbb, rsa) ||
  313. !CBB_finish(&cbb, out_bytes, out_len)) {
  314. OPENSSL_PUT_ERROR(RSA, RSA_R_ENCODE_ERROR);
  315. CBB_cleanup(&cbb);
  316. return 0;
  317. }
  318. return 1;
  319. }
  320. RSA *d2i_RSAPublicKey(RSA **out, const uint8_t **inp, long len) {
  321. if (len < 0) {
  322. return NULL;
  323. }
  324. CBS cbs;
  325. CBS_init(&cbs, *inp, (size_t)len);
  326. RSA *ret = RSA_parse_public_key(&cbs);
  327. if (ret == NULL) {
  328. return NULL;
  329. }
  330. if (out != NULL) {
  331. RSA_free(*out);
  332. *out = ret;
  333. }
  334. *inp += (size_t)len - CBS_len(&cbs);
  335. return ret;
  336. }
  337. int i2d_RSAPublicKey(const RSA *in, uint8_t **outp) {
  338. uint8_t *der;
  339. size_t der_len;
  340. if (!RSA_public_key_to_bytes(&der, &der_len, in)) {
  341. return -1;
  342. }
  343. if (der_len > INT_MAX) {
  344. OPENSSL_PUT_ERROR(RSA, ERR_R_OVERFLOW);
  345. OPENSSL_free(der);
  346. return -1;
  347. }
  348. if (outp != NULL) {
  349. if (*outp == NULL) {
  350. *outp = der;
  351. der = NULL;
  352. } else {
  353. memcpy(*outp, der, der_len);
  354. *outp += der_len;
  355. }
  356. }
  357. OPENSSL_free(der);
  358. return (int)der_len;
  359. }
  360. RSA *d2i_RSAPrivateKey(RSA **out, const uint8_t **inp, long len) {
  361. if (len < 0) {
  362. return NULL;
  363. }
  364. CBS cbs;
  365. CBS_init(&cbs, *inp, (size_t)len);
  366. RSA *ret = RSA_parse_private_key(&cbs);
  367. if (ret == NULL) {
  368. return NULL;
  369. }
  370. if (out != NULL) {
  371. RSA_free(*out);
  372. *out = ret;
  373. }
  374. *inp += (size_t)len - CBS_len(&cbs);
  375. return ret;
  376. }
  377. int i2d_RSAPrivateKey(const RSA *in, uint8_t **outp) {
  378. uint8_t *der;
  379. size_t der_len;
  380. if (!RSA_private_key_to_bytes(&der, &der_len, in)) {
  381. return -1;
  382. }
  383. if (der_len > INT_MAX) {
  384. OPENSSL_PUT_ERROR(RSA, ERR_R_OVERFLOW);
  385. OPENSSL_free(der);
  386. return -1;
  387. }
  388. if (outp != NULL) {
  389. if (*outp == NULL) {
  390. *outp = der;
  391. der = NULL;
  392. } else {
  393. memcpy(*outp, der, der_len);
  394. *outp += der_len;
  395. }
  396. }
  397. OPENSSL_free(der);
  398. return (int)der_len;
  399. }
  400. ASN1_SEQUENCE(RSA_PSS_PARAMS) = {
  401. ASN1_EXP_OPT(RSA_PSS_PARAMS, hashAlgorithm, X509_ALGOR,0),
  402. ASN1_EXP_OPT(RSA_PSS_PARAMS, maskGenAlgorithm, X509_ALGOR,1),
  403. ASN1_EXP_OPT(RSA_PSS_PARAMS, saltLength, ASN1_INTEGER,2),
  404. ASN1_EXP_OPT(RSA_PSS_PARAMS, trailerField, ASN1_INTEGER,3),
  405. } ASN1_SEQUENCE_END(RSA_PSS_PARAMS);
  406. IMPLEMENT_ASN1_FUNCTIONS(RSA_PSS_PARAMS);
  407. RSA *RSAPublicKey_dup(const RSA *rsa) {
  408. uint8_t *der;
  409. size_t der_len;
  410. if (!RSA_public_key_to_bytes(&der, &der_len, rsa)) {
  411. return NULL;
  412. }
  413. RSA *ret = RSA_public_key_from_bytes(der, der_len);
  414. OPENSSL_free(der);
  415. return ret;
  416. }
  417. RSA *RSAPrivateKey_dup(const RSA *rsa) {
  418. uint8_t *der;
  419. size_t der_len;
  420. if (!RSA_private_key_to_bytes(&der, &der_len, rsa)) {
  421. return NULL;
  422. }
  423. RSA *ret = RSA_private_key_from_bytes(der, der_len);
  424. OPENSSL_free(der);
  425. return ret;
  426. }