mirror of
https://gitee.com/willfree/min-dev-java.git
synced 2026-06-18 06:00:25 +08:00
db unittest completed(annotations added to be compatible with go version's json)
This commit is contained in:
@@ -177,6 +177,7 @@ public class CertUtils {
|
||||
case Common.TbsCertificate:
|
||||
return mapper.readValue(bytesOfCert, TbsCertificate.class);
|
||||
case Common.InnerCertificate:
|
||||
// Class c = InnerCertificate.class;
|
||||
return mapper.readValue(bytesOfCert, InnerCertificate.class);
|
||||
default:
|
||||
throw new CertException("未知证书类型");
|
||||
|
||||
@@ -1,10 +1,8 @@
|
||||
package minsecurity.certificate.cert;
|
||||
|
||||
import minsecurity.Common;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import org.bouncycastle.pqc.math.linearalgebra.ByteUtils;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
/*
|
||||
* @Author: hongyu guo
|
||||
* @Description: 将签名和签名类型单独拎出来, 签名时签TbsCertificate
|
||||
@@ -13,8 +11,11 @@ import java.util.Arrays;
|
||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
*/
|
||||
public class InnerCertificate {
|
||||
@JsonProperty("TBSCertificate") // 与go版本存储的identity兼容
|
||||
private TbsCertificate tbsCertificate;
|
||||
@JsonProperty("SignatureAlgorithm")
|
||||
private int signatureAlgorithm;
|
||||
@JsonProperty("SignatureValue")
|
||||
private byte[] signatureValue;
|
||||
|
||||
public TbsCertificate getTbsCertificate() {
|
||||
@@ -29,7 +30,8 @@ public class InnerCertificate {
|
||||
return signatureAlgorithm;
|
||||
}
|
||||
|
||||
public void setSignatureAlgorithm(int signatureAlgorithm) {
|
||||
public void setSignatureAlgorithm(int signatureAlgorithm)
|
||||
{
|
||||
this.signatureAlgorithm = signatureAlgorithm;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package minsecurity.certificate.cert;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import minsecurity.Common;
|
||||
import org.bouncycastle.pqc.math.linearalgebra.ByteUtils;
|
||||
|
||||
@@ -13,17 +14,29 @@ import java.util.Arrays;
|
||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
*/
|
||||
public class TbsCertificate {
|
||||
@JsonProperty("Version")
|
||||
private int version;
|
||||
@JsonProperty("SerialNumber")
|
||||
private long serialNumber;
|
||||
@JsonProperty("PublicKey")
|
||||
private byte[] publicKey;
|
||||
@JsonProperty("SignatureAlgorithm")
|
||||
private int signatureAlgorithm;
|
||||
@JsonProperty("PublicKeyAlgorithm")
|
||||
private int publicKeyAlgorithm;
|
||||
@JsonProperty("IssueTo")
|
||||
private String issueTo;
|
||||
@JsonProperty("Issuer")
|
||||
private String issuer;
|
||||
@JsonProperty("NotBefore")
|
||||
private long notBefore;
|
||||
@JsonProperty("NotAfter")
|
||||
private long notAfter;
|
||||
@JsonProperty("KeyUsage")
|
||||
private int keyUsage;
|
||||
@JsonProperty("IsCA")
|
||||
private boolean isCA;
|
||||
@JsonProperty("Timestamp")
|
||||
private long timestamp;
|
||||
|
||||
|
||||
|
||||
@@ -59,6 +59,7 @@ public class SM2PrivateKey implements PrivateKeyInterface {
|
||||
|
||||
@Override
|
||||
public boolean setBytes(byte[] d) {
|
||||
// TODO 待与go版本进行兼容 目前版本无法使用
|
||||
if(d.length != 32 && d.length != 33)
|
||||
return false;
|
||||
privateKey = new ECPrivateKeyParameters(new BigInteger(d), SM2Base.DOMAIN_PARAMS);
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package minsecurity.identity.persist.sqlite;
|
||||
|
||||
import org.sqlite.mc.SQLiteMCConfig;
|
||||
import org.sqlite.mc.SQLiteMCSqlCipherConfig;
|
||||
|
||||
import java.io.File;
|
||||
@@ -72,15 +71,16 @@ public class Sqlite {
|
||||
try{
|
||||
String homePath = SqliteUtil.home();
|
||||
homePath += "/min/identity/";
|
||||
String dbPath = homePath;
|
||||
if (!SqliteUtil.pathExists(dbPath)){
|
||||
new File(dbPath).mkdirs();
|
||||
db_path = homePath;
|
||||
if (!SqliteUtil.pathExists(db_path)){
|
||||
new File(db_path).mkdirs();
|
||||
}
|
||||
String real_db_file = dbPath + db_file;
|
||||
String real_db_file = db_path + db_file;
|
||||
// 兼容sqlcipher
|
||||
c = DriverManager.getConnection("jdbc:sqlite:" + real_db_file, SQLiteMCSqlCipherConfig.getV4Defaults().withKey(passwd2HexKey()).toProperties());
|
||||
stmt = c.createStatement();
|
||||
stmt.executeUpdate(table_create);
|
||||
int res = stmt.executeUpdate(table_create);
|
||||
System.out.println(res);
|
||||
stmt.close();
|
||||
c.close();
|
||||
}catch (Exception ex){
|
||||
@@ -94,11 +94,12 @@ public class Sqlite {
|
||||
Connection c = null;
|
||||
Statement stmt = null;
|
||||
try{
|
||||
boolean db_exists = SqliteUtil.pathExists(filePath);
|
||||
db_path = filePath;
|
||||
boolean db_exists = SqliteUtil.pathExists(db_path);
|
||||
if (!db_exists){
|
||||
new File(filePath).mkdirs();
|
||||
new File(db_path).mkdirs();
|
||||
}
|
||||
String real_db_file = filePath + db_file;
|
||||
String real_db_file = db_path + db_file;
|
||||
// 兼容sqlcipher
|
||||
c = DriverManager.getConnection("jdbc:sqlite:" + real_db_file, SQLiteMCSqlCipherConfig.getV4Defaults().withKey(passwd2HexKey()).toProperties());
|
||||
stmt = c.createStatement();
|
||||
@@ -117,7 +118,7 @@ public class Sqlite {
|
||||
Connection c = null;
|
||||
try{
|
||||
String real_db_file = db_path + db_file;
|
||||
Class.forName("org.sqlite.JDBC");
|
||||
// Class.forName("org.sqlite.JDBC");
|
||||
// 兼容sqlcipher
|
||||
c = DriverManager.getConnection("jdbc:sqlite:" + real_db_file, SQLiteMCSqlCipherConfig.getV4Defaults().withKey(passwd2HexKey()).toProperties());
|
||||
// TODO 设置数据库最大连接数
|
||||
|
||||
@@ -20,9 +20,13 @@ public final class Db {
|
||||
private static Identity getIdentityByNameFromStorage(String name, Connection c) throws Exception {
|
||||
PreparedStatement pstmt = c.prepareStatement("SELECT * FROM identityinfo WHERE name = ? LIMIT 1");// LIMIT 1代替Queryrow
|
||||
pstmt.setString(1, name);
|
||||
|
||||
ResultSet rs = pstmt.executeQuery();
|
||||
Identity id = getIdentityFromSqlRow(rs);
|
||||
|
||||
rs.close();
|
||||
pstmt.close();
|
||||
return getIdentityFromSqlRow(rs);
|
||||
return id;
|
||||
}
|
||||
|
||||
private static Identity getIdentityFromSqlRow(ResultSet rs) throws Exception {
|
||||
@@ -49,7 +53,10 @@ public final class Db {
|
||||
byte[] priByte = Base64.getDecoder().decode(priStr);
|
||||
PrivateKeyInterface priKey = KeyUtils.unMarshalPrivateKey(priByte, algo);
|
||||
|
||||
Certificate cert = CertUtils.fromPem(certStr, null, Common.SM4ECB);
|
||||
Certificate cert = new Certificate();
|
||||
if (!("".equals(certStr))){
|
||||
cert = CertUtils.fromPem(certStr, null, Common.SM4ECB);
|
||||
}
|
||||
byte[] priKeyByte = Base64.getDecoder().decode(prikeyRawByte);
|
||||
KeyParam keyParam = new KeyParam(algo, sign);
|
||||
return new Identity(name, keyParam, priKey, priKeyByte, pubKey, pass, cert, def == 1);
|
||||
@@ -78,7 +85,10 @@ public final class Db {
|
||||
byte[] priByte = Base64.getDecoder().decode(priStr);
|
||||
PrivateKeyInterface priKey = KeyUtils.unMarshalPrivateKey(priByte, algo);
|
||||
|
||||
Certificate cert = CertUtils.fromPem(certStr, null, Common.SM4ECB);
|
||||
Certificate cert = new Certificate();
|
||||
if (!("".equals(certStr))){
|
||||
cert = CertUtils.fromPem(certStr, null, Common.SM4ECB);
|
||||
}
|
||||
byte[] priKeyByte = Base64.getDecoder().decode(prikeyRawByte);
|
||||
KeyParam keyParam = new KeyParam(algo, sign);
|
||||
res.add(new Identity(name, keyParam, priKey, priKeyByte, pubKey, pass, cert, def == 1));
|
||||
@@ -111,7 +121,10 @@ public final class Db {
|
||||
byte[] priByte = Base64.getDecoder().decode(priStr);
|
||||
PrivateKeyInterface priKey = KeyUtils.unMarshalPrivateKey(priByte, algo);
|
||||
|
||||
Certificate cert = CertUtils.fromPem(certStr, null, Common.SM4ECB);
|
||||
Certificate cert = new Certificate();
|
||||
if (!("".equals(certStr))){
|
||||
cert = CertUtils.fromPem(certStr, null, Common.SM4ECB);
|
||||
}
|
||||
byte[] priKeyByte = Base64.getDecoder().decode(prikeyRawByte);
|
||||
KeyParam keyParam = new KeyParam(algo, sign);
|
||||
return new Identity(name, keyParam, priKey, priKeyByte, pubKey, pass, cert, def == 1);
|
||||
@@ -136,16 +149,19 @@ public final class Db {
|
||||
Connection c = Sqlite.getInstance().getConn();
|
||||
Statement stmt = c.createStatement();
|
||||
ResultSet rs = stmt.executeQuery("SELECT * from identityinfo");
|
||||
List<Identity> res = getIdentityFromSqlRows(rs);
|
||||
rs.close();
|
||||
stmt.close();
|
||||
c.close();
|
||||
List<Identity> res = getIdentityFromSqlRows(rs);
|
||||
return res;
|
||||
}
|
||||
|
||||
public static Identity getIdentityByNameFromStorage(String name) throws Exception {
|
||||
Connection c = Sqlite.getInstance().getConn();
|
||||
// c.close();
|
||||
Identity id = getIdentityByNameFromStorage(name, c);
|
||||
c.close();
|
||||
return getIdentityByNameFromStorage(name, c);
|
||||
return id;
|
||||
}
|
||||
|
||||
private static void setDefaultIdentityByNameInStorage(String name) throws Exception {
|
||||
@@ -154,19 +170,22 @@ public final class Db {
|
||||
try{
|
||||
PreparedStatement pstmt = c.prepareStatement("select * from identityinfo where name = ? LIMIT 1");
|
||||
pstmt.setString(1, name);
|
||||
ResultSet rs = pstmt.executeQuery(String.format("select * from identityinfo where name = %s LIMIT 1", name));
|
||||
ResultSet rs = pstmt.executeQuery();
|
||||
getDefaultIdentityFromStorage(rs);
|
||||
rs.close();
|
||||
pstmt.close();
|
||||
|
||||
PreparedStatement pstmt2 = c.prepareStatement("SELECT * from identityinfo where is_default = ?");
|
||||
pstmt2.setInt(1, 1);
|
||||
rs = pstmt2.executeQuery();
|
||||
Identity id = getDefaultIdentityFromStorage(rs);
|
||||
ResultSet rs2 = pstmt2.executeQuery();
|
||||
Identity id = getDefaultIdentityFromStorage(rs2);
|
||||
if (id != null){
|
||||
cancelDefaultIdentityFromStorage(id.getName(), c);
|
||||
}
|
||||
setDefaultIdentityFromStorage(name, c);
|
||||
rs2.close();
|
||||
pstmt2.close();
|
||||
|
||||
c.commit();
|
||||
c.close();
|
||||
}catch (Exception ex){
|
||||
@@ -211,7 +230,7 @@ public final class Db {
|
||||
String pubStr = "", priStr = "", certStr, prikeyRawByte = "";
|
||||
int algo, sign;
|
||||
|
||||
if (identity.hasPrivateKey()){
|
||||
if (identity.getPrikey() != null){
|
||||
byte[] priByte = identity.getPrikey().getBytes();
|
||||
priStr = Base64.getEncoder().encodeToString(priByte);
|
||||
}
|
||||
@@ -230,8 +249,8 @@ public final class Db {
|
||||
|
||||
PreparedStatement pstmt = c.prepareStatement("INSERT INTO identityinfo(name, pubkey, prikey, pubkey_algo, signature_algo, pass, cert,prikey_raw_byte) values(?,?,?,?,?,?,?,?)");
|
||||
pstmt.setString(1, identity.getName());
|
||||
pstmt.setString(2, priStr);
|
||||
pstmt.setString(3, pubStr);
|
||||
pstmt.setString(2, pubStr);
|
||||
pstmt.setString(3, priStr);
|
||||
pstmt.setInt(4, algo);
|
||||
pstmt.setInt(5, sign);
|
||||
pstmt.setString(6, identity.getPasswd());
|
||||
@@ -248,6 +267,10 @@ public final class Db {
|
||||
PreparedStatement pstmt = c.prepareStatement("SELECT * from identityinfo where is_default= ? LIMIT 1");
|
||||
pstmt.setInt(1, 1);
|
||||
ResultSet rs = pstmt.executeQuery();
|
||||
return getDefaultIdentityFromStorage(rs);
|
||||
Identity id = getDefaultIdentityFromStorage(rs);
|
||||
rs.close();
|
||||
pstmt.close();
|
||||
c.close();
|
||||
return id;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,109 @@
|
||||
package minsecurity.identity.sqlite.db;
|
||||
|
||||
public class DBTest {
|
||||
import minsecurity.Common;
|
||||
import minsecurity.certificate.cert.CertUtils;
|
||||
import minsecurity.certificate.cert.Certificate;
|
||||
import minsecurity.crypto.PrivateKeyInterface;
|
||||
import minsecurity.crypto.sm2.SM2KeyPair;
|
||||
import minsecurity.identity.Identity;
|
||||
import minsecurity.identity.KeyParam;
|
||||
import minsecurity.identity.persist.sqlite.Sqlite;
|
||||
import minsecurity.identity.persist.sqlite.db.Db;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
|
||||
class OperationThread extends Thread{
|
||||
|
||||
private int threadNo = 0;
|
||||
|
||||
private CountDownLatch countDownLatch;
|
||||
|
||||
public OperationThread(CountDownLatch countDownLatch){
|
||||
this.countDownLatch = countDownLatch;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
System.out.println(String.format("Operation %d starting", this.threadNo));
|
||||
try{
|
||||
// 测试PersistIdentity
|
||||
SM2KeyPair pair = SM2KeyPair.generateKeyPair();
|
||||
Identity identity = new Identity();
|
||||
identity.setName("wzq"+this.threadNo);
|
||||
KeyParam keyParam = new KeyParam();
|
||||
keyParam.PublicKeyAlgorithm = 0;
|
||||
keyParam.SignatureAlgorithm = 0;
|
||||
identity.setKeyParam(keyParam);
|
||||
identity.setPrikey(pair.getSm2PrivateKey());
|
||||
identity.setPubkey(pair.getSm2PublicKey());
|
||||
identity.setPasswd("2DD29CA851E7B56E4697B0E1F08507293D761A05CE4D1B628663F411A8086D99");
|
||||
identity.lock("0123456789abcdef", Common.SM4ECB);
|
||||
Certificate cert = new Certificate();
|
||||
cert.setVersion(1);
|
||||
cert.setSerialNumber(1);
|
||||
cert.setPublicKey(pair.getSm2PublicKey());
|
||||
cert.setSignatureAlgorithm(Common.SM3withSM2); // TODO 名字有误? SM2withSM3?
|
||||
cert.setPublicKeyAlgorithm(Common.SM2);
|
||||
cert.setIssueTo("root");
|
||||
cert.setIssuer("root");
|
||||
long timestamp = System.currentTimeMillis() / 1000;
|
||||
cert.setTimestamp(timestamp); // 10bit timestamp
|
||||
cert.setNotAfter(timestamp); // 10bit timestamp
|
||||
cert.setNotBefore(timestamp + 1000); // 10bit timestamp
|
||||
cert.setKeyUsage(Common.CertSign);
|
||||
cert.setCA(true);
|
||||
CertUtils.signCert(cert, pair.getSm2PrivateKey());
|
||||
|
||||
identity.setCert(cert);
|
||||
Db.persistIdentity(identity);
|
||||
|
||||
Identity id = Db.getIdentityByNameFromStorage("wzq"+this.threadNo);
|
||||
|
||||
System.out.println("插入身份:" + id.getName());
|
||||
System.out.println(String.format("开始设置 wzq%d 为default 身份", this.threadNo));
|
||||
|
||||
Db.SetDefaultIdentityByNameInStorage("wzq"+this.threadNo);
|
||||
|
||||
Identity id2 = Db.getDefaultIdentityFromStorage();
|
||||
System.out.println("当前default身份:" + id2.getName());
|
||||
}catch (Exception ex){
|
||||
System.out.println(String.format("Thread %d: %s", this.threadNo, ex.getMessage()));
|
||||
}
|
||||
countDownLatch.countDown();
|
||||
}
|
||||
|
||||
public int getThreadNo() {
|
||||
return threadNo;
|
||||
}
|
||||
|
||||
public void setThreadNo(int threadNo) {
|
||||
this.threadNo = threadNo;
|
||||
}
|
||||
}
|
||||
public class DBTest {
|
||||
@Test
|
||||
public void testDatabaseWithHighConcurrency(){
|
||||
try {
|
||||
Sqlite.getInstance().openDefault();
|
||||
CountDownLatch countDownLatch = new CountDownLatch(1);
|
||||
for (int i = 1; i <= 1; i++){
|
||||
OperationThread t = new OperationThread(countDownLatch);
|
||||
t.setThreadNo(i);
|
||||
t.start();
|
||||
}
|
||||
|
||||
countDownLatch.await();
|
||||
List<Identity> identities = Db.getAllIdentityFromStorage();
|
||||
System.out.println(identities.get(0).getPrikeyRawByte());
|
||||
identities.get(0).unLock("0123456789abcdef", Common.SM4ECB);
|
||||
PrivateKeyInterface id = identities.get(0).getPrikey();
|
||||
System.out.println(identities.get(0).getPrikey().getBytes());
|
||||
}catch (Exception ex){
|
||||
System.out.println(ex.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user