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..1cffd4e 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(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) { 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/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