|
| 1 | +import org.java_websocket.WebSocketImpl; |
| 2 | +import org.java_websocket.server.DefaultSSLWebSocketServerFactory; |
| 3 | + |
| 4 | +import javax.net.ssl.KeyManager; |
| 5 | +import javax.net.ssl.KeyManagerFactory; |
| 6 | +import javax.net.ssl.SSLContext; |
| 7 | +import javax.xml.bind.DatatypeConverter; |
| 8 | +import java.io.ByteArrayInputStream; |
| 9 | +import java.io.File; |
| 10 | +import java.io.FileInputStream; |
| 11 | +import java.io.IOException; |
| 12 | +import java.security.KeyFactory; |
| 13 | +import java.security.KeyStore; |
| 14 | +import java.security.NoSuchAlgorithmException; |
| 15 | +import java.security.cert.Certificate; |
| 16 | +import java.security.cert.CertificateException; |
| 17 | +import java.security.cert.CertificateFactory; |
| 18 | +import java.security.cert.X509Certificate; |
| 19 | +import java.security.interfaces.RSAPrivateKey; |
| 20 | +import java.security.spec.InvalidKeySpecException; |
| 21 | +import java.security.spec.PKCS8EncodedKeySpec; |
| 22 | + |
| 23 | + |
| 24 | +/** |
| 25 | + * SSL Example using the LetsEncrypt certificate |
| 26 | + * See https://github.com/TooTallNate/Java-WebSocket/wiki/Getting-a-SSLContext-from-different-sources#getting-a-sslcontext-using-a-lets-encrypt-certificate |
| 27 | + */ |
| 28 | +public class SSLServerLetsEncryptExample { |
| 29 | + |
| 30 | + public static void main( String[] args ) throws Exception { |
| 31 | + WebSocketImpl.DEBUG = true; |
| 32 | + |
| 33 | + ChatServer chatserver = new ChatServer( 8887 ); |
| 34 | + |
| 35 | + SSLContext context = getContext(); |
| 36 | + if( context != null ) { |
| 37 | + chatserver.setWebSocketFactory( new DefaultSSLWebSocketServerFactory( getContext() ) ); |
| 38 | + } |
| 39 | + chatserver.setConnectionLostTimeout( 30 ); |
| 40 | + chatserver.start(); |
| 41 | + |
| 42 | + } |
| 43 | + |
| 44 | + private static SSLContext getContext() { |
| 45 | + SSLContext context; |
| 46 | + String password = "CHANGEIT"; |
| 47 | + String pathname = "pem"; |
| 48 | + try { |
| 49 | + context = SSLContext.getInstance( "TLS" ); |
| 50 | + |
| 51 | + byte[] certBytes = parseDERFromPEM( getBytes( new File( pathname + File.separator + "cert.pem" ) ), "-----BEGIN CERTIFICATE-----", "-----END CERTIFICATE-----" ); |
| 52 | + byte[] keyBytes = parseDERFromPEM( getBytes( new File( pathname + File.separator + "privkey.pem" ) ), "-----BEGIN PRIVATE KEY-----", "-----END PRIVATE KEY-----" ); |
| 53 | + |
| 54 | + X509Certificate cert = generateCertificateFromDER( certBytes ); |
| 55 | + RSAPrivateKey key = generatePrivateKeyFromDER( keyBytes ); |
| 56 | + |
| 57 | + KeyStore keystore = KeyStore.getInstance( "JKS" ); |
| 58 | + keystore.load( null ); |
| 59 | + keystore.setCertificateEntry( "cert-alias", cert ); |
| 60 | + keystore.setKeyEntry( "key-alias", key, password.toCharArray(), new Certificate[]{ cert } ); |
| 61 | + |
| 62 | + KeyManagerFactory kmf = KeyManagerFactory.getInstance( "SunX509" ); |
| 63 | + kmf.init( keystore, password.toCharArray() ); |
| 64 | + |
| 65 | + KeyManager[] km = kmf.getKeyManagers(); |
| 66 | + |
| 67 | + context.init( km, null, null ); |
| 68 | + } catch ( Exception e ) { |
| 69 | + context = null; |
| 70 | + } |
| 71 | + return context; |
| 72 | + } |
| 73 | + |
| 74 | + private static byte[] parseDERFromPEM( byte[] pem, String beginDelimiter, String endDelimiter ) { |
| 75 | + String data = new String( pem ); |
| 76 | + String[] tokens = data.split( beginDelimiter ); |
| 77 | + tokens = tokens[1].split( endDelimiter ); |
| 78 | + return DatatypeConverter.parseBase64Binary( tokens[0] ); |
| 79 | + } |
| 80 | + |
| 81 | + private static RSAPrivateKey generatePrivateKeyFromDER( byte[] keyBytes ) throws InvalidKeySpecException, NoSuchAlgorithmException { |
| 82 | + PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec( keyBytes ); |
| 83 | + |
| 84 | + KeyFactory factory = KeyFactory.getInstance( "RSA" ); |
| 85 | + |
| 86 | + return ( RSAPrivateKey ) factory.generatePrivate( spec ); |
| 87 | + } |
| 88 | + |
| 89 | + private static X509Certificate generateCertificateFromDER( byte[] certBytes ) throws CertificateException { |
| 90 | + CertificateFactory factory = CertificateFactory.getInstance( "X.509" ); |
| 91 | + |
| 92 | + return ( X509Certificate ) factory.generateCertificate( new ByteArrayInputStream( certBytes ) ); |
| 93 | + } |
| 94 | + |
| 95 | + private static byte[] getBytes( File file ) { |
| 96 | + byte[] bytesArray = new byte[( int ) file.length()]; |
| 97 | + |
| 98 | + FileInputStream fis = null; |
| 99 | + try { |
| 100 | + fis = new FileInputStream( file ); |
| 101 | + fis.read( bytesArray ); //read file into bytes[] |
| 102 | + fis.close(); |
| 103 | + } catch ( IOException e ) { |
| 104 | + e.printStackTrace(); |
| 105 | + } |
| 106 | + return bytesArray; |
| 107 | + } |
| 108 | +} |
0 commit comments