rsa_asn1.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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/bn.h>
  60. #include <openssl/bytestring.h>
  61. #include <openssl/err.h>
  62. #include <openssl/mem.h>
  63. #include "internal.h"
  64. #include "../bytestring/internal.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_parse_asn1_unsigned_buggy(cbs, *out);
  74. }
  75. return BN_parse_asn1_unsigned(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_marshal_asn1(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. if (!BN_is_odd(ret->e) ||
  103. BN_num_bits(ret->e) < 2) {
  104. OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_RSA_PARAMETERS);
  105. RSA_free(ret);
  106. return NULL;
  107. }
  108. return ret;
  109. }
  110. RSA *RSA_parse_public_key(CBS *cbs) {
  111. return parse_public_key(cbs, 0 /* not buggy */);
  112. }
  113. RSA *RSA_parse_public_key_buggy(CBS *cbs) {
  114. /* Estonian IDs issued between September 2014 to September 2015 are
  115. * broken. See https://crbug.com/532048 and https://crbug.com/534766.
  116. *
  117. * TODO(davidben): Remove this code and callers in March 2016. */
  118. return parse_public_key(cbs, 1 /* buggy */);
  119. }
  120. RSA *RSA_public_key_from_bytes(const uint8_t *in, size_t in_len) {
  121. CBS cbs;
  122. CBS_init(&cbs, in, in_len);
  123. RSA *ret = RSA_parse_public_key(&cbs);
  124. if (ret == NULL || CBS_len(&cbs) != 0) {
  125. OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_ENCODING);
  126. RSA_free(ret);
  127. return NULL;
  128. }
  129. return ret;
  130. }
  131. int RSA_marshal_public_key(CBB *cbb, const RSA *rsa) {
  132. CBB child;
  133. if (!CBB_add_asn1(cbb, &child, CBS_ASN1_SEQUENCE) ||
  134. !marshal_integer(&child, rsa->n) ||
  135. !marshal_integer(&child, rsa->e) ||
  136. !CBB_flush(cbb)) {
  137. OPENSSL_PUT_ERROR(RSA, RSA_R_ENCODE_ERROR);
  138. return 0;
  139. }
  140. return 1;
  141. }
  142. int RSA_public_key_to_bytes(uint8_t **out_bytes, size_t *out_len,
  143. const RSA *rsa) {
  144. CBB cbb;
  145. CBB_zero(&cbb);
  146. if (!CBB_init(&cbb, 0) ||
  147. !RSA_marshal_public_key(&cbb, rsa) ||
  148. !CBB_finish(&cbb, out_bytes, out_len)) {
  149. OPENSSL_PUT_ERROR(RSA, RSA_R_ENCODE_ERROR);
  150. CBB_cleanup(&cbb);
  151. return 0;
  152. }
  153. return 1;
  154. }
  155. /* kVersionTwoPrime and kVersionMulti are the supported values of the version
  156. * field of an RSAPrivateKey structure (RFC 3447). */
  157. static const uint64_t kVersionTwoPrime = 0;
  158. static const uint64_t kVersionMulti = 1;
  159. /* rsa_parse_additional_prime parses a DER-encoded OtherPrimeInfo from |cbs| and
  160. * advances |cbs|. It returns a newly-allocated |RSA_additional_prime| on
  161. * success or NULL on error. The |r| and |mont| fields of the result are set to
  162. * NULL. */
  163. static RSA_additional_prime *rsa_parse_additional_prime(CBS *cbs) {
  164. RSA_additional_prime *ret = OPENSSL_malloc(sizeof(RSA_additional_prime));
  165. if (ret == NULL) {
  166. OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
  167. return 0;
  168. }
  169. OPENSSL_memset(ret, 0, sizeof(RSA_additional_prime));
  170. CBS child;
  171. if (!CBS_get_asn1(cbs, &child, CBS_ASN1_SEQUENCE) ||
  172. !parse_integer(&child, &ret->prime) ||
  173. !parse_integer(&child, &ret->exp) ||
  174. !parse_integer(&child, &ret->coeff) ||
  175. CBS_len(&child) != 0) {
  176. OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_ENCODING);
  177. RSA_additional_prime_free(ret);
  178. return NULL;
  179. }
  180. return ret;
  181. }
  182. RSA *RSA_parse_private_key(CBS *cbs) {
  183. BN_CTX *ctx = NULL;
  184. BIGNUM *product_of_primes_so_far = NULL;
  185. RSA *ret = RSA_new();
  186. if (ret == NULL) {
  187. return NULL;
  188. }
  189. CBS child;
  190. uint64_t version;
  191. if (!CBS_get_asn1(cbs, &child, CBS_ASN1_SEQUENCE) ||
  192. !CBS_get_asn1_uint64(&child, &version)) {
  193. OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_ENCODING);
  194. goto err;
  195. }
  196. if (version != kVersionTwoPrime && version != kVersionMulti) {
  197. OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_VERSION);
  198. goto err;
  199. }
  200. if (!parse_integer(&child, &ret->n) ||
  201. !parse_integer(&child, &ret->e) ||
  202. !parse_integer(&child, &ret->d) ||
  203. !parse_integer(&child, &ret->p) ||
  204. !parse_integer(&child, &ret->q) ||
  205. !parse_integer(&child, &ret->dmp1) ||
  206. !parse_integer(&child, &ret->dmq1) ||
  207. !parse_integer(&child, &ret->iqmp)) {
  208. goto err;
  209. }
  210. if (version == kVersionMulti) {
  211. /* Although otherPrimeInfos is written as OPTIONAL in RFC 3447, it later
  212. * says "[otherPrimeInfos] shall be omitted if version is 0 and shall
  213. * contain at least one instance of OtherPrimeInfo if version is 1." The
  214. * OPTIONAL is just so both versions share a single definition. */
  215. CBS other_prime_infos;
  216. if (!CBS_get_asn1(&child, &other_prime_infos, CBS_ASN1_SEQUENCE) ||
  217. CBS_len(&other_prime_infos) == 0) {
  218. OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_ENCODING);
  219. goto err;
  220. }
  221. ret->additional_primes = sk_RSA_additional_prime_new_null();
  222. if (ret->additional_primes == NULL) {
  223. OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
  224. goto err;
  225. }
  226. ctx = BN_CTX_new();
  227. product_of_primes_so_far = BN_new();
  228. if (ctx == NULL ||
  229. product_of_primes_so_far == NULL ||
  230. !BN_mul(product_of_primes_so_far, ret->p, ret->q, ctx)) {
  231. goto err;
  232. }
  233. while (CBS_len(&other_prime_infos) > 0) {
  234. RSA_additional_prime *ap = rsa_parse_additional_prime(&other_prime_infos);
  235. if (ap == NULL) {
  236. goto err;
  237. }
  238. if (!sk_RSA_additional_prime_push(ret->additional_primes, ap)) {
  239. OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
  240. RSA_additional_prime_free(ap);
  241. goto err;
  242. }
  243. ap->r = BN_dup(product_of_primes_so_far);
  244. if (ap->r == NULL ||
  245. !BN_mul(product_of_primes_so_far, product_of_primes_so_far,
  246. ap->prime, ctx)) {
  247. goto err;
  248. }
  249. }
  250. }
  251. if (CBS_len(&child) != 0) {
  252. OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_ENCODING);
  253. goto err;
  254. }
  255. if (!RSA_check_key(ret)) {
  256. OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_RSA_PARAMETERS);
  257. goto err;
  258. }
  259. BN_CTX_free(ctx);
  260. BN_free(product_of_primes_so_far);
  261. return ret;
  262. err:
  263. BN_CTX_free(ctx);
  264. BN_free(product_of_primes_so_far);
  265. RSA_free(ret);
  266. return NULL;
  267. }
  268. RSA *RSA_private_key_from_bytes(const uint8_t *in, size_t in_len) {
  269. CBS cbs;
  270. CBS_init(&cbs, in, in_len);
  271. RSA *ret = RSA_parse_private_key(&cbs);
  272. if (ret == NULL || CBS_len(&cbs) != 0) {
  273. OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_ENCODING);
  274. RSA_free(ret);
  275. return NULL;
  276. }
  277. return ret;
  278. }
  279. int RSA_marshal_private_key(CBB *cbb, const RSA *rsa) {
  280. const int is_multiprime =
  281. sk_RSA_additional_prime_num(rsa->additional_primes) > 0;
  282. CBB child;
  283. if (!CBB_add_asn1(cbb, &child, CBS_ASN1_SEQUENCE) ||
  284. !CBB_add_asn1_uint64(&child,
  285. is_multiprime ? kVersionMulti : kVersionTwoPrime) ||
  286. !marshal_integer(&child, rsa->n) ||
  287. !marshal_integer(&child, rsa->e) ||
  288. !marshal_integer(&child, rsa->d) ||
  289. !marshal_integer(&child, rsa->p) ||
  290. !marshal_integer(&child, rsa->q) ||
  291. !marshal_integer(&child, rsa->dmp1) ||
  292. !marshal_integer(&child, rsa->dmq1) ||
  293. !marshal_integer(&child, rsa->iqmp)) {
  294. OPENSSL_PUT_ERROR(RSA, RSA_R_ENCODE_ERROR);
  295. return 0;
  296. }
  297. CBB other_prime_infos;
  298. if (is_multiprime) {
  299. if (!CBB_add_asn1(&child, &other_prime_infos, CBS_ASN1_SEQUENCE)) {
  300. OPENSSL_PUT_ERROR(RSA, RSA_R_ENCODE_ERROR);
  301. return 0;
  302. }
  303. for (size_t i = 0; i < sk_RSA_additional_prime_num(rsa->additional_primes);
  304. i++) {
  305. RSA_additional_prime *ap =
  306. sk_RSA_additional_prime_value(rsa->additional_primes, i);
  307. CBB other_prime_info;
  308. if (!CBB_add_asn1(&other_prime_infos, &other_prime_info,
  309. CBS_ASN1_SEQUENCE) ||
  310. !marshal_integer(&other_prime_info, ap->prime) ||
  311. !marshal_integer(&other_prime_info, ap->exp) ||
  312. !marshal_integer(&other_prime_info, ap->coeff) ||
  313. !CBB_flush(&other_prime_infos)) {
  314. OPENSSL_PUT_ERROR(RSA, RSA_R_ENCODE_ERROR);
  315. return 0;
  316. }
  317. }
  318. }
  319. if (!CBB_flush(cbb)) {
  320. OPENSSL_PUT_ERROR(RSA, RSA_R_ENCODE_ERROR);
  321. return 0;
  322. }
  323. return 1;
  324. }
  325. int RSA_private_key_to_bytes(uint8_t **out_bytes, size_t *out_len,
  326. const RSA *rsa) {
  327. CBB cbb;
  328. CBB_zero(&cbb);
  329. if (!CBB_init(&cbb, 0) ||
  330. !RSA_marshal_private_key(&cbb, rsa) ||
  331. !CBB_finish(&cbb, out_bytes, out_len)) {
  332. OPENSSL_PUT_ERROR(RSA, RSA_R_ENCODE_ERROR);
  333. CBB_cleanup(&cbb);
  334. return 0;
  335. }
  336. return 1;
  337. }
  338. RSA *d2i_RSAPublicKey(RSA **out, const uint8_t **inp, long len) {
  339. if (len < 0) {
  340. return NULL;
  341. }
  342. CBS cbs;
  343. CBS_init(&cbs, *inp, (size_t)len);
  344. RSA *ret = RSA_parse_public_key(&cbs);
  345. if (ret == NULL) {
  346. return NULL;
  347. }
  348. if (out != NULL) {
  349. RSA_free(*out);
  350. *out = ret;
  351. }
  352. *inp = CBS_data(&cbs);
  353. return ret;
  354. }
  355. int i2d_RSAPublicKey(const RSA *in, uint8_t **outp) {
  356. CBB cbb;
  357. if (!CBB_init(&cbb, 0) ||
  358. !RSA_marshal_public_key(&cbb, in)) {
  359. CBB_cleanup(&cbb);
  360. return -1;
  361. }
  362. return CBB_finish_i2d(&cbb, outp);
  363. }
  364. RSA *d2i_RSAPrivateKey(RSA **out, const uint8_t **inp, long len) {
  365. if (len < 0) {
  366. return NULL;
  367. }
  368. CBS cbs;
  369. CBS_init(&cbs, *inp, (size_t)len);
  370. RSA *ret = RSA_parse_private_key(&cbs);
  371. if (ret == NULL) {
  372. return NULL;
  373. }
  374. if (out != NULL) {
  375. RSA_free(*out);
  376. *out = ret;
  377. }
  378. *inp = CBS_data(&cbs);
  379. return ret;
  380. }
  381. int i2d_RSAPrivateKey(const RSA *in, uint8_t **outp) {
  382. CBB cbb;
  383. if (!CBB_init(&cbb, 0) ||
  384. !RSA_marshal_private_key(&cbb, in)) {
  385. CBB_cleanup(&cbb);
  386. return -1;
  387. }
  388. return CBB_finish_i2d(&cbb, outp);
  389. }
  390. RSA *RSAPublicKey_dup(const RSA *rsa) {
  391. uint8_t *der;
  392. size_t der_len;
  393. if (!RSA_public_key_to_bytes(&der, &der_len, rsa)) {
  394. return NULL;
  395. }
  396. RSA *ret = RSA_public_key_from_bytes(der, der_len);
  397. OPENSSL_free(der);
  398. return ret;
  399. }
  400. RSA *RSAPrivateKey_dup(const RSA *rsa) {
  401. uint8_t *der;
  402. size_t der_len;
  403. if (!RSA_private_key_to_bytes(&der, &der_len, rsa)) {
  404. return NULL;
  405. }
  406. RSA *ret = RSA_private_key_from_bytes(der, der_len);
  407. OPENSSL_free(der);
  408. return ret;
  409. }