summaryrefslogtreecommitdiffstats
path: root/src/initconf.c
diff options
context:
space:
mode:
authorGravatar Jonathan Beck2009-04-12 16:08:06 +0200
committerGravatar Jonathan Beck2009-04-12 16:08:06 +0200
commitf893e8a9e2cc197522f838b3f2bbec8862953c2f (patch)
tree9a496fe32f376f860e266916d2bdc4bd9485bc26 /src/initconf.c
parent86289b387daebfa5607435a76b15848bcb46916e (diff)
downloadlibimobiledevice-f893e8a9e2cc197522f838b3f2bbec8862953c2f.tar.gz
libimobiledevice-f893e8a9e2cc197522f838b3f2bbec8862953c2f.tar.bz2
Use less secure random number generation so we can generate private
keys on the fly. Drop libiphone-initconf.
Diffstat (limited to 'src/initconf.c')
-rw-r--r--src/initconf.c213
1 files changed, 0 insertions, 213 deletions
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 @@
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 "utils.h"
32
33/** Generates a 2048 byte key, split into a function so that it can be run in a
34 * thread.
35 *
36 * @param key The pointer to the desired location of the new key.
37 */
38static void generate_key(gpointer key)
39{
40 gnutls_x509_privkey_generate(*((gnutls_x509_privkey_t *) key), GNUTLS_PK_RSA, 2048, 0);
41 g_thread_exit(0);
42}
43
44/** Simple function that generates a spinner until the mutex is released.
45 */
46static void progress_bar(gpointer mutex)
47{
48 const char *spinner = "|/-\\|/-\\";
49 int i = 0;
50
51 while (!g_static_mutex_trylock((GStaticMutex *) mutex)) {
52 usleep(500000);
53 printf("Generating key... %c\r", spinner[i++]);
54 fflush(stdout);
55 if (i > 8)
56 i = 0;
57 }
58 printf("Generating key... done\n");
59 g_thread_exit(0);
60}
61
62int get_rand(int min, int max)
63{
64 int retval = (rand() % (max - min)) + min;
65 return retval;
66}
67
68/** Generates a valid HostID (which is actually a UUID).
69 *
70 * @param A null terminated string containing a valid HostID.
71 */
72char *lockdownd_generate_hostid()
73{
74 char *hostid = (char *) malloc(sizeof(char) * 37); // HostID's are just UUID's, and UUID's are 36 characters long
75 const char *chars = "ABCDEF0123456789";
76 srand(time(NULL));
77 int i = 0;
78
79 for (i = 0; i < 36; i++) {
80 if (i == 8 || i == 13 || i == 18 || i == 23) {
81 hostid[i] = '-';
82 continue;
83 } else {
84 hostid[i] = chars[get_rand(0, 16)];
85 }
86 }
87 hostid[36] = '\0'; // make it a real string
88 return hostid;
89}
90
91int main(int argc, char *argv[])
92{
93 GThread *progress_thread, *key_thread;
94 GError *err;
95 static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
96 char *host_id = NULL;
97 gnutls_x509_privkey_t root_privkey;
98 gnutls_x509_privkey_t host_privkey;
99 gnutls_x509_crt_t root_cert;
100 gnutls_x509_crt_t host_cert;
101
102 iphone_set_debug(1);
103
104 // Create the thread
105 if (!g_thread_supported()) {
106 g_thread_init(NULL);
107 }
108 gnutls_global_init();
109
110 printf("This program generates keys required to connect with the iPhone\n");
111 printf("It only needs to be run ONCE.\n\n");
112 printf("Additionally it may take several minutes to run, please be patient.\n\n");
113
114
115 gnutls_x509_privkey_init(&root_privkey);
116 gnutls_x509_privkey_init(&host_privkey);
117
118 gnutls_x509_crt_init(&root_cert);
119 gnutls_x509_crt_init(&host_cert);
120
121 /* generate HostID */
122 host_id = lockdownd_generate_hostid();
123
124 /* generate root key */
125 g_static_mutex_lock(&mutex);
126 if ((key_thread = g_thread_create((GThreadFunc) generate_key, &root_privkey, TRUE, &err)) == NULL) {
127 printf("Thread create failed: %s!!\n", err->message);
128 g_error_free(err);
129 }
130 if ((progress_thread = g_thread_create((GThreadFunc) progress_bar, &mutex, TRUE, &err)) == NULL) {
131 printf("Thread create failed: %s!!\n", err->message);
132 g_error_free(err);
133 }
134 g_thread_join(key_thread);
135 g_static_mutex_unlock(&mutex);
136 g_thread_join(progress_thread);
137
138 /* generate host key */
139 g_static_mutex_init(&mutex);
140 g_static_mutex_lock(&mutex);
141 if ((key_thread = g_thread_create((GThreadFunc) generate_key, &host_privkey, TRUE, &err)) == NULL) {
142 printf("Thread create failed: %s!!\n", err->message);
143 g_error_free(err);
144 }
145 if ((progress_thread = g_thread_create((GThreadFunc) progress_bar, &mutex, TRUE, &err)) == NULL) {
146 printf("Thread create failed: %s!!\n", err->message);
147 g_error_free(err);
148 }
149 g_thread_join(key_thread);
150 g_static_mutex_unlock(&mutex);
151 g_thread_join(progress_thread);
152
153 /* generate certificates */
154 gnutls_x509_crt_set_key(root_cert, root_privkey);
155 gnutls_x509_crt_set_serial(root_cert, "\x00", 1);
156 gnutls_x509_crt_set_version(root_cert, 3);
157 gnutls_x509_crt_set_ca_status(root_cert, 1);
158 gnutls_x509_crt_set_activation_time(root_cert, time(NULL));
159 gnutls_x509_crt_set_expiration_time(root_cert, time(NULL) + (60 * 60 * 24 * 365 * 10));
160 gnutls_x509_crt_sign(root_cert, root_cert, root_privkey);
161
162
163 gnutls_x509_crt_set_key(host_cert, host_privkey);
164 gnutls_x509_crt_set_serial(host_cert, "\x00", 1);
165 gnutls_x509_crt_set_version(host_cert, 3);
166 gnutls_x509_crt_set_ca_status(host_cert, 0);
167 gnutls_x509_crt_set_key_usage(host_cert, GNUTLS_KEY_KEY_ENCIPHERMENT | GNUTLS_KEY_DIGITAL_SIGNATURE);
168 gnutls_x509_crt_set_activation_time(host_cert, time(NULL));
169 gnutls_x509_crt_set_expiration_time(host_cert, time(NULL) + (60 * 60 * 24 * 365 * 10));
170 gnutls_x509_crt_sign(host_cert, root_cert, root_privkey);
171
172
173 /* export to PEM format */
174 gnutls_datum_t root_key_pem = { NULL, 0 };
175 gnutls_datum_t host_key_pem = { NULL, 0 };
176
177 gnutls_x509_privkey_export(root_privkey, GNUTLS_X509_FMT_PEM, NULL, &root_key_pem.size);
178 gnutls_x509_privkey_export(host_privkey, GNUTLS_X509_FMT_PEM, NULL, &host_key_pem.size);
179
180 root_key_pem.data = gnutls_malloc(root_key_pem.size);
181 host_key_pem.data = gnutls_malloc(host_key_pem.size);
182
183 gnutls_x509_privkey_export(root_privkey, GNUTLS_X509_FMT_PEM, root_key_pem.data, &root_key_pem.size);
184 gnutls_x509_privkey_export(host_privkey, GNUTLS_X509_FMT_PEM, host_key_pem.data, &host_key_pem.size);
185
186 gnutls_datum_t root_cert_pem = { NULL, 0 };
187 gnutls_datum_t host_cert_pem = { NULL, 0 };
188
189 gnutls_x509_crt_export(root_cert, GNUTLS_X509_FMT_PEM, NULL, &root_cert_pem.size);
190 gnutls_x509_crt_export(host_cert, GNUTLS_X509_FMT_PEM, NULL, &host_cert_pem.size);
191
192 root_cert_pem.data = gnutls_malloc(root_cert_pem.size);
193 host_cert_pem.data = gnutls_malloc(host_cert_pem.size);
194
195 printf("Generating root certificate...");
196 gnutls_x509_crt_export(root_cert, GNUTLS_X509_FMT_PEM, root_cert_pem.data, &root_cert_pem.size);
197 printf("done\n");
198
199 printf("Generating host certificate...");
200 gnutls_x509_crt_export(host_cert, GNUTLS_X509_FMT_PEM, host_cert_pem.data, &host_cert_pem.size);
201 printf("done\n");
202
203
204 /* store values in config file */
205 init_config_file(host_id, &root_key_pem, &host_key_pem, &root_cert_pem, &host_cert_pem);
206
207 gnutls_free(root_key_pem.data);
208 gnutls_free(host_key_pem.data);
209 gnutls_free(root_cert_pem.data);
210 gnutls_free(host_cert_pem.data);
211
212 return 0;
213}