Improve RSA key file generation and reads #4083
zacknewman
started this conversation in
Ideas
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Currently
main::check_rsa_keys
,auth::PRIVATE_RSA_KEY
, andauth::PUBLIC_RSA_KEY
rely on a lot of separate I/O calls and a few avoidable assumptions which leads to TOCTOU race conditions and a loss in data integrity (e.g., when the public key file exists, there is no validation done to ensure it matches what it should be based on the private key). The change I propose avoids all of those issues. Additionally, integrating the change is highly localized since there is so little code that relies on the RSA key data.Public keys can be generated from private keys which allows us to avoid the public key file altogether. By always generating the public key from the private key data, we don't have to worry about an invalid public key file. The job is then to read or create/write the private key file using only a single operation avoiding TOCTOU race conditions. The below code relies on
std::sync::OnceLock
, but it would be trivial to adjust it to useonce_cell::sync::Lazy
.Snippet of
auth.rs
(edited to add a check that ensures an existing RSA key is 2048-bits):snippet of
main.rs
:Beta Was this translation helpful? Give feedback.
All reactions