Skip to content

Commit c045862

Browse files
committed
upgrae to latest version of rpgp
1 parent 7778d9f commit c045862

File tree

9 files changed

+1011
-544
lines changed

9 files changed

+1011
-544
lines changed

Cargo.lock

Lines changed: 937 additions & 500 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ clap_complete = "4.5.23"
2626

2727
# stuff i need for the table
2828
box_drawing = "0.1.2"
29-
colored = "2.1.0"
29+
colored = "3.0.0"
3030
console = "0.15.8"
3131
indoc = "2.0.5"
3232

@@ -40,10 +40,11 @@ ctrlc = "3.4.5"
4040
hex = "0.4.3"
4141
home = "0.5.9"
4242
rand = "0.8.5"
43+
# rand_chacha = "0.9.0"
4344
regex = "1"
4445
reqwest = { version = "0.12.7", features = ["json"] }
4546

46-
pgp = "0.13.2"
47+
pgp = "0.15"
4748

4849
serde = { version = "1.0.208", features = ["derive"] }
4950
serde_json = "1.0"
@@ -68,4 +69,4 @@ keyring = { version = "3.2.0", features = [
6869
"windows-native",
6970
"linux-native",
7071
] }
71-
bincode = "1.3.3"
72+
bincode = "1"

src/commands/gen.rs

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
11
// TODO: add uuid to config after uploading
22

