简体   繁体   中英

C/SSL/JQuery.ajax() client -> server connection reset, but 1 byte sent

Version Info

  • Libs:

    • libgnutls-openssl.so.27 (libc6,x86-64)
    • libcrypto.so.1.0.0 (libc6,x86-64)
  • GCC version: gcc (Ubuntu/Linaro 4.8.1-10ubuntu9) 4.8.1

  • OS ( uname -voir ): 3.11.0-12-generic #19-Ubuntu SMP Wed Oct 9 16:20:46 UTC 2013 x86_64 GNU/Linux
  • Browser: Google Chrome Version 39.0.2171.71 (64-bit)

Scenario

When opening a socket via PHP, I can connect and send data to my instance of a C SSL server and receive a response, all with no issues. However, when I attempt to connect using JQuery AJAX from a client, the connection is established, immediately closed, reopened, and finally the server receives a single byte 'G', presumably the first letter of the GET request.

This results in:

GET https://localhost:2343/?keyReq=961ee53a75eef2e2 net::ERR_CONNECTION_RESET

being displayed in Chrome's console.

Thoughts

I don't want to be too quick to blame the client side, but I can connect using sockets just fine. Problem is, my server needs to deal with both socket and HTTPS requests.

It's also possible that I wrote the SSL accept and reads wrong somehow, in such a way that a web client can't cope.

I would be very grateful for any advice anyone could lend me to get me going in the right direction.

Code

ssl_server.c (The connection bits.)

void datactl( SSL *ssl ) { 
/* Serve the connection - threadable */

    char buf[1024];
    char reply[1024];
    int sd, bytes, rtn;

    if( SSL_accept( ssl ) == FAIL ) {                   /* do SSL-protocol accept */
        ERR_print_errors_fp( stderr );
    }   
    else {
        crtprint( ssl );                                /* get any certificates   */
        bytes = SSL_read( ssl, buf, sizeof( buf ) );    /* get request            */
        if( bytes > 0 ) { 
            buf[ bytes ] = 0;
            char tmp[1024];
            printf("data: '%s' (%d bytes)", buf, bytes );
            procdata( buf );                            /* process data           */
            getres( buf, reply );                       /* construct reply        */
            SSL_write( ssl, reply,
                            strlen( reply );            /* send reply             */
        }
        else
            ERR_print_errors_fp( stderr );
    }   
    sd = SSL_get_fd( ssl );                             /* get socket connection  */
    SSL_free( ssl );                                    /* release SSL state      */
    close( sd );                                        /* close connection       */
}

/* main - create SSL socket server. */
int main( int argc, char *argv[] ) { 

    SSL_CTX *ctx;
    int server;
    int len;
    int addrlen;
    char cwd[1024];
    char crt[1024];
    char key[1024];

    getcwd( cwd, sizeof( cwd ) );

    strcpy( crt, cwd );
    strcpy( key, cwd );
    strcat( crt, "/servers/certs/server.crt" );
    strcat( key, "/servers/certs/server.key" );

    ctx = initsrvctx(); /* initialize SSL. */
    getcrts( ctx, crt, key ); /* load certs.           */
    server = startsck( PORTNUM ); /* create server socket. */
/* (PORTNUM is defined at compile time by using -DPORTNUM=#### */

    while( 1 ) { 
        struct sockaddr_in serv;
        int len = sizeof( serv );
        SSL *ssl;

        int client = accept(
            server,
            ( struct sockaddr * ) &serv,
            &len
        ); /* accept connection as usual. */

        ssl = SSL_new( ctx ); /* get new SSL state with context */
        SSL_set_fd( ssl, client ); /* set connection socket to SSL state */
        datactl( ssl ); /* service connection */
    }   
    close( server ); /* close server socket */
    SSL_CTX_free( ctx ); /* release context */
}

test.php

<?php require('./inc/head.php'); ?>
<script type="text/javascript">
function keyRequest() {
    $.ajax({
        type: 'GET',
        url: 'https://localhost:2343',
        data: { hello: "hello", world: "world" }
    });
}

keyRequest();
</script>

Output (The errors here are self defined.)

2015-02-04 15:58:14 [OPEN]  | Connection opened with client. (127.0.0.1)
2015-02-04 15:58:14 [CLOSE] | Connection with client closed.
2015-02-04 15:58:14 [OPEN]  | Connection opened with client. (127.0.0.1)
2015-02-04 15:58:14 [DEBUG] | data: 'G' (1 bytes)
2015-02-04 15:58:14 [DEBUG] | Starting data processing.
2015-02-04 15:58:14 [ERR]   | Malformed or invalid data. Code: 0x10a.
2015-02-04 15:58:14 [ERR]   | Malformed or invalid data. Code: 0x10a.
2015-02-04 15:58:14 [CLOSE] | Connection with client closed.

The buffer in C is to small, you should read until you get the end of the stream - this is ussualy specified in "Content-Length" header in the request. I advice you to read the HTTP Protocol Standard, afterwards I am sure that you will easily find the solution to your problem and the connect disconnect issue will also be solved.

I managed to fix my problem. When specifying the SSL_method to be used, I had SSLv3_server_method() instead of SSLv23_server_method() . After changing this, there were no problems with reading to the buffer.

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