-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththumbprint_util.py
75 lines (60 loc) · 2.16 KB
/
thumbprint_util.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
import hashlib
import ssl
import subprocess
import re
class Encryption(str, Enum):
SHA1 = "sha1"
SHA256 = "sha256"
MD5 = "md5"
def get_thumbprint_cmd(hostname, port=443, algorithm: Encryption = Encryption.SHA256):
"""
Returns SSL thumbprint for specified host using openssl cmd
:param hostname: FQDN or IP of the host
:param port: port number of the host. Default is 443
:param algorithm: Hashing algorithm. Default is sha256
"""
try:
cmd = "openssl s_client -connect %s:%s < /dev/null 2>/dev/null | openssl x509 -fingerprint -noout -%s -in /dev/stdin" % (hostname, port, algorithm)
output = subprocess.check_output(['/bin/sh', '-c', cmd])
thumbprint = output.decode().strip()
return thumbprint
except subprocess.CalledProcessError as err:
raise err
except Exception as e:
raise e
def get_pem_cert(address, port=443):
"""
Returns certificate for specified host in pem format
:param adddress: FQDN or IP of the host
:param port: port number of the host. Default is 443
"""
try:
cert = ssl.get_server_certificate((address, port))
except Exception as ex:
raise Exception(f"Failed to connect to address: {address}. Exception: {ex}")
return cert
def get_thumbprint(hostname, port=443, algorithm: Encryption = Encryption.SHA256):
"""
Returns SSL thumbprint for specified host
:param hostname: FQDN or IP of the host
:param port: port number of the host. Default is 443
:param algorithm: Hashing algorithm. Default is sha256
"""
cert = get_pem_cert(address, port)
der_cert_bin = ssl.PEM_cert_to_DER_cert(cert)
if encryption == Encryption.SHA1:
thumbprint = hashlib.sha1(der_cert_bin).hexdigest()
elif encryption == Encryption.SHA256:
thumbprint = hashlib.sha256(der_cert_bin).hexdigest()
elif encryption == Encryption.MD5:
thumbprint = hashlib.md5(der_cert_bin).hexdigest()
else:
return der_cert_bin
return thumbprint
def format_thumbprint(thumbprint):
"""
Returns SSL thumbprint without colon seperators
:param thumbprint: thumbrpint string of the format "SHAx thumbprint=y"
"""
thumbprint_string = thumbprint.split("=")[1]
return re.sub(":", "", thumbprint_string).lower()