Search in sources :

Example 1 with Timestamp

use of java.security.Timestamp in project jdk8u_jdk by JetBrains.

the class Serialize method main.

public static void main(String[] args) throws Exception {
    // Create a certpath consisting of one certificate
    File f = new File(System.getProperty("test.src", "."), "cert_file");
    FileInputStream fis = new FileInputStream(f);
    CertificateFactory cf = CertificateFactory.getInstance("X.509");
    Certificate c = cf.generateCertificate(fis);
    fis.close();
    CertPath cp = cf.generateCertPath(Collections.singletonList(c));
    // Create a code signer
    CodeSigner cs = new CodeSigner(cp, new Timestamp(new Date(), cp));
    // Serialize the code signer
    ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
    ObjectOutputStream out = new ObjectOutputStream(byteOut);
    out.writeObject(cs);
    out.close();
    // Deserialize the code signer
    byte[] data = byteOut.toByteArray();
    CodeSigner cs2 = (CodeSigner) new ObjectInputStream(new ByteArrayInputStream(data)).readObject();
    // Test for equality
    if (!cs.equals(cs2) || cs.hashCode() != cs2.hashCode()) {
        throw new Exception("CodeSigner serialization test FAILED");
    }
}
Also used : Timestamp(java.security.Timestamp) Date(java.util.Date) CodeSigner(java.security.CodeSigner)

Example 2 with Timestamp

use of java.security.Timestamp in project robovm by robovm.

the class TimestampTest method testEqualsObject.

/*
     * Class under test for boolean equals(Object)
     */
public void testEqualsObject() {
    Timestamp one = new Timestamp(now, cpath);
    Timestamp two = new Timestamp(now, cpath);
    assertTrue(one.equals(one));
    assertTrue(one.equals(two));
    assertTrue(two.equals(one));
    assertFalse(one.equals(null));
    assertFalse(one.equals(new Object()));
    Timestamp two1 = new Timestamp(new Date(9999), cpath);
    assertFalse(one.equals(two1));
    assertTrue(two1.equals(two1));
}
Also used : Timestamp(java.security.Timestamp) Date(java.util.Date)

Example 3 with Timestamp

use of java.security.Timestamp in project robovm by robovm.

the class TimestampTest method testHashCode.

/*
     * Class under test for String hashCode()
     */
public void testHashCode() {
    Timestamp one = new Timestamp(now, cpath);
    Timestamp two = new Timestamp(now, cpath);
    Timestamp three = new Timestamp(now, new MyCertPath(new byte[] { 10, 20, 30 }));
    Timestamp four = null;
    assertTrue(one.hashCode() == two.hashCode());
    assertTrue(one.hashCode() != three.hashCode());
    assertTrue(two.hashCode() != three.hashCode());
    try {
        four.hashCode();
        fail("NullPointerException expected");
    } catch (NullPointerException e) {
    // expected
    }
}
Also used : MyCertPath(org.apache.harmony.security.tests.support.cert.MyCertPath) Timestamp(java.security.Timestamp)

Example 4 with Timestamp

use of java.security.Timestamp in project jdk8u_jdk by JetBrains.

the class SignerInfo method getTimestamp.

/*
     * Extracts a timestamp from a PKCS7 SignerInfo.
     *
     * Examines the signer's unsigned attributes for a
     * {@code signatureTimestampToken} 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.
     */
public Timestamp getTimestamp() throws IOException, NoSuchAlgorithmException, SignatureException, CertificateException {
    if (timestamp != null || !hasTimestamp)
        return timestamp;
    PKCS7 tsToken = getTsToken();
    if (tsToken == null) {
        hasTimestamp = false;
        return null;
    }
    // Extract the content (an encoded timestamp token info)
    byte[] encTsTokenInfo = tsToken.getContentInfo().getData();
    // Extract the signer (the Timestamping Authority)
    // while verifying the content
    SignerInfo[] tsa = tsToken.verify(encTsTokenInfo);
    // Expect only one signer
    ArrayList<X509Certificate> chain = tsa[0].getCertificateChain(tsToken);
    CertificateFactory cf = CertificateFactory.getInstance("X.509");
    CertPath tsaChain = cf.generateCertPath(chain);
    // Create a timestamp token info object
    TimestampToken tsTokenInfo = new TimestampToken(encTsTokenInfo);
    // Check that the signature timestamp applies to this signature
    verifyTimestamp(tsTokenInfo);
    // Create a timestamp object
    timestamp = new Timestamp(tsTokenInfo.getDate(), tsaChain);
    return timestamp;
}
Also used : CertPath(java.security.cert.CertPath) CertificateFactory(java.security.cert.CertificateFactory) Timestamp(java.security.Timestamp) X509Certificate(java.security.cert.X509Certificate) TimestampToken(sun.security.timestamp.TimestampToken)

Example 5 with Timestamp

use of java.security.Timestamp in project jdk8u_jdk by JetBrains.

the class Pair method doPrintCert.

