1
1
"""JSON Web Key."""
2
2
import abc
3
+ import collections
3
4
import json
4
5
import logging
5
6
import math
@@ -257,7 +258,7 @@ def fields_to_partial_json(self):
257
258
258
259
@JWK .register
259
260
class JWKEC (JWK ):
260
- """EC JWK.
261
+ """RSA JWK.
261
262
262
263
:ivar key: :class:`~cryptography.hazmat.primitives.asymmetric.ec.EllipticCurvePrivateKey`
263
264
or :class:`~cryptography.hazmat.primitives.asymmetric.ec.EllipticCurvePublicKey`
@@ -389,24 +390,25 @@ class JWKOKP(JWK):
389
390
or :class:`~cryptography.hazmat.primitives.asymmetric.x448.X448PublicKey`
390
391
or :class:`~cryptography.hazmat.primitives.asymmetric.x25519.X25519PrivateKey`
391
392
or :class:`~cryptography.hazmat.primitives.asymmetric.x25519.X25519PublicKey`
393
+ wrapped in :class:`~josepy.util.ComparableOKPKey`
392
394
393
395
This class requires ``cryptography>=2.6`` to be installed.
394
396
"""
395
397
typ = 'OKP'
396
- __slots__ = ('key' , )
397
-
398
+ __slots__ = ('key' ,)
398
399
cryptography_key_types = (
399
400
ed25519 .Ed25519PrivateKey , ed25519 .Ed25519PrivateKey ,
400
401
ed448 .Ed448PublicKey , ed448 .Ed448PrivateKey ,
401
402
x25519 .X25519PrivateKey , x25519 .X25519PublicKey ,
402
403
x448 .X448PrivateKey , x448 .X448PublicKey ,
403
404
)
404
405
required = ('crv' , JWK .type_field_name , 'x' )
406
+ okp_curve = collections .namedtuple ('okp_curve' , 'pubkey privkey' )
405
407
crv_to_pub_priv = {
406
- "Ed25519" : ( ed25519 .Ed25519PublicKey , ed25519 .Ed25519PrivateKey ),
407
- "Ed448" : ( ed448 .Ed448PublicKey , ed448 .Ed448PrivateKey ),
408
- "X25519" : ( x25519 .X25519PublicKey , x25519 .X25519PrivateKey ),
409
- "X448" : ( x448 .X448PublicKey , x448 .X448PrivateKey ),
408
+ "Ed25519" : okp_curve ( pubkey = ed25519 .Ed25519PublicKey , privkey = ed25519 .Ed25519PrivateKey ),
409
+ "Ed448" : okp_curve ( pubkey = ed448 .Ed448PublicKey , privkey = ed448 .Ed448PrivateKey ),
410
+ "X25519" : okp_curve ( pubkey = x25519 .X25519PublicKey , privkey = x25519 .X25519PrivateKey ),
411
+ "X448" : okp_curve ( pubkey = x448 .X448PublicKey , privkey = x448 .X448PrivateKey ),
410
412
}
411
413
412
414
def __init__ (self , * args , ** kwargs ):
@@ -428,20 +430,20 @@ def _key_to_crv(self):
428
430
return "X448"
429
431
return NotImplemented
430
432
431
- def fields_to_partial_json (self ) -> Dict :
433
+ def fields_to_partial_json (self ):
432
434
params = {}
433
435
if self .key .is_private ():
434
- params ['d' ] = json_util .encode_b64jose (self .key .private_bytes (
436
+ params ['d' ] = json_util .encode_b64jose (self .key ._wrapped . private_bytes (
435
437
encoding = serialization .Encoding .Raw ,
436
438
format = serialization .PrivateFormat .Raw ,
437
439
encryption_algorithm = serialization .NoEncryption ()
438
440
))
439
- params ['x' ] = self .key .public_key ().public_bytes (
441
+ params ['x' ] = self .key ._wrapped . public_key ().public_bytes (
440
442
encoding = serialization .Encoding .Raw ,
441
443
format = serialization .PublicFormat .Raw ,
442
444
)
443
445
else :
444
- params ['x' ] = json_util .encode_b64jose (self .key .public_bytes (
446
+ params ['x' ] = json_util .encode_b64jose (self .key ._wrapped . public_bytes (
445
447
encoding = serialization .Encoding .Raw ,
446
448
format = serialization .PublicFormat .Raw ,
447
449
))
@@ -460,16 +462,13 @@ def fields_from_json(cls, jobj):
460
462
except ValueError :
461
463
raise errors .DeserializationError ("Key is not valid JSON" )
462
464
463
- if obj .get ("kty" ) != "OKP" :
464
- raise errors .DeserializationError ("Not an Octet Key Pair" )
465
-
466
- curve = obj .get ("crv" )
465
+ curve = obj ["crv" ]
467
466
if curve not in cls .crv_to_pub_priv :
468
467
raise errors .DeserializationError (f"Invalid curve: { curve } " )
469
468
470
469
if "x" not in obj :
471
470
raise errors .DeserializationError ('OKP should have "x" parameter' )
472
- x = json_util .decode_b64jose (jobj . get ( "x" ) )
471
+ x = json_util .decode_b64jose (jobj [ "x" ] )
473
472
474
473
try :
475
474
if "d" not in obj : # public key
@@ -478,16 +477,16 @@ def fields_from_json(cls, jobj):
478
477
ed448 .Ed448PublicKey ,
479
478
x25519 .X25519PublicKey ,
480
479
x448 .X448PublicKey ,
481
- ]] = cls .crv_to_pub_priv [curve ][ 0 ]
480
+ ]] = cls .crv_to_pub_priv [curve ]. pubkey
482
481
return cls (key = pub_class .from_public_bytes (x ))
483
482
else : # private key
484
- d = json_util .decode_b64jose (obj . get ( "d" ) )
483
+ d = json_util .decode_b64jose (obj [ "d" ] )
485
484
priv_key_class : Type [Union [
486
485
ed25519 .Ed25519PrivateKey ,
487
486
ed448 .Ed448PrivateKey ,
488
487
x25519 .X25519PrivateKey ,
489
488
x448 .X448PrivateKey ,
490
- ]] = cls .crv_to_pub_priv [curve ][ 1 ]
489
+ ]] = cls .crv_to_pub_priv [curve ]. privkey
491
490
return cls (key = priv_key_class .from_private_bytes (d ))
492
491
except ValueError as err :
493
492
raise errors .DeserializationError ("Invalid key parameter" ) from err
0 commit comments