-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathsigutil.py
More file actions
executable file
·90 lines (70 loc) · 2.42 KB
/
sigutil.py
File metadata and controls
executable file
·90 lines (70 loc) · 2.42 KB
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
78
79
80
81
82
83
84
85
86
87
88
from dns.resolver import dns
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.asymmetric import padding
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.backends import default_backend
from base64 import b64decode, b64encode
def verify_sig(public_key, signature, data):
try:
public_key = public_key.encode('ascii')
data = data.encode()
pk = serialization.load_pem_public_key(
public_key,
backend=default_backend()
)
pk.verify(b64decode(signature),
data,
padding.PKCS1v15(),
hashes.SHA256()
)
return True
except:
return False
# Generates a signature on the passed in data
def generate_sig(private_key, data):
try:
private_key = private_key.encode('ascii')
data = data.encode()
pk = serialization.load_pem_private_key(
private_key,
password=None,
backend=default_backend()
)
sig = pk.sign(
data,
padding.PKCS1v15(),
hashes.SHA256()
)
return b64encode(sig)
except:
return None
# Get TXT records and parse them for the public key
def get_publickey(domain):
try:
segments = {}
records = dns.resolver.query(domain, 'TXT') # Get all text records
record_strings = []
for text in records:
text = text.strings[0].decode('utf-8')
record_strings.append(text)
split_text = text.split(',') # Separate the components
index = -1
indexData = None
for kv in split_text:
if kv.startswith('p='):
index = int(kv[2:])
elif kv.startswith('d='):
indexData = kv[2:]
elif kv.startswith('a=') and kv != 'a=RS256':
return None, None
elif kv.startswith('t=') and kv != 't=x509':
return None, None
if index != -1 and indexData != None:
segments[index] = indexData
pembits = ''
# Concatenate all of the key segments
for key in sorted(list(segments.keys())):
pembits = pembits + segments[key].strip('\n').strip('\\n').strip()
return pembits, record_strings
except:
return None, None