asn1_decode_test.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * Copyright 2017-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <openssl/rand.h>
  12. #include <openssl/asn1t.h>
  13. #include "internal/numbers.h"
  14. #include "testutil.h"
  15. #ifdef __GNUC__
  16. # pragma GCC diagnostic ignored "-Wunused-function"
  17. #endif
  18. #ifdef __clang__
  19. # pragma clang diagnostic ignored "-Wunused-function"
  20. #endif
  21. /* Badly coded ASN.1 INTEGER zero wrapped in a sequence */
  22. static unsigned char t_invalid_zero[] = {
  23. 0x30, 0x02, /* SEQUENCE tag + length */
  24. 0x02, 0x00 /* INTEGER tag + length */
  25. };
  26. #ifndef OPENSSL_NO_DEPRECATED_3_0
  27. /* LONG case ************************************************************* */
  28. typedef struct {
  29. long test_long;
  30. } ASN1_LONG_DATA;
  31. ASN1_SEQUENCE(ASN1_LONG_DATA) = {
  32. ASN1_EMBED(ASN1_LONG_DATA, test_long, LONG),
  33. } static_ASN1_SEQUENCE_END(ASN1_LONG_DATA)
  34. IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(ASN1_LONG_DATA)
  35. IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(ASN1_LONG_DATA)
  36. static int test_long(void)
  37. {
  38. const unsigned char *p = t_invalid_zero;
  39. ASN1_LONG_DATA *dectst =
  40. d2i_ASN1_LONG_DATA(NULL, &p, sizeof(t_invalid_zero));
  41. if (dectst == NULL)
  42. return 0; /* Fail */
  43. ASN1_LONG_DATA_free(dectst);
  44. return 1;
  45. }
  46. #endif
  47. /* INT32 case ************************************************************* */
  48. typedef struct {
  49. int32_t test_int32;
  50. } ASN1_INT32_DATA;
  51. ASN1_SEQUENCE(ASN1_INT32_DATA) = {
  52. ASN1_EMBED(ASN1_INT32_DATA, test_int32, INT32),
  53. } static_ASN1_SEQUENCE_END(ASN1_INT32_DATA)
  54. IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(ASN1_INT32_DATA)
  55. IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(ASN1_INT32_DATA)
  56. static int test_int32(void)
  57. {
  58. const unsigned char *p = t_invalid_zero;
  59. ASN1_INT32_DATA *dectst =
  60. d2i_ASN1_INT32_DATA(NULL, &p, sizeof(t_invalid_zero));
  61. if (dectst == NULL)
  62. return 0; /* Fail */
  63. ASN1_INT32_DATA_free(dectst);
  64. return 1;
  65. }
  66. /* UINT32 case ************************************************************* */
  67. typedef struct {
  68. uint32_t test_uint32;
  69. } ASN1_UINT32_DATA;
  70. ASN1_SEQUENCE(ASN1_UINT32_DATA) = {
  71. ASN1_EMBED(ASN1_UINT32_DATA, test_uint32, UINT32),
  72. } static_ASN1_SEQUENCE_END(ASN1_UINT32_DATA)
  73. IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(ASN1_UINT32_DATA)
  74. IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(ASN1_UINT32_DATA)
  75. static int test_uint32(void)
  76. {
  77. const unsigned char *p = t_invalid_zero;
  78. ASN1_UINT32_DATA *dectst =
  79. d2i_ASN1_UINT32_DATA(NULL, &p, sizeof(t_invalid_zero));
  80. if (dectst == NULL)
  81. return 0; /* Fail */
  82. ASN1_UINT32_DATA_free(dectst);
  83. return 1;
  84. }
  85. /* INT64 case ************************************************************* */
  86. typedef struct {
  87. int64_t test_int64;
  88. } ASN1_INT64_DATA;
  89. ASN1_SEQUENCE(ASN1_INT64_DATA) = {
  90. ASN1_EMBED(ASN1_INT64_DATA, test_int64, INT64),
  91. } static_ASN1_SEQUENCE_END(ASN1_INT64_DATA)
  92. IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(ASN1_INT64_DATA)
  93. IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(ASN1_INT64_DATA)
  94. static int test_int64(void)
  95. {
  96. const unsigned char *p = t_invalid_zero;
  97. ASN1_INT64_DATA *dectst =
  98. d2i_ASN1_INT64_DATA(NULL, &p, sizeof(t_invalid_zero));
  99. if (dectst == NULL)
  100. return 0; /* Fail */
  101. ASN1_INT64_DATA_free(dectst);
  102. return 1;
  103. }
  104. /* UINT64 case ************************************************************* */
  105. typedef struct {
  106. uint64_t test_uint64;
  107. } ASN1_UINT64_DATA;
  108. ASN1_SEQUENCE(ASN1_UINT64_DATA) = {
  109. ASN1_EMBED(ASN1_UINT64_DATA, test_uint64, UINT64),
  110. } static_ASN1_SEQUENCE_END(ASN1_UINT64_DATA)
  111. IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(ASN1_UINT64_DATA)
  112. IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(ASN1_UINT64_DATA)
  113. static int test_uint64(void)
  114. {
  115. const unsigned char *p = t_invalid_zero;
  116. ASN1_UINT64_DATA *dectst =
  117. d2i_ASN1_UINT64_DATA(NULL, &p, sizeof(t_invalid_zero));
  118. if (dectst == NULL)
  119. return 0; /* Fail */
  120. ASN1_UINT64_DATA_free(dectst);
  121. return 1;
  122. }
  123. int setup_tests(void)
  124. {
  125. #ifndef OPENSSL_NO_DEPRECATED_3_0
  126. ADD_TEST(test_long);
  127. #endif
  128. ADD_TEST(test_int32);
  129. ADD_TEST(test_uint32);
  130. ADD_TEST(test_int64);
  131. ADD_TEST(test_uint64);
  132. return 1;
  133. }