rns-git 0.3.0

Reticulum Git transport tools
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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
use std::fs;
use std::io::{self, Read};
use std::path::PathBuf;

use rns_core::msgpack::{self, Value};
use rns_crypto::identity::Identity;
use rns_crypto::sha256::sha256;

use crate::{Error, Result};

const SSHSIG_MAGIC: &[u8] = b"SSHSIG";
const SSHSIG_VERSION: u32 = 1;
const NAMESPACE_GIT: &[u8] = b"git";
const HASH_ALGORITHM: &[u8] = b"sha256";
const SIG_LEN: usize = 64;

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SignatureStatus {
    pub signed: bool,
    pub valid: bool,
    pub signer_hash: Option<[u8; 16]>,
    pub author_match: bool,
    pub message: String,
}

#[derive(Debug, Default)]
struct Options {
    op: Option<String>,
    namespace: String,
    keyfile: Option<PathBuf>,
    sigfile: Option<PathBuf>,
    principal: Option<String>,
    file: Option<PathBuf>,
}

pub fn main<I>(args: I) -> Result<()>
where
    I: IntoIterator<Item = String>,
{
    let options = parse_options(args)?;
    match options.op.as_deref() {
        Some("sign") => {
            let keyfile = options
                .keyfile
                .as_deref()
                .ok_or_else(|| Error::msg("missing identity file"))?;
            let identity = rns_net::storage::load_identity(keyfile)?;
            let (message, output_path) = if let Some(file) = options.file.as_deref() {
                (
                    fs::read(file)?,
                    Some(PathBuf::from(format!("{}.sig", file.display()))),
                )
            } else {
                let mut message = Vec::new();
                io::stdin().read_to_end(&mut message)?;
                (message, None)
            };
            let armored = sign_message(&identity, &message)?;
            if let Some(path) = output_path {
                fs::write(path, armored)?;
            } else {
                print!("{armored}");
            }
        }
        Some("find-principals") => {
            let sigfile = options
                .sigfile
                .as_deref()
                .ok_or_else(|| Error::msg("missing signature file"))?;
            let armored = fs::read_to_string(sigfile)?;
            let signer = find_principal(&armored)?;
            println!("{}", crate::util::hex(&signer));
        }
        Some("check-novalidate") => {
            let sigfile = options
                .sigfile
                .as_deref()
                .ok_or_else(|| Error::msg("missing signature file"))?;
            let armored = fs::read_to_string(sigfile)?;
            if !check_novalidate(&armored) {
                return Err(Error::msg(
                    "signature is not a valid Reticulum Git signature",
                ));
            }
        }
        Some("verify") => {
            let sigfile = options
                .sigfile
                .as_deref()
                .ok_or_else(|| Error::msg("missing signature file"))?;
            let armored = fs::read_to_string(sigfile)?;
            let mut message = Vec::new();
            io::stdin().read_to_end(&mut message)?;
            let status = verify_message_signature(&armored, &message, options.principal.as_deref());
            if status.valid && status.author_match {
                println!("{}", status.message);
            } else {
                return Err(Error::msg(status.message));
            }
        }
        _ => return Err(Error::msg(usage())),
    }
    if options.namespace.as_bytes() != NAMESPACE_GIT {
        return Err(Error::msg("only git namespace is supported"));
    }
    Ok(())
}

fn parse_options<I>(args: I) -> Result<Options>
where
    I: IntoIterator<Item = String>,
{
    let mut options = Options {
        namespace: "git".into(),
        ..Options::default()
    };
    let mut iter = args.into_iter();
    while let Some(arg) = iter.next() {
        match arg.as_str() {
            "-Y" => options.op = iter.next(),
            "-n" => {
                options.namespace = iter.next().ok_or_else(|| Error::msg("missing namespace"))?;
            }
            "-f" => {
                options.keyfile = Some(PathBuf::from(
                    iter.next()
                        .ok_or_else(|| Error::msg("missing key/signers file"))?,
                ));
            }
            "-s" => {
                options.sigfile = Some(PathBuf::from(
                    iter.next()
                        .ok_or_else(|| Error::msg("missing signature file"))?,
                ));
            }
            "-I" => {
                options.principal =
                    Some(iter.next().ok_or_else(|| Error::msg("missing principal"))?);
            }
            "-O" => {
                let _ = iter.next();
            }
            other if other.starts_with("-O") => {}
            "-h" | "--help" => return Err(Error::msg(usage())),
            other if other.starts_with('-') => {
                return Err(Error::msg(format!("unknown option {other}")));
            }
            other => options.file = Some(PathBuf::from(other)),
        }
    }
    Ok(options)
}

