Click here to Skip to main content
15,895,870 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
#include "openssl\applink.c"
    #include "openssl\bio.h"
    #include "openssl\ssl.h" 
    #include "openssl\err.h" 
    #include"main.h"
    #include<winsock2.h>
    #pragma comment(lib,"ws2_32.lib")
    main()
    {
    WSADATA wsa;
    SOCKET sock;
    struct sockaddr_in server_addr;
    char Send_BUFFER[2000]="",Recv_BUFFER[2000]="";   
    FILE *fp;
    X509 *cert;
    EVP_PKEY *pkey;	
    SSL_METHOD *meth;
    SSL_CTX *ctx=NULL;
    SSL *ssl;  	
    SSL_library_init();
    OpenSSL_add_all_algorithms();
    SSL_load_error_strings();   
    if(WSAStartup(MAKEWORD(2,2),&wsa)!= 0)  
    {
    printf("Failed. Error Code : %d",WSAGetLastError());
    return 1;
    }
    if((sock =socket(AF_INET,SOCK_STREAM,0 )) == INVALID_SOCKET)
    {
    printf("Could not create socket : %d" , WSAGetLastError());
    return 1;
    }    	
    server_addr.sin_family      = AF_INET;
    server_addr.sin_port        = htons(port);       
    server_addr.sin_addr.s_addr = inet_addr(host);
    if(connect(sock,(struct sockaddr*)&server_addr,sizeof(server_addr))==SOCKET_ERROR)
    {
    printf("Connect error");
    }	
    ssl = SSL_new(ctx);
    SSL_set_fd(ssl, sock);    
    fp=fopen(CLIENT_CERT, "r");
    if(!fp)
    {
    fprintf(stderr, "unable to open: %s\n", CLIENT_CERT);	   
    return EXIT_FAILURE;
    }
    cert = PEM_read_X509(fp, NULL, NULL, NULL);
    if(!cert) 
    {
    fprintf(stderr, "unable to parse certificate in: %s\n", CLIENT_CERT);
    fclose(fp);
    return EXIT_FAILURE;
    }
    SSL_use_certificate(ssl, cert);
    pkey = X509_get_pubkey(cert);
    SSL_CTX_use_PrivateKey(ctx, pkey);    
    if( !SSL_CTX_check_private_key( ctx ) )
    {
    ERR_print_errors_fp(stderr);
    exit(1);
    }
    if(SSL_connect(ssl)<=0)
    {
    puts("SSL Connect error");
    }	 
    SSL_write(ssl, Send_BUFFER,sizeof(Send_BUFFER));
    SSL_get_error(ssl, status);
    SSL_read(ssl, Recv_BUFFER,2000);
    puts(Recv_BUFFER);  
    closesocket(sock);   	     
    }
Posted
v2

1 solution

Do you have an SSL certificate for the production site installed on the HTTP header of the site? what is the behaivour of the production environment. If you type the url with https is it going to the infinite loop? Check with the HttpWatch or Fiddler. All the traces will be available and you will get the reason why its not working in the Production site.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900