pycose icon indicating copy to clipboard operation
pycose copied to clipboard

Improve error message when private key is missing in key object when signing

Open nthState opened this issue 3 years ago • 1 comments

Hello, I have a EC2 Key:

<COSE_Key(EC2Key): {'EC2KpY': "b'\\xcc\\x01\\x99R\\xcc' ... (32 B)", 'EC2KpX': "b'_F\\xbf\\xa8\\xde' ... (32 B)", 'EC2KpCurve': 'P256', 'KpKty': 'KtyEC2', 'KpAlg': 'Es256'}>

and when I try to encode:

msg = Sign1Message(
   		 	phdr = {Algorithm: 'Es256', KID: b'EC2'},
    		payload = nonce_bytes
    		)
key_as_dict =  CoseKey.from_dict(key)
msg.key = key_as_dict
encoded = msg.encode()

I get:

Traceback (most recent call last):
  File "/Users/Chris/Developer/PassKeysTest/API/cert.py", line 153, in <module>
    key = extractKey(
  File "/Users/Chris/Developer/PassKeysTest/API/cert.py", line 136, in extractKey
    encoded = msg.encode()
  File "/opt/homebrew/lib/python3.9/site-packages/cose/messages/sign1message.py", line 67, in encode
    message = [self.phdr_encoded, self.uhdr_encoded, self.payload, self.compute_signature()]
  File "/opt/homebrew/lib/python3.9/site-packages/cose/messages/signcommon.py", line 65, in compute_signature
    return alg.sign(key=self.key, data=self._sig_structure)
  File "/opt/homebrew/lib/python3.9/site-packages/cose/algorithms.py", line 185, in sign
    sk = SigningKey.from_secret_exponent(int(hexlify(key.d), 16), curve=cls.get_curve())
ValueError: invalid literal for int() with base 16: b''

The method it's calling is:

 @classmethod
    def sign(cls, key: 'EC2', data: bytes) -> bytes:
        sk = SigningKey.from_secret_exponent(int(hexlify(key.d), 16), curve=cls.get_curve())

        return sk.sign_deterministic(data, hashfunc=cls.get_hash_func())

....but key.d doesn't exist in this key.....

nthState avatar Jul 15 '22 15:07 nthState

d is the private key part and without it you can't sign the message. The error message could be better.

letmaik avatar Nov 05 '22 22:11 letmaik