2012-09-18 18:35:14 +02:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2012 Dominik Schürmann <dominik@dominikschuermann.de>
|
|
|
|
|
* Copyright (C) 2010 Thialfihar <thi@thialfihar.org>
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
2013-01-16 14:31:16 +01:00
|
|
|
package org.sufficientlysecure.keychain.provider;
|
2012-09-18 18:35:14 +02:00
|
|
|
|
2013-01-16 14:31:16 +01:00
|
|
|
import org.sufficientlysecure.keychain.Constants;
|
2013-09-05 00:02:48 +02:00
|
|
|
import org.sufficientlysecure.keychain.provider.KeychainContract.ApiAppsColumns;
|
2013-01-16 14:31:16 +01:00
|
|
|
import org.sufficientlysecure.keychain.provider.KeychainContract.KeyRingsColumns;
|
|
|
|
|
import org.sufficientlysecure.keychain.provider.KeychainContract.KeysColumns;
|
|
|
|
|
import org.sufficientlysecure.keychain.provider.KeychainContract.UserIdsColumns;
|
|
|
|
|
import org.sufficientlysecure.keychain.util.Log;
|
2012-09-18 18:35:14 +02:00
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
|
import android.database.sqlite.SQLiteDatabase;
|
|
|
|
|
import android.database.sqlite.SQLiteOpenHelper;
|
|
|
|
|
import android.provider.BaseColumns;
|
|
|
|
|
|
2013-01-16 14:31:16 +01:00
|
|
|
public class KeychainDatabase extends SQLiteOpenHelper {
|
2012-09-18 18:35:14 +02:00
|
|
|
private static final String DATABASE_NAME = "apg.db";
|
2013-05-28 15:10:36 +02:00
|
|
|
private static final int DATABASE_VERSION = 5;
|
2012-09-18 18:35:14 +02:00
|
|
|
|
|
|
|
|
public interface Tables {
|
|
|
|
|
String KEY_RINGS = "key_rings";
|
|
|
|
|
String KEYS = "keys";
|
2012-10-25 14:52:13 +02:00
|
|
|
String USER_IDS = "user_ids";
|
2013-09-05 00:02:48 +02:00
|
|
|
String API_APPS = "api_apps";
|
2012-09-18 18:35:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static final String CREATE_KEY_RINGS = "CREATE TABLE IF NOT EXISTS " + Tables.KEY_RINGS
|
2012-10-25 14:52:13 +02:00
|
|
|
+ " (" + BaseColumns._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
|
|
|
|
|
+ KeyRingsColumns.MASTER_KEY_ID + " INT64, " + KeyRingsColumns.TYPE + " INTEGER, "
|
|
|
|
|
+ KeyRingsColumns.KEY_RING_DATA + " BLOB)";
|
2012-09-18 18:35:14 +02:00
|
|
|
|
|
|
|
|
private static final String CREATE_KEYS = "CREATE TABLE IF NOT EXISTS " + Tables.KEYS + " ("
|
2012-10-25 14:52:13 +02:00
|
|
|
+ BaseColumns._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KeysColumns.KEY_ID
|
|
|
|
|
+ " INT64, " + KeysColumns.TYPE + " INTEGER, " + KeysColumns.IS_MASTER_KEY
|
|
|
|
|
+ " INTEGER, " + KeysColumns.ALGORITHM + " INTEGER, " + KeysColumns.KEY_SIZE
|
2013-05-28 15:10:36 +02:00
|
|
|
+ " INTEGER, " + KeysColumns.CAN_CERTIFY + " INTEGER, " + KeysColumns.CAN_SIGN
|
|
|
|
|
+ " INTEGER, " + KeysColumns.CAN_ENCRYPT + " INTEGER, " + KeysColumns.IS_REVOKED
|
|
|
|
|
+ " INTEGER, " + KeysColumns.CREATION + " INTEGER, " + KeysColumns.EXPIRY
|
|
|
|
|
+ " INTEGER, " + KeysColumns.KEY_DATA + " BLOB," + KeysColumns.RANK + " INTEGER, "
|
|
|
|
|
+ KeysColumns.KEY_RING_ROW_ID + " INTEGER NOT NULL, FOREIGN KEY("
|
|
|
|
|
+ KeysColumns.KEY_RING_ROW_ID + ") REFERENCES " + Tables.KEY_RINGS + "("
|
|
|
|
|
+ BaseColumns._ID + ") ON DELETE CASCADE)";
|
2012-09-18 18:35:14 +02:00
|
|
|
|
2012-10-25 14:52:13 +02:00
|
|
|
private static final String CREATE_USER_IDS = "CREATE TABLE IF NOT EXISTS " + Tables.USER_IDS
|
|
|
|
|
+ " (" + BaseColumns._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
|
2012-10-31 18:00:40 +01:00
|
|
|
+ UserIdsColumns.USER_ID + " TEXT, " + UserIdsColumns.RANK + " INTEGER, "
|
|
|
|
|
+ UserIdsColumns.KEY_RING_ROW_ID + " INTEGER NOT NULL, FOREIGN KEY("
|
|
|
|
|
+ UserIdsColumns.KEY_RING_ROW_ID + ") REFERENCES " + Tables.KEY_RINGS + "("
|
|
|
|
|
+ BaseColumns._ID + ") ON DELETE CASCADE)";
|
2012-09-18 18:35:14 +02:00
|
|
|
|
2013-09-05 00:02:48 +02:00
|
|
|
private static final String CREATE_API_APPS = "CREATE TABLE IF NOT EXISTS "
|
|
|
|
|
+ Tables.API_APPS + " (" + BaseColumns._ID
|
|
|
|
|
+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + ApiAppsColumns.PACKAGE_NAME
|
|
|
|
|
+ " TEXT UNIQUE, " + ApiAppsColumns.PRIVATE_KEY_ID + " INT64, "
|
|
|
|
|
+ ApiAppsColumns.ASCII_ARMOR + " INTEGER, "
|
|
|
|
|
+ ApiAppsColumns.ENCRYPTION_ALGORITHM + " INTEGER, "
|
|
|
|
|
+ ApiAppsColumns.HASH_ALORITHM + " INTEGER, "
|
|
|
|
|
+ ApiAppsColumns.COMPRESSION + " INTEGER)";
|
2013-05-28 15:10:36 +02:00
|
|
|
|
2013-01-16 14:31:16 +01:00
|
|
|
KeychainDatabase(Context context) {
|
2012-09-18 18:35:14 +02:00
|
|
|
super(context, DATABASE_NAME, null, DATABASE_VERSION);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onCreate(SQLiteDatabase db) {
|
|
|
|
|
Log.w(Constants.TAG, "Creating database...");
|
|
|
|
|
|
|
|
|
|
db.execSQL(CREATE_KEY_RINGS);
|
|
|
|
|
db.execSQL(CREATE_KEYS);
|
|
|
|
|
db.execSQL(CREATE_USER_IDS);
|
2013-09-05 00:02:48 +02:00
|
|
|
db.execSQL(CREATE_API_APPS);
|
2012-09-18 18:35:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onOpen(SQLiteDatabase db) {
|
|
|
|
|
super.onOpen(db);
|
|
|
|
|
if (!db.isReadOnly()) {
|
|
|
|
|
// Enable foreign key constraints
|
|
|
|
|
db.execSQL("PRAGMA foreign_keys=ON;");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
|
|
|
|
|
Log.w(Constants.TAG, "Upgrading database from version " + oldVersion + " to " + newVersion);
|
|
|
|
|
|
|
|
|
|
// Upgrade from oldVersion through all methods to newest one
|
|
|
|
|
for (int version = oldVersion; version < newVersion; ++version) {
|
|
|
|
|
Log.w(Constants.TAG, "Upgrading database to version " + version);
|
|
|
|
|
|
|
|
|
|
switch (version) {
|
2013-03-18 18:51:24 +00:00
|
|
|
case 3:
|
2013-05-28 15:10:36 +02:00
|
|
|
db.execSQL("ALTER TABLE " + Tables.KEYS + " ADD COLUMN " + KeysColumns.CAN_CERTIFY
|
|
|
|
|
+ " INTEGER DEFAULT 0;");
|
|
|
|
|
db.execSQL("UPDATE " + Tables.KEYS + " SET " + KeysColumns.CAN_CERTIFY
|
|
|
|
|
+ " = 1 WHERE " + KeysColumns.IS_MASTER_KEY + "= 1;");
|
2013-03-18 18:51:24 +00:00
|
|
|
break;
|
2013-05-28 15:10:36 +02:00
|
|
|
case 4:
|
2013-09-05 00:02:48 +02:00
|
|
|
db.execSQL(CREATE_API_APPS);
|
2012-09-18 18:35:14 +02:00
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|