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

RSA keys don't roundtrip? #171

Closed
anmonteiro opened this issue Aug 24, 2024 · 4 comments · Fixed by #172
Closed

RSA keys don't roundtrip? #171

anmonteiro opened this issue Aug 24, 2024 · 4 comments · Fixed by #172

Comments

@anmonteiro
Copy link

forgive me if this is a dumb mistake on my part, but take this example:

# generate RSA priv key
$ openssl genpkey -algorithm RSA -out privatekey.pem -pkeyopt rsa_keygen_bits:2048

# extract the public key
$ openssl pkey -in privatekey.pem -pubout -out publickey.pem

and in a toplevel (e.g. utop):

#require "digestif.c";;
#require "x509";;

let x_pub = In_channel.input_all (In_channel.open_bin "./publickey.pem");;

let re_encoded =X509.Public_key.decode_pem x_pub |> Result.get_ok |> X509.Public_key.encode_pem;;

x_pub = re_encoded (* false *)

I'd expect these to roundtrip. In the previous version of X509 (before the Cstruct -> string migration), they did roundtrip:

let re_encoded = X509.Public_key.decode_pem (Cstruct.of_string x_pub) |> Result.get_ok |> X509.Public_key.encode_pem |> Cstruct.to_string;;

x = x_pub;;
- : bool = true
@hannesm
Copy link
Member

hannesm commented Aug 25, 2024

Thanks for your report, I opened #172 with a regression test and a fix. If you could verify that this fixes your issue, that would be great.

I'll wait for CI, then merge and release.

hannesm added a commit to hannesm/opam-repository that referenced this issue Aug 25, 2024
CHANGES:

* Fix RSA public and private key encoding and decoding (@hannesm mirleft/ocaml-x509#172, reported
  by @anmonteiro mirleft/ocaml-x509#171)
@anmonteiro
Copy link
Author

Thanks, that fixed RSA, but I think I might be seeing the same for P521?

@hannesm
Copy link
Member

hannesm commented Aug 26, 2024

@anmonteiro would you mind to open a PR with a regression test? thanks a lot.

@hannesm
Copy link
Member

hannesm commented Aug 26, 2024

I'm asking since I tried to reproduce "the same for P521", but without luck. Even before I released 1.0.2.

Note that this package does not include the public key into the PKCS8 encoded private key dump, as openssl normally does (for reasons unknown to me). This means that a openssl generated private key file, read, decoded, encoded, and written by this package won't be identical - but the private key part is identical.

I pushed #173 which includes all the P curve public and private key tests -- together with a shell script what openssl commands have been executed to generate the keypairs. WDYT?

avsm pushed a commit to avsm/opam-repository that referenced this issue Sep 5, 2024
CHANGES:

* Fix RSA public and private key encoding and decoding (@hannesm mirleft/ocaml-x509#172, reported
  by @anmonteiro mirleft/ocaml-x509#171)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

3 participants
@hannesm @anmonteiro and others