aboutsummaryrefslogtreecommitdiffstats
path: root/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/TemporaryFileProvider.java
blob: 68963d59511878539ee50d1150cac2d1169bbb10 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/*
 * Copyright (C) 2014-2015 Dominik Schürmann <dominik@dominikschuermann.de>
 * Copyright (C) 2014 Vincent Breitmoser <v.breitmoser@mugenguild.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 org.sufficientlysecure.keychain.provider;


import android.content.ClipDescription;
import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.MatrixCursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.net.Uri;
import android.os.ParcelFileDescriptor;
import android.provider.MediaStore;

import org.sufficientlysecure.keychain.Constants;
import org.sufficientlysecure.keychain.util.DatabaseUtil;
import org.sufficientlysecure.keychain.util.Log;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * TemporaryStorageProvider stores decrypted files inside the app's cache directory previously to
 * sharing them with other applications.
 * <p/>
 * Security:
 * - It is writable by OpenKeychain only (see Manifest), but exported for reading files
 * - It uses UUIDs as identifiers which makes predicting files from outside impossible
 * - Querying a number of files is not allowed, only querying single files
 * -> You can only open a file if you know the Uri containing the precise UUID, this Uri is only
 * revealed when the user shares a decrypted file with another app.
 * <p/>
 * Why is support lib's FileProvider not used?
 * Because granting Uri permissions temporarily does not work correctly. See
 * - https://code.google.com/p/android/issues/detail?id=76683
 * - https://github.com/nmr8acme/FileProvider-permission-bug
 * - http://stackoverflow.com/q/24467696
 * - http://stackoverflow.com/q/18249007
 * - Comments at http://www.blogc.at/2014/03/23/share-private-files-with-other-apps-fileprovider/
 */
public class TemporaryFileProvider extends ContentProvider {

