4
4
from dataclasses import dataclass
5
5
from typing import Optional , Union
6
6
7
+ from cryptography .hazmat .primitives .asymmetric .ec import EllipticCurvePrivateKey
7
8
from joserfc import jwe
8
9
from joserfc .jwk import JWKRegistry
10
+ from joserfc .rfc7518 .ec_key import CURVES_DSS , ECBinding , ECDictKey , ECKey
11
+ from joserfc .util import int_to_base64
9
12
from loguru import logger
10
13
11
14
from didsdk .core .did_key_holder import DidKeyHolder
@@ -32,6 +35,33 @@ class SignResult:
32
35
fail_message : str = None
33
36
34
37
38
+ class P256KECBinding (ECBinding ):
39
+ """WARNING: This class is patch for P-256K curve name binding secp256k1
40
+
41
+ If P-256K curve name removed, this class no more needed.
42
+ """
43
+
44
+ @staticmethod
45
+ def export_private_key (key : EllipticCurvePrivateKey ) -> ECDictKey :
46
+ def get_crv_name (curve_name : str ) -> str :
47
+ if curve_name == "secp256k1" :
48
+ return "P-256K"
49
+ else :
50
+ return CURVES_DSS [curve_name ]
51
+
52
+ numbers = key .private_numbers ()
53
+
54
+ return {
55
+ "crv" : get_crv_name (key .curve .name ),
56
+ "x" : int_to_base64 (numbers .public_numbers .x ),
57
+ "y" : int_to_base64 (numbers .public_numbers .y ),
58
+ "d" : int_to_base64 (numbers .private_value ),
59
+ }
60
+
61
+
62
+ ECKey .binding = P256KECBinding
63
+
64
+
35
65
class ProtocolMessage :
36
66
def __init__ (
37
67
self ,
@@ -429,12 +459,10 @@ def sign_encrypt(self, did_key_holder: Optional[DidKeyHolder], ecdh_key: Optiona
429
459
if self ._param_string :
430
460
decoded_message [PropertyName .KEY_PROTOCOL_PARAM ] = self ._param_string
431
461
432
- epk = JWKRegistry .import_key (ecdh_key .as_dict_without_kid ())
433
462
jwe_header = {
434
463
"kid" : self ._request_public_key .kid ,
435
464
"alg" : HeaderAlgorithmType .JWE_ALGO_ECDH_ES ,
436
465
"enc" : HeaderAlgorithmType .JWE_ALGO_A128GCM ,
437
- "epk" : epk .as_dict (),
438
466
}
439
467
440
468
recipient = JWKRegistry .import_key (self ._request_public_key .epk .as_dict_without_kid ())
0 commit comments