Skip to content

Commit 253e50a

Browse files
author
Tom James Holub
committed
decrypting non-armored files | #118
1 parent 128018e commit 253e50a

File tree

4 files changed

+105
-94
lines changed

4 files changed

+105
-94
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ captures/
3030
*.iml
3131
.idea/workspace.xml
3232
.idea/libraries
33+
.idea/misc.xml
3334

3435
# Keystore files
3536
*.jks

FlowCrypt/src/main/assets/js/tool.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2032,16 +2032,22 @@
20322032
}
20332033

20342034
function crypto_message_decrypt(db, account_email, encrypted_data, one_time_message_password, callback, force_output_format) {
2035-
var armored_encrypted = tool.value(crypto_armor_headers('message').begin).in(encrypted_data);
2036-
var armored_signed_only = tool.value(crypto_armor_headers('signed_message').begin).in(encrypted_data);
2035+
var armored_encrypted = false;
2036+
var armored_signed_only = false;
20372037
var other_errors = [];
20382038
try {
2039-
if(armored_encrypted) {
2040-
var message = openpgp.message.readArmored(encrypted_data);
2041-
} else if(armored_signed_only) {
2042-
var message = openpgp.cleartext.readArmored(encrypted_data);
2039+
if (encrypted_data instanceof Uint8Array) {
2040+
var message = openpgp.message.read(encrypted_data);
20432041
} else {
2044-
var message = openpgp.message.read(tool.str.to_uint8(encrypted_data));
2042+
armored_encrypted = tool.value(crypto_armor_headers('message').begin).in(encrypted_data);
2043+
armored_signed_only = !armored_encrypted && tool.value(crypto_armor_headers('signed_message').begin).in(encrypted_data);
2044+
if(armored_encrypted) {
2045+
var message = openpgp.message.readArmored(encrypted_data);
2046+
} else if(armored_signed_only) {
2047+
var message = openpgp.cleartext.readArmored(encrypted_data);
2048+
} else {
2049+
var message = openpgp.message.read(tool.str.to_uint8(encrypted_data));
2050+
}
20452051
}
20462052
} catch(format_error) {
20472053
callback({success: false, counts: zeroed_decrypt_error_counts(), format_error: format_error.message, errors: other_errors, encrypted: null, signature: null});

FlowCrypt/src/main/java/com/flowcrypt/email/js/Js.java

Lines changed: 81 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -65,37 +65,29 @@ public Js(Context context, StorageConnectorInterface storage) throws IOException
6565
}
6666

6767
public Boolean str_is_email_valid(String email) {
68-
return (Boolean) this.call(Boolean.class, new String[]{"str", "is_email_valid"}, new
69-
V8Array(v8).push(email));
68+
return (Boolean) this.call(Boolean.class, new String[]{"str", "is_email_valid"}, new V8Array(v8).push(email));
7069
}
7170

7271
public PgpContact str_parse_email(String email) {
73-
V8Object e = (V8Object) this.call(Object.class, new String[]{"str", "parse_email"}, new
74-
V8Array(v8).push(email));
72+
V8Object e = (V8Object) this.call(Object.class, new String[]{"str", "parse_email"}, new V8Array(v8).push(email));
7573
return new PgpContact(e.getString("email"), e.getString("name"));
7674
}
7775

7876
public String str_base64url_encode(String str) {
79-
return (String) this.call(String.class, new String[]{"str", "base64url_encode"}, new
80-
V8Array(v8).push(str));
77+
return (String) this.call(String.class, new String[]{"str", "base64url_encode"}, new V8Array(v8).push(str));
8178
}
8279

8380
public String str_base64url_decode(String str) {
84-
return (String) this.call(String.class, new String[]{"str", "base64url_decode"}, new
85-
V8Array(v8).push(str));
81+
return (String) this.call(String.class, new String[]{"str", "base64url_decode"}, new V8Array(v8).push(str));
8682
}
8783

8884
public long time_to_utc_timestamp(String str) {
89-
return Long.parseLong((String) this.call(String.class, new String[]{"time",
90-
"to_utc_timestamp"}, new V8Array(v8).push(str).push(true)));
85+
return Long.parseLong((String) this.call(String.class, new String[]{"time", "to_utc_timestamp"},
86+
new V8Array(v8).push(str).push(true)));
9187
}
9288

9389
public MimeMessage mime_decode(String mime_message) {
94-
Long start = System.currentTimeMillis();
95-
this.call(Object.class, new String[]{"mime", "decode"}, new V8Array(v8).push
96-
(mime_message).push(cb_catcher));
97-
Long end = System.currentTimeMillis();
98-
System.out.println("duration decode: " + (end - start));
90+
this.call(Object.class, new String[]{"mime", "decode"}, new V8Array(v8).push(mime_message).push(cb_catcher));
9991
if ((Boolean) cb_last_value[0]) {
10092
return new MimeMessage((V8Object) cb_last_value[1], this);
10193
} else {
@@ -121,49 +113,46 @@ public String mime_encode(String body, PgpContact[] to, PgpContact from, String
121113
}
122114

123115
public ProcessedMime mime_process(String mime_message) {
124-
this.call(Object.class, new String[]{"mime", "process"}, new V8Array(v8).push
125-
(mime_message).push(cb_catcher));
116+
this.call(Object.class, new String[]{"mime", "process"}, new V8Array(v8).push(mime_message).push(cb_catcher));
126117
return new ProcessedMime((V8Object) cb_last_value[0], this);
127118
}
128119

129120
public String crypto_key_normalize(String armored_key) {
130-
return (String) this.call(String.class, new String[]{"crypto", "key", "normalize"}, new
131-
V8Array(v8).push(armored_key));
121+
return (String) this.call(String.class, new String[]{"crypto", "key", "normalize"}, new V8Array(v8)
122+
.push(armored_key));
132123
}
133124

134125
public PgpKey crypto_key_read(String armored_key) {
135-
return new PgpKey((V8Object) this.call(Object.class, new String[]{"crypto", "key",
136-
"read"}, new V8Array(v8).push(armored_key)), this);
126+
return new PgpKey((V8Object) this.call(Object.class, new String[]{"crypto", "key", "read"}, new V8Array(v8)
127+
.push(armored_key)), this);
137128
}
138129

139130
public V8Object crypto_key_decrypt(PgpKey private_key, String passphrase) {
140-
return (V8Object) this.call(Object.class, new String[]{"crypto", "key", "decrypt"}, new
141-
V8Array(v8).push(private_key.getV8Object()).push(passphrase));
131+
return (V8Object) this.call(Object.class, new String[]{"crypto", "key", "decrypt"}, new V8Array(v8)
132+
.push(private_key.getV8Object()).push(passphrase));
142133
}
143134

144135
public String crypto_key_fingerprint(PgpKey key) {
145-
return (String) this.call(String.class, new String[]{"crypto", "key", "fingerprint"}, new
146-
V8Array(v8).push(key.getV8Object()));
136+
return (String) this.call(String.class, new String[]{"crypto", "key", "fingerprint"}, new V8Array(v8)
137+
.push(key.getV8Object()));
147138
}
148139

149140
public String crypto_key_longid(PgpKey key) {
150-
return (String) this.call(String.class, new String[]{"crypto", "key", "longid"}, new
151-
V8Array(v8).push(key.getV8Object()));
141+
return (String) this.call(String.class, new String[]{"crypto", "key", "longid"}, new V8Array(v8)
142+
.push(key.getV8Object()));
152143
}
153144

154145
public String crypto_key_longid(String fingerprint) {
155-
return (String) this.call(String.class, new String[]{"crypto", "key", "longid"}, new
156-
V8Array(v8).push(fingerprint));
146+
return (String) this.call(String.class, new String[]{"crypto", "key", "longid"}, new V8Array(v8)
147+
.push(fingerprint));
157148
}
158149

159150
public String crypto_armor_clip(String text) {
160-
return (String) this.call(String.class, new String[]{"crypto", "armor", "clip"}, new
161-
V8Array(v8).push(text));
151+
return (String) this.call(String.class, new String[]{"crypto", "armor", "clip"}, new V8Array(v8).push(text));
162152
}
163153

164154
public String mnemonic(String longid) {
165-
return (String) this.call(String.class, v8, new String[]{"mnemonic"}, new V8Array(v8)
166-
.push(longid));
155+
return (String) this.call(String.class, v8, new String[]{"mnemonic"}, new V8Array(v8).push(longid));
167156
}
168157

169158
public String crypto_message_encrypt(String pubkeys[], String text) {
@@ -184,27 +173,30 @@ public byte[] crypto_message_encrypt(String pubkeys[], byte[] content, String fi
184173

185174
public PgpDecrypted crypto_message_decrypt(String data, String password) {
186175
// db,account_email,encrypted_data,one_time_message_password,callback,force_output_format
187-
Long start = System.currentTimeMillis();
188-
V8Array params = new V8Array(v8).push(NULL).push("").push(data).push(password).push
189-
(cb_catcher).push(NULL);
176+
V8Array params = new V8Array(v8).push(NULL).push("").push(data).push(password).push(cb_catcher).push(NULL);
190177
this.call(void.class, new String[]{"crypto", "message", "decrypt"}, params);
191-
Long end = System.currentTimeMillis();
192-
System.out.println("duration decrypt: " + (end - start));
193178
return new PgpDecrypted((V8Object) cb_last_value[0]);
194179
}
195180

196181
public PgpDecrypted crypto_message_decrypt(String data) {
197182
return crypto_message_decrypt(data, "");
198183
}
199184

185+
public PgpDecrypted crypto_message_decrypt(byte[] bytes) {
186+
// db,account_email,encrypted_data,one_time_message_password,callback,force_output_format
187+
V8Array params = new V8Array(v8).push(NULL).push("").push(uint8(bytes)).push("").push(cb_catcher).push(NULL);
188+
this.call(void.class, new String[]{"crypto", "message", "decrypt"}, params);
189+
return new PgpDecrypted((V8Object) cb_last_value[0]);
190+
}
191+
200192
public String api_gmail_query_backups(String account_email) {
201193
return (String) this.call(String.class, new String[]{"api", "gmail", "query", "backups"},
202194
new V8Array(v8).push(account_email));
203195
}
204196

205197
public IdToken api_auth_parse_id_token(String id_token) {
206-
return new IdToken((V8Object) this.call(Object.class, new String[]{"api", "auth",
207-
"parse_id_token"}, new V8Array(v8).push(id_token)));
198+
return new IdToken((V8Object) this.call(Object.class, new String[]{"api", "auth", "parse_id_token"},
199+
new V8Array(v8).push(id_token)));
208200
}
209201

210202
/**
@@ -255,8 +247,8 @@ private static String read(InputStream inputStream) throws IOException {
255247
}
256248

257249
private V8Object mime_reply_headers(MimeMessage original) {
258-
return (V8Object) this.call(Object.class, new String[]{"mime", "reply_headers"}, new
259-
V8Array(v8).push(original.getV8Object()));
250+
return (V8Object) this.call(Object.class, new String[]{"mime", "reply_headers"}, new V8Array(v8)
251+
.push(original.getV8Object()));
260252
}
261253

262254
private Object call(Class<?> return_type, String path[], V8Array args) {
@@ -295,26 +287,19 @@ private V8Array array(String arr[]) {
295287

296288
private void bindJavaMethods() {
297289
JavaMethodsForJavascript methods = new JavaMethodsForJavascript(v8, storage);
298-
v8.registerJavaMethod(methods, "console_log", "engine_host_console_log", new
299-
Class[]{String.class});
300-
v8.registerJavaMethod(methods, "console_error", "engine_host_console_error", new
301-
Class[]{String.class});
290+
v8.registerJavaMethod(methods, "console_log", "engine_host_console_log", new Class[]{String.class});
291+
v8.registerJavaMethod(methods, "console_error", "engine_host_console_error", new Class[]{String.class});
302292
v8.registerJavaMethod(methods, "alert", "engine_host_alert", new Class[]{String.class});
303-
v8.registerJavaMethod(methods, "private_keys_get", "private_keys_get", new Class[]{String
304-
.class, V8Array.class});
305-
v8.registerJavaMethod(methods, "private_keys_get", "private_keys_get", new Class[]{String
306-
.class, String.class});
307-
v8.registerJavaMethod(methods, "private_keys_get", "private_keys_get", new Class[]{String
308-
.class});
309-
v8.registerJavaMethod(methods, "get_passphrase", "get_passphrase", new Class[]{String
310-
.class, String.class});
311-
v8.registerJavaMethod(methods, "java_mod_pow_strings", "java_mod_pow", new Class[]{String
312-
.class, String.class, String.class});
313-
v8.registerJavaMethod(methods, "secure_random", "engine_host_secure_random", new Class[]{
314-
Integer.class});
293+
v8.registerJavaMethod(methods, "private_keys_get", "private_keys_get", new Class[]{String.class, V8Array.class});
294+
v8.registerJavaMethod(methods, "private_keys_get", "private_keys_get", new Class[]{String.class, String.class});
295+
v8.registerJavaMethod(methods, "private_keys_get", "private_keys_get", new Class[]{String.class});
296+
v8.registerJavaMethod(methods, "get_passphrase", "get_passphrase", new Class[]{String.class, String.class});
297+
v8.registerJavaMethod(methods, "java_mod_pow_strings", "java_mod_pow", new Class[]{String.class, String.class,
298+
String.class});
299+
v8.registerJavaMethod(methods, "secure_random", "engine_host_secure_random", new Class[]{Integer.class});
315300
v8.registerJavaMethod(methods, "html_to_text", "html_to_text", new Class[]{String.class});
316-
v8.registerJavaMethod(methods, "rsa_decrypt", "java_rsa_decrypt", new Class[]{String.class,
317-
String.class, V8Array.class});
301+
v8.registerJavaMethod(methods, "rsa_decrypt", "java_rsa_decrypt", new Class[]{String.class, String.class,
302+
V8Array.class});
318303

319304
}
320305

@@ -361,61 +346,74 @@ class MeaningfulV8ObjectContainer {
361346
v8object = o;
362347
}
363348

364-
public V8Array getAttributeAsArray(String k) {
349+
V8Array getAttributeAsArray(String k) {
365350
return getAttributeAsArray(v8object, k);
366351
}
367352

368-
public V8Array getAttributeAsArray(V8Object obj, String k) {
353+
V8Object getAttributeAsObject(String name) {
354+
return getAttributeAsObject(v8object, name);
355+
}
356+
357+
Boolean getAttributeAsBoolean(String name) {
358+
return getAttributeAsBoolean(v8object, name);
359+
}
360+
361+
Integer getAttributeAsInteger(String name) {
362+
return getAttributeAsInteger(v8object, name);
363+
}
364+
365+
String getAttributeAsString(String k) {
366+
return getAttributeAsString(v8object, k);
367+
}
368+
369+
byte[] getAttributeAsBytes(String k) {
370+
return getAttributeAsBytes(v8object, k);
371+
}
372+
373+
static V8Array getAttributeAsArray(V8Object obj, String k) {
369374
try {
370375
return obj.getArray(k);
371376
} catch (V8ResultUndefined e) {
372377
return null;
373378
}
374379
}
375380

376-
public V8Object getAttributeAsObject(String name) {
377-
return getAttributeAsObject(v8object, name);
378-
}
379-
380-
public V8Object getAttributeAsObject(V8Object obj, String k) {
381+
static V8Object getAttributeAsObject(V8Object obj, String k) {
381382
try {
382383
return obj.getObject(k);
383384
} catch (V8ResultUndefined e) {
384385
return null;
385386
}
386387
}
387388

388-
public Boolean getAttributeAsBoolean(String name) {
389-
return getAttributeAsBoolean(v8object, name);
390-
}
391-
392-
public Boolean getAttributeAsBoolean(V8Object obj, String k) {
389+
static Boolean getAttributeAsBoolean(V8Object obj, String k) {
393390
try {
394391
return obj.getBoolean(k);
395392
} catch (V8ResultUndefined e) {
396393
return null;
397394
}
398395
}
399396

400-
public Integer getAttributeAsInteger(String name) {
401-
return getAttributeAsInteger(v8object, name);
402-
}
403-
404-
public Integer getAttributeAsInteger(V8Object obj, String k) {
397+
static Integer getAttributeAsInteger(V8Object obj, String k) {
405398
try {
406399
return obj.getInteger(k);
407400
} catch (V8ResultUndefined e) {
408401
return null;
409402
}
410403
}
411404

412-
protected String getAttributeAsString(String k) {
413-
return getAttributeAsString(v8object, k);
405+
static String getAttributeAsString(V8Object obj, String k) {
406+
try {
407+
return obj.getString(k);
408+
} catch (V8ResultUndefined e) {
409+
return null;
410+
}
414411
}
415412

416-
protected String getAttributeAsString(V8Object obj, String k) {
413+
static byte[] getAttributeAsBytes(V8Object obj, String k) {
417414
try {
418-
return obj.getString(k);
415+
V8TypedArray typedArray = (V8TypedArray) obj.getObject(k);
416+
return typedArray.getBytes(0, typedArray.length());
419417
} catch (V8ResultUndefined e) {
420418
return null;
421419
}
@@ -515,13 +513,11 @@ public BigInteger java_mod_pow(BigInteger b, BigInteger e, BigInteger m) {
515513
public String rsa_decrypt(String modulus, String exponent, V8Array encrypted) {
516514
try {
517515
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
518-
KeySpec keySpec = new RSAPrivateKeySpec(new BigInteger(modulus), new BigInteger
519-
(exponent));
516+
KeySpec keySpec = new RSAPrivateKeySpec(new BigInteger(modulus), new BigInteger(exponent));
520517
PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
521518
Cipher decryptCipher = Cipher.getInstance("RSA/ECB/NoPadding");
522519
decryptCipher.init(Cipher.DECRYPT_MODE, privateKey);
523-
byte[] decrypted_bytes = decryptCipher.doFinal(encrypted.getBytes(0, encrypted.length
524-
()));
520+
byte[] decrypted_bytes = decryptCipher.doFinal(encrypted.getBytes(0, encrypted.length()));
525521
return new BigInteger(decrypted_bytes).toString();
526522
} catch (Exception e) {
527523
System.out.println("JAVA RSA ERROR:" + e.getClass() + " --- " + e.getMessage());

FlowCrypt/src/main/java/com/flowcrypt/email/js/PgpDecrypted.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,23 @@ public String getContent() {
6868
if(content == null) {
6969
return null;
7070
}
71-
return this.getAttributeAsString(content, "data"); // todo - may need to convert from uint8
71+
return getAttributeAsString(content, "data");
72+
}
73+
74+
public byte[] getBytes() {
75+
V8Object content = this.getAttributeAsObject("content");
76+
if(content == null) {
77+
return null;
78+
}
79+
return getAttributeAsBytes(content, "data");
7280
}
7381

7482
private Integer getCount(String name) {
7583
V8Object counts = this.getAttributeAsObject("counts");
7684
if(counts == null) {
7785
return null;
7886
}
79-
return this.getAttributeAsInteger(counts, name);
87+
return getAttributeAsInteger(counts, name);
8088
}
8189

8290
private String[] getStrings(String name) {

0 commit comments

Comments
 (0)