<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title>Answers to: Can I accept self signed certificates in REST requests?</title><link>https://answers.axonivy.com/questions/2833/can-i-accept-self-signed-certificates-in-rest-requests</link><description>&lt;p&gt;In my project I am forced to use a third party REST service which is only accessible over HTTPS. Unfortunately the certificate is self signed. What can I do to accept untrusted certifticates?&lt;/p&gt;</description><atom:link href="https://answers.axonivy.com/questions/2833/can-i-accept-self-signed-certificates-in-rest-requests" rel="self"></atom:link><language>en</language><lastBuildDate>Tue, 29 Aug 2017 07:56:42 -0400</lastBuildDate><item><title>Answer by Reguel Wermelinger</title><link>https://answers.axonivy.com/questions/2833/can-i-accept-self-signed-certificates-in-rest-requests/2834</link><description>&lt;p&gt;&lt;strong&gt;Ivy Version 7.0.2 and later&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The REST Client respects the well-known ivy ssl settings. Enable your custom truststore and import the self signed certificat:&lt;/p&gt;
&lt;p&gt;&lt;a href="https://developer.axonivy.com/doc/latest/DesignerGuideHtml/ivy.introduction.html#ivy-introduction-preferences-sslclient"&gt;https://developer.axonivy.com/doc/latest/DesignerGuideHtml/ivy.introduction.html#ivy-introduction-preferences-sslclient&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Ivy Version 7.0.1 and before&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;You have multiple options:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Put the certificate into the engines trustStore&lt;/li&gt;
&lt;li&gt;Use a custom connector&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Put the certificate into the engines trustStore&lt;/strong&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;import the certificate into the trusstore of the engine/designer under &lt;code&gt;configuration/truststore.jks&lt;/code&gt;. I recommend to use something like the &lt;code&gt;KeyStore explorer&lt;/code&gt; to do this. &lt;img alt="alt text" src="https://answers.axonivy.com/upfiles/truststore.jks_withMyOwnZert.png"&gt;&lt;/li&gt;
&lt;li&gt;activate the keystore globally for the JVM by setting system properties. You could do this either in a script/java call before the REST request is executed or in the &lt;code&gt;Axon.ivy Designer.ini&lt;/code&gt; respectively the &lt;code&gt;*.ilc&lt;/code&gt; file of the engine.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;javax.net.ssl.trustStore=configuration/truststore.jks
javax.net.ssl.trustStorePassword=changeit
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img alt="alt text" src="https://answers.axonivy.com/upfiles/axonIvyDesignerIni_trustStoreProps.png"&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Use a custom connector which allows unsecure hosts:&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Test with Axon.ivy 6.7.1. Currently this is a painful workaround.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;add the &lt;a href="https://mvnrepository.com/artifact/org.glassfish.jersey.connectors/jersey-apache-connector/2.25.1"&gt;jersey-apache-connector.jar&lt;/a&gt; to the &lt;code&gt;/dropins&lt;/code&gt; directory of your product&lt;/li&gt;
&lt;li&gt;copy the &lt;a href="https://mvnrepository.com/artifact/org.glassfish.jersey.connectors/jersey-apache-connector/2.25.1"&gt;jersey-apache-connector.jar&lt;/a&gt; plus the &lt;a href="http://central.maven.org/maven2/org/glassfish/jersey/core/jersey-common/2.25.1/jersey-common-2.25.1.jar"&gt;jersey-commons.jar&lt;/a&gt; and the &lt;a href="http://central.maven.org/maven2/org/glassfish/jersey/bundles/repackaged/jersey-guava/2.25.1/jersey-guava-2.25.1.jar"&gt;jersey-guava.jar&lt;/a&gt; into your project as well. And add them to the classpath. &lt;img alt="alt text" src="https://answers.axonivy.com/upfiles/libsForApacheConnector_UWPD9rq.png"&gt;&lt;/li&gt;
&lt;li&gt;enable the apache-connector-provider via SPI. &lt;/li&gt;
&lt;li&gt;-- Create a file: /src/META-INF/services/org.glassfish.jersey.client.spi.ConnectorProvider&lt;/li&gt;
&lt;li&gt;-- In it paste the full qualified name of the connector: &lt;code&gt;org.glassfish.jersey.apache.connector.ApacheConnectorProvider&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;img alt="alt text" src="https://answers.axonivy.com/upfiles/connectorProviderSPI_nxa9U0o.png"&gt;&lt;/li&gt;
&lt;li&gt;add a java class that configures the apache-connector to allow any certificate by any host (see below)&lt;/li&gt;
&lt;li&gt;use &lt;code&gt;CustomRest.unsecureClient("myRestSErviceName")&lt;/code&gt; as entry point for your REST requests. &lt;pre&gt;&lt;code&gt;package com.axonivy.connectivity;

import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import javax.ws.rs.client.WebTarget;

import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.HttpClientConnectionManager;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.conn.BasicHttpClientConnectionManager;
import org.glassfish.jersey.apache.connector.ApacheConnectorProvider;

import ch.ivyteam.ivy.environment.Ivy;

public class CustomRest {

    public static WebTarget unsecureClient(String serviceName)
    {
        Thread th = Thread.currentThread();
        ClassLoader oldCtl = th.getContextClassLoader();
        try
        {
            th.setContextClassLoader(ApacheConnectorProvider.class.getClassLoader());

            HttpClientConnectionManager conMan = new BasicHttpClientConnectionManager(getConnectionRegistry(), null, null, null);
            return Ivy.rest().client(serviceName)
                    .property("jersey.config.apache.client.connectionManager", conMan);

        }
        finally
        {
            th.setContextClassLoader(oldCtl);
        }
    }

    private static Registry&amp;lt;ConnectionSocketFactory&amp;gt; getConnectionRegistry() {
        SSLContext ctxt = createTrustAllContext();
        HostnameVerifier verifyAllHosts = new HostnameVerifier() {
            @Override
            public boolean verify(String hostname, SSLSession session) {
                return true;
            }
        };
        SSLConnectionSocketFactory factory = new org.apache.http.conn.ssl.SSLConnectionSocketFactory(ctxt, verifyAllHosts);

        return RegistryBuilder.&amp;lt;ConnectionSocketFactory&amp;gt;create()
                .register("http", PlainConnectionSocketFactory.getSocketFactory())
                .register("https", factory)
                .build();
    }

    private static SSLContext createTrustAllContext() {
        TrustManager[] certs = new TrustManager[]{new X509TrustManager() {
            @Override
            public X509Certificate[] getAcceptedIssuers() {
                return null;
            }

            @Override
            public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
            }

            @Override
            public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
            }

        }};

        SSLContext ctx = null;
        try {
            ctx = SSLContext.getInstance("TLS");
            ctx.init(null, certs, new SecureRandom());
        } catch (java.security.GeneralSecurityException e) {
            throw new RuntimeException("failed to setup insecure ssl context", e);
        }
        return ctx;
    }

}
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;/ul&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Reguel Wermelinger</dc:creator><pubDate>Tue, 29 Aug 2017 07:56:42 -0400</pubDate><guid>https://answers.axonivy.com/questions/2833/can-i-accept-self-signed-certificates-in-rest-requests/2834</guid></item></channel></rss>