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 "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")),
76 qcrypto_tls_creds_check_cert_basic_constraints(QCryptoTLSCredsX509
*creds
,
77 gnutls_x509_crt_t cert
,
85 status
= gnutls_x509_crt_get_basic_constraints(cert
, NULL
, NULL
, NULL
);
86 trace_qcrypto_tls_creds_x509_check_basic_constraints(
87 creds
, certFile
, status
);
89 if (status
> 0) { /* It is a CA cert */
91 error_setg(errp
, isServer
?
92 "The certificate %s basic constraints show a CA, "
93 "but we need one for a server" :
94 "The certificate %s basic constraints show a CA, "
95 "but we need one for a client",
99 } else if (status
== 0) { /* It is not a CA cert */
102 "The certificate %s basic constraints do not "
107 } else if (status
== GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE
) {
108 /* Missing basicConstraints */
111 "The certificate %s is missing basic constraints "
116 } else { /* General error */
118 "Unable to query certificate %s basic constraints: %s",
119 certFile
, gnutls_strerror(status
));
128 qcrypto_tls_creds_check_cert_key_usage(QCryptoTLSCredsX509
*creds
,
129 gnutls_x509_crt_t cert
,
130 const char *certFile
,
135 unsigned int usage
= 0;
136 unsigned int critical
= 0;
138 status
= gnutls_x509_crt_get_key_usage(cert
, &usage
, &critical
);
139 trace_qcrypto_tls_creds_x509_check_key_usage(
140 creds
, certFile
, status
, usage
, critical
);
143 if (status
== GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE
) {
144 usage
= isCA
? GNUTLS_KEY_KEY_CERT_SIGN
:
145 GNUTLS_KEY_DIGITAL_SIGNATURE
|GNUTLS_KEY_KEY_ENCIPHERMENT
;
148 "Unable to query certificate %s key usage: %s",
149 certFile
, gnutls_strerror(status
));
155 if (!(usage
& GNUTLS_KEY_KEY_CERT_SIGN
)) {
158 "Certificate %s usage does not permit "
159 "certificate signing", certFile
);
164 if (!(usage
& GNUTLS_KEY_DIGITAL_SIGNATURE
)) {
167 "Certificate %s usage does not permit digital "
168 "signature", certFile
);
172 if (!(usage
& GNUTLS_KEY_KEY_ENCIPHERMENT
)) {
175 "Certificate %s usage does not permit key "
176 "encipherment", certFile
);
187 qcrypto_tls_creds_check_cert_key_purpose(QCryptoTLSCredsX509
*creds
,
188 gnutls_x509_crt_t cert
,
189 const char *certFile
,
195 unsigned int purposeCritical
;
196 unsigned int critical
;
199 bool allowClient
= false, allowServer
= false;
204 status
= gnutls_x509_crt_get_key_purpose_oid(cert
, i
, buffer
,
207 if (status
== GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE
) {
209 /* If there is no data at all, then we must allow
210 client/server to pass */
212 allowServer
= allowClient
= true;
216 if (status
!= GNUTLS_E_SHORT_MEMORY_BUFFER
) {
218 "Unable to query certificate %s key purpose: %s",
219 certFile
, gnutls_strerror(status
));
223 buffer
= g_new0(char, size
);
225 status
= gnutls_x509_crt_get_key_purpose_oid(cert
, i
, buffer
,
226 &size
, &purposeCritical
);
229 trace_qcrypto_tls_creds_x509_check_key_purpose(
230 creds
, certFile
, status
, "<none>", purposeCritical
);
233 "Unable to query certificate %s key purpose: %s",
234 certFile
, gnutls_strerror(status
));
237 trace_qcrypto_tls_creds_x509_check_key_purpose(
238 creds
, certFile
, status
, buffer
, purposeCritical
);
239 if (purposeCritical
) {
243 if (g_str_equal(buffer
, GNUTLS_KP_TLS_WWW_SERVER
)) {
245 } else if (g_str_equal(buffer
, GNUTLS_KP_TLS_WWW_CLIENT
)) {
247 } else if (g_str_equal(buffer
, GNUTLS_KP_ANY
)) {
248 allowServer
= allowClient
= true;
259 "Certificate %s purpose does not allow "
260 "use with a TLS server", certFile
);
268 "Certificate %s purpose does not allow use "
269 "with a TLS client", certFile
);
280 qcrypto_tls_creds_check_cert(QCryptoTLSCredsX509
*creds
,
281 gnutls_x509_crt_t cert
,
282 const char *certFile
,
287 if (qcrypto_tls_creds_check_cert_times(cert
, certFile
,
293 if (qcrypto_tls_creds_check_cert_basic_constraints(creds
,
300 if (qcrypto_tls_creds_check_cert_key_usage(creds
,
307 qcrypto_tls_creds_check_cert_key_purpose(creds
,
309 isServer
, errp
) < 0) {
318 qcrypto_tls_creds_check_cert_pair(gnutls_x509_crt_t cert
,
319 const char *certFile
,
320 gnutls_x509_crt_t
*cacerts
,
322 const char *cacertFile
,
328 if (gnutls_x509_crt_list_verify(&cert
, 1,
332 error_setg(errp
, isServer
?
333 "Unable to verify server certificate %s against "
334 "CA certificate %s" :
335 "Unable to verify client certificate %s against "
337 certFile
, cacertFile
);
342 const char *reason
= "Invalid certificate";
344 if (status
& GNUTLS_CERT_INVALID
) {
345 reason
= "The certificate is not trusted";
348 if (status
& GNUTLS_CERT_SIGNER_NOT_FOUND
) {
349 reason
= "The certificate hasn't got a known issuer";
352 if (status
& GNUTLS_CERT_REVOKED
) {
353 reason
= "The certificate has been revoked";
356 #ifndef GNUTLS_1_0_COMPAT
357 if (status
& GNUTLS_CERT_INSECURE_ALGORITHM
) {
358 reason
= "The certificate uses an insecure algorithm";
363 "Our own certificate %s failed validation against %s: %s",
364 certFile
, cacertFile
, reason
);
372 static gnutls_x509_crt_t
373 qcrypto_tls_creds_load_cert(QCryptoTLSCredsX509
*creds
,
374 const char *certFile
,
379 gnutls_x509_crt_t cert
= NULL
;
386 trace_qcrypto_tls_creds_x509_load_cert(creds
, isServer
, certFile
);
388 err
= gnutls_x509_crt_init(&cert
);
390 error_setg(errp
, "Unable to initialize certificate: %s",
391 gnutls_strerror(err
));
395 if (!g_file_get_contents(certFile
, &buf
, &buflen
, &gerr
)) {
396 error_setg(errp
, "Cannot load CA cert list %s: %s",
397 certFile
, gerr
->message
);
402 data
.data
= (unsigned char *)buf
;
403 data
.size
= strlen(buf
);
405 err
= gnutls_x509_crt_import(cert
, &data
, GNUTLS_X509_FMT_PEM
);
407 error_setg(errp
, isServer
?
408 "Unable to import server certificate %s: %s" :
409 "Unable to import client certificate %s: %s",
411 gnutls_strerror(err
));
419 gnutls_x509_crt_deinit(cert
);
428 qcrypto_tls_creds_load_ca_cert_list(QCryptoTLSCredsX509
*creds
,
429 const char *certFile
,
430 gnutls_x509_crt_t
*certs
,
431 unsigned int certMax
,
442 trace_qcrypto_tls_creds_x509_load_cert_list(creds
, certFile
);
444 if (!g_file_get_contents(certFile
, &buf
, &buflen
, &gerr
)) {
445 error_setg(errp
, "Cannot load CA cert list %s: %s",
446 certFile
, gerr
->message
);
451 data
.data
= (unsigned char *)buf
;
452 data
.size
= strlen(buf
);
454 if (gnutls_x509_crt_list_import(certs
, &certMax
, &data
,
455 GNUTLS_X509_FMT_PEM
, 0) < 0) {
457 "Unable to import CA certificate list %s",
473 qcrypto_tls_creds_x509_sanity_check(QCryptoTLSCredsX509
*creds
,
475 const char *cacertFile
,
476 const char *certFile
,
479 gnutls_x509_crt_t cert
= NULL
;
480 gnutls_x509_crt_t cacerts
[MAX_CERTS
];
485 memset(cacerts
, 0, sizeof(cacerts
));
487 access(certFile
, R_OK
) == 0) {
488 cert
= qcrypto_tls_creds_load_cert(creds
,
495 if (access(cacertFile
, R_OK
) == 0) {
496 if (qcrypto_tls_creds_load_ca_cert_list(creds
,
498 MAX_CERTS
, &ncacerts
,
505 qcrypto_tls_creds_check_cert(creds
,
506 cert
, certFile
, isServer
,
511 for (i
= 0; i
< ncacerts
; i
++) {
512 if (qcrypto_tls_creds_check_cert(creds
,
513 cacerts
[i
], cacertFile
,
514 isServer
, true, errp
) < 0) {
519 if (cert
&& ncacerts
&&
520 qcrypto_tls_creds_check_cert_pair(cert
, certFile
, cacerts
,
521 ncacerts
, cacertFile
,
522 isServer
, errp
) < 0) {
530 gnutls_x509_crt_deinit(cert
);
532 for (i
= 0; i
< ncacerts
; i
++) {
533 gnutls_x509_crt_deinit(cacerts
[i
]);
540 qcrypto_tls_creds_x509_load(QCryptoTLSCredsX509
*creds
,
543 char *cacert
= NULL
, *cacrl
= NULL
, *cert
= NULL
,
544 *key
= NULL
, *dhparams
= NULL
;
548 trace_qcrypto_tls_creds_x509_load(creds
,
549 creds
->parent_obj
.dir
? creds
->parent_obj
.dir
: "<nodir>");
551 if (creds
->parent_obj
.endpoint
== QCRYPTO_TLS_CREDS_ENDPOINT_SERVER
) {
552 if (qcrypto_tls_creds_get_path(&creds
->parent_obj
,
553 QCRYPTO_TLS_CREDS_X509_CA_CERT
,
554 true, &cacert
, errp
) < 0 ||
555 qcrypto_tls_creds_get_path(&creds
->parent_obj
,
556 QCRYPTO_TLS_CREDS_X509_CA_CRL
,
557 false, &cacrl
, errp
) < 0 ||
558 qcrypto_tls_creds_get_path(&creds
->parent_obj
,
559 QCRYPTO_TLS_CREDS_X509_SERVER_CERT
,
560 true, &cert
, errp
) < 0 ||
561 qcrypto_tls_creds_get_path(&creds
->parent_obj
,
562 QCRYPTO_TLS_CREDS_X509_SERVER_KEY
,
563 true, &key
, errp
) < 0 ||
564 qcrypto_tls_creds_get_path(&creds
->parent_obj
,
565 QCRYPTO_TLS_CREDS_DH_PARAMS
,
566 false, &dhparams
, errp
) < 0) {
570 if (qcrypto_tls_creds_get_path(&creds
->parent_obj
,
571 QCRYPTO_TLS_CREDS_X509_CA_CERT
,
572 true, &cacert
, errp
) < 0 ||
573 qcrypto_tls_creds_get_path(&creds
->parent_obj
,
574 QCRYPTO_TLS_CREDS_X509_CLIENT_CERT
,
575 false, &cert
, errp
) < 0 ||
576 qcrypto_tls_creds_get_path(&creds
->parent_obj
,
577 QCRYPTO_TLS_CREDS_X509_CLIENT_KEY
,
578 false, &key
, errp
) < 0) {
583 if (creds
->sanityCheck
&&
584 qcrypto_tls_creds_x509_sanity_check(creds
,
585 creds
->parent_obj
.endpoint
== QCRYPTO_TLS_CREDS_ENDPOINT_SERVER
,
586 cacert
, cert
, errp
) < 0) {
590 ret
= gnutls_certificate_allocate_credentials(&creds
->data
);
592 error_setg(errp
, "Cannot allocate credentials: '%s'",
593 gnutls_strerror(ret
));
597 ret
= gnutls_certificate_set_x509_trust_file(creds
->data
,
599 GNUTLS_X509_FMT_PEM
);
601 error_setg(errp
, "Cannot load CA certificate '%s': %s",
602 cacert
, gnutls_strerror(ret
));
606 if (cert
!= NULL
&& key
!= NULL
) {
607 char *password
= NULL
;
608 if (creds
->passwordid
) {
609 password
= qcrypto_secret_lookup_as_utf8(creds
->passwordid
,
615 ret
= gnutls_certificate_set_x509_key_file2(creds
->data
,
622 error_setg(errp
, "Cannot load certificate '%s' & key '%s': %s",
623 cert
, key
, gnutls_strerror(ret
));
629 ret
= gnutls_certificate_set_x509_crl_file(creds
->data
,
631 GNUTLS_X509_FMT_PEM
);
633 error_setg(errp
, "Cannot load CRL '%s': %s",
634 cacrl
, gnutls_strerror(ret
));
639 if (creds
->parent_obj
.endpoint
== QCRYPTO_TLS_CREDS_ENDPOINT_SERVER
) {
640 if (qcrypto_tls_creds_get_dh_params_file(&creds
->parent_obj
, dhparams
,
641 &creds
->parent_obj
.dh_params
,
645 gnutls_certificate_set_dh_params(creds
->data
,
646 creds
->parent_obj
.dh_params
);
661 qcrypto_tls_creds_x509_unload(QCryptoTLSCredsX509
*creds
)
664 gnutls_certificate_free_credentials(creds
->data
);
667 if (creds
->parent_obj
.dh_params
) {
668 gnutls_dh_params_deinit(creds
->parent_obj
.dh_params
);
669 creds
->parent_obj
.dh_params
= NULL
;
674 #else /* ! CONFIG_GNUTLS */
678 qcrypto_tls_creds_x509_load(QCryptoTLSCredsX509
*creds G_GNUC_UNUSED
,
681 error_setg(errp
, "TLS credentials support requires GNUTLS");
686 qcrypto_tls_creds_x509_unload(QCryptoTLSCredsX509
*creds G_GNUC_UNUSED
)
692 #endif /* ! CONFIG_GNUTLS */
696 qcrypto_tls_creds_x509_prop_set_loaded(Object
*obj
,
700 QCryptoTLSCredsX509
*creds
= QCRYPTO_TLS_CREDS_X509(obj
);
703 qcrypto_tls_creds_x509_load(creds
, errp
);
705 qcrypto_tls_creds_x509_unload(creds
);
714 qcrypto_tls_creds_x509_prop_get_loaded(Object
*obj
,
715 Error
**errp G_GNUC_UNUSED
)
717 QCryptoTLSCredsX509
*creds
= QCRYPTO_TLS_CREDS_X509(obj
);
719 return creds
->data
!= NULL
;
723 #else /* ! CONFIG_GNUTLS */
727 qcrypto_tls_creds_x509_prop_get_loaded(Object
*obj G_GNUC_UNUSED
,
728 Error
**errp G_GNUC_UNUSED
)
734 #endif /* ! CONFIG_GNUTLS */
738 qcrypto_tls_creds_x509_prop_set_sanity(Object
*obj
,
740 Error
**errp G_GNUC_UNUSED
)
742 QCryptoTLSCredsX509
*creds
= QCRYPTO_TLS_CREDS_X509(obj
);
744 creds
->sanityCheck
= value
;
749 qcrypto_tls_creds_x509_prop_set_passwordid(Object
*obj
,
751 Error
**errp G_GNUC_UNUSED
)
753 QCryptoTLSCredsX509
*creds
= QCRYPTO_TLS_CREDS_X509(obj
);
755 creds
->passwordid
= g_strdup(value
);
760 qcrypto_tls_creds_x509_prop_get_passwordid(Object
*obj
,
761 Error
**errp G_GNUC_UNUSED
)
763 QCryptoTLSCredsX509
*creds
= QCRYPTO_TLS_CREDS_X509(obj
);
765 return g_strdup(creds
->passwordid
);
770 qcrypto_tls_creds_x509_prop_get_sanity(Object
*obj
,
771 Error
**errp G_GNUC_UNUSED
)
773 QCryptoTLSCredsX509
*creds
= QCRYPTO_TLS_CREDS_X509(obj
);
775 return creds
->sanityCheck
;
780 qcrypto_tls_creds_x509_complete(UserCreatable
*uc
, Error
**errp
)
782 object_property_set_bool(OBJECT(uc
), true, "loaded", errp
);
787 qcrypto_tls_creds_x509_init(Object
*obj
)
789 QCryptoTLSCredsX509
*creds
= QCRYPTO_TLS_CREDS_X509(obj
);
791 creds
->sanityCheck
= true;
796 qcrypto_tls_creds_x509_finalize(Object
*obj
)
798 QCryptoTLSCredsX509
*creds
= QCRYPTO_TLS_CREDS_X509(obj
);
800 g_free(creds
->passwordid
);
801 qcrypto_tls_creds_x509_unload(creds
);
806 qcrypto_tls_creds_x509_class_init(ObjectClass
*oc
, void *data
)
808 UserCreatableClass
*ucc
= USER_CREATABLE_CLASS(oc
);
810 ucc
->complete
= qcrypto_tls_creds_x509_complete
;
812 object_class_property_add_bool(oc
, "loaded",
813 qcrypto_tls_creds_x509_prop_get_loaded
,
814 qcrypto_tls_creds_x509_prop_set_loaded
,
816 object_class_property_add_bool(oc
, "sanity-check",
817 qcrypto_tls_creds_x509_prop_get_sanity
,
818 qcrypto_tls_creds_x509_prop_set_sanity
,
820 object_class_property_add_str(oc
, "passwordid",
821 qcrypto_tls_creds_x509_prop_get_passwordid
,
822 qcrypto_tls_creds_x509_prop_set_passwordid
,
827 static const TypeInfo qcrypto_tls_creds_x509_info
= {
828 .parent
= TYPE_QCRYPTO_TLS_CREDS
,
829 .name
= TYPE_QCRYPTO_TLS_CREDS_X509
,
830 .instance_size
= sizeof(QCryptoTLSCredsX509
),
831 .instance_init
= qcrypto_tls_creds_x509_init
,
832 .instance_finalize
= qcrypto_tls_creds_x509_finalize
,
833 .class_size
= sizeof(QCryptoTLSCredsX509Class
),
834 .class_init
= qcrypto_tls_creds_x509_class_init
,
835 .interfaces
= (InterfaceInfo
[]) {
836 { TYPE_USER_CREATABLE
},
843 qcrypto_tls_creds_x509_register_types(void)
845 type_register_static(&qcrypto_tls_creds_x509_info
);
849 type_init(qcrypto_tls_creds_x509_register_types
);