hw/arm: QOM'ify pxa2xx_gpio.c
[qemu/ar7.git] / crypto / secret.c
blob285ab7a63c8f1f4b0f86fe30874580521eaca359
1 /*
2 * QEMU crypto secret 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/secret.h"
23 #include "crypto/cipher.h"
24 #include "qapi/error.h"
25 #include "qom/object_interfaces.h"
26 #include "qemu/base64.h"
27 #include "trace.h"
30 static void
31 qcrypto_secret_load_data(QCryptoSecret *secret,
32 uint8_t **output,
33 size_t *outputlen,
34 Error **errp)
36 char *data = NULL;
37 size_t length = 0;
38 GError *gerr = NULL;
40 *output = NULL;
41 *outputlen = 0;
43 if (secret->file) {
44 if (secret->data) {
45 error_setg(errp,
46 "'file' and 'data' are mutually exclusive");
47 return;
49 if (!g_file_get_contents(secret->file, &data, &length, &gerr)) {
50 error_setg(errp,
51 "Unable to read %s: %s",
52 secret->file, gerr->message);
53 g_error_free(gerr);
54 return;
56 *output = (uint8_t *)data;
57 *outputlen = length;
58 } else if (secret->data) {
59 *outputlen = strlen(secret->data);
60 *output = (uint8_t *)g_strdup(secret->data);
61 } else {
62 error_setg(errp, "Either 'file' or 'data' must be provided");
67 static void qcrypto_secret_decrypt(QCryptoSecret *secret,
68 const uint8_t *input,
69 size_t inputlen,
70 uint8_t **output,
71 size_t *outputlen,
72 Error **errp)
74 uint8_t *key = NULL, *ciphertext = NULL, *iv = NULL;
75 size_t keylen, ciphertextlen, ivlen;
76 QCryptoCipher *aes = NULL;
77 uint8_t *plaintext = NULL;
79 *output = NULL;
80 *outputlen = 0;
82 if (qcrypto_secret_lookup(secret->keyid,
83 &key, &keylen,
84 errp) < 0) {
85 goto cleanup;
88 if (keylen != 32) {
89 error_setg(errp, "Key should be 32 bytes in length");
90 goto cleanup;
93 if (!secret->iv) {
94 error_setg(errp, "IV is required to decrypt secret");
95 goto cleanup;
98 iv = qbase64_decode(secret->iv, -1, &ivlen, errp);
99 if (!iv) {
100 goto cleanup;
102 if (ivlen != 16) {
103 error_setg(errp, "IV should be 16 bytes in length not %zu",
104 ivlen);
105 goto cleanup;
108 aes = qcrypto_cipher_new(QCRYPTO_CIPHER_ALG_AES_256,
109 QCRYPTO_CIPHER_MODE_CBC,
110 key, keylen,
111 errp);
112 if (!aes) {
113 goto cleanup;
116 if (qcrypto_cipher_setiv(aes, iv, ivlen, errp) < 0) {
117 goto cleanup;
120 if (secret->format == QCRYPTO_SECRET_FORMAT_BASE64) {
121 ciphertext = qbase64_decode((const gchar*)input,
122 inputlen,
123 &ciphertextlen,
124 errp);
125 if (!ciphertext) {
126 goto cleanup;
128 plaintext = g_new0(uint8_t, ciphertextlen + 1);
129 } else {
130 ciphertextlen = inputlen;
131 plaintext = g_new0(uint8_t, inputlen + 1);
133 if (qcrypto_cipher_decrypt(aes,
134 ciphertext ? ciphertext : input,
135 plaintext,
136 ciphertextlen,
137 errp) < 0) {
138 plaintext = NULL;
139 goto cleanup;
142 if (plaintext[ciphertextlen - 1] > 16 ||
143 plaintext[ciphertextlen - 1] > ciphertextlen) {
144 error_setg(errp, "Incorrect number of padding bytes (%d) "
145 "found on decrypted data",
146 (int)plaintext[ciphertextlen - 1]);
147 g_free(plaintext);
148 plaintext = NULL;
149 goto cleanup;
152 /* Even though plaintext may contain arbitrary NUL
153 * ensure it is explicitly NUL terminated.
155 ciphertextlen -= plaintext[ciphertextlen - 1];
156 plaintext[ciphertextlen] = '\0';
158 *output = plaintext;
159 *outputlen = ciphertextlen;
161 cleanup:
162 g_free(ciphertext);
163 g_free(iv);
164 g_free(key);
165 qcrypto_cipher_free(aes);
169 static void qcrypto_secret_decode(const uint8_t *input,
170 size_t inputlen,
171 uint8_t **output,
172 size_t *outputlen,
173 Error **errp)
175 *output = qbase64_decode((const gchar*)input,
176 inputlen,
177 outputlen,
178 errp);
182 static void
183 qcrypto_secret_prop_set_loaded(Object *obj,
184 bool value,
185 Error **errp)
187 QCryptoSecret *secret = QCRYPTO_SECRET(obj);
189 if (value) {
190 Error *local_err = NULL;
191 uint8_t *input = NULL;
192 size_t inputlen = 0;
193 uint8_t *output = NULL;
194 size_t outputlen = 0;
196 qcrypto_secret_load_data(secret, &input, &inputlen, &local_err);
197 if (local_err) {
198 error_propagate(errp, local_err);
199 return;
202 if (secret->keyid) {
203 qcrypto_secret_decrypt(secret, input, inputlen,
204 &output, &outputlen, &local_err);
205 g_free(input);
206 if (local_err) {
207 error_propagate(errp, local_err);
208 return;
210 input = output;
211 inputlen = outputlen;
212 } else {
213 if (secret->format != QCRYPTO_SECRET_FORMAT_RAW) {
214 qcrypto_secret_decode(input, inputlen,
215 &output, &outputlen, &local_err);
216 g_free(input);
217 if (local_err) {
218 error_propagate(errp, local_err);
219 return;
221 input = output;
222 inputlen = outputlen;
226 secret->rawdata = input;
227 secret->rawlen = inputlen;
228 } else {
229 g_free(secret->rawdata);
230 secret->rawlen = 0;
235 static bool
236 qcrypto_secret_prop_get_loaded(Object *obj,
237 Error **errp G_GNUC_UNUSED)
239 QCryptoSecret *secret = QCRYPTO_SECRET(obj);
240 return secret->data != NULL;
244 static void
245 qcrypto_secret_prop_set_format(Object *obj,
246 int value,
247 Error **errp G_GNUC_UNUSED)
249 QCryptoSecret *creds = QCRYPTO_SECRET(obj);
251 creds->format = value;
255 static int
256 qcrypto_secret_prop_get_format(Object *obj,
257 Error **errp G_GNUC_UNUSED)
259 QCryptoSecret *creds = QCRYPTO_SECRET(obj);
261 return creds->format;
265 static void
266 qcrypto_secret_prop_set_data(Object *obj,
267 const char *value,
268 Error **errp)
270 QCryptoSecret *secret = QCRYPTO_SECRET(obj);
272 g_free(secret->data);
273 secret->data = g_strdup(value);
277 static char *
278 qcrypto_secret_prop_get_data(Object *obj,
279 Error **errp)
281 QCryptoSecret *secret = QCRYPTO_SECRET(obj);
282 return g_strdup(secret->data);
286 static void
287 qcrypto_secret_prop_set_file(Object *obj,
288 const char *value,
289 Error **errp)
291 QCryptoSecret *secret = QCRYPTO_SECRET(obj);
293 g_free(secret->file);
294 secret->file = g_strdup(value);
298 static char *
299 qcrypto_secret_prop_get_file(Object *obj,
300 Error **errp)
302 QCryptoSecret *secret = QCRYPTO_SECRET(obj);
303 return g_strdup(secret->file);
307 static void
308 qcrypto_secret_prop_set_iv(Object *obj,
309 const char *value,
310 Error **errp)
312 QCryptoSecret *secret = QCRYPTO_SECRET(obj);
314 g_free(secret->iv);
315 secret->iv = g_strdup(value);
319 static char *
320 qcrypto_secret_prop_get_iv(Object *obj,
321 Error **errp)
323 QCryptoSecret *secret = QCRYPTO_SECRET(obj);
324 return g_strdup(secret->iv);
328 static void
329 qcrypto_secret_prop_set_keyid(Object *obj,
330 const char *value,
331 Error **errp)
333 QCryptoSecret *secret = QCRYPTO_SECRET(obj);
335 g_free(secret->keyid);
336 secret->keyid = g_strdup(value);
340 static char *
341 qcrypto_secret_prop_get_keyid(Object *obj,
342 Error **errp)
344 QCryptoSecret *secret = QCRYPTO_SECRET(obj);
345 return g_strdup(secret->keyid);
349 static void
350 qcrypto_secret_complete(UserCreatable *uc, Error **errp)
352 object_property_set_bool(OBJECT(uc), true, "loaded", errp);
356 static void
357 qcrypto_secret_finalize(Object *obj)
359 QCryptoSecret *secret = QCRYPTO_SECRET(obj);
361 g_free(secret->iv);
362 g_free(secret->file);
363 g_free(secret->keyid);
364 g_free(secret->rawdata);
365 g_free(secret->data);
368 static void
369 qcrypto_secret_class_init(ObjectClass *oc, void *data)
371 UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc);
373 ucc->complete = qcrypto_secret_complete;
375 object_class_property_add_bool(oc, "loaded",
376 qcrypto_secret_prop_get_loaded,
377 qcrypto_secret_prop_set_loaded,
378 NULL);
379 object_class_property_add_enum(oc, "format",
380 "QCryptoSecretFormat",
381 QCryptoSecretFormat_lookup,
382 qcrypto_secret_prop_get_format,
383 qcrypto_secret_prop_set_format,
384 NULL);
385 object_class_property_add_str(oc, "data",
386 qcrypto_secret_prop_get_data,
387 qcrypto_secret_prop_set_data,
388 NULL);
389 object_class_property_add_str(oc, "file",
390 qcrypto_secret_prop_get_file,
391 qcrypto_secret_prop_set_file,
392 NULL);
393 object_class_property_add_str(oc, "keyid",
394 qcrypto_secret_prop_get_keyid,
395 qcrypto_secret_prop_set_keyid,
396 NULL);
397 object_class_property_add_str(oc, "iv",
398 qcrypto_secret_prop_get_iv,
399 qcrypto_secret_prop_set_iv,
400 NULL);
404 int qcrypto_secret_lookup(const char *secretid,
405 uint8_t **data,
406 size_t *datalen,
407 Error **errp)
409 Object *obj;
410 QCryptoSecret *secret;
412 obj = object_resolve_path_component(
413 object_get_objects_root(), secretid);
414 if (!obj) {
415 error_setg(errp, "No secret with id '%s'", secretid);
416 return -1;
419 secret = (QCryptoSecret *)
420 object_dynamic_cast(obj,
421 TYPE_QCRYPTO_SECRET);
422 if (!secret) {
423 error_setg(errp, "Object with id '%s' is not a secret",
424 secretid);
425 return -1;
428 if (!secret->rawdata) {
429 error_setg(errp, "Secret with id '%s' has no data",
430 secretid);
431 return -1;
434 *data = g_new0(uint8_t, secret->rawlen + 1);
435 memcpy(*data, secret->rawdata, secret->rawlen);
436 (*data)[secret->rawlen] = '\0';
437 *datalen = secret->rawlen;
439 return 0;
443 char *qcrypto_secret_lookup_as_utf8(const char *secretid,
444 Error **errp)
446 uint8_t *data;
447 size_t datalen;
449 if (qcrypto_secret_lookup(secretid,
450 &data,
451 &datalen,
452 errp) < 0) {
453 return NULL;
456 if (!g_utf8_validate((const gchar*)data, datalen, NULL)) {
457 error_setg(errp,
458 "Data from secret %s is not valid UTF-8",
459 secretid);
460 g_free(data);
461 return NULL;
464 return (char *)data;
468 char *qcrypto_secret_lookup_as_base64(const char *secretid,
469 Error **errp)
471 uint8_t *data;
472 size_t datalen;
473 char *ret;
475 if (qcrypto_secret_lookup(secretid,
476 &data,
477 &datalen,
478 errp) < 0) {
479 return NULL;
482 ret = g_base64_encode(data, datalen);
483 g_free(data);
484 return ret;
488 static const TypeInfo qcrypto_secret_info = {
489 .parent = TYPE_OBJECT,
490 .name = TYPE_QCRYPTO_SECRET,
491 .instance_size = sizeof(QCryptoSecret),
492 .instance_finalize = qcrypto_secret_finalize,
493 .class_size = sizeof(QCryptoSecretClass),
494 .class_init = qcrypto_secret_class_init,
495 .interfaces = (InterfaceInfo[]) {
496 { TYPE_USER_CREATABLE },
502 static void
503 qcrypto_secret_register_types(void)
505 type_register_static(&qcrypto_secret_info);
509 type_init(qcrypto_secret_register_types);