summaryrefslogtreecommitdiffstats
path: root/src/lockdown.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lockdown.c')
-rw-r--r--src/lockdown.c183
1 files changed, 90 insertions, 93 deletions
diff --git a/src/lockdown.c b/src/lockdown.c
index 3d39fbd..55e2e65 100644
--- a/src/lockdown.c
+++ b/src/lockdown.c
@@ -662,113 +662,110 @@ iphone_error_t lockdownd_gen_pair_cert(char *public_key_b64, char **device_cert_
662 */ 662 */
663iphone_error_t lockdownd_start_SSL_session(iphone_lckd_client_t control, const char *HostID) 663iphone_error_t lockdownd_start_SSL_session(iphone_lckd_client_t control, const char *HostID)
664{ 664{
665 xmlDocPtr plist = new_plist(); 665 plist_t plist = NULL;
666 xmlNode *dict = add_child_to_plist(plist, "dict", "\n", NULL, 0); 666 dict_t dict = NULL;
667 xmlNode *key; 667 char *XML_content = NULL;
668 char *what2send = NULL, **dictionary = NULL; 668 uint32_t length = 0, bytes = 0, return_me = 0;
669 uint32_t len = 0, bytes = 0, return_me = 0, i = 0; 669
670 iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; 670 iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR;
671 // end variables
672 671
673 key = add_key_str_dict_element(plist, dict, "HostID", HostID, 1); 672 /* Setup DevicePublicKey request plist */
674 if (!key) { 673 plist_new_plist(&plist);
675 log_debug_msg("Couldn't add a key.\n"); 674 plist_new_dict_in_plist(plist, &dict);
676 xmlFreeDoc(plist); 675 plist_add_dict_element(dict, "HostID", PLIST_STRING, (void *) HostID);
677 return IPHONE_E_DICT_ERROR; 676 plist_add_dict_element(dict, "Request", PLIST_STRING, (void *) "StartSession");
678 } 677 plist_to_xml(plist, &XML_content, &length);
679 key = add_key_str_dict_element(plist, dict, "Request", "StartSession", 1); 678 log_debug_msg("Send msg :\nsize : %i\nxml : %s", length, XML_content);
680 if (!key) {
681 log_debug_msg("Couldn't add a key.\n");
682 xmlFreeDoc(plist);
683 return IPHONE_E_DICT_ERROR;
684 }
685 679
686 xmlDocDumpMemory(plist, (xmlChar **) & what2send, &len); 680 ret = iphone_lckd_send(control, XML_content, length, &bytes);
687 ret = iphone_lckd_send(control, what2send, len, &bytes);
688 681
689 xmlFree(what2send); 682 xmlFree(XML_content);
690 xmlFreeDoc(plist); 683 XML_content = NULL;
684 plist_free(plist);
685 plist = NULL;
691 686
692 if (ret != IPHONE_E_SUCCESS) 687 if (ret != IPHONE_E_SUCCESS)
693 return ret; 688 return ret;
694 689
695 if (bytes > 0) { 690 if (bytes > 0) {
696 ret = iphone_lckd_recv(control, &what2send, &len); 691 ret = iphone_lckd_recv(control, &XML_content, &bytes);
697 plist = xmlReadMemory(what2send, len, NULL, NULL, 0); 692 log_debug_msg("Receive msg :\nsize : %i\nxml : %s", bytes, XML_content);
698 dict = xmlDocGetRootElement(plist); 693 xml_to_plist(XML_content, bytes, &plist);
699 if (!dict) 694 if (!plist)
700 return IPHONE_E_DICT_ERROR; 695 return IPHONE_E_PLIST_ERROR;
701 for (dict = dict->children; dict; dict = dict->next) { 696
702 if (!xmlStrcmp(dict->name, "dict")) 697 plist_t query_node = find_query_node(plist, "Request", "StartSession");
703 break; 698 plist_t result_key_node = g_node_next_sibling(query_node);
704 } 699 plist_t result_value_node = g_node_next_sibling(result_key_node);
705 dictionary = read_dict_element_strings(dict); 700
706 xmlFreeDoc(plist); 701 plist_type result_key_type;
707 free(what2send); 702 plist_type result_value_type;
708 for (i = 0; dictionary[i]; i += 2) { 703 char *result_key = NULL;
709 if (!strcmp(dictionary[i], "Result") && !strcmp(dictionary[i + 1], "Success")) { 704 char *result_value = NULL;
710 // Set up GnuTLS... 705
711 //gnutls_anon_client_credentials_t anoncred; 706 get_type_and_value(result_key_node, &result_key_type, (void *) (&result_key));
712 gnutls_certificate_credentials_t xcred; 707 get_type_and_value(result_value_node, &result_value_type, (void *) (&result_value));
713 708
714 log_debug_msg("We started the session OK, now trying GnuTLS\n"); 709 xmlFree(XML_content);
715 errno = 0; 710 XML_content = NULL;
716 gnutls_global_init(); 711 plist_free(plist);
717 //gnutls_anon_allocate_client_credentials(&anoncred); 712 plist = NULL;
718 gnutls_certificate_allocate_credentials(&xcred); 713
719 gnutls_certificate_set_x509_trust_file(xcred, "hostcert.pem", GNUTLS_X509_FMT_PEM); 714 if (result_key_type == PLIST_KEY &&
720 gnutls_init(control->ssl_session, GNUTLS_CLIENT); 715 result_value_type == PLIST_STRING && !strcmp(result_key, "Result") && !strcmp(result_value, "Success")) {
721 { 716 // Set up GnuTLS...
722 int protocol_priority[16] = { GNUTLS_SSL3, 0 }; 717 //gnutls_anon_client_credentials_t anoncred;
723 int kx_priority[16] = { GNUTLS_KX_ANON_DH, GNUTLS_KX_RSA, 0 }; 718 gnutls_certificate_credentials_t xcred;
724 int cipher_priority[16] = { GNUTLS_CIPHER_AES_128_CBC, GNUTLS_CIPHER_AES_256_CBC, 0 }; 719
725 int mac_priority[16] = { GNUTLS_MAC_SHA1, GNUTLS_MAC_MD5, 0 }; 720 log_debug_msg("We started the session OK, now trying GnuTLS\n");
726 int comp_priority[16] = { GNUTLS_COMP_NULL, 0 }; 721 errno = 0;
727 722 gnutls_global_init();
728 gnutls_cipher_set_priority(*control->ssl_session, cipher_priority); 723 //gnutls_anon_allocate_client_credentials(&anoncred);
729 gnutls_compression_set_priority(*control->ssl_session, comp_priority); 724 gnutls_certificate_allocate_credentials(&xcred);
730 gnutls_kx_set_priority(*control->ssl_session, kx_priority); 725 gnutls_certificate_set_x509_trust_file(xcred, "hostcert.pem", GNUTLS_X509_FMT_PEM);
731 gnutls_protocol_set_priority(*control->ssl_session, protocol_priority); 726 gnutls_init(control->ssl_session, GNUTLS_CLIENT);
732 gnutls_mac_set_priority(*control->ssl_session, mac_priority); 727 {
733 728 int protocol_priority[16] = { GNUTLS_SSL3, 0 };
734 } 729 int kx_priority[16] = { GNUTLS_KX_ANON_DH, GNUTLS_KX_RSA, 0 };
735 gnutls_credentials_set(*control->ssl_session, GNUTLS_CRD_CERTIFICATE, xcred); // this part is killing me. 730 int cipher_priority[16] = { GNUTLS_CIPHER_AES_128_CBC, GNUTLS_CIPHER_AES_256_CBC, 0 };
736 731 int mac_priority[16] = { GNUTLS_MAC_SHA1, GNUTLS_MAC_MD5, 0 };
737 log_debug_msg("GnuTLS step 1...\n"); 732 int comp_priority[16] = { GNUTLS_COMP_NULL, 0 };
738 gnutls_transport_set_ptr(*control->ssl_session, (gnutls_transport_ptr_t) control); 733
739 log_debug_msg("GnuTLS step 2...\n"); 734 gnutls_cipher_set_priority(*control->ssl_session, cipher_priority);
740 gnutls_transport_set_push_function(*control->ssl_session, (gnutls_push_func) & lockdownd_secuwrite); 735 gnutls_compression_set_priority(*control->ssl_session, comp_priority);
741 log_debug_msg("GnuTLS step 3...\n"); 736 gnutls_kx_set_priority(*control->ssl_session, kx_priority);
742 gnutls_transport_set_pull_function(*control->ssl_session, (gnutls_pull_func) & lockdownd_securead); 737 gnutls_protocol_set_priority(*control->ssl_session, protocol_priority);
743 log_debug_msg("GnuTLS step 4 -- now handshaking...\n"); 738 gnutls_mac_set_priority(*control->ssl_session, mac_priority);
744 739
745 if (errno) 740 }
746 log_debug_msg("WARN: errno says %s before handshake!\n", strerror(errno)); 741 gnutls_credentials_set(*control->ssl_session, GNUTLS_CRD_CERTIFICATE, xcred); // this part is killing me.
747 return_me = gnutls_handshake(*control->ssl_session); 742
748 log_debug_msg("GnuTLS handshake done...\n"); 743 log_debug_msg("GnuTLS step 1...\n");
749 744 gnutls_transport_set_ptr(*control->ssl_session, (gnutls_transport_ptr_t) control);
750 free_dictionary(dictionary); 745 log_debug_msg("GnuTLS step 2...\n");
751 746 gnutls_transport_set_push_function(*control->ssl_session, (gnutls_push_func) & lockdownd_secuwrite);
752 if (return_me != GNUTLS_E_SUCCESS) { 747 log_debug_msg("GnuTLS step 3...\n");
753 log_debug_msg("GnuTLS reported something wrong.\n"); 748 gnutls_transport_set_pull_function(*control->ssl_session, (gnutls_pull_func) & lockdownd_securead);
754 gnutls_perror(return_me); 749 log_debug_msg("GnuTLS step 4 -- now handshaking...\n");
755 log_debug_msg("oh.. errno says %s\n", strerror(errno)); 750
756 return IPHONE_E_SSL_ERROR; 751 if (errno)
757 } else { 752 log_debug_msg("WARN: errno says %s before handshake!\n", strerror(errno));
758 control->in_SSL = 1; 753 return_me = gnutls_handshake(*control->ssl_session);
759 return IPHONE_E_SUCCESS; 754 log_debug_msg("GnuTLS handshake done...\n");
760 } 755
756 if (return_me != GNUTLS_E_SUCCESS) {
757 log_debug_msg("GnuTLS reported something wrong.\n");
758 gnutls_perror(return_me);
759 log_debug_msg("oh.. errno says %s\n", strerror(errno));
760 return IPHONE_E_SSL_ERROR;
761 } else {
762 control->in_SSL = 1;
763 return IPHONE_E_SUCCESS;
761 } 764 }
762 } 765 }
763 766
764 log_debug_msg("Apparently failed negotiating with lockdownd.\n"); 767 log_debug_msg("Apparently failed negotiating with lockdownd.\n");
765 log_debug_msg("Responding dictionary: \n"); 768 log_debug_msg("Responding dictionary: \n");
766 for (i = 0; dictionary[i]; i += 2) {
767 log_debug_msg("\t%s: %s\n", dictionary[i], dictionary[i + 1]);
768 }
769
770
771 free_dictionary(dictionary);
772 return IPHONE_E_SSL_ERROR; 769 return IPHONE_E_SSL_ERROR;
773 } else { 770 } else {
774 log_debug_msg("Didn't get enough bytes.\n"); 771 log_debug_msg("Didn't get enough bytes.\n");