79 lines
1.7 KiB
Python
79 lines
1.7 KiB
Python
|
|
import subprocess as _subprocess
|
||
|
|
|
||
|
|
|
||
|
|
def get_certificate_info_raw(source):
|
||
|
|
p1 = _subprocess.Popen(
|
||
|
|
["openssl", "x509", "-outform", "pem"],
|
||
|
|
stdin = _subprocess.PIPE,
|
||
|
|
stdout = _subprocess.PIPE
|
||
|
|
)
|
||
|
|
(p1_stdout, p1_stderr, ) = p1.communicate(input = bytes(source, "utf-8"))
|
||
|
|
p2 = _subprocess.Popen(
|
||
|
|
["openssl", "x509", "-noout", "-text"],
|
||
|
|
stdin = _subprocess.PIPE,
|
||
|
|
stdout = _subprocess.PIPE
|
||
|
|
)
|
||
|
|
(p2_stdout, p2_stderr, ) = p2.communicate(input = p1_stdout)
|
||
|
|
return p2_stdout.decode("utf-8")
|
||
|
|
|
||
|
|
|
||
|
|
def get_certificate_info_from_file(path):
|
||
|
|
return get_certificate_info_raw(
|
||
|
|
_subprocess.check_output(
|
||
|
|
["cat", path],
|
||
|
|
text = True
|
||
|
|
)
|
||
|
|
)
|
||
|
|
|
||
|
|
|
||
|
|
def get_certificate_info_from_internet(domain):
|
||
|
|
return get_certificate_info_raw(
|
||
|
|
_subprocess.check_output(
|
||
|
|
["openssl", "s_client", "-connect", ("%s:443" % domain), "-showcerts"],
|
||
|
|
stdin = _subprocess.DEVNULL,
|
||
|
|
stderr = _subprocess.DEVNULL,
|
||
|
|
text = True
|
||
|
|
)
|
||
|
|
)
|
||
|
|
|
||
|
|
|
||
|
|
def extract_fingerprint(certificate_info):
|
||
|
|
state = {
|
||
|
|
"situation": 0,
|
||
|
|
"result": "",
|
||
|
|
}
|
||
|
|
for line in certificate_info.split("\n"):
|
||
|
|
if (state["situation"] == 0):
|
||
|
|
if (line.strip() == "Subject Public Key Info:"):
|
||
|
|
state = {
|
||
|
|
"situation": 1,
|
||
|
|
"result": state["result"],
|
||
|
|
}
|
||
|
|
else:
|
||
|
|
pass
|
||
|
|
elif (state["situation"] == 1):
|
||
|
|
if (line.strip() == "pub:"):
|
||
|
|
state = {
|
||
|
|
"situation": 2,
|
||
|
|
"result": state["result"],
|
||
|
|
}
|
||
|
|
else:
|
||
|
|
pass
|
||
|
|
elif (state["situation"] == 2):
|
||
|
|
if (line.startswith(" ")):
|
||
|
|
state = {
|
||
|
|
"situation": 2,
|
||
|
|
"result": (state["result"] + line.strip().replace(":", "")),
|
||
|
|
}
|
||
|
|
else:
|
||
|
|
state = {
|
||
|
|
"situation": 3,
|
||
|
|
"result": state["result"]
|
||
|
|
}
|
||
|
|
break
|
||
|
|
else:
|
||
|
|
pass
|
||
|
|
return state["result"]
|
||
|
|
|
||
|
|
|