wecanencrypt 0.9.0

Simple Rust OpenPGP library for encryption, signing, and key management.
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
//! SSH public key conversion and signing functions.
//!
//! This module provides functions for converting OpenPGP authentication
//! keys to SSH public key format (RFC 4253) and for performing raw
//! cryptographic signatures suitable for SSH authentication.

use std::time::SystemTime;

use pgp::types::{KeyDetails, Password, PlainSecretParams, PublicParams};
use zeroize::Zeroizing;

use crate::error::{Error, Result};
use crate::internal::{is_key_expired, is_subkey_valid, parse_public_key, parse_secret_key};
use crate::types::{RsaPublicKey, SigningPublicKey};

/// Convert a certificate's authentication key to SSH public key format.
///
/// # Arguments
/// * `cert_data` - The certificate data
/// * `comment` - Optional comment to append (e.g., email address)
///
/// # Returns
/// SSH public key string (e.g., "ssh-rsa AAAA... comment").
///
/// # Example
/// ```ignore
/// // Ignored: illustrative example with placeholder file path
/// let cert = std::fs::read("key.asc")?;
/// let ssh_key = get_ssh_pubkey(&cert, Some("user@example.com"))?;
/// println!("{}", ssh_key);
/// ```
pub fn get_ssh_pubkey(cert_data: &[u8], comment: Option<&str>) -> Result<String> {
    let public_key = parse_public_key(cert_data)?;

    // Find an authentication-capable subkey
    let auth_subkey = public_key.public_subkeys.iter().find(|sk| {
        // Check if valid and has authentication flag
        if !is_subkey_valid(sk, false) {
            return false;
        }
        sk.signatures
            .iter()
            .any(|sig| sig.key_flags().authentication())
    });

    let params = match auth_subkey {
        Some(sk) => sk.key.public_params(),
        None => {
            // Fall back to primary key if it can be used for authentication
            return Err(Error::NoAuthenticationSubkey);
        }
    };

    // Convert public parameters to SSH format
    let (key_type, key_blob) = convert_params_to_ssh(params)?;

    // Format as SSH public key line (with trailing newline as per convention)
    let key_data = base64_encode(&key_blob);
    let ssh_line = match comment {
        Some(c) => format!("{} {} {}\n", key_type, key_data, c),
        None => format!("{} {}\n", key_type, key_data),
    };

    Ok(ssh_line)
}

/// Convert public key parameters to SSH wire format.
fn convert_params_to_ssh(params: &PublicParams) -> Result<(String, Vec<u8>)> {
    match params {
        PublicParams::RSA(rsa_params) => {
            use rsa::traits::PublicKeyParts;

            let e = rsa_params.key.e().to_bytes_be();
            let n = rsa_params.key.n().to_bytes_be();

            let mut blob = Vec::new();
            // SSH format: string "ssh-rsa" + mpint e + mpint n
            write_ssh_string(&mut blob, b"ssh-rsa");
            write_ssh_mpint(&mut blob, &e);
            write_ssh_mpint(&mut blob, &n);

            Ok(("ssh-rsa".to_string(), blob))
        }
        // RFC 9580 Ed25519 (v6 keys)
        PublicParams::Ed25519(ed_params) => {
            let key_bytes = ed_params.key.as_bytes();
            let mut blob = Vec::new();
            // SSH format: string "ssh-ed25519" + string key_data
            write_ssh_string(&mut blob, b"ssh-ed25519");
            write_ssh_string(&mut blob, key_bytes);

            Ok(("ssh-ed25519".to_string(), blob))
        }
        // Legacy EdDSA (v4 keys)
        PublicParams::EdDSALegacy(ed_params) => {
            use pgp::types::EddsaLegacyPublicParams;

            match ed_params {
                EddsaLegacyPublicParams::Ed25519 { key } => {
                    let key_bytes = key.as_bytes();
                    let mut blob = Vec::new();
                    write_ssh_string(&mut blob, b"ssh-ed25519");
                    write_ssh_string(&mut blob, key_bytes);

                    Ok(("ssh-ed25519".to_string(), blob))
                }
                _ => Err(Error::UnsupportedAlgorithm(
                    "Unsupported legacy EdDSA curve for SSH".to_string(),
                )),
            }
        }
        PublicParams::Ed448(_) => {
            // Ed448 is not commonly supported in SSH
            Err(Error::UnsupportedAlgorithm(
                "Ed448 SSH conversion not supported".to_string(),
            ))
        }
        PublicParams::ECDSA(ecdsa_params) => {
            use pgp::types::EcdsaPublicParams;

            match ecdsa_params {
                EcdsaPublicParams::P256 { key } => {
                    use p256::elliptic_curve::sec1::ToEncodedPoint;
                    let mut blob = Vec::new();
                    let curve_name = b"nistp256";
                    let key_type = "ecdsa-sha2-nistp256";

                    write_ssh_string(&mut blob, key_type.as_bytes());
                    write_ssh_string(&mut blob, curve_name);
                    // The key is the uncompressed point (0x04 || x || y)
                    let point = key.to_encoded_point(false);
                    write_ssh_string(&mut blob, point.as_bytes());

                    Ok((key_type.to_string(), blob))
                }
                EcdsaPublicParams::P384 { key } => {
                    use p384::elliptic_curve::sec1::ToEncodedPoint;
                    let mut blob = Vec::new();
                    let curve_name = b"nistp384";
                    let key_type = "ecdsa-sha2-nistp384";

                    write_ssh_string(&mut blob, key_type.as_bytes());
                    write_ssh_string(&mut blob, curve_name);
                    let point = key.to_encoded_point(false);
                    write_ssh_string(&mut blob, point.as_bytes());

                    Ok((key_type.to_string(), blob))
                }
                EcdsaPublicParams::P521 { key } => {
                    use p521::elliptic_curve::sec1::ToEncodedPoint;
                    let mut blob = Vec::new();
                    let curve_name = b"nistp521";
                    let key_type = "ecdsa-sha2-nistp521";

                    write_ssh_string(&mut blob, key_type.as_bytes());
                    write_ssh_string(&mut blob, curve_name);
                    let point = key.to_encoded_point(false);
                    write_ssh_string(&mut blob, point.as_bytes());

                    Ok((key_type.to_string(), blob))
                }
                _ => Err(Error::UnsupportedAlgorithm(
                    "Unsupported ECDSA curve for SSH".to_string(),
                )),
            }
        }
        PublicParams::ECDH(ecdh_params) => {
            use pgp::types::EcdhPublicParams;

            // ECDH keys with Curve25519 can be converted for SSH authentication
            match ecdh_params {
                EcdhPublicParams::Curve25519 { .. } => {
                    // Note: X25519 is typically used for key exchange, not authentication
                    Err(Error::UnsupportedAlgorithm(
                        "X25519 is for key exchange, not authentication".to_string(),
                    ))
                }
                _ => Err(Error::UnsupportedAlgorithm(
                    "ECDH keys cannot be used for SSH authentication".to_string(),
                )),
            }
        }
        _ => Err(Error::UnsupportedAlgorithm(
            "SSH conversion not supported for this key type".to_string(),
        )),
    }
}

/// Write a string in SSH wire format (4-byte big-endian length + data).
fn write_ssh_string(buf: &mut Vec<u8>, data: &[u8]) {
    let len = data.len() as u32;
    buf.extend_from_slice(&len.to_be_bytes());
    buf.extend_from_slice(data);
}

/// Write an mpint in SSH wire format.
/// SSH mpints are big-endian, with a leading zero byte if the high bit is set.
fn write_ssh_mpint(buf: &mut Vec<u8>, data: &[u8]) {
    // Skip leading zeros
    let data = data
        .iter()
        .skip_while(|&&b| b == 0)
        .copied()
        .collect::<Vec<_>>();

    if data.is_empty() {
        // Zero value
        buf.extend_from_slice(&[0, 0, 0, 0]);
        return;
    }

    // Check if we need a leading zero (high bit set)
    let needs_padding = data[0] & 0x80 != 0;
    let len = data.len() + if needs_padding { 1 } else { 0 };

    buf.extend_from_slice(&(len as u32).to_be_bytes());
    if needs_padding {
        buf.push(0);
    }
    buf.extend_from_slice(&data);
}

