uselesskey-pgp 0.9.0

OpenPGP key fixtures (armored and binary) for deterministic and random tests.
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
use std::fmt;
use std::sync::Arc;

use pgp::composed::{EncryptionCaps, KeyType, SecretKeyParamsBuilder, SignedPublicKey};
use pgp::ser::Serialize;
use pgp::types::KeyDetails;
use rand_chacha::ChaCha20Rng;
use rand_core::SeedableRng;
use uselesskey_core::negative::{
    CorruptPem, corrupt_der_deterministic, corrupt_pem, corrupt_pem_deterministic, truncate_der,
};
use uselesskey_core::sink::TempArtifact;
use uselesskey_core::{Error, Factory};

use crate::PgpSpec;

/// Cache domain for OpenPGP keypair fixtures.
///
/// Keep this stable: changing it changes deterministic outputs.
pub const DOMAIN_PGP_KEYPAIR: &str = "uselesskey:pgp:keypair";

#[derive(Clone)]
pub struct PgpKeyPair {
    factory: Factory,
    label: String,
    spec: PgpSpec,
    inner: Arc<Inner>,
}

struct Inner {
    user_id: String,
    fingerprint: String,
    private_binary: Arc<[u8]>,
    private_armor: String,
    public_binary: Arc<[u8]>,
    public_armor: String,
}

impl fmt::Debug for PgpKeyPair {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("PgpKeyPair")
            .field("label", &self.label)
            .field("spec", &self.spec)
            .field("fingerprint", &self.inner.fingerprint)
            .finish_non_exhaustive()
    }
}

/// Extension trait to hang OpenPGP helpers off the core [`Factory`].
pub trait PgpFactoryExt {
    fn pgp(&self, label: impl AsRef<str>, spec: PgpSpec) -> PgpKeyPair;
}

impl PgpFactoryExt for Factory {
    fn pgp(&self, label: impl AsRef<str>, spec: PgpSpec) -> PgpKeyPair {
        PgpKeyPair::new(self.clone(), label.as_ref(), spec)
    }
}

impl PgpKeyPair {
    fn new(factory: Factory, label: &str, spec: PgpSpec) -> Self {
        let inner = load_inner(&factory, label, spec, "good");
        Self {
            factory,
            label: label.to_string(),
            spec,
            inner,
        }
    }

    fn load_variant(&self, variant: &str) -> Arc<Inner> {
        load_inner(&self.factory, &self.label, self.spec, variant)
    }

    /// Returns the fixture spec.
    pub fn spec(&self) -> PgpSpec {
        self.spec
    }

    /// Returns the label used to create this keypair.
    ///
    /// # Examples
    ///
    /// ```
    /// # use uselesskey_core::Factory;
    /// # use uselesskey_pgp::{PgpFactoryExt, PgpSpec};
    /// let fx = Factory::random();
    /// let kp = fx.pgp("my-svc", PgpSpec::ed25519());
    /// assert_eq!(kp.label(), "my-svc");
    /// ```
    pub fn label(&self) -> &str {
        &self.label
    }

    /// Returns the generated OpenPGP user id.
    pub fn user_id(&self) -> &str {
        &self.inner.user_id
    }

    /// Returns the OpenPGP key fingerprint.
    pub fn fingerprint(&self) -> &str {
        &self.inner.fingerprint
    }

    /// Binary transferable secret key bytes.
    pub fn private_key_binary(&self) -> &[u8] {
        &self.inner.private_binary
    }

    /// Armored transferable secret key.
    pub fn private_key_armored(&self) -> &str {
        &self.inner.private_armor
    }

    /// Binary transferable public key bytes.
    pub fn public_key_binary(&self) -> &[u8] {
        &self.inner.public_binary
    }

    /// Armored transferable public key.
    pub fn public_key_armored(&self) -> &str {
        &self.inner.public_armor
    }

