From 18d1ee3b0f17325fdffe0cf3e2770a3f0f45a1b9 Mon Sep 17 00:00:00 2001 From: Jonathan Beck Date: Thu, 11 Dec 2008 23:03:21 +0100 Subject: move stuff around to make code more organized. --- src/initconf.c | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) (limited to 'src/initconf.c') diff --git a/src/initconf.c b/src/initconf.c index 8aca2a6..00d78e2 100644 --- a/src/initconf.c +++ b/src/initconf.c @@ -28,7 +28,6 @@ #include "libiphone/libiphone.h" #include "userpref.h" -#include "lockdown.h" #include "utils.h" /** Generates a 2048 byte key, split into a function so that it can be run in a @@ -60,6 +59,35 @@ void progress_bar(gpointer mutex) g_thread_exit(0); } +int get_rand(int min, int max) +{ + int retval = (rand() % (max - min)) + min; + return retval; +} + +/** Generates a valid HostID (which is actually a UUID). + * + * @param A null terminated string containing a valid HostID. + */ +char *lockdownd_generate_hostid() +{ + char *hostid = (char *) malloc(sizeof(char) * 37); // HostID's are just UUID's, and UUID's are 36 characters long + const char *chars = "ABCDEF0123456789"; + srand(time(NULL)); + int i = 0; + + for (i = 0; i < 36; i++) { + if (i == 8 || i == 13 || i == 18 || i == 23) { + hostid[i] = '-'; + continue; + } else { + hostid[i] = chars[get_rand(0, 16)]; + } + } + hostid[36] = '\0'; // make it a real string + return hostid; +} + int main(int argc, char *argv[]) { GThread *progress_thread, *key_thread; -- cgit v1.1-32-gdbae From f893e8a9e2cc197522f838b3f2bbec8862953c2f Mon Sep 17 00:00:00 2001 From: Jonathan Beck Date: Sun, 12 Apr 2009 16:08:06 +0200 Subject: Use less secure random number generation so we can generate private keys on the fly. Drop libiphone-initconf. --- src/initconf.c | 213 --------------------------------------------------------- 1 file changed, 213 deletions(-) delete mode 100644 src/initconf.c (limited to 'src/initconf.c') diff --git a/src/initconf.c b/src/initconf.c deleted file mode 100644 index 538f344..0000000 --- a/src/initconf.c +++ /dev/null @@ -1,213 +0,0 @@ -/* - * userpref.c - * contains methods to access user specific certificates IDs and more. - * - * Copyright (c) 2008 Jonathan Beck All Rights Reserved. - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - */ - -#include -#include -#include -#include -#include -#include - -#include "libiphone/libiphone.h" -#include "userpref.h" -#include "utils.h" - -/** Generates a 2048 byte key, split into a function so that it can be run in a - * thread. - * - * @param key The pointer to the desired location of the new key. - */ -static void generate_key(gpointer key) -{ - gnutls_x509_privkey_generate(*((gnutls_x509_privkey_t *) key), GNUTLS_PK_RSA, 2048, 0); - g_thread_exit(0); -} - -/** Simple function that generates a spinner until the mutex is released. - */ -static void progress_bar(gpointer mutex) -{ - const char *spinner = "|/-\\|/-\\"; - int i = 0; - - while (!g_static_mutex_trylock((GStaticMutex *) mutex)) { - usleep(500000); - printf("Generating key... %c\r", spinner[i++]); - fflush(stdout); - if (i > 8) - i = 0; - } - printf("Generating key... done\n"); - g_thread_exit(0); -} - -int get_rand(int min, int max) -{ - int retval = (rand() % (max - min)) + min; - return retval; -} - -/** Generates a valid HostID (which is actually a UUID). - * - * @param A null terminated string containing a valid HostID. - */ -char *lockdownd_generate_hostid() -{ - char *hostid = (char *) malloc(sizeof(char) * 37); // HostID's are just UUID's, and UUID's are 36 characters long - const char *chars = "ABCDEF0123456789"; - srand(time(NULL)); - int i = 0; - - for (i = 0; i < 36; i++) { - if (i == 8 || i == 13 || i == 18 || i == 23) { - hostid[i] = '-'; - continue; - } else { - hostid[i] = chars[get_rand(0, 16)]; - } - } - hostid[36] = '\0'; // make it a real string - return hostid; -} - -int main(int argc, char *argv[]) -{ - GThread *progress_thread, *key_thread; - GError *err; - static GStaticMutex mutex = G_STATIC_MUTEX_INIT; - char *host_id = NULL; - gnutls_x509_privkey_t root_privkey; - gnutls_x509_privkey_t host_privkey; - gnutls_x509_crt_t root_cert; - gnutls_x509_crt_t host_cert; - - iphone_set_debug(1); - - // Create the thread - if (!g_thread_supported()) { - g_thread_init(NULL); - } - gnutls_global_init(); - - printf("This program generates keys required to connect with the iPhone\n"); - printf("It only needs to be run ONCE.\n\n"); - printf("Additionally it may take several minutes to run, please be patient.\n\n"); - - - gnutls_x509_privkey_init(&root_privkey); - gnutls_x509_privkey_init(&host_privkey); - - gnutls_x509_crt_init(&root_cert); - gnutls_x509_crt_init(&host_cert); - - /* generate HostID */ - host_id = lockdownd_generate_hostid(); - - /* generate root key */ - g_static_mutex_lock(&mutex); - if ((key_thread = g_thread_create((GThreadFunc) generate_key, &root_privkey, TRUE, &err)) == NULL) { - printf("Thread create failed: %s!!\n", err->message); - g_error_free(err); - } - if ((progress_thread = g_thread_create((GThreadFunc) progress_bar, &mutex, TRUE, &err)) == NULL) { - printf("Thread create failed: %s!!\n", err->message); - g_error_free(err); - } - g_thread_join(key_thread); - g_static_mutex_unlock(&mutex); - g_thread_join(progress_thread); - - /* generate host key */ - g_static_mutex_init(&mutex); - g_static_mutex_lock(&mutex); - if ((key_thread = g_thread_create((GThreadFunc) generate_key, &host_privkey, TRUE, &err)) == NULL) { - printf("Thread create failed: %s!!\n", err->message); - g_error_free(err); - } - if ((progress_thread = g_thread_create((GThreadFunc) progress_bar, &mutex, TRUE, &err)) == NULL) { - printf("Thread create failed: %s!!\n", err->message); - g_error_free(err); - } - g_thread_join(key_thread); - g_static_mutex_unlock(&mutex); - g_thread_join(progress_thread); - - /* generate certificates */ - gnutls_x509_crt_set_key(root_cert, root_privkey); - gnutls_x509_crt_set_serial(root_cert, "\x00", 1); - gnutls_x509_crt_set_version(root_cert, 3); - gnutls_x509_crt_set_ca_status(root_cert, 1); - gnutls_x509_crt_set_activation_time(root_cert, time(NULL)); - gnutls_x509_crt_set_expiration_time(root_cert, time(NULL) + (60 * 60 * 24 * 365 * 10)); - gnutls_x509_crt_sign(root_cert, root_cert, root_privkey); - - - gnutls_x509_crt_set_key(host_cert, host_privkey); - gnutls_x509_crt_set_serial(host_cert, "\x00", 1); - gnutls_x509_crt_set_version(host_cert, 3); - gnutls_x509_crt_set_ca_status(host_cert, 0); - gnutls_x509_crt_set_key_usage(host_cert, GNUTLS_KEY_KEY_ENCIPHERMENT | GNUTLS_KEY_DIGITAL_SIGNATURE); - gnutls_x509_crt_set_activation_time(host_cert, time(NULL)); - gnutls_x509_crt_set_expiration_time(host_cert, time(NULL) + (60 * 60 * 24 * 365 * 10)); - gnutls_x509_crt_sign(host_cert, root_cert, root_privkey); - - - /* export to PEM format */ - gnutls_datum_t root_key_pem = { NULL, 0 }; - gnutls_datum_t host_key_pem = { NULL, 0 }; - - gnutls_x509_privkey_export(root_privkey, GNUTLS_X509_FMT_PEM, NULL, &root_key_pem.size); - gnutls_x509_privkey_export(host_privkey, GNUTLS_X509_FMT_PEM, NULL, &host_key_pem.size); - - root_key_pem.data = gnutls_malloc(root_key_pem.size); - host_key_pem.data = gnutls_malloc(host_key_pem.size); - - gnutls_x509_privkey_export(root_privkey, GNUTLS_X509_FMT_PEM, root_key_pem.data, &root_key_pem.size); - gnutls_x509_privkey_export(host_privkey, GNUTLS_X509_FMT_PEM, host_key_pem.data, &host_key_pem.size); - - gnutls_datum_t root_cert_pem = { NULL, 0 }; - gnutls_datum_t host_cert_pem = { NULL, 0 }; - - gnutls_x509_crt_export(root_cert, GNUTLS_X509_FMT_PEM, NULL, &root_cert_pem.size); - gnutls_x509_crt_export(host_cert, GNUTLS_X509_FMT_PEM, NULL, &host_cert_pem.size); - - root_cert_pem.data = gnutls_malloc(root_cert_pem.size); - host_cert_pem.data = gnutls_malloc(host_cert_pem.size); - - printf("Generating root certificate..."); - gnutls_x509_crt_export(root_cert, GNUTLS_X509_FMT_PEM, root_cert_pem.data, &root_cert_pem.size); - printf("done\n"); - - printf("Generating host certificate..."); - gnutls_x509_crt_export(host_cert, GNUTLS_X509_FMT_PEM, host_cert_pem.data, &host_cert_pem.size); - printf("done\n"); - - - /* store values in config file */ - init_config_file(host_id, &root_key_pem, &host_key_pem, &root_cert_pem, &host_cert_pem); - - gnutls_free(root_key_pem.data); - gnutls_free(host_key_pem.data); - gnutls_free(root_cert_pem.data); - gnutls_free(host_cert_pem.data); - - return 0; -} -- cgit v1.1-32-gdbae