Code Examples For Creating SSL Sockets

Code Examples For Creating SSL Sockets

Note: 
SSLClient extends SSLSocketFactory 
SSLServer extends SSLServerSocketFactory
Client Example:

SSLClient client = new SSLClient();

// Let's trust usual "cacerts" that come with Java.  Plus, let's also trust a self-signed cert
// we know of.  We have some additional certs to trust inside a java keystore file.
client.addTrustMaterial( TrustMaterial.DEFAULT );
client.addTrustMaterial( new TrustMaterial( "/path/to/self-signed.pem" ) );
client.addTrustMaterial( new KeyMaterial( "/path/to/keystore.jks", "changeit".toCharArray() ) );

// To be different, let's allow for expired certificates (not recommended).
client.setCheckHostname( true );  // default setting is "true" for SSLClient
client.setCheckExpiry( false );   // default setting is "true" for SSLClient
client.setCheckCRL( true );       // default setting is "true" for SSLClient

// Let's load a client certificate (max: 1 per SSLClient instance).
client.setKeyMaterial( new KeyMaterial( "/path/to/client.pfx", "secret".toCharArray() ) );
SSLSocket s = (SSLSocket) client.createSocket( "www.cucbc.com", 443 );

Server Example (OpenSSL/Apache Style)
// Compatible with the private key / certificate chain created from following the Apache2
// TLS FAQ: "How do I create a self-signed SSL Certificate for testing purposes?"
// http://httpd.apache.org/docs/2.2/ssl/ssl_faq.html#selfcert

SSLServer server = new SSLServer();

// Server needs some key material.  We'll use an OpenSSL/PKCS8 style key (possibly encrypted).
String certificateChain = "/path/to/this/server.crt";
String privateKey = "/path/to/this/server.key";
char[] password = "changeit".toCharArray();
KeyMaterial km = new KeyMaterial( certificateChain, privateKey, password ); 

server.setKeyMaterial( km );

// These settings have to do with how we'll treat client certificates that are presented
// to us.  If the client doesn't present any client certificate, then these are ignored.
server.setCheckHostname( false ); // default setting is "false" for SSLServer
server.setCheckExpiry( true );    // default setting is "true" for SSLServer
server.setCheckCRL( true );       // default setting is "true" for SSLServer

// This server trusts all client certificates presented (usually people won't present
// client certs, but if they do, we'll give them a socket at the very least).
server.addTrustMaterial( TrustMaterial.TRUST_ALL );
SSLServerSocket ss = (SSLServerSocket) server.createServerSocket( 7443 );
SSLSocket socket = (SSLSocket) ss.accept();

Server Example (Traditional Java "KeyStore" Style)

SSLServer server = new SSLServer();

// Server needs some key material.   We'll use a Java Keystore (.jks) or Netscape
// PKCS12 (.pfx or .p12) file.  Commons-ssl automatically detects the type.
String pathToKeyMaterial = "/path/to/.keystore";
char[] password = "changeit".toCharArray();
KeyMaterial km = new KeyMaterial( pathToKeyMaterial, password ); 

server.setKeyMaterial( km );

// This server trusts all client certificates presented (usually people won't present
// client certs, but if they do, we'll give them a socket at the very least).
server.addTrustMaterial( TrustMaterial.TRUST_ALL );
SSLServerSocket ss = (SSLServerSocket) server.createServerSocket( 7443 );
SSLSocket socket = (SSLSocket) ss.accept();





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值