Search in sources :

Example 76 with CertPath

use of java.security.cert.CertPath in project keystore-explorer by kaikramer.

the class X509CertUtil method getCertsEncodedPkiPath.

/**
 * PKI Path encode a number of certificates.
 *
 * @return The encoding
 * @param certs
 *            The certificates
 * @throws CryptoException
 *             If there was a problem encoding the certificates
 */
public static byte[] getCertsEncodedPkiPath(X509Certificate[] certs) throws CryptoException {
    try {
        ArrayList<Certificate> encodedCerts = new ArrayList<Certificate>();
        Collections.addAll(encodedCerts, certs);
        CertificateFactory cf = CertificateFactory.getInstance(X509_CERT_TYPE, BOUNCY_CASTLE.jce());
        CertPath cp = cf.generateCertPath(encodedCerts);
        return cp.getEncoded(PKI_PATH_ENCODING);
    } catch (CertificateException | NoSuchProviderException e) {
        throw new CryptoException(res.getString("NoPkcs7Encode.exception.message"), e);
    }
}
Also used : ArrayList(java.util.ArrayList) CertificateException(java.security.cert.CertificateException) CertPath(java.security.cert.CertPath) NoSuchProviderException(java.security.NoSuchProviderException) CryptoException(org.kse.crypto.CryptoException) CertificateFactory(java.security.cert.CertificateFactory) X509Certificate(java.security.cert.X509Certificate) Certificate(java.security.cert.Certificate)

Example 77 with CertPath

use of java.security.cert.CertPath in project j2objc by google.

the class CertificateFactory3Test method testGenerateCertPath02.

/**
 * Test for
 * <code>generateCertPath(InputStream inStream, String encoding)</code>
 * method Assertion: returns CertPath with 1 Certificate
 */
