Skip to content

Commit

Permalink
feat(Jt808MsgEncryptionHandler): 支持消息加解密
Browse files Browse the repository at this point in the history
  • Loading branch information
hylexus committed Jun 2, 2024
1 parent c3b6bf1 commit 4e86eb9
Show file tree
Hide file tree
Showing 29 changed files with 858 additions and 48 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
## 2.1.4-rc.4(2024-06-02)

### ⭐ New Features

初步支持消息加解密,详情见: [#82](https://github.com/hylexus/jt-framework/issues/82)

- 新增 `Jt808MsgEncryptionHandler`
- `@Jt808ResponseBody` 新增 `encryptionType(int)` 属性
- `Jt808Response` 新增 `encryptionType(int)` 属性
- `Jt808MsgBuilder` 新增 `encryptionType(int)` 属性

## 2.1.4-rc.3(2024-03-24)

### 🐞 Bug Fixes
Expand Down
1 change: 1 addition & 0 deletions build-script/script/common-java-env-config.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ dependencyManagement {
dependency "com.google.code.findbugs:annotations:3.0.1"
dependency "javax.annotation:javax.annotation-api:1.3.2"
dependency "org.apache.commons:commons-collections4:4.4"
dependency "org.bouncycastle:bcprov-jdk18on:1.78.1"
}
}

Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
projectGroup=io.github.hylexus.jt
projectVersion=2.1.4-rc.3
projectVersion=2.1.4-rc.4
# scm
projectScmUrl=https://github.com/hylexus/jt-framework
projectScmConnection=scm:git:git@github.com:hylexus/jt-framework.git
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.github.hylexus.jt.jt808.boot.config.configuration.codec;

import io.github.hylexus.jt.jt808.spec.Jt808MsgEncryptionHandler;
import io.github.hylexus.jt.jt808.spec.Jt808MsgTypeParser;
import io.github.hylexus.jt.jt808.spec.Jt808ProtocolVersionDetector;
import io.github.hylexus.jt.jt808.spec.Jt808ProtocolVersionDetectorRegistry;
Expand Down Expand Up @@ -53,23 +54,31 @@ public Jt808MsgBytesProcessor jt808MsgBytesProcessor() {
return new DefaultJt808MsgBytesProcessor(ByteBufAllocator.DEFAULT);
}

@Bean
@ConditionalOnMissingBean
public Jt808MsgEncryptionHandler jt808MsgEncryptionHandler() {
return Jt808MsgEncryptionHandler.NO_OPS;
}

@Bean
@ConditionalOnMissingBean
public Jt808MsgDecoder jt808MsgDecoder(
Jt808MsgTypeParser msgTypeParser,
Jt808MsgBytesProcessor msgBytesProcessor,
Jt808ProtocolVersionDetectorRegistry versionDetectorRegistry) {
Jt808ProtocolVersionDetectorRegistry versionDetectorRegistry,
Jt808MsgEncryptionHandler encryptionHandler) {

return new DefaultJt808MsgDecoder(msgTypeParser, msgBytesProcessor, versionDetectorRegistry);
return new DefaultJt808MsgDecoder(msgTypeParser, msgBytesProcessor, versionDetectorRegistry, encryptionHandler);
}

@Bean
@ConditionalOnMissingBean
public Jt808MsgEncoder jt808MsgEncoder(
Jt808MsgBytesProcessor msgBytesProcessor,
Jt808ResponseSubPackageEventListener subPackageEventListener,
Jt808ResponseSubPackageStorage subPackageStorage) {
return new DefaultJt808MsgEncoder(PooledByteBufAllocator.DEFAULT, msgBytesProcessor, subPackageEventListener, subPackageStorage);
Jt808ResponseSubPackageStorage subPackageStorage,
Jt808MsgEncryptionHandler encryptionHandler) {
return new DefaultJt808MsgEncoder(PooledByteBufAllocator.DEFAULT, msgBytesProcessor, subPackageEventListener, subPackageStorage, encryptionHandler);
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import io.github.hylexus.jt.jt808.spec.impl.msg.builder.ByteBufJt808MsgBuilder;
import io.github.hylexus.jt.jt808.spec.impl.msg.builder.EntityJt808MsgBuilder;
import io.github.hylexus.jt.jt808.spec.session.Jt808FlowIdGenerator;
import io.github.hylexus.jt.jt808.support.codec.Jt808MsgEncoder;
import io.github.hylexus.jt.jt808.support.utils.JtProtocolUtils;
import io.github.hylexus.jt.utils.HexStringUtils;
import io.netty.buffer.ByteBuf;
Expand All @@ -19,10 +20,18 @@ static ByteBufJt808MsgBuilder newByteBufBuilder(Jt808FlowIdGenerator flowIdGener
return new ByteBufJt808MsgBuilder(flowIdGenerator, body);
}

static ByteBufJt808MsgBuilder newByteBufBuilder(Jt808FlowIdGenerator flowIdGenerator, Jt808MsgEncoder encoder, ByteBuf body) {
return new ByteBufJt808MsgBuilder(flowIdGenerator, encoder, body);
}

static EntityJt808MsgBuilder newEntityBuilder(Jt808FlowIdGenerator flowIdGenerator) {
return new EntityJt808MsgBuilder(flowIdGenerator);
}

static EntityJt808MsgBuilder newEntityBuilder(Jt808FlowIdGenerator flowIdGenerator, Jt808MsgEncoder encoder) {
return new EntityJt808MsgBuilder(flowIdGenerator, encoder);
}

S version(Jt808ProtocolVersion version);

S msgId(int msgId);
Expand All @@ -33,6 +42,18 @@ default S msgId(MsgType msgType) {

S terminalId(String terminalId);

/**
* @since 2.1.4
*/
S encryptionType(int encType);

/**
* @since 2.1.4
*/
default S encryptionType(Jt808MsgEncryptionType encType) {
return this.encryptionType(encType.intValue());
}

S body(B body);

B body();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package io.github.hylexus.jt.jt808.spec;

import io.github.hylexus.jt.jt808.support.annotation.msg.resp.Jt808ResponseBody;
import io.netty.buffer.ByteBuf;

/**
* @author hylexus
* @see <a href="https://github.com/hylexus/jt-framework/issues/82">https://github.com/hylexus/jt-framework/issues/82</a>
* @since 2.1.4
*/
public interface Jt808MsgEncryptionHandler {

/**
* @param header 请求头
* @param body 请求体;可能是明文也可能是密文,根据 `header` 判断
* @return 解密之后的明文 或者 原样返回 `body`
* @see Jt808RequestHeader#msgBodyProps()
* @see Jt808RequestHeader.Jt808MsgBodyProps#encryptionType()
* @see Jt808RequestHeader.Jt808MsgBodyProps#dataEncryptionType()
*/
ByteBuf decryptRequestBody(Jt808RequestHeader header, ByteBuf body);

/**
* @param response 本次响应的其他信息
* @param plaintextBody 明文数据;可能是完整包,也可能是一个子包
* @return 返回密文 或者 原样返回`plaintextBody`
* @see Jt808ResponseBody#encryptionType()
* @see Jt808Response#encryptionType(int)
* @see Jt808Response#encryptionType(Jt808MsgEncryptionType)
* @see Jt808MsgBuilder#encryptionType(int)
* @see Jt808MsgBuilder#encryptionType(Jt808MsgEncryptionType)
*/
ByteBuf encryptResponseBody(Jt808Response response, ByteBuf plaintextBody);

Jt808MsgEncryptionHandler NO_OPS = new NoOps();

class NoOps implements Jt808MsgEncryptionHandler {

@Override
public ByteBuf decryptRequestBody(Jt808RequestHeader header, ByteBuf body) {
return body;
}

@Override
public ByteBuf encryptResponseBody(Jt808Response response, ByteBuf plaintextBody) {
return plaintextBody;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package io.github.hylexus.jt.jt808.spec;

import io.github.hylexus.jt.utils.FormatUtils;

public interface Jt808MsgEncryptionType {

int intValue();

default int bit10() {
return this.intValue() & 0b001;
}

default int bit11() {
return this.intValue() & 0b010;
}

default int bit12() {
return this.intValue() & 0b100;
}

default boolean isEncrypted() {
return this.intValue() != 0;
}

static Jt808MsgEncryptionType fromMsgBodyProps(int bodyPros) {
// bit[10-12] 0001,1100,0000,0000(1C00)(加密类型)
return new Default((bodyPros & 0x1c00) >> 10);
}

static Jt808MsgEncryptionType fromIntValue(int value) {
return new Default(value & 0b111);
}

static Jt808MsgEncryptionType fromBits(int bit10, int bit11, int bit12) {
return new Default(
(bit10 & 0b1)
| ((bit11 << 1) & 0b10)
| ((bit12 << 2) & 0b100)
);
}


class Default implements Jt808MsgEncryptionType {
private final int value;

public Default(int value) {
this.value = value;
}

@Override
public int intValue() {
return this.value;
}

@Override
public String toString() {
return "Default{"
+ "value=" + value
+ "(" + FormatUtils.toBinaryString(value, 3) + ")"
+ '}';
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,23 @@ default int msgBodyLength() {
return intValue() & 0x3ff;
}

// bit[10-12] 0001,1100,0000,0000(1C00)(加密类型)
/**
* bit[10-12] 0001,1100,0000,0000(1C00)(加密类型)
*
* @see #dataEncryptionType()
*/
default int encryptionType() {
return (intValue() & 0x1c00) >> 10;
}

/**
* @see #encryptionType()
* @since 2.1.4
*/
default Jt808MsgEncryptionType dataEncryptionType() {
return Jt808MsgEncryptionType.fromIntValue(this.encryptionType());
}

// bit[13] 0010,0000,0000,0000(2000)(是否有子包)
default boolean hasSubPackage() {
return ((intValue() & 0x2000) >> 13) == 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,31 @@ default int msgBodyLength() {
return body().readableBytes();
}

// /**
// * byte[2,4).bit[10,12) -- 数据加密方式
// */
// default int encryptionType() {
// return 0;
// }
/**
* byte[2,4).bit[10,12) -- 数据加密方式
*
* @since 2.1.4
*/
int encryptionType();

// Jt808Response encryptionType(int encType);
/**
* @since 2.1.4
*/
Jt808Response encryptionType(int encType);

/**
* @since 2.1.4
*/
default Jt808Response encryptionType(Jt808MsgEncryptionType encType) {
return this.encryptionType(encType.intValue());
}

/**
* @since 2.1.4
*/
default Jt808MsgEncryptionType dataEncryptionType() {
return Jt808MsgEncryptionType.fromIntValue(this.encryptionType());
}

/**
* byte[2,4).bit[15] -- 保留位
Expand Down Expand Up @@ -184,7 +201,11 @@ default Jt808ResponseBuilder msgId(MsgType msgType) {

Jt808ResponseBuilder version(Jt808ProtocolVersion version);

//Jt808ResponseBuilder encryptionType(int encryptionType);
Jt808ResponseBuilder encryptionType(int encryptionType);

default Jt808ResponseBuilder encryptionType(Jt808MsgEncryptionType encryptionType) {
return this.encryptionType(encryptionType.intValue());
}

Jt808ResponseBuilder reversedBit15InHeader(byte reversedBit15InHeader);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import io.github.hylexus.jt.annotation.UnstableApi;
import io.github.hylexus.jt.jt808.Jt808ProtocolVersion;
import io.github.hylexus.jt.jt808.spec.Jt808MsgBuilder;
import io.github.hylexus.jt.jt808.spec.Jt808MsgEncryptionHandler;
import io.github.hylexus.jt.jt808.spec.Jt808Response;
import io.github.hylexus.jt.jt808.spec.session.Jt808FlowIdGenerator;
import io.github.hylexus.jt.jt808.support.codec.Jt808MsgBytesProcessor;
Expand Down Expand Up @@ -49,7 +50,8 @@ public AbstractJt808MsgBuilder(Jt808FlowIdGenerator flowIdGenerator, ByteBufAllo
msgBytesProcessor,
responseSubPackage -> {
},
Jt808ResponseSubPackageStorage.NO_OPS_STORAGE
Jt808ResponseSubPackageStorage.NO_OPS_STORAGE,
Jt808MsgEncryptionHandler.NO_OPS
);
}

Expand Down Expand Up @@ -83,6 +85,12 @@ public S terminalId(String terminalId) {
return self();
}

@Override
public S encryptionType(int encType) {
this.encryptionType = encType;
return self();
}

@Override
public S msgId(int msgId) {
this.msgId = Assertions.assertThat(msgId, Numbers::isPositive, "msg > 0");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import io.github.hylexus.jt.jt808.support.annotation.codec.Jt808AnnotationBasedEncoder;
import io.github.hylexus.jt.jt808.support.annotation.msg.resp.Jt808ResponseBody;
import io.github.hylexus.jt.jt808.support.codec.Jt808MsgBytesProcessor;
import io.github.hylexus.jt.jt808.support.codec.Jt808MsgEncoder;
import io.github.hylexus.jt.jt808.support.data.serializer.DefaultJt808FieldSerializerRegistry;
import io.github.hylexus.jt.jt808.support.data.serializer.Jt808FieldSerializerRegistry;
import io.netty.buffer.ByteBuf;
Expand All @@ -31,6 +32,11 @@ public EntityJt808MsgBuilder(Jt808FlowIdGenerator flowIdGenerator, Jt808FieldSer
this(new Jt808AnnotationBasedEncoder(registry), flowIdGenerator);
}

public EntityJt808MsgBuilder(Jt808FlowIdGenerator flowIdGenerator, Jt808MsgEncoder encoder) {
super(flowIdGenerator, encoder);
this.annotationBasedEncoder = new Jt808AnnotationBasedEncoder(new DefaultJt808FieldSerializerRegistry(true));
}

public EntityJt808MsgBuilder(Jt808FlowIdGenerator flowIdGenerator) {
this(new Jt808AnnotationBasedEncoder(new DefaultJt808FieldSerializerRegistry(true)), flowIdGenerator);
}
Expand Down Expand Up @@ -74,6 +80,13 @@ protected Jt808Response toJt808Response() {
final int maxPackageSize = this.maxPackageSize == null ? annotation.maxPackageSize() : this.maxPackageSize;
final int msgId = this.msgId == null ? annotation.msgId() : this.msgId;

int encryptionType;
if (this.encryptionType == 0) {
encryptionType = annotation.encryptionType();
} else {
encryptionType = this.encryptionType;
}

final ByteBuf respBody = this.annotationBasedEncoder.encodeMsgBody(null, body, null);
return new DefaultJt808Response(
requireNonNull(version, "version() can not be null"),
Expand Down
Loading

0 comments on commit 4e86eb9

Please sign in to comment.