Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
nthState opened this issue Jul 15, 2022 · 1 comment
Labels
enhancement New feature or request

Comments

@nthState
Copy link

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.....

@letmaik
Copy link
Collaborator

letmaik commented Nov 5, 2022

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

@letmaik letmaik changed the title Encode with a EC2Key has no .d component Improve error message when private key is missing in key object when signing Nov 5, 2022
@letmaik letmaik added the enhancement New feature or request label Nov 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant