BIMI fixes and improvements

This commit is contained in:
M66B 2021-07-16 10:14:49 +02:00
parent e0f1365be3
commit 666fb52e08
1 changed files with 24 additions and 16 deletions

View File

@ -64,12 +64,13 @@ public class Bimi {
private static final int READ_TIMEOUT = 15 * 1000; // milliseconds private static final int READ_TIMEOUT = 15 * 1000; // milliseconds
private static final String OID_BrandIndicatorforMessageIdentification = "1.3.6.1.5.5.7.3.31"; private static final String OID_BrandIndicatorforMessageIdentification = "1.3.6.1.5.5.7.3.31";
static Pair<Bitmap, Boolean> get(Context context, String domain, String selector, int scaleToPixels) static Pair<Bitmap, Boolean> get(
Context context, String domain, String selector, int scaleToPixels)
throws IOException { throws IOException {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean bimi_vmc = prefs.getBoolean("bimi_vmc", false); boolean bimi_vmc = prefs.getBoolean("bimi_vmc", false);
final String txt = selector + "._bimi." + domain; String txt = selector + "._bimi." + domain;
Log.i("BIMI fetch TXT=" + txt); Log.i("BIMI fetch TXT=" + txt);
DnsHelper.DnsRecord[] bimi = DnsHelper.lookup(context, txt, "txt"); DnsHelper.DnsRecord[] bimi = DnsHelper.lookup(context, txt, "txt");
if (bimi.length == 0) if (bimi.length == 0)
@ -84,25 +85,25 @@ public class Bimi {
if (kv.length != 2) if (kv.length != 2)
continue; continue;
switch (kv[0].trim().toLowerCase()) { String tag = kv[0].trim().toLowerCase();
case "v": { // Version switch (tag) {
// Version
case "v": {
String version = kv[1].trim(); String version = kv[1].trim();
if (!"BIMI1".equalsIgnoreCase(version)) if (!"BIMI1".equalsIgnoreCase(version))
Log.w("BIMI unsupported version=" + version); Log.w("BIMI unsupported version=" + version);
break; break;
} }
case "l": { // Image link // Image link
if (!bimi_vmc) case "l": {
continue;
String svg = kv[1].trim(); String svg = kv[1].trim();
if (TextUtils.isEmpty(svg)) if (TextUtils.isEmpty(svg))
continue; continue;
URL url = new URL(svg); URL url = new URL(svg);
Log.i("BIMI favicon " + url); Log.i("BIMI favicon " + url);
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(); HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
connection.setRequestMethod("GET"); connection.setRequestMethod("GET");
connection.setReadTimeout(READ_TIMEOUT); connection.setReadTimeout(READ_TIMEOUT);
@ -112,8 +113,7 @@ public class Bimi {
connection.connect(); connection.connect();
try { try {
bitmap = ImageHelper.renderSvg( bitmap = ImageHelper.renderSvg(connection.getInputStream(),
connection.getInputStream(),
Color.WHITE, scaleToPixels); Color.WHITE, scaleToPixels);
} finally { } finally {
connection.disconnect(); connection.disconnect();
@ -122,15 +122,19 @@ public class Bimi {
break; break;
} }
case "a": { // Certificate link // Certificate link
case "a": {
if (!bimi_vmc)
continue;
String a = kv[1].trim(); String a = kv[1].trim();
if (TextUtils.isEmpty(a)) if (TextUtils.isEmpty(a))
continue; continue;
URL url = new URL(a);
try { try {
URL url = new URL(a);
Log.i("BIMI PEM " + url); Log.i("BIMI PEM " + url);
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(); HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
connection.setRequestMethod("GET"); connection.setRequestMethod("GET");
connection.setReadTimeout(READ_TIMEOUT); connection.setReadTimeout(READ_TIMEOUT);
@ -171,8 +175,8 @@ public class Bimi {
X509Certificate cert = certs.remove(0); X509Certificate cert = certs.remove(0);
// Check certificate type // Check certificate type
List<String> ku = cert.getExtendedKeyUsage(); List<String> eku = cert.getExtendedKeyUsage();
if (!ku.contains(OID_BrandIndicatorforMessageIdentification)) if (!eku.contains(OID_BrandIndicatorforMessageIdentification))
throw new IllegalArgumentException("Invalid certificate type"); throw new IllegalArgumentException("Invalid certificate type");
// Check subject // Check subject
@ -225,8 +229,12 @@ public class Bimi {
} catch (Throwable ex) { } catch (Throwable ex) {
Log.w(new Throwable("BIMI", ex)); Log.w(new Throwable("BIMI", ex));
} }
break; break;
} }
default:
Log.w("Unknown BIMI tag=" + tag);
} }
} }