use of java.security.cert.CertificateFactory in project netty by netty.
the class SslContextTrustManagerTest method loadCertCollection.
private static X509Certificate[] loadCertCollection(String[] resourceNames) throws Exception {
CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
X509Certificate[] certCollection = new X509Certificate[resourceNames.length];
for (int i = 0; i < resourceNames.length; i++) {
String resourceName = resourceNames[i];
InputStream is = null;
try {
is = SslContextTest.class.getResourceAsStream(resourceName);
assertNotNull("Cannot find " + resourceName, is);
certCollection[i] = (X509Certificate) certFactory.generateCertificate(is);
} finally {
if (is != null) {
is.close();
}
}
}
return certCollection;
}
use of java.security.cert.CertificateFactory in project android by owncloud.
the class SsoWebViewClient method getX509CertificateFromError.
/**
* Obtain the X509Certificate from SslError
* @param error SslError
* @return X509Certificate from error
*/
public X509Certificate getX509CertificateFromError(SslError error) {
Bundle bundle = SslCertificate.saveState(error.getCertificate());
X509Certificate x509Certificate;
byte[] bytes = bundle.getByteArray("x509-certificate");
if (bytes == null) {
x509Certificate = null;
} else {
try {
CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
Certificate cert = certFactory.generateCertificate(new ByteArrayInputStream(bytes));
x509Certificate = (X509Certificate) cert;
} catch (CertificateException e) {
x509Certificate = null;
}
}
return x509Certificate;
}
use of java.security.cert.CertificateFactory in project dex2jar by pxb1988.
the class ApkSign method doCommandLine.
@Override
protected void doCommandLine() throws Exception {
if (remainingArgs.length != 1) {
usage();
return;
}
Path apkIn = new File(remainingArgs[0]).toPath();
if (!Files.exists(apkIn)) {
System.err.println(apkIn + " is not exists");
usage();
return;
}
if (output == null) {
if (Files.isDirectory(apkIn)) {
output = new File(apkIn.getFileName() + "-signed.apk").toPath();
} else {
output = new File(getBaseName(apkIn.getFileName().toString()) + "-signed.apk").toPath();
}
}
if (Files.exists(output) && !forceOverwrite) {
System.err.println(output + " exists, use --force to overwrite");
usage();
return;
}
Path tmp = null;
try {
final Path realJar;
if (Files.isDirectory(apkIn)) {
realJar = Files.createTempFile("d2j", ".jar");
tmp = realJar;
System.out.println("zipping " + apkIn + " -> " + realJar);
try (FileSystem fs = createZip(realJar)) {
final Path outRoot = fs.getPath("/");
walkJarOrDir(apkIn, new FileVisitorX() {
@Override
public void visitFile(Path file, String relative) throws IOException {
Path target = outRoot.resolve(relative);
createParentDirectories(target);
Files.copy(file, target);
}
});
}
} else {
realJar = apkIn;
}
AbstractJarSign signer;
if (tiny) {
signer = new TinySignImpl();
} else {
try {
CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
X509Certificate cert = (X509Certificate) certificateFactory.generateCertificate(ApkSign.class.getResourceAsStream("ApkSign.cer"));
KeyFactory rSAKeyFactory = KeyFactory.getInstance("RSA");
PrivateKey privateKey = rSAKeyFactory.generatePrivate(new PKCS8EncodedKeySpec(ZipUtil.toByteArray(ApkSign.class.getResourceAsStream("ApkSign.private"))));
signer = new SunJarSignImpl(cert, privateKey);
} catch (Exception cnfe) {
signer = new TinySignImpl();
}
}
signer.sign(apkIn.toFile(), output.toFile());
System.out.println("sign " + realJar + " -> " + output);
} finally {
if (tmp != null) {
Files.deleteIfExists(tmp);
}
}
}
use of java.security.cert.CertificateFactory in project robovm by robovm.
the class ProvisioningProfile method getCertFingerprint.
private static String getCertFingerprint(byte[] certData) {
try {
CertificateFactory x509CertFact = CertificateFactory.getInstance("X.509");
X509Certificate cert = (X509Certificate) x509CertFact.generateCertificate(new ByteArrayInputStream(certData));
MessageDigest md = MessageDigest.getInstance("SHA-1");
return toHexString(md.digest(cert.getEncoded()));
} catch (CertificateException e) {
throw new RuntimeException(e);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
}
use of java.security.cert.CertificateFactory in project robovm by robovm.
the class PKIXCertPathBuilderSpi method build.
protected CertPathBuilderResult build(X509Certificate tbvCert, ExtendedPKIXBuilderParameters pkixParams, List tbvPath) {
// PKI graph.
if (tbvPath.contains(tbvCert)) {
return null;
}
// chain.
if (pkixParams.getExcludedCerts().contains(tbvCert)) {
return null;
}
// test if certificate path exceeds maximum length
if (pkixParams.getMaxPathLength() != -1) {
if (tbvPath.size() - 1 > pkixParams.getMaxPathLength()) {
return null;
}
}
tbvPath.add(tbvCert);
CertificateFactory cFact;
CertPathValidator validator;
CertPathBuilderResult builderResult = null;
try {
cFact = CertificateFactory.getInstance("X.509", BouncyCastleProvider.PROVIDER_NAME);
validator = CertPathValidator.getInstance("PKIX", BouncyCastleProvider.PROVIDER_NAME);
} catch (Exception e) {
// cannot happen
throw new RuntimeException("Exception creating support classes.");
}
try {
// check whether the issuer of <tbvCert> is a TrustAnchor
if (CertPathValidatorUtilities.findTrustAnchor(tbvCert, pkixParams.getTrustAnchors(), pkixParams.getSigProvider()) != null) {
// exception message from possibly later tried certification
// chains
CertPath certPath = null;
PKIXCertPathValidatorResult result = null;
try {
certPath = cFact.generateCertPath(tbvPath);
} catch (Exception e) {
throw new AnnotatedException("Certification path could not be constructed from certificate list.", e);
}
try {
result = (PKIXCertPathValidatorResult) validator.validate(certPath, pkixParams);
} catch (Exception e) {
throw new AnnotatedException("Certification path could not be validated.", e);
}
return new PKIXCertPathBuilderResult(certPath, result.getTrustAnchor(), result.getPolicyTree(), result.getPublicKey());
} else {
// add additional X.509 stores from locations in certificate
try {
CertPathValidatorUtilities.addAdditionalStoresFromAltNames(tbvCert, pkixParams);
} catch (CertificateParsingException e) {
throw new AnnotatedException("No additiontal X.509 stores can be added from certificate locations.", e);
}
Collection issuers = new HashSet();
// of the stores
try {
issuers.addAll(CertPathValidatorUtilities.findIssuerCerts(tbvCert, pkixParams));
} catch (AnnotatedException e) {
throw new AnnotatedException("Cannot find issuer certificate for certificate in certification path.", e);
}
if (issuers.isEmpty()) {
throw new AnnotatedException("No issuer certificate for certificate in certification path found.");
}
Iterator it = issuers.iterator();
while (it.hasNext() && builderResult == null) {
X509Certificate issuer = (X509Certificate) it.next();
builderResult = build(issuer, pkixParams, tbvPath);
}
}
} catch (AnnotatedException e) {
certPathException = e;
}
if (builderResult == null) {
tbvPath.remove(tbvCert);
}
return builderResult;
}
Aggregations