diff options
| author | 2008-08-04 22:31:43 +0200 | |
|---|---|---|
| committer | 2008-08-05 23:28:10 -0700 | |
| commit | 4b558a53f61005b0ca49665d2da92303f6e14872 (patch) | |
| tree | 3631ebdcfbf681a239bde3192172d3014b56322b /src/userpref.c | |
| parent | 20a6f8797add1a44aa6ea2cc1d089122d1f39be3 (diff) | |
| download | libimobiledevice-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.c | 148 |
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 @@ | |||
| 24 | #include <string.h> | 24 | #include <string.h> |
| 25 | #include "userpref.h" | 25 | #include "userpref.h" |
| 26 | 26 | ||
| 27 | |||
| 27 | #define LIBIPHONE_CONF_DIR "libiphone" | 28 | #define LIBIPHONE_CONF_DIR "libiphone" |
| 28 | #define LIBIPHONE_CONF_FILE "libiphonerc" | 29 | #define LIBIPHONE_CONF_FILE "libiphonerc" |
| 29 | 30 | ||
| 31 | #define LIBIPHONE_ROOT_PRIVKEY "RootPrivateKey.pem" | ||
| 32 | #define LIBIPHONE_HOST_PRIVKEY "HostPrivateKey.pem" | ||
| 33 | #define LIBIPHONE_ROOT_CERTIF "RootCertificate.pem" | ||
| 34 | #define LIBIPHONE_HOST_CERTIF "HostCertificate.pem" | ||
| 35 | |||
| 36 | |||
| 30 | extern int debug; | 37 | extern int debug; |
| 31 | 38 | ||
| 32 | inline void create_config_dir() { | 39 | inline void create_config_dir() { |
| @@ -145,99 +152,59 @@ int store_device_public_key(char* public_key) | |||
| 145 | return 1; | 152 | return 1; |
| 146 | } | 153 | } |
| 147 | 154 | ||
| 148 | 155 | int read_file_in_confdir(char* file, gnutls_datum_t* data) | |
| 149 | char* get_root_private_key() | ||
| 150 | { | 156 | { |
| 151 | char* private_key = NULL; | 157 | if (NULL == file || NULL == data) |
| 152 | 158 | return 0; | |
| 153 | /* first get config file */ | ||
| 154 | gchar* config_file = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, LIBIPHONE_CONF_FILE, NULL); | ||
| 155 | if (g_file_test(config_file, (G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR))) { | ||
| 156 | |||
| 157 | /* now parse file to get knwon devices list */ | ||
| 158 | GKeyFile* key_file = g_key_file_new (); | ||
| 159 | if( g_key_file_load_from_file (key_file, config_file, G_KEY_FILE_KEEP_COMMENTS, NULL) ) { | ||
| 160 | 159 | ||
| 161 | gchar* loc_private_key = g_key_file_get_value(key_file, "Global", "RootPrivateKey", NULL); | 160 | gchar* filepath = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, file, NULL); |
| 162 | if (loc_private_key) | 161 | if (g_file_test(filepath, (G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR))) { |
| 163 | private_key = strdup((char*)loc_private_key); | 162 | |
| 164 | g_free(loc_private_key); | 163 | FILE * pFile; |
| 165 | } | 164 | long lSize; |
| 166 | g_key_file_free(key_file); | 165 | |
| 166 | pFile = fopen ( filepath , "rb" ); | ||
| 167 | if (pFile==NULL) | ||
| 168 | return 0; | ||
| 169 | |||
| 170 | fseek (pFile , 0 , SEEK_END); | ||
| 171 | data->size = ftell (pFile); | ||
| 172 | rewind (pFile); | ||
| 173 | |||
| 174 | data->data = (char*)gnutls_malloc(data->size); | ||
| 175 | if (data->data == NULL) | ||
| 176 | return 0; | ||
| 177 | |||
| 178 | // copy the file into the buffer: | ||
| 179 | fread (data->data,1,data->size,pFile); | ||
| 180 | fclose (pFile); | ||
| 167 | } | 181 | } |
| 168 | return private_key; | 182 | return 1; |
| 169 | } | 183 | } |
| 170 | 184 | ||
| 171 | char* get_host_private_key() | 185 | int get_root_private_key(gnutls_datum_t* root_privkey) |
| 172 | { | 186 | { |
| 173 | char* private_key = NULL; | 187 | return read_file_in_confdir(LIBIPHONE_ROOT_PRIVKEY, root_privkey); |
| 174 | |||
| 175 | /* first get config file */ | ||
| 176 | gchar* config_file = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, LIBIPHONE_CONF_FILE, NULL); | ||
| 177 | if (g_file_test(config_file, (G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR))) { | ||
| 178 | |||
| 179 | /* now parse file to get knwon devices list */ | ||
| 180 | GKeyFile* key_file = g_key_file_new (); | ||
| 181 | if( g_key_file_load_from_file (key_file, config_file, G_KEY_FILE_KEEP_COMMENTS, NULL) ) { | ||
| 182 | |||
| 183 | gchar* loc_private_key = g_key_file_get_value(key_file, "Global", "HostPrivateKey", NULL); | ||
| 184 | if (loc_private_key) | ||
| 185 | private_key = strdup((char*)loc_private_key); | ||
| 186 | g_free(loc_private_key); | ||
| 187 | } | ||
| 188 | g_key_file_free(key_file); | ||
| 189 | } | ||
| 190 | return private_key; | ||
| 191 | } | 188 | } |
| 192 | 189 | ||
| 193 | 190 | int get_host_private_key(gnutls_datum_t* host_privkey) | |
| 194 | char* get_root_certificate() | ||
| 195 | { | 191 | { |
| 196 | char* cert = NULL; | 192 | return read_file_in_confdir(LIBIPHONE_HOST_PRIVKEY, host_privkey); |
| 197 | |||
| 198 | /* first get config file */ | ||
| 199 | gchar* config_file = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, LIBIPHONE_CONF_FILE, NULL); | ||
| 200 | if (g_file_test(config_file, (G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR))) { | ||
| 201 | |||
| 202 | /* now parse file to get knwon devices list */ | ||
| 203 | GKeyFile* key_file = g_key_file_new (); | ||
| 204 | if( g_key_file_load_from_file (key_file, config_file, G_KEY_FILE_KEEP_COMMENTS, NULL) ) { | ||
| 205 | |||
| 206 | gchar* loc_cert = g_key_file_get_value(key_file, "Global", "RootCertificate", NULL); | ||
| 207 | if (loc_cert) | ||
| 208 | cert = strdup((char*)loc_cert); | ||
| 209 | g_free(loc_cert); | ||
| 210 | } | ||
| 211 | g_key_file_free(key_file); | ||
| 212 | } | ||
| 213 | return cert; | ||
| 214 | } | 193 | } |
| 215 | 194 | ||
| 216 | char* get_host_certificate() | 195 | int get_root_certificate(gnutls_datum_t* root_cert) |
| 217 | { | 196 | { |
| 218 | char* cert = NULL; | 197 | return read_file_in_confdir(LIBIPHONE_ROOT_CERTIF, root_cert); |
| 219 | 198 | } | |
| 220 | /* first get config file */ | ||
| 221 | gchar* config_file = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, LIBIPHONE_CONF_FILE, NULL); | ||
| 222 | if (g_file_test(config_file, (G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR))) { | ||
| 223 | |||
| 224 | /* now parse file to get knwon devices list */ | ||
| 225 | GKeyFile* key_file = g_key_file_new (); | ||
| 226 | if( g_key_file_load_from_file (key_file, config_file, G_KEY_FILE_KEEP_COMMENTS, NULL) ) { | ||
| 227 | 199 | ||
| 228 | gchar* loc_cert = g_key_file_get_value(key_file, "Global", "HostCertificate", NULL); | 200 | int get_host_certificate(gnutls_datum_t* host_cert) |
| 229 | if (loc_cert) | 201 | { |
| 230 | cert = strdup((char*)loc_cert); | 202 | return read_file_in_confdir(LIBIPHONE_HOST_CERTIF, host_cert); |
| 231 | g_free(loc_cert); | ||
| 232 | } | ||
| 233 | g_key_file_free(key_file); | ||
| 234 | } | ||
| 235 | return cert; | ||
| 236 | } | 203 | } |
| 237 | 204 | ||
| 238 | int init_config_file(char* host_id, char* root_private_key, char* host_private_key, char* root_cert, char* host_cert) | 205 | 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) |
| 239 | { | 206 | { |
| 240 | if (!host_id || !root_private_key || !host_private_key || !root_cert || !host_cert) | 207 | if (!host_id || !root_key || !host_key || !root_cert || !host_cert) |
| 241 | return 0; | 208 | return 0; |
| 242 | 209 | ||
| 243 | gchar* config_file = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, LIBIPHONE_CONF_FILE, NULL); | 210 | 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 | |||
| 249 | 216 | ||
| 250 | /* store in config file */ | 217 | /* store in config file */ |
| 251 | g_key_file_set_value (key_file, "Global", "HostID", host_id); | 218 | g_key_file_set_value (key_file, "Global", "HostID", host_id); |
| 252 | g_key_file_set_value (key_file, "Global", "RootPrivateKey", root_private_key); | ||
| 253 | g_key_file_set_value (key_file, "Global", "HostPrivateKey", host_private_key); | ||
| 254 | g_key_file_set_value (key_file, "Global", "RootCertificate", root_cert); | ||
| 255 | g_key_file_set_value (key_file, "Global", "HostCertificate", host_cert); | ||
| 256 | 219 | ||
| 257 | /* write config file on disk */ | 220 | /* write config file on disk */ |
| 258 | gsize length; | 221 | gsize length; |
| @@ -263,5 +226,28 @@ int init_config_file(char* host_id, char* root_private_key, char* host_private_k | |||
| 263 | 226 | ||
| 264 | g_key_file_free(key_file); | 227 | g_key_file_free(key_file); |
| 265 | 228 | ||
| 229 | //now write keys and certifs to disk | ||
| 230 | FILE * pFile; | ||
| 231 | gchar* pem; | ||
| 232 | pem = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, LIBIPHONE_ROOT_PRIVKEY, NULL); | ||
| 233 | pFile = fopen ( pem , "wb" ); | ||
| 234 | fwrite ( root_key->data, 1 , root_key->size , pFile ); | ||
| 235 | fclose (pFile); | ||
| 236 | |||
| 237 | pem = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, LIBIPHONE_HOST_PRIVKEY, NULL); | ||
| 238 | pFile = fopen ( pem , "wb" ); | ||
| 239 | fwrite ( host_key->data, 1 , host_key->size , pFile ); | ||
| 240 | fclose (pFile); | ||
| 241 | |||
| 242 | pem = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, LIBIPHONE_ROOT_CERTIF, NULL); | ||
| 243 | pFile = fopen ( pem , "wb" ); | ||
| 244 | fwrite ( root_cert->data, 1 , root_cert->size , pFile ); | ||
| 245 | fclose (pFile); | ||
| 246 | |||
| 247 | pem = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, LIBIPHONE_HOST_CERTIF, NULL); | ||
| 248 | pFile = fopen ( pem , "wb" ); | ||
| 249 | fwrite ( host_cert->data, 1 , host_cert->size , pFile ); | ||
| 250 | fclose (pFile); | ||
| 251 | |||
| 266 | return 1; | 252 | return 1; |
| 267 | } | 253 | } |