fn usage() -> &'static str {
    "usage: rngcs -Y <sign|find-principals|check-novalidate|verify> [-f KEY] [-s SIG] [-I PRINCIPAL] [FILE]"
}

pub fn sign_message(identity: &Identity, message: &[u8]) -> Result<String> {
    let rsg = create_rsg(identity, message)?;
    let ssh_pubkey = git_ssh_public_key(identity)?;
    let blob = create_ssh_signature(&ssh_pubkey, NAMESPACE_GIT, b"", HASH_ALGORITHM, &rsg);
    Ok(armor_ssh_signature(&blob))
}

pub fn find_principal(armored: &str) -> Result<[u8; 16]> {
    let sig = parse_armored_ssh_signature(armored)?;
    if sig.namespace != NAMESPACE_GIT {
        return Err(Error::msg("invalid signature namespace"));
    }
    let envelope = rsg_envelope(&sig.signature_data)?;
    let meta = envelope
        .map_get("meta")
        .ok_or_else(|| Error::msg("RSG is missing metadata"))?;
    let signer = meta
        .map_get("signer")
        .and_then(Value::as_bin)
        .ok_or_else(|| Error::msg("RSG is missing signer"))?;
    signer
        .try_into()
        .map_err(|_| Error::msg("invalid signer hash length"))
}

pub fn check_novalidate(armored: &str) -> bool {
    parse_armored_ssh_signature(armored)
        .and_then(|sig| {
            if sig.namespace != NAMESPACE_GIT {
                return Err(Error::msg("invalid namespace"));
            }
            let _ = rsg_envelope(&sig.signature_data)?;
            Ok(())
        })
        .is_ok()
}

pub fn verify_message_signature(
    armored: &str,
    message: &[u8],
    principal: Option<&str>,
) -> SignatureStatus {
    match verify_message_signature_inner(armored, message, principal) {
        Ok(status) => status,
        Err(err) => SignatureStatus {
            signed: true,
            valid: false,
            signer_hash: None,
            author_match: false,
            message: err.to_string(),
        },
    }
}

fn verify_message_signature_inner(
    armored: &str,
    message: &[u8],
    principal: Option<&str>,
) -> Result<SignatureStatus> {
    let sig = parse_armored_ssh_signature(armored)?;
    if sig.namespace != NAMESPACE_GIT {
        return Err(Error::msg("invalid commit signature namespace"));
    }
    let signer_hash = validate_rsg(&sig.signature_data, message)?;
    if let Some(principal) = principal {
        if principal != crate::util::hex(&signer_hash) {
            return Ok(SignatureStatus {
                signed: true,
                valid: true,
                signer_hash: Some(signer_hash),
                author_match: false,
                message: "principal mismatch".into(),
            });
        }
    }
    let (required_signer, object_kind) = signature_required_signer(message)
        .ok_or_else(|| Error::msg("could not determine object signer"))?;
    if required_signer != crate::util::hex(&signer_hash) {
        return Ok(SignatureStatus {
            signed: true,
            valid: true,
            signer_hash: Some(signer_hash),
            author_match: false,
            message: format!(
                "{object_kind} not signed by expected identity <{}> (actual signer <{}>)",
                required_signer,
                crate::util::hex(&signer_hash)
            ),
        });
    }
    Ok(SignatureStatus {
        signed: true,
        valid: true,
        signer_hash: Some(signer_hash),
        author_match: true,
        message: format!(
            "Good \"git\" signature for commit, signed with Reticulum Identity key <{}>",
            crate::util::hex(&signer_hash)
        ),
    })
}

