x509_internal_test.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * Copyright 2016-2022 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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 x509 and x509v3 modules */
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <openssl/x509.h>
  13. #include <openssl/x509v3.h>
  14. #include "testutil.h"
  15. #include "internal/nelem.h"
  16. /**********************************************************************
  17. *
  18. * Test of x509v3
  19. *
  20. ***/
  21. #ifdef __VMS
  22. # pragma names save
  23. # pragma names as_is,shortened
  24. #endif
  25. #include "../crypto/x509v3/ext_dat.h"
  26. #include "../crypto/x509v3/standard_exts.h"
  27. #ifdef __VMS
  28. # pragma names restore
  29. #endif
  30. static int test_standard_exts(void)
  31. {
  32. size_t i;
  33. int prev = -1, good = 1;
  34. const X509V3_EXT_METHOD **tmp;
  35. tmp = standard_exts;
  36. for (i = 0; i < OSSL_NELEM(standard_exts); i++, tmp++) {
  37. if ((*tmp)->ext_nid < prev)
  38. good = 0;
  39. prev = (*tmp)->ext_nid;
  40. }
  41. if (!good) {
  42. tmp = standard_exts;
  43. TEST_error("Extensions out of order!");
  44. for (i = 0; i < STANDARD_EXTENSION_COUNT; i++, tmp++)
  45. TEST_note("%d : %s", (*tmp)->ext_nid, OBJ_nid2sn((*tmp)->ext_nid));
  46. }
  47. return good;
  48. }
  49. typedef struct {
  50. const char *ipasc;
  51. const char *data;
  52. int length;
  53. } IP_TESTDATA;
  54. static IP_TESTDATA a2i_ipaddress_tests[] = {
  55. {"127.0.0.1", "\x7f\x00\x00\x01", 4},
  56. {"1.2.3.4", "\x01\x02\x03\x04", 4},
  57. {"1.2.3.255", "\x01\x02\x03\xff", 4},
  58. {"1.2.3", NULL, 0},
  59. {"1.2.3 .4", NULL, 0},
  60. {"::1", "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01", 16},
  61. {"1:1:1:1:1:1:1:1", "\x00\x01\x00\x01\x00\x01\x00\x01\x00\x01\x00\x01\x00\x01\x00\x01", 16},
  62. {"2001:db8::ff00:42:8329", "\x20\x01\x0d\xb8\x00\x00\x00\x00\x00\x00\xff\x00\x00\x42\x83\x29", 16},
  63. {"1:1:1:1:1:1:1:1.test", NULL, 0},
  64. {":::1", NULL, 0},
  65. {"2001::123g", NULL, 0},
  66. {"example.test", NULL, 0},
  67. {"", NULL, 0},
  68. {"1.2.3.4 ", "\x01\x02\x03\x04", 4},
  69. {" 1.2.3.4", "\x01\x02\x03\x04", 4},
  70. {" 1.2.3.4 ", "\x01\x02\x03\x04", 4},
  71. {"1.2.3.4.example.test", NULL, 0},
  72. };
  73. static int test_a2i_ipaddress(int idx)
  74. {
  75. int good = 1;
  76. ASN1_OCTET_STRING *ip;
  77. int len = a2i_ipaddress_tests[idx].length;
  78. ip = a2i_IPADDRESS(a2i_ipaddress_tests[idx].ipasc);
  79. if (len == 0) {
  80. if (!TEST_ptr_null(ip)) {
  81. good = 0;
  82. TEST_note("'%s' should not be parsed as IP address", a2i_ipaddress_tests[idx].ipasc);
  83. }
  84. } else {
  85. if (!TEST_ptr(ip)
  86. || !TEST_int_eq(ASN1_STRING_length(ip), len)
  87. || !TEST_mem_eq(ASN1_STRING_get0_data(ip), len,
  88. a2i_ipaddress_tests[idx].data, len)) {
  89. good = 0;
  90. }
  91. }
  92. ASN1_OCTET_STRING_free(ip);
  93. return good;
  94. }
  95. int setup_tests(void)
  96. {
  97. ADD_TEST(test_standard_exts);
  98. ADD_ALL_TESTS(test_a2i_ipaddress, OSSL_NELEM(a2i_ipaddress_tests));
  99. return 1;
  100. }