rsa_asn1.c 13 KB

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