From 025d2f1c62083bdf6e7f307e7ec0a9a2e5358ea0 Mon Sep 17 00:00:00 2001 From: free will <2647778488@qq.com> Date: Wed, 31 Mar 2021 19:07:56 +0800 Subject: [PATCH 1/8] add test: SignatureTypeTest --- src/main/java/component/SignatureType.java | 85 +++++++++---------- .../java/component/SignatureTypeTest.java | 76 +++++++++++++++++ 2 files changed, 114 insertions(+), 47 deletions(-) create mode 100644 src/test/java/component/SignatureTypeTest.java diff --git a/src/main/java/component/SignatureType.java b/src/main/java/component/SignatureType.java index c141ca3..dfc68c3 100644 --- a/src/main/java/component/SignatureType.java +++ b/src/main/java/component/SignatureType.java @@ -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; } } diff --git a/src/test/java/component/SignatureTypeTest.java b/src/test/java/component/SignatureTypeTest.java new file mode 100644 index 0000000..f835bc0 --- /dev/null +++ b/src/test/java/component/SignatureTypeTest.java @@ -0,0 +1,76 @@ +package component; + +import encoding.*; +import org.junit.Test; +import packet.PacketException; + +import java.util.Arrays; + +import static org.junit.Assert.assertEquals; + +/* + * @Author: Wang Feng + * @Description: + * @Version: 1.0.0 + * @Date: 10:48 2021/3/31 + * @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室 + */ +public class SignatureTypeTest { + @Test + public void TestCreateSignatureTypeByBlock() throws EncoderException, ComponentException, BlockException { + Encoder encoder=new Encoder(); + encoder.encoderReset(new SizeT(Encoder.MaxPacketSize),new SizeT(0)); + String str="/min/pkusz"; + IdentifierWrapper got=new IdentifierWrapper().createCommonIdentifierByString(str); + System.out.println("type: "+got.getTlvType().toString()); + got.wireEncode(encoder); + byte[] rawBuf=encoder.getBuffer(); + System.out.println("buf: "+ Arrays.toString(rawBuf)); + byte[] buf2=new byte[4]; + System.arraycopy(rawBuf,14,buf2,0,4); + Block block=new Block(new VlInt(202),new VlInt(4),buf2,true); + System.out.println("block length: "+block.getLength()); + SignatureType gotContent=new SignatureType(block); + System.out.println("sigtype: "+gotContent.getValue().getSignatureAlgorithm()); + } + + @Test + public void TestCreateSignatureTypeByValue() throws PacketException, EncoderException, BlockException, ComponentException { + SignatureAlgorithm value=new SignatureAlgorithm(0); + SignatureType got=new SignatureType(value); + System.out.println(got.getValue().getSignatureAlgorithm()); + Block rawBlock=new SelfEncodingBase().selfWireEncode(got); + System.out.println(rawBlock.getType()+" "+rawBlock.getLength()+" "+Arrays.toString(rawBlock.getValue())); + } + + @Test + public void TestSignatureType_WireEncode() throws PacketException, EncoderException, BlockException, ComponentException { + SignatureAlgorithm value=new SignatureAlgorithm(0); + SignatureType got=new SignatureType(value); + System.out.println(got.getValue().getSignatureAlgorithm()); + Block rawBlock=new SelfEncodingBase().selfWireEncode(got); + System.out.println(rawBlock.getType()+" "+rawBlock.getLength()+" "+Arrays.toString(rawBlock.getValue())); + Encoder encoder=new Encoder(); + encoder.encoderReset(new SizeT(Encoder.MaxPacketSize),new SizeT(0)); + got.wireEncode(encoder); + byte[] gotBuf=encoder.getBuffer(); + System.out.println(Arrays.toString(gotBuf)); + } + + @Test + public void TestCreateSignatureTypeByValue2() throws PacketException, EncoderException, BlockException, ComponentException { + SignatureAlgorithm value=new SignatureAlgorithm(0); + SignatureType got=new SignatureType(value); + System.out.println(got.getValue().getSignatureAlgorithm()); + Block rawBlock=new SelfEncodingBase().selfWireEncode(got); + System.out.println(rawBlock.getType()+" "+rawBlock.getLength()+" "+Arrays.toString(rawBlock.getValue())); + Encoder encoder=new Encoder(); + encoder.encoderReset(new SizeT(Encoder.MaxPacketSize),new SizeT(0)); + got.wireEncode(encoder); + byte[] gotBuf=encoder.getBuffer(); + System.out.println(Arrays.toString(gotBuf)); + SignatureType newSig=new SignatureType(); + newSig.wireDecode(rawBlock); + System.out.println(newSig.getValue().getSignatureAlgorithm()); + } +} From c8f0e35f1b76b4737221554b83a791b129dfa84d Mon Sep 17 00:00:00 2001 From: free will <2647778488@qq.com> Date: Wed, 31 Mar 2021 20:27:01 +0800 Subject: [PATCH 2/8] add test: KeyLocatorTest --- .../java/component/IdentifierComponent.java | 3 +- src/main/java/component/KeyLocator.java | 74 ++++++------- src/main/java/encoding/Block.java | 1 - src/test/java/component/KeyLocatorTest.java | 100 ++++++++++++++++++ 4 files changed, 136 insertions(+), 42 deletions(-) create mode 100644 src/test/java/component/KeyLocatorTest.java diff --git a/src/main/java/component/IdentifierComponent.java b/src/main/java/component/IdentifierComponent.java index 5f969dc..839c130 100644 --- a/src/main/java/component/IdentifierComponent.java +++ b/src/main/java/component/IdentifierComponent.java @@ -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(); } } } diff --git a/src/main/java/component/KeyLocator.java b/src/main/java/component/KeyLocator.java index 2365b27..8270ebe 100644 --- a/src/main/java/component/KeyLocator.java +++ b/src/main/java/component/KeyLocator.java @@ -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(totalLength)); + 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) { diff --git a/src/main/java/encoding/Block.java b/src/main/java/encoding/Block.java index 856c12d..0f74ba3 100644 --- a/src/main/java/encoding/Block.java +++ b/src/main/java/encoding/Block.java @@ -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 diff --git a/src/test/java/component/KeyLocatorTest.java b/src/test/java/component/KeyLocatorTest.java new file mode 100644 index 0000000..a1a226b --- /dev/null +++ b/src/test/java/component/KeyLocatorTest.java @@ -0,0 +1,100 @@ +package component; + +import encoding.*; +import org.checkerframework.checker.units.qual.K; +import org.junit.Test; +import packet.PacketException; + +import java.util.Arrays; + +/* + * @Author: Wang Feng + * @Description: + * @Version: 1.0.0 + * @Date: 19:08 2021/3/31 + * @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室 + */ +public class KeyLocatorTest { + @Test + public void TestCreateKeyLocatorByString() throws ComponentException { + String str="/install/hello/王"; + KeyLocator got=new KeyLocator(str); + System.out.println("keyLocator: "+got.getIdentifier().toUri()); + } + + @Test + public void TestCreateKeyLocatorByBlock() throws EncoderException, ComponentException, BlockException { + Encoder encoder=new Encoder(); + encoder.encoderReset(new SizeT(Encoder.MaxPacketSize),new SizeT(0)); + String str="/min/王/123/哈哈哈哈"; + IdentifierWrapper got=new IdentifierWrapper().createCommonIdentifierByString(str); + System.out.println("type: "+got.getTlvType()); + got.wireEncode(encoder); + byte[] rawBuf=encoder.getBuffer(); + System.out.println("buf: "+ Arrays.toString(rawBuf)); +// rawBuf[4]=101; + byte[] buf2=new byte[rawBuf.length-2]; + System.arraycopy(rawBuf,2,buf2,0,rawBuf.length-2); + System.out.println("buf2: "+ Arrays.toString(buf2)); + Block block=new Block(new VlInt(203),new VlInt(rawBuf.length-2),buf2,true); + block.parseSubElements(); + System.out.println(block.getSubElements().getBlock(0).getType()); +// System.out.println(block.getSubElements().getBlock(1).getType()); + KeyLocator gotKey=new KeyLocator(block); + System.out.println(gotKey.getIdentifier().toUri()); + System.out.println(gotKey.isValid()); + } + + @Test + public void TestCreateKeyLocatorByComponents() throws ComponentException { + IdentifierComponent component=new IdentifierComponent("min"); + IdentifierComponent component1=new IdentifierComponent("pku"); + IdentifierComponent component2=new IdentifierComponent("王"); + IdentifierComponentContainer gotComponent=new IdentifierComponentContainer(); + gotComponent.addElement(component); + gotComponent.addElement(component1); + gotComponent.addElement(component2); + KeyLocator gotKey=new KeyLocator(gotComponent); + System.out.println(gotKey.getIdentifier().toUri()); + } + + @Test + public void TestCreateKeyLocatorByIdentifier() throws ComponentException, EncoderException { + Identifier gotIdentifier=new Identifier("/min/pku/111"); + KeyLocator gotKey=new KeyLocator(gotIdentifier); + System.out.println(gotKey.getIdentifier().toUri()); + + IdentifierComponent component=new IdentifierComponent("min"); + IdentifierComponent component1=new IdentifierComponent("pku"); + IdentifierComponent component2=new IdentifierComponent(111); + IdentifierComponentContainer gotComponent=new IdentifierComponentContainer(); + gotComponent.addElement(component); + gotComponent.addElement(component1); + gotComponent.addElement(component2); + KeyLocator gotKey1=new KeyLocator(gotComponent); + System.out.println(gotKey1.getIdentifier().toUri()); + + Encoder encoder=new Encoder(); + encoder.encoderReset(new SizeT(Encoder.MaxPacketSize),new SizeT(0)); + gotKey.wireEncode(encoder); + System.out.println(Arrays.toString(encoder.getBuffer())); + + Encoder encoder1=new Encoder(); + encoder1.encoderReset(new SizeT(Encoder.MaxPacketSize),new SizeT(0)); + gotKey1.wireEncode(encoder1); + System.out.println(Arrays.toString(encoder1.getBuffer())); + } + + @Test + public void TestKeyLocator_WireDecode() throws ComponentException, BlockException, EncoderException, PacketException { + Identifier gotIdentifier=new Identifier("/min/pku/111"); + KeyLocator gotKey=new KeyLocator(gotIdentifier); + System.out.println(gotKey.getIdentifier().toUri()); + + Block block=new SelfEncodingBase().selfWireEncode(gotKey); + + KeyLocator newK=new KeyLocator(); + newK.wireDecode(block); + System.out.println(newK.getIdentifier().toUri()); + } +} From c45667cc8dc31ffefb88204b939e34a1b8dd6700 Mon Sep 17 00:00:00 2001 From: free will <2647778488@qq.com> Date: Thu, 1 Apr 2021 17:18:11 +0800 Subject: [PATCH 3/8] add test: SignatureInfoTest --- src/main/java/component/KeyLocator.java | 2 +- src/main/java/component/SignatureInfo.java | 171 ++++++++---------- src/main/java/packet/Data.java | 2 + src/main/java/packet/Interest.java | 6 + src/main/java/packet/MINPacket.java | 4 + .../java/component/SignatureInfoTest.java | 93 ++++++++++ 6 files changed, 182 insertions(+), 96 deletions(-) create mode 100644 src/test/java/component/SignatureInfoTest.java diff --git a/src/main/java/component/KeyLocator.java b/src/main/java/component/KeyLocator.java index 8270ebe..1cffd4e 100644 --- a/src/main/java/component/KeyLocator.java +++ b/src/main/java/component/KeyLocator.java @@ -130,7 +130,7 @@ public class KeyLocator implements TlvComponentBase, IEncodingAble { totalLength += tmplen; // 编码 TLV-TYPE - tmplen = encoder.prependVarNumber(new VlInt(totalLength)); + tmplen = encoder.prependVarNumber(new VlInt(TLV.TlvKeyLocator)); if (tmplen <= 0) { return -1; } diff --git a/src/main/java/component/SignatureInfo.java b/src/main/java/component/SignatureInfo.java index d4386d7..8522e27 100644 --- a/src/main/java/component/SignatureInfo.java +++ b/src/main/java/component/SignatureInfo.java @@ -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 Date: Thu, 1 Apr 2021 20:50:54 +0800 Subject: [PATCH 4/8] add test: SignatureValueTest --- src/main/java/component/SignatureValue.java | 84 +++++++++---------- .../java/component/SignatureValueTest.java | 69 +++++++++++++++ 2 files changed, 108 insertions(+), 45 deletions(-) create mode 100644 src/test/java/component/SignatureValueTest.java diff --git a/src/main/java/component/SignatureValue.java b/src/main/java/component/SignatureValue.java index 05d5c13..ddf81c6 100644 --- a/src/main/java/component/SignatureValue.java +++ b/src/main/java/component/SignatureValue.java @@ -19,8 +19,10 @@ public class SignatureValue implements TlvComponentBase, IEncodingAble { * 根据签名值字节数组创建一个 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 +30,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 +70,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 +106,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; } } diff --git a/src/test/java/component/SignatureValueTest.java b/src/test/java/component/SignatureValueTest.java new file mode 100644 index 0000000..976cb24 --- /dev/null +++ b/src/test/java/component/SignatureValueTest.java @@ -0,0 +1,69 @@ +package component; + +import encoding.*; +import org.junit.Test; + +import java.util.Arrays; + +/* + * @Author: Wang Feng + * @Description: + * @Version: 1.0.0 + * @Date: 20:21 2021/4/1 + * @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室 + */ +public class SignatureValueTest { + @Test + public void TestCreateSignatureValueByByteArray() throws ComponentException { + byte[] buf={(byte)1, (byte)2, (byte)3, (byte)4, (byte)5, (byte)6, (byte)7, (byte)8}; + SignatureValue got=new SignatureValue(buf); + System.out.println(Arrays.toString(got.getValue())); + } + + @Test + public void TestCreateSignatureValueByBlock() throws EncoderException, ComponentException, BlockException { + Encoder encoder=new Encoder(); + encoder.encoderReset(new SizeT(Encoder.MaxPacketSize),new SizeT(0)); + String str="/min/pku"; + IdentifierWrapper got=new IdentifierWrapper().createCommonIdentifierByString(str); + System.out.println("type: "+got.getTlvType()); + got.wireEncode(encoder); + byte[] rawBuf=encoder.getBuffer(); + System.out.println("buf: "+Arrays.toString(rawBuf)); + + byte[] a=new byte[4]; + System.arraycopy(rawBuf,12,a,0,4); + Block block=new Block(new VlInt(200),new VlInt(4),a,true); + SignatureValue gotValue=new SignatureValue(block); + System.out.println("sv value: "+Arrays.toString(gotValue.getValue())); + } + + @Test + public void TestSignatureValue_WireEncode() throws BlockException, ComponentException, EncoderException { + Encoder encoder=new Encoder(); + encoder.encoderReset(new SizeT(Encoder.MaxPacketSize),new SizeT(0)); + String str="/min/pku"; + IdentifierWrapper got=new IdentifierWrapper().createCommonIdentifierByString(str); + System.out.println("type: "+got.getTlvType()); + got.wireEncode(encoder); + byte[] rawBuf=encoder.getBuffer(); + System.out.println("buf: "+Arrays.toString(rawBuf)); + + byte[] a=new byte[4]; + System.arraycopy(rawBuf,12,a,0,4); + Block block=new Block(new VlInt(200),new VlInt(4),a,true); + SignatureValue gotValue=new SignatureValue(block); + System.out.println("sv value: "+Arrays.toString(gotValue.getValue())); + + encoder.encoderReset(new SizeT(Encoder.MaxPacketSize),new SizeT(0)); + gotValue.wireEncode(encoder); + System.out.println("all: "+Arrays.toString(encoder.getBuffer())); + byte[] bytes=encoder.getBuffer(); + Block block1=new Block(bytes,false); + SignatureValue newSig=new SignatureValue(block1); + System.out.println("new value: "+Arrays.toString(newSig.getValue())); + encoder.encoderReset(new SizeT(Encoder.MaxPacketSize),new SizeT(0)); + newSig.wireEncode(encoder); + System.out.println("new all: "+Arrays.toString(encoder.getBuffer())); + } +} From d61affa7612ce0e98e35a96d71652a5f7146666b Mon Sep 17 00:00:00 2001 From: zhaofeng233 Date: Thu, 1 Apr 2021 20:55:27 +0800 Subject: [PATCH 5/8] =?UTF-8?q?sm3=E5=B0=8FTest?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/minsecurity/crypto/TestSM3.java | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 src/test/java/minsecurity/crypto/TestSM3.java diff --git a/src/test/java/minsecurity/crypto/TestSM3.java b/src/test/java/minsecurity/crypto/TestSM3.java new file mode 100644 index 0000000..e02ea03 --- /dev/null +++ b/src/test/java/minsecurity/crypto/TestSM3.java @@ -0,0 +1,31 @@ +package minsecurity.crypto; + +import org.bouncycastle.pqc.math.linearalgebra.ByteUtils; +import org.junit.Test; + +import static org.junit.Assert.*; + +/* + * @Author: feng Zhao + * @Description: + * @Version: 1.0.0 + * @Date: 14:56 2021/3/29 + * @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室 + */ + +public class TestSM3 { + + @Test + public void testSm3() { + HashAlgo hashAlgo = new HashAlgo(); + String srcString = "123456"; + byte[] srcData = srcString.getBytes(); + byte[] resultHash = hashAlgo.sm3(srcData); + String sm3HexString = ByteUtils.toHexString(resultHash); + + byte[] sm3Hash = ByteUtils.fromHexString(sm3HexString); + byte[] newHash = hashAlgo.sm3(srcString); + assertArrayEquals(sm3Hash,newHash); + } + +} From 0555c43f4e36c1683f85f35582a7bd2dd40efaa9 Mon Sep 17 00:00:00 2001 From: free will <2647778488@qq.com> Date: Thu, 1 Apr 2021 20:59:40 +0800 Subject: [PATCH 6/8] add test: SignatureTest --- src/main/java/component/Signature.java | 35 +++++++++++---------- src/main/java/component/SignatureValue.java | 3 ++ src/test/java/component/SignatureTest.java | 21 +++++++++++++ 3 files changed, 43 insertions(+), 16 deletions(-) create mode 100644 src/test/java/component/SignatureTest.java diff --git a/src/main/java/component/Signature.java b/src/main/java/component/Signature.java index c81173c..429c631 100644 --- a/src/main/java/component/Signature.java +++ b/src/main/java/component/Signature.java @@ -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; + } } diff --git a/src/main/java/component/SignatureValue.java b/src/main/java/component/SignatureValue.java index ddf81c6..4904e82 100644 --- a/src/main/java/component/SignatureValue.java +++ b/src/main/java/component/SignatureValue.java @@ -15,6 +15,9 @@ import encoding.*; public class SignatureValue implements TlvComponentBase, IEncodingAble { private byte[] value; + public SignatureValue(){ + } + /** * 根据签名值字节数组创建一个 SignatureValue * @param value diff --git a/src/test/java/component/SignatureTest.java b/src/test/java/component/SignatureTest.java new file mode 100644 index 0000000..a5c27ae --- /dev/null +++ b/src/test/java/component/SignatureTest.java @@ -0,0 +1,21 @@ +package component; + +import org.junit.Test; + +import java.util.Arrays; + +/* + * @Author: Wang Feng + * @Description: + * @Version: 1.0.0 + * @Date: 20:52 2021/4/1 + * @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室 + */ +public class SignatureTest { + @Test + public void testSignature(){ + Signature signature=new Signature(); + System.out.println(signature.getSigInfo().getSignatureType().getValue().getSignatureAlgorithm()); + System.out.println(Arrays.toString(signature.getSigValue().getValue())); + } +} From ee70be62f957ffc604eaa4c954af47d6ed4a3ee7 Mon Sep 17 00:00:00 2001 From: zhaofeng233 Date: Thu, 1 Apr 2021 21:05:11 +0800 Subject: [PATCH 7/8] =?UTF-8?q?sm3=E5=B0=8FTest?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/minsecurity/crypto/HashAlgo.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/minsecurity/crypto/HashAlgo.java b/src/main/java/minsecurity/crypto/HashAlgo.java index 949cb87..a25408f 100644 --- a/src/main/java/minsecurity/crypto/HashAlgo.java +++ b/src/main/java/minsecurity/crypto/HashAlgo.java @@ -19,7 +19,6 @@ import java.nio.charset.StandardCharsets; * @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室 */ public class HashAlgo { - // TODO: 单元测试 基本上只需要对sm3做单元测试 /////////////// ///// SM3 ///// /////////////// From f3d8c927856663806c7b1d141d6d99e14e00b3bd Mon Sep 17 00:00:00 2001 From: zhaofeng233 Date: Thu, 1 Apr 2021 21:06:14 +0800 Subject: [PATCH 8/8] =?UTF-8?q?=E8=A7=A3=E5=86=B3HashAlgo=20todo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/minsecurity/crypto/HashAlgo.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/minsecurity/crypto/HashAlgo.java b/src/main/java/minsecurity/crypto/HashAlgo.java index a25408f..c88a5d9 100644 --- a/src/main/java/minsecurity/crypto/HashAlgo.java +++ b/src/main/java/minsecurity/crypto/HashAlgo.java @@ -19,6 +19,7 @@ import java.nio.charset.StandardCharsets; * @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室 */ public class HashAlgo { + /////////////// ///// SM3 ///// ///////////////