x509_acert_test.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * Copyright 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. #include <openssl/pem.h>
  10. #include <openssl/rsa.h>
  11. #include <openssl/x509_acert.h>
  12. #include "testutil.h"
  13. static int test_print_acert(int idx)
  14. {
  15. int ret = 0;
  16. const char *acert_file;
  17. X509_ACERT *acert = NULL;
  18. BIO *bp, *bout;
  19. if (!TEST_ptr(acert_file = test_get_argument(idx)))
  20. return 0;
  21. if (!TEST_ptr(bp = BIO_new_file(acert_file, "r")))
  22. return 0;
  23. if (!TEST_ptr(bout = BIO_new_fp(stderr, BIO_NOCLOSE)))
  24. goto err;
  25. if (!TEST_ptr(acert = PEM_read_bio_X509_ACERT(bp, NULL, NULL, NULL)))
  26. goto err;
  27. if (!TEST_int_eq(X509_ACERT_print(bout, acert), 1)) {
  28. goto err;
  29. }
  30. ret = 1;
  31. err:
  32. BIO_free(bp);
  33. BIO_free(bout);
  34. X509_ACERT_free(acert);
  35. return ret;
  36. }
  37. static int test_acert_sign(void)
  38. {
  39. int ret = 0;
  40. const char *acert_file;
  41. EVP_PKEY *pkey;
  42. BIO *bp = NULL;
  43. X509_ACERT *acert = NULL;
  44. if (!TEST_ptr(acert_file = test_get_argument(0)))
  45. return 0;
  46. if (!TEST_ptr(pkey = EVP_RSA_gen(2048)))
  47. return 0;
  48. if (!TEST_ptr(bp = BIO_new_file(acert_file, "r")))
  49. goto err;
  50. if (!TEST_ptr(acert = PEM_read_bio_X509_ACERT(bp, NULL, NULL, NULL)))
  51. goto err;
  52. if (!TEST_int_gt(X509_ACERT_sign(acert, pkey, EVP_sha256()), 0) ||
  53. !TEST_int_eq(X509_ACERT_verify(acert, pkey), 1))
  54. goto err;
  55. ret = 1;
  56. err:
  57. BIO_free(bp);
  58. X509_ACERT_free(acert);
  59. EVP_PKEY_free(pkey);
  60. return ret;
  61. }
  62. /* IetfAttrSyntax structure with one value */
  63. static const unsigned char attr_syntax_single[] = {
  64. 0x30, 0x15, 0xa0, 0x09, 0x86, 0x07, 0x54, 0x65, 0x73, 0x74, 0x76, 0x61,
  65. 0x6c, 0x30, 0x08, 0x0c, 0x06, 0x67, 0x72, 0x6f, 0x75, 0x70, 0x31
  66. };
  67. /* IetfAttrSyntax structure with multiple values of the same type */
  68. static const unsigned char attr_syntax_multiple[] = {
  69. 0x30, 0x1d, 0x30, 0x1b, 0x0c, 0x07, 0x67, 0x72, 0x6f, 0x75, 0x70, 0x20,
  70. 0x31, 0x0c, 0x07, 0x67, 0x72, 0x6f, 0x75, 0x70, 0x20, 0x32, 0x0c, 0x07,
  71. 0x67, 0x72, 0x6f, 0x75, 0x70, 0x20, 0x33
  72. };
  73. /* IetfAttrSyntax structure with multiple values of different types */
  74. static const unsigned char attr_syntax_diff_type[] = {
  75. 0x30, 0x11, 0x30, 0x0f, 0x04, 0x08, 0x64, 0x65, 0x61, 0x64, 0x63, 0x6f,
  76. 0x64, 0x65, 0x0c, 0x03, 0x61, 0x61, 0x61
  77. };
  78. /* IetfAttrSyntax structure with an invalid/unsupported value type */
  79. static const unsigned char attr_syntax_invalid_type[] = {
  80. 0x30, 0x05, 0x30, 0x03, 0x02, 0x01, 0x0a
  81. };
  82. #define ADD_TEST_DATA(x, valid) {x, sizeof(x), valid}
  83. struct ietf_type_test_data {
  84. const unsigned char *data;
  85. size_t len;
  86. int valid;
  87. };
  88. static const struct ietf_type_test_data ietf_syntax_tests[] = {
  89. ADD_TEST_DATA(attr_syntax_single, 1),
  90. ADD_TEST_DATA(attr_syntax_multiple, 1),
  91. ADD_TEST_DATA(attr_syntax_diff_type, 0),
  92. ADD_TEST_DATA(attr_syntax_invalid_type, 0),
  93. };
  94. static int test_object_group_attr(int idx)
  95. {
  96. int ret = 0;
  97. OSSL_IETF_ATTR_SYNTAX *ias = NULL;
  98. BIO *bout = NULL;
  99. const unsigned char *p;
  100. const struct ietf_type_test_data *test = &ietf_syntax_tests[idx];
  101. if (!TEST_ptr(bout = BIO_new_fp(stderr, BIO_NOCLOSE)))
  102. goto done;
  103. p = test->data;
  104. ias = d2i_OSSL_IETF_ATTR_SYNTAX(NULL, &p, test->len);
  105. if ((test->valid && !TEST_ptr(ias))
  106. || (!test->valid && !TEST_ptr_null(ias)))
  107. goto done;
  108. if (ias != NULL
  109. && !TEST_int_eq(OSSL_IETF_ATTR_SYNTAX_print(bout, ias, 4), 1)) {
  110. OSSL_IETF_ATTR_SYNTAX_free(ias);
  111. goto done;
  112. }
  113. ret = 1;
  114. done:
  115. OSSL_IETF_ATTR_SYNTAX_free(ias);
  116. BIO_free(bout);
  117. return ret;
  118. }
  119. OPT_TEST_DECLARE_USAGE("[<attribute certs (PEM)>...]\n")
  120. int setup_tests(void)
  121. {
  122. int cnt;
  123. if (!test_skip_common_options()) {
  124. TEST_error("Error parsing test options\n");
  125. return 0;
  126. }
  127. cnt = test_get_argument_count();
  128. if (cnt < 1) {
  129. TEST_error("Must specify at least 1 attribute certificate file\n");
  130. return 0;
  131. }
  132. ADD_ALL_TESTS(test_print_acert, cnt);
  133. ADD_TEST(test_acert_sign);
  134. ADD_ALL_TESTS(test_object_group_attr, OSSL_NELEM(ietf_syntax_tests));
  135. return 1;
  136. }