android: Add properties for IKE and ESP proposals
[strongswan.git] / src / frontends / android / app / src / main / java / org / strongswan / android / data / VpnProfileDataSource.java
1 /*
2 * Copyright (C) 2012-2017 Tobias Brunner
3 * Copyright (C) 2012 Giuliano Grassi
4 * Copyright (C) 2012 Ralf Sager
5 * HSR Hochschule fuer Technik Rapperswil
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 * for more details.
16 */
17
18 package org.strongswan.android.data;
19
20 import android.content.ContentValues;
21 import android.content.Context;
22 import android.database.Cursor;
23 import android.database.SQLException;
24 import android.database.sqlite.SQLiteDatabase;
25 import android.database.sqlite.SQLiteOpenHelper;
26 import android.database.sqlite.SQLiteQueryBuilder;
27 import android.util.Log;
28
29 import java.util.ArrayList;
30 import java.util.List;
31 import java.util.UUID;
32
33 public class VpnProfileDataSource
34 {
35 private static final String TAG = VpnProfileDataSource.class.getSimpleName();
36 public static final String KEY_ID = "_id";
37 public static final String KEY_UUID = "_uuid";
38 public static final String KEY_NAME = "name";
39 public static final String KEY_GATEWAY = "gateway";
40 public static final String KEY_VPN_TYPE = "vpn_type";
41 public static final String KEY_USERNAME = "username";
42 public static final String KEY_PASSWORD = "password";
43 public static final String KEY_CERTIFICATE = "certificate";
44 public static final String KEY_USER_CERTIFICATE = "user_certificate";
45 public static final String KEY_MTU = "mtu";
46 public static final String KEY_PORT = "port";
47 public static final String KEY_SPLIT_TUNNELING = "split_tunneling";
48 public static final String KEY_LOCAL_ID = "local_id";
49 public static final String KEY_REMOTE_ID = "remote_id";
50 public static final String KEY_EXCLUDED_SUBNETS = "excluded_subnets";
51 public static final String KEY_INCLUDED_SUBNETS = "included_subnets";
52 public static final String KEY_SELECTED_APPS = "selected_apps";
53 public static final String KEY_SELECTED_APPS_LIST = "selected_apps_list";
54 public static final String KEY_NAT_KEEPALIVE = "nat_keepalive";
55 public static final String KEY_FLAGS = "flags";
56 public static final String KEY_IKE_PROPOSAL = "ike_proposal";
57 public static final String KEY_ESP_PROPOSAL = "esp_proposal";
58
59 private DatabaseHelper mDbHelper;
60 private SQLiteDatabase mDatabase;
61 private final Context mContext;
62
63 private static final String DATABASE_NAME = "strongswan.db";
64 private static final String TABLE_VPNPROFILE = "vpnprofile";
65
66 private static final int DATABASE_VERSION = 15;
67
68 public static final DbColumn[] COLUMNS = new DbColumn[] {
69 new DbColumn(KEY_ID, "INTEGER PRIMARY KEY AUTOINCREMENT", 1),
70 new DbColumn(KEY_UUID, "TEXT UNIQUE", 9),
71 new DbColumn(KEY_NAME, "TEXT NOT NULL", 1),
72 new DbColumn(KEY_GATEWAY, "TEXT NOT NULL", 1),
73 new DbColumn(KEY_VPN_TYPE, "TEXT NOT NULL", 3),
74 new DbColumn(KEY_USERNAME, "TEXT", 1),
75 new DbColumn(KEY_PASSWORD, "TEXT", 1),
76 new DbColumn(KEY_CERTIFICATE, "TEXT", 1),
77 new DbColumn(KEY_USER_CERTIFICATE, "TEXT", 2),
78 new DbColumn(KEY_MTU, "INTEGER", 5),
79 new DbColumn(KEY_PORT, "INTEGER", 5),
80 new DbColumn(KEY_SPLIT_TUNNELING, "INTEGER", 7),
81 new DbColumn(KEY_LOCAL_ID, "TEXT", 8),
82 new DbColumn(KEY_REMOTE_ID, "TEXT", 8),
83 new DbColumn(KEY_EXCLUDED_SUBNETS, "TEXT", 10),
84 new DbColumn(KEY_INCLUDED_SUBNETS, "TEXT", 11),
85 new DbColumn(KEY_SELECTED_APPS, "INTEGER", 12),
86 new DbColumn(KEY_SELECTED_APPS_LIST, "TEXT", 12),
87 new DbColumn(KEY_NAT_KEEPALIVE, "INTEGER", 13),
88 new DbColumn(KEY_FLAGS, "INTEGER", 14),
89 new DbColumn(KEY_IKE_PROPOSAL, "TEXT", 15),
90 new DbColumn(KEY_ESP_PROPOSAL, "TEXT", 15),
91 };
92
93 private static final String[] ALL_COLUMNS = getColumns(DATABASE_VERSION);
94
95 private static String getDatabaseCreate(int version)
96 {
97 boolean first = true;
98 StringBuilder create = new StringBuilder("CREATE TABLE ");
99 create.append(TABLE_VPNPROFILE);
100 create.append(" (");
101 for (DbColumn column : COLUMNS)
102 {
103 if (column.Since <= version)
104 {
105 if (!first)
106 {
107 create.append(",");
108 }
109 first = false;
110 create.append(column.Name);
111 create.append(" ");
112 create.append(column.Type);
113 }
114 }
115 create.append(");");
116 return create.toString();
117 }
118
119 private static String[] getColumns(int version)
120 {
121 ArrayList<String> columns = new ArrayList<>();
122 for (DbColumn column : COLUMNS)
123 {
124 if (column.Since <= version)
125 {
126 columns.add(column.Name);
127 }
128 }
129 return columns.toArray(new String[0]);
130 }
131
132 private static class DatabaseHelper extends SQLiteOpenHelper
133 {
134 public DatabaseHelper(Context context)
135 {
136 super(context, DATABASE_NAME, null, DATABASE_VERSION);
137 }
138
139 @Override
140 public void onCreate(SQLiteDatabase database)
141 {
142 database.execSQL(getDatabaseCreate(DATABASE_VERSION));
143 }
144
145 @Override
146 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
147 {
148 Log.w(TAG, "Upgrading database from version " + oldVersion +
149 " to " + newVersion);
150 if (oldVersion < 2)
151 {
152 db.execSQL("ALTER TABLE " + TABLE_VPNPROFILE + " ADD " + KEY_USER_CERTIFICATE +
153 " TEXT;");
154 }
155 if (oldVersion < 3)
156 {
157 db.execSQL("ALTER TABLE " + TABLE_VPNPROFILE + " ADD " + KEY_VPN_TYPE +
158 " TEXT DEFAULT '';");
159 }
160 if (oldVersion < 4)
161 { /* remove NOT NULL constraint from username column */
162 updateColumns(db, 4);
163 }
164 if (oldVersion < 5)
165 {
166 db.execSQL("ALTER TABLE " + TABLE_VPNPROFILE + " ADD " + KEY_MTU +
167 " INTEGER;");
168 }
169 if (oldVersion < 6)
170 {
171 db.execSQL("ALTER TABLE " + TABLE_VPNPROFILE + " ADD " + KEY_PORT +
172 " INTEGER;");
173 }
174 if (oldVersion < 7)
175 {
176 db.execSQL("ALTER TABLE " + TABLE_VPNPROFILE + " ADD " + KEY_SPLIT_TUNNELING +
177 " INTEGER;");
178 }
179 if (oldVersion < 8)
180 {
181 db.execSQL("ALTER TABLE " + TABLE_VPNPROFILE + " ADD " + KEY_LOCAL_ID +
182 " TEXT;");
183 db.execSQL("ALTER TABLE " + TABLE_VPNPROFILE + " ADD " + KEY_REMOTE_ID +
184 " TEXT;");
185 }
186 if (oldVersion < 9)
187 {
188 db.execSQL("ALTER TABLE " + TABLE_VPNPROFILE + " ADD " + KEY_UUID +
189 " TEXT;");
190 updateColumns(db, 9);
191 }
192 if (oldVersion < 10)
193 {
194 db.execSQL("ALTER TABLE " + TABLE_VPNPROFILE + " ADD " + KEY_EXCLUDED_SUBNETS +
195 " TEXT;");
196 }
197 if (oldVersion < 11)
198 {
199 db.execSQL("ALTER TABLE " + TABLE_VPNPROFILE + " ADD " + KEY_INCLUDED_SUBNETS +
200 " TEXT;");
201 }
202 if (oldVersion < 12)
203 {
204 db.execSQL("ALTER TABLE " + TABLE_VPNPROFILE + " ADD " + KEY_SELECTED_APPS +
205 " INTEGER;");
206 db.execSQL("ALTER TABLE " + TABLE_VPNPROFILE + " ADD " + KEY_SELECTED_APPS_LIST +
207 " TEXT;");
208 }
209 if (oldVersion < 13)
210 {
211 db.execSQL("ALTER TABLE " + TABLE_VPNPROFILE + " ADD " + KEY_NAT_KEEPALIVE +
212 " INTEGER;");
213 }
214 if (oldVersion < 14)
215 {
216 db.execSQL("ALTER TABLE " + TABLE_VPNPROFILE + " ADD " + KEY_FLAGS +
217 " INTEGER;");
218 }
219 if (oldVersion < 15)
220 {
221 db.execSQL("ALTER TABLE " + TABLE_VPNPROFILE + " ADD " + KEY_IKE_PROPOSAL +
222 " TEXT;");
223 db.execSQL("ALTER TABLE " + TABLE_VPNPROFILE + " ADD " + KEY_ESP_PROPOSAL +
224 " TEXT;");
225 }
226 }
227
228 private void updateColumns(SQLiteDatabase db, int version)
229 {
230 db.beginTransaction();
231 try
232 {
233 db.execSQL("ALTER TABLE " + TABLE_VPNPROFILE + " RENAME TO tmp_" + TABLE_VPNPROFILE + ";");
234 db.execSQL(getDatabaseCreate(version));
235 StringBuilder insert = new StringBuilder("INSERT INTO " + TABLE_VPNPROFILE + " SELECT ");
236 SQLiteQueryBuilder.appendColumns(insert, getColumns(version));
237 db.execSQL(insert.append(" FROM tmp_" + TABLE_VPNPROFILE + ";").toString());
238 db.execSQL("DROP TABLE tmp_" + TABLE_VPNPROFILE + ";");
239 db.setTransactionSuccessful();
240 }
241 finally
242 {
243 db.endTransaction();
244 }
245 }
246 }
247
248 /**
249 * Construct a new VPN profile data source. The context is used to
250 * open/create the database.
251 * @param context context used to access the database
252 */
253 public VpnProfileDataSource(Context context)
254 {
255 this.mContext = context;
256 }
257
258 /**
259 * Open the VPN profile data source. The database is automatically created
260 * if it does not yet exist. If that fails an exception is thrown.
261 * @return itself (allows to chain initialization calls)
262 * @throws SQLException if the database could not be opened or created
263 */
264 public VpnProfileDataSource open() throws SQLException
265 {
266 if (mDbHelper == null)
267 {
268 mDbHelper = new DatabaseHelper(mContext);
269 mDatabase = mDbHelper.getWritableDatabase();
270 }
271 return this;
272 }
273
274 /**
275 * Close the data source.
276 */
277 public void close()
278 {
279 if (mDbHelper != null)
280 {
281 mDbHelper.close();
282 mDbHelper = null;
283 }
284 }
285
286 /**
287 * Insert the given VPN profile into the database. On success the Id of
288 * the object is updated and the object returned.
289 *
290 * @param profile the profile to add
291 * @return the added VPN profile or null, if failed
292 */
293 public VpnProfile insertProfile(VpnProfile profile)
294 {
295 ContentValues values = ContentValuesFromVpnProfile(profile);
296 long insertId = mDatabase.insert(TABLE_VPNPROFILE, null, values);
297 if (insertId == -1)
298 {
299 return null;
300 }
301 profile.setId(insertId);
302 return profile;
303 }
304
305 /**
306 * Updates the given VPN profile in the database.
307 * @param profile the profile to update
308 * @return true if update succeeded, false otherwise
309 */
310 public boolean updateVpnProfile(VpnProfile profile)
311 {
312 long id = profile.getId();
313 ContentValues values = ContentValuesFromVpnProfile(profile);
314 return mDatabase.update(TABLE_VPNPROFILE, values, KEY_ID + " = " + id, null) > 0;
315 }
316
317 /**
318 * Delete the given VPN profile from the database.
319 * @param profile the profile to delete
320 * @return true if deleted, false otherwise
321 */
322 public boolean deleteVpnProfile(VpnProfile profile)
323 {
324 long id = profile.getId();
325 return mDatabase.delete(TABLE_VPNPROFILE, KEY_ID + " = " + id, null) > 0;
326 }
327
328 /**
329 * Get a single VPN profile from the database.
330 * @param id the ID of the VPN profile
331 * @return the profile or null, if not found
332 */
333 public VpnProfile getVpnProfile(long id)
334 {
335 VpnProfile profile = null;
336 Cursor cursor = mDatabase.query(TABLE_VPNPROFILE, ALL_COLUMNS,
337 KEY_ID + "=" + id, null, null, null, null);
338 if (cursor.moveToFirst())
339 {
340 profile = VpnProfileFromCursor(cursor);
341 }
342 cursor.close();
343 return profile;
344 }
345
346 /**
347 * Get a single VPN profile from the database by its UUID.
348 * @param uuid the UUID of the VPN profile
349 * @return the profile or null, if not found
350 */
351 public VpnProfile getVpnProfile(UUID uuid)
352 {
353 VpnProfile profile = null;
354 Cursor cursor = mDatabase.query(TABLE_VPNPROFILE, ALL_COLUMNS,
355 KEY_UUID + "='" + uuid.toString() + "'", null, null, null, null);
356 if (cursor.moveToFirst())
357 {
358 profile = VpnProfileFromCursor(cursor);
359 }
360 cursor.close();
361 return profile;
362 }
363
364 /**
365 * Get a list of all VPN profiles stored in the database.
366 * @return list of VPN profiles
367 */
368 public List<VpnProfile> getAllVpnProfiles()
369 {
370 List<VpnProfile> vpnProfiles = new ArrayList<VpnProfile>();
371
372 Cursor cursor = mDatabase.query(TABLE_VPNPROFILE, ALL_COLUMNS, null, null, null, null, null);
373 cursor.moveToFirst();
374 while (!cursor.isAfterLast())
375 {
376 VpnProfile vpnProfile = VpnProfileFromCursor(cursor);
377 vpnProfiles.add(vpnProfile);
378 cursor.moveToNext();
379 }
380 cursor.close();
381 return vpnProfiles;
382 }
383
384 private VpnProfile VpnProfileFromCursor(Cursor cursor)
385 {
386 VpnProfile profile = new VpnProfile();
387 profile.setId(cursor.getLong(cursor.getColumnIndex(KEY_ID)));
388 profile.setUUID(getUUID(cursor, cursor.getColumnIndex(KEY_UUID)));
389 profile.setName(cursor.getString(cursor.getColumnIndex(KEY_NAME)));
390 profile.setGateway(cursor.getString(cursor.getColumnIndex(KEY_GATEWAY)));
391 profile.setVpnType(VpnType.fromIdentifier(cursor.getString(cursor.getColumnIndex(KEY_VPN_TYPE))));
392 profile.setUsername(cursor.getString(cursor.getColumnIndex(KEY_USERNAME)));
393 profile.setPassword(cursor.getString(cursor.getColumnIndex(KEY_PASSWORD)));
394 profile.setCertificateAlias(cursor.getString(cursor.getColumnIndex(KEY_CERTIFICATE)));
395 profile.setUserCertificateAlias(cursor.getString(cursor.getColumnIndex(KEY_USER_CERTIFICATE)));
396 profile.setMTU(getInt(cursor, cursor.getColumnIndex(KEY_MTU)));
397 profile.setPort(getInt(cursor, cursor.getColumnIndex(KEY_PORT)));
398 profile.setSplitTunneling(getInt(cursor, cursor.getColumnIndex(KEY_SPLIT_TUNNELING)));
399 profile.setLocalId(cursor.getString(cursor.getColumnIndex(KEY_LOCAL_ID)));
400 profile.setRemoteId(cursor.getString(cursor.getColumnIndex(KEY_REMOTE_ID)));
401 profile.setExcludedSubnets(cursor.getString(cursor.getColumnIndex(KEY_EXCLUDED_SUBNETS)));
402 profile.setIncludedSubnets(cursor.getString(cursor.getColumnIndex(KEY_INCLUDED_SUBNETS)));
403 profile.setSelectedAppsHandling(getInt(cursor, cursor.getColumnIndex(KEY_SELECTED_APPS)));
404 profile.setSelectedApps(cursor.getString(cursor.getColumnIndex(KEY_SELECTED_APPS_LIST)));
405 profile.setNATKeepAlive(getInt(cursor, cursor.getColumnIndex(KEY_NAT_KEEPALIVE)));
406 profile.setFlags(getInt(cursor, cursor.getColumnIndex(KEY_FLAGS)));
407 profile.setIkeProposal(cursor.getString(cursor.getColumnIndex(KEY_IKE_PROPOSAL)));
408 profile.setEspProposal(cursor.getString(cursor.getColumnIndex(KEY_ESP_PROPOSAL)));
409 return profile;
410 }
411
412 private ContentValues ContentValuesFromVpnProfile(VpnProfile profile)
413 {
414 ContentValues values = new ContentValues();
415 values.put(KEY_UUID, profile.getUUID() != null ? profile.getUUID().toString() : null);
416 values.put(KEY_NAME, profile.getName());
417 values.put(KEY_GATEWAY, profile.getGateway());
418 values.put(KEY_VPN_TYPE, profile.getVpnType().getIdentifier());
419 values.put(KEY_USERNAME, profile.getUsername());
420 values.put(KEY_PASSWORD, profile.getPassword());
421 values.put(KEY_CERTIFICATE, profile.getCertificateAlias());
422 values.put(KEY_USER_CERTIFICATE, profile.getUserCertificateAlias());
423 values.put(KEY_MTU, profile.getMTU());
424 values.put(KEY_PORT, profile.getPort());
425 values.put(KEY_SPLIT_TUNNELING, profile.getSplitTunneling());
426 values.put(KEY_LOCAL_ID, profile.getLocalId());
427 values.put(KEY_REMOTE_ID, profile.getRemoteId());
428 values.put(KEY_EXCLUDED_SUBNETS, profile.getExcludedSubnets());
429 values.put(KEY_INCLUDED_SUBNETS, profile.getIncludedSubnets());
430 values.put(KEY_SELECTED_APPS, profile.getSelectedAppsHandling().getValue());
431 values.put(KEY_SELECTED_APPS_LIST, profile.getSelectedApps());
432 values.put(KEY_NAT_KEEPALIVE, profile.getNATKeepAlive());
433 values.put(KEY_FLAGS, profile.getFlags());
434 values.put(KEY_IKE_PROPOSAL, profile.getIkeProposal());
435 values.put(KEY_ESP_PROPOSAL, profile.getEspProposal());
436 return values;
437 }
438
439 private Integer getInt(Cursor cursor, int columnIndex)
440 {
441 return cursor.isNull(columnIndex) ? null : cursor.getInt(columnIndex);
442 }
443
444 private UUID getUUID(Cursor cursor, int columnIndex)
445 {
446 try
447 {
448 return cursor.isNull(columnIndex) ? null : UUID.fromString(cursor.getString(columnIndex));
449 }
450 catch (Exception e)
451 {
452 return null;
453 }
454 }
455
456 private static class DbColumn
457 {
458 public final String Name;
459 public final String Type;
460 public final Integer Since;
461
462 public DbColumn(String name, String type, Integer since)
463 {
464 Name = name;
465 Type = type;
466 Since = since;
467 }
468 }
469 }