88
99from cryptography .hazmat .backends import default_backend
1010from cryptography .hazmat .primitives import serialization
11+ from cryptography .hazmat .primitives .asymmetric import ec
1112from cryptography .hazmat .primitives .asymmetric .rsa import generate_private_key
1213
1314from cryptojwt import as_unicode
14- from cryptojwt .jwk import ECKey
15+ from cryptojwt .jwk import ECKey , NIST2SEC
1516from cryptojwt .jwk import JWKException
1617from cryptojwt .jwk import RSAKey
1718from cryptojwt .jwk import SYMKey
@@ -116,6 +117,19 @@ def rsa_init(spec):
116117 :param spec:
117118 :return: KeyBundle
118119 """
120+ if 'name' not in spec :
121+ try :
122+ _key_name = spec ['key' ]
123+ except KeyError :
124+ pass
125+ else :
126+ if '/' in _key_name :
127+ (head , tail ) = os .path .split (spec ['key' ])
128+ spec ['path' ] = head
129+ spec ['name' ] = tail
130+ else :
131+ spec ['name' ] = _key_name
132+
119133 arg = {}
120134 for param in ["name" , "path" , "size" ]:
121135 try :
@@ -130,6 +144,25 @@ def rsa_init(spec):
130144 return kb
131145
132146
147+ def ec_init (spec ):
148+ """
149+ Initiate a keybundle with an elliptic curve key.
150+
151+ :param spec: Key specifics of the form::
152+ {"type": "EC", "crv": "P-256", "use": ["sig"]}
153+
154+ :return: A KeyBundle instance
155+ """
156+
157+ _key = ec .generate_private_key (NIST2SEC [spec ['crv' ]], default_backend ())
158+
159+ kb = KeyBundle (keytype = "EC" , keyusage = spec ["use" ])
160+ for use in spec ["use" ]:
161+ eck = ECKey (use = use ).load_key (_key )
162+ kb .append (eck )
163+ return kb
164+
165+
133166class KeyBundle (object ):
134167 def __init__ (self , keys = None , source = "" , cache_time = 300 , verify_ssl = True ,
135168 fileformat = "jwk" , keytype = "RSA" , keyusage = None ):
0 commit comments