diff options
author | Andrew Comminos <andrew@comminos.com> | 2016-01-12 04:36:03 +0300 |
---|---|---|
committer | Andrew Comminos <andrew@comminos.com> | 2016-01-12 04:36:03 +0300 |
commit | dee00fc458feefbece3b3393f6307a228ffe049d (patch) | |
tree | c84d6167106edf8911f43a98ff14227289359170 | |
parent | e6bf926889234e8553937f0676c01cb53613eefe (diff) |
Added methods to support certificate storage in PlumbleDatabase.
3 files changed, 124 insertions, 2 deletions
diff --git a/app/src/main/java/com/morlunk/mumbleclient/db/DatabaseCertificate.java b/app/src/main/java/com/morlunk/mumbleclient/db/DatabaseCertificate.java new file mode 100644 index 0000000..4067d47 --- /dev/null +++ b/app/src/main/java/com/morlunk/mumbleclient/db/DatabaseCertificate.java @@ -0,0 +1,40 @@ +/* + * Copyright (C) 2016 Andrew Comminos <andrew@comminos.com> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +package com.morlunk.mumbleclient.db; + +/** + * A stub for a certificate entry in Plumble's database. + * Created by andrew on 11/01/16. + */ +public class DatabaseCertificate { + private final long mId; + private final String mName; + + protected DatabaseCertificate(long id, String name) { + mId = id; + mName = name; + } + + public long getId() { + return mId; + } + + public String getName() { + return mName; + } +} diff --git a/app/src/main/java/com/morlunk/mumbleclient/db/PlumbleDatabase.java b/app/src/main/java/com/morlunk/mumbleclient/db/PlumbleDatabase.java index eddd2c3..48f0729 100644 --- a/app/src/main/java/com/morlunk/mumbleclient/db/PlumbleDatabase.java +++ b/app/src/main/java/com/morlunk/mumbleclient/db/PlumbleDatabase.java @@ -53,4 +53,26 @@ public interface PlumbleDatabase { public List<Integer> getLocalIgnoredUsers(long serverId); public void addLocalIgnoredUser(long serverId, int userId); public void removeLocalIgnoredUser(long serverId, int userId); + + /** + * Adds the given certificate binary blob to the database. + * @param name The user-readable certificate name. + * @param certificate A PKCS12 binary blob. + * @return A handle for the newly craeted certificate. + */ + DatabaseCertificate addCertificate(String name, byte[] certificate); + List<DatabaseCertificate> getCertificates(); + + /** + * Obtains the certificate data associated with the given certificate ID. + * @param id The certificate ID to fetch the data of. + * @return A binary representation of a PKCS12 certificate. + */ + byte[] getCertificateData(long id); + + /** + * Removes the certificate with the given ID. + * @param id The certificate's identifier. + */ + void removeCertificate(long id); } diff --git a/app/src/main/java/com/morlunk/mumbleclient/db/PlumbleSQLiteDatabase.java b/app/src/main/java/com/morlunk/mumbleclient/db/PlumbleSQLiteDatabase.java index c4a52e3..06cbe5a 100644 --- a/app/src/main/java/com/morlunk/mumbleclient/db/PlumbleSQLiteDatabase.java +++ b/app/src/main/java/com/morlunk/mumbleclient/db/PlumbleSQLiteDatabase.java @@ -97,12 +97,23 @@ public class PlumbleSQLiteDatabase extends SQLiteOpenHelper implements PlumbleDa + "CONSTRAINT server_user UNIQUE(" + LOCAL_IGNORE_SERVER + "," + LOCAL_IGNORE_USER + ")" + ");"; + public static final String TABLE_CERTIFICATES = "certificates"; + public static final String COLUMN_CERTIFICATES_ID = "_id"; + public static final String COLUMN_CERTIFICATES_DATA = "data"; + public static final String COLUMN_CERTIFICATES_NAME = "name"; + public static final String TABLE_CERTIFICATES_CREATE_SQL = "CREATE TABLE IF NOT EXISTS " + TABLE_CERTIFICATES + " (" + + "`" + COLUMN_CERTIFICATES_ID + "` INTEGER PRIMARY KEY AUTOINCREMENT," + + "`" + COLUMN_CERTIFICATES_DATA + "` BLOB NOT NULL," + + "`" + COLUMN_CERTIFICATES_NAME + "` TEXT NOT NULL" + + ");"; + public static final Integer PRE_FAVOURITES_DB_VERSION = 2; public static final Integer PRE_TOKENS_DB_VERSION = 3; public static final Integer PRE_COMMENTS_DB_VERSION = 4; public static final Integer PRE_LOCAL_MUTE_DB_VERSION = 5; public static final Integer PRE_LOCAL_IGNORE_DB_VERSION = 6; - public static final Integer CURRENT_DB_VERSION = 7; + public static final Integer PRE_CERTIFICATES_DB_VERSION = 7; + public static final Integer CURRENT_DB_VERSION = 8; public PlumbleSQLiteDatabase(Context context) { super(context, DATABASE_NAME, null, CURRENT_DB_VERSION); @@ -120,6 +131,7 @@ public class PlumbleSQLiteDatabase extends SQLiteOpenHelper implements PlumbleDa db.execSQL(TABLE_COMMENTS_CREATE_SQL); db.execSQL(TABLE_LOCAL_MUTE_CREATE_SQL); db.execSQL(TABLE_LOCAL_IGNORE_CREATE_SQL); + db.execSQL(TABLE_CERTIFICATES_CREATE_SQL); } @Override @@ -147,6 +159,10 @@ public class PlumbleSQLiteDatabase extends SQLiteOpenHelper implements PlumbleDa if (oldVersion <= PRE_LOCAL_IGNORE_DB_VERSION) { db.execSQL(TABLE_LOCAL_IGNORE_CREATE_SQL); } + + if (oldVersion <= PRE_CERTIFICATES_DB_VERSION) { + db.execSQL(TABLE_CERTIFICATES_CREATE_SQL); + } } @Override @@ -224,7 +240,7 @@ public class PlumbleSQLiteDatabase extends SQLiteOpenHelper implements PlumbleDa getWritableDatabase().delete(TABLE_LOCAL_MUTE, LOCAL_MUTE_SERVER + "=?", new String[] { String.valueOf(server.getId()) }); getWritableDatabase().delete(TABLE_LOCAL_IGNORE, LOCAL_IGNORE_SERVER + "=?", - new String[] { String.valueOf(server.getId()) }); + new String[]{String.valueOf(server.getId())}); } public List<Integer> getPinnedChannels(long serverId) { @@ -366,6 +382,50 @@ public class PlumbleSQLiteDatabase extends SQLiteOpenHelper implements PlumbleDa } @Override + public DatabaseCertificate addCertificate(String name, byte[] certificate) { + ContentValues values = new ContentValues(); + values.put(COLUMN_CERTIFICATES_NAME, name); + values.put(COLUMN_CERTIFICATES_DATA, certificate); + long id = getWritableDatabase().insert(TABLE_CERTIFICATES, null, values); + return new DatabaseCertificate(id, name); + } + + @Override + public List<DatabaseCertificate> getCertificates() { + Cursor cursor = getReadableDatabase().query(TABLE_CERTIFICATES, + new String[] { COLUMN_CERTIFICATES_ID, COLUMN_CERTIFICATES_NAME }, + null, null, null, null, null); + List<DatabaseCertificate> certificates = new ArrayList<>(); + cursor.moveToFirst(); + while (!cursor.isAfterLast()) { + certificates.add(new DatabaseCertificate(cursor.getLong(0), cursor.getString(1))); + cursor.moveToNext(); + } + cursor.close(); + return certificates; + } + + @Override + public byte[] getCertificateData(long id) { + Cursor cursor = getReadableDatabase().query(TABLE_CERTIFICATES, + new String[] { COLUMN_CERTIFICATES_DATA }, + COLUMN_CERTIFICATES_ID + "=?", + new String[] { String.valueOf(id) }, null, null, null); + if (!cursor.moveToFirst()) + return null; + byte[] data = cursor.getBlob(0); + cursor.close(); + return data; + } + + @Override + public void removeCertificate(long id) { + getWritableDatabase().delete(TABLE_CERTIFICATES, + COLUMN_CERTIFICATES_ID + "=?", + new String[] { String.valueOf(id) }); + } + + @Override public boolean isCommentSeen(String hash, byte[] commentHash) { Cursor cursor = getReadableDatabase().query(TABLE_COMMENTS, new String[]{COMMENTS_WHO, COMMENTS_COMMENT, COMMENTS_SEEN}, COMMENTS_WHO + "=? AND " + COMMENTS_COMMENT + "=?", |