rsign 0.1.2

A command-line tool to sign files and verify signatures.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
extern crate rsign;
extern crate sodiumoxide;
extern crate libsodium_sys as ffi;
extern crate chrono;
extern crate base64;
extern crate clap;

use chrono::prelude::*;
use rsign::*;
use sodiumoxide::crypto::sign::SIGNATUREBYTES;
use std::fmt::Debug;

use std::fs::{OpenOptions, File, DirBuilder};
use std::io::{self, BufWriter, BufReader, BufRead, Read, Write};
#[cfg(not(windows))]
use std::os::unix::fs::OpenOptionsExt;
use std::path::{Path, PathBuf};
use std::str::FromStr;

fn create_dir<P>(path: P) -> Result<()>
    where P: AsRef<Path> + Debug
{
    DirBuilder::new()
        .recursive(true)
        .create(&path)
        .map_err(|e| PError::new(ErrorKind::Io, format!("while creating: {:?} - {}", path, e)))
        .and_then(|_| Ok(()))

}
#[cfg(windows)]
fn create_file<P>(path: P, _mode: u32) -> Result<BufWriter<File>>
    where P: AsRef<Path> + Copy + Debug
{
    OpenOptions::new()
        .write(true)
        .create_new(true)
        .open(path)
        .map_err(|e| PError::new(ErrorKind::Io, format!("while creating: {:?} - {}", path, e)))
        .and_then(|file| Ok(BufWriter::new(file)))
}
#[cfg(not(windows))]
fn create_file<P>(path: P, mode: u32) -> Result<BufWriter<File>>
    where P: AsRef<Path> + Copy + Debug
{
    OpenOptions::new()
        .mode(mode)
        .write(true)
        .create_new(true)
        .open(path)
        .map_err(|e| PError::new(ErrorKind::Io, format!("while creating: {:?} - {}", path, e)))
        .and_then(|file| Ok(BufWriter::new(file)))
}

fn create_sig_file<P>(path: P) -> Result<BufWriter<File>>
    where P: AsRef<Path> + Copy + Debug
{
    OpenOptions::new()
        .write(true)
        .create(true)
        .open(path)
        .map_err(|e| PError::new(ErrorKind::Io, format!("while creating: {:?} - {}", path, e)))
        .and_then(|file| Ok(BufWriter::new(file)))
}

fn sk_load<P: AsRef<Path>>(sk_path: P) -> Result<SeckeyStruct> {
    let mut sk_str = OpenOptions::new()
        .read(true)
        .open(sk_path)
        .map_err(|e| PError::new(ErrorKind::Io, e))
        .and_then(|file| {
            let mut sk_buf = BufReader::new(file);
            let mut _comment = String::new();
            sk_buf.read_line(&mut _comment)?;
            let mut encoded_buf = String::new();
            sk_buf.read_line(&mut encoded_buf)?;
            base64::decode(encoded_buf.trim())
                .map_err(|e| PError::new(ErrorKind::Io, e))
                .and_then(|decoded_buf| SeckeyStruct::from(&decoded_buf[..]))

        })?;

    let pwd = get_password("Password: ")?;
    write!(io::stdout(),
           "Deriving a key from the password and decrypting the secret key... ")
            .map_err(|e| PError::new(ErrorKind::Io, e))
            .and_then(|_| {
                          io::stdout().flush()?;
                          derive_and_crypt(&mut sk_str, &pwd.as_bytes())
                      })
            .and(writeln!(io::stdout(), "done").map_err(|e| PError::new(ErrorKind::Io, e)))?;
    sk_str
        .read_checksum()
        .map_err(|e| From::from(e))
        .and_then(|checksum_vec| {
            let mut chk = [0u8; BYTES];
            chk.copy_from_slice(&checksum_vec[..]);
            if chk != sk_str.keynum_sk.chk {
                Err(PError::new(ErrorKind::Verify, "Wrong password for that key"))
            } else {
                Ok(sk_str)
            }
        })
}

fn pk_load<P>(pk_path: P) -> Result<PubkeyStruct> 
    where P: AsRef<Path> + Copy + Debug 
{
    let pk = OpenOptions::new()
        .read(true)
        .open(pk_path)
        .map_err(|e| {
                     PError::new(ErrorKind::Io,
                                 format!("couldn't retrieve public key from {:?}: {}", pk_path, e))
                 })
        .and_then(|file| {
            let mut pk_buf = BufReader::new(file);
            let mut _comment = String::new();
            pk_buf.read_line(&mut _comment)?;
            let mut encoded_buf = String::new();
            pk_buf.read_line(&mut encoded_buf)?;
            if encoded_buf.trim().len() != PK_B64_ENCODED_LEN {
                return Err(PError::new(ErrorKind::Io,
                                       format!("base64 conversion failed - was an actual \
                                                public key given?")));
            }
            base64::decode(encoded_buf.trim())
                .map_err(|e| {
                             PError::new(ErrorKind::Io,
                                         format!("base64 conversion failed -
                            was an actual public key given?: {}",
                                                 e))
                         })
                .and_then(|decoded_buf| PubkeyStruct::from(&decoded_buf))
        })?;
    Ok(pk)
}

