forked from GaryLaurenceau/generate-keystore-bks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.py
executable file
·78 lines (62 loc) · 2.56 KB
/
generate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/env python
import argparse
import sys
from os import path
import subprocess
import getpass
OPENSSL_CMD='openssl x509 -inform PEM -subject_hash -noout -in %s'
def setup_arg_parser():
parser = argparse.ArgumentParser(description='Generate keystore BKS file for Android\
to handle self signed HTTPS connection.')
parser.version = 1.0
parser.add_argument('-bc', '--bc-jar-location', nargs=1, dest='jar_file', required=True,
help='set bouncy castle jar file location')
parser.add_argument('-ca', '--cacert', nargs=1, dest='cacert_file', required=True,
help='set CA cert file location')
parser.add_argument('-n', '--bks-name', nargs=1, dest='name', default=['key_store.bks'],
help='set the name of the bks file generated')
parser.add_argument('-p', '--password', nargs=1, dest='password', default=[None],
help='set password')
return parser
def get_openssl_result(file):
result_code = subprocess.check_output(
['openssl', 'x509', '-inform', 'PEM', '-subject_hash', '-noout', '-in', file],
)
return result_code
def create_keystore(jar_file, cacert_file, code, name, password):
print password
while password is None:
password = getpass.getpass(prompt="Password")
if password is None or len(password) < 6:
print "Password should contain at least 6 characters"
password = None
continue
subprocess.call(
['keytool', '-import', '-v', '-trustcacerts', '-alias', code, '-file', cacert_file,
'-keystore', name,
'-storetype', 'BKS',
'-providerclass', 'org.bouncycastle.jce.provider.BouncyCastleProvider',
'-providerpath', jar_file,
'-storepass', password]
)
def main():
arg_parser = setup_arg_parser()
arg = arg_parser.parse_args()
jar_file = arg.jar_file[0]
cacert_file = arg.cacert_file[0]
name = arg.name[0]
password = arg.password[0]
if path.isfile(jar_file) is False:
print jar_file, "is not a file."
sys.exit()
elif path.isfile(cacert_file) is False:
print cacert_file, "is not a file."
sys.exit()
print ("Generating alias...")
result_code = get_openssl_result(cacert_file)
result_code = result_code.strip()
print ("Adding %s with alias %s to %s." % (jar_file, result_code, name))
create_keystore(jar_file, cacert_file, result_code, name, password)
print "%s has been created" % name
if __name__ == "__main__":
main()