fn signature_required_signer(message: &[u8]) -> Option<(String, &'static str)> {
    let (tagger, is_tag) = extract_tag_tagger(message);
    if is_tag {
        tagger.map(|tagger| (tagger, "tag"))
    } else {
        extract_commit_author(message).map(|author| (author, "commit"))
    }
}

pub fn extract_commit_author(message: &[u8]) -> Option<String> {
    for line in message.split(|b| *b == b'\n') {
        if line.is_empty() {
            break;
        }
        let Some(rest) = line.strip_prefix(b"author ") else {
            continue;
        };
        let start = rest.iter().position(|b| *b == b'<')?;
        let end = rest.iter().position(|b| *b == b'>')?;
        if end <= start {
            return None;
        }
        return std::str::from_utf8(&rest[start + 1..end])
            .ok()
            .map(str::to_string);
    }
    None
}

pub fn extract_tag_tagger(message: &[u8]) -> (Option<String>, bool) {
    let mut is_tag = false;
    for line in message.split(|b| *b == b'\n') {
        if line.is_empty() {
            break;
        }
        if line.starts_with(b"tag ") {
            is_tag = true;
            continue;
        }
        let Some(rest) = line.strip_prefix(b"tagger ") else {
            continue;
        };
        let Some(start) = rest.iter().position(|b| *b == b'<') else {
            return (None, is_tag);
        };
        let Some(end) = rest.iter().position(|b| *b == b'>') else {
            return (None, is_tag);
        };
        if end <= start {
            return (None, is_tag);
        }
        return (
            std::str::from_utf8(&rest[start + 1..end])
                .ok()
                .map(str::to_string),
            is_tag,
        );
    }
    (None, is_tag)
}

fn create_rsg(identity: &Identity, message: &[u8]) -> Result<Vec<u8>> {
    let public_key = identity
        .get_public_key()
        .ok_or_else(|| Error::msg("identity does not hold a public key"))?;
    let envelope = Value::Map(vec![
        (Value::Str("hashtype".into()), Value::Str("sha256".into())),
        (
            Value::Str("hash".into()),
            Value::Bin(sha256(message).to_vec()),
        ),
        (
            Value::Str("meta".into()),
            Value::Map(vec![
                (
                    Value::Str("signer".into()),
                    Value::Bin(identity.hash().to_vec()),
                ),
                (Value::Str("pubkey".into()), Value::Bin(public_key.to_vec())),
            ]),
        ),
    ]);
    let envelope = msgpack::pack(&envelope);
    let signature = identity
        .sign(&envelope)
        .map_err(|_| Error::msg("identity does not hold a private key"))?;
    let mut out = Vec::with_capacity(SIG_LEN + envelope.len());
    out.extend_from_slice(&signature);
    out.extend_from_slice(&envelope);
    Ok(out)
}

fn validate_rsg(rsg: &[u8], message: &[u8]) -> Result<[u8; 16]> {
    if rsg.len() <= SIG_LEN {
        return Err(Error::msg("invalid RSG"));
    }
    let signature: [u8; SIG_LEN] = rsg[..SIG_LEN].try_into().unwrap();
    let envelope_bytes = &rsg[SIG_LEN..];
    let envelope = rsg_envelope(rsg)?;
    if envelope.map_get("hashtype").and_then(Value::as_str) != Some("sha256") {
        return Err(Error::msg("unsupported RSG hash type"));
    }
    let signed_hash = envelope
        .map_get("hash")
        .and_then(Value::as_bin)
        .ok_or_else(|| Error::msg("RSG is missing hash"))?;
    if signed_hash != sha256(message).as_slice() {
        return Err(Error::msg("RSG hash does not match message"));
    }
    let meta = envelope
        .map_get("meta")
        .ok_or_else(|| Error::msg("RSG is missing metadata"))?;
    let public_key = meta
        .map_get("pubkey")
        .and_then(Value::as_bin)
        .ok_or_else(|| Error::msg("RSG is missing public key"))?;
    let public_key: [u8; 64] = public_key
        .try_into()
        .map_err(|_| Error::msg("invalid public key length"))?;
    let identity =
        Identity::from_public_key(&public_key).map_err(|_| Error::msg("invalid public key"))?;
    let signer_hash = *identity.hash();
    let meta_signer = meta
        .map_get("signer")
        .and_then(Value::as_bin)
        .ok_or_else(|| Error::msg("RSG is missing signer"))?;
    if meta_signer != signer_hash {
        return Err(Error::msg("RSG signer does not match public key"));
    }
    if !identity.verify(&signature, envelope_bytes) {
        return Err(Error::msg("invalid signature"));
    }
    Ok(signer_hash)
}

