简体   繁体   中英

Simple gSoap Server and Client with SSL

The call to soap_ssl_accept always results in the error SSL23_GET_CLIENT_HELLO:unknown protocol . I made it simple as possible and turned of server authentication by setting SOAP_SSL_NO_AUTHENTICATION . I broke it down to following server and client code. Can you help me getting a working example?

Server Side:

#include <cstdio>
#include "ADService.nsmap" // get namespace bindings
#include "stdsoap2.h"
#include "soapH.h"

int ns1__add(struct soap *soap, xsd__int a, xsd__int b, xsd__int& c) {
    c = a + b;
    return SOAP_OK;
}


int main() {
    int m, s;
    struct soap soap;
    soap_ssl_init(); // init OpenSSL (just once)
    soap_init(&soap);
    if (soap_ssl_server_context(&soap,
        SOAP_SSL_NO_AUTHENTICATION,
        NULL, // keyfile: required when server must authenticate to clients
        NULL, // password to read the key file
        NULL, // optional cacert file to store trusted certificates
        NULL, // optional capath to directory with trusted certificates
        NULL, // DH file name or DH key len bits 
        NULL, // if randfile!=NULL: use a file with random data
        NULL)) {
        soap_print_fault(&soap, stderr);
        exit(1);
    }
    m = soap_bind(&soap, NULL, 18000, 100); // use port 18000
    if (m < 0) {
        soap_print_fault(&soap, stderr);
        exit(1);
    }
    printf("Socket connection successful: master socket = %d\n", m);
    for (;;) {
        s = soap_accept(&soap);
        printf("Socket connection successful: slave socket = %d\n", s);
        if (s < 0) {
            soap_print_fault(&soap, stderr);
            break;
        }
        if (soap_ssl_accept(&soap))
            soap_print_fault(&soap, stderr);
        else
            soap_serve(&soap);
        soap_destroy(&soap);
        soap_end(&soap);
        soap_free(&soap); // done and free context  
    }
    soap_done(&soap); /* deallocates SSL context */
    return 0;
}

Client Side:

#include <iostream>
#include "soapH.h" // obtain the generated stub 
#include "ADService.nsmap" // obtain the namespace mapping table 

int main(int argc, const char* argv[]) {
    soap soap;
    soap_ssl_init();
    soap_init(&soap);
    if (soap_ssl_client_context(&soap,
       SOAP_SSL_NO_AUTHENTICATION,
       NULL, // keyfile: required only when client must authenticate to server
       NULL, // password to read the key file (not used with GNUTLS)
       NULL, // cacert file to store trusted certificates
       NULL, // capath to directory with trusted certificates
       NULL  // if randfile!=NULL: use a file with random data to seed randomness
    )) {
       soap_print_fault(&soap, stderr);
       return 1;
    }
    xsd__int result;
    if(soap_call_ns1__add(
        &soap, "localhost:18000", 0, 1, 2, result) != SOAP_OK) {
        soap_print_fault(&soap, stderr);
        soap_print_fault_location(&soap, stderr);
        return 1;
    }
    printf("Result = %d", result);
    soap_destroy(&soap);
    soap_end(&soap);
    soap_done(&soap);
    return 0;
}

Output on server side:

Socket connection successful: master socket = 1916
Socket connection successful: slave socket = 1912
Error 30 fault: SOAP-ENV:Server [no subcode]
"SSL_ERROR_SSL
error:140760FC:SSL routines:SSL23_GET_CLIENT_HELLO:unknown > protocol"
Detail: SSL_accept() failed in soap_ssl_accept()
Press any key to continue

This is not a definitive answer. But might help. How I ended up with error what you see:

3077928696:error:140760FC:SSL routines:SSL23_GET_CLIENT_HELLO:unknown protocol:s23_srvr.c:628

It happens when on successful TCP connection with server,client sends in ill-formed client_hello message. I basically in my test sent junk bytes after TCP connection.

Capture tcpdump b/w server and client and see whats going out from client.

使用主机名https://localhost:18000调用soap_call_ns1__add解决了该问题。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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