├── README └── add_ca_to_iossim.py /README: -------------------------------------------------------------------------------- 1 | Sample Usage: 2 | 3 | python add_ca_to_iossim.py 4 | 5 | ========================================== 6 | 7 | Tested using: 8 | 9 | Python 2.7 10 | Mac OS X Snow Leopard & Lion 11 | Xcode 4 12 | 13 | =========================================== 14 | 15 | Optional Dependencies 16 | 17 | M2Crypto Library 18 | 19 | - Install Instructions 20 | 1. easy_install M2Crypto 21 | 22 | If M2Crypto library is not found the script will call the openssl executable which should be installed by default on Mac OS X. 23 | -------------------------------------------------------------------------------- /add_ca_to_iossim.py: -------------------------------------------------------------------------------- 1 | ''' 2 | 3 | add_ca_to_iossim.py v0.1 4 | Copyright (C) 2011 Ron Gutierrez 5 | 6 | This program is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | This program is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with this program. If not, see . 18 | 19 | ''' 20 | 21 | import sqlite3 22 | import os 23 | import subprocess 24 | from optparse import OptionParser 25 | 26 | __usage__ = """ 27 | Please supply required arguments: 28 | 29 | add_ca_to_iossim.py 30 | """ 31 | 32 | simulator_dir = os.getenv('HOME')+"/Library/Application Support/iPhone Simulator/" 33 | truststore_path = "/Library/Keychains/TrustStore.sqlite3" 34 | 35 | 36 | def cert_fingerprint_via_openssl(cert_location): 37 | output = subprocess.check_output(["openssl", "x509", "-noout", "-in", cert_location, "-fingerprint"]) 38 | fingerprint_with_colons = output.split("=")[1] 39 | return fingerprint_with_colons.replace(':','') 40 | 41 | 42 | def cert_fingerprint(cert_location): 43 | try: 44 | from M2Crypto import X509 45 | cert = X509.load_cert(cert_location) 46 | return cert.get_fingerprint('sha1') 47 | except ImportError: 48 | return cert_fingerprint_via_openssl(cert_location) 49 | 50 | 51 | def add_to_truststore(sdk_dir, cert_fingerprint): 52 | tpath = simulator_dir+sdk_dir+truststore_path 53 | 54 | sha1="X'"+cert_fingerprint.strip()+"'" 55 | 56 | try: 57 | conn = sqlite3.connect(simulator_dir+sdk_dir+truststore_path) 58 | c = conn.cursor() 59 | sql = 'insert into tsettings values (%s,%s,%s,%s)'%(sha1, "randomblob(16)", "randomblob(16)", "randomblob(16)") 60 | c.execute(sql) 61 | conn.commit() 62 | 63 | c.close() 64 | conn.close() 65 | print("Successfully added CA to %s" % tpath) 66 | except sqlite3.OperationalError: 67 | print("Error adding CA to %s" % tpath ) 68 | print("Mostly likely failed because Truststore does not exist..skipping\n") 69 | return 70 | except sqlite3.IntegrityError: 71 | print("Error adding CA to %s" % tpath ) 72 | print("Table already has an entry with the same CA SHA1 fingerprint..skipping\n") 73 | return 74 | 75 | if __name__ == "__main__": 76 | parser = OptionParser(usage=__usage__) 77 | opt, args = parser.parse_args() 78 | 79 | if len(args) < 1: 80 | parser.print_help() 81 | exit(1) 82 | 83 | cert_location = args[0] 84 | 85 | cert_fingerprint = cert_fingerprint(cert_location) 86 | 87 | for sdk_dir in os.listdir(simulator_dir): 88 | if not sdk_dir.startswith('.') and sdk_dir != 'User': 89 | add_to_truststore(sdk_dir, cert_fingerprint) 90 | --------------------------------------------------------------------------------