Commit 25e85bf5 authored by Matt Colyer's avatar Matt Colyer

Zack's final changes to the pairing.

parent 4b558a53
...@@ -52,12 +52,11 @@ static int ifuse_getattr(const char *path, struct stat *stbuf) { ...@@ -52,12 +52,11 @@ static int ifuse_getattr(const char *path, struct stat *stbuf) {
file = afc_get_file_info(afc, path); file = afc_get_file_info(afc, path);
if (!file){ if (!file){
res = -ENOENT; res = -ENOENT;
return res; } else {
}
stbuf->st_mode = file->type | 0444; stbuf->st_mode = file->type | 0444;
stbuf->st_size = file->size; stbuf->st_size = file->size;
//stbuf->st_nlink = 2; //stbuf->st_nlink = 2;
}
return res; return res;
} }
......
...@@ -25,11 +25,34 @@ ...@@ -25,11 +25,34 @@
int debug = 1; int debug = 1;
int get_rand(int min, int max) {
int retval = (rand() % (max - min)) + min;
return retval;
}
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';
return hostid;
}
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
gnutls_global_init(); gnutls_global_init();
char* host_id = "29942970-207913891623273984"; char* host_id = NULL; //"29942970-207913891623273984"
gnutls_x509_privkey_t root_privkey; gnutls_x509_privkey_t root_privkey;
gnutls_x509_privkey_t host_privkey; gnutls_x509_privkey_t host_privkey;
...@@ -44,7 +67,8 @@ int main(int argc, char *argv[]) { ...@@ -44,7 +67,8 @@ int main(int argc, char *argv[]) {
/* generate HostID */ /* generate HostID */
//TODO //TODO
host_id = lockdownd_generate_hostid();
if (debug) printf("HostID: %s\n", host_id);
/* generate keys */ /* generate keys */
gnutls_x509_privkey_generate(root_privkey, GNUTLS_PK_RSA, 2048, 0); gnutls_x509_privkey_generate(root_privkey, GNUTLS_PK_RSA, 2048, 0);
gnutls_x509_privkey_generate(host_privkey, GNUTLS_PK_RSA, 2048, 0); gnutls_x509_privkey_generate(host_privkey, GNUTLS_PK_RSA, 2048, 0);
...@@ -62,7 +86,8 @@ int main(int argc, char *argv[]) { ...@@ -62,7 +86,8 @@ int main(int argc, char *argv[]) {
gnutls_x509_crt_set_key(host_cert, host_privkey); gnutls_x509_crt_set_key(host_cert, host_privkey);
gnutls_x509_crt_set_serial(host_cert, "\x00", 1); gnutls_x509_crt_set_serial(host_cert, "\x00", 1);
gnutls_x509_crt_set_version(host_cert, 3); gnutls_x509_crt_set_version(host_cert, 3);
gnutls_x509_crt_set_ca_status(host_cert, 1); 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_activation_time(host_cert, time(NULL));
gnutls_x509_crt_set_expiration_time(host_cert, time(NULL) + (60 * 60 * 24 * 365 * 10)); 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); gnutls_x509_crt_sign(host_cert, root_cert, root_privkey);
......
...@@ -39,7 +39,28 @@ const ASN1_ARRAY_TYPE pkcs1_asn1_tab[]={ ...@@ -39,7 +39,28 @@ const ASN1_ARRAY_TYPE pkcs1_asn1_tab[]={
{0,0,0} {0,0,0}
}; };
int get_rand(int min, int max) {
int retval = (rand() % (max - min)) + min;
return retval;
}
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;
}
lockdownd_client *new_lockdownd_client(iPhone *phone) { lockdownd_client *new_lockdownd_client(iPhone *phone) {
if (!phone) return NULL; if (!phone) return NULL;
...@@ -72,7 +93,7 @@ int lockdownd_recv(lockdownd_client *control, char **dump_data) { ...@@ -72,7 +93,7 @@ int lockdownd_recv(lockdownd_client *control, char **dump_data) {
char *receive; char *receive;
uint32 datalen = 0, bytes = 0; uint32 datalen = 0, bytes = 0;
if (!control->in_SSL) bytes = mux_recv(control->iphone, control->connection, (char*)&datalen, sizeof(datalen)); if (!control->in_SSL) bytes = mux_recv(control->connection, (char *)&datalen, sizeof(datalen));
else bytes = gnutls_record_recv(*control->ssl_session, &datalen, sizeof(datalen)); else bytes = gnutls_record_recv(*control->ssl_session, &datalen, sizeof(datalen));
datalen = ntohl(datalen); datalen = ntohl(datalen);
...@@ -120,8 +141,7 @@ int lockdownd_hello(lockdownd_client *control) { ...@@ -120,8 +141,7 @@ int lockdownd_hello(lockdownd_client *control) {
char *XML_content; char *XML_content;
uint32 length; uint32 length;
xmlDocDumpMemory(plist, (xmlChar**)&XML_content, &length); xmlDocDumpMemory(plist, (xmlChar **)&XML_content, &length);
bytes = lockdownd_send(control, XML_content, length); bytes = lockdownd_send(control, XML_content, length);
xmlFree(XML_content); xmlFree(XML_content);
...@@ -135,7 +155,6 @@ int lockdownd_hello(lockdownd_client *control) { ...@@ -135,7 +155,6 @@ int lockdownd_hello(lockdownd_client *control) {
if (!xmlStrcmp(dict->name, "dict")) break; if (!xmlStrcmp(dict->name, "dict")) break;
} }
if (!dict) return 0; if (!dict) return 0;
dictionary = read_dict_element_strings(dict); dictionary = read_dict_element_strings(dict);
xmlFreeDoc(plist); xmlFreeDoc(plist);
free(XML_content); free(XML_content);
...@@ -226,6 +245,8 @@ int lockdownd_init(iPhone *phone, lockdownd_client **control) ...@@ -226,6 +245,8 @@ int lockdownd_init(iPhone *phone, lockdownd_client **control)
} }
host_id = get_host_id(); host_id = get_host_id();
if (!host_id) host_id = lockdownd_generate_hostid();
if (!is_device_known(public_key)){ if (!is_device_known(public_key)){
ret = lockdownd_pair_device(*control, public_key, host_id); ret = lockdownd_pair_device(*control, public_key, host_id);
} }
...@@ -284,6 +305,12 @@ int lockdownd_pair_device(lockdownd_client *control, char *public_key_b64, char ...@@ -284,6 +305,12 @@ int lockdownd_pair_device(lockdownd_client *control, char *public_key_b64, char
/* Now get iPhone's answer */ /* Now get iPhone's answer */
bytes = lockdownd_recv(control, &XML_content); bytes = lockdownd_recv(control, &XML_content);
if (debug) {
printf("lockdown_pair_device: iPhone's response to our pair request:\n");
fwrite(XML_content, 1, bytes, stdout);
printf("\n\n");
}
plist = xmlReadMemory(XML_content, bytes, NULL, NULL, 0); plist = xmlReadMemory(XML_content, bytes, NULL, NULL, 0);
if (!plist) return 0; if (!plist) return 0;
dict = xmlDocGetRootElement(plist); dict = xmlDocGetRootElement(plist);
...@@ -310,8 +337,13 @@ int lockdownd_pair_device(lockdownd_client *control, char *public_key_b64, char ...@@ -310,8 +337,13 @@ int lockdownd_pair_device(lockdownd_client *control, char *public_key_b64, char
} }
/* store public key in config if pairing succeeded */ /* store public key in config if pairing succeeded */
if (success) if (success) {
if (debug) printf("lockdownd_pair_device: pair success\n");
store_device_public_key(public_key_b64); store_device_public_key(public_key_b64);
ret = 1;
} else {
if (debug) printf("lockdownd_pair_device: pair failure\n");
}
return ret; return ret;
} }
...@@ -480,6 +512,7 @@ int lockdownd_start_SSL_session(lockdownd_client *control, const char *HostID) { ...@@ -480,6 +512,7 @@ int lockdownd_start_SSL_session(lockdownd_client *control, const char *HostID) {
// Set up GnuTLS... // Set up GnuTLS...
//gnutls_anon_client_credentials_t anoncred; //gnutls_anon_client_credentials_t anoncred;
gnutls_certificate_credentials_t xcred; gnutls_certificate_credentials_t xcred;
if (debug) printf("We started the session OK, now trying GnuTLS\n"); if (debug) printf("We started the session OK, now trying GnuTLS\n");
errno = 0; errno = 0;
gnutls_global_init(); gnutls_global_init();
...@@ -703,4 +736,3 @@ int lockdownd_start_service(lockdownd_client *control, const char *service) { ...@@ -703,4 +736,3 @@ int lockdownd_start_service(lockdownd_client *control, const char *service) {
return 0; return 0;
} }
...@@ -57,6 +57,7 @@ int main(int argc, char *argv[]) { ...@@ -57,6 +57,7 @@ int main(int argc, char *argv[]) {
} }
printf("Now starting SSL.\n"); printf("Now starting SSL.\n");
host_id = get_host_id(); host_id = get_host_id();
if (host_id && !lockdownd_start_SSL_session(control, host_id)) { if (host_id && !lockdownd_start_SSL_session(control, host_id)) {
printf("Error happened in GnuTLS...\n"); printf("Error happened in GnuTLS...\n");
......
...@@ -33,4 +33,5 @@ void free_plist(xmlDocPtr plist); ...@@ -33,4 +33,5 @@ void free_plist(xmlDocPtr plist);
xmlDocPtr new_plist(); xmlDocPtr new_plist();
char **read_dict_element_strings(xmlNode *dict); char **read_dict_element_strings(xmlNode *dict);
void free_dictionary(char **dictionary); void free_dictionary(char **dictionary);
char **read_dict_element_strings(xmlNode *dict);
#endif #endif
...@@ -224,7 +224,7 @@ int mux_send(usbmux_connection *connection, const char *data, uint32 datalen) { ...@@ -224,7 +224,7 @@ int mux_send(usbmux_connection *connection, const char *data, uint32 datalen) {
connection->header->length16 = ntohs(connection->header->length16); connection->header->length16 = ntohs(connection->header->length16);
// Now return the bytes. // Now return the bytes.
if (bytes < sizeof(*connection)+datalen) { if (bytes < sizeof(usbmux_tcp_header)+datalen) {
return -1; // blah return -1; // blah
} else { } else {
return bytes - 28; // actual length sent. :/ return bytes - 28; // actual length sent. :/
......
...@@ -23,6 +23,8 @@ ...@@ -23,6 +23,8 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include "userpref.h" #include "userpref.h"
#include <string.h>
#include <stdio.h>
#define LIBIPHONE_CONF_DIR "libiphone" #define LIBIPHONE_CONF_DIR "libiphone"
...@@ -87,7 +89,7 @@ int is_device_known(char* public_key) ...@@ -87,7 +89,7 @@ int is_device_known(char* public_key)
g_io_channel_read_to_end (keyfile, &stored_key, NULL, NULL); g_io_channel_read_to_end (keyfile, &stored_key, NULL, NULL);
/* now compare to input */ /* now compare to input */
if (strcmp(public_key, stored_key) == 2) if (strcmp(public_key, stored_key) == 2 || !strcmp(public_key, stored_key))
ret = 1; ret = 1;
g_free(stored_key); g_free(stored_key);
g_io_channel_shutdown(keyfile, FALSE, NULL); g_io_channel_shutdown(keyfile, FALSE, NULL);
...@@ -118,6 +120,7 @@ int store_device_public_key(char* public_key) ...@@ -118,6 +120,7 @@ int store_device_public_key(char* public_key)
gchar** devices_list = g_key_file_get_string_list (key_file, "Global", "DevicesList", NULL, NULL); gchar** devices_list = g_key_file_get_string_list (key_file, "Global", "DevicesList", NULL, NULL);
guint length = 0; guint length = 0;
guint wlength = 0;
if (devices_list) if (devices_list)
g_strv_length(devices_list); g_strv_length(devices_list);
g_strfreev(devices_list); g_strfreev(devices_list);
...@@ -127,8 +130,9 @@ int store_device_public_key(char* public_key) ...@@ -127,8 +130,9 @@ int store_device_public_key(char* public_key)
gchar* device_file = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, dev_file, NULL); gchar* device_file = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, dev_file, NULL);
GIOChannel* file = g_io_channel_new_file (device_file, "w", NULL); GIOChannel* file = g_io_channel_new_file (device_file, "w", NULL);
g_io_channel_write_chars (file, public_key, length, NULL, NULL); wlength = strlen(public_key); // why this wasn't discovered before... ugh
g_io_channel_shutdown(file, FALSE, NULL); g_io_channel_write_chars (file, public_key, wlength, NULL, NULL);
g_io_channel_shutdown(file, TRUE, NULL);
/* append device to list */ /* append device to list */
gchar** new_devices_list = (gchar**)g_malloc(sizeof(gchar*)* (length + 1)); gchar** new_devices_list = (gchar**)g_malloc(sizeof(gchar*)* (length + 1));
...@@ -145,7 +149,7 @@ int store_device_public_key(char* public_key) ...@@ -145,7 +149,7 @@ int store_device_public_key(char* public_key)
gchar* buf = g_key_file_to_data (key_file, &length,NULL); gchar* buf = g_key_file_to_data (key_file, &length,NULL);
GIOChannel* file = g_io_channel_new_file (config_file, "w", NULL); GIOChannel* file = g_io_channel_new_file (config_file, "w", NULL);
g_io_channel_write_chars (file, buf, length, NULL, NULL); g_io_channel_write_chars (file, buf, length, NULL, NULL);
g_io_channel_shutdown(file, FALSE, NULL); g_io_channel_shutdown(file, TRUE, NULL);
g_key_file_free(key_file); g_key_file_free(key_file);
} }
...@@ -215,6 +219,7 @@ int init_config_file(char* host_id, gnutls_datum_t* root_key, gnutls_datum_t* ho ...@@ -215,6 +219,7 @@ int init_config_file(char* host_id, gnutls_datum_t* root_key, gnutls_datum_t* ho
GKeyFile* key_file = g_key_file_new (); GKeyFile* key_file = g_key_file_new ();
/* store in config file */ /* store in config file */
if (debug) printf("init_config_file setting hostID to %s\n", host_id);
g_key_file_set_value (key_file, "Global", "HostID", host_id); g_key_file_set_value (key_file, "Global", "HostID", host_id);
/* write config file on disk */ /* write config file on disk */
...@@ -222,7 +227,7 @@ int init_config_file(char* host_id, gnutls_datum_t* root_key, gnutls_datum_t* ho ...@@ -222,7 +227,7 @@ int init_config_file(char* host_id, gnutls_datum_t* root_key, gnutls_datum_t* ho
gchar* buf = g_key_file_to_data (key_file, &length,NULL); gchar* buf = g_key_file_to_data (key_file, &length,NULL);
GIOChannel* file = g_io_channel_new_file (config_file, "w", NULL); GIOChannel* file = g_io_channel_new_file (config_file, "w", NULL);
g_io_channel_write_chars (file, buf, length, NULL, NULL); g_io_channel_write_chars (file, buf, length, NULL, NULL);
g_io_channel_shutdown(file, FALSE, NULL); g_io_channel_shutdown(file, TRUE, NULL);
g_key_file_free(key_file); g_key_file_free(key_file);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment