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 0043580..b39e543 100644 --- a/src/main/java/minsecurity/identity/persist/sqlite/db/Db.java +++ b/src/main/java/minsecurity/identity/persist/sqlite/db/Db.java @@ -15,11 +15,13 @@ 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(); + PreparedStatement pstmt = c.prepareStatement("SELECT * FROM identityinfo WHERE name = ? LIMIT 1");// LIMIT 1代替Queryrow + pstmt.setString(1, name); + ResultSet rs = pstmt.executeQuery(); + pstmt.close(); return getIdentityFromSqlRow(rs); } @@ -86,7 +88,7 @@ public final class Db { } private static Identity getDefaultIdentityFromStorage(ResultSet rs) throws Exception{ - if (rs == null) + if (!rs.isBeforeFirst()) return null; String pubStr = "", priStr = "", pass = "", certStr = "", name = "", prikeyRawByte = ""; @@ -116,20 +118,18 @@ public final class Db { } - private static boolean setDefaultIdentityFromStorage(String name, Connection c) throws Exception{ + private static void 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{ + private static void 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 { @@ -148,23 +148,105 @@ public final class Db { return getIdentityByNameFromStorage(name, c); } - private static boolean setDefaultIdentityByNameInStorage(String name) throws Exception { + private static void setDefaultIdentityByNameInStorage(String name) throws Exception { Connection c = Sqlite.getInstance().getConn(); - return false; + c.setAutoCommit(false); + 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)); + getDefaultIdentityFromStorage(rs); + pstmt.close(); + + PreparedStatement pstmt2 = c.prepareStatement("SELECT * from identityinfo where is_default = ?"); + pstmt2.setInt(1, 1); + rs = pstmt2.executeQuery(); + Identity id = getDefaultIdentityFromStorage(rs); + if (id != null){ + cancelDefaultIdentityFromStorage(id.getName(), c); + } + setDefaultIdentityFromStorage(name, c); + pstmt2.close(); + c.commit(); + c.close(); + }catch (Exception ex){ + System.out.println(ex.getMessage()); + c.rollback(); + c.close(); + throw ex; + } } - public static boolean SetDefaultIdentityByNameInStorage(String name) throws Exception { + + public static void SetDefaultIdentityByNameInStorage(String name) throws Exception { for (int i = 0; i < 4; i++){ - boolean res = false; try{ - res = setDefaultIdentityByNameInStorage(name); - return res; + setDefaultIdentityByNameInStorage(name); + return; }catch (Exception ex){ + if (i == 3) + return; if (ex.getMessage().equals("database is locked")) Thread.sleep(50); else - return false; + throw ex; } } - return false; + } + + public static void deleteIdentityByName(String name) throws Exception{ + Connection c = Sqlite.getInstance().getConn(); + PreparedStatement pstmt = c.prepareStatement("elete from identityinfo where name=?"); + pstmt.setString(1, name); + pstmt.executeUpdate(); + pstmt.close(); + c.close(); + } + + public static void persistIdentity(Identity identity) throws Exception{ + Identity id = getIdentityByNameFromStorage(identity.getName()); + if (id != null) + throw new Exception("The name has existed"); + + Connection c = Sqlite.getInstance().getConn(); + String pubStr = "", priStr = "", certStr, prikeyRawByte = ""; + int algo, sign; + if (identity.hasPrivateKey()){ + byte[] priByte = identity.getPrikey().getBytes(); + priStr = Base64.getEncoder().encodeToString(priByte); + } + if (identity.getPubkey() != null){ + byte[] pubByte = identity.getPubkey().getBytes(); + pubStr = Base64.getEncoder().encodeToString(pubByte); + } + + algo = identity.getKeyParam().PublicKeyAlgorithm; + sign = identity.getKeyParam().SignatureAlgorithm; + + certStr = CertUtils.toPem(identity.getCert(), null, Common.SM4ECB); + + if (identity.getPrikeyRawByte() != null) + prikeyRawByte = Base64.getEncoder().encodeToString(identity.getPrikeyRawByte()); + + 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.setInt(4, algo); + pstmt.setInt(5, sign); + pstmt.setString(6, identity.getPasswd()); + pstmt.setString(7, certStr); + pstmt.setString(8, prikeyRawByte); + + pstmt.executeUpdate(); + pstmt.close(); + c.close(); + } + + public Identity getDefaultIdentityFromStorage() throws Exception{ + Connection c = Sqlite.getInstance().getConn(); + PreparedStatement pstmt = c.prepareStatement("SELECT * from identityinfo where is_default= ? LIMIT 1"); + pstmt.setInt(1, 1); + ResultSet rs = pstmt.executeQuery(); + return getDefaultIdentityFromStorage(rs); } }