/// Get signing public key components for external verification.
///
/// # Arguments
/// * `cert_data` - The certificate data
///
/// # Returns
/// Public key components in algorithm-specific format.
pub fn get_signing_pubkey(cert_data: &[u8]) -> Result<SigningPublicKey> {
    let public_key = parse_public_key(cert_data)?;

    // Find a signing-capable subkey
    let sign_subkey = public_key.public_subkeys.iter().find(|sk| {
        if !is_subkey_valid(sk, false) {
            return false;
        }
        sk.signatures.iter().any(|sig| sig.key_flags().sign())
    });

    // Check if primary can sign
    let primary_can_sign = public_key
        .details
        .users
        .iter()
        .any(|user| user.signatures.iter().any(|sig| sig.key_flags().sign()));

    // Get the public params from the appropriate key
    let params = if let Some(sk) = sign_subkey {
        sk.key.public_params()
    } else if primary_can_sign {
        public_key.primary_key.public_params()
    } else {
        return Err(Error::NoSigningSubkey);
    };

    // Extract actual key material
    match params {
        PublicParams::RSA(rsa_params) => {
            use rsa::traits::PublicKeyParts;

            let n = hex::encode_upper(rsa_params.key.n().to_bytes_be());
            let e = hex::encode_upper(rsa_params.key.e().to_bytes_be());

            Ok(SigningPublicKey::Rsa(RsaPublicKey { n, e }))
        }
        // RFC 9580 Ed25519 (v6 keys)
        PublicParams::Ed25519(ed_params) => {
            let public = hex::encode_upper(ed_params.key.as_bytes());
            Ok(SigningPublicKey::Ed25519 { public })
        }
        // Legacy EdDSA (v4 keys)
        PublicParams::EdDSALegacy(ed_params) => {
            use pgp::types::EddsaLegacyPublicParams;

            match ed_params {
                EddsaLegacyPublicParams::Ed25519 { key } => {
                    let public = hex::encode_upper(key.as_bytes());
                    Ok(SigningPublicKey::Ed25519 { public })
                }
                _ => Err(Error::UnsupportedAlgorithm(
                    "Unsupported legacy EdDSA variant".to_string(),
                )),
            }
        }
        PublicParams::ECDSA(ecdsa_params) => {
            use pgp::types::EcdsaPublicParams;

            let (curve, point) = match ecdsa_params {
                EcdsaPublicParams::P256 { key } => {
                    use p256::elliptic_curve::sec1::ToEncodedPoint;
                    let encoded = key.to_encoded_point(false);
                    ("P-256".to_string(), hex::encode_upper(encoded.as_bytes()))
                }
                EcdsaPublicParams::P384 { key } => {
                    use p384::elliptic_curve::sec1::ToEncodedPoint;
                    let encoded = key.to_encoded_point(false);
                    ("P-384".to_string(), hex::encode_upper(encoded.as_bytes()))
                }
                EcdsaPublicParams::P521 { key } => {
                    use p521::elliptic_curve::sec1::ToEncodedPoint;
                    let encoded = key.to_encoded_point(false);
                    ("P-521".to_string(), hex::encode_upper(encoded.as_bytes()))
                }
                _ => {
                    return Err(Error::UnsupportedAlgorithm(
                        "Unsupported ECDSA curve".to_string(),
                    ))
                }
            };

            Ok(SigningPublicKey::Ecdsa { curve, point })
        }
        _ => Err(Error::UnsupportedAlgorithm(
            "Signing key extraction not supported for this key type".to_string(),
        )),
    }
}

/// Simple base64 encoding (standard alphabet, no padding removal needed for SSH).
fn base64_encode(data: &[u8]) -> String {
    const ALPHABET: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

    let mut result = String::new();
    let mut i = 0;

    while i < data.len() {
        let b0 = data[i];
        let b1 = if i + 1 < data.len() { data[i + 1] } else { 0 };
        let b2 = if i + 2 < data.len() { data[i + 2] } else { 0 };

        let c0 = (b0 >> 2) as usize;
        let c1 = (((b0 & 0x03) << 4) | (b1 >> 4)) as usize;
        let c2 = (((b1 & 0x0f) << 2) | (b2 >> 6)) as usize;
        let c3 = (b2 & 0x3f) as usize;

        result.push(ALPHABET[c0] as char);
        result.push(ALPHABET[c1] as char);

        if i + 1 < data.len() {
            result.push(ALPHABET[c2] as char);
        } else {
            result.push('=');
        }

        if i + 2 < data.len() {
            result.push(ALPHABET[c3] as char);
        } else {
            result.push('=');
        }

        i += 3;
    }

    result
}

/// The algorithm and raw signature bytes from an SSH signing operation.
#[derive(Debug)]
pub enum SshSignResult {
    /// Ed25519 signature (64 bytes).
    Ed25519(Vec<u8>),
    /// ECDSA signature with named curve. Contains raw (r, s) scalars.
    Ecdsa {
        curve: String,
        r: Vec<u8>,
        s: Vec<u8>,
    },
    /// RSA signature bytes.
    Rsa(Vec<u8>),
}

/// Hash algorithm for SSH signing operations.
///
/// This tells `ssh_sign_raw` which PKCS#1v15 hash to use for RSA signing.
/// For Ed25519 and ECDSA the hash is determined by the key type, so this
/// value is only consulted for RSA.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SshHashAlgorithm {
    Sha256,
    Sha512,
}

