Refactoring

pull/212/head
M66B 2 years ago
parent 3d6876d51f
commit cb9102b9ed

@ -2013,6 +2013,20 @@ public class MessageHelper {
return signers; return signers;
for (String header : headers) { for (String header : headers) {
String signer = verifySignatureHeader(context, header, DKIM_SIGNATURE, amessage);
if (signer != null && !signers.contains(signer))
signers.add(signer);
}
Log.i("DKIM signers=" + TextUtils.join(",", signers));
} catch (Throwable ex) {
Log.e("DKIM", ex);
}
return signers;
}
private String verifySignatureHeader(Context context, String header, String name, MimeMessage amessage) {
Map<String, String> kv = getKeyValues(MimeUtility.unfold(header)); Map<String, String> kv = getKeyValues(MimeUtility.unfold(header));
String a = kv.get("a"); String a = kv.get("a");
@ -2027,7 +2041,7 @@ public class MessageHelper {
} else { } else {
// TODO: Ed25519 // TODO: Ed25519
Log.i("DKIM a=" + a); Log.i("DKIM a=" + a);
continue; return null;
} }
try { try {
@ -2036,7 +2050,7 @@ public class MessageHelper {
Log.i("DKIM lookup " + dns); Log.i("DKIM lookup " + dns);
DnsHelper.DnsRecord[] records = DnsHelper.lookup(context, dns, "txt"); DnsHelper.DnsRecord[] records = DnsHelper.lookup(context, dns, "txt");
if (records.length == 0) if (records.length == 0)
continue; return null;
Log.i("DKIM got " + records[0].name); Log.i("DKIM got " + records[0].name);
Map<String, String> dk = getKeyValues(records[0].name); Map<String, String> dk = getKeyValues(records[0].name);
@ -2062,7 +2076,7 @@ public class MessageHelper {
if (!from) if (!from)
throw new IllegalArgumentException("from missing: " + hs); throw new IllegalArgumentException("from missing: " + hs);
keys.add(DKIM_SIGNATURE); keys.add(name);
Map<String, Integer> index = new Hashtable<>(); Map<String, Integer> index = new Hashtable<>();
for (String key : keys) { for (String key : keys) {
@ -2072,7 +2086,7 @@ public class MessageHelper {
idx = (idx == null ? 1 : idx + 1); idx = (idx == null ? 1 : idx + 1);
index.put(_key, idx); index.put(_key, idx);
String[] values = (DKIM_SIGNATURE.equals(key) String[] values = (name.equals(key)
? new String[]{header} ? new String[]{header}
: amessage.getHeader(key)); : amessage.getHeader(key));
if (values == null || idx > values.length) { if (values == null || idx > values.length) {
@ -2083,7 +2097,7 @@ public class MessageHelper {
} }
String value = values[values.length - idx]; String value = values[values.length - idx];
if (DKIM_SIGNATURE.equals(key)) { if (name.equals(key)) {
int b = value.lastIndexOf("b="); int b = value.lastIndexOf("b=");
int s = value.indexOf(";", b + 2); int s = value.indexOf(";", b + 2);
value = value.substring(0, b + 2) + (s < 0 ? "" : value.substring(s)); value = value.substring(0, b + 2) + (s < 0 ? "" : value.substring(s));
@ -2091,7 +2105,7 @@ public class MessageHelper {
Log.i("DKIM " + key + "=" + value.replaceAll("\\r?\\n", "|")); Log.i("DKIM " + key + "=" + value.replaceAll("\\r?\\n", "|"));
if ("simple".equals(c[0])) { if ("simple".equals(c[0])) {
if (DKIM_SIGNATURE.equals(key)) if (name.equals(key))
head.append(key).append(": ").append(value); head.append(key).append(": ").append(value);
else { else {
// Find original header/name (case sensitive) // Find original header/name (case sensitive)
@ -2115,7 +2129,7 @@ public class MessageHelper {
} else } else
throw new IllegalArgumentException(c[0]); throw new IllegalArgumentException(c[0]);
if (!DKIM_SIGNATURE.equals(key)) if (!name.equals(key))
head.append("\r\n"); head.append("\r\n");
} }
Log.i("DKIM head=" + head.toString().replace("\r\n", "|")); Log.i("DKIM head=" + head.toString().replace("\r\n", "|"));
@ -2162,7 +2176,7 @@ public class MessageHelper {
String pubkey = dk.get("p"); String pubkey = dk.get("p");
if (pubkey == null) if (pubkey == null)
continue; return null;
String p = pubkey.replaceAll("\\s+", ""); String p = pubkey.replaceAll("\\s+", "");
Log.i("DKIM pubkey=" + p); Log.i("DKIM pubkey=" + p);
@ -2174,7 +2188,7 @@ public class MessageHelper {
String hash = kv.get("b"); String hash = kv.get("b");
if (hash == null) if (hash == null)
continue; return null;
String s = hash.replaceAll("\\s+", ""); String s = hash.replaceAll("\\s+", "");
Log.i("DKIM signature=" + s); Log.i("DKIM signature=" + s);
@ -2188,20 +2202,13 @@ public class MessageHelper {
" dns=" + dns + " dns=" + dns +
" from=" + formatAddresses(getFrom())); " from=" + formatAddresses(getFrom()));
if (verified && if (verified)
!signers.contains(signer)) return signer;
signers.add(signer);
} catch (Throwable ex) {
Log.e("DKIM", ex);
}
}
Log.i("DKIM signers=" + TextUtils.join(",", signers));
} catch (Throwable ex) { } catch (Throwable ex) {
Log.e("DKIM", ex); Log.e("DKIM", ex);
} }
return signers; return null;
} }
Address[] getMailFrom(String[] headers) { Address[] getMailFrom(String[] headers) {

Loading…
Cancel
Save