fn pk_load_string(pk_string: &str) -> Result<PubkeyStruct> {
    let pk = String::from_str(pk_string)
        .map_err(|e| PError::new(ErrorKind::Io, e))
        .and_then(|encoded_string| {
            if encoded_string.trim().len() != PK_B64_ENCODED_LEN {
                return Err(PError::new(ErrorKind::Io,
                                       format!("base64 conversion failed -
                 was an actual public key given?")));
            }
            base64::decode(encoded_string.as_bytes())
                .map_err(|e| {
                             PError::new(ErrorKind::Io,
                                         format!("base64 conversion
                          failed - was an actual public key given?: {}",
                                                 e))
                         })
                .and_then(|decoded_string| PubkeyStruct::from(&decoded_string))
        })?;
    Ok(pk)
}

fn sig_load<P>(sig_file: P,
               global_sig: &mut Vec<u8>,
               trusted_comment: &mut Vec<u8>,
               hashed: &mut bool)
               -> Result<SigStruct>
    where P: AsRef<Path> + Copy + Debug
{
    File::open(sig_file)
        .map_err(|e| PError::new(ErrorKind::Io, format!("{} {:?}", e, sig_file)))
        .and_then(|file| {
            let mut buf = BufReader::new(file);
            let mut untrusted_comment = String::with_capacity(COMMENTBYTES);
            buf.read_line(&mut untrusted_comment)
                .map_err(|e| PError::new(ErrorKind::Io, e))
                .and_then(|_| {
                    let mut sig_string = String::with_capacity(SigStruct::len());
                    buf.read_line(&mut sig_string)
                        .map_err(|e| PError::new(ErrorKind::Io, e))
                        .and_then(|_| {
                            let mut t_comment = String::with_capacity(TRUSTEDCOMMENTMAXBYTES);
                            buf.read_line(&mut t_comment)
                                .map_err(|e| PError::new(ErrorKind::Io, e))
                                .and_then(|_| {
                                    let mut g_sig = String::with_capacity(SIGNATUREBYTES);
                                    buf.read_line(&mut g_sig)
                                        .map_err(|e| PError::new(ErrorKind::Io, e))
                                        .and_then(|_| {
                                            if !untrusted_comment.starts_with(COMMENT_PREFIX) {
                                                return Err(PError::new(ErrorKind::Verify,
                                                                       format!("Untrusted comment must start with: {}", COMMENT_PREFIX)));
                                            }
                                            base64::decode(sig_string.trim().as_bytes())
                                                .map_err(|e| PError::new(ErrorKind::Io, e))
                                                .and_then(|sig_bytes| {
                                                    SigStruct::from(&sig_bytes).and_then(|sig| {
                                                        if !t_comment.starts_with(TRUSTED_COMMENT_PREFIX) {
                                                            return Err(PError::new(ErrorKind::Verify,
                                                                                   format!("trusted comment should start with: {}",
                                                                                           TRUSTED_COMMENT_PREFIX)));
                                                        }
                                                        if sig.sig_alg == SIGALG {
                                                            *hashed = false;
                                                        } else if sig.sig_alg == SIGALG_HASHED {
                                                            *hashed = true;
                                                        } else {
                                                            return Err(PError::new(ErrorKind::Verify,
                                                                                   format!("Unsupported signature algorithm")));
                                                        }
                                                        let _ = t_comment.drain(..TR_COMMENT_PREFIX_LEN).count();
                                                        trusted_comment.extend(sig.sig.iter());
                                                        trusted_comment.extend_from_slice(t_comment.trim().as_bytes());
                                                        base64::decode(g_sig.trim().as_bytes())
                                                            .map_err(|e| PError::new(ErrorKind::Io, e))
                                                            .and_then(|comm_sig| {
                                                                          global_sig.extend_from_slice(&comm_sig);
                                                                          Ok(sig)
                                                                      })
                                                    })
                                                })

                                        })
                                })
                        })
                })
        })
}

fn load_message_file<P>(message_file: P, hashed: &bool) -> Result<Vec<u8>>
    where P: AsRef<Path> + Copy + Debug
{
    if *hashed {
        return hash_message_file(message_file);
    }
    OpenOptions::new()
        .read(true)
        .open(message_file)
        .map_err(|e| PError::new(ErrorKind::Io, e))
        .and_then(|mut file| {
            if file.metadata().unwrap().len() > (1u64 << 30) {
                return Err(PError::new(ErrorKind::Io,
                                       format!("{:?} is larger than 1G try using -H",
                                               message_file)));
            }
            let mut msg_buf: Vec<u8> = Vec::new();
            file.read_to_end(&mut msg_buf)?;
            Ok(msg_buf)
        })
}

fn hash_message_file<P>(message_file: P) -> Result<Vec<u8>>
    where P: AsRef<Path> + Copy
{
    OpenOptions::new()
        .read(true)
        .open(message_file)
        .map_err(|e| PError::new(ErrorKind::Io, e))
        .and_then(|file| {
            let mut buf_reader = BufReader::new(file);
            let mut buf_chunk = [0u8; 65536];
            let state_sz = unsafe { ffi::crypto_generichash_statebytes() };
            let mut state: Vec<u8> = vec![0;state_sz];
            let ptr_state = state.as_mut_ptr() as *mut ffi::crypto_generichash_state;
            generichash::init(ptr_state).unwrap();
            while buf_reader.read(&mut buf_chunk).unwrap() > 0 {
                generichash::update(ptr_state, &buf_chunk).unwrap();
            }
            Ok(generichash::finalize(ptr_state)
                   .unwrap()
                   .as_ref()
                   .to_vec())
        })

}

fn run<'a>(args: clap::ArgMatches<'a>) -> Result<()> {

    if let Some(generate_action) = args.subcommand_matches("generate") {
        let force = generate_action.is_present("force");
        let pk_path = match generate_action.value_of("pk_path") {
            Some(path) => PathBuf::from(path),
            None => PathBuf::from(SIG_DEFAULT_PKFILE),
        };
        if pk_path.exists() {
            if !force {
                return Err(PError::new(ErrorKind::Io,
                                       format!("Key generation aborted:\n
                {:?} already exists\n
                If you really want to overwrite the existing key pair, add the -f switch to\n
                force this operation.",
                                               pk_path)));
            } else {
                try!(std::fs::remove_file(&pk_path));
            }
        }

        let sk_path = match generate_action.value_of("sk_path") {
            Some(path) => {
                let complete_path = PathBuf::from(path);
                let mut dir = complete_path.clone();
                dir.pop();
                try!(create_dir(dir));
                complete_path
            }
            None => {
                let env_path = std::env::var(SIG_DEFAULT_CONFIG_DIR_ENV_VAR);
                let path = match env_path {
                    Ok(env_path) => {
                        let mut complete_path = PathBuf::from(env_path);
                        if !complete_path.exists() {
                            return Err(PError::new(ErrorKind::Io,
                                                   format!("folder {:?} referenced by {} \
                                                            doesn't exists,
                                     \
                                                            you'll have to create yourself",
                                                           complete_path,
                                                           SIG_DEFAULT_CONFIG_DIR_ENV_VAR)));
                        }
                        complete_path.push(SIG_DEFAULT_SKFILE);
                        complete_path
                    }
                    Err(_) => {
                        let home_path =
                            std::env::home_dir().ok_or(PError::new(ErrorKind::Io,
                                                                   "can't find home dir"));
                        let mut complete_path = PathBuf::from(home_path.unwrap());
                        complete_path.push(SIG_DEFAULT_CONFIG_DIR);
                        if !complete_path.exists() {
                            try!(create_dir(&complete_path));
                        }
                        complete_path.push(SIG_DEFAULT_SKFILE);
                        complete_path
                    }
                };
                path
            }
        };

        if sk_path.exists() {
            if !force {
                return Err(PError::new(ErrorKind::Io,
                                       format!("Key generation aborted:
{:?} already exists

If you really want to overwrite the existing key pair, add the -f switch to
force this operation.",
                                               sk_path)));
            } else {
                try!(std::fs::remove_file(&sk_path));
            }
        }
        let pk_file = create_file(&pk_path, 0o644)?;
        let sk_file = create_file(&sk_path, 0o600)?;
        let (pk_str, _) = generate(pk_file, sk_file, generate_action.value_of("comment"))?;

        println!("\nThe secret key was saved as {:?} - Keep it secret!",
                 sk_path);
        println!("The public key was saved as {:?} - That one can be public.\n",
                 pk_path);
        println!("Files signed using this key pair can be verified with the following command:\n");
        println!("rsign verify <file> -P {}",
                 base64::encode(pk_str.bytes().as_slice()));

    } else if let Some(sign_action) = args.subcommand_matches("sign") {
        let sk_path = match sign_action.value_of("sk_path") {
            Some(path) => PathBuf::from(path),
            None => {
                let home_path =
                    std::env::home_dir().ok_or(PError::new(ErrorKind::Io, "can't find home dir"));
                let mut complete_path = PathBuf::from(home_path.unwrap());
                complete_path.push(SIG_DEFAULT_CONFIG_DIR);
                complete_path.push(SIG_DEFAULT_SKFILE);
                complete_path
            }
        };
        if !sk_path.exists() {
            return Err(PError::new(ErrorKind::Io,
                                   format!("can't find secret key file at {:?}, try using -s",
                                           sk_path)));
        }

        let mut pk: Option<PubkeyStruct> = None;
        if sign_action.is_present("pk_path") {
            if let Some(filename) = sign_action.value_of("pk_path") {
                pk = Some(try!(pk_load(filename)));
            }
        } else if sign_action.is_present("public_key") {
            if let Some(string) = sign_action.value_of("public_key") {
                pk = Some(try!(pk_load_string(string)));
            }
        }
        let hashed = sign_action.is_present("hash");
        let message_file = sign_action.value_of("message").unwrap(); // safe to unwrap

        let sig_file_name = if let Some(file) = sign_action.value_of("sig_file") {
            format!("{}", file)
        } else {
            format!("{}{}", message_file, SIG_SUFFIX)
        };
        let sig_buf = create_sig_file(&sig_file_name)?;

        let sk = try!(sk_load(sk_path));

        let t_comment = if let Some(trusted_comment) = sign_action.value_of("trusted-comment") {
            format!("{}", trusted_comment)
        } else {
            format!("timestamp:{}\tfile:{}",
                    Utc::now().timestamp(),
                    message_file)
        };

        let unt_comment = if let Some(untrusted_comment) =
            sign_action.value_of("untrusted-comment") {
            format!("{}{}", COMMENT_PREFIX, untrusted_comment)
        } else {
            format!("{}{}", COMMENT_PREFIX, DEFAULT_COMMENT)
        };
        let message = load_message_file(message_file, &hashed)?;

        sign(sk,
             pk,
             sig_buf,
             message.as_ref(),
             hashed,
             t_comment.as_str(),
             unt_comment.as_str())?;
    } else if let Some(verify_action) = args.subcommand_matches("verify") {

        let input = verify_action
            .value_of("pk_path")
            .or(verify_action.value_of("public_key"));

        let pk = match input {
            Some(path_or_string) => {
                if verify_action.is_present("pk_path") {
                    try!(pk_load(path_or_string))
                } else {
                    try!(pk_load_string(path_or_string))
                }
            }
            None => try!(pk_load(SIG_DEFAULT_PKFILE)),
        };

        let message_file = verify_action.value_of("file").unwrap(); //safe to unwrap

        let sig_file_name = if let Some(file) = verify_action.value_of("sig_file") {
            format!("{}", file)
        } else {
            format!("{}{}", message_file, SIG_SUFFIX)
        };
        let mut hashed: bool = false;

        let mut trusted_comment: Vec<u8> = Vec::with_capacity(TRUSTEDCOMMENTMAXBYTES);
        let mut global_sig: Vec<u8> = Vec::with_capacity(SIGNATUREBYTES);
        let sig = sig_load(sig_file_name.as_str(),
                           &mut global_sig,
                           &mut trusted_comment,
                           &mut hashed)?;

        let message = load_message_file(message_file, &hashed)?;

        verify(pk,
               sig,
               &global_sig[..],
               trusted_comment.as_ref(),
               message.as_ref(),
               verify_action.is_present("quiet"),
               verify_action.is_present("output"))?;

    } else {
        println!("{}\n", args.usage());
    }

    Ok(())
}

fn main() {
    let args = parse_args();
    run(args).map_err(|e| e.exit()).unwrap();
    std::process::exit(0);
}

#[test]
fn load_public_key_string() {
    assert!(pk_load_string("RWRzq51bKcS8oJvZ4xEm+nRvGYPdsNRD3ciFPu1YJEL8Bl/3daWaj72r").is_ok());
    assert!(pk_load_string("RWQt7oYqpar/yePp+nonossdnononovlOSkkckMMfvHuGc+0+oShmJyN5Y").is_err());
}