package com.miniabs.net; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; import android.content.Context; /** Mozilla roots shipped as assets/roots/-.der */ public final class AssetRoots implements CertVerifier.RootStore { private final Context ctx; private Set names; public AssetRoots(Context c) { ctx = c.getApplicationContext(); } public synchronized List find(byte[] subjectDer) { try { if (names == null) { names = new HashSet(); String[] l = ctx.getAssets().list("roots"); if (l != null) for (int i = 0; i < l.length; i++) names.add(l[i]); } String h = CertVerifier.sha1Hex(subjectDer); List out = null; for (int i = 0; names.contains(h + "-" + i + ".der"); i++) { if (out == null) out = new ArrayList(); out.add(readAll(ctx.getAssets().open("roots/" + h + "-" + i + ".der"))); } return out; } catch (IOException e) { return null; } } static byte[] readAll(InputStream in) throws IOException { try { ByteArrayOutputStream b = new ByteArrayOutputStream(); byte[] buf = new byte[4096]; int n; while ((n = in.read(buf)) > 0) b.write(buf, 0, n); return b.toByteArray(); } finally { in.close(); } } }