/// Perform a raw SSH signature using a software authentication subkey.
///
/// This unlocks the secret key's authentication subkey and produces a raw
/// cryptographic signature suitable for the SSH agent protocol (not an
/// OpenPGP signature).
///
/// # Arguments
/// * `secret_cert` - The secret certificate data (armored or binary)
/// * `data` - The data to sign (for Ed25519: the raw message; for ECDSA/RSA: the pre-hashed digest)
/// * `password` - Password to unlock the secret key
/// * `hash_alg` - Hash algorithm hint (used for RSA PKCS#1v15 signing)
///
/// # Returns
/// An `SshSignResult` containing the algorithm-specific signature.
pub fn ssh_sign_raw(
    secret_cert: &[u8],
    data: &[u8],
    password: &str,
    hash_alg: SshHashAlgorithm,
) -> Result<SshSignResult> {
    let secret_key = parse_secret_key(secret_cert)?;
    let pw: Password = password.into();

    // Find the authentication subkey
    for subkey in &secret_key.secret_subkeys {
        let is_auth = subkey
            .signatures
            .iter()
            .any(|sig| sig.key_flags().authentication());

        if !is_auth {
            continue;
        }

        // Check the subkey isn't revoked
        let is_revoked = subkey
            .signatures
            .iter()
            .any(|sig| sig.typ() == Some(pgp::packet::SignatureType::SubkeyRevocation));
        if is_revoked {
            continue;
        }

        // Check the subkey isn't expired
        if let Some(sig) = subkey.signatures.last() {
            if let Some(validity) = sig.key_expiration_time() {
                let creation_time: SystemTime = subkey.key.created_at().into();
                if is_key_expired(creation_time, Some(validity.as_secs() as u64)) {
                    continue;
                }
            }
        }

        // Unlock the subkey and perform raw signing
        let result = subkey.key.unlock(&pw, |pub_params, secret_params| {
            ssh_raw_sign_with_params(pub_params, secret_params, data, hash_alg).map_err(|e| {
                pgp::errors::Error::Message {
                    message: e.to_string(),
                    backtrace: Some(std::backtrace::Backtrace::capture()),
                }
            })
        });

        match result {
            Ok(Ok(sig)) => return Ok(sig),
            Ok(Err(e)) => return Err(Error::Crypto(e.to_string())),
            Err(e) => return Err(Error::Crypto(e.to_string())),
        }
    }

    Err(Error::NoAuthenticationSubkey)
}

