-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0ef6f13
commit 71589b4
Showing
8 changed files
with
319 additions
and
156 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package tlschannel.helpers; | ||
|
||
import java.io.IOException; | ||
import java.nio.ByteBuffer; | ||
import java.nio.channels.AsynchronousByteChannel; | ||
import java.nio.channels.ByteChannel; | ||
import java.util.concurrent.ExecutionException; | ||
|
||
import tlschannel.WouldBlockException; | ||
|
||
/** | ||
* Transforms a non-blocking {@link ByteChannel} into a blocking one. | ||
*/ | ||
class BlockerByteChannel implements ByteChannel { | ||
|
||
private final AsynchronousByteChannel impl; | ||
|
||
public BlockerByteChannel(AsynchronousByteChannel impl) { | ||
this.impl = impl; | ||
} | ||
|
||
@Override | ||
public int write(ByteBuffer src)throws IOException { | ||
try { | ||
return impl.write(src).get(); | ||
} catch (InterruptedException e) { | ||
Thread.currentThread().interrupt(); | ||
throw new RuntimeException(e); | ||
} catch (ExecutionException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
@Override | ||
public int read(ByteBuffer dst) throws IOException { | ||
try { | ||
return impl.read(dst).get(); | ||
} catch (InterruptedException e) { | ||
Thread.currentThread().interrupt(); | ||
throw new RuntimeException(e); | ||
} catch (ExecutionException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean isOpen() { | ||
return impl.isOpen(); | ||
} | ||
|
||
@Override | ||
public void close() throws IOException { | ||
try { | ||
impl.close(); | ||
} catch (WouldBlockException e) { | ||
// OK | ||
} | ||
} | ||
|
||
} |
25 changes: 0 additions & 25 deletions
25
src/test/scala/tlschannel/helpers/BlockerByteChannel.scala
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package tlschannel.helpers; | ||
|
||
import javax.net.ssl.*; | ||
import java.security.SecureRandom; | ||
|
||
|
||
public class NullSslContext extends SSLContext { | ||
public NullSslContext() { | ||
super(new NullSslContextSpi(), null, null); | ||
} | ||
|
||
} | ||
|
||
class NullSslContextSpi extends SSLContextSpi { | ||
|
||
@Override | ||
public SSLEngine engineCreateSSLEngine() { | ||
return new NullSslEngine(); | ||
} | ||
|
||
@Override | ||
public SSLEngine engineCreateSSLEngine(String s, int i) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public SSLSessionContext engineGetClientSessionContext() { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public SSLSessionContext engineGetServerSessionContext() { | ||
throw new UnsupportedOperationException(); | ||
} | ||
@Override | ||
public SSLServerSocketFactory engineGetServerSocketFactory() { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public SSLSocketFactory engineGetSocketFactory() { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public void engineInit(KeyManager[] km, TrustManager[] tm, SecureRandom sc) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
package tlschannel.helpers; | ||
|
||
import javax.net.ssl.SSLEngine; | ||
import javax.net.ssl.SSLEngineResult.HandshakeStatus; | ||
import java.nio.ByteBuffer; | ||
import javax.net.ssl.SSLEngineResult; | ||
import javax.net.ssl.SSLEngineResult.Status; | ||
import javax.net.ssl.SSLSession; | ||
|
||
import tlschannel.impl.ByteBufferSet; | ||
import tlschannel.impl.ByteBufferUtil; | ||
|
||
/** | ||
* "Null" {@link SSLEngine} that does nothing to the bytesProduced. | ||
*/ | ||
class NullSslEngine extends SSLEngine { | ||
|
||
/** Internal buffers are still used to prevent any underlying optimization of the wrap/unwrap. | ||
*/ | ||
private final int bufferSize = 16000; | ||
|
||
@Override | ||
public void beginHandshake() {} | ||
|
||
@Override | ||
public void closeInbound() {} | ||
|
||
@Override | ||
public void closeOutbound() {} | ||
|
||
@Override | ||
public Runnable getDelegatedTask() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public boolean getEnableSessionCreation() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public String[] getEnabledCipherSuites() { | ||
return new String[]{}; | ||
} | ||
|
||
@Override | ||
public String[] getEnabledProtocols() { | ||
return new String[]{}; | ||
} | ||
|
||
@Override | ||
public HandshakeStatus getHandshakeStatus() { | ||
return HandshakeStatus.NOT_HANDSHAKING; | ||
} | ||
|
||
@Override | ||
public boolean getNeedClientAuth() { | ||
return false; | ||
} | ||
|
||
@Override | ||
public SSLSession getSession() { | ||
return new NullSslSession(bufferSize); | ||
} | ||
|
||
@Override | ||
public String[] getSupportedCipherSuites() { | ||
return new String[]{}; | ||
} | ||
|
||
@Override | ||
public String[] getSupportedProtocols(){ | ||
return new String[]{}; | ||
} | ||
|
||
@Override | ||
public boolean getUseClientMode() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public boolean getWantClientAuth() { | ||
return false; | ||
} | ||
|
||
@Override | ||
public boolean isInboundDone() { | ||
return false; | ||
} | ||
|
||
@Override | ||
public boolean isOutboundDone() { | ||
return false; | ||
} | ||
|
||
@Override | ||
public void setEnableSessionCreation(boolean b) {} | ||
|
||
@Override | ||
public void setEnabledCipherSuites(String[] a) {} | ||
|
||
@Override | ||
public void setEnabledProtocols(String[] a) {} | ||
|
||
@Override | ||
public void setNeedClientAuth(boolean b) {} | ||
|
||
@Override | ||
public void setUseClientMode(boolean b) {} | ||
|
||
@Override | ||
public void setWantClientAuth(boolean b) {} | ||
|
||
private final ByteBuffer unwrapBuffer = ByteBuffer.allocate(bufferSize); | ||
private final ByteBuffer wrapBuffer = ByteBuffer.allocate(bufferSize); | ||
|
||
@Override | ||
public SSLEngineResult unwrap(ByteBuffer src, ByteBuffer[] dsts, int offset, int length) { | ||
var dstSet = new ByteBufferSet(dsts, offset, length); | ||
if (!src.hasRemaining()) { | ||
return new SSLEngineResult(Status.BUFFER_UNDERFLOW, HandshakeStatus.NOT_HANDSHAKING, 0, 0); | ||
} | ||
int unwrapSize = Math.min(unwrapBuffer.capacity(), src.remaining()); | ||
if (dstSet.remaining() < unwrapSize) { | ||
return new SSLEngineResult(Status.BUFFER_OVERFLOW, HandshakeStatus.NOT_HANDSHAKING, 0, 0); | ||
} | ||
unwrapBuffer.clear(); | ||
ByteBufferUtil.copy(src, unwrapBuffer, unwrapSize); | ||
unwrapBuffer.flip(); | ||
dstSet.putRemaining(unwrapBuffer); | ||
return new SSLEngineResult(Status.OK, HandshakeStatus.NOT_HANDSHAKING, unwrapSize, unwrapSize); | ||
} | ||
|
||
@Override | ||
public SSLEngineResult wrap(ByteBuffer[] srcs, int offset, int length, ByteBuffer dst) { | ||
var srcSet = new ByteBufferSet(srcs, offset, length); | ||
if (!srcSet.hasRemaining()) { | ||
return new SSLEngineResult(Status.OK, HandshakeStatus.NOT_HANDSHAKING, 0, 0); | ||
} | ||
int wrapSize = (int) Math.min(wrapBuffer.capacity(), srcSet.remaining()); | ||
if (dst.remaining() < wrapSize) { | ||
return new SSLEngineResult(Status.BUFFER_OVERFLOW, HandshakeStatus.NOT_HANDSHAKING, 0, 0); | ||
} | ||
wrapBuffer.clear(); | ||
srcSet.get(wrapBuffer, wrapSize); | ||
wrapBuffer.flip(); | ||
dst.put(wrapBuffer); | ||
return new SSLEngineResult(Status.OK, HandshakeStatus.NOT_HANDSHAKING, wrapSize, wrapSize); | ||
} | ||
|
||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.