EGA-archive / crypt4gh

GA4GH cryptographic tools
http://crypt4gh.readthedocs.io
Apache License 2.0
16 stars 12 forks source link

generate private key before public key #30

Closed teemukataja closed 2 years ago

teemukataja commented 2 years ago

Problem

Key pair can become mismatched if keys are generated twice, because the private key is write protected, but the public key is not. What happens with calling c4gh.generate() twice, is the first key pair is generated, and then during the generation of the second key pair, the original private key stays the same, but the public key is overwritten with a new key that no longer matches the private key that didn't change.

The problem was encountered, when a user had forgotten their private key password, and they generated a new key pair, but they didn't remove the old keys before generating new ones.

Reproduction

Generate key pair twice with c4gh.generate()

Solution

̃~Make public key to be write protected similarly to the private key, so that the key pair doesn't get accidentally mismatched, if trying to generate new keys, while there are existing keys in the same directory.~

Generate private key before public key, similarly to OpenSSH conventions, as per https://github.com/EGA-archive/crypt4gh/pull/30#issuecomment-1171089414

silverdaz commented 2 years ago

Enforcing some extra access permissions should be left for the user to choose (via the umask).

You have a point in the problem description, and I have another idea, different from your solution. How about simply reversing the order: first we try to write the private key, and then the public key? Looky here: that's what's done in OpenSSH