mirror of
https://gitee.com/willfree/min-dev-java.git
synced 2026-10-04 19:42:01 +08:00
fix: keyChain init function
This commit is contained in:
1 parent
401ae5a1c1
commit
5c7738294f
4 files changed
+97
-76
No files matched your search
+11
-4
@@ -35,7 +35,7 @@ import java.util.concurrent.ConcurrentMap;
|
||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
*/
|
||||
// TODO: 测试
|
||||
public class IdentifyManager {
|
||||
public class IdentityManager {
|
||||
|
||||
public static String DefaultIdentityDBPath = "/usr/local/.mir/identity/";
|
||||
public void setIdentifies(ConcurrentMap<String, Identity> identifies) {
|
||||
@@ -61,11 +61,11 @@ public class IdentifyManager {
|
||||
* @author hongyu guo
|
||||
* @date 2021/3/11
|
||||
**/
|
||||
public IdentifyManager(){
|
||||
public IdentityManager(){
|
||||
init();
|
||||
}
|
||||
|
||||
public IdentifyManager(String dbPath) {
|
||||
public IdentityManager(String dbPath) {
|
||||
try {
|
||||
Sqlite.getInstance().open(dbPath);
|
||||
} catch (Exception e) {
|
||||
@@ -420,7 +420,14 @@ public class IdentifyManager {
|
||||
public Identity getDefaultIdentity() {
|
||||
return defaultIdentity;
|
||||
}
|
||||
|
||||
/**
|
||||
* setter方法!
|
||||
* @param defaultIdentity
|
||||
* @return void
|
||||
* @throws
|
||||
* @author hongyu guo
|
||||
* @date 2021/5/21
|
||||
**/
|
||||
public void setDefaultIdentity(Identity defaultIdentity) {
|
||||
this.defaultIdentity = defaultIdentity;
|
||||
}
|
||||
@@ -13,11 +13,9 @@ import packet.MINPacket;
|
||||
import javax.crypto.BadPaddingException;
|
||||
import javax.crypto.IllegalBlockSizeException;
|
||||
import javax.crypto.NoSuchPaddingException;
|
||||
import java.lang.reflect.Array;
|
||||
import java.security.InvalidKeyException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.NoSuchProviderException;
|
||||
import java.util.IdentityHashMap;
|
||||
|
||||
/*
|
||||
* @Author: hongyu guo
|
||||
@@ -27,24 +25,9 @@ import java.util.IdentityHashMap;
|
||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
*/
|
||||
public class KeyChain {
|
||||
private static final String defaultIdentifyName = "/localhost/operator";
|
||||
public IdentifyManager getIdentifyManager() {
|
||||
return identifyManager;
|
||||
}
|
||||
private static final String defaultIdentityName = "/localhost/operator";
|
||||
|
||||
public void setIdentifyManager(IdentifyManager identifyManager) {
|
||||
this.identifyManager = identifyManager;
|
||||
}
|
||||
|
||||
public Identity getCurrentIdentity() {
|
||||
return currentIdentity;
|
||||
}
|
||||
|
||||
public void setCurrentIdentity(Identity currentIdentity) {
|
||||
this.currentIdentity = currentIdentity;
|
||||
}
|
||||
|
||||
private IdentifyManager identifyManager;
|
||||
private IdentityManager identityManager;
|
||||
private Identity currentIdentity;
|
||||
|
||||
/**
|
||||
@@ -55,13 +38,40 @@ public class KeyChain {
|
||||
* @date 2021/3/11
|
||||
**/
|
||||
public KeyChain() throws Exception{
|
||||
identifyManager = new IdentifyManager();
|
||||
currentIdentity = identifyManager.getDefaultIdentity();
|
||||
// DONE: 考虑是否需要在没有默认身份的时候创建一个缺省的本地网络身份
|
||||
if (currentIdentity == null){
|
||||
Identity newId = this.identifyManager.createIdentityByName(defaultIdentifyName, "", true);
|
||||
this.identifyManager.setDefaultIdentity(newId);
|
||||
this.currentIdentity = newId;
|
||||
// identifyManager = new IdentifyManager();
|
||||
// currentIdentity = identifyManager.getDefaultIdentity();
|
||||
// // DONE: 考虑是否需要在没有默认身份的时候创建一个缺省的本地网络身份
|
||||
// if (currentIdentity == null){
|
||||
// Identity newId = this.identifyManager.createIdentityByName(defaultIdentifyName, "", true);
|
||||
// this.identifyManager.setDefaultIdentity(newId);
|
||||
// this.currentIdentity = newId;
|
||||
// }
|
||||
init(IdentityManager.DefaultIdentityDBPath);
|
||||
}
|
||||
|
||||
public KeyChain(String dbPath) throws Exception {
|
||||
init(dbPath);
|
||||
}
|
||||
|
||||
private void init(String dbPath) throws Exception {
|
||||
identityManager = new IdentityManager(dbPath);
|
||||
currentIdentity = identityManager.getDefaultIdentity();
|
||||
if(identityManager.getDefaultIdentity() == null) {
|
||||
Identity defaultIdentity = identityManager.getIdentityByName(defaultIdentityName);
|
||||
if(defaultIdentity != null) {
|
||||
currentIdentity = defaultIdentity;
|
||||
return;
|
||||
}
|
||||
|
||||
Identity newIdentity = identityManager.createIdentityByName(defaultIdentityName, "", true);
|
||||
if(newIdentity == null) {
|
||||
throw new KeyChainException("init failed: can not create identity [" + defaultIdentityName + "]");
|
||||
}
|
||||
boolean succ = identityManager.setDefaultIdentity(newIdentity, true);
|
||||
if(!succ) {
|
||||
throw new KeyChainException("init failed: can not set default identity [" + newIdentity.getName() + "]");
|
||||
}
|
||||
currentIdentity = newIdentity;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -75,23 +85,15 @@ public class KeyChain {
|
||||
**/
|
||||
public void setCurrentIdentity(Identity identity, String passwd) {
|
||||
try {
|
||||
if(passwd != null && !passwd.equals("")){
|
||||
identity.unLock(passwd, identifyManager.getPrivateKeyEncryptionAlgorithm());
|
||||
currentIdentity = identity;
|
||||
if(!"".equals(passwd) && identity.isLocked()){
|
||||
boolean success = identity.unLock(passwd, identityManager.getPrivateKeyEncryptionAlgorithm());
|
||||
if(!success) {
|
||||
throw new KeyChainException("Unlock " + identity.getName() + " by " + passwd + " failed!!");
|
||||
}
|
||||
}
|
||||
} catch (IdentityException e) {
|
||||
e.printStackTrace();
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
e.printStackTrace();
|
||||
} catch (InvalidKeyException e) {
|
||||
e.printStackTrace();
|
||||
} catch (NoSuchPaddingException e) {
|
||||
e.printStackTrace();
|
||||
} catch (BadPaddingException e) {
|
||||
e.printStackTrace();
|
||||
} catch (NoSuchProviderException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IllegalBlockSizeException e) {
|
||||
currentIdentity = identity;
|
||||
} catch (IdentityException | NoSuchAlgorithmException | InvalidKeyException | NoSuchPaddingException
|
||||
| BadPaddingException | NoSuchProviderException | IllegalBlockSizeException | KeyChainException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
@@ -225,7 +227,7 @@ public class KeyChain {
|
||||
Signature signature = minPacket.signatureField.getSignature(0);
|
||||
|
||||
String identityName = signature.getSigInfo().getKeyLocator().getIdentifier().toUri();
|
||||
Identity identity = this.identifyManager.getIdentityByName(identityName);
|
||||
Identity identity = this.identityManager.getIdentityByName(identityName);
|
||||
|
||||
if (identity == null){
|
||||
throw new KeyChainException("Verify failed, could not find the identity");
|
||||
@@ -289,13 +291,29 @@ public class KeyChain {
|
||||
*/
|
||||
public void importSafeBag(SafeBag safeBag, String passwd, boolean force) throws Exception{
|
||||
if (safeBag == null)
|
||||
throw new KeyChainException(String.format("SafeBag is %s", safeBag));
|
||||
throw new KeyChainException("SafeBag is null");
|
||||
Identity identity = Identity.load(safeBag.getValue(), passwd);
|
||||
|
||||
if (!this.identifyManager.existIdentity(identity.getName()) || force){
|
||||
this.identifyManager.saveIdentity(identity, force, false);
|
||||
if (!this.identityManager.existIdentity(identity.getName()) || force){
|
||||
this.identityManager.saveIdentity(identity, force, false);
|
||||
}else {
|
||||
throw new KeyChainException(String.format("Identify %s is already exists!", identity.getName()));
|
||||
}
|
||||
}
|
||||
|
||||
public IdentityManager getIdentifyManager() {
|
||||
return identityManager;
|
||||
}
|
||||
|
||||
public void setIdentifyManager(IdentityManager identityManager) {
|
||||
this.identityManager = identityManager;
|
||||
}
|
||||
|
||||
public Identity getCurrentIdentity() {
|
||||
return currentIdentity;
|
||||
}
|
||||
|
||||
// public void setCurrentIdentity(Identity currentIdentity) {
|
||||
// this.currentIdentity = currentIdentity;
|
||||
// }
|
||||
}
|
||||
Reference in new issue
Block a user