ssh-agent-lib 0.5.1

A collection of types for writing custom SSH agents
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
//! OpenPGP wrapper for SSH keys
//!
//! Creates an OpenPGP certificate based on the SSH key and allows signing files
//! emitting OpenPGP framed packets.
//!
//! Requires that the first key in SSH is ed25519 (see `ssh-add -L`).
//!
//! Generate a key with:
//! `cargo run --example pgp-wrapper generate "John Doe <john@example.com>" > key.pgp`
//!
//! Sign data using:
//! `cargo run --example pgp-wrapper sign < Cargo.toml > Cargo.toml.sig`
//!
//! Import the certificate using GnuPG:
//! ```sh
//! $ gpg --import key.pgp
//! gpg: key A142E92C91BE3AD5: public key "John Doe <john@example.com>" imported
//! gpg: Total number processed: 1
//! gpg:               imported: 1
//! ```
//!
//! Verify the signature using GnuPG:
//! ```sh
//! $ gpg --verify Cargo.toml.sig
//! gpg: assuming signed data in 'Cargo.toml'
//! gpg: Signature made Fri May 10 11:15:53 2024 CEST
//! gpg:                using EDDSA key 4EB27E153DDC454364B36B59A142E92C91BE3AD5
//! gpg: Good signature from "John Doe <john@example.com>" [unknown]
//! gpg: WARNING: This key is not certified with a trusted signature!
//! gpg:          There is no indication that the signature belongs to the owner.
//! Primary key fingerprint: 4EB2 7E15 3DDC 4543 64B3  6B59 A142 E92C 91BE 3AD5
//! ```
//!
//! Works perfectly in conjunction with `openpgp-card-agent.rs`!
//!
//! If the SSH agent implements `decrypt derive` extension this agent additionally
//! creates encryption capable subkey and supports the `decrypt` subcommand:
//!
//! ```sh
//! echo I like strawberries | gpg -er 4EB27E153DDC454364B36B59A142E92C91BE3AD5 > /tmp/encrypted.pgp
//! SSH_AUTH_SOCK=/tmp/ext-agent.sock cargo run --example pgp-wrapper -- decrypt < /tmp/encrypted.pgp
//! ...
//! I like strawberries
//! ```

use std::io::Write as _;

use chrono::DateTime;
use clap::Parser;
use pgp::{
    crypto::{ecc_curve::ECCCurve, hash::HashAlgorithm, public_key::PublicKeyAlgorithm},
    packet::{
        KeyFlags, PacketTrait, PublicKey, SignatureConfig, SignatureType, SignatureVersion,
        Subpacket, SubpacketData, UserId,
    },
    ser::Serialize,
    types::{
        CompressionAlgorithm, KeyTrait, KeyVersion, Mpi, PublicKeyTrait, PublicParams,
        SecretKeyTrait, Version,
    },
    Deserializable as _, Esk, KeyDetails, Message, PlainSessionKey, Signature,
};
use service_binding::Binding;
use ssh_agent_lib::{
    agent::Session,
    client::connect,
    proto::{Extension, SignRequest},
};
use ssh_key::public::KeyData;
use tokio::runtime::Runtime;
use tokio::sync::Mutex;
mod extensions;
use extensions::{
    DecryptDeriveRequest, DecryptDeriveResponse, DecryptIdentities, RequestDecryptIdentities,
};

struct WrappedKey {
    public_key: PublicKey,
    pubkey: KeyData,
    client: Mutex<Box<dyn Session>>,
}

#[derive(Clone, Copy, Debug)]
enum KeyRole {
    Signing,
    Decryption,
}

impl From<KeyRole> for PublicKeyAlgorithm {
    fn from(value: KeyRole) -> Self {
        match value {
            KeyRole::Signing => PublicKeyAlgorithm::EdDSA,
            KeyRole::Decryption => PublicKeyAlgorithm::ECDH,
        }
    }
}

