diff --git a/src/main/java/minsecurity/identity/persist/sqlite/Sqlite.java b/src/main/java/minsecurity/identity/persist/sqlite/Sqlite.java index bc2ffa4..cd4d468 100644 --- a/src/main/java/minsecurity/identity/persist/sqlite/Sqlite.java +++ b/src/main/java/minsecurity/identity/persist/sqlite/Sqlite.java @@ -64,12 +64,13 @@ public class Sqlite { Statement stmt = null; try{ String homePath = SqliteUtil.home(); - homePath += "/min/identity"; + homePath += "/min/identity/"; String dbPath = homePath; if (!SqliteUtil.pathExists(dbPath)){ new File(dbPath).mkdirs(); } String real_db_file = dbPath + db_file; + Class.forName("org.sqlite.JDBC"); c = DriverManager.getConnection("jdbc:sqlite:" + real_db_file); stmt = c.createStatement(); stmt.executeUpdate(table_create); @@ -91,6 +92,7 @@ public class Sqlite { new File(filePath).mkdirs(); } String real_db_file = filePath + db_file; + Class.forName("org.sqlite.JDBC"); c = DriverManager.getConnection("jdbc:sqlite:" + real_db_file); stmt = c.createStatement(); stmt.executeUpdate(table_create); @@ -104,10 +106,11 @@ public class Sqlite { } } - public Connection getConn() throws SQLException { + public Connection getConn() throws Exception { Connection c = null; try{ String real_db_file = db_path + db_file; + Class.forName("org.sqlite.JDBC"); c = DriverManager.getConnection("jdbc:sqlite:" + real_db_file); // TODO 设置数据库最大连接数 return c; diff --git a/src/main/java/minsecurity/identity/persist/sqlite/db/Db.java b/src/main/java/minsecurity/identity/persist/sqlite/db/Db.java index 8198d7a..0043580 100644 --- a/src/main/java/minsecurity/identity/persist/sqlite/db/Db.java +++ b/src/main/java/minsecurity/identity/persist/sqlite/db/Db.java @@ -1,4 +1,170 @@ package minsecurity.identity.persist.sqlite.db; -public class Db { +import minsecurity.Common; +import minsecurity.certificate.cert.CertUtils; +import minsecurity.certificate.cert.Certificate; +import minsecurity.crypto.KeyUtils; +import minsecurity.crypto.PrivateKeyInterface; +import minsecurity.crypto.PublicKeyInterface; +import minsecurity.identity.Identity; +import minsecurity.identity.KeyParam; +import minsecurity.identity.persist.sqlite.Sqlite; + +import java.sql.*; +import java.util.ArrayList; +import java.util.Base64; +import java.util.List; + +public final class Db { + private static Identity getIdentityByNameFromStorage(String name, Connection c) throws Exception { + Statement stmt = c.createStatement(); + ResultSet rs = stmt.executeQuery(String.format("SELECT * FROM identityinfo WHERE name = %s LIMIT 1", name)); // LIMIT 1代替Queryrow + stmt.close(); + return getIdentityFromSqlRow(rs); + } + + private static Identity getIdentityFromSqlRow(ResultSet rs) throws Exception { + if (!rs.isBeforeFirst()) + return null; + + String pubStr = "", priStr = "", pass = "", certStr = "", name = "", prikeyRawByte = ""; + int algo = 0, sign = 0, def = 0; + + while (rs.next()){ + pubStr = rs.getString("pubKey"); + priStr = rs.getString("priKey"); + pass = rs.getString("pass"); + certStr = rs.getString("cert"); + name = rs.getString("name"); + prikeyRawByte = rs.getString("prikey_raw_byte"); + algo = rs.getInt("pubkey_algo"); + sign = rs.getInt("signature_algo"); + def = rs.getInt("is_default"); + } + + byte[] pubByte = Base64.getDecoder().decode(pubStr); + PublicKeyInterface pubKey = KeyUtils.unMarshalPublicKey(pubByte, algo); + byte[] priByte = Base64.getDecoder().decode(priStr); + PrivateKeyInterface priKey = KeyUtils.unMarshalPrivateKey(priByte, algo); + + Certificate 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); + } + + private static List getIdentityFromSqlRows(ResultSet rs) throws Exception { + if (!rs.isBeforeFirst()) + return new ArrayList<>(); + + List res = new ArrayList<>(); + String pubStr = "", priStr = "", pass = "", certStr = "", name = "", prikeyRawByte = ""; + int algo = 0, sign = 0, def = 0; + + while (rs.next()){ + pubStr = rs.getString("pubKey"); + priStr = rs.getString("priKey"); + pass = rs.getString("pass"); + certStr = rs.getString("cert"); + name = rs.getString("name"); + prikeyRawByte = rs.getString("prikey_raw_byte"); + algo = rs.getInt("pubkey_algo"); + sign = rs.getInt("signature_algo"); + def = rs.getInt("is_default"); + byte[] pubByte = Base64.getDecoder().decode(pubStr); + PublicKeyInterface pubKey = KeyUtils.unMarshalPublicKey(pubByte, algo); + byte[] priByte = Base64.getDecoder().decode(priStr); + PrivateKeyInterface priKey = KeyUtils.unMarshalPrivateKey(priByte, algo); + + Certificate 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)); + } + + return res; + } + + private static Identity getDefaultIdentityFromStorage(ResultSet rs) throws Exception{ + if (rs == null) + return null; + + String pubStr = "", priStr = "", pass = "", certStr = "", name = "", prikeyRawByte = ""; + int algo = 0, sign = 0, def = 0; + + while (rs.next()){ + pubStr = rs.getString("pubKey"); + priStr = rs.getString("priKey"); + pass = rs.getString("pass"); + certStr = rs.getString("cert"); + name = rs.getString("name"); + prikeyRawByte = rs.getString("prikey_raw_byte"); + algo = rs.getInt("pubkey_algo"); + sign = rs.getInt("signature_algo"); + def = rs.getInt("is_default"); + } + + byte[] pubByte = Base64.getDecoder().decode(pubStr); + PublicKeyInterface pubKey = KeyUtils.unMarshalPublicKey(pubByte, algo); + byte[] priByte = Base64.getDecoder().decode(priStr); + PrivateKeyInterface priKey = KeyUtils.unMarshalPrivateKey(priByte, algo); + + Certificate 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); + } + + + private static boolean setDefaultIdentityFromStorage(String name, Connection c) throws Exception{ + PreparedStatement stmt = c.prepareStatement("update identityinfo set is_default = 1 where name= ?"); + stmt.setString(1, name); + stmt.executeUpdate(); + stmt.close(); + return true; + } + + private static boolean cancelDefaultIdentityFromStorage(String name, Connection c) throws Exception{ + PreparedStatement stmt = c.prepareStatement("update identityinfo set is_default = 0 where name= ?"); + stmt.setString(1, name); + stmt.executeUpdate(); + stmt.close(); + return true; + } + + public static List getAllIdentityFromStorage(String name) throws Exception { + Connection c = Sqlite.getInstance().getConn(); + Statement stmt = c.createStatement(); + ResultSet rs = stmt.executeQuery("SELECT * from identityinfo"); + stmt.close(); + c.close(); + List res = getIdentityFromSqlRows(rs); + return res; + } + + public static Identity getIdentityByNameFromStorage(String name) throws Exception { + Connection c = Sqlite.getInstance().getConn(); + c.close(); + return getIdentityByNameFromStorage(name, c); + } + + private static boolean setDefaultIdentityByNameInStorage(String name) throws Exception { + Connection c = Sqlite.getInstance().getConn(); + return false; + } + public static boolean SetDefaultIdentityByNameInStorage(String name) throws Exception { + for (int i = 0; i < 4; i++){ + boolean res = false; + try{ + res = setDefaultIdentityByNameInStorage(name); + return res; + }catch (Exception ex){ + if (ex.getMessage().equals("database is locked")) + Thread.sleep(50); + else + return false; + } + } + return false; + } } diff --git a/src/main/java/minsecurity/identity/persist/sqlite/db/Internal.java b/src/main/java/minsecurity/identity/persist/sqlite/db/Internal.java deleted file mode 100644 index 11433d0..0000000 --- a/src/main/java/minsecurity/identity/persist/sqlite/db/Internal.java +++ /dev/null @@ -1,4 +0,0 @@ -package minsecurity.identity.persist.sqlite.db; - -public class Internal { -}