简体   繁体   English

如何使用rust-openssl创建与`-starttls smtp`等效的连接?

[英]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. 我得到一个CONNECTED(00000005)响应。

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: 我正在尝试使用rust-openssl与Rust进行相同的连接(尽管我对其他库也开放),这就是我所拥有的:

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. 我猜我在Rust代码中缺少-starttls smtp部分,但无法弄清楚如何添加它。

Take a more detailed look in the openssl-sources on what -starttls smtp actually does, and replay that in Rust? 在openssl-sources中更详细地了解-starttls smtp实际功能,然后在Rust中重播该内容。 It will just be a few library calls, but first you need to speak STMP to your server, and then switch to TLS. 这只是几个库调用,但是首先您需要对服务器说出STMP, 然后切换到TLS。 (Maybe that's already bundled up in the openssl-library in a single call.) (也许已经在一个调用中将其捆绑在openssl库中。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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