fn rsg_envelope(rsg: &[u8]) -> Result<Value> {
    if rsg.len() <= SIG_LEN {
        return Err(Error::msg("invalid RSG"));
    }
    msgpack::unpack_exact(&rsg[SIG_LEN..])
        .map_err(|e| Error::msg(format!("invalid RSG envelope: {e}")))
}

fn git_ssh_public_key(identity: &Identity) -> Result<Vec<u8>> {
    let public_key = identity
        .get_public_key()
        .ok_or_else(|| Error::msg("identity does not hold a public key"))?;
    let mut out = Vec::new();
    push_ssh_string(&mut out, b"ssh-ed25519");
    push_ssh_string(&mut out, &public_key[32..]);
    Ok(out)
}

pub struct ParsedSshSignature {
    pub namespace: Vec<u8>,
    pub signature_data: Vec<u8>,
}

pub fn parse_armored_ssh_signature(armored: &str) -> Result<ParsedSshSignature> {
    let blob = unarmor_ssh_signature(armored)?;
    parse_ssh_signature(&blob)
}

fn create_ssh_signature(
    public_key_wire: &[u8],
    namespace: &[u8],
    reserved: &[u8],
    hash_algorithm: &[u8],
    signature_data: &[u8],
) -> Vec<u8> {
    let mut out = Vec::new();
    out.extend_from_slice(SSHSIG_MAGIC);
    out.extend_from_slice(&SSHSIG_VERSION.to_be_bytes());
    push_ssh_string(&mut out, public_key_wire);
    push_ssh_string(&mut out, namespace);
    push_ssh_string(&mut out, reserved);
    push_ssh_string(&mut out, hash_algorithm);
    push_ssh_string(&mut out, signature_data);
    out
}

fn parse_ssh_signature(blob: &[u8]) -> Result<ParsedSshSignature> {
    let mut offset = 0usize;
    if !blob.starts_with(SSHSIG_MAGIC) {
        return Err(Error::msg("invalid SSH signature magic"));
    }
    offset += SSHSIG_MAGIC.len();
    let version = read_u32(blob, &mut offset)?;
    if version != SSHSIG_VERSION {
        return Err(Error::msg(format!(
            "unsupported SSH signature version {version}"
        )));
    }
    let _public_key = read_ssh_string(blob, &mut offset)?;
    let namespace = read_ssh_string(blob, &mut offset)?;
    let _reserved = read_ssh_string(blob, &mut offset)?;
    let hash_algorithm = read_ssh_string(blob, &mut offset)?;
    if hash_algorithm != HASH_ALGORITHM {
        return Err(Error::msg("unsupported SSH signature hash algorithm"));
    }
    let signature_data = read_ssh_string(blob, &mut offset)?;
    Ok(ParsedSshSignature {
        namespace,
        signature_data,
    })
}

fn push_ssh_string(out: &mut Vec<u8>, data: &[u8]) {
    out.extend_from_slice(&(data.len() as u32).to_be_bytes());
    out.extend_from_slice(data);
}

fn read_u32(data: &[u8], offset: &mut usize) -> Result<u32> {
    if data.len().saturating_sub(*offset) < 4 {
        return Err(Error::msg("truncated SSH signature"));
    }
    let value = u32::from_be_bytes(data[*offset..*offset + 4].try_into().unwrap());
    *offset += 4;
    Ok(value)
}