fn ssh_to_pgp(pubkey: KeyData, key_role: KeyRole) -> PublicKey {
    let KeyData::Ed25519(key) = pubkey.clone() else {
        panic!("The first key was not ed25519!");
    };

    let mut key_bytes = key.0.to_vec();
    // Add prefix to mark that this MPI uses EdDSA point representation.
    // See https://datatracker.ietf.org/doc/draft-koch-eddsa-for-openpgp/
    key_bytes.insert(0, 0x40);

    let public_params = match key_role {
        KeyRole::Signing => PublicParams::EdDSA {
            curve: ECCCurve::Ed25519,
            q: key_bytes.into(),
        },
        // most common values taken from
        // https://gitlab.com/sequoia-pgp/sequoia/-/issues/838#note_909813463
        KeyRole::Decryption => PublicParams::ECDH {
            curve: ECCCurve::Curve25519,
            p: key_bytes.into(),
            hash: HashAlgorithm::SHA2_256,
            alg_sym: pgp::crypto::sym::SymmetricKeyAlgorithm::AES128,
        },
    };

    PublicKey::new(
        Version::New,
        KeyVersion::V4,
        key_role.into(),
        // use fixed date so that the fingerprint generation is deterministic
        DateTime::parse_from_rfc3339("2016-09-06T17:00:00+02:00")
            .expect("date to be valid")
            .into(),
        None,
        public_params,
    )
    .expect("key to be valid")
}

impl WrappedKey {
    fn new(pubkey: KeyData, client: Box<dyn Session>, key_role: KeyRole) -> Self {
        let public_key = ssh_to_pgp(pubkey.clone(), key_role);
        Self {
            pubkey,
            client: Mutex::new(client),
            public_key,
        }
    }

    fn decrypt(
        &self,
        mpis: &[Mpi],
    ) -> Result<(Vec<u8>, pgp::crypto::sym::SymmetricKeyAlgorithm), pgp::errors::Error> {
        if let PublicParams::ECDH {
            curve,
            alg_sym,
            hash,
            ..
        } = self.public_key().public_params()
        {
            let ciphertext = mpis[0].as_bytes();

            // encrypted and wrapped value derived from the session key
            let encrypted_session_key = mpis[2].as_bytes();

            let ciphertext = if *curve == ECCCurve::Curve25519 {
                assert_eq!(
                    ciphertext[0], 0x40,
                    "Unexpected shape of Cv25519 encrypted data"
                );

                // Strip trailing 0x40
                &ciphertext[1..]
            } else {
                unimplemented!();
            };

            let plaintext = Runtime::new()
                .expect("creating runtime to succeed")
                .handle()
                .block_on(async {
                    let mut client = self.client.lock().await;
                    let result = client.extension(
                        Extension::new_message(DecryptDeriveRequest {
                            pubkey: self.pubkey.clone(),
                            data: ciphertext.to_vec(),
                            flags: 0,
                        })
                        .expect("encoding to work"),
                    );
                    result.await
                })
                .expect("decryption to succeed")
                .expect("result not to be empty");

            let shared_secret = &plaintext
                .parse_message::<DecryptDeriveResponse>()
                .expect("decoding to succeed")
                .expect("not to be empty")
                .data[..];

            let encrypted_key_len: usize = mpis[1].first().copied().map(Into::into).unwrap_or(0);

            let decrypted_key: Vec<u8> = pgp::crypto::ecdh::derive_session_key(
                shared_secret,
                encrypted_session_key,
                encrypted_key_len,
                &(curve.clone(), *alg_sym, *hash),
                &self.public_key.fingerprint(),
            )?;

            // strip off the leading session key algorithm octet, and the two trailing checksum octets
            let dec_len = decrypted_key.len();
            let (sessionkey, checksum) = (
                &decrypted_key[1..dec_len - 2],
                &decrypted_key[dec_len - 2..],
            );

            // ... check the checksum, while we have it at hand
            pgp::crypto::checksum::simple(checksum, sessionkey)?;

            let session_key_algorithm = decrypted_key[0].into();
            Ok((sessionkey.to_vec(), session_key_algorithm))
        } else {
            unimplemented!();
        }
    }
}

