Skip to content

Commit

Permalink
implement ssl engine
Browse files Browse the repository at this point in the history
  • Loading branch information
JacksonTian committed Jun 9, 2023
1 parent 880e5f8 commit c2c978d
Show file tree
Hide file tree
Showing 10 changed files with 1,035 additions and 12 deletions.
13 changes: 13 additions & 0 deletions src/main/java/com/aliyun/gmsse/ClientHandshakeProtocol.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.aliyun.gmsse;

public class ClientHandshakeProtocol extends HandshakeProtocol {

public ClientHandshakeProtocol(GMSSLContextSpi context) {

}

public void connect() {

}

}
43 changes: 43 additions & 0 deletions src/main/java/com/aliyun/gmsse/ConnectionContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.io.IOException;

import javax.net.ssl.SSLEngineResult.HandshakeStatus;

public abstract class ConnectionContext {

protected GMSSLContextSpi sslContext;
Expand All @@ -18,4 +20,45 @@ public ConnectionContext(GMSSLContextSpi context, GMSSLSocket socket, SSLConfigu
}

public abstract void kickstart() throws IOException;

public void setUseClientMode(boolean mode) {
sslConfig.isClientMode = mode;
}

public HandshakeStatus getHandshakeStatus() {
// if (!outputRecord.isEmpty()) {
// // If no handshaking, special case to wrap alters or
// // post-handshake messages.
// return HandshakeStatus.NEED_WRAP;
// } else if (isOutboundClosed() && isInboundClosed()) {
// return HandshakeStatus.NOT_HANDSHAKING;
// } else if (handshakeContext != null) {
// if (!handshakeContext.delegatedActions.isEmpty()) {
// return HandshakeStatus.NEED_TASK;
// } else if (!isInboundClosed()) {
// //JDK8 NEED_UNWRAP returnned for NEED_UNWRAP_AGAIN status
// // needUnwrapAgain should be used to determine NEED_UNWRAP_AGAIN
// return HandshakeStatus.NEED_UNWRAP;
// } else if (!isOutboundClosed()) {
// // Special case that the inbound was closed, but outbound open.
// return HandshakeStatus.NEED_WRAP;
// }
// } else if (isOutboundClosed() && !isInboundClosed()) {
// // Special case that the outbound was closed, but inbound open.
// return HandshakeStatus.NEED_UNWRAP;
// } else if (!isOutboundClosed() && isInboundClosed()) {
// // Special case that the inbound was closed, but outbound open.
// return HandshakeStatus.NEED_WRAP;
// }

return HandshakeStatus.NOT_HANDSHAKING;
}

public boolean isInboundClosed() {
return false;
}

public boolean isOutboundDone() {
return false;
}
}
4 changes: 2 additions & 2 deletions src/main/java/com/aliyun/gmsse/GMSSLContextSpi.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ public GMSSLContextSpi() {

@Override
protected SSLEngine engineCreateSSLEngine() {
return null;
return new GMSSLEngine(this);
}

@Override
protected SSLEngine engineCreateSSLEngine(String host, int port) {
return null;
return new GMSSLEngine(this, host, port);
}

@Override
Expand Down
182 changes: 182 additions & 0 deletions src/main/java/com/aliyun/gmsse/GMSSLEngine.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
package com.aliyun.gmsse;

import java.io.IOException;
import java.nio.ByteBuffer;

import javax.net.ssl.SSLEngine;
import javax.net.ssl.SSLEngineResult;
import javax.net.ssl.SSLEngineResult.HandshakeStatus;
import javax.net.ssl.SSLEngineResult.Status;

import com.aliyun.gmsse.protocol.ClientConnectionContext;

import javax.net.ssl.SSLException;
import javax.net.ssl.SSLSession;

public class GMSSLEngine extends SSLEngine {

private GMSSLContextSpi context;
private ConnectionContext connection;
private HandshakeProtocol protocol;

public GMSSLEngine(GMSSLContextSpi context, String host, int port) {
super(host, port);
this.context = context;
this.connection = new ClientConnectionContext(context, null);
}

public GMSSLEngine(GMSSLContextSpi context) {
this(context, null, -1);
}

@Override
public void beginHandshake() throws SSLException {
if (getUseClientMode()) {
ClientHandshakeProtocol clientProtocol = new ClientHandshakeProtocol(context);
clientProtocol.connect();
this.protocol = clientProtocol;
} else {
// TODO: server side
}
}

@Override
public void closeInbound() throws SSLException {
try {
this.protocol.closeInbound();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

@Override
public void closeOutbound() {
try {
this.protocol.closeOutbound();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

@Override
public Runnable getDelegatedTask() {
// TODO Auto-generated method stub
return null;
}

@Override
public boolean getEnableSessionCreation() {
return connection.sslConfig.enableSessionCreation;
}

@Override
public String[] getEnabledCipherSuites() {
return CipherSuite.namesOf(connection.sslConfig.enabledCipherSuites);
}

@Override
public String[] getEnabledProtocols() {
return ProtocolVersion.toStringArray(connection.sslConfig.enabledProtocols);
}

@Override
public HandshakeStatus getHandshakeStatus() {
return connection.getHandshakeStatus();
}

@Override
public boolean getNeedClientAuth() {
// TODO Auto-generated method stub
return false;
}

@Override
public SSLSession getSession() {
return connection.session;
}

@Override
public String[] getSupportedCipherSuites() {
return CipherSuite.namesOf(context.getSupportedCipherSuites());
}

@Override
public String[] getSupportedProtocols() {
return ProtocolVersion.toStringArray(context.getSupportedProtocolVersions());
}

@Override
public boolean getUseClientMode() {
return connection.sslConfig.isClientMode;
}

@Override
public boolean getWantClientAuth() {
// TODO Auto-generated method stub
return false;
}

@Override
public boolean isInboundDone() {
return connection.isInboundClosed();
}

@Override
public boolean isOutboundDone() {
return connection.isOutboundDone();
}

@Override
public void setEnableSessionCreation(boolean flag) {
connection.sslConfig.enableSessionCreation = flag;
}

@Override
public void setEnabledCipherSuites(String[] suites) {
// TODO Auto-generated method stub
}

@Override
public void setEnabledProtocols(String[] arg0) {
// TODO Auto-generated method stub
}

@Override
public void setNeedClientAuth(boolean need) {
// TODO Auto-generated method stub
}

@Override
public void setUseClientMode(boolean mode) {
connection.setUseClientMode(mode);
}

@Override
public void setWantClientAuth(boolean want) {
// TODO Auto-generated method stub
}

@Override
public SSLEngineResult unwrap(ByteBuffer src, ByteBuffer[] dsts, int offset, int length) throws SSLException {
return unwrap(new ByteBuffer[]{src}, 0, 1, dsts, offset, length);
}

private SSLEngineResult unwrap(ByteBuffer[] srcs, int srcsOffset, int srcslength, ByteBuffer[] dsts, int dstsOffset, int dstsLength) {
if (isInboundDone()) {
return new SSLEngineResult(Status.CLOSED, getHandshakeStatus(), 0, 0, -1);
}
return null;
}

@Override
public SSLEngineResult wrap(ByteBuffer[] appData, int offset, int length, ByteBuffer netData) throws SSLException {
return wrap(appData, offset, length, new ByteBuffer[]{ netData }, 0, 1);
}

private SSLEngineResult wrap(ByteBuffer[] srcs, int srcsOffset, int srcsLength, ByteBuffer[] dsts, int dstsOffset, int dstsLength) {
return null;
}

}
16 changes: 16 additions & 0 deletions src/main/java/com/aliyun/gmsse/HandshakeProtocol.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.aliyun.gmsse;

import java.io.IOException;

public class HandshakeProtocol {

private RecordStream recordStream;

public void closeInbound() throws IOException {
this.recordStream.getInputStream().close();
}

public void closeOutbound() throws IOException {
this.recordStream.getOutputStream().close();
}
}
8 changes: 8 additions & 0 deletions src/main/java/com/aliyun/gmsse/RecordStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -290,4 +290,12 @@ synchronized long nextValue() throws AlertException {
}
}

public OutputStream getOutputStream() {
return output;
}

public InputStream getInputStream() {
return input;
}

}
5 changes: 5 additions & 0 deletions src/main/java/com/aliyun/gmsse/ServerHandshakeProtocol.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.aliyun.gmsse;

public class ServerHandshakeProtocol {

}
Loading

0 comments on commit c2c978d

Please sign in to comment.