# SSL Java example

**URL:** <https://forum.microej.com/t/ssl-java-example/252>\
**Category:** Technical Support\
**Tags:** sdk\
**Created:** [June 12, 2018, 9:05am UTC](https://forum.microej.com/t/ssl-java-example/252 "2018-06-12T09:05:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![tomdu85](https://forum.microej.com/letter_avatar_proxy/v4/letter/t/ed655f/32.png) [@tomdu85](https://forum.microej.com/u/tomdu85)\
**Post date:** [June 12, 2018, 9:05am UTC](https://forum.microej.com/t/ssl-java-example/252/1 "2018-06-12T09:05:40Z")

</div>

Hi

I need to do a secure socket connection.  
I use the SSL-2.0-api.jar library

This is my sample code :

```
		CertificateFactory certificateFactory = null;
		certificateFactory = CertificateFactory.getInstance("X.509");
		ByteArrayInputStream rootFile = new ByteArrayInputStream(sRootCert.getBytes());
		ByteArrayInputStream clientFile = new ByteArrayInputStream(sClientCert.getBytes());
		X509Certificate certificat_root = (X509Certificate) certificateFactory.generateCertificate(rootFile);
		X509Certificate certificat_client = (X509Certificate) certificateFactory.generateCertificate(clientFile);

		TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
		KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
		ks.setCertificateEntry("CA_CERT", certificat_root);
		ks.setCertificateEntry("client", certificat_client);

		tmf.init(ks);

		SSLContext sc = SSLContext.getInstance("TLS");
		sc.init(null, tmf.getTrustManagers(), null);

		SSLSocketFactory ssf = sc.getSocketFactory();
		s = (SSLSocket) ssf.createSocket(HOST_CLIENT, PORT_CLIENT);
		s.startHandshake();

```

I just want to add my certificats.  
I’ve got a CertificateException : “X.509 not found” (certificateFactory = CertificateFactory.getInstance(“X.509”)😉

Is it normal ?

Thanks

---

<div class="post-metadata">

**Author:** ![pierre.rivron](https://forum.microej.com/user_avatar/forum.microej.com/pierre.rivron/32/46_2.png) [@pierre.rivron](https://forum.microej.com/u/pierre.rivron)\
**Post date:** [June 13, 2018, 6:10am UTC](https://forum.microej.com/t/ssl-java-example/252/2 "2018-06-13T06:10:35Z")

</div>

Hi @tomdu85,

This is a compatibility issue with ssl-api-2.0.4, we’ll try to fix it on the next versions.  
As a workaround, could you use:  
certificateFactory = CertificateFactory.getInstance(“X509”);

Regards,

---

<div class="post-metadata">

**Author:** ![tomdu85](https://forum.microej.com/letter_avatar_proxy/v4/letter/t/ed655f/32.png) [@tomdu85](https://forum.microej.com/u/tomdu85)\
**Post date:** [June 13, 2018, 9:02am UTC](https://forum.microej.com/t/ssl-java-example/252/3 "2018-06-13T09:02:12Z")

</div>

Ok Thanks. That was my problem !

Regards
