Skip to content

Commit

Permalink
Provide a way to access the SSLEngine of a websocket instance
Browse files Browse the repository at this point in the history
  • Loading branch information
marci4 committed May 20, 2019
1 parent 36a8102 commit 1cde8a7
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/main/java/org/java_websocket/SSLSocketChannel.java
Expand Up @@ -25,6 +25,7 @@

package org.java_websocket;

import org.java_websocket.interfaces.ISSLChannel;
import org.java_websocket.util.ByteBufferUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -62,7 +63,7 @@
* <p>
* Permission for usage recieved at May 25, 2017 by Alex Karnezis
*/
public class SSLSocketChannel implements WrappedByteChannel, ByteChannel {
public class SSLSocketChannel implements WrappedByteChannel, ByteChannel, ISSLChannel {

/**
* Logger instance
Expand Down Expand Up @@ -512,4 +513,9 @@ public boolean isOpen() {
public void close() throws IOException {
closeConnection();
}

@Override
public SSLEngine getSSLEngine() {
return engine;
}
}
7 changes: 6 additions & 1 deletion src/main/java/org/java_websocket/SSLSocketChannel2.java
Expand Up @@ -24,6 +24,7 @@
*/
package org.java_websocket;

import org.java_websocket.interfaces.ISSLChannel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -52,7 +53,7 @@
/**
* Implements the relevant portions of the SocketChannel interface with the SSLEngine wrapper.
*/
public class SSLSocketChannel2 implements ByteChannel, WrappedByteChannel {
public class SSLSocketChannel2 implements ByteChannel, WrappedByteChannel, ISSLChannel {

/**
* Logger instance
Expand Down Expand Up @@ -425,4 +426,8 @@ public boolean isBlocking() {
return socketChannel.isBlocking();
}

@Override
public SSLEngine getSSLEngine() {
return sslEngine;
}
}
17 changes: 17 additions & 0 deletions src/main/java/org/java_websocket/WebSocket.java
Expand Up @@ -35,6 +35,8 @@
import org.java_websocket.exceptions.WebsocketNotConnectedException;
import org.java_websocket.framing.Framedata;

import javax.net.ssl.SSLEngine;

public interface WebSocket {

/**
Expand Down Expand Up @@ -207,4 +209,19 @@ public interface WebSocket {
* @since 1.3.7
**/
<T> T getAttachment();

/**
* Does this websocket use an encrypted (wss/ssl) or unencrypted (ws) connection
* @return true, if the websocket does use wss and therefore has a SSLEngine
* @since 1.4.1
*/
boolean hasSSLEngine();

/**
* Returns the ssl engine of websocket, if ssl/wss is used for this instance.
* @return the ssl engine of this websocket instance
* @throws IllegalArgumentException the underlying channel does not use ssl (use hasSSLEngine() to check)
* @since 1.4.1
*/
SSLEngine getSSLEngine() throws IllegalArgumentException;
}
16 changes: 16 additions & 0 deletions src/main/java/org/java_websocket/WebSocketImpl.java
Expand Up @@ -25,6 +25,7 @@

package org.java_websocket;

import org.java_websocket.interfaces.ISSLChannel;
import org.java_websocket.drafts.Draft;
import org.java_websocket.drafts.Draft_6455;
import org.java_websocket.enums.*;
Expand All @@ -51,6 +52,8 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.net.ssl.SSLEngine;

/**
* Represents one end (client or server) of a single WebSocketImpl connection.
* Takes care of the "handshake" phase, then allows for easy sending of
Expand Down Expand Up @@ -820,6 +823,19 @@ public <T> T getAttachment() {
return (T) attachment;
}

@Override
public boolean hasSSLEngine() {
return channel instanceof ISSLChannel;
}

@Override
public SSLEngine getSSLEngine() {
if (!hasSSLEngine()) {
throw new IllegalArgumentException("This websocket does use ws instead of wss. No SSLEngine available.");
}
return ((ISSLChannel) channel).getSSLEngine();
}

@Override
public <T> void setAttachment(T attachment) {
this.attachment = attachment;
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/org/java_websocket/client/WebSocketClient.java
Expand Up @@ -42,6 +42,7 @@

import javax.net.SocketFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLEngine;
import javax.net.ssl.SSLException;
import javax.net.ssl.SSLSocketFactory;

Expand Down Expand Up @@ -850,6 +851,15 @@ public String getResourceDescriptor() {
return uri.getPath();
}

@Override
public boolean hasSSLEngine() {
return engine.hasSSLEngine();
}

@Override
public SSLEngine getSSLEngine() {
return engine.getSSLEngine();
}

/**
* Method to give some additional info for specific IOExceptions
Expand Down
43 changes: 43 additions & 0 deletions src/main/java/org/java_websocket/interfaces/ISSLChannel.java
@@ -0,0 +1,43 @@
/*
* Copyright (c) 2010-2019 Nathan Rajlich
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
*/

package org.java_websocket.interfaces;

import javax.net.ssl.SSLEngine;

/**
* Interface which specifies all required methods a SSLSocketChannel has to make public.
*
* @since 1.4.1
*/
public interface ISSLChannel {

/**
* Get the ssl engine used for the de- and encryption of the communication.
* @return the ssl engine of this channel
*/
SSLEngine getSSLEngine();
}
30 changes: 30 additions & 0 deletions src/main/java/org/java_websocket/interfaces/package-info.java
@@ -0,0 +1,30 @@
/*
* Copyright (c) 2010-2019 Nathan Rajlich
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
*/

/**
* This package encapsulates all new interfaces.
*/
package org.java_websocket.interfaces;

0 comments on commit 1cde8a7

Please sign in to comment.