Hi guys,

This is the code I was talking about.

OS is WinNT 4.0
SSL is 0.9.2b

Can anyone see what I am doing wrong.  
When I try to connect with
any client, the client reports that the SSL_connect fails.
While the server reads a null string.

Thanks again,
Bryan


JNIEXPORT void JNICALL Java_com_bpsinfo_javassl_SSLSocket_nativeAccept
(JNIEnv *env, jobject obj, jint fd, jstring CertFile, jstring KeyFile)
{

        int                                     sd;
        struct sockaddr_in      sa;
        struct sockaddr_in  myname;
        int                                     mynameSize;
        mynameSize = sizeof(myname);

        const char *CertFileCS = env->GetStringUTFChars(CertFile, 0);
        const char *KeyFileCS = env->GetStringUTFChars(KeyFile, 0);

        SSL_CTX*                        ctx;
        SSL*                            ssl;
        jfieldID                        fid;                    // Used to find the 
field ID of java variables
        
        //File descriptor for socket that listens
        int sfd = (int) fd;

        // Initialize the SSL environment
        SSL_load_error_strings();                       
        SSLeay_add_ssl_algorithms();  
        
        // Decide which SSL method to use based upon compilation 
        SSL_METHOD *meth=NULL;
        
#if !defined(NO_SSL2) && !defined(NO_SSL3)
        meth=SSLv23_client_method();
#elif !defined(NO_SSL3)
        meth=SSLv3_client_method();
#elif !defined(NO_SSL2)
        meth=SSLv2_client_method();
#endif
        
        // If no SSL method then they compiled this with too many NO_SSL values.
        if (meth == NULL) {
                // Throw a java exception
                jclass newExcCls = env->FindClass("java/io/IOException");
                env->ThrowNew(newExcCls, "No SSL method");
                return;
        }
        
        // See if there was an error initializing the CTX context
        if ((ctx = SSL_CTX_new (meth)) == NULL) {
                // Throw a java exception
                jclass newExcCls = env->FindClass("java/io/IOException");
                env->ThrowNew(newExcCls, "SSL_CTX_new failed.");
                return;
        }

        if (SSL_CTX_use_RSAPrivateKey_file(ctx, (char*)KeyFileCS, SSL_FILETYPE_PEM)
< 0)
        {
                // Throw a java exception
                jclass newExcCls = env->FindClass("java/io/IOException");
                env->ThrowNew(newExcCls, "SSL_CTX_use_PrivateKey_file failed.");
                return;
        }

        if (SSL_CTX_use_certificate_file(ctx, (char*)CertFileCS, SSL_FILETYPE_PEM)
<= 0)
        {
                // Throw a java exception
                jclass newExcCls = env->FindClass("java/io/IOException");
                env->ThrowNew(newExcCls, "SSL_CTX_use_certificate_file failed.");
                return;
        }


        if (!SSL_CTX_check_private_key(ctx))
        {
                // Throw a java exception
                jclass newExcCls = env->FindClass("java/io/IOException");
                env->ThrowNew(newExcCls, "SSL_CTX_check_private_key failed.");
                return;
        }

        //block and wait for someone to connect and then accept
        sd = accept(sfd,(struct sockaddr *) &myname, &mynameSize);

        // Figure out what the local port we got is
        if (getpeername(sd, (struct sockaddr *) &myname, &mynameSize) < 0) {
                // Throw a java exception
                jclass newExcCls = env->FindClass("java/io/IOException");
                env->ThrowNew(newExcCls, "getpeername error");
                return;
        }       

        // Figure out what the remote port we got is
        if (getsockname(sd, (struct sockaddr *) &sa, &mynameSize) < 0) {
                // Throw a java exception
                jclass newExcCls = env->FindClass("java/io/IOException");
                env->ThrowNew(newExcCls, "getsockname error");
                return;
        }
        
        // Get the local IP addr
        char *localIP = inet_ntoa(myname.sin_addr);
        
        // Allocate memory to hold UTF version of the string
        unsigned short *localIPUTF;
        if ((localIPUTF = (unsigned short *) malloc(sizeof(unsigned short) *
(strlen(localIP) + 1))) == NULL) {
                // Throw a java exception
                jclass newExcCls = env->FindClass("java/io/IOException");
                env->ThrowNew(newExcCls, "memory allocation failed");
                return;
        }
        
        // Convert the local ip addr string into UTF
        int len = strlen(localIP) + 1;
        for (int i = 0; i < len; i++) {
                localIPUTF[i] = (unsigned short) localIP[i];
        }
        
        // Convert the local ip addr UTF into a java string
        jstring javaIP = env->NewString(localIPUTF, strlen(localIP));
        
        // Get rid of the local UTF
        free(localIPUTF);
        
        // Store the new java string
        fid = env->GetFieldID(env->GetObjectClass(obj), "localAddr",
"Ljava/lang/String;");
        env->SetObjectField(obj, fid, javaIP);
        
        // Store the local port number
        fid = env->GetFieldID(env->GetObjectClass(obj), "localPort", "I");
        env->SetIntField(obj, fid, ntohs(myname.sin_port));
        
        // Store the remote port number
        fid = env->GetFieldID(env->GetObjectClass(obj), "port", "I");
        env->SetIntField(obj, fid, ntohs(sa.sin_port));
        
        // Start SSL negotiation
        if ((ssl = SSL_new (ctx)) == NULL) {
                // See if there was an error initializing SSL
                // Throw a java exception
                jclass newExcCls = env->FindClass("java/io/IOException");
                env->ThrowNew(newExcCls, "SSL_new failed");
                return;
        }

        SSL_set_accept_state(ssl);

        if (!SSL_set_fd (ssl, sd)) {
                // Throw a java exception
                jclass newExcCls = env->FindClass("java/io/IOException");
                env->ThrowNew(newExcCls, "SSL_set_fd failed");
                return;
        }
        
        // See if there was an error in the SSL negotition
        if (SSL_accept(ssl) < 0) {
                // Throw a java exception
                jclass newExcCls = env->FindClass("java/io/IOException");
                env->ThrowNew(newExcCls, "SSL negotitation failed");
                return;
        }

        SSL_renegotiate(ssl);

        printf("SSL State : %s\n",SSL_state_string_long(ssl) );
        printf ("SSL connection using %s\n", SSL_get_cipher (ssl));

        // Turn on read ahead for better performance
        SSL_set_read_ahead(ssl, 1);             // No return code
        
        // Store the ctx pointer
        fid = env->GetFieldID(env->GetObjectClass(obj), "ctx", "I");
        env->SetIntField(obj, fid, (int) ctx);
        
        // Store SSL pointer
        fid = env->GetFieldID(env->GetObjectClass(obj), "ssl", "I");
        env->SetIntField(obj, fid, (int) ssl);
        
        // Store socket file descriptor
        fid = env->GetFieldID(env->GetObjectClass(obj), "sfd", "I");
        env->SetIntField(obj, fid, (int) sd);
        
        // Release the native version of java data types
        env->ReleaseStringUTFChars(CertFile, CertFileCS);
        env->ReleaseStringUTFChars(KeyFile, KeyFileCS);

}




_______________________________________________________
Get your free, private email at http://mail.excite.com/
______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
Development Mailing List                       [EMAIL PROTECTED]
Automated List Manager                           [EMAIL PROTECTED]

Reply via email to