Skip to content

Instantly share code, notes, and snippets.

@qnoid
Created September 10, 2011 09:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save qnoid/1208155 to your computer and use it in GitHub Desktop.
Save qnoid/1208155 to your computer and use it in GitHub Desktop.
Provides access to secure url connections; given certificate as specified by java.security.cert.CertificateFactory#generateCertificate
/*
* This file is under the license Attribution-ShareAlike 3.0 Unported
* (CC BY-SA 3.0) http://creativecommons.org/licenses/by-sa/3.0/
*
* You are free:
* to Share - to copy, distribute and transmit the work
* to Remix - to adapt the work
*
* Under the following conditions:
*
* Attribution - You must attribute the work in the manner specified by the
* author or licensor (but not in any way that suggests that they endorse you
* or your use of the work).
*
* Share Alike - If you alter, transform, or build upon this work, you may
* distribute the resulting work only under the same or similar license to
* this one.
*
* With the understanding that:
*
* Waiver - Any of the above conditions can be waived if you get permission
* from the copyright holder.
*
* Public Domain - Where the work or any of its elements is in the public
* domain under applicable law, that status is in no way affected by the
* license.
*
* Other Rights - In no way are any of the following rights affected by the
* license:
* Your fair dealing or fair use rights, or other applicable copyright
* exceptions and limitations;
*
* The author's moral rights;
* Rights other persons may have either in the work itself or in how the work
* is used, such as publicity or privacy rights.
*
*/
package com.forrst.java.twKo;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.charset.Charset;
import java.security.KeyStore;
import java.security.cert.Certificate;
import java.security.cert.CertificateFactory;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManagerFactory;
/**
* @author Markos Charatzas [http://forrst.me/Cue]
*
* @date Sep 10, 2011
*/
public final class HttpsURLConnectionFactory
{
/*
*
*/
private static SSLSocketFactory newSSLSocketFactory(InputStream certificateStream)
{
try {
CertificateFactory cf = CertificateFactory.getInstance("X.509");
Certificate certificate = cf.generateCertificate(certificateStream);
KeyStore keystore =
KeyStore.getInstance( KeyStore.getDefaultType() );
keystore.setCertificateEntry("foo", certificate);
String defaultAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
TrustManagerFactory tmf =
TrustManagerFactory.getInstance(defaultAlgorithm);
tmf.init(keystore);
SSLContext ctx = SSLContext.getInstance("TLS");
ctx.init(null, tmf.getTrustManagers(), null);
return ctx.getSocketFactory();
}
catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
*
* @param certificate the full certificate as String. Needs to be UTF-8 encoded.
* @param urlScheme the full url e.g. https://www.example.com
* @param type method type of {GET, POST}
* @return an established {@link HttpsURLConnection} ready to read from
* @throws IOException if the urlsceme is malformed or there is an error
* opening the connection
*/
public static HttpsURLConnection newSecureHttpConnection(String certificate, String urlScheme, String type) throws IOException
{
InputStream certificateStream =
new ByteArrayInputStream(certificate.getBytes(Charset.forName("UTF-8")));
SSLSocketFactory sslFactory = newSSLSocketFactory(certificateStream);
URL url = new URL(urlScheme);
HttpsURLConnection conn = (HttpsURLConnection)url.openConnection();
conn.setSSLSocketFactory(sslFactory);
conn.setRequestMethod(type);
return conn;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment