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 "qapi/error.h"
26 #include "qom/object_interfaces.h"
32 #include <gnutls/x509.h>
36 qcrypto_tls_creds_check_cert_times(gnutls_x509_crt_t cert
,
42 time_t now
= time(NULL
);
44 if (now
== ((time_t)-1)) {
45 error_setg_errno(errp
, errno
, "cannot get current time");
49 if (gnutls_x509_crt_get_expiration_time(cert
) < now
) {
52 "The CA certificate %s has expired" :
54 "The server certificate %s has expired" :
55 "The client certificate %s has expired")),
60 if (gnutls_x509_crt_get_activation_time(cert
) > now
) {
63 "The CA certificate %s is not yet active" :
65 "The server certificate %s is not yet active" :
66 "The client certificate %s is not yet active")),
75 #if LIBGNUTLS_VERSION_NUMBER >= 2
77 * The gnutls_x509_crt_get_basic_constraints function isn't
78 * available in GNUTLS 1.0.x branches. This isn't critical
79 * though, since gnutls_certificate_verify_peers2 will do
80 * pretty much the same check at runtime, so we can just
84 qcrypto_tls_creds_check_cert_basic_constraints(QCryptoTLSCredsX509
*creds
,
85 gnutls_x509_crt_t cert
,
93 status
= gnutls_x509_crt_get_basic_constraints(cert
, NULL
, NULL
, NULL
);
94 trace_qcrypto_tls_creds_x509_check_basic_constraints(
95 creds
, certFile
, status
);
97 if (status
> 0) { /* It is a CA cert */
99 error_setg(errp
, isServer
?
100 "The certificate %s basic constraints show a CA, "
101 "but we need one for a server" :
102 "The certificate %s basic constraints show a CA, "
103 "but we need one for a client",
107 } else if (status
== 0) { /* It is not a CA cert */
110 "The certificate %s basic constraints do not "
115 } else if (status
== GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE
) {
116 /* Missing basicConstraints */
119 "The certificate %s is missing basic constraints "
124 } else { /* General error */
126 "Unable to query certificate %s basic constraints: %s",
127 certFile
, gnutls_strerror(status
));
137 qcrypto_tls_creds_check_cert_key_usage(QCryptoTLSCredsX509
*creds
,
138 gnutls_x509_crt_t cert
,
139 const char *certFile
,
144 unsigned int usage
= 0;
145 unsigned int critical
= 0;
147 status
= gnutls_x509_crt_get_key_usage(cert
, &usage
, &critical
);
148 trace_qcrypto_tls_creds_x509_check_key_usage(
149 creds
, certFile
, status
, usage
, critical
);
152 if (status
== GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE
) {
153 usage
= isCA
? GNUTLS_KEY_KEY_CERT_SIGN
:
154 GNUTLS_KEY_DIGITAL_SIGNATURE
|GNUTLS_KEY_KEY_ENCIPHERMENT
;
157 "Unable to query certificate %s key usage: %s",
158 certFile
, gnutls_strerror(status
));
164 if (!(usage
& GNUTLS_KEY_KEY_CERT_SIGN
)) {
167 "Certificate %s usage does not permit "
168 "certificate signing", certFile
);
173 if (!(usage
& GNUTLS_KEY_DIGITAL_SIGNATURE
)) {
176 "Certificate %s usage does not permit digital "
177 "signature", certFile
);
181 if (!(usage
& GNUTLS_KEY_KEY_ENCIPHERMENT
)) {
184 "Certificate %s usage does not permit key "
185 "encipherment", certFile
);
196 qcrypto_tls_creds_check_cert_key_purpose(QCryptoTLSCredsX509
*creds
,
197 gnutls_x509_crt_t cert
,
198 const char *certFile
,
204 unsigned int purposeCritical
;
205 unsigned int critical
;
208 bool allowClient
= false, allowServer
= false;
213 status
= gnutls_x509_crt_get_key_purpose_oid(cert
, i
, buffer
,
216 if (status
== GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE
) {
218 /* If there is no data at all, then we must allow
219 client/server to pass */
221 allowServer
= allowClient
= true;
225 if (status
!= GNUTLS_E_SHORT_MEMORY_BUFFER
) {
227 "Unable to query certificate %s key purpose: %s",
228 certFile
, gnutls_strerror(status
));
232 buffer
= g_new0(char, size
);
234 status
= gnutls_x509_crt_get_key_purpose_oid(cert
, i
, buffer
,
235 &size
, &purposeCritical
);
238 trace_qcrypto_tls_creds_x509_check_key_purpose(
239 creds
, certFile
, status
, "<none>", purposeCritical
);
242 "Unable to query certificate %s key purpose: %s",
243 certFile
, gnutls_strerror(status
));
246 trace_qcrypto_tls_creds_x509_check_key_purpose(
247 creds
, certFile
, status
, buffer
, purposeCritical
);
248 if (purposeCritical
) {
252 if (g_str_equal(buffer
, GNUTLS_KP_TLS_WWW_SERVER
)) {
254 } else if (g_str_equal(buffer
, GNUTLS_KP_TLS_WWW_CLIENT
)) {
256 } else if (g_str_equal(buffer
, GNUTLS_KP_ANY
)) {
257 allowServer
= allowClient
= true;
268 "Certificate %s purpose does not allow "
269 "use with a TLS server", certFile
);
277 "Certificate %s purpose does not allow use "
278 "with a TLS client", certFile
);
289 qcrypto_tls_creds_check_cert(QCryptoTLSCredsX509
*creds
,
290 gnutls_x509_crt_t cert
,
291 const char *certFile
,
296 if (qcrypto_tls_creds_check_cert_times(cert
, certFile
,
302 #if LIBGNUTLS_VERSION_NUMBER >= 2
303 if (qcrypto_tls_creds_check_cert_basic_constraints(creds
,
311 if (qcrypto_tls_creds_check_cert_key_usage(creds
,
318 qcrypto_tls_creds_check_cert_key_purpose(creds
,
320 isServer
, errp
) < 0) {
329 qcrypto_tls_creds_check_cert_pair(gnutls_x509_crt_t cert
,
330 const char *certFile
,
331 gnutls_x509_crt_t
*cacerts
,
333 const char *cacertFile
,
339 if (gnutls_x509_crt_list_verify(&cert
, 1,
343 error_setg(errp
, isServer
?
344 "Unable to verify server certificate %s against "
345 "CA certificate %s" :
346 "Unable to verify client certificate %s against "
348 certFile
, cacertFile
);
353 const char *reason
= "Invalid certificate";
355 if (status
& GNUTLS_CERT_INVALID
) {
356 reason
= "The certificate is not trusted";
359 if (status
& GNUTLS_CERT_SIGNER_NOT_FOUND
) {
360 reason
= "The certificate hasn't got a known issuer";
363 if (status
& GNUTLS_CERT_REVOKED
) {
364 reason
= "The certificate has been revoked";
367 #ifndef GNUTLS_1_0_COMPAT
368 if (status
& GNUTLS_CERT_INSECURE_ALGORITHM
) {
369 reason
= "The certificate uses an insecure algorithm";
374 "Our own certificate %s failed validation against %s: %s",
375 certFile
, cacertFile
, reason
);
383 static gnutls_x509_crt_t
384 qcrypto_tls_creds_load_cert(QCryptoTLSCredsX509
*creds
,
385 const char *certFile
,
390 gnutls_x509_crt_t cert
= NULL
;
396 trace_qcrypto_tls_creds_x509_load_cert(creds
, isServer
, certFile
);
398 if (gnutls_x509_crt_init(&cert
) < 0) {
399 error_setg(errp
, "Unable to initialize certificate");
403 if (!g_file_get_contents(certFile
, &buf
, &buflen
, &gerr
)) {
404 error_setg(errp
, "Cannot load CA cert list %s: %s",
405 certFile
, gerr
->message
);
410 data
.data
= (unsigned char *)buf
;
411 data
.size
= strlen(buf
);
413 if (gnutls_x509_crt_import(cert
, &data
, GNUTLS_X509_FMT_PEM
) < 0) {
414 error_setg(errp
, isServer
?
415 "Unable to import server certificate %s" :
416 "Unable to import client certificate %s",
425 gnutls_x509_crt_deinit(cert
);
434 qcrypto_tls_creds_load_ca_cert_list(QCryptoTLSCredsX509
*creds
,
435 const char *certFile
,
436 gnutls_x509_crt_t
*certs
,
437 unsigned int certMax
,
448 trace_qcrypto_tls_creds_x509_load_cert_list(creds
, certFile
);
450 if (!g_file_get_contents(certFile
, &buf
, &buflen
, &gerr
)) {
451 error_setg(errp
, "Cannot load CA cert list %s: %s",
452 certFile
, gerr
->message
);
457 data
.data
= (unsigned char *)buf
;
458 data
.size
= strlen(buf
);
460 if (gnutls_x509_crt_list_import(certs
, &certMax
, &data
,
461 GNUTLS_X509_FMT_PEM
, 0) < 0) {
463 "Unable to import CA certificate list %s",
479 qcrypto_tls_creds_x509_sanity_check(QCryptoTLSCredsX509
*creds
,
481 const char *cacertFile
,
482 const char *certFile
,
485 gnutls_x509_crt_t cert
= NULL
;
486 gnutls_x509_crt_t cacerts
[MAX_CERTS
];
491 memset(cacerts
, 0, sizeof(cacerts
));
493 access(certFile
, R_OK
) == 0) {
494 cert
= qcrypto_tls_creds_load_cert(creds
,
501 if (access(cacertFile
, R_OK
) == 0) {
502 if (qcrypto_tls_creds_load_ca_cert_list(creds
,
504 MAX_CERTS
, &ncacerts
,
511 qcrypto_tls_creds_check_cert(creds
,
512 cert
, certFile
, isServer
,
517 for (i
= 0; i
< ncacerts
; i
++) {
518 if (qcrypto_tls_creds_check_cert(creds
,
519 cacerts
[i
], cacertFile
,
520 isServer
, true, errp
) < 0) {
525 if (cert
&& ncacerts
&&
526 qcrypto_tls_creds_check_cert_pair(cert
, certFile
, cacerts
,
527 ncacerts
, cacertFile
,
528 isServer
, errp
) < 0) {
536 gnutls_x509_crt_deinit(cert
);
538 for (i
= 0; i
< ncacerts
; i
++) {
539 gnutls_x509_crt_deinit(cacerts
[i
]);
546 qcrypto_tls_creds_x509_load(QCryptoTLSCredsX509
*creds
,
549 char *cacert
= NULL
, *cacrl
= NULL
, *cert
= NULL
,
550 *key
= NULL
, *dhparams
= NULL
;
554 trace_qcrypto_tls_creds_x509_load(creds
,
555 creds
->parent_obj
.dir
? creds
->parent_obj
.dir
: "<nodir>");
557 if (creds
->parent_obj
.endpoint
== QCRYPTO_TLS_CREDS_ENDPOINT_SERVER
) {
558 if (qcrypto_tls_creds_get_path(&creds
->parent_obj
,
559 QCRYPTO_TLS_CREDS_X509_CA_CERT
,
560 true, &cacert
, errp
) < 0 ||
561 qcrypto_tls_creds_get_path(&creds
->parent_obj
,
562 QCRYPTO_TLS_CREDS_X509_CA_CRL
,
563 false, &cacrl
, errp
) < 0 ||
564 qcrypto_tls_creds_get_path(&creds
->parent_obj
,
565 QCRYPTO_TLS_CREDS_X509_SERVER_CERT
,
566 true, &cert
, errp
) < 0 ||
567 qcrypto_tls_creds_get_path(&creds
->parent_obj
,
568 QCRYPTO_TLS_CREDS_X509_SERVER_KEY
,
569 true, &key
, errp
) < 0 ||
570 qcrypto_tls_creds_get_path(&creds
->parent_obj
,
571 QCRYPTO_TLS_CREDS_DH_PARAMS
,
572 false, &dhparams
, errp
) < 0) {
576 if (qcrypto_tls_creds_get_path(&creds
->parent_obj
,
577 QCRYPTO_TLS_CREDS_X509_CA_CERT
,
578 true, &cacert
, errp
) < 0 ||
579 qcrypto_tls_creds_get_path(&creds
->parent_obj
,
580 QCRYPTO_TLS_CREDS_X509_CLIENT_CERT
,
581 false, &cert
, errp
) < 0 ||
582 qcrypto_tls_creds_get_path(&creds
->parent_obj
,
583 QCRYPTO_TLS_CREDS_X509_CLIENT_KEY
,
584 false, &key
, errp
) < 0) {
589 if (creds
->sanityCheck
&&
590 qcrypto_tls_creds_x509_sanity_check(creds
,
591 creds
->parent_obj
.endpoint
== QCRYPTO_TLS_CREDS_ENDPOINT_SERVER
,
592 cacert
, cert
, errp
) < 0) {
596 ret
= gnutls_certificate_allocate_credentials(&creds
->data
);
598 error_setg(errp
, "Cannot allocate credentials: '%s'",
599 gnutls_strerror(ret
));
603 ret
= gnutls_certificate_set_x509_trust_file(creds
->data
,
605 GNUTLS_X509_FMT_PEM
);
607 error_setg(errp
, "Cannot load CA certificate '%s': %s",
608 cacert
, gnutls_strerror(ret
));
612 if (cert
!= NULL
&& key
!= NULL
) {
613 #if GNUTLS_VERSION_NUMBER >= 0x030111
614 char *password
= NULL
;
615 if (creds
->passwordid
) {
616 password
= qcrypto_secret_lookup_as_utf8(creds
->passwordid
,
622 ret
= gnutls_certificate_set_x509_key_file2(creds
->data
,
628 #else /* GNUTLS_VERSION_NUMBER < 0x030111 */
629 if (creds
->passwordid
) {
630 error_setg(errp
, "PKCS8 decryption requires GNUTLS >= 3.1.11");
633 ret
= gnutls_certificate_set_x509_key_file(creds
->data
,
635 GNUTLS_X509_FMT_PEM
);
636 #endif /* GNUTLS_VERSION_NUMBER < 0x030111 */
638 error_setg(errp
, "Cannot load certificate '%s' & key '%s': %s",
639 cert
, key
, gnutls_strerror(ret
));
645 ret
= gnutls_certificate_set_x509_crl_file(creds
->data
,
647 GNUTLS_X509_FMT_PEM
);
649 error_setg(errp
, "Cannot load CRL '%s': %s",
650 cacrl
, gnutls_strerror(ret
));
655 if (creds
->parent_obj
.endpoint
== QCRYPTO_TLS_CREDS_ENDPOINT_SERVER
) {
656 if (qcrypto_tls_creds_get_dh_params_file(&creds
->parent_obj
, dhparams
,
657 &creds
->parent_obj
.dh_params
,
661 gnutls_certificate_set_dh_params(creds
->data
,
662 creds
->parent_obj
.dh_params
);
677 qcrypto_tls_creds_x509_unload(QCryptoTLSCredsX509
*creds
)
680 gnutls_certificate_free_credentials(creds
->data
);
683 if (creds
->parent_obj
.dh_params
) {
684 gnutls_dh_params_deinit(creds
->parent_obj
.dh_params
);
685 creds
->parent_obj
.dh_params
= NULL
;
690 #else /* ! CONFIG_GNUTLS */
694 qcrypto_tls_creds_x509_load(QCryptoTLSCredsX509
*creds G_GNUC_UNUSED
,
697 error_setg(errp
, "TLS credentials support requires GNUTLS");
702 qcrypto_tls_creds_x509_unload(QCryptoTLSCredsX509
*creds G_GNUC_UNUSED
)
708 #endif /* ! CONFIG_GNUTLS */
712 qcrypto_tls_creds_x509_prop_set_loaded(Object
*obj
,
716 QCryptoTLSCredsX509
*creds
= QCRYPTO_TLS_CREDS_X509(obj
);
719 qcrypto_tls_creds_x509_load(creds
, errp
);
721 qcrypto_tls_creds_x509_unload(creds
);
730 qcrypto_tls_creds_x509_prop_get_loaded(Object
*obj
,
731 Error
**errp G_GNUC_UNUSED
)
733 QCryptoTLSCredsX509
*creds
= QCRYPTO_TLS_CREDS_X509(obj
);
735 return creds
->data
!= NULL
;
739 #else /* ! CONFIG_GNUTLS */
743 qcrypto_tls_creds_x509_prop_get_loaded(Object
*obj G_GNUC_UNUSED
,
744 Error
**errp G_GNUC_UNUSED
)
750 #endif /* ! CONFIG_GNUTLS */
754 qcrypto_tls_creds_x509_prop_set_sanity(Object
*obj
,
756 Error
**errp G_GNUC_UNUSED
)
758 QCryptoTLSCredsX509
*creds
= QCRYPTO_TLS_CREDS_X509(obj
);
760 creds
->sanityCheck
= value
;
765 qcrypto_tls_creds_x509_prop_set_passwordid(Object
*obj
,
767 Error
**errp G_GNUC_UNUSED
)
769 QCryptoTLSCredsX509
*creds
= QCRYPTO_TLS_CREDS_X509(obj
);
771 creds
->passwordid
= g_strdup(value
);
776 qcrypto_tls_creds_x509_prop_get_passwordid(Object
*obj
,
777 Error
**errp G_GNUC_UNUSED
)
779 QCryptoTLSCredsX509
*creds
= QCRYPTO_TLS_CREDS_X509(obj
);
781 return g_strdup(creds
->passwordid
);
786 qcrypto_tls_creds_x509_prop_get_sanity(Object
*obj
,
787 Error
**errp G_GNUC_UNUSED
)
789 QCryptoTLSCredsX509
*creds
= QCRYPTO_TLS_CREDS_X509(obj
);
791 return creds
->sanityCheck
;
796 qcrypto_tls_creds_x509_complete(UserCreatable
*uc
, Error
**errp
)
798 object_property_set_bool(OBJECT(uc
), true, "loaded", errp
);
803 qcrypto_tls_creds_x509_init(Object
*obj
)
805 QCryptoTLSCredsX509
*creds
= QCRYPTO_TLS_CREDS_X509(obj
);
807 creds
->sanityCheck
= true;
812 qcrypto_tls_creds_x509_finalize(Object
*obj
)
814 QCryptoTLSCredsX509
*creds
= QCRYPTO_TLS_CREDS_X509(obj
);
816 g_free(creds
->passwordid
);
817 qcrypto_tls_creds_x509_unload(creds
);
822 qcrypto_tls_creds_x509_class_init(ObjectClass
*oc
, void *data
)
824 UserCreatableClass
*ucc
= USER_CREATABLE_CLASS(oc
);
826 ucc
->complete
= qcrypto_tls_creds_x509_complete
;
828 object_class_property_add_bool(oc
, "loaded",
829 qcrypto_tls_creds_x509_prop_get_loaded
,
830 qcrypto_tls_creds_x509_prop_set_loaded
,
832 object_class_property_add_bool(oc
, "sanity-check",
833 qcrypto_tls_creds_x509_prop_get_sanity
,
834 qcrypto_tls_creds_x509_prop_set_sanity
,
836 object_class_property_add_str(oc
, "passwordid",
837 qcrypto_tls_creds_x509_prop_get_passwordid
,
838 qcrypto_tls_creds_x509_prop_set_passwordid
,
843 static const TypeInfo qcrypto_tls_creds_x509_info
= {
844 .parent
= TYPE_QCRYPTO_TLS_CREDS
,
845 .name
= TYPE_QCRYPTO_TLS_CREDS_X509
,
846 .instance_size
= sizeof(QCryptoTLSCredsX509
),
847 .instance_init
= qcrypto_tls_creds_x509_init
,
848 .instance_finalize
= qcrypto_tls_creds_x509_finalize
,
849 .class_size
= sizeof(QCryptoTLSCredsX509Class
),
850 .class_init
= qcrypto_tls_creds_x509_class_init
,
851 .interfaces
= (InterfaceInfo
[]) {
852 { TYPE_USER_CREATABLE
},
859 qcrypto_tls_creds_x509_register_types(void)
861 type_register_static(&qcrypto_tls_creds_x509_info
);
865 type_init(qcrypto_tls_creds_x509_register_types
);