public void testGenerateCertPath02() throws Exception {
    CertificateFactory[] certFs = initCertFs();
    assertNotNull("CertificateFactory objects were not created", certFs);
    for (int i = 0; i < certFs.length; i++) {
        CertPath certPath = null;
        InputStream fis = Support_Resources.getResourceStream(fileCertPathPki);
        certPath = certFs[i].generateCertPath(fis, "PkiPath");
        fis.close();
        assertEquals(defaultType, certPath.getType());
        List<? extends Certificate> list1 = certPath.getCertificates();
        assertFalse("Result list is empty", list1.isEmpty());
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) CertPath(java.security.cert.CertPath) CertificateFactory(java.security.cert.CertificateFactory)

Example 78 with CertPath

use of java.security.cert.CertPath in project j2objc by google.

the class CertificateFactory3Test method testGenerateCertPath01.

/**
 * Test for <code>generateCertPath(List certificates)</code> method
 * Assertion: returns CertPath with 1 Certificate
 */
public void testGenerateCertPath01() throws Exception {
    CertificateFactory[] certFs = initCertFs();
    assertNotNull("CertificateFactory objects were not created", certFs);
    // create list of certificates with one certificate
    Certificate cert = certFs[0].generateCertificate(new ByteArrayInputStream(TestUtils.getEncodedX509Certificate()));
    List<Certificate> list = new Vector<Certificate>();
    list.add(cert);
    for (int i = 0; i < certFs.length; i++) {
        CertPath certPath = null;
        certPath = certFs[i].generateCertPath(list);
        assertEquals(cert.getType(), certPath.getType());
        List<? extends Certificate> list1 = certPath.getCertificates();
        assertFalse("Result list is empty", list1.isEmpty());
        Iterator<? extends Certificate> it = list1.iterator();
        assertEquals("Incorrect Certificate in CertPath", cert, it.next());
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) CertPath(java.security.cert.CertPath) CertificateFactory(java.security.cert.CertificateFactory) Vector(java.util.Vector) Certificate(java.security.cert.Certificate)

Example 79 with CertPath

use of java.security.cert.CertPath in project j2objc by google.

the class CertificateFactory3Test method testGenerateCertPath03.

/**
 * Test for <code>generateCertPath(InputStream inStream)</code> method
 * Assertion: returns CertPath with 1 Certificate
 */
public void testGenerateCertPath03() throws Exception {
    String certPathEncoding = "PkiPath";
    CertificateFactory[] certFs = initCertFs();
    assertNotNull("CertificateFactory objects were not created", certFs);
    for (int i = 0; i < certFs.length; i++) {
        Iterator<String> it = certFs[0].getCertPathEncodings();
        assertTrue("no CertPath encodings", it.hasNext());
        assertEquals("Incorrect default encoding", certPathEncoding, it.next());
        CertPath certPath = null;
        InputStream fis = Support_Resources.getResourceStream(fileCertPathPki);
        certPath = certFs[i].generateCertPath(fis);
        fis.close();
        assertEquals(defaultType, certPath.getType());
        List<? extends Certificate> list1 = certPath.getCertificates();
        assertFalse("Result list is empty", list1.isEmpty());
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) CertPath(java.security.cert.CertPath) CertificateFactory(java.security.cert.CertificateFactory)

Example 80 with CertPath

use of java.security.cert.CertPath in project j2objc by google.

the class SignatureFileVerifier method getTimestamp.

/*
     * Examines a signature timestamp token to generate a timestamp object.
     *
     * Examines the signer's unsigned attributes for a
     * <tt>signatureTimestampToken</tt> attribute. If present,
     * then it is parsed to extract the date and time at which the
     * timestamp was generated.
     *
     * @param info A signer information element of a PKCS 7 block.
     *
     * @return A timestamp token or null if none is present.
     * @throws IOException if an error is encountered while parsing the
     *         PKCS7 data.
     * @throws NoSuchAlgorithmException if an error is encountered while
     *         verifying the PKCS7 object.
     * @throws SignatureException if an error is encountered while
     *         verifying the PKCS7 object.
     * @throws CertificateException if an error is encountered while generating
     *         the TSA's certpath.
     */
private Timestamp getTimestamp(SignerInfo info) throws IOException, NoSuchAlgorithmException, SignatureException, CertificateException {
    Timestamp timestamp = null;
    // Extract the signer's unsigned attributes
    PKCS9Attributes unsignedAttrs = info.getUnauthenticatedAttributes();
    if (unsignedAttrs != null) {
        PKCS9Attribute timestampTokenAttr = unsignedAttrs.getAttribute("signatureTimestampToken");
        if (timestampTokenAttr != null) {
            PKCS7 timestampToken = new PKCS7((byte[]) timestampTokenAttr.getValue());
            // Extract the content (an encoded timestamp token info)
            byte[] encodedTimestampTokenInfo = timestampToken.getContentInfo().getData();
            // Extract the signer (the Timestamping Authority)
            // while verifying the content
            SignerInfo[] tsa = timestampToken.verify(encodedTimestampTokenInfo);
            // Expect only one signer
            ArrayList<X509Certificate> chain = tsa[0].getCertificateChain(timestampToken);
            CertPath tsaChain = certificateFactory.generateCertPath(chain);
            // Create a timestamp token info object
            TimestampToken timestampTokenInfo = new TimestampToken(encodedTimestampTokenInfo);
            // Check that the signature timestamp applies to this signature
            verifyTimestamp(timestampTokenInfo, info.getEncryptedDigest());
            // Create a timestamp object
            timestamp = new Timestamp(timestampTokenInfo.getDate(), tsaChain);
        }
    }
    return timestamp;
}
Also used : CertPath(java.security.cert.CertPath) X509Certificate(java.security.cert.X509Certificate) TimestampToken(sun.security.timestamp.TimestampToken)

Aggregations

CertPath (java.security.cert.CertPath)86 X509Certificate (java.security.cert.X509Certificate)36 CertificateFactory (java.security.cert.CertificateFactory)29 Certificate (java.security.cert.Certificate)19 CertPathValidator (java.security.cert.CertPathValidator)18 CertPathValidatorException (java.security.cert.CertPathValidatorException)18 MyCertPath (org.apache.harmony.security.tests.support.cert.MyCertPath)17 CertificateException (java.security.cert.CertificateException)15 ArrayList (java.util.ArrayList)15 PKIXParameters (java.security.cert.PKIXParameters)14 MyFailingCertPath (org.apache.harmony.security.tests.support.cert.MyFailingCertPath)14 TrustAnchor (java.security.cert.TrustAnchor)12 HashSet (java.util.HashSet)12 ByteArrayInputStream (java.io.ByteArrayInputStream)11 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)11 CertPathBuilderResult (java.security.cert.CertPathBuilderResult)11 PKIXBuilderParameters (java.security.cert.PKIXBuilderParameters)10 PKIXCertPathValidatorResult (java.security.cert.PKIXCertPathValidatorResult)10 X509CertSelector (java.security.cert.X509CertSelector)10 CertPathBuilder (java.security.cert.CertPathBuilder)9