运行 boost asio ssl 示例时出现异常

发布于 2024-11-16 16:57:32 字数 864 浏览 6 评论 0 原文

我正在尝试从 boost::asio 运行 SSL 示例,但在运行它们时出现“无效参数”异常。我在 Linux x86_64 上。

http://www.boost.org /doc/libs/1_46_1/doc/html/boost_asio/example/ssl/client.cpp

http://www.boost.org/doc /libs/1_46_1/doc/html/boost_asio/example/ssl/server.cpp

编译:

g++ server.cpp -o server -lboost_system -lssl
g++ client.cpp -o client -lboost_system -lssl

运行如下:

$ ./server 
Usage: server <port>
$ ./server 10000
Exception: Invalid argument
$ ./server 1000
Exception: Permission denied
$ sudo ./server 1000
Exception: Invalid argument

不确定问题是什么:( 任何帮助将不胜感激。

谢谢!

I'm trying to run the SSL examples from boost::asio and I'm getting an "Invalid argument" exception when I run them. I'm on Linux x86_64.

http://www.boost.org/doc/libs/1_46_1/doc/html/boost_asio/example/ssl/client.cpp

http://www.boost.org/doc/libs/1_46_1/doc/html/boost_asio/example/ssl/server.cpp

Compiled with:

g++ server.cpp -o server -lboost_system -lssl
g++ client.cpp -o client -lboost_system -lssl

Run like:

$ ./server 
Usage: server <port>
$ ./server 10000
Exception: Invalid argument
$ ./server 1000
Exception: Permission denied
$ sudo ./server 1000
Exception: Invalid argument

Not sure what the problem is :(
Any help would be greatly appreciated.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

痴意少年 2024-11-23 16:57:32

好的,对于将来发现此问题的任何人,您需要创建证书并对其进行适当的签名。
以下是 Linux 的命令:

//生成私钥

openssl genrsa -des3 -out server.key 1024

//生成证书签名请求

openssl req -new -key server.key -out server.csr

//使用私钥签署证书

openssl x509 -req -days 3650 -in server.csr -signkey server.key -out server.crt

//删除密码要求(例如需要)

cp server.key server.key.secure
openssl rsa -in server.key.secure -out server.key

//生成 dhparam 文件

openssl dhparam -out dh512.pem 512

完成后,您需要更改 server.cpp 和 client.cpp 中的文件名。

server.cpp

context_.use_certificate_chain_file("server.crt"); 
context_.use_private_key_file("server.key", boost::asio::ssl::context::pem);
context_.use_tmp_dh_file("dh512.pem");

client.cpp

ctx.load_verify_file("server.crt");

那么它应该一切正常!

OK, for anyone finding this in the future, you need to create your certificates and sign them appropriately.
Here are the commands for linux:

//Generate a private key

openssl genrsa -des3 -out server.key 1024

//Generate Certificate signing request

openssl req -new -key server.key -out server.csr

//Sign certificate with private key

openssl x509 -req -days 3650 -in server.csr -signkey server.key -out server.crt

//Remove password requirement (needed for example)

cp server.key server.key.secure
openssl rsa -in server.key.secure -out server.key

//Generate dhparam file

openssl dhparam -out dh512.pem 512

Once you've done that, you need to change the filenames in server.cpp and client.cpp.

server.cpp

context_.use_certificate_chain_file("server.crt"); 
context_.use_private_key_file("server.key", boost::asio::ssl::context::pem);
context_.use_tmp_dh_file("dh512.pem");

client.cpp

ctx.load_verify_file("server.crt");

Then it should all work!

橘亓 2024-11-23 16:57:32

使用 strace 再次执行测试以查看哪个系统调用获得 EINVAL,作为奖励,您将看到失败调用的参数。这可能是安全上下文设置失败的一部分,除非您拥有示例中的正确文件和数据:

context_.use_certificate_chain_file("server.pem");
context_.use_private_key_file("server.pem", boost::asio::ssl::context::pem);
context_.use_tmp_dh_file("dh512.pem");

您收到 EPERM 因为您试图绑定到特权 TCP 端口(其值为小于 1024)。这就是 ./server 10000 无法获取 EPERM 的原因。

Execute the tests again with strace to see which syscall gets the EINVAL, as a bonus you'll get to see the args for the failing call. It's likely part of the security context setup that's failing, unless you have the right files and data from the example:

context_.use_certificate_chain_file("server.pem");
context_.use_private_key_file("server.pem", boost::asio::ssl::context::pem);
context_.use_tmp_dh_file("dh512.pem");

You were getting EPERM because you were trying to bind to a privileged TCP port (one whose value is less than 1024). That's why ./server 10000 does not get EPERM.

梅窗月明清似水 2024-11-23 16:57:32

当遵循 @Shootfast 的答案时,出现错误:“错误的 SSL 配置:use_certificate_chain_file:ee 密钥太小”

更改第一个行:

openssl genrsa -des3 -out server.key 1024

至:

openssl genrsa -des3 -out server.key 2048

为我修好了。

之后,我收到错误:“错误的 SSL 配置:use_private_key_file:无起始行”,原因和解决方案如下所述:解决方案< /a> (这或多或少是@Shootfast 答案。)

When following the answer of @Shootfast an error appered: 'bad SSL configuration: use_certificate_chain_file: ee key too small'

Changing the first line:

openssl genrsa -des3 -out server.key 1024

to:

openssl genrsa -des3 -out server.key 2048

fixed it for me.

After that I got the error: 'bad SSL configuration: use_private_key_file: no start line' the reason and solution to this is explained here: solution (It is more or less the reason for the last command of @Shootfast answer.)

吃不饱 2024-11-23 16:57:32

对于未来的读者;对于遇到连接问题的人(例如客户端意外关闭):请确保在客户端 ssl 套接字上设置主机名:

sock.set_verify_callback(ssl::host_name_verification("host.name"));

For future readers; and for people running into connection problems (e.g. unexpected close on client): make sure to set the host name on the client ssl socket:

sock.set_verify_callback(ssl::host_name_verification("host.name"));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文