private void doPrintCert(final PrintStream out) throws Exception {
    if (jarfile != null) {
        JarFile jf = new JarFile(jarfile, true);
        Enumeration<JarEntry> entries = jf.entries();
        Set<CodeSigner> ss = new HashSet<>();
        byte[] buffer = new byte[8192];
        int pos = 0;
        while (entries.hasMoreElements()) {
            JarEntry je = entries.nextElement();
            try (InputStream is = jf.getInputStream(je)) {
                while (is.read(buffer) != -1) {
                // we just read. this will throw a SecurityException
                // if a signature/digest check fails. This also
                // populate the signers
                }
            }
            CodeSigner[] signers = je.getCodeSigners();
            if (signers != null) {
                for (CodeSigner signer : signers) {
                    if (!ss.contains(signer)) {
                        ss.add(signer);
                        out.printf(rb.getString("Signer.d."), ++pos);
                        out.println();
                        out.println();
                        out.println(rb.getString("Signature."));
                        out.println();
                        for (Certificate cert : signer.getSignerCertPath().getCertificates()) {
                            X509Certificate x = (X509Certificate) cert;
                            if (rfc) {
                                out.println(rb.getString("Certificate.owner.") + x.getSubjectDN() + "\n");
                                dumpCert(x, out);
                            } else {
                                printX509Cert(x, out);
                            }
                            out.println();
                        }
                        Timestamp ts = signer.getTimestamp();
                        if (ts != null) {
                            out.println(rb.getString("Timestamp."));
                            out.println();
                            for (Certificate cert : ts.getSignerCertPath().getCertificates()) {
                                X509Certificate x = (X509Certificate) cert;
                                if (rfc) {
                                    out.println(rb.getString("Certificate.owner.") + x.getSubjectDN() + "\n");
                                    dumpCert(x, out);
                                } else {
                                    printX509Cert(x, out);
                                }
                                out.println();
                            }
                        }
                    }
                }
            }
        }
        jf.close();
        if (ss.isEmpty()) {
            out.println(rb.getString("Not.a.signed.jar.file"));
        }
    } else if (sslserver != null) {
        // Lazily load SSLCertStoreHelper if present
        CertStoreHelper helper = CertStoreHelper.getInstance("SSLServer");
        CertStore cs = helper.getCertStore(new URI("https://" + sslserver));
        Collection<? extends Certificate> chain;
        try {
            chain = cs.getCertificates(null);
            if (chain.isEmpty()) {
                // even if the URL connection is successful.
                throw new Exception(rb.getString("No.certificate.from.the.SSL.server"));
            }
        } catch (CertStoreException cse) {
            if (cse.getCause() instanceof IOException) {
                throw new Exception(rb.getString("No.certificate.from.the.SSL.server"), cse.getCause());
            } else {
                throw cse;
            }
        }
        int i = 0;
        for (Certificate cert : chain) {
            try {
                if (rfc) {
                    dumpCert(cert, out);
                } else {
                    out.println("Certificate #" + i++);
                    out.println("====================================");
                    printX509Cert((X509Certificate) cert, out);
                    out.println();
                }
            } catch (Exception e) {
                if (debug) {
                    e.printStackTrace();
                }
            }
        }
    } else {
        if (filename != null) {
            try (FileInputStream inStream = new FileInputStream(filename)) {
                printCertFromStream(inStream, out);
            }
        } else {
            printCertFromStream(System.in, out);
        }
    }
}
Also used : CertStoreException(java.security.cert.CertStoreException) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry) Timestamp(java.security.Timestamp) URI(java.net.URI) X509Certificate(java.security.cert.X509Certificate) KeyStoreException(java.security.KeyStoreException) UnrecoverableEntryException(java.security.UnrecoverableEntryException) CertStoreException(java.security.cert.CertStoreException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) UnrecoverableKeyException(java.security.UnrecoverableKeyException) CertificateException(java.security.cert.CertificateException) CertStoreHelper(sun.security.provider.certpath.CertStoreHelper) CertStore(java.security.cert.CertStore) CodeSigner(java.security.CodeSigner) X509Certificate(java.security.cert.X509Certificate) Certificate(java.security.cert.Certificate)

Aggregations

Timestamp (java.security.Timestamp)10 X509Certificate (java.security.cert.X509Certificate)5 CodeSigner (java.security.CodeSigner)3 CertificateException (java.security.cert.CertificateException)3 URI (java.net.URI)2 KeyStoreException (java.security.KeyStoreException)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 UnrecoverableEntryException (java.security.UnrecoverableEntryException)2 UnrecoverableKeyException (java.security.UnrecoverableKeyException)2 CertPath (java.security.cert.CertPath)2 CertStore (java.security.cert.CertStore)2 CertStoreException (java.security.cert.CertStoreException)2 Certificate (java.security.cert.Certificate)2 CertificateFactory (java.security.cert.CertificateFactory)2 Date (java.util.Date)2 JarEntry (java.util.jar.JarEntry)2 JarFile (java.util.jar.JarFile)2 TimestampToken (sun.security.timestamp.TimestampToken)2 IOException (java.io.IOException)1 InvalidKeyException (java.security.InvalidKeyException)1