summaryrefslogtreecommitdiffstats
path: root/common
diff options
context:
space:
mode:
Diffstat (limited to 'common')
-rw-r--r--common/debug.c45
-rw-r--r--common/userpref.c53
-rw-r--r--common/userpref.h2
3 files changed, 54 insertions, 46 deletions
diff --git a/common/debug.c b/common/debug.c
index cf1bc2f..7a593fc 100644
--- a/common/debug.c
+++ b/common/debug.c
@@ -30,6 +30,9 @@
30#include <stdint.h> 30#include <stdint.h>
31#include <stdlib.h> 31#include <stdlib.h>
32#include <time.h> 32#include <time.h>
33#ifndef _WIN32
34#include <sys/time.h>
35#endif
33 36
34#include "src/idevice.h" 37#include "src/idevice.h"
35#include "debug.h" 38#include "debug.h"
@@ -51,27 +54,31 @@ void internal_set_debug_level(int level)
51#ifndef STRIP_DEBUG_CODE 54#ifndef STRIP_DEBUG_CODE
52static void debug_print_line(const char *func, const char *file, int line, const char *buffer) 55static void debug_print_line(const char *func, const char *file, int line, const char *buffer)
53{ 56{
54 char *str_time = NULL; 57 char str_time[24];
55 char *header = NULL; 58#ifdef _WIN32
59 SYSTEMTIME lt;
60 GetLocalTime(&lt);
61 snprintf(str_time, 24, "%02d:%02d:%02d.%03d", lt.wHour, lt.wMinute, lt.wSecond, lt.wMilliseconds);
62#else
63#ifdef HAVE_GETTIMEOFDAY
64 struct timeval tv;
65 struct tm *tp;
66 gettimeofday(&tv, NULL);
67#ifdef HAVE_LOCALTIME_R
68 struct tm tp_;
69 tp = localtime_r(&tv.tv_sec, &tp_);
70#else
71 tp = localtime(&tv.tv_sec);
72#endif
73 strftime(str_time, 9, "%H:%M:%S", tp);
74 snprintf(str_time+8, 10, ".%03d", (int)tv.tv_usec/1000);
75#else
56 time_t the_time; 76 time_t the_time;
57
58 time(&the_time); 77 time(&the_time);
59 str_time = (char*)malloc(255); 78 strftime(str_time, 15, "%H:%M:%S", localtime (&the_time));
60 strftime(str_time, 254, "%H:%M:%S", localtime (&the_time)); 79#endif
61 80#endif
62 /* generate header text */ 81 fprintf(stderr, "%s %s:%d %s(): %s\n", str_time, file, line, func, buffer);
63 if(asprintf(&header, "%s %s:%d %s()", str_time, file, line, func)<0){}
64 free (str_time);
65
66 /* trim ending newlines */
67
68 /* print header */
69 fprintf(stderr, "%s: ", header);
70
71 /* print actual debug content */
72 fprintf(stderr, "%s\n", buffer);
73
74 free (header);
75} 82}
76#endif 83#endif
77 84
diff --git a/common/userpref.c b/common/userpref.c
index 48bcfcb..76945e1 100644
--- a/common/userpref.c
+++ b/common/userpref.c
@@ -29,13 +29,18 @@
29#include <stdint.h> 29#include <stdint.h>
30#include <stdlib.h> 30#include <stdlib.h>
31#include <string.h> 31#include <string.h>
32#include <errno.h>
33
32#ifdef HAVE_SYS_TYPES_H 34#ifdef HAVE_SYS_TYPES_H
33#include <sys/types.h> 35#include <sys/types.h>
34#endif 36#endif
35#ifndef WIN32 37#include <dirent.h>
38#ifndef _WIN32
36#include <pwd.h> 39#include <pwd.h>
37#endif
38#include <unistd.h> 40#include <unistd.h>
41#include <libgen.h>
42#include <sys/stat.h>
43#endif
39#include <usbmuxd.h> 44#include <usbmuxd.h>
40#if defined(HAVE_OPENSSL) 45#if defined(HAVE_OPENSSL)
41#include <openssl/bn.h> 46#include <openssl/bn.h>
@@ -64,12 +69,7 @@
64#error No supported TLS/SSL library enabled 69#error No supported TLS/SSL library enabled
65#endif 70#endif
66 71
67#include <dirent.h> 72#ifdef _WIN32
68#include <libgen.h>
69#include <sys/stat.h>
70#include <errno.h>
71
72#ifdef WIN32
73#include <shlobj.h> 73#include <shlobj.h>
74#endif 74#endif
75 75
@@ -77,6 +77,7 @@
77#define ETIMEDOUT 138 77#define ETIMEDOUT 138
78#endif 78#endif
79 79
80#include <libimobiledevice/libimobiledevice.h>
80#include <libimobiledevice-glue/utils.h> 81#include <libimobiledevice-glue/utils.h>
81 82
82#include "userpref.h" 83#include "userpref.h"
@@ -93,7 +94,7 @@ const ASN1_ARRAY_TYPE pkcs1_asn1_tab[] = {
93}; 94};
94#endif 95#endif
95 96
96#ifdef WIN32 97#ifdef _WIN32
97#define DIR_SEP '\\' 98#define DIR_SEP '\\'
98#define DIR_SEP_S "\\" 99#define DIR_SEP_S "\\"
99#else 100#else
@@ -103,7 +104,7 @@ const ASN1_ARRAY_TYPE pkcs1_asn1_tab[] = {
103 104
104#define USERPREF_CONFIG_EXTENSION ".plist" 105#define USERPREF_CONFIG_EXTENSION ".plist"
105 106
106#ifdef WIN32 107#ifdef _WIN32
107#define USERPREF_CONFIG_DIR "Apple"DIR_SEP_S"Lockdown" 108#define USERPREF_CONFIG_DIR "Apple"DIR_SEP_S"Lockdown"
108#else 109#else
109#define USERPREF_CONFIG_DIR "lockdown" 110#define USERPREF_CONFIG_DIR "lockdown"
@@ -113,7 +114,7 @@ const ASN1_ARRAY_TYPE pkcs1_asn1_tab[] = {
113 114
114static char *__config_dir = NULL; 115static char *__config_dir = NULL;
115 116
116#ifdef WIN32 117#ifdef _WIN32
117static char *userpref_utf16_to_utf8(wchar_t *unistr, long len, long *items_read, long *items_written) 118static char *userpref_utf16_to_utf8(wchar_t *unistr, long len, long *items_read, long *items_written)
118{ 119{
119 if (!unistr || (len <= 0)) return NULL; 120 if (!unistr || (len <= 0)) return NULL;
@@ -155,7 +156,7 @@ const char *userpref_get_config_dir()
155 if (__config_dir) 156 if (__config_dir)
156 return __config_dir; 157 return __config_dir;
157 158
158#ifdef WIN32 159#ifdef _WIN32
159 wchar_t path[MAX_PATH+1]; 160 wchar_t path[MAX_PATH+1];
160 HRESULT hr; 161 HRESULT hr;
161 LPITEMIDLIST pidl = NULL; 162 LPITEMIDLIST pidl = NULL;
@@ -419,7 +420,7 @@ static int _mbedtls_x509write_crt_set_basic_constraints_critical(mbedtls_x509wri
419 * 420 *
420 * @return 1 if keys were successfully generated, 0 otherwise 421 * @return 1 if keys were successfully generated, 0 otherwise
421 */ 422 */
422userpref_error_t pair_record_generate_keys_and_certs(plist_t pair_record, key_data_t public_key) 423userpref_error_t pair_record_generate_keys_and_certs(plist_t pair_record, key_data_t public_key, unsigned int device_version)
423{ 424{
424 userpref_error_t ret = USERPREF_E_SSL_ERROR; 425 userpref_error_t ret = USERPREF_E_SSL_ERROR;
425 426
@@ -484,7 +485,7 @@ userpref_error_t pair_record_generate_keys_and_certs(plist_t pair_record, key_da
484 X509_set_pubkey(root_cert, root_pkey); 485 X509_set_pubkey(root_cert, root_pkey);
485 486
486 /* sign root cert with root private key */ 487 /* sign root cert with root private key */
487 X509_sign(root_cert, root_pkey, EVP_sha1()); 488 X509_sign(root_cert, root_pkey, (device_version < IDEVICE_DEVICE_VERSION(4,0,0)) ? EVP_sha1() : EVP_sha256());
488 } 489 }
489 490
490 /* create host certificate */ 491 /* create host certificate */
@@ -517,7 +518,7 @@ userpref_error_t pair_record_generate_keys_and_certs(plist_t pair_record, key_da
517 X509_set_pubkey(host_cert, host_pkey); 518 X509_set_pubkey(host_cert, host_pkey);
518 519
519 /* sign host cert with root private key */ 520 /* sign host cert with root private key */
520 X509_sign(host_cert, root_pkey, EVP_sha1()); 521 X509_sign(host_cert, root_pkey, (device_version < IDEVICE_DEVICE_VERSION(4,0,0)) ? EVP_sha1() : EVP_sha256());
521 } 522 }
522 523
523 if (root_cert && root_pkey && host_cert && host_pkey) { 524 if (root_cert && root_pkey && host_cert && host_pkey) {
@@ -609,7 +610,7 @@ userpref_error_t pair_record_generate_keys_and_certs(plist_t pair_record, key_da
609 X509_add_ext_helper(dev_cert, NID_key_usage, (char*)"critical,digitalSignature,keyEncipherment"); 610 X509_add_ext_helper(dev_cert, NID_key_usage, (char*)"critical,digitalSignature,keyEncipherment");
610 611
611 /* sign device certificate with root private key */ 612 /* sign device certificate with root private key */
612 if (X509_sign(dev_cert, root_pkey, EVP_sha1())) { 613 if (X509_sign(dev_cert, root_pkey, (device_version < IDEVICE_DEVICE_VERSION(4,0,0)) ? EVP_sha1() : EVP_sha256())) {
613 /* if signing succeeded, export in PEM format */ 614 /* if signing succeeded, export in PEM format */
614 BIO* membp = BIO_new(BIO_s_mem()); 615 BIO* membp = BIO_new(BIO_s_mem());
615 if (PEM_write_bio_X509(membp, dev_cert) > 0) { 616 if (PEM_write_bio_X509(membp, dev_cert) > 0) {
@@ -661,7 +662,7 @@ userpref_error_t pair_record_generate_keys_and_certs(plist_t pair_record, key_da
661 gnutls_x509_crt_set_ca_status(root_cert, 1); 662 gnutls_x509_crt_set_ca_status(root_cert, 1);
662 gnutls_x509_crt_set_activation_time(root_cert, time(NULL)); 663 gnutls_x509_crt_set_activation_time(root_cert, time(NULL));
663 gnutls_x509_crt_set_expiration_time(root_cert, time(NULL) + (60 * 60 * 24 * 365 * 10)); 664 gnutls_x509_crt_set_expiration_time(root_cert, time(NULL) + (60 * 60 * 24 * 365 * 10));
664 gnutls_x509_crt_sign2(root_cert, root_cert, root_privkey, GNUTLS_DIG_SHA1, 0); 665 gnutls_x509_crt_sign2(root_cert, root_cert, root_privkey, (device_version < IDEVICE_DEVICE_VERSION(4,0,0)) ? GNUTLS_DIG_SHA1 : GNUTLS_DIG_SHA256, 0);
665 666
666 gnutls_x509_crt_set_key(host_cert, host_privkey); 667 gnutls_x509_crt_set_key(host_cert, host_privkey);
667 gnutls_x509_crt_set_serial(host_cert, "\x01", 1); 668 gnutls_x509_crt_set_serial(host_cert, "\x01", 1);
@@ -670,7 +671,7 @@ userpref_error_t pair_record_generate_keys_and_certs(plist_t pair_record, key_da
670 gnutls_x509_crt_set_key_usage(host_cert, GNUTLS_KEY_KEY_ENCIPHERMENT | GNUTLS_KEY_DIGITAL_SIGNATURE); 671 gnutls_x509_crt_set_key_usage(host_cert, GNUTLS_KEY_KEY_ENCIPHERMENT | GNUTLS_KEY_DIGITAL_SIGNATURE);
671 gnutls_x509_crt_set_activation_time(host_cert, time(NULL)); 672 gnutls_x509_crt_set_activation_time(host_cert, time(NULL));
672 gnutls_x509_crt_set_expiration_time(host_cert, time(NULL) + (60 * 60 * 24 * 365 * 10)); 673 gnutls_x509_crt_set_expiration_time(host_cert, time(NULL) + (60 * 60 * 24 * 365 * 10));
673 gnutls_x509_crt_sign2(host_cert, root_cert, root_privkey, GNUTLS_DIG_SHA1, 0); 674 gnutls_x509_crt_sign2(host_cert, root_cert, root_privkey, (device_version < IDEVICE_DEVICE_VERSION(4,0,0)) ? GNUTLS_DIG_SHA1 : GNUTLS_DIG_SHA256, 0);
674 675
675 /* export to PEM format */ 676 /* export to PEM format */
676 size_t root_key_export_size = 0; 677 size_t root_key_export_size = 0;
@@ -768,17 +769,17 @@ userpref_error_t pair_record_generate_keys_and_certs(plist_t pair_record, key_da
768 gnutls_x509_crt_set_expiration_time(dev_cert, time(NULL) + (60 * 60 * 24 * 365 * 10)); 769 gnutls_x509_crt_set_expiration_time(dev_cert, time(NULL) + (60 * 60 * 24 * 365 * 10));
769 770
770 /* use custom hash generation for compatibility with the "Apple ecosystem" */ 771 /* use custom hash generation for compatibility with the "Apple ecosystem" */
771 const gnutls_digest_algorithm_t dig_sha1 = GNUTLS_DIG_SHA1; 772 const gnutls_digest_algorithm_t dig_sha = (device_version < IDEVICE_DEVICE_VERSION(4,0,0)) ? GNUTLS_DIG_SHA1 : GNUTLS_DIG_SHA256;
772 size_t hash_size = gnutls_hash_get_len(dig_sha1); 773 size_t hash_size = gnutls_hash_get_len(dig_sha);
773 unsigned char hash[hash_size]; 774 unsigned char hash[hash_size];
774 if (gnutls_hash_fast(dig_sha1, der_pub_key.data, der_pub_key.size, (unsigned char*)&hash) < 0) { 775 if (gnutls_hash_fast(dig_sha, der_pub_key.data, der_pub_key.size, (unsigned char*)&hash) < 0) {
775 debug_info("ERROR: Failed to generate SHA1 for public key"); 776 debug_info("ERROR: Failed to generate SHA for public key");
776 } else { 777 } else {
777 gnutls_x509_crt_set_subject_key_id(dev_cert, hash, hash_size); 778 gnutls_x509_crt_set_subject_key_id(dev_cert, hash, hash_size);
778 } 779 }
779 780
780 gnutls_x509_crt_set_key_usage(dev_cert, GNUTLS_KEY_DIGITAL_SIGNATURE | GNUTLS_KEY_KEY_ENCIPHERMENT); 781 gnutls_x509_crt_set_key_usage(dev_cert, GNUTLS_KEY_DIGITAL_SIGNATURE | GNUTLS_KEY_KEY_ENCIPHERMENT);
781 gnutls_error = gnutls_x509_crt_sign2(dev_cert, root_cert, root_privkey, GNUTLS_DIG_SHA1, 0); 782 gnutls_error = gnutls_x509_crt_sign2(dev_cert, root_cert, root_privkey, (device_version < IDEVICE_DEVICE_VERSION(4,0,0)) ? GNUTLS_DIG_SHA1 : GNUTLS_DIG_SHA256, 0);
782 if (GNUTLS_E_SUCCESS == gnutls_error) { 783 if (GNUTLS_E_SUCCESS == gnutls_error) {
783 /* if everything went well, export in PEM format */ 784 /* if everything went well, export in PEM format */
784 size_t export_size = 0; 785 size_t export_size = 0;
@@ -872,7 +873,7 @@ userpref_error_t pair_record_generate_keys_and_certs(plist_t pair_record, key_da
872 873
873 /* sign root cert with root private key */ 874 /* sign root cert with root private key */
874 mbedtls_x509write_crt_set_issuer_key(&cert, &root_pkey); 875 mbedtls_x509write_crt_set_issuer_key(&cert, &root_pkey);
875 mbedtls_x509write_crt_set_md_alg(&cert, MBEDTLS_MD_SHA1); 876 mbedtls_x509write_crt_set_md_alg(&cert, (device_version < IDEVICE_DEVICE_VERSION(4,0,0)) ? MBEDTLS_MD_SHA1 : MBEDTLS_MD_SHA256);
876 877
877 unsigned char outbuf[16384]; 878 unsigned char outbuf[16384];
878 879
@@ -931,7 +932,7 @@ userpref_error_t pair_record_generate_keys_and_certs(plist_t pair_record, key_da
931 932
932 /* sign host cert with root private key */ 933 /* sign host cert with root private key */
933 mbedtls_x509write_crt_set_issuer_key(&cert, &root_pkey); 934 mbedtls_x509write_crt_set_issuer_key(&cert, &root_pkey);
934 mbedtls_x509write_crt_set_md_alg(&cert, MBEDTLS_MD_SHA1); 935 mbedtls_x509write_crt_set_md_alg(&cert, (device_version < IDEVICE_DEVICE_VERSION(4,0,0)) ? MBEDTLS_MD_SHA1 : MBEDTLS_MD_SHA256);
935 936
936 /* write host private key */ 937 /* write host private key */
937 mbedtls_pk_write_key_pem(&host_pkey, outbuf, sizeof(outbuf)); 938 mbedtls_pk_write_key_pem(&host_pkey, outbuf, sizeof(outbuf));
@@ -991,7 +992,7 @@ userpref_error_t pair_record_generate_keys_and_certs(plist_t pair_record, key_da
991 992
992 /* sign device certificate with root private key */ 993 /* sign device certificate with root private key */
993 mbedtls_x509write_crt_set_issuer_key(&cert, &root_pkey); 994 mbedtls_x509write_crt_set_issuer_key(&cert, &root_pkey);
994 mbedtls_x509write_crt_set_md_alg(&cert, MBEDTLS_MD_SHA1); 995 mbedtls_x509write_crt_set_md_alg(&cert, (device_version < IDEVICE_DEVICE_VERSION(4,0,0)) ? MBEDTLS_MD_SHA1 : MBEDTLS_MD_SHA256);
995 996
996 /* write device certificate */ 997 /* write device certificate */
997 mbedtls_x509write_crt_pem(&cert, outbuf, sizeof(outbuf), mbedtls_ctr_drbg_random, &ctr_drbg); 998 mbedtls_x509write_crt_pem(&cert, outbuf, sizeof(outbuf), mbedtls_ctr_drbg_random, &ctr_drbg);
diff --git a/common/userpref.h b/common/userpref.h
index 75bb8b7..9a1832c 100644
--- a/common/userpref.h
+++ b/common/userpref.h
@@ -68,7 +68,7 @@ userpref_error_t userpref_read_pair_record(const char *udid, plist_t *pair_recor
68userpref_error_t userpref_save_pair_record(const char *udid, uint32_t device_id, plist_t pair_record); 68userpref_error_t userpref_save_pair_record(const char *udid, uint32_t device_id, plist_t pair_record);
69userpref_error_t userpref_delete_pair_record(const char *udid); 69userpref_error_t userpref_delete_pair_record(const char *udid);
70 70
71userpref_error_t pair_record_generate_keys_and_certs(plist_t pair_record, key_data_t public_key); 71userpref_error_t pair_record_generate_keys_and_certs(plist_t pair_record, key_data_t public_key, unsigned int device_version);
72#if defined(HAVE_OPENSSL) || defined(HAVE_MBEDTLS) 72#if defined(HAVE_OPENSSL) || defined(HAVE_MBEDTLS)
73userpref_error_t pair_record_import_key_with_name(plist_t pair_record, const char* name, key_data_t* key); 73userpref_error_t pair_record_import_key_with_name(plist_t pair_record, const char* name, key_data_t* key);
74userpref_error_t pair_record_import_crt_with_name(plist_t pair_record, const char* name, key_data_t* cert); 74userpref_error_t pair_record_import_crt_with_name(plist_t pair_record, const char* name, key_data_t* cert);