对身份的导入导出做了测试,支持了无证书下的身份导出和导入

This commit is contained in:
free will committed 2021-08-06 11:22:23 +08:00
1 parent fada6a8a25
commit d4bdf84271
5 files changed
+121 -24

No files matched your search

@@ -1,5 +1,11 @@
package examples;
import security.SafeBag;
import util.FileUtil;
import java.io.IOException;
import java.util.Arrays;
/*
* @Author: Wang Feng
* @Description: 测试身份的导入导出
@@ -20,22 +26,76 @@ public class IdentityDumpExample {
* @param username
* @param identityPath
*/
public void generateForeverIdentity(String username,String identityPath){
private void generateForeverIdentity(String username,String identityPath){
// identityName是将username前面加上"/"
String identityName="/"+username;
KeyManagerExample.INSTANCE.initKeyChain(identityName,identityPath);
}
public static void main(String[] args){
/**
* 测试导出用户身份
*/
private void exportIdentity(){
// 生成数据库身份
new IdentityDumpExample().generateForeverIdentity(username,identityPath);
System.out.println("生成的用户身份: "+KeyManagerExample.INSTANCE
.getIdentity("/"+username,identityPath));
// 导出数据库身份
SafeBag safeBag;
try {
KeyManagerExample.INSTANCE.getKeyChain().exportSafeBag("");
safeBag=KeyManagerExample.INSTANCE.getKeyChain().exportSafeBag("");
System.out.println(Arrays.toString(safeBag.getValue()));
} catch (Exception e) {
e.printStackTrace();
return;
}
// 将身份数据写入本地文件
try {
FileUtil.writeFile(identityFilePath,safeBag.getValue());
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 测试导入用户身份
*/
private void importIdentity(){
// 导入身份前查看是否有该用户身份
// System.out.println("试图取出未导入的用户身份: "+KeyManagerExample.INSTANCE
// .getIdentity("/"+username,identityPath));
// 从本地文件中读取身份数据
byte[] identityBytes;
try {
identityBytes=FileUtil.readFile(identityFilePath);
} catch (IOException e) {
e.printStackTrace();
return;
}
// 转成SafeBag
SafeBag safeBag=new SafeBag(identityBytes);
// 导入到本地身份数据库中
try {
KeyManagerExample.INSTANCE.getKeyChain().importSafeBag(safeBag,"",true);
} catch (Exception e) {
e.printStackTrace();
}
// 导入身份后查看是否有该用户身份
System.out.println("查看是否成功导入用户身份: "+KeyManagerExample.INSTANCE
.getIdentity("/"+username,identityPath));
}
public static void main(String[] args){
// 初始化KeyManager
KeyManagerExample.INSTANCE.initKeyChain("/default",identityPath);
// 测试生成并导出用户身份
// new IdentityDumpExample().exportIdentity();
// 测试导入用户身份
System.out.println("当前用户身份: "+
KeyManagerExample.INSTANCE.getKeyChain().getCurrentIdentity().getName());
new IdentityDumpExample().importIdentity();
System.out.println("当前用户身份: "+
KeyManagerExample.INSTANCE.getKeyChain().getCurrentIdentity().getName());
}
}
+4 -4
View File
@@ -234,9 +234,9 @@ public class VPNLoginExample {
// 测试注册登录
public static void main(String[] args){
new VPNLoginExample().testRegister();
// new VPNLoginExample().testLogin();
// VpnServerInfoListExample infoList = new VPNLoginExample().testGetVPNInfo();
// System.out.println(infoList);
// new VPNLoginExample().testRegister();
//// new VPNLoginExample().testLogin();
VpnServerInfoListExample infoList = new VPNLoginExample().testGetVPNInfo();
System.out.println(infoList);
}
}
+23 -8
View File
@@ -147,6 +147,18 @@ public class CommandExecutor {
if(this.keyChain!=null){
this.keyChain.signInterest(interest);
}
// 测试字段-------------------
System.out.println("CommandExecutor-构造的兴趣包的签名字段: "+
Arrays.toString(interest.minPacket.signatureField.getSignature(0)
.getSigValue().getValue()));
Encoder encoder=new Encoder();
if(!encoder.encoderReset(new SizeT(Encoder.MaxPacketSize),new SizeT(0))){
System.out.println("fucking test");
}
int bufLen=interest.wireEncode(encoder);
byte[] buf=encoder.getBuffer();
System.out.println("CommandExecutor-构造的兴趣包的最终形态: "+ Arrays.toString(buf));
// 测试字段---------------------
return interest;
} catch (Exception e) {
throw new MgmtException("CommandExecutor.newCommandInterest: "+e.getMessage());
@@ -257,11 +269,16 @@ public class CommandExecutor {
// 打印输出
try {
System.out.println(Arrays.toString(new SelfEncodingBase()
System.out.println("旧的输出【最最终形态】: "+Arrays.toString(new SelfEncodingBase()
.selfWireEncode(commandInterest).getRaw()));
} catch (EncoderException e) {
e.printStackTrace();
} catch (BlockException e) {
Encoder encoder=new Encoder();
if(!encoder.encoderReset(new SizeT(Encoder.MaxPacketSize),new SizeT(0))){
System.out.println("fucking test");
}
int bufLen=commandInterest.wireEncode(encoder);
byte[] buf=encoder.getBuffer();
System.out.println("新的输出【最最终形态】: "+ Arrays.toString(buf));
} catch (EncoderException | BlockException e) {
e.printStackTrace();
}
@@ -273,11 +290,9 @@ public class CommandExecutor {
// 等待兴趣包应答
MINPacket minPacket=this.logicFace.receivePacket(commandInterest.interestLifeTime.getInterestLifeTime());
try {
System.out.println(Arrays.toString(new SelfEncodingBase()
System.out.println("收到的注册应答包:"+Arrays.toString(new SelfEncodingBase()
.selfWireEncode(minPacket).getRaw()));
} catch (EncoderException e) {
e.printStackTrace();
} catch (BlockException e) {
} catch (EncoderException | BlockException e) {
e.printStackTrace();
}
@@ -24,9 +24,14 @@ import java.security.NoSuchProviderException;
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
public class IdentityUtil {
// 标记在导入导出用户身份时,是否要忽略掉证书。false表示忽略掉
private static boolean haveUsedCert = false;
/**
* 将identity转化为内部结构(innerIdentity), 便于编码
* identity的公私钥以及证书都不能为空
* => wefree: 为了保证客户端程序在没有接收区块链后台颁发的证书时,仍然能够导出身份,
* 这里将默认导出不包含证书的身份。
* @param identity
* @return minsecurity.identity.InnerIdentity
* @throws CertException ...
@@ -37,19 +42,27 @@ public class IdentityUtil {
throws NoSuchPaddingException, InvalidKeyException, CertException,
IllegalBlockSizeException, BadPaddingException, NoSuchAlgorithmException,
NoSuchProviderException, InvalidAlgorithmParameterException, IdentityException {
if(identity.getPubkey() == null)
if (identity.getPubkey() == null)
throw new IdentityException("public key in identity is null");
if(identity.getPrikey() == null)
if (identity.getPrikey() == null)
throw new IdentityException("private key in identity is null");
if(identity.getCert() == null)
throw new IdentityException("certificate in identity is null");
if (identity.getCert() == null)
if (haveUsedCert) {
throw new IdentityException("certificate in identity is null");
} else{
System.out.println("certificate in identity is null");
}
InnerIdentity innerIdentity = new InnerIdentity();
innerIdentity.setName(identity.getName());
innerIdentity.setKeyParam(identity.getKeyParam());
innerIdentity.setPrikey(identity.getPrikey().getBytes());
innerIdentity.setPubkey(identity.getPubkey().getBytes());
innerIdentity.setPasswd(identity.getPasswd());
innerIdentity.setCert(CertUtils.toPem(identity.getCert(), identity.getPasswd().getBytes(), Common.SM4CBC));
if(haveUsedCert) {
innerIdentity.setCert(CertUtils.toPem(identity.getCert(), identity.getPasswd().getBytes(), Common.SM4CBC));
}else {
innerIdentity.setCert(null);
}
// add
innerIdentity.setPrikeyRawByte(identity.getPrikeyRawByte());
return innerIdentity;
@@ -82,12 +95,15 @@ public class IdentityUtil {
KeyUtils.unMarshalPrivateKey(innerIdentity.getPrikey(), innerIdentity.getKeyParam().PublicKeyAlgorithm);
identity.setPrikey(priv);
identity.setPasswd(innerIdentity.getPasswd());
Certificate certificate =
CertUtils.fromPem(innerIdentity.getCert(), innerIdentity.getPasswd().getBytes(), Common.SM4CBC);
identity.setCert(certificate);
if(haveUsedCert) {
Certificate certificate =
CertUtils.fromPem(innerIdentity.getCert(), innerIdentity.getPasswd().getBytes(), Common.SM4CBC);
identity.setCert(certificate);
}else{
identity.setCert(null);
}
identity.setPrikeyRawByte(innerIdentity.getPrikeyRawByte());
return identity;
}
}
+6
View File
@@ -347,6 +347,12 @@ public class KeyChain {
throw new KeyChainException("SafeBag is null");
Identity identity = Identity.load(safeBag.getValue(), passwd);
// 测试代码
if(this.identityManager.existIdentity(identity.getName())){
System.out.println("已存在该身份: "+identity.getName());
System.out.println("已存在该身份: "+identity);
}
if (!this.identityManager.existIdentity(identity.getName()) || force){
this.identityManager.saveIdentity(identity, force, false);
}else {