Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

326 linhas
8.8 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(CBS *cbs, BIGNUM **out) {
  67. assert(*out == NULL);
  68. *out = BN_new();
  69. if (*out == NULL) {
  70. return 0;
  71. }
  72. return BN_parse_asn1_unsigned(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_marshal_asn1(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. if (!BN_is_odd(ret->e) ||
  97. BN_num_bits(ret->e) < 2) {
  98. OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_RSA_PARAMETERS);
  99. RSA_free(ret);
  100. return NULL;
  101. }
  102. return ret;
  103. }
  104. RSA *RSA_public_key_from_bytes(const uint8_t *in, size_t in_len) {
  105. CBS cbs;
  106. CBS_init(&cbs, in, in_len);
  107. RSA *ret = RSA_parse_public_key(&cbs);
  108. if (ret == NULL || CBS_len(&cbs) != 0) {
  109. OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_ENCODING);
  110. RSA_free(ret);
  111. return NULL;
  112. }
  113. return ret;
  114. }
  115. int RSA_marshal_public_key(CBB *cbb, const RSA *rsa) {
  116. CBB child;
  117. if (!CBB_add_asn1(cbb, &child, CBS_ASN1_SEQUENCE) ||
  118. !marshal_integer(&child, rsa->n) ||
  119. !marshal_integer(&child, rsa->e) ||
  120. !CBB_flush(cbb)) {
  121. OPENSSL_PUT_ERROR(RSA, RSA_R_ENCODE_ERROR);
  122. return 0;
  123. }
  124. return 1;
  125. }
  126. int RSA_public_key_to_bytes(uint8_t **out_bytes, size_t *out_len,
  127. const RSA *rsa) {
  128. CBB cbb;
  129. CBB_zero(&cbb);
  130. if (!CBB_init(&cbb, 0) ||
  131. !RSA_marshal_public_key(&cbb, rsa) ||
  132. !CBB_finish(&cbb, out_bytes, out_len)) {
  133. OPENSSL_PUT_ERROR(RSA, RSA_R_ENCODE_ERROR);
  134. CBB_cleanup(&cbb);
  135. return 0;
  136. }
  137. return 1;
  138. }
  139. // kVersionTwoPrime is the value of the version field for a two-prime
  140. // RSAPrivateKey structure (RFC 3447).
  141. static const uint64_t kVersionTwoPrime = 0;
  142. RSA *RSA_parse_private_key(CBS *cbs) {
  143. RSA *ret = RSA_new();
  144. if (ret == NULL) {
  145. return NULL;
  146. }
  147. CBS child;
  148. uint64_t version;
  149. if (!CBS_get_asn1(cbs, &child, CBS_ASN1_SEQUENCE) ||
  150. !CBS_get_asn1_uint64(&child, &version)) {
  151. OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_ENCODING);
  152. goto err;
  153. }
  154. if (version != kVersionTwoPrime) {
  155. OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_VERSION);
  156. goto err;
  157. }
  158. if (!parse_integer(&child, &ret->n) ||
  159. !parse_integer(&child, &ret->e) ||
  160. !parse_integer(&child, &ret->d) ||
  161. !parse_integer(&child, &ret->p) ||
  162. !parse_integer(&child, &ret->q) ||
  163. !parse_integer(&child, &ret->dmp1) ||
  164. !parse_integer(&child, &ret->dmq1) ||
  165. !parse_integer(&child, &ret->iqmp)) {
  166. goto err;
  167. }
  168. if (CBS_len(&child) != 0) {
  169. OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_ENCODING);
  170. goto err;
  171. }
  172. if (!RSA_check_key(ret)) {
  173. OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_RSA_PARAMETERS);
  174. goto err;
  175. }
  176. return ret;
  177. err:
  178. RSA_free(ret);
  179. return NULL;
  180. }
  181. RSA *RSA_private_key_from_bytes(const uint8_t *in, size_t in_len) {
  182. CBS cbs;
  183. CBS_init(&cbs, in, in_len);
  184. RSA *ret = RSA_parse_private_key(&cbs);
  185. if (ret == NULL || CBS_len(&cbs) != 0) {
  186. OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_ENCODING);
  187. RSA_free(ret);
  188. return NULL;
  189. }
  190. return ret;
  191. }
  192. int RSA_marshal_private_key(CBB *cbb, const RSA *rsa) {
  193. CBB child;
  194. if (!CBB_add_asn1(cbb, &child, CBS_ASN1_SEQUENCE) ||
  195. !CBB_add_asn1_uint64(&child, kVersionTwoPrime) ||
  196. !marshal_integer(&child, rsa->n) ||
  197. !marshal_integer(&child, rsa->e) ||
  198. !marshal_integer(&child, rsa->d) ||
  199. !marshal_integer(&child, rsa->p) ||
  200. !marshal_integer(&child, rsa->q) ||
  201. !marshal_integer(&child, rsa->dmp1) ||
  202. !marshal_integer(&child, rsa->dmq1) ||
  203. !marshal_integer(&child, rsa->iqmp) ||
  204. !CBB_flush(cbb)) {
  205. OPENSSL_PUT_ERROR(RSA, RSA_R_ENCODE_ERROR);
  206. return 0;
  207. }
  208. return 1;
  209. }
  210. int RSA_private_key_to_bytes(uint8_t **out_bytes, size_t *out_len,
  211. const RSA *rsa) {
  212. CBB cbb;
  213. CBB_zero(&cbb);
  214. if (!CBB_init(&cbb, 0) ||
  215. !RSA_marshal_private_key(&cbb, rsa) ||
  216. !CBB_finish(&cbb, out_bytes, out_len)) {
  217. OPENSSL_PUT_ERROR(RSA, RSA_R_ENCODE_ERROR);
  218. CBB_cleanup(&cbb);
  219. return 0;
  220. }
  221. return 1;
  222. }
  223. RSA *d2i_RSAPublicKey(RSA **out, const uint8_t **inp, long len) {
  224. if (len < 0) {
  225. return NULL;
  226. }
  227. CBS cbs;
  228. CBS_init(&cbs, *inp, (size_t)len);
  229. RSA *ret = RSA_parse_public_key(&cbs);
  230. if (ret == NULL) {
  231. return NULL;
  232. }
  233. if (out != NULL) {
  234. RSA_free(*out);
  235. *out = ret;
  236. }
  237. *inp = CBS_data(&cbs);
  238. return ret;
  239. }
  240. int i2d_RSAPublicKey(const RSA *in, uint8_t **outp) {
  241. CBB cbb;
  242. if (!CBB_init(&cbb, 0) ||
  243. !RSA_marshal_public_key(&cbb, in)) {
  244. CBB_cleanup(&cbb);
  245. return -1;
  246. }
  247. return CBB_finish_i2d(&cbb, outp);
  248. }
  249. RSA *d2i_RSAPrivateKey(RSA **out, const uint8_t **inp, long len) {
  250. if (len < 0) {
  251. return NULL;
  252. }
  253. CBS cbs;
  254. CBS_init(&cbs, *inp, (size_t)len);
  255. RSA *ret = RSA_parse_private_key(&cbs);
  256. if (ret == NULL) {
  257. return NULL;
  258. }
  259. if (out != NULL) {
  260. RSA_free(*out);
  261. *out = ret;
  262. }
  263. *inp = CBS_data(&cbs);
  264. return ret;
  265. }
  266. int i2d_RSAPrivateKey(const RSA *in, uint8_t **outp) {
  267. CBB cbb;
  268. if (!CBB_init(&cbb, 0) ||
  269. !RSA_marshal_private_key(&cbb, in)) {
  270. CBB_cleanup(&cbb);
  271. return -1;
  272. }
  273. return CBB_finish_i2d(&cbb, outp);
  274. }
  275. RSA *RSAPublicKey_dup(const RSA *rsa) {
  276. uint8_t *der;
  277. size_t der_len;
  278. if (!RSA_public_key_to_bytes(&der, &der_len, rsa)) {
  279. return NULL;
  280. }
  281. RSA *ret = RSA_public_key_from_bytes(der, der_len);
  282. OPENSSL_free(der);
  283. return ret;
  284. }
  285. RSA *RSAPrivateKey_dup(const RSA *rsa) {
  286. uint8_t *der;
  287. size_t der_len;
  288. if (!RSA_private_key_to_bytes(&der, &der_len, rsa)) {
  289. return NULL;
  290. }
  291. RSA *ret = RSA_private_key_from_bytes(der, der_len);
  292. OPENSSL_free(der);
  293. return ret;
  294. }