impl std::fmt::Debug for WrappedKey {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "WrappedKey")
    }
}

impl KeyTrait for WrappedKey {
    fn fingerprint(&self) -> Vec<u8> {
        self.public_key.fingerprint()
    }

    fn key_id(&self) -> pgp::types::KeyId {
        self.public_key.key_id()
    }

    fn algorithm(&self) -> pgp::crypto::public_key::PublicKeyAlgorithm {
        self.public_key.algorithm()
    }
}

impl PublicKeyTrait for WrappedKey {
    fn verify_signature(
        &self,
        hash: pgp::crypto::hash::HashAlgorithm,
        data: &[u8],
        sig: &[pgp::types::Mpi],
    ) -> pgp::errors::Result<()> {
        self.public_key.verify_signature(hash, data, sig)
    }

    fn encrypt<R: rand::prelude::CryptoRng + rand::prelude::Rng>(
        &self,
        rng: &mut R,
        plain: &[u8],
    ) -> pgp::errors::Result<Vec<pgp::types::Mpi>> {
        self.public_key.encrypt(rng, plain)
    }

    fn to_writer_old(&self, writer: &mut impl std::io::Write) -> pgp::errors::Result<()> {
        self.public_key.to_writer_old(writer)
    }
}

impl SecretKeyTrait for WrappedKey {
    type PublicKey = PublicKey;

    type Unlocked = Self;

    fn unlock<F, G, T>(&self, _pw: F, work: G) -> pgp::errors::Result<T>
    where
        F: FnOnce() -> String,
        G: FnOnce(&Self::Unlocked) -> pgp::errors::Result<T>,
    {
        work(self)
    }

    fn create_signature<F>(
        &self,
        _key_pw: F,
        _hash: pgp::crypto::hash::HashAlgorithm,
        data: &[u8],
    ) -> pgp::errors::Result<Vec<pgp::types::Mpi>>
    where
        F: FnOnce() -> String,
    {
        let signature = Runtime::new()
            .expect("creating runtime to succeed")
            .handle()
            .block_on(async {
                let mut client = self.client.lock().await;
                let result = client.sign(SignRequest {
                    pubkey: self.pubkey.clone(),
                    data: data.to_vec(),
                    flags: 0,
                });
                result.await
            })
            .expect("signing to succeed");

        let sig = &signature.as_bytes();

        assert_eq!(sig.len(), 64);

        Ok(vec![
            Mpi::from_raw_slice(&sig[..32]),
            Mpi::from_raw_slice(&sig[32..]),
        ])
    }

    fn public_key(&self) -> Self::PublicKey {
        self.public_key.clone()
    }

    fn public_params(&self) -> &pgp::types::PublicParams {
        self.public_key.public_params()
    }
}

#[derive(Debug, Parser)]
enum Args {
    Generate { userid: String },
    Sign,
    Decrypt,
}

