While executing the below code its coring randomly in two cases,

1) While executing the method SSL_library_init() in the constructor.
2) Coring while executing the call back method locking_function(). 

We are not sure, now the call back method is calling after it is set to NULL 

Ex : CRYPTO_set_locking_callback(NULL)

Here, after we set to NULL its calling the call back method.

We want to make sure it should not be called after setting to NULL.

It will be great if someone explain me in detail, how the call back
mechanism works internally.

Code:
-----
pthread_mutex_t *SslBIO::_lnSslBioMutex=NULL;

void SslBIO::locking_function(int mode, int type, const char * file, int
line)
{
  int rstat;
  if (mode & CRYPTO_LOCK)
  {
    fprintf(stderr, "\nDEBUG: Locking the Mutex _lnSslBioMutex[%d] Mode = %d
File :%s Line No : %d\n",type,mode,file,line);
    rstat = pthread_mutex_lock(&(SslBIO::_lnSslBioMutex[type]));
    lnChkMutex(rstat, FL);
  }
  else
  {
    fprintf(stderr, "\nDEBUG: UnLocking the Mutex _lnSslBioMutex[%d] Mode =
%d File :%s Line No : %d\n",type,mode,file,line);
    rstat = pthread_mutex_unlock(&(SslBIO::_lnSslBioMutex[type]));
    lnChkMutex(rstat, FL);
  }
}

unsigned long SslBIO::id_function()
{
  unsigned long ulThreadId = (unsigned long)pthread_self();
  fprintf(stderr, "\nDEBUG: Thread ID = %d\n",ulThreadId);
  return (ulThreadId);
}

int SslBIO::init(const char * initarg)
{
     int i;

    _lnSslBioMutex = (pthread_mutex_t *) OPENSSL_malloc(CRYPTO_num_locks() *
sizeof(pthread_mutex_t));

    if (!_lnSslBioMutex)
      return 0;

    fprintf(stderr, "\nDEBUG: Number of Locks(CRYPTO_NUM_LOCKS) = %d
\n",CRYPTO_num_locks());

    for(i=0;i<CRYPTO_num_locks();i++)
    {
      fprintf(stderr, "\nDEBUG: Initialize the Mutex
_lnSslBioMutex[%d]\n",i);
      int rstat = pthread_mutex_init(&(_lnSslBioMutex[i]),
pthread_mutexattr_default);
      lnChkMutex(rstat, FL);
    }

    CRYPTO_set_id_callback(SslBIO::id_function);
    CRYPTO_set_locking_callback(SslBIO::locking_function);

    
   return 0;
}

int SslBIO::terminate()
{
  int i = 0;
  int rstat;
  if (!_lnSslBioMutex)
  {
    return 0;
  }

  CRYPTO_set_id_callback(NULL);
  CRYPTO_set_locking_callback(NULL);

  for(i=0;i<CRYPTO_num_locks();i++)
  {
    fprintf(stderr, "\nDEBUG: Cleanup the Mutex _lnSslBioMutex[%d]\n",i);
    rstat = pthread_mutex_destroy(&(_lnSslBioMutex[i]));
    lnChkMutex(rstat, FL);
  }

  OPENSSL_free(_lnSslBioMutex);
  _lnSslBioMutex = NULL;
}

SslBIO::SslBIO(const char *host,
                 const int port, const int timeout, 
                 int &retCode, int blockingConnect) 
{
  _debug = 0;
  _lnreqctx = 0;
  _type = SslBIO::CALLER;
  _totSent = 0;
  _totReceived = 0;
  _errBuf[0] = '\0';
  if(host!=NULL)
    strcpy(_hostName,(char *)host);
  _portNum = port;
  
  retCode = FAIL;

  

  /* Set up the library */
  SSL_library_init();
  ERR_load_BIO_strings();
  SSL_load_error_strings();
  OpenSSL_add_all_algorithms();

  _sslctx = SSL_CTX_new(SSLv23_client_method());
  if(_sslctx == 0)
  {
    fprintf(stderr, "failed SslBIO::SslBIO. SslBIO not initialized.
_sslctx=0\n");
    return;
  }

  _bio = BIO_new_ssl_connect(_sslctx);


  BIO_get_ssl(_bio, &_ssl);
  SSL_set_mode(_ssl, SSL_MODE_AUTO_RETRY);

  /* Create and setup the connection */
  BIO_set_conn_hostname(_bio, _hostName); //
"cdc13-www.lexisnexis.com:https");
  BIO_set_conn_int_port(_bio, &_portNum); // "443");

  if(BIO_do_connect(_bio) <= 0)
  {
      fprintf(stderr, "Error attempting to connect
[%s[%d]]\n",_hostName,_portNum);
      ERR_print_errors_fp(stderr);
      BIO_free_all(_bio);
      _bio = NULL; //Nullify the _bio member object after deallocating
      return;
  }
  else
  {
    fprintf(stderr, "SslBIO: connected[%s[%d]]\n",_hostName,_portNum);
  }

  /* Check the certificate */

  if(SSL_get_verify_result(_ssl) != X509_V_OK)
  {
      fprintf(stderr, "Certificate verification error: %i\n",
SSL_get_verify_result(_ssl));
  }

  _timeout = timeout;
  retCode = OK;
}


SslBIO::~SslBIO() {

  /* Close the connection and free the context */
  if (_bio != 0)
  {
    BIO_free_all(_bio);
    _bio=NULL; // bulletproof for webstar 3019980
  }
  if (_sslctx != 0)
  {
    fprintf(stderr, "Freeing SslBIO::_sslctx\n");
    SSL_CTX_free(_sslctx);
    _sslctx=NULL; // bulletproof for webstar 3019980
  }
}

int SslBIO::read(char *buf, int len, int &currRead) 
{
  int  retCode;
  
  if (buf == LN_NULL) 
  {
      return(FAIL);
  }

  printf("DEBUG: Before Read \n");
  currRead = BIO_read(_bio, buf, len); 
  printf("DEBUG: After Read currRead = %d Buf = %s Length =
%d\n",currRead,buf,len);

  if (currRead >= 0)
  {
    buf[currRead] = 0;
    if (_debug)
    {
      ostrstream strm;
      strm << "Out of SslBIO::read currRead =  " << currRead << endl;
      
      strm << ends;
      
      delete strm.str();
    }
  }
  else
    buf[0] = 0;

  return(OK);
}

int SslBIO::write(const char *buf, const int len)
{
    
  int          written;
  int          totLen;
  
  if (len < 0) 
  {
    return(FAIL);
  }

  totLen = len;
  printf("DEBUG: Before Write\n");
  if ((written = BIO_write(_bio, buf, len)) != totLen) 
  {
    return(FAIL);
  }
  printf("DEBUG: After Write written = %d Buf = %s Length =
%d\n",written,buf,len);
  _totSent += totLen;
  
  return(OK);
}

-- 
View this message in context: 
http://old.nabble.com/Core-occurred-while-executing-SSL_library_init%28%29-and-call-back-method-locking_function%28%29-tp31596267p31596267.html
Sent from the OpenSSL - User mailing list archive at Nabble.com.

______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
User Support Mailing List                    openssl-users@openssl.org
Automated List Manager                           majord...@openssl.org

Reply via email to