summaryrefslogtreecommitdiffstats
path: root/src/userpref.c
diff options
context:
space:
mode:
authorGravatar Jonathan Beck2008-08-04 22:31:43 +0200
committerGravatar Matt Colyer2008-08-05 23:28:10 -0700
commit4b558a53f61005b0ca49665d2da92303f6e14872 (patch)
tree3631ebdcfbf681a239bde3192172d3014b56322b /src/userpref.c
parent20a6f8797add1a44aa6ea2cc1d089122d1f39be3 (diff)
downloadlibimobiledevice-4b558a53f61005b0ca49665d2da92303f6e14872.tar.gz
libimobiledevice-4b558a53f61005b0ca49665d2da92303f6e14872.tar.bz2
Store certificates and private keys as PEM files instead of storing them in config file. Added functions to generate proper pairing request.
Signed-off-by: Matt Colyer <matt@colyer.name>
Diffstat (limited to 'src/userpref.c')
-rw-r--r--src/userpref.c148
1 files changed, 67 insertions, 81 deletions
diff --git a/src/userpref.c b/src/userpref.c
index f93cff4..12ff8f3 100644
--- a/src/userpref.c
+++ b/src/userpref.c
@@ -24,9 +24,16 @@
#include <string.h>
#include "userpref.h"
+
#define LIBIPHONE_CONF_DIR "libiphone"
#define LIBIPHONE_CONF_FILE "libiphonerc"
+#define LIBIPHONE_ROOT_PRIVKEY "RootPrivateKey.pem"
+#define LIBIPHONE_HOST_PRIVKEY "HostPrivateKey.pem"
+#define LIBIPHONE_ROOT_CERTIF "RootCertificate.pem"
+#define LIBIPHONE_HOST_CERTIF "HostCertificate.pem"
+
+
extern int debug;
inline void create_config_dir() {
@@ -145,99 +152,59 @@ int store_device_public_key(char* public_key)
return 1;
}
-
-char* get_root_private_key()
+int read_file_in_confdir(char* file, gnutls_datum_t* data)
{
- char* private_key = NULL;
-
- /* first get config file */
- gchar* config_file = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, LIBIPHONE_CONF_FILE, NULL);
- if (g_file_test(config_file, (G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR))) {
-
- /* now parse file to get knwon devices list */
- GKeyFile* key_file = g_key_file_new ();
- if( g_key_file_load_from_file (key_file, config_file, G_KEY_FILE_KEEP_COMMENTS, NULL) ) {
+ if (NULL == file || NULL == data)
+ return 0;
- gchar* loc_private_key = g_key_file_get_value(key_file, "Global", "RootPrivateKey", NULL);
- if (loc_private_key)
- private_key = strdup((char*)loc_private_key);
- g_free(loc_private_key);
- }
- g_key_file_free(key_file);
+ gchar* filepath = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, file, NULL);
+ if (g_file_test(filepath, (G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR))) {
+
+ FILE * pFile;
+ long lSize;
+
+ pFile = fopen ( filepath , "rb" );
+ if (pFile==NULL)
+ return 0;
+
+ fseek (pFile , 0 , SEEK_END);
+ data->size = ftell (pFile);
+ rewind (pFile);
+
+ data->data = (char*)gnutls_malloc(data->size);
+ if (data->data == NULL)
+ return 0;
+
+ // copy the file into the buffer:
+ fread (data->data,1,data->size,pFile);
+ fclose (pFile);
}
- return private_key;
+ return 1;
}
-char* get_host_private_key()
+int get_root_private_key(gnutls_datum_t* root_privkey)
{
- char* private_key = NULL;
-
- /* first get config file */
- gchar* config_file = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, LIBIPHONE_CONF_FILE, NULL);
- if (g_file_test(config_file, (G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR))) {
-
- /* now parse file to get knwon devices list */
- GKeyFile* key_file = g_key_file_new ();
- if( g_key_file_load_from_file (key_file, config_file, G_KEY_FILE_KEEP_COMMENTS, NULL) ) {
-
- gchar* loc_private_key = g_key_file_get_value(key_file, "Global", "HostPrivateKey", NULL);
- if (loc_private_key)
- private_key = strdup((char*)loc_private_key);
- g_free(loc_private_key);
- }
- g_key_file_free(key_file);
- }
- return private_key;
+ return read_file_in_confdir(LIBIPHONE_ROOT_PRIVKEY, root_privkey);
}
-
-char* get_root_certificate()
+int get_host_private_key(gnutls_datum_t* host_privkey)
{
- char* cert = NULL;
-
- /* first get config file */
- gchar* config_file = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, LIBIPHONE_CONF_FILE, NULL);
- if (g_file_test(config_file, (G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR))) {
-
- /* now parse file to get knwon devices list */
- GKeyFile* key_file = g_key_file_new ();
- if( g_key_file_load_from_file (key_file, config_file, G_KEY_FILE_KEEP_COMMENTS, NULL) ) {
-
- gchar* loc_cert = g_key_file_get_value(key_file, "Global", "RootCertificate", NULL);
- if (loc_cert)
- cert = strdup((char*)loc_cert);
- g_free(loc_cert);
- }
- g_key_file_free(key_file);
- }
- return cert;
+ return read_file_in_confdir(LIBIPHONE_HOST_PRIVKEY, host_privkey);
}
-char* get_host_certificate()
+int get_root_certificate(gnutls_datum_t* root_cert)
{
- char* cert = NULL;
-
- /* first get config file */
- gchar* config_file = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, LIBIPHONE_CONF_FILE, NULL);
- if (g_file_test(config_file, (G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR))) {
-
- /* now parse file to get knwon devices list */
- GKeyFile* key_file = g_key_file_new ();
- if( g_key_file_load_from_file (key_file, config_file, G_KEY_FILE_KEEP_COMMENTS, NULL) ) {
+ return read_file_in_confdir(LIBIPHONE_ROOT_CERTIF, root_cert);
+}
- gchar* loc_cert = g_key_file_get_value(key_file, "Global", "HostCertificate", NULL);
- if (loc_cert)
- cert = strdup((char*)loc_cert);
- g_free(loc_cert);
- }
- g_key_file_free(key_file);
- }
- return cert;
+int get_host_certificate(gnutls_datum_t* host_cert)
+{
+ return read_file_in_confdir(LIBIPHONE_HOST_CERTIF, host_cert);
}
-int init_config_file(char* host_id, char* root_private_key, char* host_private_key, char* root_cert, char* host_cert)
+int init_config_file(char* host_id, gnutls_datum_t* root_key, gnutls_datum_t* host_key, gnutls_datum_t* root_cert, gnutls_datum_t* host_cert)
{
- if (!host_id || !root_private_key || !host_private_key || !root_cert || !host_cert)
+ if (!host_id || !root_key || !host_key || !root_cert || !host_cert)
return 0;
gchar* config_file = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, LIBIPHONE_CONF_FILE, NULL);
@@ -249,10 +216,6 @@ int init_config_file(char* host_id, char* root_private_key, char* host_private_k
/* store in config file */
g_key_file_set_value (key_file, "Global", "HostID", host_id);
- g_key_file_set_value (key_file, "Global", "RootPrivateKey", root_private_key);
- g_key_file_set_value (key_file, "Global", "HostPrivateKey", host_private_key);
- g_key_file_set_value (key_file, "Global", "RootCertificate", root_cert);
- g_key_file_set_value (key_file, "Global", "HostCertificate", host_cert);
/* write config file on disk */
gsize length;
@@ -263,5 +226,28 @@ int init_config_file(char* host_id, char* root_private_key, char* host_private_k
g_key_file_free(key_file);
+ //now write keys and certifs to disk
+ FILE * pFile;
+ gchar* pem;
+ pem = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, LIBIPHONE_ROOT_PRIVKEY, NULL);
+ pFile = fopen ( pem , "wb" );
+ fwrite ( root_key->data, 1 , root_key->size , pFile );
+ fclose (pFile);
+
+ pem = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, LIBIPHONE_HOST_PRIVKEY, NULL);
+ pFile = fopen ( pem , "wb" );
+ fwrite ( host_key->data, 1 , host_key->size , pFile );
+ fclose (pFile);
+
+ pem = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, LIBIPHONE_ROOT_CERTIF, NULL);
+ pFile = fopen ( pem , "wb" );
+ fwrite ( root_cert->data, 1 , root_cert->size , pFile );
+ fclose (pFile);
+
+ pem = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, LIBIPHONE_HOST_CERTIF, NULL);
+ pFile = fopen ( pem , "wb" );
+ fwrite ( host_cert->data, 1 , host_cert->size , pFile );
+ fclose (pFile);
+
return 1;
}