/// Perform a raw cryptographic signature using unlocked key parameters.
fn ssh_raw_sign_with_params(
    pub_params: &PublicParams,
    secret_params: &PlainSecretParams,
    data: &[u8],
    hash_alg: SshHashAlgorithm,
) -> Result<SshSignResult> {
    match secret_params {
        // Ed25519 (RFC 9580) or legacy EdDSA
        PlainSecretParams::Ed25519(sk) | PlainSecretParams::Ed25519Legacy(sk) => {
            let key_bytes = sk.as_bytes();
            let signing_key = pgp::crypto::ed25519::SecretKey::try_from_bytes(
                *key_bytes,
                pgp::crypto::ed25519::Mode::Ed25519,
            )
            .map_err(|e| Error::Crypto(e.to_string()))?;
            use std::ops::Deref;
            let dalek_key: &ed25519_dalek::SigningKey = signing_key.deref();
            use ed25519_dalek::Signer;
            let signature = dalek_key.sign(data);
            Ok(SshSignResult::Ed25519(signature.to_bytes().to_vec()))
        }

        // ECDSA - determine curve from pub_params, not scalar length
        PlainSecretParams::ECDSA(ecdsa_sk) => {
            use pgp::types::EcdsaPublicParams;

            let scalar_bytes = Zeroizing::new(ecdsa_sk.to_bytes());

            match pub_params {
                PublicParams::ECDSA(EcdsaPublicParams::P256 { .. }) => {
                    use p256::ecdsa::{signature::hazmat::PrehashSigner, Signature, SigningKey};
                    let signing_key =
                        SigningKey::from_bytes(p256::FieldBytes::from_slice(&scalar_bytes))
                            .map_err(|e| Error::Crypto(e.to_string()))?;
                    let sig: Signature = signing_key
                        .sign_prehash(data)
                        .map_err(|e| Error::Crypto(e.to_string()))?;
                    let (r, s) = sig.split_bytes();
                    Ok(SshSignResult::Ecdsa {
                        curve: "nistp256".to_string(),
                        r: r.to_vec(),
                        s: s.to_vec(),
                    })
                }
                PublicParams::ECDSA(EcdsaPublicParams::P384 { .. }) => {
                    use p384::ecdsa::{signature::hazmat::PrehashSigner, Signature, SigningKey};
                    let signing_key =
                        SigningKey::from_bytes(p384::FieldBytes::from_slice(&scalar_bytes))
                            .map_err(|e| Error::Crypto(e.to_string()))?;
                    let sig: Signature = signing_key
                        .sign_prehash(data)
                        .map_err(|e| Error::Crypto(e.to_string()))?;
                    let (r, s) = sig.split_bytes();
                    Ok(SshSignResult::Ecdsa {
                        curve: "nistp384".to_string(),
                        r: r.to_vec(),
                        s: s.to_vec(),
                    })
                }
                PublicParams::ECDSA(EcdsaPublicParams::P521 { .. }) => {
                    use p521::ecdsa::{signature::hazmat::PrehashSigner, Signature, SigningKey};
                    let signing_key =
                        SigningKey::from_bytes(p521::FieldBytes::from_slice(&scalar_bytes))
                            .map_err(|e| Error::Crypto(e.to_string()))?;
                    let sig: Signature = signing_key
                        .sign_prehash(data)
                        .map_err(|e| Error::Crypto(e.to_string()))?;
                    let (r, s) = sig.split_bytes();
                    Ok(SshSignResult::Ecdsa {
                        curve: "nistp521".to_string(),
                        r: r.to_vec(),
                        s: s.to_vec(),
                    })
                }
                _ => Err(Error::UnsupportedAlgorithm(
                    "Unsupported ECDSA curve for SSH signing (only P-256, P-384, P-521)"
                        .to_string(),
                )),
            }
        }

        // RSA - reconstruct the RsaPrivateKey from components and sign with PKCS#1v15
        PlainSecretParams::RSA(rsa_sk) => {
            use rsa::pkcs1v15::SigningKey as RsaSigningKey;
            use rsa::signature::{hazmat::PrehashSigner, SignatureEncoding};

            // Get d, p, q, u from the secret key (all zeroized on drop)
            let (d_bytes, p_bytes, q_bytes, u_bytes) = rsa_sk.to_bytes();
            let d_bytes = Zeroizing::new(d_bytes);
            let p_bytes = Zeroizing::new(p_bytes);
            let q_bytes = Zeroizing::new(q_bytes);
            let _u_bytes = Zeroizing::new(u_bytes);

            // Get n, e from the public params
            let (n, e) = match pub_params {
                PublicParams::RSA(rsa_pub) => {
                    use rsa::traits::PublicKeyParts;
                    (rsa_pub.key.n().clone(), rsa_pub.key.e().clone())
                }
                _ => {
                    return Err(Error::Crypto(
                        "RSA secret key with non-RSA public params".to_string(),
                    ));
                }
            };

            let private_key = rsa::RsaPrivateKey::from_components(
                n,
                e,
                rsa::BigUint::from_bytes_be(&d_bytes),
                vec![
                    rsa::BigUint::from_bytes_be(&p_bytes),
                    rsa::BigUint::from_bytes_be(&q_bytes),
                ],
            )
            .map_err(|e| Error::Crypto(format!("Failed to reconstruct RSA key: {}", e)))?;

            // Use the explicitly provided hash algorithm
            let sig_bytes = match hash_alg {
                SshHashAlgorithm::Sha256 => {
                    let signer = RsaSigningKey::<sha2::Sha256>::new(private_key);
                    signer
                        .sign_prehash(data)
                        .map_err(|e| Error::Crypto(format!("RSA signing failed: {}", e)))?
                        .to_vec()
                }
                SshHashAlgorithm::Sha512 => {
                    let signer = RsaSigningKey::<sha2::Sha512>::new(private_key);
                    signer
                        .sign_prehash(data)
                        .map_err(|e| Error::Crypto(format!("RSA signing failed: {}", e)))?
                        .to_vec()
                }
            };

            Ok(SshSignResult::Rsa(sig_bytes))
        }

        _ => Err(Error::UnsupportedAlgorithm(
            "Unsupported key type for SSH signing".to_string(),
        )),
    }
}

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

    #[test]
    fn test_base64_encode() {
        assert_eq!(base64_encode(b""), "");
        assert_eq!(base64_encode(b"f"), "Zg==");
        assert_eq!(base64_encode(b"fo"), "Zm8=");
        assert_eq!(base64_encode(b"foo"), "Zm9v");
        assert_eq!(base64_encode(b"foobar"), "Zm9vYmFy");
    }
}