fn read_ssh_string(data: &[u8], offset: &mut usize) -> Result<Vec<u8>> {
    let len = read_u32(data, offset)? as usize;
    if data.len().saturating_sub(*offset) < len {
        return Err(Error::msg("truncated SSH signature string"));
    }
    let value = data[*offset..*offset + len].to_vec();
    *offset += len;
    Ok(value)
}

fn armor_ssh_signature(blob: &[u8]) -> String {
    let encoded = base64_encode(blob);
    let mut out = String::from("-----BEGIN SSH SIGNATURE-----\n");
    for chunk in encoded.as_bytes().chunks(70) {
        out.push_str(std::str::from_utf8(chunk).unwrap());
        out.push('\n');
    }
    out.push_str("-----END SSH SIGNATURE-----\n");
    out
}

fn unarmor_ssh_signature(armored: &str) -> Result<Vec<u8>> {
    let mut in_signature = false;
    let mut encoded = String::new();
    for line in armored.lines() {
        if line.contains("BEGIN SSH SIGNATURE") {
            in_signature = true;
            continue;
        }
        if line.contains("END SSH SIGNATURE") {
            break;
        }
        if in_signature {
            encoded.push_str(line.trim());
        }
    }
    if encoded.is_empty() {
        return Err(Error::msg("no SSH signature data found"));
    }
    base64_decode(&encoded).ok_or_else(|| Error::msg("invalid base64 SSH signature"))
}

fn base64_encode(data: &[u8]) -> String {
    const CHARS: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
    let mut result = String::new();
    let mut i = 0;
    while i < data.len() {
        let b0 = data[i] as u32;
        let b1 = if i + 1 < data.len() {
            data[i + 1] as u32
        } else {
            0
        };
        let b2 = if i + 2 < data.len() {
            data[i + 2] as u32
        } else {
            0
        };
        let triple = (b0 << 16) | (b1 << 8) | b2;
        result.push(CHARS[((triple >> 18) & 0x3f) as usize] as char);
        result.push(CHARS[((triple >> 12) & 0x3f) as usize] as char);
        result.push(if i + 1 < data.len() {
            CHARS[((triple >> 6) & 0x3f) as usize] as char
        } else {
            '='
        });
        result.push(if i + 2 < data.len() {
            CHARS[(triple & 0x3f) as usize] as char
        } else {
            '='
        });
        i += 3;
    }
    result
}

fn base64_decode(s: &str) -> Option<Vec<u8>> {
    let mut chars: Vec<char> = s.trim().chars().filter(|c| !c.is_whitespace()).collect();
    if chars.len() % 4 == 1 {
        return None;
    }
    while !chars.len().is_multiple_of(4) {
        chars.push('=');
    }
    let mut out = Vec::with_capacity(chars.len() / 4 * 3);
    for chunk in chars.chunks(4) {
        let mut pad = 0usize;
        let mut sextets = [0u8; 4];
        for (i, c) in chunk.iter().copied().enumerate() {
            if c == '=' {
                pad += 1;
                sextets[i] = 0;
                continue;
            }
            if pad > 0 {
                return None;
            }
            sextets[i] = match c {
                'A'..='Z' => c as u8 - b'A',
                'a'..='z' => c as u8 - b'a' + 26,
                '0'..='9' => c as u8 - b'0' + 52,
                '+' | '-' => 62,
                '/' | '_' => 63,
                _ => return None,
            };
        }
        if pad > 2 {
            return None;
        }
        let triple = ((sextets[0] as u32) << 18)
            | ((sextets[1] as u32) << 12)
            | ((sextets[2] as u32) << 6)
            | (sextets[3] as u32);
        out.push(((triple >> 16) & 0xff) as u8);
        if pad < 2 {
            out.push(((triple >> 8) & 0xff) as u8);
        }
        if pad < 1 {
            out.push((triple & 0xff) as u8);
        }
    }
    Some(out)
}

