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