Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / Security / November 2006

Tip: Looking for answers? Try searching our database.

Beware! -> (code)bugs in the "Java Developers Almanac"

Thread view: 
Lion-O - 24 Nov 2006 00:39 GMT
Sometimes I wonder; don't people /read/ API docs? I guess not..

I'm currently in the process to build a webapplication for online
payment and naturally security and encryption will become a major deal
for the whole project. Many examples show you how you need to use the
"-Djavax.net.ssl.trustStore=<keystore file>" java parameter in order to
load your JVM with a keystore to trust. Or they simply set the JVM
system property 'javax.net.ssl.trustStore'
(java.lang.System.setProperty)) to change this value.

I don't want that, my goal was to allow a user to dump a plain
certificate file (plain ASCII file in PEM format) which is then parsed
by the webapplication and used to secure the actual https connection. If
the certificate matches all is well, and if not..  etc.

Although I can find my way around the API docs quite well peeking at
some examples never hurt and so I came across the 'Java Developers
Almanac' website (http://javaalmanac.com/) featuring free example code.

People: BEWARE!!   The author seems unable to read the API docs himself
and as such certain code contains nasty bugs who's cause I can only
conclude to be plain 'PEBCAK' (Problem Exists Between Chair And
Keyboard).

Example...  When working with https its rather easy to use the
'HttpsURLConnection' class which makes working with https enabled
websites a breeze. The only possible "problem" might be the TrustManager
which demands that the "authorizing certificate" (the CA certificate
which tells you that you can trust the other party) needs to be known
somehow. In order to overcome this they show you how to create your own
trustmanager and then assign it to the used HttpsURLConnection.

They do this by setting up an SSLContext, then initializing this using
the new truststore and finally load up the HttpsURLConnection with the
SSLSocketFactory through the SSLContext mentioned earlier.

HOWEVER...  Look at this code example (source:
http://javaalmanac.com/egs/javax.net.ssl/TrustAll.html):

 // Install the all-trusting trust manager
   try {
       SSLContext sc = SSLContext.getInstance("SSL");
       sc.init(null, trustAllCerts, new java.security.SecureRandom());
       HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
   } catch (Exception e) {
   }
   
   // Now you can access an https URL without having the certificate in
   // the truststore
   try {
       URL url = new URL("https://hostname/index.html");
   } catch (MalformedURLException e) {
   }

Notice the use of the setDefaultSSLSocketFactory() method...  Now read
the API docs for javax.net.ssl.HttpsURLConnection and you'll come
across: "setDefaultSSLSocketFactory - 'Sets the default SSLSocketFactory
inherited by new instances of this class.'".

Reading the description of the method these folks /should/ be using
(setSSLSocketFactory) shows you: "Sets the SSLSocketFactory to be used
when this instance creates sockets for secure https URL connections.".

I don't get it..  Is it really /that/ hard to RTFAD (Read The 'Fine' API
Documentation) ?

Well, I hope I might help some people with this....

Signature

Groetjes, Peter

.\\ PGP/GPG key: http://www.catslair.org/pubkey.asc

sgoo - 24 Nov 2006 12:29 GMT
>         HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());

I don't think there's any wrong with the code in the book. Here,
setDefaultSSLSocketFactory is a *static* method that can be called on
the class name. If you want to call the none static method
setSSLSocketFactory, you need an object (aka class instance). Where is
that object?

I have no idea how JRE handles an https URL inside. It seems at some
phase an object of the HttpsURLConnection type will be created to the
real connection things, and this is exactly what the doc calls "new
instances of this class".

Goo
Lion-O - 25 Nov 2006 21:08 GMT
>>         HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
>
> I don't think there's any wrong with the code in the book. Here,
> setDefaultSSLSocketFactory is a *static* method that can be called on
> the class name.

The setDefaultSSLSocketFactory "Sets the default SSLSocketFactory
inherited by new instances of this class.".  Notice how
javax.net.ssl.HttpsURLConnection is an abstract class with a protected
constructor?  You don't simply "instantiate" it.

In the book example they define a reference to it through use of the
java.net.URL.openConnection() method and casting its result to a
HttpsURLConnection object. This automaticly implies that all further
operations will be using the /current/ object and not any optional new
instances.

Therefor you need setSSLSocketFactory() because the settings need to
apply to new sockets created by the current instance.

Signature

Groetjes, Peter

.\\ PGP/GPG key: http://www.catslair.org/pubkey.asc

sgoo - 26 Nov 2006 01:17 GMT
> >>         HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
>
[quoted text clipped - 4 lines]
> javax.net.ssl.HttpsURLConnection is an abstract class with a protected
> constructor?  You don't simply "instantiate" it.

I believe this includes new instances of child classes of
HttpsURLConnection, which can be instantiated.

> In the book example they define a reference to it through use of the
> java.net.URL.openConnection() method and casting its result to a
> HttpsURLConnection object. This automaticly implies that all further
> operations will be using the /current/ object and not any optional new
> instances.

I don't see openConnection() called in the book example. Therefore I
think the example shows that by calling
HttpsURLConnection.setDefaultSSLSocketFactory(...) all URLs created
later can "automagically" use the new TrustManager. Maybe you can use
openConnection() and call setSSLSocketFactory(...) on the object you
get. That's another topic. It doesn't mean this example is incorrect.

> Therefor you need setSSLSocketFactory() because the settings need to
> apply to new sockets created by the current instance.
[quoted text clipped - 3 lines]
>
> .\\ PGP/GPG key:http://www.catslair.org/pubkey.asc
Lion-O - 26 Nov 2006 02:15 GMT
>> Notice how javax.net.ssl.HttpsURLConnection is an abstract class with
>> a protected constructor?  You don't simply "instantiate" it.

> I believe this includes new instances of child classes of
> HttpsURLConnection, which can be instantiated.

You are right, but thats not what the book is using hence my comment.

> I don't see openConnection() called in the book example. Therefore I
> think the example shows that by calling
> HttpsURLConnection.setDefaultSSLSocketFactory(...)

Thats incorrect. This method does not open a connection.

And once again, the last time I will repeat this, opening a connection
applies to the /current/ instance.

Signature

Groetjes, Peter

.\\ PGP/GPG key: http://www.catslair.org/pubkey.asc

sgoo - 26 Nov 2006 02:38 GMT
What I mean is, there are 2 ways:

1. Call HttpsURLConnection.setDefaultSSLSocketFactory(...), and all
HTTPS URL created later can go on
2. Call
((HttpsURLConnection)(myURL.getConnection())).setSSLSocketFactory(...)
and /this/ URL can go on

Either works. You just cannot say the first is wrong if you prefer the
second one.
jeff.lanzarotta@gmail.com - 28 Nov 2006 19:41 GMT
Interesting that you are building a payment application, as I am
also...  I have been really struggling over this SSL stuff... Is there
any good references/examples you can point me to?

> Sometimes I wonder; don't people /read/ API docs? I guess not..
>
[quoted text clipped - 68 lines]
>
> .\\ PGP/GPG key: http://www.catslair.org/pubkey.asc
jeff.lanzarotta@gmail.com - 28 Nov 2006 19:44 GMT
The processor that I am using gave me a URL 'https://xxx.yyy.zzz' to
connect to and a port number. I have had no luck in finding a way to
connect to the URL and then to this specific port... Any ideas?

> Sometimes I wonder; don't people /read/ API docs? I guess not..
>
[quoted text clipped - 68 lines]
>
> .\\ PGP/GPG key: http://www.catslair.org/pubkey.asc


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.