|
| 1 | +// Google BSD license https://developers.google.com/google-bsd-license |
| 2 | +// Copyright 2012 Google Inc. [email protected] |
| 3 | + |
| 4 | +#ifdef HAVE_CONFIG_H |
| 5 | +#include <config.h> |
| 6 | +#endif |
| 7 | + |
| 8 | +#define _GNU_SOURCE |
| 9 | +#include <string.h> |
| 10 | +#include <stdio.h> |
| 11 | +#include <time.h> |
| 12 | +#ifdef WIN32 |
| 13 | +#include <windows.h> |
| 14 | +#endif |
| 15 | + |
| 16 | +#include <usbmuxd.h> |
| 17 | + |
| 18 | +#include "idevice_ext.h" |
| 19 | + |
| 20 | +typedef struct { |
| 21 | + unsigned char *data; |
| 22 | + unsigned int size; |
| 23 | +} key_data_t; |
| 24 | + |
| 25 | +int read_pair_record(const char *udid, plist_t *pair_record) { |
| 26 | + char* record_data = NULL; |
| 27 | + uint32_t record_size = 0; |
| 28 | + |
| 29 | + int res = usbmuxd_read_pair_record(udid, &record_data, &record_size); |
| 30 | + if (res < 0) { |
| 31 | + free(record_data); |
| 32 | + return -1; |
| 33 | + } |
| 34 | + |
| 35 | + *pair_record = NULL; |
| 36 | + plist_from_memory(record_data, record_size, pair_record); |
| 37 | + free(record_data); |
| 38 | + |
| 39 | + if (!*pair_record) { |
| 40 | + return -1; |
| 41 | + } |
| 42 | + |
| 43 | + return 0; |
| 44 | +} |
| 45 | + |
| 46 | +int pair_record_get_item_as_key_data(plist_t pair_record, const char* name, key_data_t *value) { |
| 47 | + char* buffer = NULL; |
| 48 | + uint64_t length = 0; |
| 49 | + plist_t node = plist_dict_get_item(pair_record, name); |
| 50 | + |
| 51 | + if (node && plist_get_node_type(node) == PLIST_DATA) { |
| 52 | + plist_get_data_val(node, &buffer, &length); |
| 53 | + value->data = (unsigned char*)malloc(length+1); |
| 54 | + memcpy(value->data, buffer, length); |
| 55 | + value->data[length] = '\0'; |
| 56 | + value->size = length+1; |
| 57 | + free(buffer); |
| 58 | + return 0; |
| 59 | + } |
| 60 | + |
| 61 | + return -1; |
| 62 | +} |
| 63 | + |
| 64 | +int idevice_ext_connection_enable_ssl(const char *device_id, int fd, SSL **to_session) { |
| 65 | + plist_t pair_record = NULL; |
| 66 | + if (read_pair_record(device_id, &pair_record)) { |
| 67 | + fprintf(stderr, "Failed to read pair record\n"); |
| 68 | + return -1; |
| 69 | + } |
| 70 | + |
| 71 | + key_data_t root_cert = { NULL, 0 }; |
| 72 | + key_data_t root_privkey = { NULL, 0 }; |
| 73 | + pair_record_get_item_as_key_data(pair_record, "RootCertificate", &root_cert); |
| 74 | + pair_record_get_item_as_key_data(pair_record, "RootPrivateKey", &root_privkey); |
| 75 | + plist_free(pair_record); |
| 76 | + |
| 77 | + BIO *ssl_bio = BIO_new(BIO_s_socket()); |
| 78 | + if (!ssl_bio) { |
| 79 | + fprintf(stderr, "Could not create SSL bio\n"); |
| 80 | + return -1; |
| 81 | + } |
| 82 | + |
| 83 | + BIO_set_fd(ssl_bio, fd, BIO_NOCLOSE); |
| 84 | + SSL_CTX *ssl_ctx = SSL_CTX_new(TLS_method()); |
| 85 | + if (ssl_ctx == NULL) { |
| 86 | + fprintf(stderr, "Could not create SSL context\n"); |
| 87 | + BIO_free(ssl_bio); |
| 88 | + } |
| 89 | + |
| 90 | + SSL_CTX_set_security_level(ssl_ctx, 0); |
| 91 | + SSL_CTX_set_min_proto_version(ssl_ctx, TLS1_VERSION); |
| 92 | + |
| 93 | + BIO* membp; |
| 94 | + X509* rootCert = NULL; |
| 95 | + membp = BIO_new_mem_buf(root_cert.data, root_cert.size); |
| 96 | + PEM_read_bio_X509(membp, &rootCert, NULL, NULL); |
| 97 | + BIO_free(membp); |
| 98 | + SSL_CTX_use_certificate(ssl_ctx, rootCert); |
| 99 | + X509_free(rootCert); |
| 100 | + free(root_cert.data); |
| 101 | + |
| 102 | + RSA* rootPrivKey = NULL; |
| 103 | + membp = BIO_new_mem_buf(root_privkey.data, root_privkey.size); |
| 104 | + PEM_read_bio_RSAPrivateKey(membp, &rootPrivKey, NULL, NULL); |
| 105 | + BIO_free(membp); |
| 106 | + SSL_CTX_use_RSAPrivateKey(ssl_ctx, rootPrivKey); |
| 107 | + RSA_free(rootPrivKey); |
| 108 | + free(root_privkey.data); |
| 109 | + |
| 110 | + SSL *ssl = SSL_new(ssl_ctx); |
| 111 | + if (!ssl) { |
| 112 | + fprintf(stderr, "Could not create SSL object\n"); |
| 113 | + BIO_free(ssl_bio); |
| 114 | + SSL_CTX_free(ssl_ctx); |
| 115 | + return -1; |
| 116 | + } |
| 117 | + |
| 118 | + SSL_set_connect_state(ssl); |
| 119 | + SSL_set_verify(ssl, 0, NULL); |
| 120 | + SSL_set_bio(ssl, ssl_bio, ssl_bio); |
| 121 | + |
| 122 | + int ssl_error = 0; |
| 123 | + while (1) { |
| 124 | + ssl_error = SSL_get_error(ssl, SSL_do_handshake(ssl)); |
| 125 | + if (ssl_error == 0 || ssl_error != SSL_ERROR_WANT_READ) { |
| 126 | + break; |
| 127 | + } |
| 128 | +#ifdef WIN32 |
| 129 | + Sleep(100); |
| 130 | +#else |
| 131 | + struct timespec ts = { 0, 100000000 }; |
| 132 | + nanosleep(&ts, NULL); |
| 133 | +#endif |
| 134 | + } |
| 135 | + |
| 136 | + if (ssl_error != 0) { |
| 137 | + SSL_free(ssl); |
| 138 | + SSL_CTX_free(ssl_ctx); |
| 139 | + return ssl_error; |
| 140 | + } |
| 141 | + |
| 142 | + *to_session = ssl; |
| 143 | + return 0; |
| 144 | +} |
0 commit comments