LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-22-2011, 05:14 AM   #1
thirumalesh
Member
 
Registered: Sep 2007
Posts: 54

Rep: Reputation: 15
SSL_read problem


Hi,

I am writing a SSL Client program which is working fine if the request is lessthan or equal to 500 bytes to server.But whenever the request becomes more than 500 bytes my client receiving only 1 byte of response which is [H] and after that I/O error is occuring(SSL_ERROR_SYSCALL)

can anyone have an idea why this is happening?

Thanks in advance
 
Old 09-22-2011, 07:01 AM   #2
Proud
Senior Member
 
Registered: Dec 2002
Location: England
Distribution: Used to use Mandrake/Mandriva
Posts: 2,794

Rep: Reputation: 116Reputation: 116
Post your code being as this client is your own.
Also read the definition of the functions & libraries you're calling, be sure you're not missing a documented behaviour/a check you should make in your read loop before assuming a variable's contents, or that you're not looping too much and only capturing the last read/buffer's worth of data.

This seems relevant
http://www.openssl.org/docs/ssl/SSL_read.html
Quote:
SSL_read() tries to read num bytes from the specified ssl into the buffer buf.
...
SSL_read() works based on the SSL/TLS records. The data are received in records (with a maximum record size of 16kB for SSLv3/TLSv1). Only when a record has been completely received, it can be processed (decryption and check of integrity). Therefore data that was not retrieved at the last call of SSL_read() can still be buffered inside the SSL layer and will be retrieved on the next call to SSL_read(). If num is higher than the number of bytes buffered, SSL_read() will return with the bytes buffered. If no more bytes are in the buffer, SSL_read() will trigger the processing of the next record. Only when the record has been received and processed completely, SSL_read() will return reporting success. At most the contents of the record will be returned. As the size of an SSL/TLS record may exceed the maximum packet size of the underlying transport (e.g. TCP), it may be necessary to read several packets from the transport layer before the record is complete and SSL_read() can succeed.
http://www.mail-archive.com/openssl-.../msg57544.html
Quote:
SSL_ERROR_SYSCALL means that an underlying call to the system failed.
Check errno in that case.

If you're getting the error on larger pieces of data, instead of
smaller pieces of data, it sounds like you're not properly handling
the case where your read buffer isn't large enough, needs to be
extended, and the read continued. Without knowing the value of errno,
it's impossible to guess, though.
Quote:
SSL_ERROR_SYSCALL

Some I/O error occurred. The OpenSSL error queue may contain more information on the error. If the error queue is empty (i.e. ERR_get_error() returns 0), ret can be used to find out more about the error: If ret == 0, an EOF was observed that violates the protocol. If ret == -1, the underlying BIO reported an I/O error (for socket I/O on Unix systems, consult errno for details).
Is it just that the request is >500bytes, or that the source is different and possibly introducing an invalid EOF?

Last edited by Proud; 09-22-2011 at 07:25 AM.
 
Old 09-23-2011, 03:59 AM   #3
thirumalesh
Member
 
Registered: Sep 2007
Posts: 54

Original Poster
Rep: Reputation: 15
Hi Proud,

Thanks for the reply...

I have pasted my code for sending and receiving data using SSL below..

The complete request as it is i can't post...

And the response that I am getting is

Quote:
Received [H] from Server
SSL read Error: Premature close errno[0]

This is only if the requst length is > 500 bytes




Code:
static char *REQUEST_TEMPLATE="POST / HTTP/1.1\r\nHost:secure.somesite.com\r\nContent-Type: application/x-www-form-urlencoded\r\nContent-Length:398\r\n\r\nXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";


static char *host=HOST;
static int port=PORT;
static int require_server_auth=1;

static int http_request(ssl)
  SSL *ssl;
  {
    char *request=0;
    char buf[65536];
    int r;
    int len, request_len;

    /* Now construct our HTTP request */
    request_len=strlen(REQUEST_TEMPLATE);
    if(!(request=(char *)malloc(request_len)))
      err_exit("Couldn't allocate request");
    snprintf(request,request_len,REQUEST_TEMPLATE,
      host,port);

    /* Find the exact request_len */
    request_len=strlen(request);

    r=SSL_write(ssl,request,request_len);
    printf("sent [%d] bytes\n Request [%s]\n",r,request);
    switch(SSL_get_error(ssl,r)){
      case SSL_ERROR_NONE:
        if(request_len!=r)
          err_exit("Incomplete write!");
        break;
        default:
          berr_exit("SSL write problem");
    }

    char errbuf[256];
    memset(errbuf,0,256);
    memset(buf,0,65536);
    /* Now read the server's response, assuming
       that it's terminated by a close */
    //SSL_set_connect_state(ssl);
    while(1)
    {
      ERR_clear_error();
      r=SSL_read(ssl,buf,BUFSIZZ);
      switch(SSL_get_error(ssl,r))
      {
        case SSL_ERROR_NONE:
          len=r;
          break;
        case SSL_ERROR_ZERO_RETURN:
          goto shutdown;
        case SSL_ERROR_SYSCALL:
        //perror("SSL Read: ");
        //ERR_error_string(r,errbuf);
        printf(errbuf);
          fprintf(stderr,
            "SSL read  Error: Premature close errno[%d]\n",errno);
          goto done;
        default:
          berr_exit("SSL read problem");
      }

//      fwrite(buf,1,r,stdout);
      printf("Received [%s] from Server\n",buf);
    }

  shutdown:
    r=SSL_shutdown(ssl);
    switch(r){
      case 1:
        break; /* Success */
      case 0:
      case -1:
      default:
        berr_exit("Shutdown failed");
    }

  done:
    SSL_free(ssl);
    free(request);
    return(0);
  }
 
Old 09-23-2011, 06:45 AM   #4
Proud
Senior Member
 
Registered: Dec 2002
Location: England
Distribution: Used to use Mandrake/Mandriva
Posts: 2,794

Rep: Reputation: 116Reputation: 116
I'm not a C man, but BUFSIZZ is being defined where? Looks like it should be 65536 but there's a lot of occurances of this magic number instead.

Re-reading that documentation I originally linked, it looks like you could do more error value investigation in your code rather than your current print and goto.

Last edited by Proud; 09-23-2011 at 06:53 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
SSL_read error due of a SSL protocol violation salimshahzad Linux - Newbie 1 01-19-2011 06:45 PM
Do I have a path problem, an Apache2 problem or a Javascript problem or any other pro rblampain Linux - Networking 0 12-29-2010 03:50 AM
Sound Card problem(every time i install linux i have diffirent hardware problem) jacka1l Linux - Newbie 7 08-11-2005 06:10 AM
Lan configuration problem - NFS boot problem - RX&TX packets errors 242VDM242 Linux - Networking 4 11-25-2004 01:35 PM
perl problem? apache problem? cgi problem? WorldBuilder Linux - Software 1 09-17-2003 07:45 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:58 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration