Skip to content

Commit 840c05a

Browse files
authored
Patched P-256K curve name binding secp256k1 from joserfc (#16)
1 parent 17715c0 commit 840c05a

File tree

1 file changed

+30
-2
lines changed

1 file changed

+30
-2
lines changed

didsdk/protocol/protocol_message.py

+30-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@
44
from dataclasses import dataclass
55
from typing import Optional, Union
66

7+
from cryptography.hazmat.primitives.asymmetric.ec import EllipticCurvePrivateKey
78
from joserfc import jwe
89
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
912
from loguru import logger
1013

1114
from didsdk.core.did_key_holder import DidKeyHolder
@@ -32,6 +35,33 @@ class SignResult:
3235
fail_message: str = None
3336

3437

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+
3565
class ProtocolMessage:
3666
def __init__(
3767
self,
@@ -429,12 +459,10 @@ def sign_encrypt(self, did_key_holder: Optional[DidKeyHolder], ecdh_key: Optiona
429459
if self._param_string:
430460
decoded_message[PropertyName.KEY_PROTOCOL_PARAM] = self._param_string
431461

432-
epk = JWKRegistry.import_key(ecdh_key.as_dict_without_kid())
433462
jwe_header = {
434463
"kid": self._request_public_key.kid,
435464
"alg": HeaderAlgorithmType.JWE_ALGO_ECDH_ES,
436465
"enc": HeaderAlgorithmType.JWE_ALGO_A128GCM,
437-
"epk": epk.as_dict(),
438466
}
439467

440468
recipient = JWKRegistry.import_key(self._request_public_key.epk.as_dict_without_kid())

0 commit comments

Comments
 (0)