asn1_internal_test.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * Copyright 1999-2021 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. /* Internal tests for the asn1 module */
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <openssl/asn1.h>
  13. #include <openssl/evp.h>
  14. #include <openssl/objects.h>
  15. #include "testutil.h"
  16. #include "internal/nelem.h"
  17. /**********************************************************************
  18. *
  19. * Test of a_strnid's tbl_standard
  20. *
  21. ***/
  22. #include "../crypto/asn1/tbl_standard.h"
  23. static int test_tbl_standard(void)
  24. {
  25. const ASN1_STRING_TABLE *tmp;
  26. int last_nid = -1;
  27. size_t i;
  28. for (tmp = tbl_standard, i = 0; i < OSSL_NELEM(tbl_standard); i++, tmp++) {
  29. if (tmp->nid < last_nid) {
  30. last_nid = 0;
  31. break;
  32. }
  33. last_nid = tmp->nid;
  34. }
  35. if (TEST_int_ne(last_nid, 0)) {
  36. TEST_info("asn1 tbl_standard: Table order OK");
  37. return 1;
  38. }
  39. TEST_info("asn1 tbl_standard: out of order");
  40. for (tmp = tbl_standard, i = 0; i < OSSL_NELEM(tbl_standard); i++, tmp++)
  41. TEST_note("asn1 tbl_standard: Index %zu, NID %d, Name=%s",
  42. i, tmp->nid, OBJ_nid2ln(tmp->nid));
  43. return 0;
  44. }
  45. /**********************************************************************
  46. *
  47. * Test of ameth_lib's standard_methods
  48. *
  49. ***/
  50. #include "crypto/asn1.h"
  51. #include "../crypto/asn1/standard_methods.h"
  52. static int test_standard_methods(void)
  53. {
  54. const EVP_PKEY_ASN1_METHOD **tmp;
  55. int last_pkey_id = -1;
  56. size_t i;
  57. int ok = 1;
  58. for (tmp = standard_methods, i = 0; i < OSSL_NELEM(standard_methods);
  59. i++, tmp++) {
  60. if ((*tmp)->pkey_id < last_pkey_id) {
  61. last_pkey_id = 0;
  62. break;
  63. }
  64. last_pkey_id = (*tmp)->pkey_id;
  65. /*
  66. * One of the following must be true:
  67. *
  68. * pem_str == NULL AND ASN1_PKEY_ALIAS is set
  69. * pem_str != NULL AND ASN1_PKEY_ALIAS is clear
  70. *
  71. * Anything else is an error and may lead to a corrupt ASN1 method table
  72. */
  73. if (!TEST_true(((*tmp)->pem_str == NULL && ((*tmp)->pkey_flags & ASN1_PKEY_ALIAS) != 0)
  74. || ((*tmp)->pem_str != NULL && ((*tmp)->pkey_flags & ASN1_PKEY_ALIAS) == 0))) {
  75. TEST_note("asn1 standard methods: Index %zu, pkey ID %d, Name=%s",
  76. i, (*tmp)->pkey_id, OBJ_nid2sn((*tmp)->pkey_id));
  77. ok = 0;
  78. }
  79. }
  80. if (TEST_int_ne(last_pkey_id, 0)) {
  81. TEST_info("asn1 standard methods: Table order OK");
  82. return ok;
  83. }
  84. TEST_note("asn1 standard methods: out of order");
  85. for (tmp = standard_methods, i = 0; i < OSSL_NELEM(standard_methods);
  86. i++, tmp++)
  87. TEST_note("asn1 standard methods: Index %zu, pkey ID %d, Name=%s",
  88. i, (*tmp)->pkey_id, OBJ_nid2sn((*tmp)->pkey_id));
  89. return 0;
  90. }
  91. /**********************************************************************
  92. *
  93. * Tests of the Unicode code point range
  94. *
  95. ***/
  96. static int test_unicode(const unsigned char *univ, size_t len, int expected)
  97. {
  98. const unsigned char *end = univ + len;
  99. int ok = 1;
  100. for (; univ < end; univ += 4) {
  101. if (!TEST_int_eq(ASN1_mbstring_copy(NULL, univ, 4, MBSTRING_UNIV,
  102. B_ASN1_UTF8STRING),
  103. expected))
  104. ok = 0;
  105. }
  106. return ok;
  107. }
  108. static int test_unicode_range(void)
  109. {
  110. const unsigned char univ_ok[] = "\0\0\0\0"
  111. "\0\0\xd7\xff"
  112. "\0\0\xe0\x00"
  113. "\0\x10\xff\xff";
  114. const unsigned char univ_bad[] = "\0\0\xd8\x00"
  115. "\0\0\xdf\xff"
  116. "\0\x11\x00\x00"
  117. "\x80\x00\x00\x00"
  118. "\xff\xff\xff\xff";
  119. int ok = 1;
  120. if (!test_unicode(univ_ok, sizeof univ_ok - 1, V_ASN1_UTF8STRING))
  121. ok = 0;
  122. if (!test_unicode(univ_bad, sizeof univ_bad - 1, -1))
  123. ok = 0;
  124. return ok;
  125. }
  126. int setup_tests(void)
  127. {
  128. ADD_TEST(test_tbl_standard);
  129. ADD_TEST(test_standard_methods);
  130. ADD_TEST(test_unicode_range);
  131. return 1;
  132. }