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