fn main() -> testresult::TestResult {
    let args = Args::parse();

    let rt = Runtime::new()?;

    let (client, identities, decrypt_ids) = rt.block_on(async move {
        #[cfg(unix)]
        let mut client =
            connect(Binding::FilePath(std::env::var("SSH_AUTH_SOCK")?.into()).try_into()?)?;

        #[cfg(windows)]
        let mut client =
            connect(Binding::NamedPipe(std::env::var("SSH_AUTH_SOCK")?.into()).try_into()?)?;

        let identities = client.request_identities().await?;

        if identities.is_empty() {
            panic!("We need at least one ed25519 identity!");
        }

        let decrypt_ids = if let Ok(Some(identities)) = client
            .extension(Extension::new_message(RequestDecryptIdentities)?)
            .await
        {
            identities
                .parse_message::<DecryptIdentities>()?
                .map(|d| d.identities)
                .unwrap_or_default()
        } else {
            vec![]
        };

        Ok::<_, testresult::TestError>((client, identities, decrypt_ids))
    })?;

    let pubkey = &identities[0].pubkey;

    match args {
        Args::Generate { userid } => {
            let subkeys = if let Some(decryption_id) = decrypt_ids.first() {
                let mut keyflags = KeyFlags::default();
                keyflags.set_encrypt_comms(true);
                keyflags.set_encrypt_storage(true);
                let pk = ssh_to_pgp(decryption_id.pubkey.clone(), KeyRole::Decryption);
                vec![pgp::PublicSubkey::new(
                    pgp::packet::PublicSubkey::new(
                        pk.packet_version(),
                        pk.version(),
                        pk.algorithm(),
                        *pk.created_at(),
                        pk.expiration(),
                        pk.public_params().clone(),
                    )?,
                    keyflags,
                )]
            } else {
                vec![]
            };

            let signer = WrappedKey::new(pubkey.clone(), client, KeyRole::Signing);
            let mut keyflags = KeyFlags::default();
            keyflags.set_sign(true);
            keyflags.set_certify(true);

            let composed_pk = pgp::PublicKey::new(
                signer.public_key(),
                KeyDetails::new(
                    UserId::from_str(Default::default(), &userid),
                    vec![],
                    vec![],
                    keyflags,
                    Default::default(),
                    Default::default(),
                    vec![CompressionAlgorithm::Uncompressed].into(),
                    None,
                ),
                subkeys,
            );
            let signed_pk = composed_pk.sign(&signer, String::new)?;
            signed_pk.to_writer(&mut std::io::stdout())?;
        }
        Args::Sign => {
            let signer = WrappedKey::new(pubkey.clone(), client, KeyRole::Signing);
            let signature = SignatureConfig::new_v4(
                SignatureVersion::V4,
                SignatureType::Binary,
                signer.algorithm(),
                HashAlgorithm::SHA2_256,
                vec![
                    Subpacket::regular(SubpacketData::SignatureCreationTime(
                        std::time::SystemTime::now().into(),
                    )),
                    Subpacket::regular(SubpacketData::Issuer(signer.key_id())),
                    Subpacket::regular(SubpacketData::IssuerFingerprint(
                        KeyVersion::V4,
                        signer.fingerprint().into(),
                    )),
                ],
                vec![],
            );

            let mut hasher = signature.hash_alg.new_hasher()?;

            signature.hash_data_to_sign(&mut *hasher, std::io::stdin())?;
            let len = signature.hash_signature_data(&mut *hasher)?;
            hasher.update(&signature.trailer(len)?);

            let hash = &hasher.finish()[..];

            let signed_hash_value = [hash[0], hash[1]];
            let raw_sig = signer.create_signature(String::new, HashAlgorithm::SHA2_256, hash)?;

            let signature = Signature::from_config(signature, signed_hash_value, raw_sig);
            pgp::packet::write_packet(&mut std::io::stdout(), &signature)?;
        }
        Args::Decrypt => {
            let decryptor =
                WrappedKey::new(decrypt_ids[0].pubkey.clone(), client, KeyRole::Decryption);
            let message = Message::from_bytes(std::io::stdin())?;

            let Message::Encrypted { esk, edata } = message else {
                panic!("not encrypted");
            };

            let mpis = if let Esk::PublicKeyEncryptedSessionKey(ref k) = esk[0] {
                k.mpis()
            } else {
                panic!("whoops")
            };

            let (session_key, session_key_algorithm) =
                decryptor.unlock(String::new, |priv_key| priv_key.decrypt(mpis))?;

            let plain_session_key = PlainSessionKey::V4 {
                key: session_key,
                sym_alg: session_key_algorithm,
            };

            let decrypted = edata.decrypt(plain_session_key)?;

            if let Message::Literal(data) = decrypted {
                std::io::stdout().write_all(data.data())?;
            } else {
                eprintln!("decrypted: {:?}", &decrypted);
            }
        }
    }

    Ok(())
}