x509_internal_test.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * Copyright 2016-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 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. #include "../crypto/x509/ext_dat.h"
  22. #include "../crypto/x509/standard_exts.h"
  23. static int test_standard_exts(void)
  24. {
  25. size_t i;
  26. int prev = -1, good = 1;
  27. const X509V3_EXT_METHOD **tmp;
  28. tmp = standard_exts;
  29. for (i = 0; i < OSSL_NELEM(standard_exts); i++, tmp++) {
  30. if ((*tmp)->ext_nid < prev)
  31. good = 0;
  32. prev = (*tmp)->ext_nid;
  33. }
  34. if (!good) {
  35. tmp = standard_exts;
  36. TEST_error("Extensions out of order!");
  37. for (i = 0; i < STANDARD_EXTENSION_COUNT; i++, tmp++)
  38. TEST_note("%d : %s", (*tmp)->ext_nid, OBJ_nid2sn((*tmp)->ext_nid));
  39. }
  40. return good;
  41. }
  42. typedef struct {
  43. const char *ipasc;
  44. const char *data;
  45. int length;
  46. } IP_TESTDATA;
  47. static IP_TESTDATA a2i_ipaddress_tests[] = {
  48. {"127.0.0.1", "\x7f\x00\x00\x01", 4},
  49. {"1.2.3.4", "\x01\x02\x03\x04", 4},
  50. {"1.2.3.255", "\x01\x02\x03\xff", 4},
  51. {"1.2.3", NULL, 0},
  52. {"1.2.3 .4", NULL, 0},
  53. {"::1", "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01", 16},
  54. {"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},
  55. {"2001:db8::ff00:42:8329", "\x20\x01\x0d\xb8\x00\x00\x00\x00\x00\x00\xff\x00\x00\x42\x83\x29", 16},
  56. {"1:1:1:1:1:1:1:1.test", NULL, 0},
  57. {":::1", NULL, 0},
  58. {"2001::123g", NULL, 0},
  59. {"example.test", NULL, 0},
  60. {"", NULL, 0},
  61. {"1.2.3.4 ", "\x01\x02\x03\x04", 4},
  62. {" 1.2.3.4", "\x01\x02\x03\x04", 4},
  63. {" 1.2.3.4 ", "\x01\x02\x03\x04", 4},
  64. {"1.2.3.4.example.test", NULL, 0},
  65. };
  66. static int test_a2i_ipaddress(int idx)
  67. {
  68. int good = 1;
  69. ASN1_OCTET_STRING *ip;
  70. int len = a2i_ipaddress_tests[idx].length;
  71. ip = a2i_IPADDRESS(a2i_ipaddress_tests[idx].ipasc);
  72. if (len == 0) {
  73. if (!TEST_ptr_null(ip)) {
  74. good = 0;
  75. TEST_note("'%s' should not be parsed as IP address", a2i_ipaddress_tests[idx].ipasc);
  76. }
  77. } else {
  78. if (!TEST_ptr(ip)
  79. || !TEST_int_eq(ASN1_STRING_length(ip), len)
  80. || !TEST_mem_eq(ASN1_STRING_get0_data(ip), len,
  81. a2i_ipaddress_tests[idx].data, len)) {
  82. good = 0;
  83. }
  84. }
  85. ASN1_OCTET_STRING_free(ip);
  86. return good;
  87. }
  88. int setup_tests(void)
  89. {
  90. ADD_TEST(test_standard_exts);
  91. ADD_ALL_TESTS(test_a2i_ipaddress, OSSL_NELEM(a2i_ipaddress_tests));
  92. return 1;
  93. }