db unittest completed(annotations added to be compatible with go version's json)

This commit is contained in:
ChessNineeee committed 2021-04-07 19:34:43 +08:00
1 parent f727a5c505
commit c43a9a3a1c
7 files changed
+173 -27

No files matched your search

@@ -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;
}
}