Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

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