mirror of
https://gitee.com/willfree/min-dev-java.git
synced 2026-10-04 19:42:01 +08:00
finish Component: SignatureInfo
This commit is contained in:
1 parent
236aecf91d
commit
abaf0dda4a
1 file changed
+182
@@ -0,0 +1,182 @@
|
||||
package component;
|
||||
|
||||
import encoding.*;
|
||||
/*
|
||||
* @Author: Wang Feng
|
||||
* @Description: 表示签名的元数据,包含:签名类型、用于签名的证书或公钥的位置
|
||||
// 1. 格式如下:
|
||||
// SignatureInfo = 201 TLV-LENGTH
|
||||
// <SignatureType>
|
||||
// [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;
|
||||
|
||||
/**
|
||||
* 根据签名值类型创建一个 SignatureInfo
|
||||
* @param value
|
||||
*/
|
||||
public SignatureInfo(SignatureAlgorithm value){
|
||||
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){
|
||||
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){
|
||||
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) {
|
||||
int totalLength=0;
|
||||
// 编码 TLV-VALUE
|
||||
if(!this.keyLocator.isValid()){
|
||||
// KeyLocator 是可选的,如果当前 SignatureInfo 对象有一个有效的 KeyLocator,则对其进行编码
|
||||
int tmplen=this.keyLocator.wireEncode(encoder);
|
||||
if(tmplen<=0){
|
||||
return 0;
|
||||
}
|
||||
totalLength+=tmplen;
|
||||
}
|
||||
int tmplen=this.signatureType.wireEncode(encoder);
|
||||
if(tmplen<=0){
|
||||
return 0;
|
||||
}
|
||||
totalLength+=tmplen;
|
||||
|
||||
// 编码 TLV-LENGTH
|
||||
tmplen=encoder.prependVarNumber(new VlInt(totalLength));
|
||||
if(tmplen<=0){
|
||||
return 0;
|
||||
}
|
||||
totalLength+=tmplen;
|
||||
|
||||
// 编码 TLV-TYPE
|
||||
tmplen=encoder.prependVarNumber(new VlInt(TLV.TlvSignatureInfo));
|
||||
if(tmplen<=0){
|
||||
return 0;
|
||||
}
|
||||
totalLength+=tmplen;
|
||||
|
||||
return totalLength;
|
||||
}
|
||||
|
||||
/**
|
||||
* 从 TLV Block 中解码出一个 SignatureInfo
|
||||
* @param block
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public boolean wireDecode(Block block) {
|
||||
// 检查 Type 是否正确
|
||||
if(!TLV.expectType(block.getType(),new VlInt(TLV.TlvSignatureInfo))){
|
||||
return false;
|
||||
}
|
||||
|
||||
// 解析子 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);
|
||||
switch (cacheBlock.getType().getVlIntValue2Int()){
|
||||
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 Error("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;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user