Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int setupSSL(int server){
- if(InitCTX() != 0)
- return -1;
- ssl = SSL_new(ctx); /* create new SSL connection state */
- SSL_set_fd(ssl, server); /* attach the socket descriptor */
- if ( SSL_connect(ssl) != 1 ){ /* perform the connection */
- debug_log("SYSTEM:SSL_SOCKET:Could not build a SSL session\n",TRACE_LOG);
- return -1;
- }
- cert = SSL_get_peer_certificate(ssl);
- if(cert == NULL)
- {
- debug_log("SYSTEM:SSL_SOCKET:Unable to retrive server certificate\n",TRACE_LOG);
- return -1;
- }
- if(SSL_get_verify_result(ssl)!=X509_V_OK)
- {
- debug_log("SYSTEM:SSL_SOCKET:Certificate doesn't verify\n",TRACE_LOG);
- return -1;
- }
- /*X509_NAME_get_text_by_NID (X509_get_subject_name (cert), NID_commonName, peer_CN, 256);
- if(strcasecmp(peer_CN, cnName)){
- debug_log("SYSTEM:SSL_SOCKET:Common name doesn't match host name\n",TRACE_LOG);
- return -1;
- }*/
- return 0;
- // LoadCertificates(ctx, CertFile, KeyFile);
- }
- int InitCTX(void)
- {
- OpenSSL_add_all_algorithms();/* Load cryptos, et.al. */
- SSL_load_error_strings();/* Bring in and register error messages */
- if(SSL_library_init() < 0)
- {
- debug_log("SYSTEM:SSL_SOCKET:Could not initialize the OpenSSL library\n",TRACE_LOG);
- return -1;
- }
- method = SSLv3_client_method();/* Create new client-method instance */
- ctx = SSL_CTX_new(method);/* Create new context */
- if ( ctx == NULL)
- {
- debug_log("SYSTEM:SSL_SOCKET:Unable to create a new SSL context structure\n",TRACE_LOG);
- return -1;
- }
- SSL_CTX_set_options(ctx, SSL_OP_NO_SSLv2);
- if (SSL_CTX_use_certificate_file(ctx,CertFile, SSL_FILETYPE_PEM) <= 0)
- {
- SSL_CTX_free(ctx);
- ctx = NULL;
- debug_log("SYSTEM:SSL_SOCKET:Error setting the certificate file.\n",TRACE_LOG);
- return -1;
- }
- /* Set the list of trusted CAs based on the file and/or directory provided*/
- if(SSL_CTX_load_verify_locations(ctx,CertFile,NULL)<1)
- {
- SSL_CTX_free(ctx);
- ctx = NULL;
- debug_log("SYSTEM:SSL_SOCKET:Error setting verify location.\n",TRACE_LOG);
- return -1;
- }
- SSL_CTX_set_verify(ctx,SSL_VERIFY_PEER,NULL);
- SSL_CTX_set_timeout (ctx, 60);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement