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.
 
 
 
 
 
 

447 line
13 KiB

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