While talking to Ge0rg about latest issues in Java TLS we stumbled upon the question whether the JavaMail API would have similar problems.
Naturally one would expect that Java’s SSL implementation is secure. However, this is not the case: Special care needs to be taken regarding Man-In-The-Middle attacks: While a certificate may turn out to be valid, you cannot be sure that it has the right origin!
The problem is known for a while and library maintainers are taking steps to avoid it. However, for compatibility reasons those features may need to be turned on.
For JavaMail version 1.5.2 the SSLNOTES.TXT says specifically:
— Server Identity
Check RFC 2595 specifies addition checks that must be performed on the server’s certificate to ensure that the server you connected to is the server you intended to connect to. This reduces the risk of “man in the middle” attacks. For compatibility with earlier releases of JavaMail, these additional checks are disabled by default. We strongly recommend that you enable these checks when using SSL. To enable these checks, set the “mail..ssl.checkserveridentity” property to “true”.
Here is the thing that most examples forget: You need to switch that feature on!
final Authenticator auth = ... // somewhere in your application final Properties p = new Properties(); // add your JavaMail configuration here // this is implied by the protocol "imaps" p.put("mail.imap.starttls.enable", "true"); // not only check the certificate, but also make sure that we are // connected to the right server. p.put("mail.imap.ssl.checkserveridentity", "true"); try { Session session = Session.getDefaultInstance(p, auth); Store store = session.getStore(); store.connect(); // do something with the store } catch (MessagingException e) { // do something meaningful(!) with the exception } // close the store when you are done
To use SSL at all, you need to turn it on, either by specifying “imaps” in the property mail.store.protocol or by setting mail.imap.starttls.enable to “true”. Replace imap respectively for other protocol suites (e.g. smtp).
Update 2014-08-05: Inserted the Link to Georg’s blog post about latest issues in Java TLS.
I don’t know about TLS can you share some information about TLS?