pub fn split_commit_signature(commit: &[u8]) -> (Option<String>, Vec<u8>) {
    let Ok(text) = std::str::from_utf8(commit) else {
        return (None, commit.to_vec());
    };

    let mut signature = Vec::new();
    let mut signed = String::new();
    let mut in_signature = false;
    for line in text.split_inclusive('\n') {
        let line_without_lf = line.strip_suffix('\n').unwrap_or(line);
        if let Some(rest) = line_without_lf
            .strip_prefix("gpgsig ")
            .or_else(|| line_without_lf.strip_prefix("gpgsig-sha256 "))
        {
            in_signature = true;
            signature.push(rest.to_string());
            continue;
        }
        if in_signature {
            if let Some(rest) = line_without_lf.strip_prefix(' ') {
                signature.push(rest.to_string());
                continue;
            }
            in_signature = false;
        }
        signed.push_str(line);
    }

    (
        (!signature.is_empty()).then(|| signature.join("\n")),
        signed.into_bytes(),
    )
}

pub fn extract_signature_from_commit_object(commit: &[u8]) -> Option<String> {
    let (signature, _) = split_commit_signature(commit);
    signature
}

#[cfg(test)]
mod tests {
    use super::*;
    use rns_crypto::OsRng;

    fn commit_message(author: &str) -> Vec<u8> {
        format!(
            "tree 0123456789012345678901234567890123456789\nauthor Tester <{author}> 0 +0000\ncommitter Tester <{author}> 0 +0000\n\nmessage\n"
        )
        .into_bytes()
    }

    fn tag_message(tagger: &str) -> Vec<u8> {
        format!(
            "object 0123456789012345678901234567890123456789\ntype commit\ntag v1\ntagger Tester <{tagger}> 0 +0000\n\nmessage\n"
        )
        .into_bytes()
    }

    #[test]
    fn sign_find_principal_and_verify_commit() {
        let identity = Identity::new(&mut OsRng);
        let author = crate::util::hex(identity.hash());
        let message = commit_message(&author);
        let armored = sign_message(&identity, &message).unwrap();

        assert_eq!(find_principal(&armored).unwrap(), *identity.hash());
        assert!(check_novalidate(&armored));

        let status = verify_message_signature(&armored, &message, Some(&author));
        assert!(status.valid);
        assert!(status.author_match);
        assert_eq!(status.signer_hash, Some(*identity.hash()));
    }

    #[test]
    fn verify_tag_uses_tagger_identity() {
        let identity = Identity::new(&mut OsRng);
        let tagger = crate::util::hex(identity.hash());
        let message = tag_message(&tagger);
        let armored = sign_message(&identity, &message).unwrap();

        let status = verify_message_signature(&armored, &message, Some(&tagger));
        assert!(status.valid);
        assert!(status.author_match);
        assert_eq!(extract_tag_tagger(&message), (Some(tagger), true));
    }

    #[test]
    fn verify_rejects_author_mismatch() {
        let identity = Identity::new(&mut OsRng);
        let message = commit_message("00112233445566778899aabbccddeeff");
        let armored = sign_message(&identity, &message).unwrap();

        let status = verify_message_signature(&armored, &message, None);
        assert!(status.valid);
        assert!(!status.author_match);
    }

    #[test]
    fn verify_rejects_tampered_message() {
        let identity = Identity::new(&mut OsRng);
        let author = crate::util::hex(identity.hash());
        let message = commit_message(&author);
        let armored = sign_message(&identity, &message).unwrap();

        let status = verify_message_signature(&armored, b"tampered", None);
        assert!(!status.valid);
    }

    #[test]
    fn extracts_multiline_gpgsig_from_commit_object() {
        let signed = b"tree abc\nauthor A <a> 0 +0000\n\nmsg\n";
        let commit = b"tree abc\ngpgsig -----BEGIN SSH SIGNATURE-----\n line1\n line2\n -----END SSH SIGNATURE-----\nauthor A <a> 0 +0000\n\nmsg\n";
        let sig = extract_signature_from_commit_object(commit).unwrap();
        assert!(sig.contains("BEGIN SSH SIGNATURE"));
        assert!(sig.contains("line1"));
        assert!(sig.contains("END SSH SIGNATURE"));
        assert_eq!(split_commit_signature(commit).1, signed);
    }
}