|
| 1 | +use std::error::Error; |
| 2 | +use std::io::{Read, Write}; |
| 3 | +use std::iter; |
| 4 | +use std::str::FromStr; |
| 5 | +use std::string::FromUtf8Error; |
| 6 | +use age; |
| 7 | +use age::armor::{ArmoredReader, ArmoredWriter}; |
| 8 | +use age::armor::Format::AsciiArmor; |
| 9 | +use age::{DecryptError, Decryptor}; |
| 10 | +use age::secrecy::{ExposeSecret, Secret, SecretString}; |
| 11 | +use age::x25519::{Identity, Recipient}; |
| 12 | +use chrono; |
| 13 | +use js_sys::{Array, JsString}; |
| 14 | +use wasm_bindgen::prelude::*; |
| 15 | +use wasm_streams::{readable::ReadableStream, writable::WritableStream}; |
| 16 | +use web_sys::{Blob, BlobPropertyBag}; |
| 17 | + |
| 18 | +mod utils; |
| 19 | +mod encrypt; |
| 20 | + |
| 21 | +#[cfg(feature = "wee_alloc")] |
| 22 | +#[global_allocator] |
| 23 | +static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT; |
| 24 | + |
| 25 | +#[wasm_bindgen] |
| 26 | +extern { |
| 27 | + #[wasm_bindgen(js_namespace = console)] |
| 28 | + fn log(s: &str); |
| 29 | +} |
| 30 | + |
| 31 | +macro_rules! console_log { |
| 32 | + // Note that this is using the `log` function imported above during |
| 33 | + // `bare_bones` |
| 34 | + ($($t:tt)*) => (log(&format_args!($($t)*).to_string())) |
| 35 | +} |
| 36 | + |
| 37 | +#[wasm_bindgen] |
| 38 | +pub struct ArcusKeypair { |
| 39 | + pubkey: Recipient, |
| 40 | + secret: SecretString |
| 41 | +} |
| 42 | + |
| 43 | +#[wasm_bindgen] |
| 44 | +impl ArcusKeypair { |
| 45 | + #[wasm_bindgen(constructor)] |
| 46 | + pub fn new() -> Self { |
| 47 | + let kp = Identity::generate(); |
| 48 | + Self { |
| 49 | + pubkey: kp.to_public(), |
| 50 | + secret: kp.to_string() |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + pub fn get_pubkey(&self) -> String { |
| 55 | + self.pubkey.to_string().clone() |
| 56 | + } |
| 57 | + |
| 58 | + pub fn get_secret(&self) -> String { |
| 59 | + self.secret.expose_secret().to_string().clone() |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +#[wasm_bindgen] |
| 64 | +pub struct Arcus {} |
| 65 | + |
| 66 | +#[wasm_bindgen] |
| 67 | +impl Arcus { |
| 68 | + #[wasm_bindgen(constructor)] |
| 69 | + pub fn new() -> Self { |
| 70 | + Self {} |
| 71 | + } |
| 72 | + |
| 73 | + pub fn encrypt_text(&self, cleartext: String, recipient_pubkey: String) -> String { |
| 74 | + utils::set_panic_hook(); |
| 75 | + |
| 76 | + let new_array = Array::new(); |
| 77 | + new_array.push(&JsString::from(recipient_pubkey)); |
| 78 | + |
| 79 | + self.encrypt_text_multi(cleartext, new_array) |
| 80 | + } |
| 81 | + |
| 82 | + pub fn encrypt_text_multi(&self, cleartext: String, recipient_pubkeys: Array) -> String { |
| 83 | + utils::set_panic_hook(); |
| 84 | + |
| 85 | + let mut recs: Vec<Box<dyn age::Recipient + Send + 'static>> = Vec::new(); |
| 86 | + |
| 87 | + for recipient_pubkey in recipient_pubkeys.iter() { |
| 88 | + if !recipient_pubkey.is_string() { |
| 89 | + console_log!("{:?} is not a string", recipient_pubkey); |
| 90 | + continue; |
| 91 | + } |
| 92 | + |
| 93 | + let pubkey = recipient_pubkey.as_string().unwrap(); |
| 94 | + let rpk_str = pubkey.as_str(); |
| 95 | + |
| 96 | + let re = match age::x25519::Recipient::from_str(rpk_str) { |
| 97 | + Ok(recipient) => recipient, |
| 98 | + Err(e) => return "invalid recipient string".to_string() |
| 99 | + }; |
| 100 | + |
| 101 | + recs.push(Box::new(re)); |
| 102 | + } |
| 103 | + |
| 104 | + let encrypted = { |
| 105 | + let encryptor = age::Encryptor::with_recipients(recs) |
| 106 | + .expect("we provided a recipient"); |
| 107 | + |
| 108 | + let mut enc_vec = vec![]; |
| 109 | + let mut writer = encryptor.wrap_output( |
| 110 | + ArmoredWriter::wrap_output(&mut enc_vec, AsciiArmor) |
| 111 | + .expect("armored wrap_output failed")) |
| 112 | + .expect("wrap_output failed"); |
| 113 | + |
| 114 | + writer.write_all(cleartext.as_bytes()) |
| 115 | + .expect("write_all failed"); |
| 116 | + |
| 117 | + match writer.finish() { |
| 118 | + Ok(aw) => { |
| 119 | + aw.finish().expect("armored writer could not finish"); |
| 120 | + }, |
| 121 | + Err(e) => { |
| 122 | + console_log!("{:?}", e); |
| 123 | + } |
| 124 | + }; |
| 125 | + |
| 126 | + enc_vec |
| 127 | + }; |
| 128 | + |
| 129 | + let res = match String::from_utf8(encrypted) { |
| 130 | + Ok(ct) => ct, |
| 131 | + Err(e) => e.to_string() |
| 132 | + }; |
| 133 | + |
| 134 | + res |
| 135 | + } |
| 136 | + |
| 137 | + pub fn decrypt_text(&self, ciphertext: String, own_secretkey: String) -> String { |
| 138 | + utils::set_panic_hook(); |
| 139 | + |
| 140 | + let secret_id = match age::x25519::Identity::from_str(own_secretkey.as_str()) { |
| 141 | + Ok(identity) => identity, |
| 142 | + Err(e) => { |
| 143 | + console_log!("{:?}", e); |
| 144 | + return "error parsing secret key".to_string() |
| 145 | + } |
| 146 | + }; |
| 147 | + |
| 148 | + let decrypted = { |
| 149 | + let decryptor = match age::Decryptor::new(ArmoredReader::new(ciphertext.as_bytes())).expect("Error") { |
| 150 | + age::Decryptor::Recipients(d) => d, |
| 151 | + _ => unreachable!(), |
| 152 | + }; |
| 153 | + |
| 154 | + let key = age::x25519::Identity::from_str(own_secretkey.as_str()).unwrap(); |
| 155 | + |
| 156 | + let mut dec_vec = vec![]; |
| 157 | + let mut reader = decryptor |
| 158 | + .decrypt(iter::once(&key as &dyn age::Identity)) |
| 159 | + .expect("decryptor.decrypt failed"); |
| 160 | + reader.read_to_end(&mut dec_vec).expect("read_to_end failed"); |
| 161 | + dec_vec |
| 162 | + }; |
| 163 | + |
| 164 | + let res = match String::from_utf8(decrypted) { |
| 165 | + Ok(ct) => ct, |
| 166 | + Err(e) => e.to_string() |
| 167 | + }; |
| 168 | + |
| 169 | + res |
| 170 | + } |
| 171 | + |
| 172 | + pub fn generate_key(&self) -> ArcusKeypair { |
| 173 | + ArcusKeypair::new() |
| 174 | + } |
| 175 | + |
| 176 | + pub fn pub_from_secret(&self, secret_key: String) -> String { |
| 177 | + let id = match age::x25519::Identity::from_str(secret_key.as_str()) { |
| 178 | + Ok(parsed_id) => parsed_id, |
| 179 | + Err(e) => return e.to_string() |
| 180 | + }; |
| 181 | + |
| 182 | + id.to_public().to_string() |
| 183 | + } |
| 184 | +} |
| 185 | + |
| 186 | + |
| 187 | +#[wasm_bindgen] |
| 188 | +pub struct X25519Identity { |
| 189 | + identity: age::x25519::Identity, |
| 190 | + created: chrono::DateTime<chrono::Local>, |
| 191 | +} |
| 192 | + |
| 193 | +#[wasm_bindgen] |
| 194 | +impl X25519Identity { |
| 195 | + /// Generates a new age identity. |
| 196 | + pub fn generate() -> Self { |
| 197 | + // This is an entrance from JS to our WASM APIs; perform one-time setup steps. |
| 198 | + utils::set_panic_hook(); |
| 199 | + |
| 200 | + X25519Identity { |
| 201 | + identity: age::x25519::Identity::generate(), |
| 202 | + created: chrono::Local::now(), |
| 203 | + } |
| 204 | + } |
| 205 | + |
| 206 | + /// Writes this identity to a blob that can be saved as a file. |
| 207 | + pub fn write(&self) -> Result<Blob, JsValue> { |
| 208 | + let output = format!( |
| 209 | + "# created: {}\n# recipient: {}\n{}", |
| 210 | + self.created |
| 211 | + .to_rfc3339_opts(chrono::SecondsFormat::Secs, true), |
| 212 | + self.identity.to_public(), |
| 213 | + self.identity.to_string().expose_secret() |
| 214 | + ); |
| 215 | + |
| 216 | + Blob::new_with_u8_array_sequence_and_options( |
| 217 | + &Array::of1(&JsValue::from_str(&output)).into(), |
| 218 | + &BlobPropertyBag::new().type_("text/plain;charset=utf-8"), |
| 219 | + ) |
| 220 | + } |
| 221 | + |
| 222 | + /// Returns the recipient corresponding to this identity. |
| 223 | + pub fn recipient(&self) -> String { |
| 224 | + self.identity.to_public().to_string() |
| 225 | + } |
| 226 | +} |
| 227 | + |
0 commit comments