0

I am trying to clone one bitbucket repo to my local directory through java code. However is am getting Caused by: javax.net.ssl.SSLHandshakeException: java.security.cert.CertificateException: No subject alternative DNS name matching cedt-gct-bitbucket found.

Below is the code for the same.

import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.api.CloneCommand;
    import java.io.File;
import java.nio.file.Paths;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;

import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
public class Checkout {

    private TrustManager[] trustAllCerts;
    private SSLContext sslContext;


    public void setUp() throws Exception {
        trustAllCerts = new TrustManager[]{
          new X509TrustManager() {
            public java.security.cert.X509Certificate[] getAcceptedIssuers() {
              return new X509Certificate[0];
            }
            public void checkClientTrusted(
                java.security.cert.X509Certificate[] certs, String authType) {
            }
            public void checkServerTrusted(
                java.security.cert.X509Certificate[] certs, String authType) {
              if (certs.length != 1 || !certs[0].getIssuerX500Principal().getName()
                  .contains("CN=localhost")) {
                throw new SecurityException("Invalid certificate");
              }
            }
          }
        };
        sslContext = SSLContext.getInstance("TLS");
        sslContext.init(null, trustAllCerts, new SecureRandom());
        }

    public static void main(String[] args) {

        Checkout co = new Checkout();
        try {
            co.setUp();
        } catch (Exception e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        String repoUrl = "<------BITBUCKET REPOSITORY----->; //link for bit repo
        String cloneDirectoryPath = "C:\\Users\\username\\TestFolder"; 
        try {
            System.out.println("Cloning "+repoUrl+" into "+repoUrl);
            Git.cloneRepository()
                .setURI(repoUrl)
                .setDirectory(Paths.get(cloneDirectoryPath).toFile())
                .call();
            System.out.println("Completed Cloning");
        } catch (GitAPIException e) {
            System.out.println("Exception occurred while cloning repo");
            e.printStackTrace();
        }
        }
    }

Could any pls suggest?

Aman
  • 159
  • 2
  • 15
  • Possible duplicate of [How to fix the "java.security.cert.CertificateException: No subject alternative names present" error?](https://stackoverflow.com/questions/19540289/how-to-fix-the-java-security-cert-certificateexception-no-subject-alternative) – f1sh Jun 27 '18 at 11:07
  • 1
    Two comments: Using trust all certs is something which should _never_ be used in production, because it totally defeats the purpose of SSL. Second, are you certain that this API is compatible with Bitbucket. I imagine it would be, but maybe it isn't. – Tim Biegeleisen Jun 27 '18 at 11:08
  • @TimBiegeleisen is there a way around to handle SSL issue? Regarding the Git API, I googled this API. Is there any alternate way? – Aman Jun 27 '18 at 12:07

0 Answers0