c++ - gSOAP - 如何使用 SSL 和自签名证书

标签 c++ soap openssl ssl-certificate gsoap

我需要将我的跨平台程序连接到 SOAP Web 服务。我已经编译了 gSOAP 工具 wsdl2h 和soapcpp2,并使用这些工具从 .wsdl 文件生成了源代码文件。我已将定义“#define WITH_OPENSSL”放入 stdsoap2.h 中,并以这种方式使用 SSL。问题是,当我调用该服务时,调用返回 ERROR 30,这意味着 SSL ERROR,但我不知道真正的问题是什么。我知道服务器发送自签名证书,因为这是一个测试环境。实际上,打印了与此相关的错误消息。输出为:

Creating SOAP objects ...
Calling SOAP httpAccessService:
SSL verify error or warning with certificate at depth 0: self signed certificate
certificate issuer /C=IT/ST=Milan/L=Milan/O=Company/OU=Company/CN=company.it
certificate subject /C=IT/ST=Milan/L=Milan/O=Company/OU=Company/CN=company.it
SOAP ERROR 30

我用来调用服务的函数是这样的:

void gSOAPTesting::runTest() { 整数结果 = 0; size_t 请求大小; size_t 响应大小;

char endpoint[1024];
char buffer[8192];

string SoapAction;
struct soap *soap_container; 

ApplicationConfigurationServiceSoapBindingProxy Proxy1;

_ns1__httpAccessService *httpAccessService;
_ns1__httpAccessServiceResponse *httpAccessServiceResponse;

printf("Creating SOAP objects ...\n");
soap_container = soap_new();
//soap_container->mode   
httpAccessService = (_ns1__httpAccessService *) soap_instantiate(soap_container , SOAP_TYPE___ns1__httpAccessService , "" , "" , &requestSize);
httpAccessServiceResponse = (_ns1__httpAccessServiceResponse *) soap_instantiate(soap_container , SOAP_TYPE___ns1__httpAccessService , "" , "" , &responseSize);

soap_ssl_init(); /* init OpenSSL (just once) */

if(soap_ssl_client_context(soap_container ,
SOAP_SSL_DEFAULT ,
NULL,
NULL,
NULL,
NULL,
NULL     
) != SOAP_OK)
{
   printf("SOAP SSL Initialization Failure\n");  
   soap_print_fault(soap_container , stderr);
   return ;
}  

printf("Calling SOAP httpAccessService:\n");

SoapAction.clear();
SoapAction.append(SOAP_NAMESPACE_OF_ns1);
SoapAction.append("/");
SoapAction.append("httpAccessService");    

result = Proxy1.httpAccessService("https://XXX.XXX.XXX.XXX:XXXX" , NULL , httpAccessService , httpAccessServiceResponse);

if(result == SOAP_OK)
{
    printf("SOAP OK\n");                         
}
else
{
    printf("SOAP ERROR %d\n" , result);

    if(soap_check_state(soap_container) ) printf("Error: request soap struct not initialized\n");

    if(httpAccessService->soap == NULL)
    {
        printf("Error: NULL request SOAP struct\n");   
        return;                          
    }

    if(httpAccessService->soap->endpoint == NULL) printf("Error: Empty request endpoint\n");

    soap_stream_fault(soap_container , std::cout);        
}    

}

感谢任何帮助。

最佳答案

该问题与由于自签名而不受信任的证书有关。如果我在 stdsoap2.cpp 中评论这些行...

if (!ok)
{ soap_set_sender_error(soap, "SSL/TLS error", "SSL/TLS certificate host name mismatch in tcp_connect()", SOAP_SSL_ERROR);
  soap->fclosesocket(soap, sk);
  return SOAP_INVALID_SOCKET;
}

...即使证书是由未知机构颁发的,也会被接受。

关于c++ - gSOAP - 如何使用 SSL 和自签名证书,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19221108/

相关文章:

c++ - 修改 char *const 字符串

java - Spring WebService 不初始化服务和消费者的 bean

java - 如何在 SOAP 请求中设置字符编码

c++ - 尝试在 OpenSSL 中使用 EVP 函数时出现段错误

php - 解析 X509 证书

c++ - *(&arr + 1) - arr 如何给出数组 arr 元素的长度?

c++ - mbedTLS pk 解析错误

openssl - openssl 中的 SHA1_Init 实现在哪里

c++ - 搜索空字符串时 find vs find_first_of

c# - HTTPS 的 HttpWebRequest 出现 403 禁止错误