25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

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