Search in sources :

Example 36 with CertificateFactory

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;
}
Also used : InputStream(java.io.InputStream) CertificateFactory(java.security.cert.CertificateFactory) X509Certificate(java.security.cert.X509Certificate)

Example 37 with CertificateFactory

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;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) Bundle(android.os.Bundle) CertificateException(java.security.cert.CertificateException) CertificateFactory(java.security.cert.CertificateFactory) X509Certificate(java.security.cert.X509Certificate) X509Certificate(java.security.cert.X509Certificate) Certificate(java.security.cert.Certificate) SslCertificate(android.net.http.SslCertificate)

Example 38 with CertificateFactory

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);
        }
    }
}
Also used : Path(java.nio.file.Path) SunJarSignImpl(com.googlecode.d2j.signapk.SunJarSignImpl) PrivateKey(java.security.PrivateKey) IOException(java.io.IOException) CertificateFactory(java.security.cert.CertificateFactory) X509Certificate(java.security.cert.X509Certificate) IOException(java.io.IOException) AbstractJarSign(com.googlecode.d2j.signapk.AbstractJarSign) FileSystem(java.nio.file.FileSystem) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) File(java.io.File) KeyFactory(java.security.KeyFactory) TinySignImpl(com.googlecode.d2j.signapk.TinySignImpl)

Example 39 with CertificateFactory

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);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) CertificateException(java.security.cert.CertificateException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest) CertificateFactory(java.security.cert.CertificateFactory) X509Certificate(java.security.cert.X509Certificate)

Example 40 with CertificateFactory

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;
}
Also used : CertificateParsingException(java.security.cert.CertificateParsingException) CertPathBuilderResult(java.security.cert.CertPathBuilderResult) PKIXCertPathBuilderResult(java.security.cert.PKIXCertPathBuilderResult) CertificateFactory(java.security.cert.CertificateFactory) CertificateParsingException(java.security.cert.CertificateParsingException) ExtCertPathBuilderException(org.bouncycastle.jce.exception.ExtCertPathBuilderException) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) CertPathBuilderException(java.security.cert.CertPathBuilderException) X509Certificate(java.security.cert.X509Certificate) CertPathValidator(java.security.cert.CertPathValidator) PKIXCertPathValidatorResult(java.security.cert.PKIXCertPathValidatorResult) PKIXCertPathBuilderResult(java.security.cert.PKIXCertPathBuilderResult) Iterator(java.util.Iterator) Collection(java.util.Collection) CertPath(java.security.cert.CertPath) HashSet(java.util.HashSet)

Aggregations

CertificateFactory (java.security.cert.CertificateFactory)550 X509Certificate (java.security.cert.X509Certificate)409 ByteArrayInputStream (java.io.ByteArrayInputStream)372 Certificate (java.security.cert.Certificate)272 CertificateException (java.security.cert.CertificateException)120 KeyFactory (java.security.KeyFactory)103 PrivateKey (java.security.PrivateKey)93 InputStream (java.io.InputStream)92 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)86 IOException (java.io.IOException)80 KeyStore (java.security.KeyStore)77 PrivateKeyEntry (java.security.KeyStore.PrivateKeyEntry)67 Entry (java.security.KeyStore.Entry)59 TrustedCertificateEntry (java.security.KeyStore.TrustedCertificateEntry)59 KeyStoreException (java.security.KeyStoreException)49 ArrayList (java.util.ArrayList)49 FileInputStream (java.io.FileInputStream)47 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)35 File (java.io.File)23 HashSet (java.util.HashSet)21