Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 
 

345 rader
9.4 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 "../fipsmodule/rsa/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 is the value of the version field for a two-prime
  156. * RSAPrivateKey structure (RFC 3447). */
  157. static const uint64_t kVersionTwoPrime = 0;
  158. RSA *RSA_parse_private_key(CBS *cbs) {
  159. RSA *ret = RSA_new();
  160. if (ret == NULL) {
  161. return NULL;
  162. }
  163. CBS child;
  164. uint64_t version;
  165. if (!CBS_get_asn1(cbs, &child, CBS_ASN1_SEQUENCE) ||
  166. !CBS_get_asn1_uint64(&child, &version)) {
  167. OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_ENCODING);
  168. goto err;
  169. }
  170. if (version != kVersionTwoPrime) {
  171. OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_VERSION);
  172. goto err;
  173. }
  174. if (!parse_integer(&child, &ret->n) ||
  175. !parse_integer(&child, &ret->e) ||
  176. !parse_integer(&child, &ret->d) ||
  177. !parse_integer(&child, &ret->p) ||
  178. !parse_integer(&child, &ret->q) ||
  179. !parse_integer(&child, &ret->dmp1) ||
  180. !parse_integer(&child, &ret->dmq1) ||
  181. !parse_integer(&child, &ret->iqmp)) {
  182. goto err;
  183. }
  184. if (CBS_len(&child) != 0) {
  185. OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_ENCODING);
  186. goto err;
  187. }
  188. if (!RSA_check_key(ret)) {
  189. OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_RSA_PARAMETERS);
  190. goto err;
  191. }
  192. return ret;
  193. err:
  194. RSA_free(ret);
  195. return NULL;
  196. }
  197. RSA *RSA_private_key_from_bytes(const uint8_t *in, size_t in_len) {
  198. CBS cbs;
  199. CBS_init(&cbs, in, in_len);
  200. RSA *ret = RSA_parse_private_key(&cbs);
  201. if (ret == NULL || CBS_len(&cbs) != 0) {
  202. OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_ENCODING);
  203. RSA_free(ret);
  204. return NULL;
  205. }
  206. return ret;
  207. }
  208. int RSA_marshal_private_key(CBB *cbb, const RSA *rsa) {
  209. CBB child;
  210. if (!CBB_add_asn1(cbb, &child, CBS_ASN1_SEQUENCE) ||
  211. !CBB_add_asn1_uint64(&child, kVersionTwoPrime) ||
  212. !marshal_integer(&child, rsa->n) ||
  213. !marshal_integer(&child, rsa->e) ||
  214. !marshal_integer(&child, rsa->d) ||
  215. !marshal_integer(&child, rsa->p) ||
  216. !marshal_integer(&child, rsa->q) ||
  217. !marshal_integer(&child, rsa->dmp1) ||
  218. !marshal_integer(&child, rsa->dmq1) ||
  219. !marshal_integer(&child, rsa->iqmp) ||
  220. !CBB_flush(cbb)) {
  221. OPENSSL_PUT_ERROR(RSA, RSA_R_ENCODE_ERROR);
  222. return 0;
  223. }
  224. return 1;
  225. }
  226. int RSA_private_key_to_bytes(uint8_t **out_bytes, size_t *out_len,
  227. const RSA *rsa) {
  228. CBB cbb;
  229. CBB_zero(&cbb);
  230. if (!CBB_init(&cbb, 0) ||
  231. !RSA_marshal_private_key(&cbb, rsa) ||
  232. !CBB_finish(&cbb, out_bytes, out_len)) {
  233. OPENSSL_PUT_ERROR(RSA, RSA_R_ENCODE_ERROR);
  234. CBB_cleanup(&cbb);
  235. return 0;
  236. }
  237. return 1;
  238. }
  239. RSA *d2i_RSAPublicKey(RSA **out, const uint8_t **inp, long len) {
  240. if (len < 0) {
  241. return NULL;
  242. }
  243. CBS cbs;
  244. CBS_init(&cbs, *inp, (size_t)len);
  245. RSA *ret = RSA_parse_public_key(&cbs);
  246. if (ret == NULL) {
  247. return NULL;
  248. }
  249. if (out != NULL) {
  250. RSA_free(*out);
  251. *out = ret;
  252. }
  253. *inp = CBS_data(&cbs);
  254. return ret;
  255. }
  256. int i2d_RSAPublicKey(const RSA *in, uint8_t **outp) {
  257. CBB cbb;
  258. if (!CBB_init(&cbb, 0) ||
  259. !RSA_marshal_public_key(&cbb, in)) {
  260. CBB_cleanup(&cbb);
  261. return -1;
  262. }
  263. return CBB_finish_i2d(&cbb, outp);
  264. }
  265. RSA *d2i_RSAPrivateKey(RSA **out, const uint8_t **inp, long len) {
  266. if (len < 0) {
  267. return NULL;
  268. }
  269. CBS cbs;
  270. CBS_init(&cbs, *inp, (size_t)len);
  271. RSA *ret = RSA_parse_private_key(&cbs);
  272. if (ret == NULL) {
  273. return NULL;
  274. }
  275. if (out != NULL) {
  276. RSA_free(*out);
  277. *out = ret;
  278. }
  279. *inp = CBS_data(&cbs);
  280. return ret;
  281. }
  282. int i2d_RSAPrivateKey(const RSA *in, uint8_t **outp) {
  283. CBB cbb;
  284. if (!CBB_init(&cbb, 0) ||
  285. !RSA_marshal_private_key(&cbb, in)) {
  286. CBB_cleanup(&cbb);
  287. return -1;
  288. }
  289. return CBB_finish_i2d(&cbb, outp);
  290. }
  291. RSA *RSAPublicKey_dup(const RSA *rsa) {
  292. uint8_t *der;
  293. size_t der_len;
  294. if (!RSA_public_key_to_bytes(&der, &der_len, rsa)) {
  295. return NULL;
  296. }
  297. RSA *ret = RSA_public_key_from_bytes(der, der_len);
  298. OPENSSL_free(der);
  299. return ret;
  300. }
  301. RSA *RSAPrivateKey_dup(const RSA *rsa) {
  302. uint8_t *der;
  303. size_t der_len;
  304. if (!RSA_private_key_to_bytes(&der, &der_len, rsa)) {
  305. return NULL;
  306. }
  307. RSA *ret = RSA_private_key_from_bytes(der, der_len);
  308. OPENSSL_free(der);
  309. return ret;
  310. }