    /// Write the armored private key to a tempfile.
    pub fn write_private_key_armored(&self) -> Result<TempArtifact, Error> {
        TempArtifact::new_string("uselesskey-", ".pgp.priv.asc", self.private_key_armored())
    }

    /// Write the armored public key to a tempfile.
    pub fn write_public_key_armored(&self) -> Result<TempArtifact, Error> {
        TempArtifact::new_string("uselesskey-", ".pgp.pub.asc", self.public_key_armored())
    }

    /// Produce a corrupted armored private key variant.
    pub fn private_key_armored_corrupt(&self, how: CorruptPem) -> String {
        corrupt_pem(self.private_key_armored(), how)
    }

    /// Produce a deterministic corrupted armored private key using a variant string.
    pub fn private_key_armored_corrupt_deterministic(&self, variant: &str) -> String {
        corrupt_pem_deterministic(self.private_key_armored(), variant)
    }

    /// Produce a truncated private key binary variant.
    pub fn private_key_binary_truncated(&self, len: usize) -> Vec<u8> {
        truncate_der(self.private_key_binary(), len)
    }

    /// Produce a deterministic corrupted private key binary using a variant string.
    pub fn private_key_binary_corrupt_deterministic(&self, variant: &str) -> Vec<u8> {
        corrupt_der_deterministic(self.private_key_binary(), variant)
    }

    /// Return a valid (parseable) public key that does not match this private key.
    pub fn mismatched_public_key_binary(&self) -> Vec<u8> {
        let other = self.load_variant("mismatch");
        other.public_binary.as_ref().to_vec()
    }

    /// Return an armored public key that does not match this private key.
    pub fn mismatched_public_key_armored(&self) -> String {
        let other = self.load_variant("mismatch");
        other.public_armor.clone()
    }
}

fn load_inner(factory: &Factory, label: &str, spec: PgpSpec, variant: &str) -> Arc<Inner> {
    let spec_bytes = spec.stable_bytes();

    factory.get_or_init(DOMAIN_PGP_KEYPAIR, label, &spec_bytes, variant, |seed| {
        let mut rng = ChaCha20Rng::from_seed(*seed.bytes());
        let user_id = build_user_id(label);

        let mut key_params = SecretKeyParamsBuilder::default();
        key_params
            .key_type(spec_to_key_type(spec))
            .can_certify(true)
            .can_sign(true)
            .can_encrypt(EncryptionCaps::None)
            .primary_user_id(user_id.clone());

        let secret_key_params = key_params
            .build()
            .expect("failed to build OpenPGP secret key params");

        let secret_key = secret_key_params
            .generate(&mut rng)
            .expect("OpenPGP key generation failed");
        let public_key = SignedPublicKey::from(secret_key.clone());

        let mut private_binary = Vec::new();
        secret_key
            .to_writer(&mut private_binary)
            .expect("failed to encode OpenPGP private key bytes");

        let mut public_binary = Vec::new();
        public_key
            .to_writer(&mut public_binary)
            .expect("failed to encode OpenPGP public key bytes");

        let private_armor = secret_key
            .to_armored_string(None.into())
            .expect("failed to armor OpenPGP private key");
        let public_armor = public_key
            .to_armored_string(None.into())
            .expect("failed to armor OpenPGP public key");

        Inner {
            user_id,
            fingerprint: secret_key.fingerprint().to_string(),
            private_binary: Arc::from(private_binary),
            private_armor,
            public_binary: Arc::from(public_binary),
            public_armor,
        }
    })
}

fn spec_to_key_type(spec: PgpSpec) -> KeyType {
    match spec {
        PgpSpec::Rsa2048 => KeyType::Rsa(2048),
        PgpSpec::Rsa3072 => KeyType::Rsa(3072),
        PgpSpec::Ed25519 => KeyType::Ed25519,
    }
}

