Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
New Developer
mike58
Posts: 9
Registered: ‎04-29-2009
My Device: Not Specified

Re: Problem with SecureConnection

how do you know in Wireshark that a specific number of plaintext bytes (say exactly 3) are available when you actually eavesdrop on an end-to-end secured SSL connection where you only see the ciphertext?

 

I don't, really. All my testing with Wireshark has been done on non-SSL connections. At the moment, I'm just trying to get this to work without using the 'available()' method.

 

Try using streams returned by openInputStream and openOutputStream instead of openDataInputStream/openDataOutputStream just to see whether it makes a difference (although it shouldn't).

 

I already tried that before, but did it again just in case; no difference.

 

Also, try opening the connection in read+write mode and with connection and read timeouts enabled ( Connector.open(url, Connector.READ_WRITE, true) .

 

Just tried that, had high hopes for this one 'cause it was something I hadn't done before, but unfortunately no difference.

 

I'm really trying to simplify my code to get down to the cause of the problem. The SSL part was just because the 'available()' method doesn't work with SSL, so now I'm trying to make it work without SSL and without using 'available()'.  Very simple code. After opening the connection:

byte[] in = new byte[10];
while (connected) {

// if I comment this out, available() returns 3 the first time around

// try { Thread.sleep(100); }

// catch (InterruptedException ex) { /* do nothing */ }

 

int av = is.available(); // just for fun, returns 0 the first time around
int rd = is.read(in, 0, 10); // this waits FOREVER
}

 

 

If I change it to this:

byte[] in = new byte[10];
while (connected) {
int av = is.available(); // returns 0 the first time around, 3 the second time
if (av > 0)
int rd = is.read(in, 0, 10); // still waits FOREVER
}

 

 

and finally, this works, but...

byte[] in = new byte[10];
while (connected) {
int av = is.available(); // returns 0 the first time around, 3 the second time
if (av > 0)
int rd = is.read(in, 0, av); // that reads the 3 available bytes
}

 


BTW, I'm using the 4.3.0 JDE on the 8120 simulator with Eclipse if that matters.

 

So how can I make this work, without using 'available()' to find out how much is waiting to be read?

 

Thanks, Mike

 

 

 

Developer
Posts: 1,474
Registered: ‎04-14-2009
My Device: Not Specified

Re: Problem with SecureConnection

I'm wondering whether this could be a server-side rather than a BlackBerry-side issue. As I mentioned earlier, try stunnel + Netcat: point stunnel in client mode to the SSL/TLS port of the telnet server, then connect to the local stunnel endpoint via Netcat (nc). You'll then see all the plaintext traffic the server sends and will be able to send plaintext traffic to the server via this SSL channel created by stunnel. Another thing to try is to use a newer simulator/device, say 4.6.0 or higher -- just to make sure it's not a 4.3.0-specific issue.

 

Actually, since you support a plaintext and a secure (SSL/TLS) connection to the server, how is the server accepting SSL/TLS connections? Is it using its own libraries or stunnel in server mode (or something similar)?

Developer
Berek
Posts: 97
Registered: ‎06-01-2009
My Device: Not Specified

Re: Problem with SecureConnection

I'm having the same problem with direct tcp and not using SSL. The read(byte[]) blocks even when data is available. Anyone have a solution for this?
New Developer
mike58
Posts: 9
Registered: ‎04-29-2009
My Device: Not Specified

Re: Problem with SecureConnection

Berek

Are you checking the number of bytes available on the input stream before calling 'read'? I have the same problem if I try to read more data than is available, but it works if I call 'available()' first, and then 'read' with the result returned from 'available()' if it is greater than zero.

I still couldn't get it work that way for SSL connections though.

HTH, Mike

Developer
Berek
Posts: 97
Registered: ‎06-01-2009
My Device: Not Specified

Re: Problem with SecureConnection

That's exactly what I did. Although the BB documentation insists available() does not work and always returns zero. I added my code to this thread in a message this morning, but it seems it's not here now. I'll add it again.
Developer
Berek
Posts: 97
Registered: ‎06-01-2009
My Device: Not Specified

Re: Problem with SecureConnection

This code works:

 

    public void run()
    {
        int nBytesRead = 0;
        int nBytesAvail = 0;

        int nNbrSleeps = 0;

        try {

            while (true) {

                try {

                    for (nNbrSleeps = 0;
                         nNbrSleeps < MAX_SLEEPS;
                         nNbrSleeps++) {

                        nBytesAvail = m_isRdrStream.available();

                        if (nBytesAvail == 0) {

                            Thread.sleep(BYTES_AVAIL_DELAY);

                        } else {

                            break; // for
                        }
                    }
                }
                catch (InterruptedException exIntExec) {

                    exIntExec.printStackTrace();
                }
                if (nNbrSleeps == MAX_SLEEPS) {

                    break; // while
                }
                // Get bytes available again to narrow the window for error.
                nBytesAvail = m_isRdrStream.available();

                if (nBytesAvail > 0
                &&  nBytesAvail != m_ayReadBuf.length) {

                    m_ayReadBuf = new byte[nBytesAvail];
                }
                nBytesRead = m_isRdrStream.read(m_ayReadBuf);

                if (nBytesRead == END_OF_DATA) {

                    break; // while
                }
                if (nBytesRead > 0) {

                    writeOutput(m_ayReadBuf, nBytesRead);

                    nBytesRead = 0;

                    // Zero buffer for debugging purposes only.
                    for (int nBufNdx = 0;
                         nBufNdx < m_ayReadBuf.length;
                         nBufNdx++)
                    {
                        m_ayReadBuf[nBufNdx] = 0x00;

                    } // for (...)
                }
            } // while (true)
        }
        catch (IOException ioExcp) {

            ioExcp.printStackTrace();

            m_zIoExcpMsg = ioExcp.getMessage();
        }
        finally {

            // If we have any data remaining in the buffer...
            if (nBytesRead > 0) {

                writeOutput(m_ayReadBuf, nBytesRead);
            }
            closeOutput(); // close output media

        } // finally

        m_coRdrComplete.coAcquireLock();
        m_coRdrComplete.coSignal();
        m_coRdrComplete.coReleaseLock();

        return;

    } // run()