33
use super::*;
4+
use crate::constants::MINIMUM_PASSWORD_LENGTH;
45
use crate::sdk::SDK;
56
use crate::utils::config::{self};
67
use crate::utils::key::Key;
78
use crate::utils::keyring::set_password;
8-
// use crate::utils::prompt::prompt_password;
9-
use crate::constants::MINIMUM_PASSWORD_LENGTH;
10-
use crate::utils::prompt::{prompt_email, prompt_password, prompt_text};
11-
use crate::utils::rpgp::{
12-
generate_hashed_primary_user_id, generate_key_pair, get_vault_location,
13-
user_id,
14-
};
9+
use crate::utils::prompt::{prompt_password, prompt_text};
10+
use crate::utils::rpgp::{generate_key_pair, get_vault_location, user_id};
1511
use crate::utils::vecu8::ToHex;
1612
use anyhow::Context;
17-
use pgp::types::KeyTrait;
13+
use pgp::types::PublicKeyTrait;
1814
use pgp::ArmorOptions;
1915
use std::fs;
2016
use std::str;
@@ -30,9 +26,9 @@ pub struct Args {
3026
#[clap(short, long)]
3127
interactive: bool,
3228

33-
/// Nickname for the key. Do NOT use your real name.
29+
/// Username for the key. Do NOT use your real name, or anything that could be used to identify you.
3430
#[clap(short, long)]
35-
nickname: Option<String>,
31+
username: Option<String>,
3632

3733
/// Passphrase to encrypt the key with
3834
#[clap(short, long)]
@@ -71,8 +67,8 @@ pub async fn command(args: Args) -> Result<()> {
7167
let mut config = config::Config::get().context("Failed to get config")?;
7268
let settings = config.get_settings()?;
7369

74-
let nickname = args
75-
.nickname
70+
let username = args
71+
.username
7672
.unwrap_or_else(|| prompt_text("Set a nickname for the key").unwrap());
7773

7874
let passphrase = args
@@ -87,7 +83,7 @@ pub async fn command(args: Args) -> Result<()> {
8783
eprintln!("You can disable this warning with `envx config --no-warn-on-short-passwords`");
8884
}
8985

90-
let key_pair = generate_key_pair(&nickname, passphrase.to_owned())
86+
let key_pair = generate_key_pair(&username, passphrase.to_owned())
9187
.expect("Failed to generate key pair");
9288

9389
let priv_key = key_pair
@@ -100,7 +96,7 @@ pub async fn command(args: Args) -> Result<()> {
10096
.to_armored_string(ArmorOptions::default())
10197
.expect("Failed to convert public key to armored ASCII string");
10298

103-
let fingerprint = key_pair.secret_key.fingerprint().to_hex();
99+
let fingerprint = key_pair.secret_key.fingerprint().as_bytes().to_hex();
104100

105101
let result =
106102
set_password(&fingerprint, &passphrase, settings.get_keyring_expiry());
@@ -154,13 +150,13 @@ pub async fn command(args: Args) -> Result<()> {
154150
let mut key_to_insert: Key = Key {
155151
fingerprint: fingerprint.clone(),
156152
note: "".to_string(),
157-
primary_user_id: user_id(&nickname),
153+
primary_user_id: user_id(&username),
158154
pubkey_only: None,
159155
uuid: None,
160156
};
161157

162158
if config.online {
163-
match SDK::new_user(&nickname, &pub_key).await {
159+
match SDK::new_user(&username, &pub_key).await {
164160
Ok(id) => {
165161
println!("User ID: {}", id);
166162
key_to_insert.uuid = Some(id);

src/commands/get/projects.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ pub async fn command(args: Args) -> Result<()> {
1616
let remote_projects = SDK::list_projects(&key.fingerprint)
1717
.await
1818
.context("Failed to get projects from server".red())?;
19+
1920
let remote_projects = remote_projects
2021
.iter()
2122
.filter(|p| {

src/commands/import.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::utils::rpgp::get_vault_location;
55
use crate::utils::vecu8::ToHex;
66
use clap::Subcommand;
77
use pgp::ArmorOptions;
8-
use pgp::{types::KeyTrait, Deserializable};
8+
use pgp::{types::PublicKeyTrait, Deserializable};
99
use std::fs;
1010
use std::io::Cursor;
1111

@@ -33,7 +33,8 @@ pub async fn command(args: Args) -> Result<()> {
3333
pgp::composed::SignedPublicKey::from_armor_single(buf)
3434
.context("Failed to parse armored key")?;
3535

36-
let fingerprint = pubkey.fingerprint().to_hex().to_uppercase();
36+
let fingerprint =
37+
pubkey.fingerprint().as_bytes().to_hex().to_uppercase();
3738

3839
println!("Importing key: {}", fingerprint);
3940

@@ -46,6 +47,7 @@ pub async fn command(args: Args) -> Result<()> {
4647
.id()
4748
.to_string();
4849

50+
// TODO: fix this
4951
let (primary_user_id, hashed_note) = if only_hex(&first_user_id)
5052
&& first_user_id.len() == 128
5153
{
@@ -69,7 +71,6 @@ pub async fn command(args: Args) -> Result<()> {
6971
note: "".to_string(),
7072
pubkey_only: Some(true),
7173
primary_user_id,
72-
hashed_note,
7374
uuid: None,
7475
};
7576

src/commands/update.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
use std::cmp::Ordering;
2+
use std::process::Stdio;
23

34
use crate::utils::{compare_semver, config::Config};
45

56
use super::*;
67

7-
/// If your key is not in the database, use this command to upload it
8+
/// Update the envx CLI
89
#[derive(Parser)]
910
pub struct Args {}
1011

1112
pub async fn command(_args: Args) -> Result<()> {
1213
let mut config = Config::get()?;
1314
let result = config.check_update(true).await?;
1415
config.write()?;
16+
1517
let latest_version = if let Some(latest_version) = result {
1618
latest_version
1719
} else {
@@ -29,18 +31,19 @@ pub async fn command(_args: Args) -> Result<()> {
2931
env!("CARGO_PKG_VERSION").yellow(),
3032
latest_version.bright_yellow(),
3133
);
32-
// println!(
33-
// "Run `{}` to update\n",
34-
// "curl -fsSL https://get.envx.sh | sh".green()
35-
// );
34+
} else {
35+
println!("You are already on or ahead of the latest version");
36+
println!("Current version: {}", env!("CARGO_PKG_VERSION"));
37+
println!("Latest version: {}", latest_version);
38+
return Ok(());
3639
}
3740

3841
let mut output = tokio::process::Command::new("sh")
3942
.arg("-c")
4043
.arg("curl -fsSL https://get.envx.sh | sh")
41-
.stdout(std::process::Stdio::inherit())
42-
.stderr(std::process::Stdio::inherit())
43-
.stdin(std::process::Stdio::inherit())
44+
.stdout(Stdio::inherit())
45+
.stderr(Stdio::inherit())
46+
.stdin(Stdio::inherit())
4447
.spawn()?;
4548

4649
let status = output.wait().await?;

src/utils/auth.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use anyhow::{anyhow, Context};
33
use chrono::Utc;
44
use pgp::composed::message::Message;
55
use pgp::{crypto, ArmorOptions, Deserializable, SignedSecretKey};
6+
use rand::rngs::OsRng;
67
use serde::{Deserialize, Serialize};
78

89
use super::keyring::try_get_password;
@@ -27,7 +28,9 @@ pub async fn get_token(
2728
let passphrase = try_get_password(fingerprint, &config)?;
2829
let pw = || passphrase;
2930

30-
let signature = msg.sign(&key, pw, crypto::hash::HashAlgorithm::SHA3_512);
31+
let rng = OsRng;
32+
let signature =
33+
msg.sign(rng, &key, pw, crypto::hash::HashAlgorithm::SHA3_512);
3134

3235
let signature = match signature {
3336
Ok(s) => s,

src/utils/rpgp.rs

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::config::Config;
22
use super::keyring::try_get_password;
3-
use anyhow::{anyhow, Context, Ok, Result};
3+
use anyhow::{anyhow, bail, Context, Ok, Result};
44
use colored::Colorize;
55
use crypto_hash::{hex_digest, Algorithm};
66
use hex::ToHex;
@@ -12,6 +12,7 @@ use pgp::{
1212
Deserializable,
1313
};
1414
use rand::prelude::*;
15+
use rand::rngs::OsRng;
1516
use rayon::iter::{IntoParallelRefIterator, ParallelIterator};
1617
use smallvec::*;
1718
use std::{fs, io::Cursor, path::Path};
@@ -57,18 +58,18 @@ pub fn generate_key_pair(nickname: &str, password: String) -> Result<KeyPair> {
5758
.context("Failed to create secret key params.")?;
5859

5960
let secret_key = secret_key_params
60-
.generate()
61+
.generate(OsRng)
6162
.context("Failed to generate a plain key.")?;
6263

6364
let passwd_fn = || password.clone();
6465

6566
let signed_secret_key = secret_key
66-
.sign(passwd_fn)
67+
.sign(OsRng, passwd_fn)
6768
.context("Failed to sign secret key.")?;
6869

6970
let public_key = signed_secret_key.public_key();
7071
let signed_public_key = public_key
71-
.sign(&signed_secret_key, passwd_fn)
72+
.sign(OsRng, &signed_secret_key, passwd_fn)
7273
.context("Failed to sign public key.")?;
7374

7475
let key_pair = KeyPair {
@@ -85,7 +86,7 @@ pub fn encrypt(msg: &str, pubkey_str: &str) -> Result<String> {
8586
let msg = composed::message::Message::new_literal("none", msg);
8687

8788
let mut rng = StdRng::from_entropy();
88-
let new_msg = msg.encrypt_to_keys(
89+
let new_msg = msg.encrypt_to_keys_seipdv1(
8990
&mut rng,
9091
crypto::sym::SymmetricKeyAlgorithm::AES128,
9192
&[&pubkey],
@@ -102,7 +103,7 @@ pub fn encrypt_multi(msg: &str, pubkeys: &[SignedPublicKey]) -> Result<String> {
102103

103104
let msg = composed::message::Message::new_literal("none", msg);
104105

105-
let new_msg = msg.encrypt_to_keys(
106+
let new_msg = msg.encrypt_to_keys_seipdv1(
106107
&mut rng,
107108
crypto::sym::SymmetricKeyAlgorithm::AES128,
108109
&borrowed_keys,
@@ -111,15 +112,33 @@ pub fn encrypt_multi(msg: &str, pubkeys: &[SignedPublicKey]) -> Result<String> {
111112
Ok(new_msg.to_armored_string(ArmorOptions::default())?)
112113
}
113114

115+
trait GetRecipients {
116+
fn get_recipients(&self) -> Vec<&pgp::types::KeyId>;
117+
}
118+
119+
impl GetRecipients for composed::message::Message {
120+
fn get_recipients(&self) -> Vec<&pgp::types::KeyId> {
121+
match self {
122+
Message::Encrypted { esk, .. } => esk
123+
.iter()
124+
.filter_map(|e| match e {
125+
pgp::Esk::PublicKeyEncryptedSessionKey(k) => k.id().ok(),
126+
_ => None,
127+
})
128+
.collect::<Vec<&pgp::types::KeyId>>(),
129+
_ => todo!(),
130+
}
131+
}
132+
}
133+
114134
pub fn decrypt(
115135
armored: &str,
116136
seckey: &SignedSecretKey,
117137
password: String,
118138
) -> Result<String> {
119-
let buf = Cursor::new(armored);
120-
let (msg, _) = composed::message::Message::from_armor_single(buf)
139+
let (msg, _) = composed::message::Message::from_string(armored)
121140
.context("Failed to convert &str to armored message")?;
122-
let (dec, _) = msg
141+
let (dec, k) = msg
123142
.decrypt(|| password, &[seckey])
124143
.context("Decrypting the message")?;
125144

@@ -148,9 +167,8 @@ pub fn generate_hashed_primary_user_id(name: String, email: String) -> String {
148167
}
149168

150169
pub fn decrypt_full(message: String, config: &Config) -> Result<String> {
151-
let buf = Cursor::new(message.clone());
152-
let (msg, _) = composed::message::Message::from_armor_single(buf)
153-
.context("Failed to convert &str to armored message")?;
170+
let (msg, _) = composed::message::Message::from_string(&message)
171+
.context("Failed to parse message")?;
154172

155173
let recipients: Vec<String> = msg
156174
.get_recipients()
@@ -209,7 +227,8 @@ pub fn decrypt_full_many(
209227
return Ok(vec![]);
210228
};
211229

212-
let msg = Message::from_string(first.as_str())?.0;
230+
let (msg, headers) = Message::from_string(first.as_str())?;
231+
dbg!(&headers);
213232

214233
let recipients: Vec<String> = msg
215234
.get_recipients()

src/utils/vecu8.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,9 @@ impl ToHex for Vec<u8> {
77
hex::encode(self)
88
}
99
}
10+
11+
impl ToHex for [u8] {
12+
fn to_hex(&self) -> String {
13+
hex::encode(self)
14+
}
15+
}

0 commit comments

Comments
 (0)