-
Notifications
You must be signed in to change notification settings - Fork 878
/
Copy pathkeystore.ts
229 lines (197 loc) · 8.48 KB
/
keystore.ts
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
import { colors as c } from "../lib/color.js";
import {
IKeyStoreDetail,
IKeyStoreEntry
} from "./lib/interfaces.js";
import { wrapJavaPerform } from "./lib/libjava.js";
import {
KeyFactory,
KeyInfo,
KeyStore,
SecretKeyFactory
} from "./lib/types.js";
import * as jobs from "../lib/jobs.js";
// Dump entries in the Android Keystore, together with a flag
// indicating if its a key or a certificate.
//
// Ref: https://developer.android.com/reference/java/security/KeyStore.html
export const list = (): Promise<IKeyStoreEntry[]> => {
// - Sample Java
//
// KeyStore ks = KeyStore.getInstance("AndroidKeyStore");
// ks.load(null);
// Enumeration<String> aliases = ks.aliases();
//
// while(aliases.hasMoreElements()) {
// Log.e("E", "Aliases = " + aliases.nextElement());
// }
return wrapJavaPerform(() => {
const keyStore: KeyStore = Java.use("java.security.KeyStore");
const entries: IKeyStoreEntry[] = [];
// Prepare the AndroidKeyStore keystore provider and load it.
// Maybe at a later stage we should support adding other stores
// like from file or JKS.
const ks: KeyStore = keyStore.getInstance("AndroidKeyStore");
ks.load(null, null);
// Get the aliases and loop through them. The aliases() method
// return an Enumeration<String> type.
const aliases = ks.aliases();
while (aliases.hasMoreElements()) {
const alias = aliases.nextElement();
entries.push({
alias: alias.toString(),
is_certificate: ks.isCertificateEntry(alias),
is_key: ks.isKeyEntry(alias),
});
}
return entries;
});
};
// Dump detailed information about keystore entries per alias.
//
// Refs:
// https://labs.f-secure.com/blog/how-secure-is-your-android-keystore-authentication
// https://github.com/FSecureLABS/android-keystore-audit
export const detail = (): Promise<IKeyStoreDetail[]> => {
// helper function to extract keystore alias information
const keystore_info = (alias): IKeyStoreDetail => {
const r: IKeyStoreDetail = {};
wrapJavaPerform(() => {
// java class handles
const keyStore: KeyStore = Java.use('java.security.KeyStore');
const keyFactory: KeyFactory = Java.use('java.security.KeyFactory');
const keyInfo: KeyInfo = Java.use('android.security.keystore.KeyInfo');
const keySecretKeyFactory: SecretKeyFactory = Java.use('javax.crypto.SecretKeyFactory');
// load the keystore entry
const keyStoreObj = keyStore.getInstance('AndroidKeyStore');
keyStoreObj.load(null);
const key = keyStoreObj.getKey(alias, null);
if (key == null) return null;
let keySpec = null;
try {
keySpec = keyFactory.getInstance(key.getAlgorithm(), 'AndroidKeyStore')
.getKeySpec(key, keyInfo.class);
} catch (err) {
keySpec = keySecretKeyFactory.getInstance(key.getAlgorithm(), 'AndroidKeyStore')
.getKeySpec(key, keyInfo.class);
}
// set result fields
r.keyAlgorithm = key.getAlgorithm();
r.keySize = keyInfo['getKeySize'].call(keySpec);
r.blockModes = keyInfo['getBlockModes'].call(keySpec);
r.digests = keyInfo['getDigests'].call(keySpec);
r.encryptionPaddings = keyInfo['getEncryptionPaddings'].call(keySpec);
r.keyValidityForConsumptionEnd = keyInfo['getKeyValidityForConsumptionEnd'].call(keySpec);
r.keyValidityForOriginationEnd = keyInfo['getKeyValidityForOriginationEnd'].call(keySpec);
r.keyValidityStart = keyInfo['getKeyValidityStart'].call(keySpec);
r.keystoreAlias = keyInfo['getKeystoreAlias'].call(keySpec);
r.origin = keyInfo['getOrigin'].call(keySpec);
r.purposes = keyInfo['getPurposes'].call(keySpec);
r.signaturePaddings = keyInfo['getSignaturePaddings'].call(keySpec);
r.userAuthenticationValidityDurationSeconds = keyInfo['getUserAuthenticationValidityDurationSeconds'].call(keySpec);
r.isInsideSecureHardware = keyInfo['isInsideSecureHardware'].call(keySpec);
r.isInvalidatedByBiometricEnrollment = keyInfo['isInvalidatedByBiometricEnrollment'].call(keySpec);
r.isUserAuthenticationRequired = keyInfo['isUserAuthenticationRequired'].call(keySpec);
r.isUserAuthenticationRequirementEnforcedBySecureHardware = keyInfo['isUserAuthenticationRequirementEnforcedBySecureHardware'].call(keySpec);
r.isUserAuthenticationValidWhileOnBody = keyInfo['isUserAuthenticationValidWhileOnBody'].call(keySpec);
// "crashy" calls that's ok if they fail
try {
r.isTrustedUserPresenceRequired = keyInfo['isTrustedUserPresenceRequired'].call(keySpec);
} catch (err) { }
try {
r.isUserConfirmationRequired = keyInfo['isUserConfirmationRequired'].call(keySpec);
} catch (err) { }
// translate some values to string representation if they are not empty
if (r.keyValidityForConsumptionEnd != null)
r.keyValidityForConsumptionEnd = r.keyValidityForConsumptionEnd.toString();
if (r.keyValidityForOriginationEnd != null)
r.keyValidityForOriginationEnd = r.keyValidityForOriginationEnd.toString();
if (r.keyValidityStart != null)
r.keyValidityStart = r.keyValidityStart.toString();
});
return r;
};
return wrapJavaPerform((): IKeyStoreDetail[] => {
const keyStore: KeyStore = Java.use("java.security.KeyStore");
const ks: KeyStore = keyStore.getInstance("AndroidKeyStore");
ks.load(null, null);
const aliases = ks.aliases();
const info: IKeyStoreDetail[] = [];
while (aliases.hasMoreElements()) {
var a = aliases.nextElement();
info.push(keystore_info(a.toString()));
}
return info;
});
};
// Delete all entries in the Android Keystore
//
// Ref: https://developer.android.com/reference/java/security/KeyStore.html#deleteEntry(java.lang.String)
export const clear = () => {
// - Sample Java
//
// KeyStore ks = KeyStore.getInstance("AndroidKeyStore");
// ks.load(null);
// Enumeration<String> aliases = ks.aliases();
//
// while(aliases.hasMoreElements()) {
// ks.deleteEntry(aliases.nextElement());
// }
return wrapJavaPerform(() => {
const keyStore: KeyStore = Java.use("java.security.KeyStore");
// Prepare the AndroidKeyStore keystore provider and load it.
// Maybe at a later stage we should support adding other stores
// like from file or JKS.
const ks: KeyStore = keyStore.getInstance("AndroidKeyStore");
ks.load(null, null);
// Get the aliases and loop through them. The aliases() method
// return an Enumeration<String> type.
const aliases = ks.aliases();
while (aliases.hasMoreElements()) {
ks.deleteEntry(aliases.nextElement());
}
send(c.blackBright(`Keystore entries cleared`));
});
};
// keystore watch methods
// Watch for KeyStore.load();
// TODO: Store the keystores themselves maybe?
const keystoreLoad = (ident: number): Promise<any> => {
return wrapJavaPerform(() => {
const ks: KeyStore = Java.use("java.security.KeyStore");
const ksLoad = ks.load.overload("java.io.InputStream", "[C");
send(c.blackBright(`[${ident}] Watching Keystore.load("java.io.InputStream", "[C")`));
ksLoad.implementation = function (stream, password) {
send(c.blackBright(`[${ident}] `) +
`Keystore.load(${c.greenBright(stream)}, ${c.redBright(password || `null`)}) ` +
`called, loading a ${c.cyanBright(this.getType())} keystore.`);
return this.load(stream, password);
};
return ksLoad
});
};
// Watch for Keystore.getKey().
// TODO: Extract more information, like the key itself maybe?
const keystoreGetKey = (ident: number): Promise<any> => {
return wrapJavaPerform(() => {
const ks: KeyStore = Java.use("java.security.KeyStore");
const ksGetKey = ks.getKey.overload("java.lang.String", "[C");
send(c.blackBright(`[${ident}] Watching Keystore.getKey("java.lang.String", "[C")`));
ksGetKey.implementation = function (alias, password) {
const key = this.getKey(alias, password);
send(c.blackBright(`[${ident}] `) +
`Keystore.getKey(${c.greenBright(alias)}, ${c.redBright(password || `null`)}) ` +
`called, returning a ${c.greenBright(key.$className)} instance.`);
return key;
};
return ksGetKey;
});
};
// Android KeyStore watcher.
// Many, many more methods can be added here..
export const watchKeystore = async (): Promise<void> => {
const job: jobs.Job = new jobs.Job(jobs.identifier(), "android-keystore-watch");
job.addImplementation(await keystoreLoad(job.identifier));
job.addImplementation(await keystoreGetKey(job.identifier));
jobs.add(job);
};