简体   繁体   中英

How do I create a connection with the equivalent of `-starttls smtp` using rust-openssl?

When I run the following command in the terminal

openssl s_client -connect gmail-smtp-in.l.google.com:25 -starttls smtp

I get a CONNECTED(00000005) response.

I am trying to achieve the same connection with Rust using rust-openssl (I am open to other libraries though), and here's what I have:

extern crate openssl;

use openssl::ssl::{SslConnector, SslMethod};

use std::net::TcpStream;

fn main() {
    let connector = SslConnector::builder(SslMethod::tls()).unwrap().build();
    let stream = TcpStream::connect("gmail-smtp-in.l.google.com:25").unwrap();
    let e = connector
        .connect("gmail-smtp-in.l.google.com", stream)
        .unwrap_err();
    eprintln!("{:#?}", e);
}

However, the response I get is:

Failure(
    MidHandshakeSslStream {
        stream: SslStream {
            stream: TcpStream {
                addr: V6(
                    [2001:41d0:a:2346::1]:57190
                ),
                peer: V6(
                    [2a00:1450:400c:c0c::1b]:25
                ),
                fd: 3
            },
            ssl: Ssl {
                state: "SSLv3/TLS write client hello",
                verify_result: X509VerifyResult {
                    code: 0,
                    error: "ok"
                }
            }
        },
        error: Error {
            code: ErrorCode(
                1
            ),
            cause: Some(
                Ssl(
                    ErrorStack(
                        [
                            Error {
                                code: 336130315,
                                library: "SSL routines",
                                function: "ssl3_get_record",
                                reason: "wrong version number",
                                file: "ssl/record/ssl3_record.c",
                                line: 252
                            }
                        ]
                    )
                )
            )
        }
    }
)

I'm guessing I'm missing the -starttls smtp part in the Rust code, but can't figure out how to add it.

Take a more detailed look in the openssl-sources on what -starttls smtp actually does, and replay that in Rust? It will just be a few library calls, but first you need to speak STMP to your server, and then switch to TLS. (Maybe that's already bundled up in the openssl-library in a single call.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM