Merge remote-tracking branch 'origin/master'

This commit is contained in:
ChessNineeee committed 2021-04-01 22:06:20 +08:00
commit 33b3d40855
17 files changed
+614 -246

No files matched your search

@@ -353,9 +353,8 @@ public class IdentifierComponent implements TlvComponentBase, IEncodingAble {
} else if(isByteArray()){
return StringHelper.escape(new String(this.byteArrayValue));
}else{
// return StringHelper.FormatUint(this.intValue,10);
// todo: 需要对long类型按照指定编码规则编码成字符串
return "";
return Long.valueOf(this.intValue).toString();
}
}
}
+35 -39
View File
@@ -13,10 +13,9 @@ import encoding.*;
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
public class KeyLocator implements TlvComponentBase, IEncodingAble {
private Identifier identifier;
private Identifier identifier=new Identifier();
public KeyLocator(){
identifier=new Identifier();
}
/**
@@ -24,15 +23,19 @@ public class KeyLocator implements TlvComponentBase, IEncodingAble {
* @param block
*/
public KeyLocator(Block block) throws ComponentException {
this.buildKeyLocatorByBlock(block);
if(!this.buildKeyLocatorByBlock(block)){
throw new ComponentException("KeyLocator(Block): block invalid.");
}
}
/**
* 根据标识字符串 创建一个 KeyLocator
* @param identifierString
*/
public KeyLocator(String identifierString){
this.buildKeyLocatorByString(identifierString);
public KeyLocator(String identifierString) throws ComponentException {
if(!this.buildKeyLocatorByString(identifierString)){
throw new ComponentException("KeyLocator(String): string invalid.");
}
}
/**
@@ -110,42 +113,33 @@ public class KeyLocator implements TlvComponentBase, IEncodingAble {
*/
@Override
public int wireEncode(Encoder encoder) throws ComponentException {
int totalLength=0;
// 编码 TLV-VALUE
int tmplen= 0;
try {
tmplen = this.identifier.wireEncode(encoder);
} catch (ComponentException e) {
int totalLength = 0;
// 编码 TLV-VALUE
int tmplen = this.identifier.wireEncode(encoder);
if (tmplen <= 0) {
return -1;
}
totalLength += tmplen;
// 编码 TLV-LENGTH
tmplen = encoder.prependVarNumber(new VlInt(totalLength));
if (tmplen <= 0) {
return -1;
}
totalLength += tmplen;
// 编码 TLV-TYPE
tmplen = encoder.prependVarNumber(new VlInt(TLV.TlvKeyLocator));
if (tmplen <= 0) {
return -1;
}
totalLength += tmplen;
return totalLength;
} catch (ComponentException | EncoderException e) {
throw new ComponentException("KeyLocator.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
// 编码 TLV-LENGTH
try {
tmplen=encoder.prependVarNumber(new VlInt(totalLength));
} catch (EncoderException e) {
throw new ComponentException("KeyLocator.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
// 编码 TLV-TYPE
try {
tmplen=encoder.prependVarNumber(new VlInt(totalLength));
} catch (EncoderException e) {
throw new ComponentException("KeyLocator.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
return totalLength;
}
/**
@@ -157,17 +151,19 @@ public class KeyLocator implements TlvComponentBase, IEncodingAble {
public boolean wireDecode(Block block) throws ComponentException {
try {
// 检查Type是否正确
if(!TLV.isValidIdentifierType(block.getType())){
if(!TLV.expectType(block.getType(),new VlInt(TLV.TlvKeyLocator))){
return false;
}
// 解析子 TLV
if(!block.parseSubElements()){
return false;
}
// 判断是否有且仅有一个子TLV
if(block.getSubElements().length()!=1){
return false;
}
// 解析子TLV为一个Identifier
return this.identifier.wireDecode(block.getSubElements().getBlock(0));
} catch (BlockException e) {
+19 -16
View File
@@ -10,25 +10,12 @@ import minsecurity.identity.Identity;
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
public class Signature {
public SignatureInfo getSigInfo() {
return sigInfo;
}
private SignatureInfo sigInfo=new SignatureInfo();
private SignatureValue sigValue=new SignatureValue();
public void setSigInfo(SignatureInfo sigInfo) {
this.sigInfo = sigInfo;
public Signature(){
}
public SignatureValue getSigValue() {
return sigValue;
}
public void setSigValue(SignatureValue sigValue) {
this.sigValue = sigValue;
}
private SignatureInfo sigInfo;
private SignatureValue sigValue;
/**
* 根据网络身份创建一个 Signature
* @param identity
@@ -46,4 +33,20 @@ public class Signature {
this.sigInfo=signatureInfo;
this.sigValue=signatureValue;
}
public SignatureInfo getSigInfo() {
return sigInfo;
}
public void setSigInfo(SignatureInfo sigInfo) {
this.sigInfo = sigInfo;
}
public SignatureValue getSigValue() {
return sigValue;
}
public void setSigValue(SignatureValue sigValue) {
this.sigValue = sigValue;
}
}
+76 -95
View File
@@ -14,22 +14,20 @@ import encoding.*;
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
public class SignatureInfo implements TlvComponentBase, IEncodingAble {
private SignatureType signatureType;
private KeyLocator keyLocator;
private SignatureType signatureType=new SignatureType();
private KeyLocator keyLocator=new KeyLocator();
public SignatureInfo(){
signatureType=new SignatureType();
keyLocator=new KeyLocator();
}
/**
* 根据签名值类型创建一个 SignatureInfo
* @param value
*/
public SignatureInfo(SignatureAlgorithm value){
signatureType=new SignatureType();
keyLocator=new KeyLocator();
this.buildSignatureInfoBySignatureTypeValue(value);
public SignatureInfo(SignatureAlgorithm value) throws ComponentException {
if(!this.buildSignatureInfoBySignatureTypeValue(value)){
throw new ComponentException("SignatureInfo(SignatureAlgorithm value): value invalid");
}
}
/**
@@ -38,7 +36,10 @@ public class SignatureInfo implements TlvComponentBase, IEncodingAble {
* @param locator
*/
public SignatureInfo(SignatureAlgorithm value,KeyLocator locator) throws ComponentException {
this.buildSignatureInfoBySignatureTypeValueAndKeyLocator(value,locator);
if(!this.buildSignatureInfoBySignatureTypeValueAndKeyLocator(value,locator)){
throw new ComponentException("SignatureInfo(SignatureAlgorithm value,KeyLocator locator):" +
" value or locator invalid");
}
}
/**
@@ -69,7 +70,7 @@ public class SignatureInfo implements TlvComponentBase, IEncodingAble {
return false;
}
if(!this.keyLocator.buildKeyLocatorByIdentifier(keyLocator.getIdentifier())){
if(!this.keyLocator.buildKeyLocatorByIdentifier(locator.getIdentifier())){
return false;
}
@@ -108,51 +109,41 @@ public class SignatureInfo implements TlvComponentBase, IEncodingAble {
*/
@Override
public int wireEncode(Encoder encoder) throws ComponentException {
int totalLength=0;
// 编码 TLV-VALUE
if(!this.keyLocator.isValid()){
// KeyLocator 是可选的,如果当前 SignatureInfo 对象有一个有效的 KeyLocator,则对其进行编码
int tmplen= 0;
try {
tmplen = this.keyLocator.wireEncode(encoder);
} catch (ComponentException e) {
throw new ComponentException("SignatureInfo.wireEncode: "+e.getMessage());
try {
int totalLength = 0;
// 编码 TLV-VALUE
if (this.keyLocator.isValid()) {
// KeyLocator 是可选的,如果当前 SignatureInfo 对象有一个有效的 KeyLocator,则对其进行编码
int tmplen = this.keyLocator.wireEncode(encoder);
if (tmplen <= 0) {
return -1;
}
totalLength += tmplen;
}
if(tmplen<=0){
int tmplen = this.signatureType.wireEncode(encoder);
if (tmplen <= 0) {
return -1;
}
totalLength+=tmplen;
}
int tmplen= 0;
tmplen = this.signatureType.wireEncode(encoder);
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
totalLength += tmplen;
// 编码 TLV-LENGTH
try {
tmplen=encoder.prependVarNumber(new VlInt(totalLength));
// 编码 TLV-LENGTH
tmplen = encoder.prependVarNumber(new VlInt(totalLength));
if (tmplen <= 0) {
return -1;
}
totalLength += tmplen;
// 编码 TLV-TYPE
tmplen = encoder.prependVarNumber(new VlInt(TLV.TlvSignatureInfo));
if (tmplen <= 0) {
return -1;
}
totalLength += tmplen;
return totalLength;
} catch (EncoderException e) {
throw new ComponentException("SignatureInfo.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
// 编码 TLV-TYPE
try {
tmplen=encoder.prependVarNumber(new VlInt(TLV.TlvSignatureInfo));
} catch (EncoderException e) {
throw new ComponentException("SignatureInfo.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
return totalLength;
}
/**
@@ -162,57 +153,47 @@ public class SignatureInfo implements TlvComponentBase, IEncodingAble {
*/
@Override
public boolean wireDecode(Block block) throws ComponentException {
// 检查 Type 是否正确
if(!TLV.expectType(block.getType(),new VlInt(TLV.TlvSignatureInfo))){
return false;
}
// 解析子 TLV
try {
if(!block.parseSubElements()){
// 检查 Type 是否正确
if (!TLV.expectType(block.getType(), new VlInt(TLV.TlvSignatureInfo))) {
return false;
}
} catch (BlockException e) {
// 解析子 TLV
if (!block.parseSubElements()) {
return false;
}
boolean findSignatureType = false;
int elementSize = block.getSubElements().length();
for (int i = 0; i < elementSize; i++) {
Block cacheBlock = block.getSubElements().getBlock(i);
int type = cacheBlock.getType().getVlIntValue2Int();
switch (type) {
case TLV.TlvSignatureType:
if (!this.signatureType.wireDecode(cacheBlock)) {
return false;
}
findSignatureType = true;
break;
case TLV.TlvKeyLocator:
if (!this.keyLocator.wireDecode(cacheBlock)) {
return false;
}
break;
default:
throw new ComponentException("SignatureInfo.wireDecode: SignatureInfo sub element type error");
}
}
// 缺失必要的 SignatureType 子TLV
if (!findSignatureType) {
throw new Error("SignatureInfo.wireDecode: Parse SignatureInfo error, require SignatureType sub-element");
}
return true;
} catch (BlockException | VlIntException e) {
throw new ComponentException("SignatureInfo.wireDecode: "+e.getMessage());
}
boolean findSignatureType=false;
int elementSize=block.getSubElements().length();
for (int i=0;i<elementSize;i++) {
Block cacheBlock= null;
try {
cacheBlock = block.getSubElements().getBlock(i);
} catch (BlockException e) {
throw new ComponentException("SignatureInfo.wireDecode:"+e.getMessage());
}
int type= 0;
try {
type = cacheBlock.getType().getVlIntValue2Int();
} catch (VlIntException e) {
e.printStackTrace();
}
switch (type){
case TLV.TlvSignatureType:
if(!this.signatureType.wireDecode(cacheBlock)){
return false;
}
findSignatureType=true;
case TLV.TlvKeyLocator:
if(!this.keyLocator.wireDecode(cacheBlock)){
return false;
}
default:
throw new ComponentException("SignatureInfo.wireDecode: SignatureInfo sub element type error");
// return false;
}
}
// 缺失必要的 SignatureType 子TLV
if(!findSignatureType){
throw new Error("SignatureInfo.wireDecode: Parse SignatureInfo error, require SignatureType sub-element");
}
return true;
}
}
+38 -47
View File
@@ -108,43 +108,35 @@ public class SignatureType implements TlvComponentBase, IEncodingAble {
*/
@Override
public int wireEncode(Encoder encoder) throws ComponentException {
int totalLength=0;
// 编码 TLV-VALUE
int tmplen= 0;
try {
tmplen = encoder.prependNonNegativeInteger(this.value.getSignatureAlgorithm());
int totalLength = 0;
// 编码 TLV-VALUE
int tmplen = encoder.prependNonNegativeInteger(this.value.getSignatureAlgorithm());
if (tmplen <= 0) {
return -1;
}
totalLength += tmplen;
// 编码 TLV-LENGTH
tmplen = encoder.prependVarNumber(new VlInt(totalLength));
if (tmplen <= 0) {
return -1;
}
totalLength += tmplen;
// 编码 TLV-TYPE
tmplen = encoder.prependVarNumber(new VlInt(TLV.TlvSignatureType));
if (tmplen <= 0) {
return -1;
}
totalLength += tmplen;
return totalLength;
} catch (EncoderException e) {
throw new ComponentException("SignatureType.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
// 编码 TLV-LENGTH
try {
tmplen=encoder.prependVarNumber(new VlInt(totalLength));
} catch (EncoderException e) {
throw new ComponentException("SignatureType.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
// 编码 TLV-TYPE
try {
tmplen=encoder.prependVarNumber(new VlInt(TLV.TlvSignatureType));
} catch (EncoderException e) {
throw new ComponentException("SignatureType.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
return totalLength;
}
/**
@@ -154,23 +146,22 @@ public class SignatureType implements TlvComponentBase, IEncodingAble {
*/
@Override
public boolean wireDecode(Block block) throws ComponentException {
// 检查 Type 是否正确
if(!TLV.expectType(block.getType(),new VlInt(TLV.TlvSignatureType))){
return false;
}
// 尝试从 TLE-VALUE 中读取出一个非负整数
long result= 0;
try {
result = TLV.readNonNegativeInteger(block.getValue(),0,block.getLength().getVlIntValue2Int());
// 检查 Type 是否正确
if (!TLV.expectType(block.getType(), new VlInt(TLV.TlvSignatureType))) {
return false;
}
// 尝试从 TLE-VALUE 中读取出一个非负整数
long result = TLV.readNonNegativeInteger(block.getValue(), 0, block.getLength().getVlIntValue2Int());
if (result < 0) {
return false;
}
this.value = new SignatureAlgorithm((int) result);
return true;
} catch (TLVException | VlIntException e) {
throw new ComponentException("SignatureType.wireDecode: " + e.getMessage());
}
if(result<0){
return false;
}
this.value=new SignatureAlgorithm((int)result);
return true;
}
}
+42 -45
View File
@@ -15,12 +15,17 @@ import encoding.*;
public class SignatureValue implements TlvComponentBase, IEncodingAble {
private byte[] value;
public SignatureValue(){
}
/**
* 根据签名值字节数组创建一个 SignatureValue
* @param value
*/
public SignatureValue(byte[] value){
this.buildSignatureValueByByteArray(value);
public SignatureValue(byte[] value) throws ComponentException {
if(!this.buildSignatureValueByByteArray(value)){
throw new ComponentException("Signature(byte[] value): value invalid");
}
}
/**
@@ -28,7 +33,9 @@ public class SignatureValue implements TlvComponentBase, IEncodingAble {
* @param block
*/
public SignatureValue(Block block) throws ComponentException {
this.buildSignatureValueByBlock(block);
if(!this.buildSignatureValueByBlock(block)){
throw new ComponentException("Signature(Block block): block invalid");
}
}
/**
@@ -66,42 +73,33 @@ public class SignatureValue implements TlvComponentBase, IEncodingAble {
*/
@Override
public int wireEncode(Encoder encoder) throws ComponentException {
int totalLength=0;
// 编码 TLV-VALUE
int tmplen= 0;
try {
tmplen = encoder.prependByteArray(this.value,new SizeT(this.value.length));
int totalLength = 0;
// 编码 TLV-VALUE
int tmplen = encoder.prependByteArray(this.value, new SizeT(this.value.length));
if (tmplen <= 0) {
return -1;
}
totalLength += tmplen;
// 编码 TLV-LENGTH
tmplen = encoder.prependVarNumber(new VlInt(totalLength));
if (tmplen <= 0) {
return -1;
}
totalLength += tmplen;
// 编码 TLV-TYPE
tmplen = encoder.prependVarNumber(new VlInt(TLV.TlvSignatureValue));
if (tmplen <= 0) {
return -1;
}
totalLength += tmplen;
return totalLength;
} catch (EncoderException e) {
throw new ComponentException("SignatureValue.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
// 编码 TLV-LENGTH
try {
tmplen=encoder.prependVarNumber(new VlInt(totalLength));
} catch (EncoderException e) {
throw new ComponentException("SignatureValue.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
// 编码 TLV-TYPE
try {
tmplen=encoder.prependVarNumber(new VlInt(TLV.TlvSignatureValue));
} catch (EncoderException e) {
throw new ComponentException("SignatureValue.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
return totalLength;
}
/**
@@ -111,19 +109,18 @@ public class SignatureValue implements TlvComponentBase, IEncodingAble {
*/
@Override
public boolean wireDecode(Block block) throws ComponentException {
// 检查 Type 是否正确
if(!TLV.expectType(block.getType(),new VlInt(TLV.TlvSignatureValue))){
return false;
}
int len= 0;
try {
len = block.getLength().getVlIntValue2Int();
// 检查 Type 是否正确
if (!TLV.expectType(block.getType(), new VlInt(TLV.TlvSignatureValue))) {
return false;
}
int len = block.getLength().getVlIntValue2Int();
this.value = new byte[len];
System.arraycopy(block.getValue(), 0, this.value, 0, len);
return true;
} catch (VlIntException e) {
throw new ComponentException("SignatureValue.wireDecode: "+e.getMessage());
}
this.value=new byte[len];
System.arraycopy(block.getValue(),0,this.value,0,len);
return true;
}
}
-1
View File
@@ -247,7 +247,6 @@ public class Block {
if ((this.value.length == 0) || this.elements.length() != 0) {
return true;
}
// 循环解析所有的一级子TLV
for (int start = 0; start < this.value.length; ) {
// 读取 TLV-TYPE
@@ -19,7 +19,7 @@ import java.nio.charset.StandardCharsets;
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
public class HashAlgo {
// TODO: 单元测试 基本上只需要对sm3做单元测试
///////////////
///// SM3 /////
///////////////
+2
View File
@@ -212,10 +212,12 @@ public class Data implements IEncodingAble, InteractWithField {
if (!this.freshnessPeriod.wireDecode(newblock)) {
return false;
}
break;
case TLV.TlvPayload:
if (!this.payload.wireDecode(newblock)) {
return false;
}
break;
default:
// TODO: 这边可以规定哪些范围是自定义的,哪些是预留的,然后根据需要这边抛出错误
+6
View File
@@ -258,26 +258,32 @@ public class Interest implements InteractWithField, IEncodingAble {
if (!this.canBePrefix.wireDecode(newblock)) {
return false;
}
break;
case TLV.TlvMustBeRefresh:
if (!this.mustBeRefresh.wireDecode(newblock)) {
return false;
}
break;
case TLV.TlvInterestLifeTime:
if (!this.interestLifeTime.wireDecode(newblock)) {
return false;
}
break;
case TLV.TlvNonce:
if (!this.nonce.wireDecode(newblock)) {
return false;
}
break;
case TLV.TlvHopLimit:
if (!this.hopLimit.wireDecode(newblock)) {
return false;
}
break;
case TLV.TlvPayload:
if (!this.payload.wireDecode(newblock)) {
return false;
}
break;
default:
// TODO: 这边可以规定哪些范围是自定义的,哪些是预留的,然后根据需要这边抛出错误
+4
View File
@@ -147,21 +147,25 @@ public class MINPacket implements IEncodingAble {
// 标示解析到了至少一个以上的合法标识
existIdentifier = true;
break;
case TLV.TlvSignatureField: // 签名区
// 解析签名区,保证如果存在签名,则签名均符合格式
if (!this.signatureField.wireDecode(cacheBlock)) {
return false;
}
break;
case TLV.TlvReadOnlyField: // 只读区
// 解析只读区
if (!this.readOnlyField.wireDecode(cacheBlock)) {
return false;
}
break;
case TLV.TlvMutableField: // 可变区
// 解析可变区
if (!this.mutableField.wireDecode(cacheBlock)) {
return false;
}
break;
default:
return false;