Advertisement
opexxx

add_ca_to_iossim.py

Aug 21st, 2014
359
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.58 KB | None | 0 0
  1. '''
  2. add_ca_to_iossim.py v0.1
  3. Copyright (C) 2011 Ron Gutierrez
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. '''
  15. import sqlite3
  16. import os
  17. import subprocess
  18. from optparse import OptionParser
  19. __usage__ = """
  20. Please supply required arguments: <CA Certificate Path>
  21. add_ca_to_iossim.py <CA Certificate Path>
  22. """
  23. simulator_dir = os.getenv('HOME')+"/Library/Application Support/iPhone Simulator/"
  24. truststore_path = "/Library/Keychains/TrustStore.sqlite3"
  25. def cert_fingerprint_via_openssl(cert_location):
  26. output = subprocess.check_output(["openssl", "x509", "-noout", "-in", cert_location, "-fingerprint"])
  27. fingerprint_with_colons = output.split("=")[1]
  28. return fingerprint_with_colons.replace(':','')
  29. def cert_fingerprint(cert_location):
  30. try:
  31. from M2Crypto import X509  
  32. cert = X509.load_cert(cert_location)
  33. return cert.get_fingerprint('sha1')
  34. except ImportError:
  35. return cert_fingerprint_via_openssl(cert_location) 
  36. def add_to_truststore(sdk_dir, cert_fingerprint):
  37. tpath = simulator_dir+sdk_dir+truststore_path
  38. sha1="X'"+cert_fingerprint.strip()+"'"
  39. try:
  40. conn = sqlite3.connect(simulator_dir+sdk_dir+truststore_path)
  41. c = conn.cursor()
  42. sql = 'insert into tsettings values (%s,%s,%s,%s)'%(sha1, "randomblob(16)", "randomblob(16)", "randomblob(16)")
  43. c.execute(sql)
  44. conn.commit()
  45. c.close()
  46. conn.close()
  47. print("Successfully added CA to %s" % tpath)
  48. except sqlite3.OperationalError:
  49. print("Error adding CA to %s" % tpath )
  50. print("Mostly likely failed because Truststore does not exist..skipping\n")
  51. return
  52. except sqlite3.IntegrityError:
  53. print("Error adding CA to %s" % tpath )
  54. print("Table already has an entry with the same CA SHA1 fingerprint..skipping\n")
  55. return
  56. if __name__ == "__main__":
  57. parser = OptionParser(usage=__usage__)
  58. opt, args = parser.parse_args()
  59. if len(args) < 1:
  60. parser.print_help()
  61. exit(1)
  62. cert_location = args[0]
  63. cert_fingerprint = cert_fingerprint(cert_location)
  64. for sdk_dir in os.listdir(simulator_dir):
  65. if not sdk_dir.startswith('.') and sdk_dir != 'User':
  66. add_to_truststore(sdk_dir, cert_fingerprint)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement