2 * QEMU crypto TLS 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 "crypto/tlscredspriv.h"
28 qcrypto_tls_creds_get_dh_params_file(QCryptoTLSCreds
*creds
,
30 gnutls_dh_params_t
*dh_params
,
35 trace_qcrypto_tls_creds_load_dh(creds
, filename
? filename
: "<generated>");
37 if (filename
== NULL
) {
38 ret
= gnutls_dh_params_init(dh_params
);
40 error_setg(errp
, "Unable to initialize DH parameters: %s",
41 gnutls_strerror(ret
));
44 ret
= gnutls_dh_params_generate2(*dh_params
, DH_BITS
);
46 gnutls_dh_params_deinit(*dh_params
);
48 error_setg(errp
, "Unable to generate DH parameters: %s",
49 gnutls_strerror(ret
));
57 if (!g_file_get_contents(filename
,
62 error_setg(errp
, "%s", gerr
->message
);
66 data
.data
= (unsigned char *)contents
;
68 ret
= gnutls_dh_params_init(dh_params
);
71 error_setg(errp
, "Unable to initialize DH parameters: %s",
72 gnutls_strerror(ret
));
75 ret
= gnutls_dh_params_import_pkcs3(*dh_params
,
80 gnutls_dh_params_deinit(*dh_params
);
82 error_setg(errp
, "Unable to load DH parameters from %s: %s",
83 filename
, gnutls_strerror(ret
));
93 qcrypto_tls_creds_get_path(QCryptoTLSCreds
*creds
,
104 error_setg(errp
, "Missing 'dir' property value");
111 *cred
= g_strdup_printf("%s/%s", creds
->dir
, filename
);
113 if (stat(*cred
, &sb
) < 0) {
114 if (errno
== ENOENT
&& !required
) {
117 error_setg_errno(errp
, errno
,
118 "Unable to access credentials %s",
128 trace_qcrypto_tls_creds_get_path(creds
, filename
,
129 *cred
? *cred
: "<none>");
134 #endif /* ! CONFIG_GNUTLS */
138 qcrypto_tls_creds_prop_set_verify(Object
*obj
,
140 Error
**errp G_GNUC_UNUSED
)
142 QCryptoTLSCreds
*creds
= QCRYPTO_TLS_CREDS(obj
);
144 creds
->verifyPeer
= value
;
149 qcrypto_tls_creds_prop_get_verify(Object
*obj
,
150 Error
**errp G_GNUC_UNUSED
)
152 QCryptoTLSCreds
*creds
= QCRYPTO_TLS_CREDS(obj
);
154 return creds
->verifyPeer
;
159 qcrypto_tls_creds_prop_set_dir(Object
*obj
,
161 Error
**errp G_GNUC_UNUSED
)
163 QCryptoTLSCreds
*creds
= QCRYPTO_TLS_CREDS(obj
);
165 creds
->dir
= g_strdup(value
);
170 qcrypto_tls_creds_prop_get_dir(Object
*obj
,
171 Error
**errp G_GNUC_UNUSED
)
173 QCryptoTLSCreds
*creds
= QCRYPTO_TLS_CREDS(obj
);
175 return g_strdup(creds
->dir
);
180 qcrypto_tls_creds_prop_set_endpoint(Object
*obj
,
182 Error
**errp G_GNUC_UNUSED
)
184 QCryptoTLSCreds
*creds
= QCRYPTO_TLS_CREDS(obj
);
186 creds
->endpoint
= value
;
191 qcrypto_tls_creds_prop_get_endpoint(Object
*obj
,
192 Error
**errp G_GNUC_UNUSED
)
194 QCryptoTLSCreds
*creds
= QCRYPTO_TLS_CREDS(obj
);
196 return creds
->endpoint
;
201 qcrypto_tls_creds_init(Object
*obj
)
203 QCryptoTLSCreds
*creds
= QCRYPTO_TLS_CREDS(obj
);
205 creds
->verifyPeer
= true;
207 object_property_add_bool(obj
, "verify-peer",
208 qcrypto_tls_creds_prop_get_verify
,
209 qcrypto_tls_creds_prop_set_verify
,
211 object_property_add_str(obj
, "dir",
212 qcrypto_tls_creds_prop_get_dir
,
213 qcrypto_tls_creds_prop_set_dir
,
215 object_property_add_enum(obj
, "endpoint",
216 "QCryptoTLSCredsEndpoint",
217 QCryptoTLSCredsEndpoint_lookup
,
218 qcrypto_tls_creds_prop_get_endpoint
,
219 qcrypto_tls_creds_prop_set_endpoint
,
225 qcrypto_tls_creds_finalize(Object
*obj
)
227 QCryptoTLSCreds
*creds
= QCRYPTO_TLS_CREDS(obj
);
233 static const TypeInfo qcrypto_tls_creds_info
= {
234 .parent
= TYPE_OBJECT
,
235 .name
= TYPE_QCRYPTO_TLS_CREDS
,
236 .instance_size
= sizeof(QCryptoTLSCreds
),
237 .instance_init
= qcrypto_tls_creds_init
,
238 .instance_finalize
= qcrypto_tls_creds_finalize
,
239 .class_size
= sizeof(QCryptoTLSCredsClass
),
245 qcrypto_tls_creds_register_types(void)
247 type_register_static(&qcrypto_tls_creds_info
);
251 type_init(qcrypto_tls_creds_register_types
);