mirror of
https://gitee.com/willfree/min-dev-java.git
synced 2026-10-04 19:42:01 +08:00
219 lines
6.5 KiB
Java
219 lines
6.5 KiB
Java
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;
|
|
|
|
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<elementSize;i++) {
|
|
Block cacheBlock= null;
|
|
try {
|
|
cacheBlock = block.getSubElements().getBlock(i);
|
|
} catch (BlockException e) {
|
|
throw new ComponentException("SignatureInfo.wireDecode:"+e.getMessage());
|
|
}
|
|
int type= 0;
|
|
try {
|
|
type = cacheBlock.getType().getVlIntValue2Int();
|
|
} catch (VlIntException e) {
|
|
e.printStackTrace();
|
|
}
|
|
switch (type){
|
|
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 ComponentException("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;
|
|
}
|
|
}
|