-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy path__init__.py
More file actions
368 lines (306 loc) · 10.5 KB
/
__init__.py
File metadata and controls
368 lines (306 loc) · 10.5 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
import base64
import hashlib
import json
import ssl
from ..exception import UnsupportedAlgorithm
from ..utils import as_bytes, as_unicode, b64e, base64url_to_long
from .utils import DIGEST_HASH
USE = {"sign": "sig", "decrypt": "enc", "encrypt": "enc", "verify": "sig"}
class JWK:
"""
Basic JSON Web key class. Jason Web keys are described in
RFC 7517 (https://tools.ietf.org/html/rfc7517).
The name of parameters used in this class are the same as
specified in RFC 7518 (https://tools.ietf.org/html/rfc7518).
"""
members = ["kty", "alg", "use", "kid", "x5c", "x5t", "x5u", "key_ops"]
longs: list[str] = []
public_members = ["kty", "alg", "use", "kid", "x5c", "x5t", "x5u", "key_ops"]
required = ["kty"]
def __init__(
self,
kty="",
alg="",
use="",
kid="",
x5c=None,
x5t="",
x5u="",
key_ops=None,
**kwargs,
):
self.extra_args = kwargs
# want kty, alg, use and kid to be strings
if isinstance(kty, str):
self.kty = kty
else:
self.kty = as_unicode(kty)
if alg:
if not isinstance(alg, str):
alg = as_unicode(alg)
if use == "enc":
if alg not in [
"RSA1_5",
"RSA-OAEP",
"RSA-OAEP-256",
"A128KW",
"A192KW",
"A256KW",
"ECDH-ES",
"ECDH-ES+A128KW",
"ECDH-ES+A192KW",
"ECDH-ES+A256KW",
]:
raise UnsupportedAlgorithm(f"Unknown algorithm: {alg}")
elif use == "sig":
# The list comes from https://tools.ietf.org/html/rfc7518#page-6
# Should map against SIGNER_ALGS in cryptojwt.jws.jws
if alg not in [
"HS256",
"HS384",
"HS512",
"RS256",
"RS384",
"RS512",
"ES256",
"ES256K",
"ES384",
"ES512",
"PS256",
"PS384",
"PS512",
"EdDSA",
"Ed25519",
"Ed448",
"none",
]:
raise UnsupportedAlgorithm(f"Unknown algorithm: {alg}")
else: # potentially used both for encryption and signing
if alg not in [
"HS256",
"HS384",
"HS512",
"RS256",
"RS384",
"RS512",
"ES256",
"ES256K",
"ES384",
"ES512",
"PS256",
"PS384",
"PS512",
"EdDSA",
"Ed25519",
"Ed448",
"none",
"RSA1_5",
"RSA-OAEP",
"RSA-OAEP-256",
"A128KW",
"A192KW",
"A256KW",
"ECDH-ES",
"ECDH-ES+A128KW",
"ECDH-ES+A192KW",
"ECDH-ES+A256KW",
]:
raise UnsupportedAlgorithm(f"Unknown algorithm: {alg}")
self.alg = alg
if isinstance(use, str):
self.use = use
else:
self.use = as_unicode(use)
if isinstance(kid, str):
self.kid = kid
else:
self.kid = as_unicode(kid)
if key_ops:
self.key_ops: list[str] = []
for ops in key_ops:
if isinstance(ops, str):
self.key_ops.append(ops)
else:
self.key_ops.append(as_unicode(key_ops))
else:
self.key_ops = []
if self.use and self.key_ops:
raise ValueError("Not allowed to specify use and key_ops")
self.x5c = x5c or []
self.x5t = x5t
self.x5u = x5u
self.inactive_since = kwargs.get("inactive_since", 0)
def to_dict(self):
"""
A wrapper for to_dict the makes sure that all the private information
as well as extra arguments are included. This method should *not* be
used for exporting information about the key.
:return: A dictionary representation of the JSON Web key
"""
res = self.serialize(private=True)
res.update(self.extra_args)
return res
def common(self):
"""
Return the set of parameters that are common to all types of keys.
:return: Dictionary
"""
res = {}
for param in ["kty", "use", "kid", "alg", "key_ops"]:
_val = getattr(self, param)
if _val:
res[param] = _val
return res
def __str__(self):
return str(self.to_dict())
def deserialize(self):
"""
Starting with information gathered from the on-the-wire representation
initiate an appropriate key.
"""
pass
def serialize(self, private=False):
"""
map key characteristics into attribute values that can be used
to create an on-the-wire representation of the key
"""
pass
def get_key(self, private=False, **kwargs):
"""
Get a key useful for signing and/or encrypting information.
:param private: Private key requested. If false return a public key.
:return: A key instance. This can be an RSA, EC or other
type of key.
"""
pass
def verify(self):
"""
Verify that the information gathered from the on-the-wire
representation is of the right type.
This is supposed to be run before the info is deserialized.
:return: True/False
"""
for param in self.longs:
item = getattr(self, param)
if not item or isinstance(item, str):
continue
if isinstance(item, bytes):
item = item.decode("utf-8")
setattr(self, param, item)
try:
_ = base64url_to_long(item)
except Exception:
return False
else:
if [e for e in ["+", "/", "="] if e in item]:
return False
if self.kid:
if not isinstance(self.kid, str):
raise ValueError("kid of wrong value type")
return True
def __eq__(self, other):
"""
Compare 2 Key instances to find out if they represent the same key
:param other: The other Key instance
:return: True if they are the same otherwise False.
"""
if self.__class__ != other.__class__:
return False
if set(self.__dict__.keys()) != set(other.__dict__.keys()):
return False
return all(getattr(other, key) == getattr(self, key) for key in self.public_members)
def keys(self):
return list(self.to_dict().keys())
def thumbprint(self, hash_function, members=None):
"""
Create a thumbprint of the key following the outline in
https://tools.ietf.org/html/draft-jones-jose-jwk-thumbprint-01
:param hash_function: A hash function to use for hashing the
information
:param members: Which attributes of the Key instance that should
be included when computing the hash value. If members is undefined
then all the required attributes are used.
:return: A base64 encode hash over a set of Key attributes
"""
if members is None:
members = self.required
members.sort()
ser = self.serialize()
_se = []
for elem in members:
try:
_val = ser[elem]
except KeyError: # should never happen with the required set
pass
else:
if isinstance(_val, bytes):
_val = as_unicode(_val)
_se.append(f'"{elem}":{json.dumps(_val)}')
_json = "{{{}}}".format(",".join(_se))
return b64e(DIGEST_HASH[hash_function](_json))
def add_kid(self):
"""
Construct a Key ID using the thumbprint method and add it to
the key attributes.
"""
self.kid = self.thumbprint("SHA-256").decode("utf8")
def appropriate_for(self, usage, **kwargs):
"""
Make sure that key can be used for the specified usage.
:return: True/False
"""
if self.use:
return self.use == USE[usage]
elif self.key_ops:
return usage in self.key_ops
def update(self):
pass
def key_len(self):
raise NotImplementedError
def pems_to_x5c(cert_chain):
"""
Takes a list of PEM formated certificates and transforms them into a x5c attribute
values as described in RFC 7517.
:param cert_pem: List of PKIX certificates
:return: List of strings
"""
return [
as_unicode(v)
for v in [base64.b64encode(d) for d in [ssl.PEM_cert_to_DER_cert(c) for c in cert_chain]]
]
def x5c_to_pems(x5c):
"""
Takes a x5c value from a JWK and converts it into a list of PEM formated certificates.
:param x5c: List of strings
:return:
"""
return [
ssl.DER_cert_to_PEM_cert(d)
for d in [base64.b64decode(x) for x in [as_bytes(y) for y in x5c]]
]
def x5c_to_ders(x5c):
"""
Takes a x5c value from a JWK and converts it into a list of PEM formated certificates.
:param x5c: List of strings
:return:
"""
return [base64.b64decode(x) for x in [as_bytes(y) for y in x5c]]
def certificate_fingerprint(der, hash="sha256"):
"""
:param der: DER encoded certificate
:return:
"""
if hash == "sha256":
fp = hashlib.sha256(as_bytes(der)).hexdigest()
elif hash == "sha1":
fp = hashlib.sha1(as_bytes(der)).hexdigest()
elif hash == "md5":
fp = hashlib.md5(as_bytes(der)).hexdigest()
else:
raise UnsupportedAlgorithm(hash)
return ":".join([fp[i : i + 2] for i in range(0, len(fp), 2)]).upper()
def pem_hash(pem_file):
with open(pem_file) as fp:
pem = fp.read()
der = ssl.PEM_cert_to_DER_cert(pem)
return hashlib.sha1(as_bytes(der)).hexdigest()