annotate Tools/scripts/get-remote-certificate.py @ 78069:a8aa77458e12 3.2

Add c_size_t and c_ssize_t to the ctypes summary table.
author Antoine Pitrou <solipsis@pitrou.net>
date Thu, 12 Jul 2012 20:31:50 +0200
parents 3c9232a684ae
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
59522
fcfe324663a6 convert shebang lines: python -> python3
Benjamin Peterson <benjamin@python.org>
parents: 45471
diff changeset
1 #!/usr/bin/env python3
42954
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
2 #
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
3 # fetch the certificate that the server(s) are providing in PEM form
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
4 #
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
5 # args are HOST:PORT [, HOST:PORT...]
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
6 #
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
7 # By Bill Janssen.
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
8
65674
3ff99089c02e Fix bytes/str issues in get-remote-certificate.py.
Georg Brandl <georg@python.org>
parents: 59522
diff changeset
9 import re
3ff99089c02e Fix bytes/str issues in get-remote-certificate.py.
Georg Brandl <georg@python.org>
parents: 59522
diff changeset
10 import os
45471
7dd2c1746267 Merged revisions 60990-61002 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 42954
diff changeset
11 import sys
65674
3ff99089c02e Fix bytes/str issues in get-remote-certificate.py.
Georg Brandl <georg@python.org>
parents: 59522
diff changeset
12 import tempfile
3ff99089c02e Fix bytes/str issues in get-remote-certificate.py.
Georg Brandl <georg@python.org>
parents: 59522
diff changeset
13
42954
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
14
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
15 def fetch_server_certificate (host, port):
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
16
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
17 def subproc(cmd):
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
18 from subprocess import Popen, PIPE, STDOUT
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
19 proc = Popen(cmd, stdout=PIPE, stderr=STDOUT, shell=True)
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
20 status = proc.wait()
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
21 output = proc.stdout.read()
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
22 return status, output
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
23
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
24 def strip_to_x509_cert(certfile_contents, outfile=None):
65674
3ff99089c02e Fix bytes/str issues in get-remote-certificate.py.
Georg Brandl <georg@python.org>
parents: 59522
diff changeset
25 m = re.search(br"^([-]+BEGIN CERTIFICATE[-]+[\r]*\n"
3ff99089c02e Fix bytes/str issues in get-remote-certificate.py.
Georg Brandl <georg@python.org>
parents: 59522
diff changeset
26 br".*[\r]*^[-]+END CERTIFICATE[-]+)$",
42954
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
27 certfile_contents, re.MULTILINE | re.DOTALL)
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
28 if not m:
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
29 return None
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
30 else:
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
31 tn = tempfile.mktemp()
65674
3ff99089c02e Fix bytes/str issues in get-remote-certificate.py.
Georg Brandl <georg@python.org>
parents: 59522
diff changeset
32 fp = open(tn, "wb")
3ff99089c02e Fix bytes/str issues in get-remote-certificate.py.
Georg Brandl <georg@python.org>
parents: 59522
diff changeset
33 fp.write(m.group(1) + b"\n")
42954
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
34 fp.close()
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
35 try:
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
36 tn2 = (outfile or tempfile.mktemp())
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
37 status, output = subproc(r'openssl x509 -in "%s" -out "%s"' %
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
38 (tn, tn2))
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
39 if status != 0:
67223
54ef52f971a5 Fix NameErrors.
Georg Brandl <georg@python.org>
parents: 67222
diff changeset
40 raise RuntimeError('OpenSSL x509 failed with status %s and '
54ef52f971a5 Fix NameErrors.
Georg Brandl <georg@python.org>
parents: 67222
diff changeset
41 'output: %r' % (status, output))
42954
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
42 fp = open(tn2, 'rb')
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
43 data = fp.read()
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
44 fp.close()
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
45 os.unlink(tn2)
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
46 return data
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
47 finally:
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
48 os.unlink(tn)
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
49
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
50 if sys.platform.startswith("win"):
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
51 tfile = tempfile.mktemp()
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
52 fp = open(tfile, "w")
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
53 fp.write("quit\n")
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
54 fp.close()
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
55 try:
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
56 status, output = subproc(
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
57 'openssl s_client -connect "%s:%s" -showcerts < "%s"' %
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
58 (host, port, tfile))
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
59 finally:
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
60 os.unlink(tfile)
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
61 else:
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
62 status, output = subproc(
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
63 'openssl s_client -connect "%s:%s" -showcerts < /dev/null' %
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
64 (host, port))
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
65 if status != 0:
67223
54ef52f971a5 Fix NameErrors.
Georg Brandl <georg@python.org>
parents: 67222
diff changeset
66 raise RuntimeError('OpenSSL connect failed with status %s and '
54ef52f971a5 Fix NameErrors.
Georg Brandl <georg@python.org>
parents: 67222
diff changeset
67 'output: %r' % (status, output))
42954
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
68 certtext = strip_to_x509_cert(output)
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
69 if not certtext:
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
70 raise ValueError("Invalid response received from server at %s:%s" %
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
71 (host, port))
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
72 return certtext
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
73
65674
3ff99089c02e Fix bytes/str issues in get-remote-certificate.py.
Georg Brandl <georg@python.org>
parents: 59522
diff changeset
74
42954
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
75 if __name__ == "__main__":
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
76 if len(sys.argv) < 2:
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
77 sys.stderr.write(
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
78 "Usage: %s HOSTNAME:PORTNUMBER [, HOSTNAME:PORTNUMBER...]\n" %
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
79 sys.argv[0])
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
80 sys.exit(1)
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
81 for arg in sys.argv[1:]:
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
82 host, port = arg.split(":")
65674
3ff99089c02e Fix bytes/str issues in get-remote-certificate.py.
Georg Brandl <georg@python.org>
parents: 59522
diff changeset
83 sys.stdout.buffer.write(fetch_server_certificate(host, int(port)))
42954
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
84 sys.exit(0)