-
Notifications
You must be signed in to change notification settings - Fork 28
Rsa keygen #184
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
Merged
Merged
Rsa keygen #184
Changes from all commits
Commits
Show all changes
84 commits
Select commit
Hold shift + click to select a range
981eb80
start on key splitter
Zacholme7 423df87
wip
Zacholme7 ed9a7da
merge
Zacholme7 f50c223
read in keystore file
Zacholme7 d7e788e
wip
Zacholme7 89c7dca
Merge branch 'unstable' into keysplitter
Zacholme7 a02ceb0
Merge branch 'unstable' into keysplitter
Zacholme7 e2d419a
crypto
Zacholme7 43fbf31
wip
Zacholme7 3d013a2
cleanup
Zacholme7 7ed622e
impl error
Zacholme7 dd88456
bubblegum impl
Zacholme7 4e4af57
wip
Zacholme7 d7a52c2
dirty working version
Zacholme7 e1c8ab0
cleanup
Zacholme7 045b7af
cleanup
Zacholme7 eca2089
Merge branch 'unstable' into keysplitter
Zacholme7 9fcf59a
fmt and sort
Zacholme7 47c8fc4
remove unused dep
Zacholme7 bac0144
merge
Zacholme7 33e90ce
revert sync for now
Zacholme7 62211a3
nits
Zacholme7 709cc2d
fmt
Zacholme7 3333e2d
spelling
Zacholme7 c5b60ee
Merge branch 'unstable' into keysplitter
Zacholme7 63cd485
rought draft for onchain sync
Zacholme7 5edb56e
rough draft onchain
Zacholme7 bf43d71
working onchain sync!
Zacholme7 982f860
nits
Zacholme7 8c06f40
ci
Zacholme7 7ba6930
keygen to keysplit
Zacholme7 aef494d
better errors
Zacholme7 120d698
merge
Zacholme7 6d66b5a
verify and small nits
Zacholme7 27fe175
fix error
Zacholme7 ff071e8
fix db path
Zacholme7 e655fcd
testing
Zacholme7 58efbc0
initial
Zacholme7 9ca6e00
keygen
Zacholme7 aaec9b3
sort
Zacholme7 f11a7d5
writing to file
Zacholme7 a19f39f
json output
Zacholme7 ec900c6
support pbkdf2
Zacholme7 9f46e35
unused dep
Zacholme7 ba00d9c
Merge branch 'unstable' into onchain-split
Zacholme7 2fd87d2
Merge branch 'unstable' into rsa-keygen
Zacholme7 8c7d503
fix pr commentsg
Zacholme7 25615e7
initial
Zacholme7 8515461
keygen
Zacholme7 b9ee7eb
sort
Zacholme7 56b34be
writing to file
Zacholme7 417679a
json output
Zacholme7 8093c29
Merge branch 'rsa-keygen' of github.com:Zacholme7/anchor into rsa-keygen
Zacholme7 cb8c571
fix merge
Zacholme7 bd570bb
move from anchor to node
Zacholme7 ea83455
hex encoding keys before encryption
Zacholme7 a2cd71b
keygen with new headers
Zacholme7 b23c8e5
fmt
Zacholme7 5975bb2
merge
Zacholme7 f535b4d
fix comment
Zacholme7 0c986db
fix merge
Zacholme7 c9f3cc7
also write pem file
Zacholme7 783520b
Merge branch 'unstable' into onchain-split
Zacholme7 7e744a3
merge
Zacholme7 4d7dcae
fix compile
Zacholme7 d58c798
Merge branch 'onchain-split' into rsa-keygen
Zacholme7 febf950
fix unwrap
Zacholme7 c9da4b3
Merge branch 'unstable' into onchain-split
Zacholme7 7e03ef7
Merge branch 'unstable' into rsa-keygen
Zacholme7 fe56858
convert header
Zacholme7 29772cf
Merge branch 'unstable' into onchain-split
Zacholme7 74132df
Merge branch 'onchain-split' into rsa-keygen
Zacholme7 1a68ec2
Merge branch 'unstable' into onchain-split
Zacholme7 a7f8e08
Merge branch 'onchain-split' into rsa-keygen
Zacholme7 b787234
merge
Zacholme7 30e09d3
format
Zacholme7 ca097f5
Merge branch 'unstable' into rsa-keygen
jking-aus 9dbbc3e
suggestions
ThreeHrSleep 73d22ea
return error if files already exist
ThreeHrSleep d6d90a9
force file overwrite
Zacholme7 b6c0072
zeroize
Zacholme7 f314de4
change error handling
Zacholme7 2e2a3bb
sort
Zacholme7 9567bca
todo comments for encryption
jking-aus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| [package] | ||
| name = "keygen" | ||
| version = "0.1.0" | ||
| edition = { workspace = true } | ||
| authors = ["Sigma Prime <contact@sigmaprime.io>"] | ||
|
|
||
| [dependencies] | ||
| base64 = { workspace = true } | ||
| clap = { workspace = true } | ||
| openssl = { workspace = true } | ||
| serde = { workspace = true } | ||
| serde_json = { workspace = true } | ||
| thiserror = { workspace = true } | ||
| tracing = { workspace = true } | ||
| zeroize = { workspace = true } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| use base64::prelude::*; | ||
| use clap::Parser; | ||
| use openssl::{error::ErrorStack, pkey::Private, rsa::Rsa}; | ||
| use serde::Serialize; | ||
| use std::{fs, io, path::PathBuf, string::FromUtf8Error}; | ||
| use thiserror::Error; | ||
| use tracing::info; | ||
| use zeroize::{Zeroize, ZeroizeOnDrop, Zeroizing}; | ||
|
|
||
| #[derive(Error, Debug)] | ||
| pub enum KeygenError { | ||
| #[error("Failed to generate new private key: {0}")] | ||
| Generate(#[source] ErrorStack), | ||
|
|
||
| #[error("Failed to convert key to PEM: {0}")] | ||
| Pem(#[source] ErrorStack), | ||
|
|
||
| #[error("Failed to write output: {0}")] | ||
| Output(#[from] io::Error), | ||
|
|
||
| #[error("Failed to convert to UTF8: {0}")] | ||
| Utf8(#[from] FromUtf8Error), | ||
|
|
||
| #[error("Failed to convert output data to JSON: {0}")] | ||
| Json(#[from] serde_json::Error), | ||
|
|
||
| #[error("{0}")] | ||
| Custom(String), | ||
| } | ||
|
|
||
| #[derive(Parser, Clone, Debug)] | ||
| #[clap(name = "keygen", about = "RSA key generation tool")] | ||
| pub struct Keygen { | ||
| #[clap(long, help = "Path to output keys to", value_name = "OUTPUT_PATH")] | ||
| pub output_path: Option<String>, | ||
|
|
||
| #[clap( | ||
| long, | ||
| help = "Force file overwrite", | ||
| value_name = "FORCE", | ||
| default_value = "false" | ||
| )] | ||
| pub force: bool, | ||
| // TODO: add prompt for password | ||
| } | ||
|
|
||
| #[derive(Debug, Serialize, Zeroize, ZeroizeOnDrop)] | ||
| struct PrettyOutput { | ||
| #[zeroize(skip)] | ||
| public: String, | ||
| private: String, | ||
| } | ||
| // TODO: add encryption and get password functions | ||
|
|
||
| // Run RSA keygeneration | ||
| pub fn run_keygen(keygen: Keygen) -> Result<Rsa<Private>, KeygenError> { | ||
| // Generate the new rsa private key | ||
| let private_key = Rsa::generate(2048).map_err(KeygenError::Generate)?; | ||
|
|
||
| // Extract the PEM of the public and private keys | ||
| let private_pem = Zeroizing::new(private_key.private_key_to_pem().map_err(KeygenError::Pem)?); | ||
diegomrsantos marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| let public_pem = private_key.public_key_to_pem().map_err(KeygenError::Pem)?; | ||
|
|
||
| let public_pem_string = String::from_utf8(public_pem)?; | ||
| // TODO: Fix RSA headers and implement legacy support | ||
| let public_pem = public_pem_string | ||
| .replace( | ||
| "-----BEGIN PUBLIC KEY-----", | ||
| "-----BEGIN RSA PUBLIC KEY-----", | ||
| ) | ||
| .replace("-----END PUBLIC KEY-----", "-----END RSA PUBLIC KEY-----"); | ||
|
|
||
| // Encode them to onchain format | ||
| let private_pem_encoded = Zeroizing::new(BASE64_STANDARD.encode(&private_pem)); | ||
| let public_pem_encoded = BASE64_STANDARD.encode(&public_pem); | ||
|
|
||
| // Determine the output directory | ||
| let output_dir = if let Some(output_path) = keygen.output_path { | ||
| PathBuf::from(output_path) | ||
| } else { | ||
| PathBuf::from(".") // Current working directory | ||
| }; | ||
|
|
||
| // Create output paths for both files | ||
| let pem_file = output_dir.join("key.pem"); | ||
| let json_file = output_dir.join("keys.json"); | ||
|
|
||
| // Create JSON data structure | ||
| let data = PrettyOutput { | ||
| public: public_pem_encoded, | ||
| private: private_pem_encoded.to_string(), | ||
| }; | ||
|
|
||
| // Convert to pretty JSON | ||
| let pretty_json = Zeroizing::new(serde_json::to_string_pretty(&data)?); | ||
| // TODO: Encrypt and password protect the private key | ||
| if keygen.force || (!pem_file.exists() && !json_file.exists()) { | ||
| // Write the PEM file | ||
| fs::write(&pem_file, &private_pem)?; | ||
|
|
||
| info!("Private key written to: {}", pem_file.display()); | ||
|
|
||
| // Write the JSON file | ||
| fs::write(&json_file, pretty_json)?; | ||
|
|
||
| info!("JSON keys written to: {}", json_file.display()); | ||
| } else { | ||
| return Err(KeygenError::Custom(format!( | ||
| "PEM file or JSON file already exist in {}", | ||
| output_dir.display() | ||
| ))); | ||
| } | ||
|
|
||
| Ok(private_key) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.