Skip to content

Commit e9abbf9

Browse files
committed
The *_init functions should not store anything on disc.
Create one key per usage. Use the new_*_key functions.
1 parent 645a404 commit e9abbf9

File tree

2 files changed

+26
-77
lines changed

2 files changed

+26
-77
lines changed

src/cryptojwt/jwk/rsa.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
from ..utils import deser
1313
from ..utils import long_to_base64
1414

15-
from ..exception import DeSerializationNotPossible, UnsupportedKeyType
15+
from ..exception import DeSerializationNotPossible
16+
from ..exception import UnsupportedKeyType
1617
from ..exception import JWKESTException
1718
from ..exception import SerializationNotPossible
1819

src/cryptojwt/key_bundle.py

Lines changed: 24 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,15 @@
22
import logging
33
import os
44
import requests
5-
import sys
65
import time
76

8-
from cryptography.hazmat.backends import default_backend
9-
from cryptography.hazmat.primitives import serialization
10-
from cryptography.hazmat.primitives.asymmetric import ec
11-
from cryptography.hazmat.primitives.asymmetric.rsa import generate_private_key
12-
137
from .exception import JWKException
148
from .exception import UnknownKeyType
159
from .exception import UpdateFailed
1610
from .jwk.hmac import SYMKey
1711
from .jwk.ec import ECKey
18-
from .jwk.ec import NIST2SEC
12+
from .jwk.ec import new_ec_key
13+
from .jwk.rsa import new_rsa_key
1914
from .jwk.rsa import RSAKey
2015
from .jwk.rsa import import_private_rsa_key_from_file
2116
from .utils import as_unicode
@@ -56,104 +51,57 @@ def harmonize_usage(use):
5651
return list(set([MAP[u] for u in use if u in ul]))
5752

5853

59-
def create_and_store_rsa_key_pair(name="oidcmsg", path=".", size=2048, use=''):
60-
"""
61-
Mints a new RSA key pair and stores it in a file.
62-
63-
:param name: Name of the key file. 2 files will be created one with
64-
the private key the name without extension and the other containing
65-
the public key with '.pub' as extension.
66-
:param path: Path to where the key files are stored
67-
:param size: RSA key size
68-
:return: RSA key
69-
"""
70-
71-
key = generate_private_key(public_exponent=65537, key_size=size,
72-
backend=default_backend())
73-
74-
os.makedirs(path, exist_ok=True)
75-
76-
if name:
77-
if use:
78-
name = '{}_{}'.format(name, use)
79-
80-
pem = key.private_bytes(
81-
encoding = serialization.Encoding.PEM,
82-
format = serialization.PrivateFormat.PKCS8,
83-
encryption_algorithm = serialization.NoEncryption())
84-
85-
with open(os.path.join(path, name), 'wb') as f:
86-
f.write(pem)
87-
88-
public_key = key.public_key()
89-
pub_pem = public_key.public_bytes(
90-
encoding = serialization.Encoding.PEM,
91-
format = serialization.PublicFormat.SubjectPublicKeyInfo)
92-
93-
with open(os.path.join(path, '{}.pub'.format(name)), 'wb') as f:
94-
f.write(pub_pem)
95-
96-
return key
97-
98-
9954
def rsa_init(spec):
10055
"""
10156
Initiates a :py:class:`oidcmsg.keybundle.KeyBundle` instance
10257
containing newly minted RSA keys according to a spec.
10358
10459
Example of specification::
105-
{'name': 'myrsakey', 'path': 'keystore', 'size':2048,
106-
'use': ['enc', 'sig'] }
60+
{'size':2048, 'use': ['enc', 'sig'] }
10761
10862
Using the spec above 2 RSA keys would be minted, one for
10963
encryption and one for signing.
11064
11165
:param spec:
11266
:return: KeyBundle
11367
"""
114-
if 'name' not in spec:
115-
try:
116-
_key_name = spec['key']
117-
except KeyError:
118-
pass
119-
else:
120-
if '/' in _key_name:
121-
(head, tail) = os.path.split(spec['key'])
122-
spec['path'] = head
123-
spec['name'] = tail
124-
else:
125-
spec['name'] = _key_name
12668

127-
arg = {}
128-
for param in ["name", "path", "size"]:
129-
try:
130-
arg[param] = spec[param]
131-
except KeyError:
132-
pass
69+
try:
70+
size = spec['size']
71+
except KeyError:
72+
size = 2048
13373

13474
kb = KeyBundle(keytype="RSA")
135-
for use in harmonize_usage(spec["use"]):
136-
_key = create_and_store_rsa_key_pair(use=use, **arg)
137-
kb.append(RSAKey(use=use, priv_key=_key))
75+
if 'use' in spec:
76+
for use in harmonize_usage(spec["use"]):
77+
_key = new_rsa_key(use=use, key_size=size)
78+
kb.append(_key)
79+
else:
80+
_key = new_rsa_key(key_size=size)
81+
kb.append(_key)
82+
13883
return kb
13984

14085

14186
def ec_init(spec):
14287
"""
143-
Initiate a keybundle with an elliptic curve key.
88+
Initiate a key bundle with an elliptic curve key.
14489
14590
:param spec: Key specifics of the form::
14691
{"type": "EC", "crv": "P-256", "use": ["sig"]}
14792
14893
:return: A KeyBundle instance
14994
"""
15095

151-
_key = ec.generate_private_key(NIST2SEC[spec['crv']], default_backend())
152-
153-
kb = KeyBundle(keytype="EC", keyusage=spec["use"])
154-
for use in spec["use"]:
155-
eck = ECKey(use=use).load_key(_key)
96+
kb = KeyBundle(keytype="EC")
97+
if 'use' in spec:
98+
for use in spec["use"]:
99+
eck = new_ec_key(crv=spec['crv'], use=use)
100+
kb.append(eck)
101+
else:
102+
eck = new_ec_key(crv=spec['crv'])
156103
kb.append(eck)
104+
157105
return kb
158106

159107

0 commit comments

Comments
 (0)