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.
 
 
 
 
 
 

80 rader
2.4 KiB

  1. /* Copyright (c) 2016, 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 <stdio.h>
  15. #include <openssl/asn1.h>
  16. #include <openssl/crypto.h>
  17. #include <openssl/err.h>
  18. // kTag128 is an ASN.1 structure with a universal tag with number 128.
  19. static const uint8_t kTag128[] = {
  20. 0x1f, 0x81, 0x00, 0x01, 0x00,
  21. };
  22. // kTag258 is an ASN.1 structure with a universal tag with number 258.
  23. static const uint8_t kTag258[] = {
  24. 0x1f, 0x82, 0x02, 0x01, 0x00,
  25. };
  26. static_assert(V_ASN1_NEG_INTEGER == 258,
  27. "V_ASN1_NEG_INTEGER changed. Update kTag258 to collide with it.");
  28. // kTagOverflow is an ASN.1 structure with a universal tag with number 2^35-1,
  29. // which will not fit in an int.
  30. static const uint8_t kTagOverflow[] = {
  31. 0x1f, 0xff, 0xff, 0xff, 0xff, 0x7f, 0x01, 0x00,
  32. };
  33. static bool TestLargeTags() {
  34. const uint8_t *p = kTag258;
  35. bssl::UniquePtr<ASN1_TYPE> obj(d2i_ASN1_TYPE(NULL, &p, sizeof(kTag258)));
  36. if (obj) {
  37. fprintf(stderr, "Parsed value with illegal tag (type = %d).\n", obj->type);
  38. return false;
  39. }
  40. ERR_clear_error();
  41. p = kTagOverflow;
  42. obj.reset(d2i_ASN1_TYPE(NULL, &p, sizeof(kTagOverflow)));
  43. if (obj) {
  44. fprintf(stderr, "Parsed value with tag overflow (type = %d).\n", obj->type);
  45. return false;
  46. }
  47. ERR_clear_error();
  48. p = kTag128;
  49. obj.reset(d2i_ASN1_TYPE(NULL, &p, sizeof(kTag128)));
  50. if (!obj || obj->type != 128 || obj->value.asn1_string->length != 1 ||
  51. obj->value.asn1_string->data[0] != 0) {
  52. fprintf(stderr, "Failed to parse value with tag 128.\n");
  53. ERR_print_errors_fp(stderr);
  54. return false;
  55. }
  56. return true;
  57. }
  58. int main() {
  59. CRYPTO_library_init();
  60. if (!TestLargeTags()) {
  61. return 1;
  62. }
  63. printf("PASS\n");
  64. return 0;
  65. }