forked from czervenka/gapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert_key.py
executable file
·64 lines (58 loc) · 2.15 KB
/
convert_key.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
#!/usr/bin/env python
#
# Copyright 2007 Robin Gottfried <[email protected]>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
__author__ = 'Robin Gottfried <[email protected]>'
from os.path import basename
import sys
try:
from OpenSSL.crypto import load_pkcs12, dump_privatekey, FILETYPE_PEM
except ImportError:
print """OpenSSL library not found. Please install pyOpenSSL."""
try:
import pip
except ImportError:
pass
else:
print "I found that you have pip installed, would you like me to install pyOpenSSL for you? [y/n]"
answer = raw_input()
if answer.lower() in ['y', 'yes', 'yeah', 'yeah!']:
pip.main(['install', 'pyOpenSSL'])
from OpenSSL.crypto import load_pkcs12, dump_privatekey, FILETYPE_PEM
print "\n\n\nOpenSSL library installed.\n\n\n"
else:
print "Exitting.\n\nYou must install pyOpenSSL manually to use this utility.\n(ie. by running `pip install pyOpenSSL`\n\n"
def _usage():
print """USAGE: %s <pkcs12_file> [<pem_file>]
pkcs12_file ... the key you downloaded from Google Console
pem_file ... the key to be used in this library
If no pem_file is specified as argument, it will be written to std out.
""" % basename(sys.argv[0])
def main(*args):
if len(args) < 2:
_usage()
sys.exit(1)
archive = load_pkcs12(open(args[1], 'rb').read(), 'notasecret')
key = archive.get_privatekey()
pem = dump_privatekey(FILETYPE_PEM, key)
if len(args) > 2:
f = open(args[2], 'wb')
f.write(pem)
f.close()
return ''
else:
return pem
if __name__ == '__main__':
print main(*sys.argv)