summaryrefslogtreecommitdiffstats
path: root/src/initconf.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/initconf.c')
-rw-r--r--src/initconf.c185
1 files changed, 0 insertions, 185 deletions
diff --git a/src/initconf.c b/src/initconf.c
deleted file mode 100644
index 205c97a..0000000
--- a/src/initconf.c
+++ /dev/null
@@ -1,185 +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 <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <gnutls/gnutls.h>
26#include <gnutls/x509.h>
27#include <glib.h>
28
29#include "libiphone/libiphone.h"
30#include "userpref.h"
31#include "lockdown.h"
32#include "utils.h"
33
34/** Generates a 2048 byte key, split into a function so that it can be run in a
35 * thread.
36 *
37 * @param key The pointer to the desired location of the new key.
38 */
39static void generate_key(gpointer key)
40{
41 gnutls_x509_privkey_generate(*((gnutls_x509_privkey_t *) key), GNUTLS_PK_RSA, 2048, 0);
42 g_thread_exit(0);
43}
44
45/** Simple function that generates a spinner until the mutex is released.
46 */
47static void progress_bar(gpointer mutex)
48{
49 const char *spinner = "|/-\\|/-\\";
50 int i = 0;
51
52 while (!g_static_mutex_trylock((GStaticMutex *) mutex)) {
53 usleep(500000);
54 printf("Generating key... %c\r", spinner[i++]);
55 fflush(stdout);
56 if (i > 8)
57 i = 0;
58 }
59 printf("Generating key... done\n");
60 g_thread_exit(0);
61}
62
63int main(int argc, char *argv[])
64{
65 GThread *progress_thread, *key_thread;
66 GError *err;
67 static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
68 char *host_id = NULL;
69 gnutls_x509_privkey_t root_privkey;
70 gnutls_x509_privkey_t host_privkey;
71 gnutls_x509_crt_t root_cert;
72 gnutls_x509_crt_t host_cert;
73
74 iphone_set_debug(1);
75
76 // Create the thread
77 if (!g_thread_supported()) {
78 g_thread_init(NULL);
79 }
80 gnutls_global_init();
81
82 printf("This program generates keys required to connect with the iPhone\n");
83 printf("It only needs to be run ONCE.\n\n");
84 printf("Additionally it may take several minutes to run, please be patient.\n\n");
85
86
87 gnutls_x509_privkey_init(&root_privkey);
88 gnutls_x509_privkey_init(&host_privkey);
89
90 gnutls_x509_crt_init(&root_cert);
91 gnutls_x509_crt_init(&host_cert);
92
93 /* generate HostID */
94 host_id = lockdownd_generate_hostid();
95
96 /* generate root key */
97 g_static_mutex_lock(&mutex);
98 if ((key_thread = g_thread_create((GThreadFunc) generate_key, &root_privkey, TRUE, &err)) == NULL) {
99 printf("Thread create failed: %s!!\n", err->message);
100 g_error_free(err);
101 }
102 if ((progress_thread = g_thread_create((GThreadFunc) progress_bar, &mutex, TRUE, &err)) == NULL) {
103 printf("Thread create failed: %s!!\n", err->message);
104 g_error_free(err);
105 }
106 g_thread_join(key_thread);
107 g_static_mutex_unlock(&mutex);
108 g_thread_join(progress_thread);
109
110 /* generate host key */
111 g_static_mutex_init(&mutex);
112 g_static_mutex_lock(&mutex);
113 if ((key_thread = g_thread_create((GThreadFunc) generate_key, &host_privkey, TRUE, &err)) == NULL) {
114 printf("Thread create failed: %s!!\n", err->message);
115 g_error_free(err);
116 }
117 if ((progress_thread = g_thread_create((GThreadFunc) progress_bar, &mutex, TRUE, &err)) == NULL) {
118 printf("Thread create failed: %s!!\n", err->message);
119 g_error_free(err);
120 }
121 g_thread_join(key_thread);
122 g_static_mutex_unlock(&mutex);
123 g_thread_join(progress_thread);
124
125 /* generate certificates */
126 gnutls_x509_crt_set_key(root_cert, root_privkey);
127 gnutls_x509_crt_set_serial(root_cert, "\x00", 1);
128 gnutls_x509_crt_set_version(root_cert, 3);
129 gnutls_x509_crt_set_ca_status(root_cert, 1);
130 gnutls_x509_crt_set_activation_time(root_cert, time(NULL));
131 gnutls_x509_crt_set_expiration_time(root_cert, time(NULL) + (60 * 60 * 24 * 365 * 10));
132 gnutls_x509_crt_sign(root_cert, root_cert, root_privkey);
133
134
135 gnutls_x509_crt_set_key(host_cert, host_privkey);
136 gnutls_x509_crt_set_serial(host_cert, "\x00", 1);
137 gnutls_x509_crt_set_version(host_cert, 3);
138 gnutls_x509_crt_set_ca_status(host_cert, 0);
139 gnutls_x509_crt_set_key_usage(host_cert, GNUTLS_KEY_KEY_ENCIPHERMENT | GNUTLS_KEY_DIGITAL_SIGNATURE);
140 gnutls_x509_crt_set_activation_time(host_cert, time(NULL));
141 gnutls_x509_crt_set_expiration_time(host_cert, time(NULL) + (60 * 60 * 24 * 365 * 10));
142 gnutls_x509_crt_sign(host_cert, root_cert, root_privkey);
143
144
145 /* export to PEM format */
146 gnutls_datum_t root_key_pem = { NULL, 0 };
147 gnutls_datum_t host_key_pem = { NULL, 0 };
148
149 gnutls_x509_privkey_export(root_privkey, GNUTLS_X509_FMT_PEM, NULL, &root_key_pem.size);
150 gnutls_x509_privkey_export(host_privkey, GNUTLS_X509_FMT_PEM, NULL, &host_key_pem.size);
151
152 root_key_pem.data = gnutls_malloc(root_key_pem.size);
153 host_key_pem.data = gnutls_malloc(host_key_pem.size);
154
155 gnutls_x509_privkey_export(root_privkey, GNUTLS_X509_FMT_PEM, root_key_pem.data, &root_key_pem.size);
156 gnutls_x509_privkey_export(host_privkey, GNUTLS_X509_FMT_PEM, host_key_pem.data, &host_key_pem.size);
157
158 gnutls_datum_t root_cert_pem = { NULL, 0 };
159 gnutls_datum_t host_cert_pem = { NULL, 0 };
160
161 gnutls_x509_crt_export(root_cert, GNUTLS_X509_FMT_PEM, NULL, &root_cert_pem.size);
162 gnutls_x509_crt_export(host_cert, GNUTLS_X509_FMT_PEM, NULL, &host_cert_pem.size);
163
164 root_cert_pem.data = gnutls_malloc(root_cert_pem.size);
165 host_cert_pem.data = gnutls_malloc(host_cert_pem.size);
166
167 printf("Generating root certificate...");
168 gnutls_x509_crt_export(root_cert, GNUTLS_X509_FMT_PEM, root_cert_pem.data, &root_cert_pem.size);
169 printf("done\n");
170
171 printf("Generating host certificate...");
172 gnutls_x509_crt_export(host_cert, GNUTLS_X509_FMT_PEM, host_cert_pem.data, &host_cert_pem.size);
173 printf("done\n");
174
175
176 /* store values in config file */
177 init_config_file(host_id, &root_key_pem, &host_key_pem, &root_cert_pem, &host_cert_pem);
178
179 gnutls_free(root_key_pem.data);
180 gnutls_free(host_key_pem.data);
181 gnutls_free(root_cert_pem.data);
182 gnutls_free(host_cert_pem.data);
183
184 return 0;
185}