diff options
Diffstat (limited to 'src/userpref.c')
| -rw-r--r-- | src/userpref.c | 605 |
1 files changed, 0 insertions, 605 deletions
diff --git a/src/userpref.c b/src/userpref.c deleted file mode 100644 index 6e62000..0000000 --- a/src/userpref.c +++ /dev/null | |||
| @@ -1,605 +0,0 @@ | |||
| 1 | /* | ||
| 2 | * userpref.c | ||
| 3 | * contains methods to access user specific certificates IDs and more. | ||
| 4 | * | ||
| 5 | * Copyright (c) 2008 Jonathan Beck All Rights Reserved. | ||
| 6 | * | ||
| 7 | * This library is free software; you can redistribute it and/or | ||
| 8 | * modify it under the terms of the GNU Lesser General Public | ||
| 9 | * License as published by the Free Software Foundation; either | ||
| 10 | * version 2.1 of the License, or (at your option) any later version. | ||
| 11 | * | ||
| 12 | * This library is distributed in the hope that it will be useful, | ||
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 15 | * Lesser General Public License for more details. | ||
| 16 | * | ||
| 17 | * You should have received a copy of the GNU Lesser General Public | ||
| 18 | * License along with this library; if not, write to the Free Software | ||
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 20 | */ | ||
| 21 | |||
| 22 | #include <glib.h> | ||
| 23 | #include <glib/gstdio.h> | ||
| 24 | #include <glib/gprintf.h> | ||
| 25 | #include <stdio.h> | ||
| 26 | #include <stdint.h> | ||
| 27 | #include <stdlib.h> | ||
| 28 | #include <string.h> | ||
| 29 | #include <gnutls/gnutls.h> | ||
| 30 | #include <gnutls/x509.h> | ||
| 31 | #include <gcrypt.h> | ||
| 32 | |||
| 33 | #include "userpref.h" | ||
| 34 | #include "debug.h" | ||
| 35 | |||
| 36 | #define LIBIMOBILEDEVICE_CONF_DIR "libimobiledevice" | ||
| 37 | #define LIBIMOBILEDEVICE_CONF_FILE "libimobiledevicerc" | ||
| 38 | |||
| 39 | #define LIBIMOBILEDEVICE_ROOT_PRIVKEY "RootPrivateKey.pem" | ||
| 40 | #define LIBIMOBILEDEVICE_HOST_PRIVKEY "HostPrivateKey.pem" | ||
| 41 | #define LIBIMOBILEDEVICE_ROOT_CERTIF "RootCertificate.pem" | ||
| 42 | #define LIBIMOBILEDEVICE_HOST_CERTIF "HostCertificate.pem" | ||
| 43 | |||
| 44 | |||
| 45 | /** | ||
| 46 | * Creates a freedesktop compatible configuration directory. | ||
| 47 | */ | ||
| 48 | static void userpref_create_config_dir(void) | ||
| 49 | { | ||
| 50 | gchar *config_dir = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIMOBILEDEVICE_CONF_DIR, NULL); | ||
| 51 | |||
| 52 | if (!g_file_test(config_dir, (G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR))) | ||
| 53 | g_mkdir_with_parents(config_dir, 0755); | ||
| 54 | |||
| 55 | g_free(config_dir); | ||
| 56 | } | ||
| 57 | |||
| 58 | static int get_rand(int min, int max) | ||
| 59 | { | ||
| 60 | int retval = (rand() % (max - min)) + min; | ||
| 61 | return retval; | ||
| 62 | } | ||
| 63 | |||
| 64 | /** | ||
| 65 | * Generates a valid HostID (which is actually a UUID). | ||
| 66 | * | ||
| 67 | * @return A null terminated string containing a valid HostID. | ||
| 68 | */ | ||
| 69 | static char *userpref_generate_host_id() | ||
| 70 | { | ||
| 71 | /* HostID's are just UUID's, and UUID's are 36 characters long */ | ||
| 72 | char *hostid = (char *) malloc(sizeof(char) * 37); | ||
| 73 | const char *chars = "ABCDEF0123456789"; | ||
| 74 | srand(time(NULL)); | ||
| 75 | int i = 0; | ||
| 76 | |||
| 77 | for (i = 0; i < 36; i++) { | ||
| 78 | if (i == 8 || i == 13 || i == 18 || i == 23) { | ||
| 79 | hostid[i] = '-'; | ||
| 80 | continue; | ||
| 81 | } else { | ||
| 82 | hostid[i] = chars[get_rand(0, 16)]; | ||
| 83 | } | ||
| 84 | } | ||
| 85 | /* make it a real string */ | ||
| 86 | hostid[36] = '\0'; | ||
| 87 | return hostid; | ||
| 88 | } | ||
| 89 | |||
| 90 | /** | ||
| 91 | * Store HostID in config file. | ||
| 92 | * | ||
| 93 | * @param host_id A null terminated string containing a valid HostID. | ||
| 94 | */ | ||
| 95 | static int userpref_set_host_id(const char *host_id) | ||
| 96 | { | ||
| 97 | GKeyFile *key_file; | ||
| 98 | gsize length; | ||
| 99 | gchar *buf, *config_file; | ||
| 100 | GIOChannel *file; | ||
| 101 | |||
| 102 | if (!host_id) | ||
| 103 | return 0; | ||
| 104 | |||
| 105 | /* Make sure config directory exists */ | ||
| 106 | userpref_create_config_dir(); | ||
| 107 | |||
| 108 | /* Now parse file to get the HostID */ | ||
| 109 | key_file = g_key_file_new(); | ||
| 110 | |||
| 111 | /* Store in config file */ | ||
| 112 | debug_info("setting hostID to %s", host_id); | ||
| 113 | g_key_file_set_value(key_file, "Global", "HostID", host_id); | ||
| 114 | |||
| 115 | /* Write config file on disk */ | ||
| 116 | buf = g_key_file_to_data(key_file, &length, NULL); | ||
| 117 | config_file = | ||
| 118 | g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIMOBILEDEVICE_CONF_DIR, LIBIMOBILEDEVICE_CONF_FILE, NULL); | ||
| 119 | file = g_io_channel_new_file(config_file, "w", NULL); | ||
| 120 | g_free(config_file); | ||
| 121 | g_io_channel_write_chars(file, buf, length, NULL, NULL); | ||
| 122 | g_io_channel_shutdown(file, TRUE, NULL); | ||
| 123 | g_io_channel_unref(file); | ||
| 124 | |||
| 125 | g_key_file_free(key_file); | ||
| 126 | return 1; | ||
| 127 | } | ||
| 128 | |||
| 129 | /** | ||
| 130 | * Reads the HostID from a previously generated configuration file. | ||
| 131 | * | ||
| 132 | * @note It is the responsibility of the calling function to free the returned host_id | ||
| 133 | * | ||
| 134 | * @return The string containing the HostID or NULL | ||
| 135 | */ | ||
| 136 | void userpref_get_host_id(char **host_id) | ||
| 137 | { | ||
| 138 | gchar *config_file; | ||
| 139 | GKeyFile *key_file; | ||
| 140 | gchar *loc_host_id; | ||
| 141 | |||
| 142 | config_file = | ||
| 143 | g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIMOBILEDEVICE_CONF_DIR, LIBIMOBILEDEVICE_CONF_FILE, NULL); | ||
| 144 | |||
| 145 | /* now parse file to get the HostID */ | ||
| 146 | key_file = g_key_file_new(); | ||
| 147 | if (g_key_file_load_from_file(key_file, config_file, G_KEY_FILE_KEEP_COMMENTS, NULL)) { | ||
| 148 | loc_host_id = g_key_file_get_value(key_file, "Global", "HostID", NULL); | ||
| 149 | if (loc_host_id) | ||
| 150 | *host_id = strdup((char *) loc_host_id); | ||
| 151 | g_free(loc_host_id); | ||
| 152 | } | ||
| 153 | g_key_file_free(key_file); | ||
| 154 | g_free(config_file); | ||
| 155 | |||
| 156 | if (!*host_id) { | ||
| 157 | /* no config, generate host_id */ | ||
| 158 | *host_id = userpref_generate_host_id(); | ||
| 159 | userpref_set_host_id(*host_id); | ||
| 160 | } | ||
| 161 | |||
| 162 | debug_info("Using %s as HostID", *host_id); | ||
| 163 | } | ||
| 164 | |||
| 165 | /** | ||
| 166 | * Determines whether this device has been connected to this system before. | ||
| 167 | * | ||
| 168 | * @param uid The device uid as given by the device. | ||
| 169 | * | ||
| 170 | * @return 1 if the device has been connected previously to this configuration | ||
| 171 | * or 0 otherwise. | ||
| 172 | */ | ||
| 173 | int userpref_has_device_public_key(const char *uuid) | ||
| 174 | { | ||
| 175 | int ret = 0; | ||
| 176 | gchar *config_file; | ||
| 177 | |||
| 178 | /* first get config file */ | ||
| 179 | gchar *device_file = g_strconcat(uuid, ".pem", NULL); | ||
| 180 | config_file = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIMOBILEDEVICE_CONF_DIR, device_file, NULL); | ||
| 181 | if (g_file_test(config_file, (G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR))) | ||
| 182 | ret = 1; | ||
| 183 | g_free(config_file); | ||
| 184 | g_free(device_file); | ||
| 185 | return ret; | ||
| 186 | } | ||
| 187 | |||
| 188 | /** | ||
| 189 | * Fills a list with UUIDs of devices that have been connected to this | ||
| 190 | * system before, i.e. for which a public key file exists. | ||
| 191 | * | ||
| 192 | * @param list A pointer to a char** initially pointing to NULL that will | ||
| 193 | * hold a newly allocated list of UUIDs upon successful return. | ||
| 194 | * The caller is responsible for freeing the memory. Note that if | ||
| 195 | * no public key file was found the list has to be freed too as it | ||
| 196 | * points to a terminating NULL element. | ||
| 197 | * @param count The number of UUIDs found. This parameter can be NULL if it | ||
| 198 | * is not required. | ||
| 199 | * | ||
| 200 | * @return USERPREF_E_SUCCESS on success, or USERPREF_E_INVALID_ARG if the | ||
| 201 | * list parameter is not pointing to NULL. | ||
| 202 | */ | ||
| 203 | userpref_error_t userpref_get_paired_uuids(char ***list, unsigned int *count) | ||
| 204 | { | ||
| 205 | GDir *config_dir; | ||
| 206 | gchar *config_path; | ||
| 207 | const gchar *dir_file; | ||
| 208 | GList *uuids = NULL; | ||
| 209 | unsigned int i; | ||
| 210 | unsigned int found = 0; | ||
| 211 | |||
| 212 | if (!list || (list && *list)) { | ||
| 213 | debug_info("ERROR: The list parameter needs to point to NULL!"); | ||
| 214 | return USERPREF_E_INVALID_ARG; | ||
| 215 | } | ||
| 216 | |||
| 217 | if (count) { | ||
| 218 | *count = 0; | ||
| 219 | } | ||
| 220 | |||
| 221 | config_path = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIMOBILEDEVICE_CONF_DIR, NULL); | ||
| 222 | |||
| 223 | config_dir = g_dir_open(config_path,0,NULL); | ||
| 224 | if (config_dir) { | ||
| 225 | while ((dir_file = g_dir_read_name(config_dir))) { | ||
| 226 | if (g_str_has_suffix(dir_file, ".pem") && (strlen(dir_file) == 44)) { | ||
| 227 | uuids = g_list_append(uuids, g_strndup(dir_file, strlen(dir_file)-4)); | ||
| 228 | found++; | ||
| 229 | } | ||
| 230 | } | ||
| 231 | g_dir_close(config_dir); | ||
| 232 | } | ||
| 233 | *list = (char**)malloc(sizeof(char*) * (found+1)); | ||
| 234 | for (i = 0; i < found; i++) { | ||
| 235 | (*list)[i] = g_list_nth_data(uuids, i); | ||
| 236 | } | ||
| 237 | (*list)[i] = NULL; | ||
| 238 | |||
| 239 | if (count) { | ||
| 240 | *count = found; | ||
| 241 | } | ||
| 242 | g_list_free(uuids); | ||
| 243 | g_free(config_path); | ||
| 244 | |||
| 245 | return USERPREF_E_SUCCESS; | ||
| 246 | } | ||
| 247 | |||
| 248 | /** | ||
| 249 | * Mark the device (as represented by the key) as having connected to this | ||
| 250 | * configuration. | ||
| 251 | * | ||
| 252 | * @param public_key The public key given by the device | ||
| 253 | * | ||
| 254 | * @return 1 on success and 0 if no public key is given or if it has already | ||
| 255 | * been marked as connected previously. | ||
| 256 | */ | ||
| 257 | userpref_error_t userpref_set_device_public_key(const char *uuid, gnutls_datum_t public_key) | ||
| 258 | { | ||
| 259 | if (NULL == public_key.data) | ||
| 260 | return USERPREF_E_INVALID_ARG; | ||
| 261 | |||
| 262 | if (userpref_has_device_public_key(uuid)) | ||
| 263 | return USERPREF_E_SUCCESS; | ||
| 264 | |||
| 265 | /* ensure config directory exists */ | ||
| 266 | userpref_create_config_dir(); | ||
| 267 | |||
| 268 | /* build file path */ | ||
| 269 | gchar *device_file = g_strconcat(uuid, ".pem", NULL); | ||
| 270 | gchar *pem = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIMOBILEDEVICE_CONF_DIR, device_file, NULL); | ||
| 271 | |||
| 272 | /* store file */ | ||
| 273 | FILE *pFile = fopen(pem, "wb"); | ||
| 274 | fwrite(public_key.data, 1, public_key.size, pFile); | ||
| 275 | fclose(pFile); | ||
| 276 | g_free(pem); | ||
| 277 | g_free(device_file); | ||
| 278 | |||
| 279 | return USERPREF_E_SUCCESS; | ||
| 280 | } | ||
| 281 | |||
| 282 | /** | ||
| 283 | * Remove the public key stored for the device with uuid from this host. | ||
| 284 | * | ||
| 285 | * @param uuid The uuid of the device | ||
| 286 | * | ||
| 287 | * @return USERPREF_E_SUCCESS on success. | ||
| 288 | */ | ||
| 289 | userpref_error_t userpref_remove_device_public_key(const char *uuid) | ||
| 290 | { | ||
| 291 | if (!userpref_has_device_public_key(uuid)) | ||
| 292 | return USERPREF_E_SUCCESS; | ||
| 293 | |||
| 294 | /* build file path */ | ||
| 295 | gchar *device_file = g_strconcat(uuid, ".pem", NULL); | ||
| 296 | gchar *pem = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIMOBILEDEVICE_CONF_DIR, device_file, NULL); | ||
| 297 | |||
| 298 | /* remove file */ | ||
| 299 | g_remove(pem); | ||
| 300 | |||
| 301 | g_free(pem); | ||
| 302 | g_free(device_file); | ||
| 303 | |||
| 304 | return USERPREF_E_SUCCESS; | ||
| 305 | } | ||
| 306 | |||
| 307 | /** | ||
| 308 | * Private function which reads the given file into a gnutls structure. | ||
| 309 | * | ||
| 310 | * @param file The filename of the file to read | ||
| 311 | * @param data The pointer at which to store the data. | ||
| 312 | * | ||
| 313 | * @return 1 if the file contents where read successfully and 0 otherwise. | ||
| 314 | */ | ||
| 315 | static int userpref_get_file_contents(const char *file, gnutls_datum_t * data) | ||
| 316 | { | ||
| 317 | gboolean success; | ||
| 318 | gsize size; | ||
| 319 | char *content; | ||
| 320 | gchar *filepath; | ||
| 321 | |||
| 322 | if (NULL == file || NULL == data) | ||
| 323 | return 0; | ||
| 324 | |||
| 325 | /* Read file */ | ||
| 326 | filepath = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIMOBILEDEVICE_CONF_DIR, file, NULL); | ||
| 327 | success = g_file_get_contents(filepath, &content, &size, NULL); | ||
| 328 | g_free(filepath); | ||
| 329 | |||
| 330 | /* Add it to the gnutls_datnum_t structure */ | ||
| 331 | data->data = (uint8_t*) content; | ||
| 332 | data->size = size; | ||
| 333 | |||
| 334 | return success; | ||
| 335 | } | ||
| 336 | |||
| 337 | /** | ||
| 338 | * Private function which generate private keys and certificates. | ||
| 339 | * | ||
| 340 | * @return 1 if keys were successfully generated, 0 otherwise | ||
| 341 | */ | ||
| 342 | static userpref_error_t userpref_gen_keys_and_cert(void) | ||
| 343 | { | ||
| 344 | userpref_error_t ret = USERPREF_E_SSL_ERROR; | ||
| 345 | |||
| 346 | gnutls_x509_privkey_t root_privkey; | ||
| 347 | gnutls_x509_crt_t root_cert; | ||
| 348 | gnutls_x509_privkey_t host_privkey; | ||
| 349 | gnutls_x509_crt_t host_cert; | ||
| 350 | |||
| 351 | gnutls_global_deinit(); | ||
| 352 | gnutls_global_init(); | ||
| 353 | |||
| 354 | //use less secure random to speed up key generation | ||
| 355 | gcry_control(GCRYCTL_ENABLE_QUICK_RANDOM); | ||
| 356 | |||
| 357 | gnutls_x509_privkey_init(&root_privkey); | ||
| 358 | gnutls_x509_privkey_init(&host_privkey); | ||
| 359 | |||
| 360 | gnutls_x509_crt_init(&root_cert); | ||
| 361 | gnutls_x509_crt_init(&host_cert); | ||
| 362 | |||
| 363 | /* generate root key */ | ||
| 364 | gnutls_x509_privkey_generate(root_privkey, GNUTLS_PK_RSA, 2048, 0); | ||
| 365 | gnutls_x509_privkey_generate(host_privkey, GNUTLS_PK_RSA, 2048, 0); | ||
| 366 | |||
| 367 | /* generate certificates */ | ||
| 368 | gnutls_x509_crt_set_key(root_cert, root_privkey); | ||
| 369 | gnutls_x509_crt_set_serial(root_cert, "\x00", 1); | ||
| 370 | gnutls_x509_crt_set_version(root_cert, 3); | ||
| 371 | gnutls_x509_crt_set_ca_status(root_cert, 1); | ||
| 372 | gnutls_x509_crt_set_activation_time(root_cert, time(NULL)); | ||
| 373 | gnutls_x509_crt_set_expiration_time(root_cert, time(NULL) + (60 * 60 * 24 * 365 * 10)); | ||
| 374 | gnutls_x509_crt_sign(root_cert, root_cert, root_privkey); | ||
| 375 | |||
| 376 | gnutls_x509_crt_set_key(host_cert, host_privkey); | ||
| 377 | gnutls_x509_crt_set_serial(host_cert, "\x00", 1); | ||
| 378 | gnutls_x509_crt_set_version(host_cert, 3); | ||
| 379 | gnutls_x509_crt_set_ca_status(host_cert, 0); | ||
| 380 | gnutls_x509_crt_set_key_usage(host_cert, GNUTLS_KEY_KEY_ENCIPHERMENT | GNUTLS_KEY_DIGITAL_SIGNATURE); | ||
| 381 | gnutls_x509_crt_set_activation_time(host_cert, time(NULL)); | ||
| 382 | gnutls_x509_crt_set_expiration_time(host_cert, time(NULL) + (60 * 60 * 24 * 365 * 10)); | ||
| 383 | gnutls_x509_crt_sign(host_cert, root_cert, root_privkey); | ||
| 384 | |||
| 385 | /* export to PEM format */ | ||
| 386 | size_t root_key_export_size = 0; | ||
| 387 | size_t host_key_export_size = 0; | ||
| 388 | gnutls_datum_t root_key_pem = { NULL, 0 }; | ||
| 389 | gnutls_datum_t host_key_pem = { NULL, 0 }; | ||
| 390 | |||
| 391 | gnutls_x509_privkey_export(root_privkey, GNUTLS_X509_FMT_PEM, NULL, &root_key_export_size); | ||
| 392 | gnutls_x509_privkey_export(host_privkey, GNUTLS_X509_FMT_PEM, NULL, &host_key_export_size); | ||
| 393 | |||
| 394 | root_key_pem.data = gnutls_malloc(root_key_export_size); | ||
| 395 | host_key_pem.data = gnutls_malloc(host_key_export_size); | ||
| 396 | |||
| 397 | gnutls_x509_privkey_export(root_privkey, GNUTLS_X509_FMT_PEM, root_key_pem.data, &root_key_export_size); | ||
| 398 | root_key_pem.size = root_key_export_size; | ||
| 399 | gnutls_x509_privkey_export(host_privkey, GNUTLS_X509_FMT_PEM, host_key_pem.data, &host_key_export_size); | ||
| 400 | host_key_pem.size = host_key_export_size; | ||
| 401 | |||
| 402 | size_t root_cert_export_size = 0; | ||
| 403 | size_t host_cert_export_size = 0; | ||
| 404 | gnutls_datum_t root_cert_pem = { NULL, 0 }; | ||
| 405 | gnutls_datum_t host_cert_pem = { NULL, 0 }; | ||
| 406 | |||
| 407 | gnutls_x509_crt_export(root_cert, GNUTLS_X509_FMT_PEM, NULL, &root_cert_export_size); | ||
| 408 | gnutls_x509_crt_export(host_cert, GNUTLS_X509_FMT_PEM, NULL, &host_cert_export_size); | ||
| 409 | |||
| 410 | root_cert_pem.data = gnutls_malloc(root_cert_export_size); | ||
| 411 | host_cert_pem.data = gnutls_malloc(host_cert_export_size); | ||
| 412 | |||
| 413 | gnutls_x509_crt_export(root_cert, GNUTLS_X509_FMT_PEM, root_cert_pem.data, &root_cert_export_size); | ||
| 414 | root_cert_pem.size = root_cert_export_size; | ||
| 415 | gnutls_x509_crt_export(host_cert, GNUTLS_X509_FMT_PEM, host_cert_pem.data, &host_cert_export_size); | ||
| 416 | host_cert_pem.size = host_cert_export_size; | ||
| 417 | |||
| 418 | if (NULL != root_cert_pem.data && 0 != root_cert_pem.size && | ||
| 419 | NULL != host_cert_pem.data && 0 != host_cert_pem.size) | ||
| 420 | ret = USERPREF_E_SUCCESS; | ||
| 421 | |||
| 422 | /* store values in config file */ | ||
| 423 | userpref_set_keys_and_certs( &root_key_pem, &root_cert_pem, &host_key_pem, &host_cert_pem); | ||
| 424 | |||
| 425 | gnutls_free(root_key_pem.data); | ||
| 426 | gnutls_free(root_cert_pem.data); | ||
| 427 | gnutls_free(host_key_pem.data); | ||
| 428 | gnutls_free(host_cert_pem.data); | ||
| 429 | |||
| 430 | //restore gnutls env | ||
| 431 | gnutls_global_deinit(); | ||
| 432 | gnutls_global_init(); | ||
| 433 | |||
| 434 | return ret; | ||
| 435 | } | ||
| 436 | |||
| 437 | /** | ||
| 438 | * Private function which import the given key into a gnutls structure. | ||
| 439 | * | ||
| 440 | * @param key_name The filename of the private key to import. | ||
| 441 | * @param key the gnutls key structure. | ||
| 442 | * | ||
| 443 | * @return 1 if the key was successfully imported. | ||
| 444 | */ | ||
| 445 | static userpref_error_t userpref_import_key(const char* key_name, gnutls_x509_privkey_t key) | ||
| 446 | { | ||
| 447 | userpref_error_t ret = USERPREF_E_INVALID_CONF; | ||
| 448 | gnutls_datum_t pem_key = { NULL, 0 }; | ||
| 449 | |||
| 450 | if (userpref_get_file_contents(key_name, &pem_key)) { | ||
| 451 | if (GNUTLS_E_SUCCESS == gnutls_x509_privkey_import(key, &pem_key, GNUTLS_X509_FMT_PEM)) | ||
| 452 | ret = USERPREF_E_SUCCESS; | ||
| 453 | else | ||
| 454 | ret = USERPREF_E_SSL_ERROR; | ||
| 455 | } | ||
| 456 | gnutls_free(pem_key.data); | ||
| 457 | return ret; | ||
| 458 | } | ||
| 459 | |||
| 460 | /** | ||
| 461 | * Private function which import the given certificate into a gnutls structure. | ||
| 462 | * | ||
| 463 | * @param crt_name The filename of the certificate to import. | ||
| 464 | * @param cert the gnutls certificate structure. | ||
| 465 | * | ||
| 466 | * @return IDEVICE_E_SUCCESS if the certificate was successfully imported. | ||
| 467 | */ | ||
| 468 | static userpref_error_t userpref_import_crt(const char* crt_name, gnutls_x509_crt_t cert) | ||
| 469 | { | ||
| 470 | userpref_error_t ret = USERPREF_E_INVALID_CONF; | ||
| 471 | gnutls_datum_t pem_cert = { NULL, 0 }; | ||
| 472 | |||
| 473 | if (userpref_get_file_contents(crt_name, &pem_cert)) { | ||
| 474 | if (GNUTLS_E_SUCCESS == gnutls_x509_crt_import(cert, &pem_cert, GNUTLS_X509_FMT_PEM)) | ||
| 475 | ret = USERPREF_E_SUCCESS; | ||
| 476 | else | ||
| 477 | ret = USERPREF_E_SSL_ERROR; | ||
| 478 | } | ||
| 479 | gnutls_free(pem_cert.data); | ||
| 480 | return ret; | ||
| 481 | } | ||
| 482 | |||
| 483 | /** | ||
| 484 | * Function to retrieve host keys and certificates. | ||
| 485 | * This function trigger key generation if they do not exists yet or are invalid. | ||
| 486 | * | ||
| 487 | * @note This function can take few seconds to complete (typically 5 seconds) | ||
| 488 | * | ||
| 489 | * @param root_privkey The root private key. | ||
| 490 | * @param root_crt The root certificate. | ||
| 491 | * @param host_privkey The host private key. | ||
| 492 | * @param host_crt The host certificate. | ||
| 493 | * | ||
| 494 | * @return 1 if the keys and certificates were successfully retrieved, 0 otherwise | ||
| 495 | */ | ||
| 496 | userpref_error_t userpref_get_keys_and_certs(gnutls_x509_privkey_t root_privkey, gnutls_x509_crt_t root_crt, gnutls_x509_privkey_t host_privkey, gnutls_x509_crt_t host_crt) | ||
| 497 | { | ||
| 498 | userpref_error_t ret = USERPREF_E_SUCCESS; | ||
| 499 | |||
| 500 | if (ret == USERPREF_E_SUCCESS) | ||
| 501 | ret = userpref_import_key(LIBIMOBILEDEVICE_ROOT_PRIVKEY, root_privkey); | ||
| 502 | |||
| 503 | if (ret == USERPREF_E_SUCCESS) | ||
| 504 | ret = userpref_import_key(LIBIMOBILEDEVICE_HOST_PRIVKEY, host_privkey); | ||
| 505 | |||
| 506 | if (ret == USERPREF_E_SUCCESS) | ||
| 507 | ret = userpref_import_crt(LIBIMOBILEDEVICE_ROOT_CERTIF, root_crt); | ||
| 508 | |||
| 509 | if (ret == USERPREF_E_SUCCESS) | ||
| 510 | ret = userpref_import_crt(LIBIMOBILEDEVICE_HOST_CERTIF, host_crt); | ||
| 511 | |||
| 512 | |||
| 513 | if (USERPREF_E_SUCCESS != ret) { | ||
| 514 | //we had problem reading or importing root cert | ||
| 515 | //try with a new ones. | ||
| 516 | ret = userpref_gen_keys_and_cert(); | ||
| 517 | |||
| 518 | if (ret == USERPREF_E_SUCCESS) | ||
| 519 | ret = userpref_import_key(LIBIMOBILEDEVICE_ROOT_PRIVKEY, root_privkey); | ||
| 520 | |||
| 521 | if (ret == USERPREF_E_SUCCESS) | ||
| 522 | ret = userpref_import_key(LIBIMOBILEDEVICE_HOST_PRIVKEY, host_privkey); | ||
| 523 | |||
| 524 | if (ret == USERPREF_E_SUCCESS) | ||
| 525 | ret = userpref_import_crt(LIBIMOBILEDEVICE_ROOT_CERTIF, root_crt); | ||
| 526 | |||
| 527 | if (ret == USERPREF_E_SUCCESS) | ||
| 528 | ret = userpref_import_crt(LIBIMOBILEDEVICE_HOST_CERTIF, host_crt); | ||
| 529 | } | ||
| 530 | |||
| 531 | return ret; | ||
| 532 | } | ||
| 533 | |||
| 534 | /** | ||
| 535 | * Function to retrieve certificates encoded in PEM format. | ||
| 536 | * | ||
| 537 | * @param pem_root_cert The root certificate. | ||
| 538 | * @param pem_host_cert The host certificate. | ||
| 539 | * | ||
| 540 | * @return 1 if the certificates were successfully retrieved, 0 otherwise | ||
| 541 | */ | ||
| 542 | userpref_error_t userpref_get_certs_as_pem(gnutls_datum_t *pem_root_cert, gnutls_datum_t *pem_host_cert) | ||
| 543 | { | ||
| 544 | if (!pem_root_cert || !pem_host_cert) | ||
| 545 | return USERPREF_E_INVALID_ARG; | ||
| 546 | |||
| 547 | if (userpref_get_file_contents(LIBIMOBILEDEVICE_ROOT_CERTIF, pem_root_cert) && userpref_get_file_contents(LIBIMOBILEDEVICE_HOST_CERTIF, pem_host_cert)) | ||
| 548 | return USERPREF_E_SUCCESS; | ||
| 549 | else { | ||
| 550 | g_free(pem_root_cert->data); | ||
| 551 | g_free(pem_host_cert->data); | ||
| 552 | } | ||
| 553 | return USERPREF_E_INVALID_CONF; | ||
| 554 | } | ||
| 555 | |||
| 556 | /** | ||
| 557 | * Create and save a configuration file containing the given data. | ||
| 558 | * | ||
| 559 | * @note: All fields must specified and be non-null | ||
| 560 | * | ||
| 561 | * @param root_key The root key | ||
| 562 | * @param root_cert The root certificate | ||
| 563 | * @param host_key The host key | ||
| 564 | * @param host_cert The host certificate | ||
| 565 | * | ||
| 566 | * @return 1 on success and 0 otherwise. | ||
| 567 | */ | ||
| 568 | userpref_error_t userpref_set_keys_and_certs(gnutls_datum_t * root_key, gnutls_datum_t * root_cert, gnutls_datum_t * host_key, gnutls_datum_t * host_cert) | ||
| 569 | { | ||
| 570 | FILE *pFile; | ||
| 571 | gchar *pem; | ||
| 572 | |||
| 573 | if (!root_key || !host_key || !root_cert || !host_cert) | ||
| 574 | return USERPREF_E_INVALID_ARG; | ||
| 575 | |||
| 576 | /* Make sure config directory exists */ | ||
| 577 | userpref_create_config_dir(); | ||
| 578 | |||
| 579 | /* Now write keys and certificates to disk */ | ||
| 580 | pem = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIMOBILEDEVICE_CONF_DIR, LIBIMOBILEDEVICE_ROOT_PRIVKEY, NULL); | ||
| 581 | pFile = fopen(pem, "wb"); | ||
| 582 | fwrite(root_key->data, 1, root_key->size, pFile); | ||
| 583 | fclose(pFile); | ||
| 584 | g_free(pem); | ||
| 585 | |||
| 586 | pem = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIMOBILEDEVICE_CONF_DIR, LIBIMOBILEDEVICE_HOST_PRIVKEY, NULL); | ||
| 587 | pFile = fopen(pem, "wb"); | ||
| 588 | fwrite(host_key->data, 1, host_key->size, pFile); | ||
| 589 | fclose(pFile); | ||
| 590 | g_free(pem); | ||
| 591 | |||
| 592 | pem = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIMOBILEDEVICE_CONF_DIR, LIBIMOBILEDEVICE_ROOT_CERTIF, NULL); | ||
| 593 | pFile = fopen(pem, "wb"); | ||
| 594 | fwrite(root_cert->data, 1, root_cert->size, pFile); | ||
| 595 | fclose(pFile); | ||
| 596 | g_free(pem); | ||
| 597 | |||
| 598 | pem = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIMOBILEDEVICE_CONF_DIR, LIBIMOBILEDEVICE_HOST_CERTIF, NULL); | ||
| 599 | pFile = fopen(pem, "wb"); | ||
| 600 | fwrite(host_cert->data, 1, host_cert->size, pFile); | ||
| 601 | fclose(pFile); | ||
| 602 | g_free(pem); | ||
| 603 | |||
| 604 | return USERPREF_E_SUCCESS; | ||
| 605 | } | ||
