use of java.security.cert.CertificateException in project carat by amplab.
the class SamplingLibrary method getSignatures.
public static List<String> getSignatures(PackageInfo pak) {
List<String> sigList = new LinkedList<String>();
String[] pmInfos = pak.requestedPermissions;
if (pmInfos != null) {
byte[] bytes = getPermissionBytes(pmInfos);
String hexB = convertToHex(bytes);
sigList.add(hexB);
}
Signature[] sigs = pak.signatures;
for (Signature s : sigs) {
MessageDigest md = null;
try {
md = MessageDigest.getInstance("SHA-1");
md.update(s.toByteArray());
byte[] dig = md.digest();
// Add SHA-1
sigList.add(convertToHex(dig));
CertificateFactory fac = CertificateFactory.getInstance("X.509");
if (fac == null)
continue;
X509Certificate cert = (X509Certificate) fac.generateCertificate(new ByteArrayInputStream(s.toByteArray()));
if (cert == null)
continue;
PublicKey pkPublic = cert.getPublicKey();
if (pkPublic == null)
continue;
String al = pkPublic.getAlgorithm();
if (al.equals("RSA")) {
md = MessageDigest.getInstance("SHA-256");
RSAPublicKey rsa = (RSAPublicKey) pkPublic;
byte[] data = rsa.getModulus().toByteArray();
if (data[0] == 0) {
byte[] copy = new byte[data.length - 1];
System.arraycopy(data, 1, copy, 0, data.length - 1);
md.update(copy);
} else
md.update(data);
dig = md.digest();
// Add SHA-256 of modulus
sigList.add(convertToHex(dig));
} else if (al.equals("DSA")) {
DSAPublicKey dsa = (DSAPublicKey) pkPublic;
md = MessageDigest.getInstance("SHA-256");
byte[] data = dsa.getY().toByteArray();
if (data[0] == 0) {
byte[] copy = new byte[data.length - 1];
System.arraycopy(data, 1, copy, 0, data.length - 1);
md.update(copy);
} else
md.update(data);
dig = md.digest();
// Add SHA-256 of public key (DSA)
sigList.add(convertToHex(dig));
} else {
Log.e("SamplingLibrary", "Weird algorithm: " + al + " for " + pak.packageName);
}
} catch (NoSuchAlgorithmException e) {
// Do nothing
} catch (CertificateException e) {
// Do nothing
}
}
return sigList;
}
use of java.security.cert.CertificateException in project Lazy by l123456789jy.
the class AppUtils method isDebuggable.
/**
* 检测当前应用是否是Debug版本
*
* @param ctx 上下文
* @return 是否是Debug版本
*/
public static boolean isDebuggable(Context ctx) {
boolean debuggable = false;
try {
PackageInfo pinfo = ctx.getPackageManager().getPackageInfo(ctx.getPackageName(), PackageManager.GET_SIGNATURES);
Signature[] signatures = pinfo.signatures;
for (int i = 0; i < signatures.length; i++) {
CertificateFactory cf = CertificateFactory.getInstance("X.509");
ByteArrayInputStream stream = new ByteArrayInputStream(signatures[i].toByteArray());
X509Certificate cert = (X509Certificate) cf.generateCertificate(stream);
debuggable = cert.getSubjectX500Principal().equals(DEBUG_DN);
if (debuggable)
break;
}
} catch (NameNotFoundException e) {
} catch (CertificateException e) {
}
return debuggable;
}
use of java.security.cert.CertificateException in project android-async-http by loopj.
the class CustomCASample method onCreate.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
InputStream is = null;
try {
// Configure the library to use a custom 'bks' file to perform
// SSL negotiation.
KeyStore store = KeyStore.getInstance(KeyStore.getDefaultType());
is = getResources().openRawResource(R.raw.store);
store.load(is, STORE_PASS.toCharArray());
getAsyncHttpClient().setSSLSocketFactory(new SecureSocketFactory(store, STORE_ALIAS));
} catch (IOException e) {
throw new KeyStoreException(e);
} catch (CertificateException e) {
throw new KeyStoreException(e);
} catch (NoSuchAlgorithmException e) {
throw new KeyStoreException(e);
} catch (KeyManagementException e) {
throw new KeyStoreException(e);
} catch (UnrecoverableKeyException e) {
throw new KeyStoreException(e);
} finally {
AsyncHttpClient.silentCloseInputStream(is);
}
} catch (KeyStoreException e) {
Log.e(LOG_TAG, "Unable to initialize key store", e);
showCustomCAHelp();
}
}
use of java.security.cert.CertificateException in project keywhiz by square.
the class ExpirationExtractor method expirationFromRawCertificate.
@Nullable
public static Instant expirationFromRawCertificate(byte[] content) {
CertificateFactory cf;
try {
cf = CertificateFactory.getInstance("X.509");
} catch (CertificateException e) {
// Should never happen (X.509 supported by default)
throw Throwables.propagate(e);
}
X509Certificate cert;
try {
cert = (X509Certificate) cf.generateCertificate(new ByteArrayInputStream(content));
} catch (CertificateException e) {
// Certificate must have been invalid
logger.info("Failed to parse certificate", e);
return null;
}
return cert.getNotAfter().toInstant();
}
use of java.security.cert.CertificateException in project keywhiz by square.
the class ExpirationExtractor method expirationFromKeystore.
@Nullable
public static Instant expirationFromKeystore(String type, String password, byte[] content) {
KeyStore ks;
try {
ks = KeyStore.getInstance(type);
} catch (KeyStoreException e) {
// Should never occur (assuming JCE is installed)
throw Throwables.propagate(e);
}
try {
ks.load(new ByteArrayInputStream(content), password.toCharArray());
} catch (IOException | NoSuchAlgorithmException | CertificateException e) {
// Failed to parse
logger.info("Failed to parse keystore", e);
return null;
}
Instant earliest = null;
try {
for (String alias : list(ks.aliases())) {
Certificate[] chain = ks.getCertificateChain(alias);
if (chain == null) {
Certificate certificate = ks.getCertificate(alias);
if (certificate == null) {
// No certs in this entry
continue;
}
chain = new Certificate[] { certificate };
}
for (Certificate cert : chain) {
if (cert instanceof X509Certificate) {
X509Certificate c = (X509Certificate) cert;
if (earliest == null || c.getNotAfter().toInstant().isBefore(earliest)) {
earliest = c.getNotAfter().toInstant();
}
}
}
}
} catch (KeyStoreException e) {
// Should never occur (ks was initialized)
throw Throwables.propagate(e);
}
return earliest;
}
Aggregations