UPDATE 1/26/2015 -- It appears the most recent JRE/JDK for Java 8 (update >= 31) and JRE/JDK for Java 7 now include the Godaddy G2 CA server in the default trust st
Update - this "solution" is no longer valid (see my above accepted answer) - keeping this answer because it did help alleviate the problem so long as the side-effects are tolerable.
Ok, I may have found a work-around for my case.
props.put("mail.smtp.ssl.trust", "smtp.somecompany.com");
I added this to my Session construction, and now it works. This is a work-around, not a fix imho since I still do not know why my Godaddy SSL cert is not default trusted... it is not a self-signed cert.
Anyone please feel free to chime in as I'd really like to understand this problem.
UPDATE 1/26/2015 -- It appears the most recent JRE/JDK for Java 8 (update >= 31) and JRE/JDK for Java 7 now include the Godaddy G2 CA server in the default trust store. If possible, it's urged you upgrade your JRE/JDK to the latest Java 8 update to resolve this issue.
UPDATE 11/29/2014 -- This is still a problem, and Godaddy appears to not care nor will do anything about it. There is a blog post[here][1]
by Godaddy VP of Security Products from several months ago saying a fix was on it's way and provided a temporary work-around, but as-of today nothing has changed. It is important to note that Godaddy's G2 CA server has been around for a minimum of 5 years, and in that time Godaddy has not taken the proper steps to resolve this known issue. The work-around provided is just that, a work-around, not a solution. Users of 3rd party services have zero control over how the cert is installed on the server.
It seems users should avoid purchasing Godaddy SSL certs until they get serious about being a CA.
Here is their SSL team's contact info if you feel inclined to call:
GoDaddy SSL Team Support Number: 1-480-505-8852 -- Email: ra@godaddy.com
UPDATE 9/17/2014 -- This is still a problem, and Godaddy appears to not care nor will do anything about it. Come November when Google deprecates all SHA-1 certs, this will become a major issue. I highly recommend anyone who can contact Godaddy and point them here.
~~~~
My initial post/question was regarding why my chain was not working. It became obvious I had a bad setup (which was quickly fixed with some advice from @Bruno and others - thanks). However, when my corrected chain still did not work with Java, it became apparent there was a much bigger problem lurking. It took a while, but the problem is actually with GoDaddy.
This actually is indeed a GoDaddy problem (I've had lengthy support emails with them).
They have 2 CA servers, one called Class 2 CA
and the other called G2 CA
. Their Class 2 CA
signs all SHA-1
certificates, while the G2 CA
signs all their SHA-2
certificates.
This is where the problem lies - GoDaddy has not added their newer G2 CA
server to the default Java truststore/keystore
- causing default Java installations to not trust it's authority, and hence, does not trust your chained certificate.
The work-around until GoDaddy adds the G2 CA
server to the default truststore/keystore is to simply rekey your cert using SHA-1
as-to get a cert signed by the Class 2 CA
server. Rekeying is free for GoDaddy customers until your cert expires (obviously).
Once you have a SHA-1
cert signed by the Class 2 CA
server, your trust chain should work as expected and no custom truststore/keystore imports and/or setup is required.
It does not make me happy that I must use a "weaker" cert in order to get it to work properly, and discussions with GoDaddy via email support thus far have indicated they have no current plans to add the G2 CA
server to the default truststore/keystore. I guess until they do add it, make sure you get a SHA-1
Class 2 CA
server signed cert if you plan to work with Java.
If u are using below properties while sending mail, then comment it. This works for me. But this might cause security problem.
props.put("mail.smtp.starttls.enable","true");
In the "Java Control Panel" I just added the GD Root Certificate to the "Secure Site CA" and I no longer have the cert error when using Java. The cert I added was: Go Daddy Class 2 Certification Authority Root Certificate - G2
To get Godaddy certificates to work in Java with SHA2 you will need to use their cross certificate in your chain to chain the G2(SHA2) root to the G1(SHA1) root until Java decides to update their repository. The Cross Certificate bundle can be downloaded here:
https://certs.godaddy.com/anonymous/repository.pki
GoDaddy Certificate Bundles - G2 With Cross to G1, includes Root
[gd_bundle-g2-g1.crt][1]
Here is what you can try. Add the GoDaddy root and intermediate certificates to trust manager at run time. i.e start if the application.
static final String GD_CERT1 = //"-----BEGIN CERTIFICATE-----" "MIIE0DCCA7igAwIBAgIBBzANBgkqhkiG9w0BAQsFADCBgzELMAkGA1UEBhMCVVMx" +"EDAOBgNVBAgTB0FyaXpvbmExEzARBgNVBAcTClNjb3R0c2RhbGUxGjAYBgNVBAoT" +"EUdvRGFkZHkuY29tLCBJbmMuMTEwLwYDVQQDEyhHbyBEYWRkeSBSb290IENlcnRp" +"ZmljYXRlIEF1dGhvcml0eSAtIEcyMB4XDTExMDUwMzA3MDAwMFoXDTMxMDUwMzA3" +"MDAwMFowgbQxCzAJBgNVBAYTAlVTMRAwDgYDVQQIEwdBcml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KPpH8EsahB4yOd6AzBhRckB4Y9wimPQoZ+YeAEW5p5JYXMP80kWNy"
+"OO7MHAGjHZQopDH2esRU1/blMVgDoszOYtuURXO1v0XJJLXVggKtI3lpjbi2Tc7P"
+"TMozI+gciKqdi0FuFskg5YmezTvacPd+mSYgFFQlq25zheabIZ0KbIIOqPjCDPoQ"
+"HmyW74cNxA9hi63ugyuV+I6ShHI56yDqg+2DzZduCLzrTia2cyvk0/ZM/iZx4mER"
+"dEr/VxqHD3VILs9RaRegAhJhldXRQLIQTO7ErBBDpqWeCtWVYpoNz4iCxTIM5Cuf"
+"ReYNnyicsbkqWletNw+vHX/bvZ8=";
//+"-----END CERTIFICATE-----";
public static void main(String[] args) throws Exception {
TrustManagerFactory dtmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
dtmf.init((KeyStore) null); // gets you the default trust manager
X509TrustManager defaultTm = null;
for (TrustManager tm : dtmf.getTrustManagers())
{
if (tm instanceof X509TrustManager)
{
defaultTm = (X509TrustManager) tm;
break;
}
}
CertificateFactory cf = CertificateFactory.getInstance("X.509");
byte [] decoded = Base64.getDecoder().decode(GD_CERT1);
ByteArrayInputStream in = new ByteArrayInputStream(decoded);
Certificate ca1 = cf.generateCertificate(in);
in.close();
decoded = Base64.getDecoder().decode(GD_CERT2);
in = new ByteArrayInputStream(decoded);
Certificate ca2 = cf.generateCertificate(in);
in.close();
decoded = Base64.getDecoder().decode(GD_CERT3);
in = new ByteArrayInputStream(decoded);
Certificate ca3 = cf.generateCertificate(in);
in.close();
String keyStoreType = KeyStore.getDefaultType();
KeyStore ks = KeyStore.getInstance(keyStoreType);
ks.load(null, null);
ks.setCertificateEntry("cert1", ca1);
ks.setCertificateEntry("cert2", ca2);
ks.setCertificateEntry("cert3", ca3);
TrustManagerFactory gdtmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
gdtmf.init(ks);
X509TrustManager gdTm = null;
for (TrustManager tm : gdtmf.getTrustManagers())
{
if (tm instanceof X509TrustManager)
{
gdTm = (X509TrustManager) tm;
break;
}
}
TrustManager tms[] = new TrustManager[2];
tms[0] = gdTm;
tms[1] = defaultTm;
try
{
SSLContext sslCtx = SSLContext.getInstance("TLS");
sslCtx.init(null, tms, new SecureRandom());
}
catch (java.security.GeneralSecurityException e)
{
e.printStackTrace();
throw e;
}
HttpsURLConnection.setDefaultSSLSocketFactory(sslCtx.getSocketFactory());
}
I copied the coded from my working version. so there might be complication error. you just need to work through those.