2 * Copyright (C) 2015 Red Hat, Inc.
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.
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.
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/>.
18 * Author: Daniel P. Berrange <berrange@redhat.com>
21 #include "qemu/osdep.h"
23 #include "crypto-tls-x509-helpers.h"
24 #include "crypto/init.h"
25 #include "qemu/sockets.h"
28 * This stores some static data that is needed when
29 * encoding extensions in the x509 certs
34 * To avoid consuming random entropy to generate keys,
35 * here's one we prepared earlier :-)
37 gnutls_x509_privkey_t privkey
;
38 # define PRIVATE_KEY \
39 "-----BEGIN RSA PRIVATE KEY-----\n" \
40 "MIIG5AIBAAKCAYEAyjWyLSNm5PZvYUKUcDWGqbLX10b2ood+YaFjWSnJrqx/q3qh\n" \
41 "rVGBJglD25AJENJsmZF3zPP1oMhfIxsXu63Hdkb6Rdlc2RUoUP34x9VC1izH25mR\n" \
42 "6c8DPDp1d6IraZ/llDMI1HsBFz0qGWtvOHgm815XG4PAr/N8rDsuqfv/cJ01KlnO\n" \
43 "0OdO5QRXCJf9g/dYd41MPu7wOXk9FqjQlmRoP59HgtJ+zUpE4z+Keruw9cMT9VJj\n" \
44 "0oT+pQ9ysenqeZ3gbT224T1khrEhT5kifhtFLNyDssRchUUWH0hiqoOO1vgb+850\n" \
45 "W6/1VdxvuPam48py4diSPi1Vip8NITCOBaX9FIpVp4Ruw4rTPVMNMjq9Cpx/DwMP\n" \
46 "9MbfXfnaVaZaMrmq67/zPhl0eVbUrecH2hQ3ZB9oIF4GkNskzlWF5+yPy6zqk304\n" \
47 "AKaiFR6jRyh3YfHo2XFqV8x/hxdsIEX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 * This loads the private key we defined earlier
82 static gnutls_x509_privkey_t
test_tls_load_key(void)
84 gnutls_x509_privkey_t key
;
85 const gnutls_datum_t data
= { (unsigned char *)PRIVATE_KEY
,
86 strlen(PRIVATE_KEY
) };
89 err
= gnutls_x509_privkey_init(&key
);
91 g_critical("Failed to init key %s", gnutls_strerror(err
));
95 err
= gnutls_x509_privkey_import(key
, &data
,
98 if (err
!= GNUTLS_E_BASE64_UNEXPECTED_HEADER_ERROR
&&
99 err
!= GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE
) {
100 g_critical("Failed to import key %s", gnutls_strerror(err
));
104 err
= gnutls_x509_privkey_import_pkcs8(
105 key
, &data
, GNUTLS_X509_FMT_PEM
, NULL
, 0);
107 g_critical("Failed to import PKCS8 key %s", gnutls_strerror(err
));
116 void test_tls_init(const char *keyfile
)
118 qcrypto_init(&error_abort
);
120 if (asn1_array2tree(pkix_asn1_tab
, &pkix_asn1
, NULL
) != ASN1_SUCCESS
) {
124 privkey
= test_tls_load_key();
125 if (!g_file_set_contents(keyfile
, PRIVATE_KEY
, -1, NULL
)) {
131 void test_tls_cleanup(const char *keyfile
)
133 asn1_delete_structure(&pkix_asn1
);
138 * Turns an ASN1 object into a DER encoded byte array
140 static void test_tls_der_encode(asn1_node src
,
141 const char *src_name
,
148 asn1_der_coding(src
, src_name
, NULL
, &size
, NULL
);
150 data
= g_new0(char, size
);
152 asn1_der_coding(src
, src_name
, data
, &size
, NULL
);
154 res
->data
= (unsigned char *)data
;
160 test_tls_get_ipaddr(const char *addrstr
,
164 struct addrinfo
*res
;
165 struct addrinfo hints
;
167 memset(&hints
, 0, sizeof(hints
));
168 hints
.ai_flags
= AI_NUMERICHOST
;
169 g_assert(getaddrinfo(addrstr
, NULL
, &hints
, &res
) == 0);
171 if (res
->ai_family
== AF_INET
) {
172 struct sockaddr_in
*in
= (struct sockaddr_in
*)res
->ai_addr
;
173 *datalen
= sizeof(in
->sin_addr
);
174 *data
= g_new(char, *datalen
);
175 memcpy(*data
, &in
->sin_addr
, *datalen
);
176 } else if (res
->ai_family
== AF_INET6
) {
177 struct sockaddr_in6
*in
= (struct sockaddr_in6
*)res
->ai_addr
;
178 *datalen
= sizeof(in
->sin6_addr
);
179 *data
= g_new(char, *datalen
);
180 memcpy(*data
, &in
->sin6_addr
, *datalen
);
182 g_assert_not_reached();
188 * This is a fairly lame x509 certificate generator.
190 * Do not copy/use this code for generating real certificates
191 * since it leaves out many things that you would want in
192 * certificates for real world usage.
194 * This is good enough only for doing tests of the QEMU
195 * TLS certificate code
198 test_tls_generate_cert(QCryptoTLSTestCertReq
*req
,
199 gnutls_x509_crt_t ca
)
201 gnutls_x509_crt_t crt
;
203 static char buffer
[1024 * 1024];
204 size_t size
= sizeof(buffer
);
205 char serial
[5] = { 1, 2, 3, 4, 0 };
207 time_t start
= time(NULL
) + (60 * 60 * req
->start_offset
);
208 time_t expire
= time(NULL
) + (60 * 60 * (req
->expire_offset
209 ? req
->expire_offset
: 24));
212 * Prepare our new certificate object
214 err
= gnutls_x509_crt_init(&crt
);
216 g_critical("Failed to initialize certificate %s", gnutls_strerror(err
));
219 err
= gnutls_x509_crt_set_key(crt
, privkey
);
221 g_critical("Failed to set certificate key %s", gnutls_strerror(err
));
226 * A v3 certificate is required in order to be able
227 * set any of the basic constraints, key purpose and
230 gnutls_x509_crt_set_version(crt
, 3);
233 err
= gnutls_x509_crt_set_dn_by_oid(
234 crt
, GNUTLS_OID_X520_COUNTRY_NAME
, 0,
235 req
->country
, strlen(req
->country
));
237 g_critical("Failed to set certificate country name %s",
238 gnutls_strerror(err
));
243 err
= gnutls_x509_crt_set_dn_by_oid(
244 crt
, GNUTLS_OID_X520_COMMON_NAME
, 0,
245 req
->cn
, strlen(req
->cn
));
247 g_critical("Failed to set certificate common name %s",
248 gnutls_strerror(err
));
254 * Setup the subject altnames, which are used
255 * for hostname checks in live sessions
258 err
= gnutls_x509_crt_set_subject_alt_name(
259 crt
, GNUTLS_SAN_DNSNAME
,
261 strlen(req
->altname1
),
264 g_critical("Failed to set certificate alt name %s",
265 gnutls_strerror(err
));
270 err
= gnutls_x509_crt_set_subject_alt_name(
271 crt
, GNUTLS_SAN_DNSNAME
,
273 strlen(req
->altname2
),
276 g_critical("Failed to set certificate %s alt name",
277 gnutls_strerror(err
));
283 * IP address need to be put into the cert in their
284 * raw byte form, not strings, hence this is a little
291 test_tls_get_ipaddr(req
->ipaddr1
, &data
, &len
);
293 err
= gnutls_x509_crt_set_subject_alt_name(
294 crt
, GNUTLS_SAN_IPADDRESS
,
295 data
, len
, GNUTLS_FSAN_APPEND
);
297 g_critical("Failed to set certificate alt name %s",
298 gnutls_strerror(err
));
307 test_tls_get_ipaddr(req
->ipaddr2
, &data
, &len
);
309 err
= gnutls_x509_crt_set_subject_alt_name(
310 crt
, GNUTLS_SAN_IPADDRESS
,
311 data
, len
, GNUTLS_FSAN_APPEND
);
313 g_critical("Failed to set certificate alt name %s",
314 gnutls_strerror(err
));
322 * Basic constraints are used to decide if the cert
323 * is for a CA or not. We can't use the convenient
324 * gnutls API for setting this, since it hardcodes
325 * the 'critical' field which we want control over
327 if (req
->basicConstraintsEnable
) {
328 asn1_node ext
= NULL
;
330 asn1_create_element(pkix_asn1
, "PKIX1.BasicConstraints", &ext
);
331 asn1_write_value(ext
, "cA",
332 req
->basicConstraintsIsCA
? "TRUE" : "FALSE", 1);
333 asn1_write_value(ext
, "pathLenConstraint", NULL
, 0);
334 test_tls_der_encode(ext
, "", &der
);
335 err
= gnutls_x509_crt_set_extension_by_oid(
338 req
->basicConstraintsCritical
);
340 g_critical("Failed to set certificate basic constraints %s",
341 gnutls_strerror(err
));
345 asn1_delete_structure(&ext
);
350 * Next up the key usage extension. Again we can't
351 * use the gnutls API since it hardcodes the extension
354 if (req
->keyUsageEnable
) {
355 asn1_node ext
= NULL
;
358 str
[0] = req
->keyUsageValue
& 0xff;
359 str
[1] = (req
->keyUsageValue
>> 8) & 0xff;
361 asn1_create_element(pkix_asn1
, "PKIX1.KeyUsage", &ext
);
362 asn1_write_value(ext
, "", str
, 9);
363 test_tls_der_encode(ext
, "", &der
);
364 err
= gnutls_x509_crt_set_extension_by_oid(
367 req
->keyUsageCritical
);
369 g_critical("Failed to set certificate key usage %s",
370 gnutls_strerror(err
));
374 asn1_delete_structure(&ext
);
379 * Finally the key purpose extension. This time
380 * gnutls has the opposite problem, always hardcoding
381 * it to be non-critical. So once again we have to
382 * set this the hard way building up ASN1 data ourselves
384 if (req
->keyPurposeEnable
) {
385 asn1_node ext
= NULL
;
387 asn1_create_element(pkix_asn1
, "PKIX1.ExtKeyUsageSyntax", &ext
);
388 if (req
->keyPurposeOID1
) {
389 asn1_write_value(ext
, "", "NEW", 1);
390 asn1_write_value(ext
, "?LAST", req
->keyPurposeOID1
, 1);
392 if (req
->keyPurposeOID2
) {
393 asn1_write_value(ext
, "", "NEW", 1);
394 asn1_write_value(ext
, "?LAST", req
->keyPurposeOID2
, 1);
396 test_tls_der_encode(ext
, "", &der
);
397 err
= gnutls_x509_crt_set_extension_by_oid(
400 req
->keyPurposeCritical
);
402 g_critical("Failed to set certificate key purpose %s",
403 gnutls_strerror(err
));
407 asn1_delete_structure(&ext
);
412 * Any old serial number will do, so lets pick 5
414 err
= gnutls_x509_crt_set_serial(crt
, serial
, 5);
416 g_critical("Failed to set certificate serial %s",
417 gnutls_strerror(err
));
421 err
= gnutls_x509_crt_set_activation_time(crt
, start
);
423 g_critical("Failed to set certificate activation %s",
424 gnutls_strerror(err
));
427 err
= gnutls_x509_crt_set_expiration_time(crt
, expire
);
429 g_critical("Failed to set certificate expiration %s",
430 gnutls_strerror(err
));
436 * If no 'ca' is set then we are self signing
437 * the cert. This is done for the root CA certs
439 err
= gnutls_x509_crt_sign2(crt
, ca
? ca
: crt
, privkey
,
440 GNUTLS_DIG_SHA256
, 0);
442 g_critical("Failed to sign certificate %s",
443 gnutls_strerror(err
));
448 * Finally write the new cert out to disk
450 err
= gnutls_x509_crt_export(
451 crt
, GNUTLS_X509_FMT_PEM
, buffer
, &size
);
453 g_critical("Failed to export certificate %s: %d",
454 gnutls_strerror(err
), err
);
458 if (!g_file_set_contents(req
->filename
, buffer
, -1, NULL
)) {
459 g_critical("Failed to write certificate %s",
468 void test_tls_write_cert_chain(const char *filename
,
469 gnutls_x509_crt_t
*certs
,
473 size_t capacity
= 1024, offset
= 0;
474 char *buffer
= g_new0(char, capacity
);
477 for (i
= 0; i
< ncerts
; i
++) {
478 size_t len
= capacity
- offset
;
480 err
= gnutls_x509_crt_export(certs
[i
], GNUTLS_X509_FMT_PEM
,
481 buffer
+ offset
, &len
);
483 if (err
== GNUTLS_E_SHORT_MEMORY_BUFFER
) {
484 buffer
= g_renew(char, buffer
, offset
+ len
);
485 capacity
= offset
+ len
;
488 g_critical("Failed to export certificate chain %s: %d",
489 gnutls_strerror(err
), err
);
495 if (!g_file_set_contents(filename
, buffer
, offset
, NULL
)) {
502 void test_tls_discard_cert(QCryptoTLSTestCertReq
*req
)
508 gnutls_x509_crt_deinit(req
->crt
);
511 if (getenv("QEMU_TEST_DEBUG_CERTS") == NULL
) {
512 unlink(req
->filename
);