2 * QEMU crypto TLS x509 credential support
4 * Copyright (c) 2015 Red Hat, Inc.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
21 #include "qemu/osdep.h"
22 #include "crypto/tlscredsx509.h"
23 #include "crypto/tlscredspriv.h"
24 #include "crypto/secret.h"
25 #include "qom/object_interfaces.h"
31 #include <gnutls/x509.h>
35 qcrypto_tls_creds_check_cert_times(gnutls_x509_crt_t cert,
41 time_t now = time(NULL);
43 if (now == ((time_t)-1)) {
44 error_setg_errno(errp, errno, "cannot get current time");
48 if (gnutls_x509_crt_get_expiration_time(cert) < now) {
51 "The CA certificate %s has expired" :
53 "The server certificate %s has expired" :
54 "The client certificate %s has expired")),
59 if (gnutls_x509_crt_get_activation_time(cert) > now) {
62 "The CA certificate %s is not yet active" :
64 "The server certificate %s is not yet active" :
65 "The client certificate %s is not yet active")),
74 #if LIBGNUTLS_VERSION_NUMBER >= 2
76 * The gnutls_x509_crt_get_basic_constraints function isn't
77 * available in GNUTLS 1.0.x branches. This isn't critical
78 * though, since gnutls_certificate_verify_peers2 will do
79 * pretty much the same check at runtime, so we can just
83 qcrypto_tls_creds_check_cert_basic_constraints(QCryptoTLSCredsX509 *creds,
84 gnutls_x509_crt_t cert,
92 status = gnutls_x509_crt_get_basic_constraints(cert, NULL, NULL, NULL);
93 trace_qcrypto_tls_creds_x509_check_basic_constraints(
94 creds, certFile, status);
96 if (status > 0) { /* It is a CA cert */
98 error_setg(errp, isServer ?
99 "The certificate %s basic constraints show a CA, "
100 "but we need one for a server" :
101 "The certificate %s basic constraints show a CA, "
102 "but we need one for a client",
106 } else if (status == 0) { /* It is not a CA cert */
109 "The certificate %s basic constraints do not "
114 } else if (status == GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE) {
115 /* Missing basicConstraints */
118 "The certificate %s is missing basic constraints "
123 } else { /* General error */
125 "Unable to query certificate %s basic constraints: %s",
126 certFile, gnutls_strerror(status));
136 qcrypto_tls_creds_check_cert_key_usage(QCryptoTLSCredsX509 *creds,
137 gnutls_x509_crt_t cert,
138 const char *certFile,
143 unsigned int usage = 0;
144 unsigned int critical = 0;
146 status = gnutls_x509_crt_get_key_usage(cert, &usage, &critical);
147 trace_qcrypto_tls_creds_x509_check_key_usage(
148 creds, certFile, status, usage, critical);
151 if (status == GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE) {
152 usage = isCA ? GNUTLS_KEY_KEY_CERT_SIGN :
153 GNUTLS_KEY_DIGITAL_SIGNATURE|GNUTLS_KEY_KEY_ENCIPHERMENT;
156 "Unable to query certificate %s key usage: %s",
157 certFile, gnutls_strerror(status));
163 if (!(usage & GNUTLS_KEY_KEY_CERT_SIGN)) {
166 "Certificate %s usage does not permit "
167 "certificate signing", certFile);
172 if (!(usage & GNUTLS_KEY_DIGITAL_SIGNATURE)) {
175 "Certificate %s usage does not permit digital "
176 "signature", certFile);
180 if (!(usage & GNUTLS_KEY_KEY_ENCIPHERMENT)) {
183 "Certificate %s usage does not permit key "
184 "encipherment", certFile);
195 qcrypto_tls_creds_check_cert_key_purpose(QCryptoTLSCredsX509 *creds,
196 gnutls_x509_crt_t cert,
197 const char *certFile,
203 unsigned int purposeCritical;
204 unsigned int critical;
207 bool allowClient = false, allowServer = false;
212 status = gnutls_x509_crt_get_key_purpose_oid(cert, i, buffer,
215 if (status == GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE) {
217 /* If there is no data at all, then we must allow
218 client/server to pass */
220 allowServer = allowClient = true;
224 if (status != GNUTLS_E_SHORT_MEMORY_BUFFER) {
226 "Unable to query certificate %s key purpose: %s",
227 certFile, gnutls_strerror(status));
231 buffer = g_new0(char, size);
233 status = gnutls_x509_crt_get_key_purpose_oid(cert, i, buffer,
234 &size, &purposeCritical);
237 trace_qcrypto_tls_creds_x509_check_key_purpose(
238 creds, certFile, status, "<none>", purposeCritical);
241 "Unable to query certificate %s key purpose: %s",
242 certFile, gnutls_strerror(status));
245 trace_qcrypto_tls_creds_x509_check_key_purpose(
246 creds, certFile, status, buffer, purposeCritical);
247 if (purposeCritical) {
251 if (g_str_equal(buffer, GNUTLS_KP_TLS_WWW_SERVER)) {
253 } else if (g_str_equal(buffer, GNUTLS_KP_TLS_WWW_CLIENT)) {
255 } else if (g_str_equal(buffer, GNUTLS_KP_ANY)) {
256 allowServer = allowClient = true;
267 "Certificate %s purpose does not allow "
268 "use with a TLS server", certFile);
276 "Certificate %s purpose does not allow use "
277 "with a TLS client", certFile);
288 qcrypto_tls_creds_check_cert(QCryptoTLSCredsX509 *creds,
289 gnutls_x509_crt_t cert,
290 const char *certFile,
295 if (qcrypto_tls_creds_check_cert_times(cert, certFile,
301 #if LIBGNUTLS_VERSION_NUMBER >= 2
302 if (qcrypto_tls_creds_check_cert_basic_constraints(creds,
310 if (qcrypto_tls_creds_check_cert_key_usage(creds,
317 qcrypto_tls_creds_check_cert_key_purpose(creds,
319 isServer, errp) < 0) {
328 qcrypto_tls_creds_check_cert_pair(gnutls_x509_crt_t cert,
329 const char *certFile,
330 gnutls_x509_crt_t *cacerts,
332 const char *cacertFile,
338 if (gnutls_x509_crt_list_verify(&cert, 1,
342 error_setg(errp, isServer ?
343 "Unable to verify server certificate %s against "
344 "CA certificate %s" :
345 "Unable to verify client certificate %s against "
347 certFile, cacertFile);
352 const char *reason = "Invalid certificate";
354 if (status & GNUTLS_CERT_INVALID) {
355 reason = "The certificate is not trusted";
358 if (status & GNUTLS_CERT_SIGNER_NOT_FOUND) {
359 reason = "The certificate hasn't got a known issuer";
362 if (status & GNUTLS_CERT_REVOKED) {
363 reason = "The certificate has been revoked";
366 #ifndef GNUTLS_1_0_COMPAT
367 if (status & GNUTLS_CERT_INSECURE_ALGORITHM) {
368 reason = "The certificate uses an insecure algorithm";
373 "Our own certificate %s failed validation against %s: %s",
374 certFile, cacertFile, reason);
382 static gnutls_x509_crt_t
383 qcrypto_tls_creds_load_cert(QCryptoTLSCredsX509 *creds,
384 const char *certFile,
389 gnutls_x509_crt_t cert = NULL;
395 trace_qcrypto_tls_creds_x509_load_cert(creds, isServer, certFile);
397 if (gnutls_x509_crt_init(&cert) < 0) {
398 error_setg(errp, "Unable to initialize certificate");
402 if (!g_file_get_contents(certFile, &buf, &buflen, &gerr)) {
403 error_setg(errp, "Cannot load CA cert list %s: %s",
404 certFile, gerr->message);
409 data.data = (unsigned char *)buf;
410 data.size = strlen(buf);
412 if (gnutls_x509_crt_import(cert, &data, GNUTLS_X509_FMT_PEM) < 0) {
413 error_setg(errp, isServer ?
414 "Unable to import server certificate %s" :
415 "Unable to import client certificate %s",
424 gnutls_x509_crt_deinit(cert);
433 qcrypto_tls_creds_load_ca_cert_list(QCryptoTLSCredsX509 *creds,
434 const char *certFile,
435 gnutls_x509_crt_t *certs,
436 unsigned int certMax,
447 trace_qcrypto_tls_creds_x509_load_cert_list(creds, certFile);
449 if (!g_file_get_contents(certFile, &buf, &buflen, &gerr)) {
450 error_setg(errp, "Cannot load CA cert list %s: %s",
451 certFile, gerr->message);
456 data.data = (unsigned char *)buf;
457 data.size = strlen(buf);
459 if (gnutls_x509_crt_list_import(certs, &certMax, &data,
460 GNUTLS_X509_FMT_PEM, 0) < 0) {
462 "Unable to import CA certificate list %s",
478 qcrypto_tls_creds_x509_sanity_check(QCryptoTLSCredsX509 *creds,
480 const char *cacertFile,
481 const char *certFile,
484 gnutls_x509_crt_t cert = NULL;
485 gnutls_x509_crt_t cacerts[MAX_CERTS];
490 memset(cacerts, 0, sizeof(cacerts));
492 access(certFile, R_OK) == 0) {
493 cert = qcrypto_tls_creds_load_cert(creds,
500 if (access(cacertFile, R_OK) == 0) {
501 if (qcrypto_tls_creds_load_ca_cert_list(creds,
503 MAX_CERTS, &ncacerts,
510 qcrypto_tls_creds_check_cert(creds,
511 cert, certFile, isServer,
516 for (i = 0; i < ncacerts; i++) {
517 if (qcrypto_tls_creds_check_cert(creds,
518 cacerts[i], cacertFile,
519 isServer, true, errp) < 0) {
524 if (cert && ncacerts &&
525 qcrypto_tls_creds_check_cert_pair(cert, certFile, cacerts,
526 ncacerts, cacertFile,
527 isServer, errp) < 0) {
535 gnutls_x509_crt_deinit(cert);
537 for (i = 0; i < ncacerts; i++) {
538 gnutls_x509_crt_deinit(cacerts[i]);
545 qcrypto_tls_creds_x509_load(QCryptoTLSCredsX509 *creds,
548 char *cacert = NULL, *cacrl = NULL, *cert = NULL,
549 *key = NULL, *dhparams = NULL;
553 trace_qcrypto_tls_creds_x509_load(creds,
554 creds->parent_obj.dir ? creds->parent_obj.dir : "<nodir>");
556 if (creds->parent_obj.endpoint == QCRYPTO_TLS_CREDS_ENDPOINT_SERVER) {
557 if (qcrypto_tls_creds_get_path(&creds->parent_obj,
558 QCRYPTO_TLS_CREDS_X509_CA_CERT,
559 true, &cacert, errp) < 0 ||
560 qcrypto_tls_creds_get_path(&creds->parent_obj,
561 QCRYPTO_TLS_CREDS_X509_CA_CRL,
562 false, &cacrl, errp) < 0 ||
563 qcrypto_tls_creds_get_path(&creds->parent_obj,
564 QCRYPTO_TLS_CREDS_X509_SERVER_CERT,
565 true, &cert, errp) < 0 ||
566 qcrypto_tls_creds_get_path(&creds->parent_obj,
567 QCRYPTO_TLS_CREDS_X509_SERVER_KEY,
568 true, &key, errp) < 0 ||
569 qcrypto_tls_creds_get_path(&creds->parent_obj,
570 QCRYPTO_TLS_CREDS_DH_PARAMS,
571 false, &dhparams, errp) < 0) {
575 if (qcrypto_tls_creds_get_path(&creds->parent_obj,
576 QCRYPTO_TLS_CREDS_X509_CA_CERT,
577 true, &cacert, errp) < 0 ||
578 qcrypto_tls_creds_get_path(&creds->parent_obj,
579 QCRYPTO_TLS_CREDS_X509_CLIENT_CERT,
580 false, &cert, errp) < 0 ||
581 qcrypto_tls_creds_get_path(&creds->parent_obj,
582 QCRYPTO_TLS_CREDS_X509_CLIENT_KEY,
583 false, &key, errp) < 0) {
588 if (creds->sanityCheck &&
589 qcrypto_tls_creds_x509_sanity_check(creds,
590 creds->parent_obj.endpoint == QCRYPTO_TLS_CREDS_ENDPOINT_SERVER,
591 cacert, cert, errp) < 0) {
595 ret = gnutls_certificate_allocate_credentials(&creds->data);
597 error_setg(errp, "Cannot allocate credentials: '%s'",
598 gnutls_strerror(ret));
602 ret = gnutls_certificate_set_x509_trust_file(creds->data,
604 GNUTLS_X509_FMT_PEM);
606 error_setg(errp, "Cannot load CA certificate '%s': %s",
607 cacert, gnutls_strerror(ret));
611 if (cert != NULL && key != NULL) {
612 #if GNUTLS_VERSION_NUMBER >= 0x030111
613 char *password = NULL;
614 if (creds->passwordid) {
615 password = qcrypto_secret_lookup_as_utf8(creds->passwordid,
621 ret = gnutls_certificate_set_x509_key_file2(creds->data,
627 #else /* GNUTLS_VERSION_NUMBER < 0x030111 */
628 if (creds->passwordid) {
629 error_setg(errp, "PKCS8 decryption requires GNUTLS >= 3.1.11");
632 ret = gnutls_certificate_set_x509_key_file(creds->data,
634 GNUTLS_X509_FMT_PEM);
635 #endif /* GNUTLS_VERSION_NUMBER < 0x030111 */
637 error_setg(errp, "Cannot load certificate '%s' & key '%s': %s",
638 cert, key, gnutls_strerror(ret));
644 ret = gnutls_certificate_set_x509_crl_file(creds->data,
646 GNUTLS_X509_FMT_PEM);
648 error_setg(errp, "Cannot load CRL '%s': %s",
649 cacrl, gnutls_strerror(ret));
654 if (creds->parent_obj.endpoint == QCRYPTO_TLS_CREDS_ENDPOINT_SERVER) {
655 if (qcrypto_tls_creds_get_dh_params_file(&creds->parent_obj, dhparams,
656 &creds->parent_obj.dh_params,
660 gnutls_certificate_set_dh_params(creds->data,
661 creds->parent_obj.dh_params);
676 qcrypto_tls_creds_x509_unload(QCryptoTLSCredsX509 *creds)
679 gnutls_certificate_free_credentials(creds->data);
682 if (creds->parent_obj.dh_params) {
683 gnutls_dh_params_deinit(creds->parent_obj.dh_params);
684 creds->parent_obj.dh_params = NULL;
689 #else /* ! CONFIG_GNUTLS */
693 qcrypto_tls_creds_x509_load(QCryptoTLSCredsX509 *creds G_GNUC_UNUSED,
696 error_setg(errp, "TLS credentials support requires GNUTLS");
701 qcrypto_tls_creds_x509_unload(QCryptoTLSCredsX509 *creds G_GNUC_UNUSED)
707 #endif /* ! CONFIG_GNUTLS */
711 qcrypto_tls_creds_x509_prop_set_loaded(Object *obj,
715 QCryptoTLSCredsX509 *creds = QCRYPTO_TLS_CREDS_X509(obj);
718 qcrypto_tls_creds_x509_load(creds, errp);
720 qcrypto_tls_creds_x509_unload(creds);
729 qcrypto_tls_creds_x509_prop_get_loaded(Object *obj,
730 Error **errp G_GNUC_UNUSED)
732 QCryptoTLSCredsX509 *creds = QCRYPTO_TLS_CREDS_X509(obj);
734 return creds->data != NULL;
738 #else /* ! CONFIG_GNUTLS */
742 qcrypto_tls_creds_x509_prop_get_loaded(Object *obj G_GNUC_UNUSED,
743 Error **errp G_GNUC_UNUSED)
749 #endif /* ! CONFIG_GNUTLS */
753 qcrypto_tls_creds_x509_prop_set_sanity(Object *obj,
755 Error **errp G_GNUC_UNUSED)
757 QCryptoTLSCredsX509 *creds = QCRYPTO_TLS_CREDS_X509(obj);
759 creds->sanityCheck = value;
764 qcrypto_tls_creds_x509_prop_set_passwordid(Object *obj,
766 Error **errp G_GNUC_UNUSED)
768 QCryptoTLSCredsX509 *creds = QCRYPTO_TLS_CREDS_X509(obj);
770 creds->passwordid = g_strdup(value);
775 qcrypto_tls_creds_x509_prop_get_passwordid(Object *obj,
776 Error **errp G_GNUC_UNUSED)
778 QCryptoTLSCredsX509 *creds = QCRYPTO_TLS_CREDS_X509(obj);
780 return g_strdup(creds->passwordid);
785 qcrypto_tls_creds_x509_prop_get_sanity(Object *obj,
786 Error **errp G_GNUC_UNUSED)
788 QCryptoTLSCredsX509 *creds = QCRYPTO_TLS_CREDS_X509(obj);
790 return creds->sanityCheck;
795 qcrypto_tls_creds_x509_complete(UserCreatable *uc, Error **errp)
797 object_property_set_bool(OBJECT(uc), true, "loaded", errp);
802 qcrypto_tls_creds_x509_init(Object *obj)
804 QCryptoTLSCredsX509 *creds = QCRYPTO_TLS_CREDS_X509(obj);
806 creds->sanityCheck = true;
811 qcrypto_tls_creds_x509_finalize(Object *obj)
813 QCryptoTLSCredsX509 *creds = QCRYPTO_TLS_CREDS_X509(obj);
815 g_free(creds->passwordid);
816 qcrypto_tls_creds_x509_unload(creds);
821 qcrypto_tls_creds_x509_class_init(ObjectClass *oc, void *data)
823 UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc);
825 ucc->complete = qcrypto_tls_creds_x509_complete;
827 object_class_property_add_bool(oc, "loaded",
828 qcrypto_tls_creds_x509_prop_get_loaded,
829 qcrypto_tls_creds_x509_prop_set_loaded,
831 object_class_property_add_bool(oc, "sanity-check",
832 qcrypto_tls_creds_x509_prop_get_sanity,
833 qcrypto_tls_creds_x509_prop_set_sanity,
835 object_class_property_add_str(oc, "passwordid",
836 qcrypto_tls_creds_x509_prop_get_passwordid,
837 qcrypto_tls_creds_x509_prop_set_passwordid,
842 static const TypeInfo qcrypto_tls_creds_x509_info = {
843 .parent = TYPE_QCRYPTO_TLS_CREDS,
844 .name = TYPE_QCRYPTO_TLS_CREDS_X509,
845 .instance_size = sizeof(QCryptoTLSCredsX509),
846 .instance_init = qcrypto_tls_creds_x509_init,
847 .instance_finalize = qcrypto_tls_creds_x509_finalize,
848 .class_size = sizeof(QCryptoTLSCredsX509Class),
849 .class_init = qcrypto_tls_creds_x509_class_init,
850 .interfaces = (InterfaceInfo[]) {
851 { TYPE_USER_CREATABLE },
858 qcrypto_tls_creds_x509_register_types(void)
860 type_register_static(&qcrypto_tls_creds_x509_info);
864 type_init(qcrypto_tls_creds_x509_register_types);