我正在尝试使用SSL设置示例gSOAP服务器。我创建了一个自签名证书:
openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -days 365 -nodes
然后将证书加入一个文件:
cat cert.pem key.pem >both.pem
然后运行此代码:
#include <stdsoap2.h>
#include <stdexcept>
#include <sstream>
#include <iostream>
#define PORT 6667
int main(int argc, char **argv) {
struct soap engine(SOAP_C_UTFSTRING);
engine.bind_flags=SO_REUSEADDR;
soap_ssl_noinit();
int rc = soap_ssl_server_context(&engine,
SOAP_SSL_DEFAULT,
"both.pem", // keyfile
nullptr, // password
nullptr, // cacert
nullptr, // capath
nullptr, // dhfile
nullptr, // randfile
nullptr // serverid
);
if (rc) {
std::stringstream error;
soap_stream_fault(&engine, error);
throw std::runtime_error("soap_ssl_server context failed " + error.str());
}
rc = soap_bind(&engine, NULL, PORT, 100);
if (rc < 0) {
throw std::runtime_error("soap_bind failed " + std::to_string(rc));
}
int master_socket = rc;
std::cout << "master socket: " << master_socket << std::endl;
engine.accept_timeout = 50;
engine.send_timeout = 50;
engine.recv_timeout = 50;
rc = soap_ssl_accept(&engine);
if (rc) {
soap_stream_fault(&engine, std::cout);
throw std::runtime_error("soap_ssl_accept failed " + std::to_string(rc));
}
std::cout << "soap_ssl_accept " << rc << std::endl;
return 0;
}
但我得到了这个输出:
master socket: 3
Error 30 fault detected[no subcode]
"SSL/TLS error"
Detail: No socket in soap_ssl_accept()
terminate called after throwing an instance of 'std::runtime_error'
what(): soap_ssl_accept failed 30
我做错了什么?
答案 0 :(得分:0)
我明白了。在致电soap_accept
之前,必须致电soap_ssl_accept
。正确的代码是:
rc = soap_accept(&engine);
if (rc) {
soap_stream_fault(&engine, std::cout);
throw std::runtime_error("soap_accept failed " + std::to_string(rc));
}
rc = soap_ssl_accept(&engine);
if (rc) {
soap_stream_fault(&engine, std::cout);
throw std::runtime_error("soap_ssl_accept failed " + std::to_string(rc));
}
然后修复错误。