    private static final String DB_NAME = "tempstorage.db";
    private static final String TABLE_FILES = "files";
    public static final String AUTHORITY = Constants.TEMP_FILE_PROVIDER_AUTHORITY;
    public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY);
    private static final int DB_VERSION = 3;

    interface TemporaryFileColumns {
        String COLUMN_UUID = "id";
        String COLUMN_NAME = "name";
        String COLUMN_TIME = "time";
        String COLUMN_TYPE = "mimetype";
    }

    private static final String TEMP_FILES_DIR = "temp";
    private static File tempFilesDir;

    private static Pattern UUID_PATTERN = Pattern.compile("[a-fA-F0-9-]+");

    public static Uri createFile(Context context, String targetName, String mimeType) {
        ContentValues contentValues = new ContentValues();
        contentValues.put(TemporaryFileColumns.COLUMN_NAME, targetName);
        contentValues.put(TemporaryFileColumns.COLUMN_TYPE, mimeType);
        return context.getContentResolver().insert(CONTENT_URI, contentValues);
    }

    public static Uri createFile(Context context, String targetName) {
        ContentValues contentValues = new ContentValues();
        contentValues.put(TemporaryFileColumns.COLUMN_NAME, targetName);
        return context.getContentResolver().insert(CONTENT_URI, contentValues);
    }

    public static Uri createFile(Context context) {
        ContentValues contentValues = new ContentValues();
        return context.getContentResolver().insert(CONTENT_URI, contentValues);
    }

    public static int setMimeType(Context context, Uri uri, String mimetype) {
        ContentValues values = new ContentValues();
        values.put(TemporaryFileColumns.COLUMN_TYPE, mimetype);
        return context.getContentResolver().update(uri, values, null, null);
    }

    public static int cleanUp(Context context) {
        return context.getContentResolver().delete(
                CONTENT_URI,
                TemporaryFileColumns.COLUMN_TIME + "< ?",
                new String[]{Long.toString(System.currentTimeMillis() - Constants.TEMPFILE_TTL)}
        );
    }

    private class TemporaryStorageDatabase extends SQLiteOpenHelper {

        public TemporaryStorageDatabase(Context context) {
            super(context, DB_NAME, null, DB_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL("CREATE TABLE IF NOT EXISTS " + TABLE_FILES + " (" +
                    TemporaryFileColumns.COLUMN_UUID + " TEXT PRIMARY KEY, " +
                    TemporaryFileColumns.COLUMN_NAME + " TEXT, " +
                    TemporaryFileColumns.COLUMN_TYPE + " TEXT, " +
                    TemporaryFileColumns.COLUMN_TIME + " INTEGER" +
                    ");");
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            Log.d(Constants.TAG, "Upgrading files db from " + oldVersion + " to " + newVersion);

            switch (oldVersion) {
                case 1:
                    db.execSQL("DROP TABLE IF EXISTS files");
                    db.execSQL("CREATE TABLE IF NOT EXISTS " + TABLE_FILES + " (" +
                            TemporaryFileColumns.COLUMN_UUID + " TEXT PRIMARY KEY, " +
                            TemporaryFileColumns.COLUMN_NAME + " TEXT, " +
                            TemporaryFileColumns.COLUMN_TIME + " INTEGER" +
                            ");");
                case 2:
                    db.execSQL("ALTER TABLE files ADD COLUMN " + TemporaryFileColumns.COLUMN_TYPE + " TEXT");
            }
        }
    }

    private static TemporaryStorageDatabase db;

    private File getFile(Uri uri) throws FileNotFoundException {
        try {
            return getFile(uri.getLastPathSegment());
        } catch (NumberFormatException e) {
            throw new FileNotFoundException();
        }
    }

    private File getFile(String id) {
        Matcher m = UUID_PATTERN.matcher(id);
        if (!m.matches()) {
            throw new SecurityException("Can only open temporary files with UUIDs!");
        }

        return new File(tempFilesDir, id);
    }

    @Override
    public boolean onCreate() {
        db = new TemporaryStorageDatabase(getContext());
        tempFilesDir = new File(getContext().getCacheDir(), TEMP_FILES_DIR);
        return tempFilesDir.mkdirs();
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
        if (uri.getLastPathSegment() == null) {
            throw new SecurityException("Listing temporary files is not allowed, only querying single files.");
        }

        File file;
        try {
            file = getFile(uri);
        } catch (FileNotFoundException e) {
            Log.e(Constants.TAG, "file not found!");
            return null;
        }

        Cursor fileName = db.getReadableDatabase().query(TABLE_FILES,
                new String[]{TemporaryFileColumns.COLUMN_NAME},
                TemporaryFileColumns.COLUMN_UUID + "=?",
                new String[]{uri.getLastPathSegment()}, null, null, null);
        if (fileName != null) {
            if (fileName.moveToNext()) {
                MatrixCursor cursor = new MatrixCursor(new String[]{
                        MediaStore.MediaColumns.DISPLAY_NAME,
                        MediaStore.MediaColumns.SIZE,
                        MediaStore.MediaColumns.DATA,
                });
                cursor.newRow()
                        .add(fileName.getString(0))
                        .add(file.length())
                        .add(file.getAbsolutePath());
                fileName.close();
                return cursor;
            }
            fileName.close();
        }
        return null;
    }

    @Override
    public String getType(Uri uri) {
        Cursor cursor = db.getReadableDatabase().query(TABLE_FILES,
                new String[]{TemporaryFileColumns.COLUMN_TYPE},
                TemporaryFileColumns.COLUMN_UUID + "=?",
                new String[]{uri.getLastPathSegment()}, null, null, null);
        if (cursor != null) {
            try {
                if (cursor.moveToNext()) {
                    if (!cursor.isNull(0)) {
                        return cursor.getString(0);
                    }
                }
            } finally {
                cursor.close();
            }
        }
        return "application/octet-stream";
    }

    @Override
    public String[] getStreamTypes(Uri uri, String mimeTypeFilter) {
        String type = getType(uri);
        if (ClipDescription.compareMimeTypes(type, mimeTypeFilter)) {
            return new String[]{type};
        }
        return null;
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        if (!values.containsKey(TemporaryFileColumns.COLUMN_TIME)) {
            values.put(TemporaryFileColumns.COLUMN_TIME, System.currentTimeMillis());
        }
        String uuid = UUID.randomUUID().toString();
        values.put(TemporaryFileColumns.COLUMN_UUID, uuid);
        int insert = (int) db.getWritableDatabase().insert(TABLE_FILES, null, values);
        if (insert == -1) {
            Log.e(Constants.TAG, "Insert failed!");
            return null;
        }
        try {
            getFile(uuid).createNewFile();
        } catch (IOException e) {
            Log.e(Constants.TAG, "File creation failed!");
            return null;
        }
        return Uri.withAppendedPath(CONTENT_URI, uuid);
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        if (uri == null || uri.getLastPathSegment() == null) {
            return 0;
        }

        selection = DatabaseUtil.concatenateWhere(selection, TemporaryFileColumns.COLUMN_UUID + "=?");
        selectionArgs = DatabaseUtil.appendSelectionArgs(selectionArgs, new String[]{uri.getLastPathSegment()});

        Cursor files = db.getReadableDatabase().query(TABLE_FILES, new String[]{TemporaryFileColumns.COLUMN_UUID}, selection,
                selectionArgs, null, null, null);
        if (files != null) {
            while (files.moveToNext()) {
                getFile(files.getString(0)).delete();
            }
            files.close();
            return db.getWritableDatabase().delete(TABLE_FILES, selection, selectionArgs);
        }
        return 0;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
        if (values.size() != 1 || !values.containsKey(TemporaryFileColumns.COLUMN_TYPE)) {
            throw new UnsupportedOperationException("Update supported only for type field!");
        }
        if (selection != null || selectionArgs != null) {
            throw new UnsupportedOperationException("Update supported only for plain uri!");
        }
        return db.getWritableDatabase().update(TABLE_FILES, values,
                TemporaryFileColumns.COLUMN_UUID + " = ?", new String[]{uri.getLastPathSegment()});
    }

    @Override
    public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
        return openFileHelper(uri, mode);
    }

}