File: import.rs

package info (click to toggle)
rust-openpgp-card-rpgp 0.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 392 kB
  • sloc: sh: 6; makefile: 2
file content (104 lines) | stat: -rw-r--r-- 2,964 bytes parent folder | download
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
// SPDX-FileCopyrightText: Heiko Schaefer <heiko@schaefer.name>
// SPDX-License-Identifier: MIT OR Apache-2.0

#![allow(dead_code)]

use std::env;

use anyhow::Result;
use openpgp_card::state::Open;
use openpgp_card::Card;
use pgp::{Deserializable, SignedPublicKey, SignedSecretKey};

use crate::cards::TestConfig;
use crate::util::{
    run_test, test_decrypt, test_reset, test_set_login_data, test_set_user_data, test_sign,
    test_upload_keys, TestError,
};

mod cards;
mod util;

#[ignore]
#[test]
fn import() -> Result<()> {
    env_logger::init();

    let config = match env::var("TEST_CONFIG") {
        Ok(path) => TestConfig::load(&path)?,
        Err(_) => TestConfig::load("config/test-cards.toml")?, // fallback
    };

    let cards = config.into_cardapps();

    for card in cards {
        println!("** Run tests on card '{}' **", card.get_name());

        let mut c: Card<Open> = card.get_card()?;
        println!(" -> Card opened");
        let mut tx = c.transaction()?;
        println!("    started transaction");

        println!("Reset");
        let _ = run_test(&mut tx, test_reset, &[])?;

        print!("Set user data");
        let userdata_out = run_test(&mut tx, test_set_user_data, &[])?;
        println!(" {userdata_out:x?}");

        print!("Set login data");
        let login_data_out = run_test(&mut tx, test_set_login_data, &[])?;
        println!(" {login_data_out:x?}");

        let key_files = {
            let config = card.get_config();
            if let Some(import) = &config.import {
                import.clone()
            } else {
                vec![]
            }
        };

        for key_file in &key_files {
            // upload keys
            print!("Upload key '{key_file}'");
            let upload_res = run_test(&mut tx, test_upload_keys, &[key_file]);

            if let Err(TestError::KeyUploadError(_file, err)) = &upload_res {
                // The card doesn't support this key type, so skip to the
                // next key - don't try to decrypt/sign for this key.

                println!(" => Upload failed ({err:?}), skip tests");

                continue;
            }

            let upload_out = upload_res?;
            println!(" {upload_out:x?}");

            let key = std::fs::read_to_string(key_file).expect("Unable to read ciphertext");

            // decrypt
            print!("  Decrypt");

            let (ssk, _) = SignedSecretKey::from_string(&key)?;
            let spk: SignedPublicKey = ssk.into();
            let ciphertext = util::encrypt_to("Hello world!\n", &spk)?;

            let dec_out = run_test(&mut tx, test_decrypt, &[&key, &ciphertext])?;
            println!(" {dec_out:x?}");

            // sign
            print!("  Sign");

            let sign_out = run_test(&mut tx, test_sign, &[&key])?;
            println!(" {sign_out:x?}");
        }

        // FIXME: import key with password

        println!();
    }

    Ok(())
}