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
302
|
package se.leap.leapclient;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.math.BigInteger;
import java.io.InputStream;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import org.json.JSONException;
import org.json.JSONObject;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Environment;
/**
* Stores constants, and implements auxiliary methods used across all LEAP Android classes.
*
* @author parmegv
* @author MeanderingCode
*
*/
public class ConfigHelper {
public static SharedPreferences shared_preferences;
private static KeyStore keystore_trusted;
final public static String
DOWNLOAD_JSON_FILES_BUNDLE_EXTRA = "downloadJSONFiles",
UPDATE_PROVIDER_DOTJSON = "updateProviderDotJSON",
DOWNLOAD_NEW_PROVIDER_DOTJSON = "downloadNewProviderDotJSON",
LOG_IN_DIALOG = "logInDialog",
NEW_PROVIDER_DIALOG = "logInDialog",
SRP_REGISTER = "srpRegister",
SRP_AUTH = "srpAuth",
M1_KEY = "M1",
M2_KEY = "M2",
LOG_IN = "logIn",
LOG_OUT = "logOut",
DOWNLOAD_CERTIFICATE = "downloadUserAuthedCertificate",
API_VERSION_KEY = "api_version",
RESULT_KEY = "result",
RECEIVER_KEY = "receiver",
PROVIDER_KEY = "provider",
SERVICE_KEY = "service",
ALLOWED_ANON = "allow_anonymous",
MAIN_CERT_KEY = "main_cert",
CERT_KEY = "cert",
KEY_KEY = "key",
EIP_SERVICE_KEY = "eip",
TYPE_OF_CERTIFICATE = "type_of_certificate",
ANON_CERTIFICATE = "anon_certificate",
AUTHED_CERTIFICATE = "authed_certificate",
SALT_KEY = "salt",
SESSION_ID_COOKIE_KEY = "session_id_cookie_key",
SESSION_ID_KEY = "session_id",
PREFERENCES_KEY = "LEAPPreferences",
USER_DIRECTORY = "leap_android",
PROVIDER_NAME = "provider_name",
PROVIDER_ID = "provider_id",
PROVIDER_MAIN_URL = "provider_main_url",
PROVIDER_JSON_URL = "provider_json_url",
CUSTOM = "custom",
DANGER_ON = "danger_on",
API_URL_KEY = "api_uri",
USERNAME_KEY = "username",
PASSWORD_KEY = "password",
ALLOW_REGISTRATION_KEY = "allow_registration",
EIP_SERVICE_API_PATH = "config/eip-service.json",
ERRORS_KEY = "errors",
RECEIVER_TAG = "receiverTag",
REQUEST_TAG = "requestTag";
;
final public static String NG_1024 =
"eeaf0ab9adb38dd69c33f80afa8fc5e86072618775ff3c0b9ea2314c9c256576d674df7496ea81d3383b4813d692c6e0e0d5d8e250b98be48e495c1d6089dad15dc7d7b46154d6b6ce8ef4ad69b15d4982559b297bcf1885c529f566660e57ec68edbc3c05726cc02fd4cbf4976eaa9afd5138fe8376435b9fc61d2fc0eb06e3";
final public static BigInteger G = new BigInteger("2");
final public static int
CUSTOM_PROVIDER_ADDED = 0,
CORRECTLY_DOWNLOADED_JSON_FILES = 1,
INCORRECTLY_DOWNLOADED_JSON_FILES = 2,
SRP_AUTHENTICATION_SUCCESSFUL = 3,
SRP_AUTHENTICATION_FAILED = 4,
SRP_REGISTRATION_SUCCESSFUL = 5,
SRP_REGISTRATION_FAILED = 6,
LOGOUT_SUCCESSFUL = 7,
LOGOUT_FAILED = 8,
CORRECTLY_DOWNLOADED_CERTIFICATE = 9,
INCORRECTLY_DOWNLOADED_CERTIFICATE = 10,
CORRECTLY_UPDATED_PROVIDER_DOT_JSON = 11,
INCORRECTLY_UPDATED_PROVIDER_DOT_JSON = 12,
CORRECTLY_DOWNLOADED_ANON_CERTIFICATE = 13,
INCORRECTLY_DOWNLOADED_ANON_CERTIFICATE = 14
;
private static boolean checkSharedPrefs() {
try {
shared_preferences = Dashboard.getAppContext().getSharedPreferences(PREFERENCES_KEY,Context.MODE_PRIVATE);
} catch (Exception e) {
return false;
}
return true;
}
/**
* Saves a JSON object into class scope Shared Preferences
* @param shared_preferences_key
* @param content
*/
public static void saveSharedPref(String shared_preferences_key, JSONObject content) {
SharedPreferences.Editor shared_preferences_editor = shared_preferences
.edit();
shared_preferences_editor.putString(shared_preferences_key,
content.toString());
shared_preferences_editor.commit();
}
/**
* Saves a String object into class scope Shared Preferences
* @param shared_preferences_key
* @param content
*/
public static void saveSharedPref(String shared_preferences_key, String content) {
SharedPreferences.Editor shared_preferences_editor = shared_preferences
.edit();
shared_preferences_editor.putString(shared_preferences_key,
content);
shared_preferences_editor.commit();
}
/**
* Saves a boolean object into class scope Shared Preferences
* @param shared_preferences_key
* @param content
*/
public static void saveSharedPref(String shared_preferences_key, boolean content) {
SharedPreferences.Editor shared_preferences_editor = shared_preferences
.edit();
shared_preferences_editor.putBoolean(shared_preferences_key, content);
shared_preferences_editor.commit();
}
/**
* Gets String object from class scope Shared Preferences
* @param shared_preferences_key
* @return the string correspondent to the key parameter
*/
public static String getStringFromSharedPref(String shared_preferences_key) {
String content = null;
if ( checkSharedPrefs() ) {
content = shared_preferences.getString(shared_preferences_key, "");
}
return content;
}
/**
* Gets JSON object from class scope Shared Preferences
* @param shared_preferences_key
* @return the JSON object correspondent to the key parameter
*/
public static JSONObject getJsonFromSharedPref(String shared_preferences_key) throws JSONException {
JSONObject content = null;
if ( checkSharedPrefs() ) {
content = new JSONObject( shared_preferences.getString(shared_preferences_key, "") );
}
return content;
}
/*
* This method defaults to false.
* If you use this method, be sure to fail-closed on false!
* TODO This is obviously less than ideal...solve it!
*/
public static boolean getBoolFromSharedPref(String shared_preferences_key) {
boolean value = false;
if ( checkSharedPrefs() ) {
value = shared_preferences.getBoolean(shared_preferences_key, false);
}
return value;
}
/**
* Opens a FileInputStream from the user directory of the external storage directory.
* @param filename
* @return a file input stream
*/
public static FileInputStream openFileInputStream(String filename) {
FileInputStream input_stream = null;
File root = Environment.getExternalStorageDirectory();
File leap_dir = new File(root.getAbsolutePath() + File.separator + USER_DIRECTORY);
try {
input_stream = new FileInputStream(leap_dir + File.separator + filename);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return input_stream;
}
/**
* Treat the input as the MSB representation of a number,
* and lop off leading zero elements. For efficiency, the
* input is simply returned if no leading zeroes are found.
*
* @param in array to be trimmed
*/
public static byte[] trim(byte[] in) {
if(in.length == 0 || in[0] != 0)
return in;
int len = in.length;
int i = 1;
while(in[i] == 0 && i < len)
++i;
byte[] ret = new byte[len - i];
System.arraycopy(in, i, ret, 0, len - i);
return ret;
}
/**
* Sets class scope Shared Preferences
* @param shared_preferences
*/
public static void setSharedPreferences(
SharedPreferences shared_preferences) {
ConfigHelper.shared_preferences = shared_preferences;
}
/**
* Adds a new X509 certificate given its input stream and its provider name
* @param provider used to store the certificate in the keystore
* @param inputStream from which X509 certificate must be generated.
*/
public static void addTrustedCertificate(String provider, InputStream inputStream) {
CertificateFactory cf;
try {
cf = CertificateFactory.getInstance("X.509");
X509Certificate cert =
(X509Certificate)cf.generateCertificate(inputStream);
keystore_trusted.setCertificateEntry(provider, cert);
} catch (CertificateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (KeyStoreException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* Adds a new X509 certificate given in its string from and using its provider name
* @param provider used to store the certificate in the keystore
* @param certificate
*/
public static void addTrustedCertificate(String provider, String certificate) {
String filename_to_save = provider + "_certificate.cer";
CertificateFactory cf;
try {
cf = CertificateFactory.getInstance("X.509");
X509Certificate cert =
(X509Certificate)cf.generateCertificate(openFileInputStream(filename_to_save));
if(keystore_trusted == null) {
keystore_trusted = KeyStore.getInstance("BKS");
keystore_trusted.load(null);
}
keystore_trusted.setCertificateEntry(provider, cert);
} catch (CertificateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (KeyStoreException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* @return class wide keystore
*/
public static KeyStore getKeystore() {
return keystore_trusted;
}
}
|