fn build_user_id(label: &str) -> String {
    let display = if label.trim().is_empty() {
        "fixture"
    } else {
        label.trim()
    };

    let mut local = String::new();
    for ch in display.chars() {
        if ch.is_ascii_alphanumeric() {
            local.push(ch.to_ascii_lowercase());
        } else if !local.ends_with('-') {
            local.push('-');
        }
    }

    let local = local.trim_matches('-');
    let local = if local.is_empty() { "fixture" } else { local };

    format!("{display} <{local}@uselesskey.test>")
}

#[cfg(test)]
mod tests {
    use std::io::Cursor;

    use pgp::composed::{Deserializable, SignedPublicKey, SignedSecretKey};
    use pgp::types::KeyDetails;
    use uselesskey_core::Seed;

    use super::*;

    #[test]
    fn deterministic_key_is_stable() {
        let fx = Factory::deterministic(Seed::from_env_value("pgp-det").unwrap());
        let a = fx.pgp("issuer", PgpSpec::ed25519());
        let b = fx.pgp("issuer", PgpSpec::ed25519());

        assert_eq!(a.private_key_armored(), b.private_key_armored());
        assert_eq!(a.public_key_armored(), b.public_key_armored());
        assert_eq!(a.fingerprint(), b.fingerprint());
    }

    #[test]
    fn random_mode_caches_per_identity() {
        let fx = Factory::random();
        let a = fx.pgp("issuer", PgpSpec::rsa_2048());
        let b = fx.pgp("issuer", PgpSpec::rsa_2048());

        assert_eq!(a.private_key_armored(), b.private_key_armored());
    }

    #[test]
    fn different_labels_produce_different_keys() {
        let fx = Factory::deterministic(Seed::from_env_value("pgp-label").unwrap());
        let a = fx.pgp("a", PgpSpec::rsa_3072());
        let b = fx.pgp("b", PgpSpec::rsa_3072());

        assert_ne!(a.private_key_binary(), b.private_key_binary());
        assert_ne!(a.fingerprint(), b.fingerprint());
    }

    #[test]
    fn armored_outputs_have_expected_headers() {
        let fx = Factory::random();
        let key = fx.pgp("issuer", PgpSpec::ed25519());

        assert!(
            key.private_key_armored()
                .contains("BEGIN PGP PRIVATE KEY BLOCK")
        );
        assert!(
            key.public_key_armored()
                .contains("BEGIN PGP PUBLIC KEY BLOCK")
        );
    }

    #[test]
    fn armored_outputs_parse_and_match_fingerprint() {
        let fx = Factory::random();
        let key = fx.pgp("parser", PgpSpec::ed25519());

        let (secret, _) =
            SignedSecretKey::from_armor_single(Cursor::new(key.private_key_armored()))
                .expect("parse armored private key");
        secret.verify_bindings().expect("verify private bindings");

        let (public, _) = SignedPublicKey::from_armor_single(Cursor::new(key.public_key_armored()))
            .expect("parse armored public key");
        public.verify_bindings().expect("verify public bindings");

        assert_eq!(secret.fingerprint().to_string(), key.fingerprint());
        assert_eq!(public.fingerprint().to_string(), key.fingerprint());
    }

    #[test]
    fn binary_outputs_parse() {
        let fx = Factory::random();
        let key = fx.pgp("binary", PgpSpec::rsa_2048());

        let secret = SignedSecretKey::from_bytes(Cursor::new(key.private_key_binary()))
            .expect("parse private key bytes");
        let public = SignedPublicKey::from_bytes(Cursor::new(key.public_key_binary()))
            .expect("parse public key bytes");

        assert_eq!(secret.fingerprint().to_string(), key.fingerprint());
        assert_eq!(public.fingerprint().to_string(), key.fingerprint());
    }

