本文整理汇总了Java中javax.net.ssl.SSLEngine类的典型用法代码示例。如果您正苦于以下问题:Java SSLEngine类的具体用法?Java SSLEngine怎么用?Java SSLEngine使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
SSLEngine类属于javax.net.ssl包,在下文中一共展示了SSLEngine类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: createSSLEngine
import javax.net.ssl.SSLEngine; //导入依赖的package包/类
public static SSLEngine createSSLEngine(SSLOption option, SSLCustom custom) {
SSLContext context = createSSLContext(option, custom);
SSLEngine engine =
context.createSSLEngine();
engine.setEnabledProtocols(option.getProtocols().split(","));
String[] supported = engine.getSupportedCipherSuites();
String[] eanbled = option.getCiphers().split(",");
engine.setEnabledCipherSuites(getEnabledCiphers(supported, eanbled));
engine.setNeedClientAuth(option.isAuthPeer());
return engine;
}
示例2: createSSLEngine
import javax.net.ssl.SSLEngine; //导入依赖的package包/类
protected SSLEngine createSSLEngine() {
SSLEngine engine = sslContext.createSSLEngine();
if ("false".equals(getClientAuth())) {
engine.setNeedClientAuth(false);
engine.setWantClientAuth(false);
} else if ("true".equals(getClientAuth()) || "yes".equals(getClientAuth())) {
engine.setNeedClientAuth(true);
} else if ("want".equals(getClientAuth())) {
engine.setWantClientAuth(true);
}
engine.setUseClientMode(false);
engine.setEnabledCipherSuites(enabledCiphers);
engine.setEnabledProtocols(enabledProtocols);
configureUseServerCipherSuitesOrder(engine);
return engine;
}
示例3: doUnWrap
import javax.net.ssl.SSLEngine; //导入依赖的package包/类
/**
* Unwraps data with the specified engine.
*
* @param engine - SSLEngine that unwraps data.
* @param unwrapper - Set unwrapper id, e.g. "server" of "client".
* Used for logging only.
* @param net - Buffer with data to unwrap.
* @param wantedStatus - Specifies expected result status of wrapping.
* @param result - Array which first element will be used to output
* wrap result object.
* @return - Buffer with unwrapped data.
* @throws SSLException - thrown on engine errors.
*/
public static ByteBuffer doUnWrap(SSLEngine engine, String unwrapper,
ByteBuffer net, SSLEngineResult.Status wantedStatus,
SSLEngineResult[] result) throws SSLException {
ByteBuffer app = ByteBuffer.allocate(
engine.getSession().getApplicationBufferSize());
int length = net.remaining();
System.out.println(unwrapper + " unwrapping " + length + " bytes...");
SSLEngineResult r = engine.unwrap(net, app);
app.flip();
System.out.println(unwrapper + " handshake status is "
+ engine.getHandshakeStatus());
checkResult(r, wantedStatus);
if (result != null && result.length > 0) {
result[0] = r;
}
return app;
}
示例4: testCreateSSLEngine
import javax.net.ssl.SSLEngine; //导入依赖的package包/类
@Test
public void testCreateSSLEngine() {
SSLOption option = SSLOption.build(DIR + "/server.ssl.properties");
SSLCustom custom = new SSLCustom() {
@Override
public String getFullPath(String filename) {
return DIR + "/ssl/" + filename;
}
@Override
public char[] decode(char[] encrypted) {
return encrypted;
}
};
SSLEngine aSSLEngine = SSLManager.createSSLEngine(option, custom);
Assert.assertEquals(false, aSSLEngine.getUseClientMode());
Assert.assertNotNull(aSSLEngine);
}
示例5: testCreateSSLEnginewithPort
import javax.net.ssl.SSLEngine; //导入依赖的package包/类
@Test
public void testCreateSSLEnginewithPort() {
SSLOption option = SSLOption.build(DIR + "/server.ssl.properties");
SSLCustom custom = new SSLCustom() {
@Override
public String getFullPath(String filename) {
return DIR + "/ssl/" + filename;
}
@Override
public char[] decode(char[] encrypted) {
return encrypted;
}
};
int port = 39093;
String peerHost = "host1";
SSLEngine aSSLEngine = SSLManager.createSSLEngine(option, custom, peerHost, port);
Assert.assertNotNull(aSSLEngine);
Assert.assertEquals("host1", aSSLEngine.getPeerHost().toString());
}
示例6: testTrustAllManager
import javax.net.ssl.SSLEngine; //导入依赖的package包/类
@Test
public void testTrustAllManager() throws Exception {
TrustAllManager manager = new TrustAllManager();
manager.checkClientTrusted((X509Certificate[]) null, (String) null);
manager.checkServerTrusted((X509Certificate[]) null, (String) null);
manager.checkClientTrusted((X509Certificate[]) null,
(String) null,
(Socket) null);
manager.checkClientTrusted((X509Certificate[]) null,
(String) null,
(SSLEngine) null);
manager.checkServerTrusted((X509Certificate[]) null,
(String) null,
(Socket) null);
manager.checkServerTrusted((X509Certificate[]) null,
(String) null,
(SSLEngine) null);
Assert.assertEquals(manager.getAcceptedIssuers() == null, true);
}
示例7: initChannel
import javax.net.ssl.SSLEngine; //导入依赖的package包/类
/**
* Adds pipelines to channel.
*
* @param ch channel to be operated on
*/
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipe = ch.pipeline();
if (ssl) {
// HTTPs connection
SSLEngine sslEng = getSsl(null);
sslEng.setUseClientMode(true);
pipe.addLast("SSL", new SslHandler(sslEng, false));
}
pipe.addFirst("Timer", new ReadTimeoutHandler(30));
pipe.addLast("Codec", new HttpClientCodec());
pipe.addLast("Inflater", new HttpContentDecompressor());
pipe.addLast("Handler", new HTTPMessageHandler(builder));
}
示例8: SSLSocketChannel2
import javax.net.ssl.SSLEngine; //导入依赖的package包/类
public SSLSocketChannel2( SocketChannel channel , SSLEngine sslEngine , ExecutorService exec , SelectionKey key ) throws IOException {
if( channel == null || sslEngine == null || exec == null )
throw new IllegalArgumentException( "parameter must not be null" );
this.socketChannel = channel;
this.sslEngine = sslEngine;
this.exec = exec;
readEngineResult = writeEngineResult = new SSLEngineResult( Status.BUFFER_UNDERFLOW, sslEngine.getHandshakeStatus(), 0, 0 ); // init to prevent NPEs
tasks = new ArrayList<Future<?>>( 3 );
if( key != null ) {
key.interestOps( key.interestOps() | SelectionKey.OP_WRITE );
this.selectionKey = key;
}
createBuffers( sslEngine.getSession() );
// kick off handshake
socketChannel.write( wrap( emptybuffer ) );// initializes res
processHandshake();
}
示例9: checkIncorrectAppDataUnwrap
import javax.net.ssl.SSLEngine; //导入依赖的package包/类
private void checkIncorrectAppDataUnwrap(SSLEngine sendEngine,
SSLEngine recvEngine) throws SSLException {
String direction = sendEngine.getUseClientMode() ? "client"
: "server";
System.out.println("================================================="
+ "===========");
System.out.println("Testing DTLS incorrect app data packages unwrapping"
+ " by sending data from " + direction);
ByteBuffer app = ByteBuffer.wrap(MESSAGE.getBytes());
ByteBuffer net = doWrap(sendEngine, direction, 0, app);
final Random RNG = RandomFactory.getRandom();
int randomPlace = RNG.nextInt(net.remaining());
net.array()[randomPlace] += 1;
app = ByteBuffer.allocate(recvEngine.getSession()
.getApplicationBufferSize());
recvEngine.unwrap(net, app);
app.flip();
int length = app.remaining();
System.out.println("Unwrapped " + length + " bytes.");
}
示例10: newChannel
import javax.net.ssl.SSLEngine; //导入依赖的package包/类
@Override
public SocketChannel newChannel(ChannelPipeline pipeline) {
try {
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, new TrustManager[]{new PermissiveTrustManager()},
null);
SSLEngine sslEngine = sslContext.createSSLEngine();
sslEngine.setUseClientMode(true);
// addFirst() will make SSL handling the first stage of decoding
// and the last stage of encoding
pipeline.addFirst("ssl", new SslHandler(sslEngine));
return super.newChannel(pipeline);
} catch (Exception ex) {
throw new RuntimeException("Cannot create SSL channel", ex);
}
}
示例11: doWrap
import javax.net.ssl.SSLEngine; //导入依赖的package包/类
/**
* Wraps data with the specified engine.
*
* @param engine - SSLEngine that wraps data.
* @param wrapper - Set wrapper id, e.g. "server" of "client".
* Used for logging only.
* @param maxPacketSize - Max packet size to check that MFLN extension
* works or zero for no check.
* @param app - Buffer with data to wrap.
* @param wantedStatus - Specifies expected result status of wrapping.
* @param result - Array which first element will be used to output
* wrap result object.
* @return - Buffer with wrapped data.
* @throws SSLException - thrown on engine errors.
*/
public static ByteBuffer doWrap(SSLEngine engine, String wrapper,
int maxPacketSize, ByteBuffer app,
SSLEngineResult.Status wantedStatus,
SSLEngineResult[] result)
throws SSLException {
ByteBuffer net = ByteBuffer.allocate(engine.getSession()
.getPacketBufferSize());
SSLEngineResult r = engine.wrap(app, net);
net.flip();
int length = net.remaining();
System.out.println(wrapper + " wrapped " + length + " bytes.");
System.out.println(wrapper + " handshake status is "
+ engine.getHandshakeStatus());
if (maxPacketSize < length && maxPacketSize != 0) {
throw new AssertionError("Handshake wrapped net buffer length "
+ length + " exceeds maximum packet size "
+ maxPacketSize);
}
checkResult(r, wantedStatus);
if (result != null && result.length > 0) {
result[0] = r;
}
return net;
}
示例12: createSSLEngine
import javax.net.ssl.SSLEngine; //导入依赖的package包/类
protected SSLEngine createSSLEngine() {
SSLEngine engine = sslContext.createSSLEngine();
if ("false".equals(getClientAuth())) {
engine.setNeedClientAuth(false);
engine.setWantClientAuth(false);
} else if ("true".equals(getClientAuth()) || "yes".equals(getClientAuth())){
engine.setNeedClientAuth(true);
} else if ("want".equals(getClientAuth())) {
engine.setWantClientAuth(true);
}
engine.setUseClientMode(false);
engine.setEnabledCipherSuites(enabledCiphers);
engine.setEnabledProtocols(enabledProtocols);
configureUseServerCipherSuitesOrder(engine);
return engine;
}
示例13: SSLSocketChannel2
import javax.net.ssl.SSLEngine; //导入依赖的package包/类
public SSLSocketChannel2(SocketChannel channel, SSLEngine sslEngine, ExecutorService exec, SelectionKey key) throws IOException {
if (channel == null || sslEngine == null || exec == null) {
throw new IllegalArgumentException("parameter must not be null");
}
this.socketChannel = channel;
this.sslEngine = sslEngine;
this.exec = exec;
this.tasks = new ArrayList(3);
if (key != null) {
key.interestOps(key.interestOps() | 4);
this.selectionKey = key;
}
createBuffers(sslEngine.getSession());
this.socketChannel.write(wrap(emptybuffer));
processHandshake();
}
示例14: createSslEngine
import javax.net.ssl.SSLEngine; //导入依赖的package包/类
public SSLEngine createSslEngine(String peerHost, int peerPort) {
SSLEngine sslEngine = sslContext.createSSLEngine(peerHost, peerPort);
if (cipherSuites != null) sslEngine.setEnabledCipherSuites(cipherSuites);
if (enabledProtocols != null) sslEngine.setEnabledProtocols(enabledProtocols);
// SSLParameters#setEndpointIdentificationAlgorithm enables endpoint validation
// only in client mode. Hence, validation is enabled only for clients.
if (mode == Mode.SERVER) {
sslEngine.setUseClientMode(false);
if (needClientAuth)
sslEngine.setNeedClientAuth(needClientAuth);
else
sslEngine.setWantClientAuth(wantClientAuth);
} else {
sslEngine.setUseClientMode(true);
SSLParameters sslParams = sslEngine.getSSLParameters();
sslParams.setEndpointIdentificationAlgorithm(endpointIdentification);
sslEngine.setSSLParameters(sslParams);
}
return sslEngine;
}
示例15: checkBufferOverflowOnUnWrap
import javax.net.ssl.SSLEngine; //导入依赖的package包/类
private void checkBufferOverflowOnUnWrap(SSLEngine wrappingEngine,
SSLEngine unwrappingEngine)
throws SSLException {
String wrapperMode = wrappingEngine.getUseClientMode() ? "client"
: "server";
String unwrapperMode = unwrappingEngine.getUseClientMode() ? "client"
: "server";
if (wrapperMode.equals(unwrapperMode)) {
throw new Error("Test error: both engines are in the same mode!");
}
System.out.println("================================================="
+ "===========");
System.out.println("Testing SSLEngine buffer overflow"
+ " on unwrap by " + unwrapperMode);
ByteBuffer app = ByteBuffer.wrap(MESSAGE.getBytes());
ByteBuffer net = ByteBuffer
.allocate(wrappingEngine.getSession().getPacketBufferSize());
SSLEngineResult r = wrappingEngine.wrap(app, net);
checkResult(r, SSLEngineResult.Status.OK);
//Making app buffer size less than required by 1 byte.
app = ByteBuffer.allocate(MESSAGE.length() - 1);
net.flip();
r = unwrappingEngine.unwrap(net, app);
checkResult(r, SSLEngineResult.Status.BUFFER_OVERFLOW);
System.out.println("Passed");
}