【问题标题】:How to set APLN protocols before TLS handshake with OpenSSL in Rust?如何在 Rust 中使用 OpenSSL 在 TLS 握手之前设置 APLN 协议?
【发布时间】:2023-01-08 06:06:22
【问题描述】:

我想在 TLS 握手之前将 APLN 协议设置为 "h2""http/1.1"。我正在使用.set_alpn_protos()。但是,我的尝试在运行时产生错误:

context.set_alpn_protos(b"\x06h2\x08http/1.1").expect("set ALPN error");
thread 'main' panicked at 'set ALPN error: ErrorStack([])', src/checker/tls/get_tls_info.rs:58:56

我可以像这样在 Python 中成功设置它们:

ssl.set_alpn_protos([b'h2', b'http/1.1'])

我究竟做错了什么?

【问题讨论】:

    标签: rust openssl h2 http-1.1 alpn


    【解决方案1】:

    从您链接的文档(强调我的):

    它由一系列受支持的协议名称组成,这些名称以它们的前缀为前缀字节长度.

    替换,因为b"h2"2字节而不是6

    context.set_alpn_protos(b"h2http/1.1").expect("set ALPN error");
    

    您还可以使用类似以下的内容从类似于 python 的列表中计算有线格式:

    let protos: &[&[u8]] = &[b"h21", b"http/1.1"];
    let wire = protos.into_iter().flat_map(|proto| {
        let mut proto = proto.to_vec();
        let len: u8 = proto.len().try_into().expect("proto is too long");
        proto.insert(0, len);
        proto
    }).collect::<Vec<_>>();
    

    【讨论】:

    • 谢谢你的帮助,你是对的。我只是错过了那个,再次感谢我对我的英语感到不好....
    猜你喜欢
    • 2018-10-14
    • 2013-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多