xref: /openbmc/qemu/crypto/tlscreds.c (revision 61d9f32b)
1 /*
2  * QEMU crypto TLS credential support
3  *
4  * Copyright (c) 2015 Red Hat, Inc.
5  *
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.
10  *
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.
15  *
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/>.
18  *
19  */
20 
21 #include "qemu/osdep.h"
22 #include "crypto/tlscredspriv.h"
23 #include "trace.h"
24 
25 #define DH_BITS 2048
26 
27 #ifdef CONFIG_GNUTLS
28 int
29 qcrypto_tls_creds_get_dh_params_file(QCryptoTLSCreds *creds,
30                                      const char *filename,
31                                      gnutls_dh_params_t *dh_params,
32                                      Error **errp)
33 {
34     int ret;
35 
36     trace_qcrypto_tls_creds_load_dh(creds, filename ? filename : "<generated>");
37 
38     if (filename == NULL) {
39         ret = gnutls_dh_params_init(dh_params);
40         if (ret < 0) {
41             error_setg(errp, "Unable to initialize DH parameters: %s",
42                        gnutls_strerror(ret));
43             return -1;
44         }
45         ret = gnutls_dh_params_generate2(*dh_params, DH_BITS);
46         if (ret < 0) {
47             gnutls_dh_params_deinit(*dh_params);
48             *dh_params = NULL;
49             error_setg(errp, "Unable to generate DH parameters: %s",
50                        gnutls_strerror(ret));
51             return -1;
52         }
53     } else {
54         GError *gerr = NULL;
55         gchar *contents;
56         gsize len;
57         gnutls_datum_t data;
58         if (!g_file_get_contents(filename,
59                                  &contents,
60                                  &len,
61                                  &gerr)) {
62 
63             error_setg(errp, "%s", gerr->message);
64             g_error_free(gerr);
65             return -1;
66         }
67         data.data = (unsigned char *)contents;
68         data.size = len;
69         ret = gnutls_dh_params_init(dh_params);
70         if (ret < 0) {
71             g_free(contents);
72             error_setg(errp, "Unable to initialize DH parameters: %s",
73                        gnutls_strerror(ret));
74             return -1;
75         }
76         ret = gnutls_dh_params_import_pkcs3(*dh_params,
77                                             &data,
78                                             GNUTLS_X509_FMT_PEM);
79         g_free(contents);
80         if (ret < 0) {
81             gnutls_dh_params_deinit(*dh_params);
82             *dh_params = NULL;
83             error_setg(errp, "Unable to load DH parameters from %s: %s",
84                        filename, gnutls_strerror(ret));
85             return -1;
86         }
87     }
88 
89     return 0;
90 }
91 
92 
93 int
94 qcrypto_tls_creds_get_path(QCryptoTLSCreds *creds,
95                            const char *filename,
96                            bool required,
97                            char **cred,
98                            Error **errp)
99 {
100     struct stat sb;
101     int ret = -1;
102 
103     if (!creds->dir) {
104         if (required) {
105             error_setg(errp, "Missing 'dir' property value");
106             return -1;
107         } else {
108             return 0;
109         }
110     }
111 
112     *cred = g_strdup_printf("%s/%s", creds->dir, filename);
113 
114     if (stat(*cred, &sb) < 0) {
115         if (errno == ENOENT && !required) {
116             ret = 0;
117         } else {
118             error_setg_errno(errp, errno,
119                              "Unable to access credentials %s",
120                              *cred);
121         }
122         g_free(*cred);
123         *cred = NULL;
124         goto cleanup;
125     }
126 
127     ret = 0;
128  cleanup:
129     trace_qcrypto_tls_creds_get_path(creds, filename,
130                                      *cred ? *cred : "<none>");
131     return ret;
132 }
133 
134 
135 #endif /* ! CONFIG_GNUTLS */
136 
137 
138 static void
139 qcrypto_tls_creds_prop_set_verify(Object *obj,
140                                   bool value,
141                                   Error **errp G_GNUC_UNUSED)
142 {
143     QCryptoTLSCreds *creds = QCRYPTO_TLS_CREDS(obj);
144 
145     creds->verifyPeer = value;
146 }
147 
148 
149 static bool
150 qcrypto_tls_creds_prop_get_verify(Object *obj,
151                                   Error **errp G_GNUC_UNUSED)
152 {
153     QCryptoTLSCreds *creds = QCRYPTO_TLS_CREDS(obj);
154 
155     return creds->verifyPeer;
156 }
157 
158 
159 static void
160 qcrypto_tls_creds_prop_set_dir(Object *obj,
161                                const char *value,
162                                Error **errp G_GNUC_UNUSED)
163 {
164     QCryptoTLSCreds *creds = QCRYPTO_TLS_CREDS(obj);
165 
166     creds->dir = g_strdup(value);
167 }
168 
169 
170 static char *
171 qcrypto_tls_creds_prop_get_dir(Object *obj,
172                                Error **errp G_GNUC_UNUSED)
173 {
174     QCryptoTLSCreds *creds = QCRYPTO_TLS_CREDS(obj);
175 
176     return g_strdup(creds->dir);
177 }
178 
179 
180 static void
181 qcrypto_tls_creds_prop_set_endpoint(Object *obj,
182                                     int value,
183                                     Error **errp G_GNUC_UNUSED)
184 {
185     QCryptoTLSCreds *creds = QCRYPTO_TLS_CREDS(obj);
186 
187     creds->endpoint = value;
188 }
189 
190 
191 static int
192 qcrypto_tls_creds_prop_get_endpoint(Object *obj,
193                                     Error **errp G_GNUC_UNUSED)
194 {
195     QCryptoTLSCreds *creds = QCRYPTO_TLS_CREDS(obj);
196 
197     return creds->endpoint;
198 }
199 
200 
201 static void
202 qcrypto_tls_creds_init(Object *obj)
203 {
204     QCryptoTLSCreds *creds = QCRYPTO_TLS_CREDS(obj);
205 
206     creds->verifyPeer = true;
207 
208     object_property_add_bool(obj, "verify-peer",
209                              qcrypto_tls_creds_prop_get_verify,
210                              qcrypto_tls_creds_prop_set_verify,
211                              NULL);
212     object_property_add_str(obj, "dir",
213                             qcrypto_tls_creds_prop_get_dir,
214                             qcrypto_tls_creds_prop_set_dir,
215                             NULL);
216     object_property_add_enum(obj, "endpoint",
217                              "QCryptoTLSCredsEndpoint",
218                              QCryptoTLSCredsEndpoint_lookup,
219                              qcrypto_tls_creds_prop_get_endpoint,
220                              qcrypto_tls_creds_prop_set_endpoint,
221                              NULL);
222 }
223 
224 
225 static void
226 qcrypto_tls_creds_finalize(Object *obj)
227 {
228     QCryptoTLSCreds *creds = QCRYPTO_TLS_CREDS(obj);
229 
230     g_free(creds->dir);
231 }
232 
233 
234 static const TypeInfo qcrypto_tls_creds_info = {
235     .parent = TYPE_OBJECT,
236     .name = TYPE_QCRYPTO_TLS_CREDS,
237     .instance_size = sizeof(QCryptoTLSCreds),
238     .instance_init = qcrypto_tls_creds_init,
239     .instance_finalize = qcrypto_tls_creds_finalize,
240     .class_size = sizeof(QCryptoTLSCredsClass),
241     .abstract = true,
242 };
243 
244 
245 static void
246 qcrypto_tls_creds_register_types(void)
247 {
248     type_register_static(&qcrypto_tls_creds_info);
249 }
250 
251 
252 type_init(qcrypto_tls_creds_register_types);
253