How to use a self signed certificate with the `mailin-embedded` crate?

Dear community,

using a self signed certificate with the mailin-embedded crate leads my testprogram to the following runtime error:

Error { original: Some(General("invalid private key")), msg: "unexpected error: invalid private key" }

Do you have an idea how to solve this? Or how to investigate the issue further?

The certificate was generated with the following command:

openssl req -newkey rsa:1024 -x509 -sha256 -days 3650 -nodes -out localmail.crt -keyout localmail.key

The code (a rough prototype to get familiar with these crates):

use std::thread;

use lettre::{
    transport::smtp::authentication::Credentials, AsyncSmtpTransport, AsyncStd1Executor,
    AsyncTransport, Message,
};
use mailin_embedded::{Handler, Server};

type AnyResult = Result<(), Box<dyn std::error::Error>>;

fn send_mail() -> AnyResult {
    let smtp_credentials = Credentials::new("user@example.com".into(), "password".into());

    let mailer = AsyncSmtpTransport::<AsyncStd1Executor>::relay("localhost")?
        .port(2525)
        // .tls(lettre::transport::smtp::client::Tls::None)
        // .credentials(smtp_credentials)
        .build();

    let email = Message::builder()
        .from("sender@example.com".parse()?)
        .reply_to("reply_dest@example.com".parse()?)
        .to("receipient@example.com".parse()?)
        .subject("this is a test mail")
        .body("a testmail to myself. hopefully this works ^^".to_string())?;

    async_std::task::block_on(mailer.send(email))?;

    Ok(())
}

#[derive(Clone)]
struct MyHandler {}
impl Handler for MyHandler {
    fn mail(
        &mut self,
        ip: std::net::IpAddr,
        domain: &str,
        from: &str,
    ) -> mailin_embedded::Response {
        dbg!(ip);
        dbg!(domain);
        dbg!(from);

        mailin_embedded::response::OK
    }
}

fn start_mail_server() {
    let handler = MyHandler {};
    let mut server = Server::new(handler);

    server
        .with_name("localhost")
        .with_ssl(mailin_embedded::SslConfig::SelfSigned {
            cert_path: "./cert/localmail.crt".into(),
            key_path: "./cert/localmail.key".into(),
        })
        .unwrap()
        .with_addr("localhost:2525")
        .unwrap();
    server.serve().unwrap();
}

fn main() -> AnyResult {
    thread::spawn(start_mail_server);
    send_mail()?;
    Ok(())
}

I found the mistake: the rsa key must have at least 2048 bits.

The self signed cert with this command works:

openssl req -newkey rsa:2048 -x509 -sha256 -days 3650 -nodes -out localmail.crt -keyout localmail.key

To let lettre accept our self signed cert I had to tell it to not verify the cert:

let mailer = AsyncSmtpTransport::<AsyncStd1Executor>::relay("localhost")?
        .port(2525)
        .tls(lettre::transport::smtp::client::Tls::Required(
            TlsParametersBuilder::new("localhost".into())
                .dangerous_accept_invalid_certs(true)
                .build()
                .unwrap(),
        ))
        .build();

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.