1
|
1 |
|
import os |
2
|
|
|
|
3
|
1 |
|
from cryptography import x509 |
4
|
|
|
from cryptography.exceptions import InvalidSignature |
5
|
|
|
from cryptography.hazmat.backends import default_backend |
6
|
|
|
from cryptography.hazmat.primitives import serialization |
7
|
|
|
from cryptography.hazmat.primitives import hashes |
8
|
|
|
from cryptography.hazmat.primitives import hmac |
9
|
|
|
from cryptography.hazmat.primitives.asymmetric import padding |
10
|
|
|
from cryptography.hazmat.primitives.ciphers import Cipher |
11
|
|
|
from cryptography.hazmat.primitives.ciphers import algorithms |
12
|
|
|
from cryptography.hazmat.primitives.ciphers import modes |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
def load_certificate(path): |
16
|
|
|
_, ext = os.path.splitext(path) |
17
|
|
|
with open(path, "br") as f: |
18
|
|
|
if ext == ".pem": |
19
|
|
|
return x509.load_pem_x509_certificate(f.read(), default_backend()) |
20
|
|
|
else: |
21
|
|
|
return x509.load_der_x509_certificate(f.read(), default_backend()) |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
def x509_from_der(data): |
25
|
|
|
if not data: |
26
|
|
|
return None |
27
|
|
|
return x509.load_der_x509_certificate(data, default_backend()) |
28
|
|
|
|
29
|
|
|
|
30
|
|
|
def x509_to_der(cert): |
31
|
|
|
if not cert: |
32
|
|
|
return b'' |
33
|
|
|
return cert.public_bytes(serialization.Encoding.DER) |
34
|
|
|
|
35
|
|
|
|
36
|
|
|
def load_private_key(path): |
37
|
|
|
_, ext = os.path.splitext(path) |
38
|
|
|
with open(path, "br") as f: |
39
|
|
|
if ext == ".pem": |
40
|
|
|
return serialization.load_pem_private_key(f.read(), password=None, backend=default_backend()) |
41
|
|
|
else: |
42
|
|
|
return serialization.load_der_private_key(f.read(), password=None, backend=default_backend()) |
43
|
|
|
|
44
|
|
|
|
45
|
|
|
def der_from_x509(certificate): |
46
|
|
|
if certificate is None: |
47
|
|
|
return b"" |
48
|
|
|
return certificate.public_bytes(serialization.Encoding.DER) |
49
|
|
|
|
50
|
|
|
|
51
|
|
|
def sign_sha1(private_key, data): |
52
|
|
|
signer = private_key.signer( |
53
|
|
|
padding.PKCS1v15(), |
54
|
|
|
hashes.SHA1() |
55
|
|
|
) |
56
|
|
|
signer.update(data) |
57
|
|
|
return signer.finalize() |
58
|
|
|
|
59
|
|
|
|
60
|
|
|
def verify_sha1(certificate, data, signature): |
61
|
|
|
verifier = certificate.public_key().verifier( |
62
|
|
|
signature, |
63
|
|
|
padding.PKCS1v15(), |
64
|
|
|
hashes.SHA1()) |
65
|
|
|
verifier.update(data) |
66
|
|
|
verifier.verify() |
67
|
|
|
|
68
|
|
|
|
69
|
|
|
def encrypt_basic256(public_key, data): |
70
|
|
|
ciphertext = public_key.encrypt( |
71
|
|
|
data, |
72
|
|
|
padding.OAEP( |
73
|
|
|
mgf=padding.MGF1(algorithm=hashes.SHA256()), |
74
|
|
|
algorithm=hashes.SHA256(), |
75
|
|
|
label=None) |
76
|
|
|
) |
77
|
|
|
return ciphertext |
78
|
|
|
|
79
|
|
|
|
80
|
|
|
def encrypt_rsa_oaep(public_key, data): |
81
|
|
|
ciphertext = public_key.encrypt( |
82
|
|
|
data, |
83
|
|
|
padding.OAEP( |
84
|
|
|
mgf=padding.MGF1(algorithm=hashes.SHA1()), |
85
|
|
|
algorithm=hashes.SHA1(), |
86
|
|
|
label=None) |
87
|
|
|
) |
88
|
|
|
return ciphertext |
89
|
|
|
|
90
|
|
|
|
91
|
|
|
def encrypt_rsa15(public_key, data): |
92
|
|
|
ciphertext = public_key.encrypt( |
93
|
|
|
data, |
94
|
|
|
padding.PKCS1v15() |
95
|
|
|
) |
96
|
|
|
return ciphertext |
97
|
|
|
|
98
|
|
|
|
99
|
|
|
def decrypt_rsa_oaep(private_key, data): |
100
|
|
|
text = private_key.decrypt( |
101
|
|
|
data, |
102
|
|
|
padding.OAEP( |
103
|
|
|
mgf=padding.MGF1(algorithm=hashes.SHA1()), |
104
|
|
|
algorithm=hashes.SHA1(), |
105
|
|
|
label=None) |
106
|
|
|
) |
107
|
|
|
return text |
108
|
|
|
|
109
|
|
|
|
110
|
|
|
def decrypt_rsa15(private_key, data): |
111
|
|
|
text = private_key.decrypt( |
112
|
|
|
data, |
113
|
|
|
padding.PKCS1v15() |
114
|
|
|
) |
115
|
|
|
return text |
116
|
|
|
|
117
|
|
|
|
118
|
|
|
def cipher_aes_cbc(key, init_vec): |
119
|
|
|
return Cipher(algorithms.AES(key), modes.CBC(init_vec), default_backend()) |
120
|
|
|
|
121
|
|
|
|
122
|
|
|
def cipher_encrypt(cipher, data): |
123
|
|
|
encryptor = cipher.encryptor() |
124
|
|
|
return encryptor.update(data) + encryptor.finalize() |
125
|
|
|
|
126
|
|
|
|
127
|
|
|
def cipher_decrypt(cipher, data): |
128
|
|
|
decryptor = cipher.decryptor() |
129
|
|
|
return decryptor.update(data) + decryptor.finalize() |
130
|
|
|
|
131
|
|
|
|
132
|
|
|
def hmac_sha1(key, message): |
133
|
|
|
hasher = hmac.HMAC(key, hashes.SHA1(), backend=default_backend()) |
134
|
|
|
hasher.update(message) |
135
|
|
|
return hasher.finalize() |
136
|
|
|
|
137
|
|
|
|
138
|
|
|
def sha1_size(): |
139
|
|
|
return hashes.SHA1.digest_size |
140
|
|
|
|
141
|
|
|
|
142
|
|
|
def p_sha1(secret, seed, sizes=()): |
143
|
|
|
""" |
144
|
|
|
Derive one or more keys from secret and seed. |
145
|
|
|
(See specs part 6, 6.7.5 and RFC 2246 - TLS v1.0) |
146
|
|
|
Lengths of keys will match sizes argument |
147
|
|
|
""" |
148
|
|
|
full_size = 0 |
149
|
|
|
for size in sizes: |
150
|
|
|
full_size += size |
151
|
|
|
|
152
|
|
|
result = b'' |
153
|
|
|
accum = seed |
154
|
|
|
while len(result) < full_size: |
155
|
|
|
accum = hmac_sha1(secret, accum) |
156
|
|
|
result += hmac_sha1(secret, accum + seed) |
157
|
|
|
|
158
|
|
|
parts = [] |
159
|
|
|
for size in sizes: |
160
|
|
|
parts.append(result[:size]) |
161
|
|
|
result = result[size:] |
162
|
|
|
return tuple(parts) |
163
|
|
|
|
164
|
|
|
|
165
|
|
|
if __name__ == "__main__": |
166
|
|
|
# Convert from PEM to DER |
167
|
|
|
cert = load_certificate("../examples/server_cert.pem") |
168
|
|
|
#rsa_pubkey = pubkey_from_dercert(der) |
169
|
|
|
rsa_privkey = load_private_key("../examples/mykey.pem") |
170
|
|
|
|
171
|
|
|
from IPython import embed |
172
|
|
|
embed() |
173
|
|
|
|