    #[test]
    fn mismatched_public_key_differs() {
        let fx = Factory::deterministic(Seed::from_env_value("pgp-mismatch").unwrap());
        let key = fx.pgp("issuer", PgpSpec::ed25519());

        let mismatch = key.mismatched_public_key_binary();
        assert_ne!(mismatch, key.public_key_binary());
    }

    #[test]
    fn user_id_is_exposed_and_sanitized() {
        let fx = Factory::deterministic(Seed::from_env_value("pgp-user-id").unwrap());
        let key = fx.pgp("Test User!@#", PgpSpec::ed25519());
        let blank = fx.pgp("   ", PgpSpec::ed25519());

        assert_eq!(key.user_id(), "Test User!@# <test-user@uselesskey.test>");
        assert_eq!(blank.user_id(), "fixture <fixture@uselesskey.test>");
    }

    #[test]
    fn armored_corruption_helpers_are_invalid_and_stable() {
        let fx = Factory::deterministic(Seed::from_env_value("pgp-corrupt-armor").unwrap());
        let key = fx.pgp("issuer", PgpSpec::ed25519());

        let bad = key.private_key_armored_corrupt(CorruptPem::BadBase64);
        assert_ne!(bad, key.private_key_armored());
        assert!(bad.contains("THIS_IS_NOT_BASE64!!!"));
        assert!(SignedSecretKey::from_armor_single(Cursor::new(&bad)).is_err());

        let det_a = key.private_key_armored_corrupt_deterministic("corrupt:v1");
        let det_b = key.private_key_armored_corrupt_deterministic("corrupt:v1");
        assert_eq!(det_a, det_b);
        assert_ne!(det_a, key.private_key_armored());
        assert!(det_a.starts_with('-'));
        assert!(SignedSecretKey::from_armor_single(Cursor::new(&det_a)).is_err());
    }

    #[test]
    fn binary_corruption_helpers_are_invalid_and_stable() {
        let fx = Factory::deterministic(Seed::from_env_value("pgp-corrupt-bin").unwrap());
        let key = fx.pgp("issuer", PgpSpec::ed25519());

        let truncated = key.private_key_binary_truncated(32);
        assert_eq!(truncated.len(), 32);
        assert!(SignedSecretKey::from_bytes(Cursor::new(&truncated)).is_err());

        let det_a = key.private_key_binary_corrupt_deterministic("corrupt:v1");
        let det_b = key.private_key_binary_corrupt_deterministic("corrupt:v1");
        assert_eq!(det_a, det_b);
        assert_ne!(det_a, key.private_key_binary());
        assert_eq!(det_a.len(), key.private_key_binary().len());
    }

    #[test]
    fn mismatched_public_key_variants_parse_and_fingerprint_differs() {
        let fx = Factory::deterministic(Seed::from_env_value("pgp-mismatch-parse").unwrap());
        let key = fx.pgp("issuer", PgpSpec::ed25519());

        let mismatch_bin = key.mismatched_public_key_binary();
        let mismatch_pub = SignedPublicKey::from_bytes(Cursor::new(&mismatch_bin))
            .expect("parse mismatched public binary");
        assert_ne!(mismatch_pub.fingerprint().to_string(), key.fingerprint());

        let mismatch_arm = key.mismatched_public_key_armored();
        assert_ne!(mismatch_arm, key.public_key_armored());
        let (mismatch_pub_arm, _) = SignedPublicKey::from_armor_single(Cursor::new(&mismatch_arm))
            .expect("parse mismatched public armor");
        assert_ne!(
            mismatch_pub_arm.fingerprint().to_string(),
            key.fingerprint()
        );
    }

    #[test]
    fn debug_does_not_leak_key_material() {
        let fx = Factory::random();
        let key = fx.pgp("debug", PgpSpec::ed25519());
        let dbg = format!("{key:?}");

        assert!(dbg.contains("PgpKeyPair"));
        assert!(dbg.contains("debug"));
        assert!(!dbg.contains("BEGIN PGP PRIVATE KEY BLOCK"));
    }
}