crypto-tls-x509-helpers.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * Copyright (C) 2015 Red Hat, Inc.
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library. If not, see
  16. * <http://www.gnu.org/licenses/>.
  17. *
  18. * Author: Daniel P. Berrange <berrange@redhat.com>
  19. */
  20. #ifndef TESTS_CRYPTO_TLS_X509_HELPERS_H
  21. #define TESTS_CRYPTO_TLS_X509_HELPERS_H
  22. #include <gnutls/gnutls.h>
  23. #include <gnutls/x509.h>
  24. #if !(defined WIN32) && \
  25. defined(CONFIG_TASN1)
  26. # define QCRYPTO_HAVE_TLS_TEST_SUPPORT
  27. #endif
  28. #ifdef QCRYPTO_HAVE_TLS_TEST_SUPPORT
  29. # include <libtasn1.h>
  30. /*
  31. * This contains parameter about how to generate
  32. * certificates.
  33. */
  34. typedef struct QCryptoTLSTestCertReq QCryptoTLSTestCertReq;
  35. struct QCryptoTLSTestCertReq {
  36. gnutls_x509_crt_t crt;
  37. const char *filename;
  38. /* Identifying information */
  39. const char *country;
  40. const char *cn;
  41. const char *altname1;
  42. const char *altname2;
  43. const char *ipaddr1;
  44. const char *ipaddr2;
  45. /* Basic constraints */
  46. bool basicConstraintsEnable;
  47. bool basicConstraintsCritical;
  48. bool basicConstraintsIsCA;
  49. /* Key usage */
  50. bool keyUsageEnable;
  51. bool keyUsageCritical;
  52. int keyUsageValue;
  53. /* Key purpose (aka Extended key usage) */
  54. bool keyPurposeEnable;
  55. bool keyPurposeCritical;
  56. const char *keyPurposeOID1;
  57. const char *keyPurposeOID2;
  58. /* zero for current time, or non-zero for hours from now */
  59. int start_offset;
  60. /* zero for 24 hours from now, or non-zero for hours from now */
  61. int expire_offset;
  62. };
  63. void test_tls_generate_cert(QCryptoTLSTestCertReq *req,
  64. gnutls_x509_crt_t ca);
  65. void test_tls_write_cert_chain(const char *filename,
  66. gnutls_x509_crt_t *certs,
  67. size_t ncerts);
  68. void test_tls_discard_cert(QCryptoTLSTestCertReq *req);
  69. void test_tls_init(const char *keyfile);
  70. void test_tls_cleanup(const char *keyfile);
  71. # define TLS_CERT_REQ(varname, cavarname, \
  72. country, commonname, \
  73. altname1, altname2, \
  74. ipaddr1, ipaddr2, \
  75. basicconsenable, basicconscritical, basicconsca, \
  76. keyusageenable, keyusagecritical, keyusagevalue, \
  77. keypurposeenable, keypurposecritical, \
  78. keypurposeoid1, keypurposeoid2, \
  79. startoffset, endoffset) \
  80. static QCryptoTLSTestCertReq varname = { \
  81. NULL, WORKDIR #varname "-ctx.pem", \
  82. country, commonname, altname1, altname2, \
  83. ipaddr1, ipaddr2, \
  84. basicconsenable, basicconscritical, basicconsca, \
  85. keyusageenable, keyusagecritical, keyusagevalue, \
  86. keypurposeenable, keypurposecritical, \
  87. keypurposeoid1, keypurposeoid2, \
  88. startoffset, endoffset \
  89. }; \
  90. test_tls_generate_cert(&varname, cavarname.crt)
  91. # define TLS_ROOT_REQ(varname, \
  92. country, commonname, \
  93. altname1, altname2, \
  94. ipaddr1, ipaddr2, \
  95. basicconsenable, basicconscritical, basicconsca, \
  96. keyusageenable, keyusagecritical, keyusagevalue, \
  97. keypurposeenable, keypurposecritical, \
  98. keypurposeoid1, keypurposeoid2, \
  99. startoffset, endoffset) \
  100. static QCryptoTLSTestCertReq varname = { \
  101. NULL, WORKDIR #varname "-ctx.pem", \
  102. country, commonname, altname1, altname2, \
  103. ipaddr1, ipaddr2, \
  104. basicconsenable, basicconscritical, basicconsca, \
  105. keyusageenable, keyusagecritical, keyusagevalue, \
  106. keypurposeenable, keypurposecritical, \
  107. keypurposeoid1, keypurposeoid2, \
  108. startoffset, endoffset \
  109. }; \
  110. test_tls_generate_cert(&varname, NULL)
  111. extern const ASN1_ARRAY_TYPE pkix_asn1_tab[];
  112. #endif /* QCRYPTO_HAVE_TLS_TEST_SUPPORT */
  113. #endif