package component; import encoding.*; /* * @Author: Wang Feng * @Description: 表示签名的元数据,包含:签名类型、用于签名的证书或公钥的位置 // 1. 格式如下: // SignatureInfo = 201 TLV-LENGTH // // [KeyLocator] // 2. 详情参见:http://gitea.qjm253.cn/PKUSZ-future-network-lab/minlib/src/branch/master/docs/PacketFormat.md#4-signature * @Version: 1.0.0 * @Date: 20:26 2021/3/15 * @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室 */ public class SignatureInfo implements TlvComponentBase, IEncodingAble { private SignatureType signatureType; private KeyLocator 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); } /** * 根据签名值类型和 KeyLocator 创建一个 SignatureInfo * @param value * @param locator */ public SignatureInfo(SignatureAlgorithm value,KeyLocator locator){ this.buildSignatureInfoBySignatureTypeValueAndKeyLocator(value,locator); } /** * 根据 TLV Block 创建一个 SignatureInfo * @param block */ public SignatureInfo(Block block) throws ComponentException { this.buildSignatureInfoByBlock(block); } /** * 根据签名类型值初始化当前 SignatureInfo * @param signatureTypeValue * @return */ public boolean buildSignatureInfoBySignatureTypeValue(SignatureAlgorithm signatureTypeValue){ return this.signatureType.buildSignatureTypeByValue(signatureTypeValue); } /** * 根据签名类型值和 KeyLocator 初始化当前 SignatureInfo * @param value * @param locator * @return */ public boolean buildSignatureInfoBySignatureTypeValueAndKeyLocator(SignatureAlgorithm value,KeyLocator locator){ if(!this.signatureType.buildSignatureTypeByValue(value)){ return false; } if(!this.keyLocator.buildKeyLocatorByIdentifier(keyLocator.getIdentifier())){ return false; } return true; } /** * 根据 TLV Block 初始化当前 SignatureInfo * @param block * @return */ public boolean buildSignatureInfoByBlock(Block block) throws ComponentException { return this.wireDecode(block); } /** * 获得签名类型 * @return */ public SignatureType getSignatureType() { return signatureType; } /** * 获得 KeyLocator * @return */ public KeyLocator getKeyLocator() { return keyLocator; } /** * 将 SignatureInfo 线速编码为 TLV * @param encoder * @return */ @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()); } if(tmplen<=0){ return -1; } totalLength+=tmplen; } int tmplen= 0; tmplen = this.signatureType.wireEncode(encoder); if(tmplen<=0){ return -1; } totalLength+=tmplen; // 编码 TLV-LENGTH try { tmplen=encoder.prependVarNumber(new VlInt(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; } /** * 从 TLV Block 中解码出一个 SignatureInfo * @param block * @return */ @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()){ return false; } } catch (BlockException e) { throw new ComponentException("SignatureInfo.wireDecode: "+e.getMessage()); } boolean findSignatureType=false; int elementSize=block.getSubElements().length(); for (int i=0;i