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.

bn_asn1.c 2.6 KiB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /* Copyright (c) 2015, Google Inc.
  2. *
  3. * Permission to use, copy, modify, and/or distribute this software for any
  4. * purpose with or without fee is hereby granted, provided that the above
  5. * copyright notice and this permission notice appear in all copies.
  6. *
  7. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  10. * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  12. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
  14. #include <openssl/bn.h>
  15. #include <openssl/bytestring.h>
  16. #include <openssl/err.h>
  17. int BN_parse_asn1_unsigned(CBS *cbs, BIGNUM *ret) {
  18. CBS child;
  19. if (!CBS_get_asn1(cbs, &child, CBS_ASN1_INTEGER) ||
  20. CBS_len(&child) == 0) {
  21. OPENSSL_PUT_ERROR(BN, BN_R_BAD_ENCODING);
  22. return 0;
  23. }
  24. if (CBS_data(&child)[0] & 0x80) {
  25. OPENSSL_PUT_ERROR(BN, BN_R_NEGATIVE_NUMBER);
  26. return 0;
  27. }
  28. /* INTEGERs must be minimal. */
  29. if (CBS_data(&child)[0] == 0x00 &&
  30. CBS_len(&child) > 1 &&
  31. !(CBS_data(&child)[1] & 0x80)) {
  32. OPENSSL_PUT_ERROR(BN, BN_R_BAD_ENCODING);
  33. return 0;
  34. }
  35. return BN_bin2bn(CBS_data(&child), CBS_len(&child), ret) != NULL;
  36. }
  37. int BN_parse_asn1_unsigned_buggy(CBS *cbs, BIGNUM *ret) {
  38. CBS child;
  39. if (!CBS_get_asn1(cbs, &child, CBS_ASN1_INTEGER) ||
  40. CBS_len(&child) == 0) {
  41. OPENSSL_PUT_ERROR(BN, BN_R_BAD_ENCODING);
  42. return 0;
  43. }
  44. /* This function intentionally does not reject negative numbers or non-minimal
  45. * encodings. Estonian IDs issued between September 2014 to September 2015 are
  46. * broken. See https://crbug.com/532048 and https://crbug.com/534766.
  47. *
  48. * TODO(davidben): Remove this code and callers in March 2016. */
  49. return BN_bin2bn(CBS_data(&child), CBS_len(&child), ret) != NULL;
  50. }
  51. int BN_marshal_asn1(CBB *cbb, const BIGNUM *bn) {
  52. /* Negative numbers are unsupported. */
  53. if (BN_is_negative(bn)) {
  54. OPENSSL_PUT_ERROR(BN, BN_R_NEGATIVE_NUMBER);
  55. return 0;
  56. }
  57. CBB child;
  58. if (!CBB_add_asn1(cbb, &child, CBS_ASN1_INTEGER) ||
  59. /* The number must be padded with a leading zero if the high bit would
  60. * otherwise be set or if |bn| is zero. */
  61. (BN_num_bits(bn) % 8 == 0 && !CBB_add_u8(&child, 0x00)) ||
  62. !BN_bn2cbb_padded(&child, BN_num_bytes(bn), bn) ||
  63. !CBB_flush(cbb)) {
  64. OPENSSL_PUT_ERROR(BN, BN_R_ENCODE_ERROR);
  65. return 0;
  66. }
  67. return 1;
  68. }