hey,

ive been looking around in the demos and apps folders and have been trying
to work things out with the ssl client/server. i tried porting the
demos\ssl examples to win32, the server runs perfectly but the client has
problems. seems there is something going wrong with the context creation.
the client connects without any cipher and the calls to SSL read/write
fail. although it does pickup the server certificate info fine. anyway i
included the code below.

any help is appreciated.
-neal

----------------------------------------------------------
int 
main(void) {                    

        SSL                     *ssl;        
        SSL_CTX         *context;

        WORD    wVersionRequested;              /* Version of Winsock to load */
        WSADATA         wsaData;                          /* Winsock implementation 
details */  
        struct sockaddr_in sa_cli;
        int client_sd;
        int bytes_received;

        char buffer[4096];

        wVersionRequested = MAKEWORD(2, 0);                      /* Request Winsock 
v2.0 */
        if (WSAStartup(wVersionRequested, &wsaData) != 0){
                fprintf(stderr,"Failed to load Winsocks");
                exit(-1);
        }

        /* socket preperation */
        client_sd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
        memset(&sa_cli, 0, sizeof(sa_cli));

        sa_cli.sin_family                   = AF_INET;
        sa_cli.sin_addr.s_addr  = inet_addr("192.168.1.1");
        sa_cli.sin_port                      = htons(4433);

        if (connect(client_sd, (struct sockaddr*) &sa_cli, sizeof(sa_cli)) < 0 ) {
                fprintf(stderr, "Failed to connect");
                exit(-1);
        }
        printf("Connection established.\n");

        /* SSL preperation */
        context   = Create_CTX();
        ssl             = SSL_new(context);

        SSL_set_fd(ssl, client_sd);
        SSL_connect(ssl);

        /* print connection info */
        printf("SSL connection using (%s)\n", SSL_get_cipher(ssl));
        Print_SSLcert(SSL_get_peer_certificate(ssl));
  
        /* data exchange */
        SSL_write(ssl, "Connection successful.", 22);
        printf("Message transmitted.\n");

        bytes_received = SSL_read(ssl, buffer, sizeof(buffer) - 1);
        buffer[bytes_received] = '\0';

        printf("Got (%d) chars: (%s)\n", bytes_received, buffer);

        /* cleanup  */
        SSL_shutdown(ssl);
        printf("Connection shut down.\n");
        closesocket(sd);  
        
        SSL_free(ssl_socket);
        SSL_CTX_free(ctx);
}


SSL_CTX 
*Create_CTX(void) {

        SSL_METHOD      *method;
        SSL_CTX         *ctx;   
        
        /* ssl prelimenaries */
        SSL_load_error_strings();
        OpenSSL_add_ssl_algorithms();

        /* load ssl/method ctx */
        method = SSLv23_client_method();

        ctx=SSL_CTX_new(method);
        CHK_ALLOC(ctx, 2);

        SSL_CTX_set_cipher_list(ctx,"DES-CBC3-SHA");
        return(ctx);
}


void 
Print_SSLcert(server_cert)
        X509 *server_cert;
 {

        char *str;

        printf("Server certificate:\n");
        str = X509_NAME_oneline(X509_get_subject_name(server_cert), 0, 0);
        CHK_NULL(str);

        printf("\t subject: %s\n", str);
        Free(str);

        str = X509_NAME_oneline(X509_get_issuer_name(server_cert), 0, 0);
        CHK_NULL(str);

        printf("\t issuer: %s\n", str);
        Free(str);

        X509_free(server_cert);
}

----------------------------------------------------------
______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
User Support Mailing List                    [EMAIL PROTECTED]
Automated List Manager                           [EMAIL PROTECTED]

Reply via email to