Example usage for java.security.cert X509Certificate getNotBefore

List of usage examples for java.security.cert X509Certificate getNotBefore

Introduction

In this page you can find the example usage for java.security.cert X509Certificate getNotBefore.

Prototype

public abstract Date getNotBefore();

Source Link

Document

Gets the notBefore date from the validity period of the certificate.

Usage

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    FileInputStream fr = new FileInputStream("sdo.cer");
    CertificateFactory cf = CertificateFactory.getInstance("X509");
    X509Certificate c = (X509Certificate) cf.generateCertificate(fr);
    System.out.println("\tCertificate for: " + c.getSubjectDN());
    System.out.println("\tCertificate issued by: " + c.getIssuerDN());
    System.out.println("\tThe certificate is valid from " + c.getNotBefore() + " to " + c.getNotAfter());
    System.out.println("\tCertificate SN# " + c.getSerialNumber());
    System.out.println("\tGenerated with " + c.getSigAlgName());
}

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    CertificateFactory cf = CertificateFactory.getInstance("X.509");
    FileInputStream in = new FileInputStream(args[0]);
    java.security.cert.Certificate c = cf.generateCertificate(in);
    in.close();/*from   ww  w . j ava 2s. c o m*/

    X509Certificate t = (X509Certificate) c;
    System.out.println(t.getVersion());
    System.out.println(t.getSerialNumber().toString(16));
    System.out.println(t.getSubjectDN());
    System.out.println(t.getIssuerDN());
    System.out.println(t.getNotBefore());
    System.out.println(t.getNotAfter());
    System.out.println(t.getSigAlgName());
    byte[] sig = t.getSignature();
    System.out.println(new BigInteger(sig).toString(16));
    PublicKey pk = t.getPublicKey();
    byte[] pkenc = pk.getEncoded();
    for (int i = 0; i < pkenc.length; i++) {
        System.out.print(pkenc[i] + ",");
    }
}

From source file:org.rhq.enterprise.server.plugins.rhnhosted.RHNSSLCertReader.java

static public void main(String[] args) {
    if (args.length < 1) {
        System.out.println("Please re-run and specify an argument for the location of a RHN SSL Cert.");
        System.exit(0);//from w  ww  .j  a  va 2  s. c o m
    }
    String path = args[0];
    System.out.println("path is " + path);
    String rawCert = null;
    try {
        rawCert = FileUtils.readFileToString(new File(path));
    } catch (IOException e) {
        e.printStackTrace();
        System.exit(0);
    }
    List<String> certs = RHNSSLCertReader.getCertText(rawCert);
    for (String cert : certs) {
        System.out.println("Parsed SSL Certificate: \n" + cert);
    }

    List<X509Certificate> sslCerts = new ArrayList<X509Certificate>();
    try {
        sslCerts = getSSLCertificates(rawCert);
    } catch (CertificateException e) {
        e.printStackTrace();
        System.exit(0);
    }
    for (X509Certificate c : sslCerts) {
        System.out.println("Cert SigAlgName = " + c.getSigAlgName());
        System.out.println("Cert IssuerDN = " + c.getIssuerDN());
        System.out.println("Cert NotAfter = " + c.getNotAfter());
        System.out.println("Cert NotBefore = " + c.getNotBefore());
        System.out.println("Cert PublicKey = " + c.getPublicKey());
        System.out.println("Cert SubjectDN = " + c.getSubjectDN());
    }
}

From source file:net.ripe.rpki.commons.crypto.x509cert.X509CertificateUtil.java

public static ValidityPeriod getValidityPeriod(X509Certificate certificate) {
    return new ValidityPeriod(certificate.getNotBefore(), certificate.getNotAfter());
}

From source file:org.openhealthtools.openatna.net.ConnectionCertificateHandler.java

/**
 * For debuging only.  Prints out keystore certificate chain.
 *
 * @param keystore Keystore to print out.
 * @throws KeyStoreException If the keystore is broken.
 *///www . j ava 2 s  .  co  m
public static void printTrustCerts(KeyStore keystore) throws KeyStoreException {
    Enumeration<String> aliases = keystore.aliases();
    while (aliases.hasMoreElements()) {
        String alias = aliases.nextElement();
        String message = "Trusted certificate '" + alias + "':";
        Certificate trustedcert = keystore.getCertificate(alias);
        if (trustedcert != null && trustedcert instanceof X509Certificate) {
            X509Certificate cert = (X509Certificate) trustedcert;
            message += "\n  Subject DN: " + cert.getSubjectDN();
            message += "\n  Signature Algorithm: " + cert.getSigAlgName();
            message += "\n  Valid from: " + cert.getNotBefore();
            message += "\n  Valid until: " + cert.getNotAfter();
            message += "\n  Issuer: " + cert.getIssuerDN();
        }
        log.info(message);
    }
}

From source file:org.glite.slcs.httpclient.ssl.ExtendedX509TrustManager.java

static private void dumpCertificate(X509Certificate cert) {
    LOG.debug("Certificate:");
    LOG.debug("  Subject: " + cert.getSubjectDN());
    LOG.debug("  Issuer: " + cert.getIssuerDN());
    LOG.debug("  Valid from: " + cert.getNotBefore());
    LOG.debug("  Valid until: " + cert.getNotAfter());
    LOG.debug("  Fingerprint: " + getCertificateFingerprint(cert, "MD5"));
}

From source file:org.openhealthtools.openatna.net.ConnectionCertificateHandler.java

/**
 * For debuging only.  Prints out keystore certificate chain.
 *
 * @param keystore Keystore to print out.
 * @throws KeyStoreException If the keystore is broken.
 *///  www  .  j  a v  a2 s . c o  m
public static void printKeyCertificates(KeyStore keystore) throws KeyStoreException {
    Enumeration<String> aliases = keystore.aliases();
    while (aliases.hasMoreElements()) {
        String alias = aliases.nextElement();
        Certificate[] certs = keystore.getCertificateChain(alias);
        if (certs != null) {
            String message = "Certificate chain '" + alias + "':";
            int i = 1;
            for (Certificate cert : certs) {
                if (cert instanceof X509Certificate) {
                    X509Certificate Xcert = (X509Certificate) cert;
                    message += "\n Certificate " + i++ + ":";
                    message += "\n  Subject DN: " + Xcert.getSubjectDN();
                    message += "\n  Signature Algorithm: " + Xcert.getSigAlgName();
                    message += "\n  Valid from: " + Xcert.getNotBefore();
                    message += "\n  Valid until: " + Xcert.getNotAfter();
                    message += "\n  Issuer: " + Xcert.getIssuerDN();
                }
            }
            log.info(message);
        }
    }
}

From source file:be.fedict.eid.applet.service.JSONServlet.java

private static JSONObject createCertJSONObject(X509Certificate certificate, SimpleDateFormat simpleDateFormat)
        throws CertificateEncodingException, IOException {
    JSONObject certJSONObject = new JSONObject();
    certJSONObject.put("subject", certificate.getSubjectX500Principal().toString());
    certJSONObject.put("issuer", certificate.getIssuerX500Principal().toString());
    certJSONObject.put("serialNumber", certificate.getSerialNumber().toString());
    certJSONObject.put("notBefore", certificate.getNotBefore().toString());
    certJSONObject.put("notAfter", certificate.getNotAfter().toString());
    certJSONObject.put("signatureAlgo", certificate.getSigAlgName());
    certJSONObject.put("thumbprint", DigestUtils.shaHex(certificate.getEncoded()));
    certJSONObject.put("details", certificate.toString());
    certJSONObject.put("pem", toPem(certificate));

    return certJSONObject;
}

From source file:nl.nn.adapterframework.http.AuthSSLProtocolSocketFactoryBase.java

protected static KeyStore createKeyStore(final URL url, final String password, String keyStoreType,
        String prefix) throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException {
    if (url == null) {
        throw new IllegalArgumentException("Keystore url for " + prefix + " may not be null");
    }//from   ww w  . jav a2  s  . c om
    log.info("Initializing keystore for " + prefix + " from " + url.toString());
    KeyStore keystore = KeyStore.getInstance(keyStoreType);
    keystore.load(url.openStream(), password != null ? password.toCharArray() : null);
    if (log.isInfoEnabled()) {
        Enumeration aliases = keystore.aliases();
        while (aliases.hasMoreElements()) {
            String alias = (String) aliases.nextElement();
            log.info(prefix + " '" + alias + "':");
            Certificate trustedcert = keystore.getCertificate(alias);
            if (trustedcert != null && trustedcert instanceof X509Certificate) {
                X509Certificate cert = (X509Certificate) trustedcert;
                log.info("  Subject DN: " + cert.getSubjectDN());
                log.info("  Signature Algorithm: " + cert.getSigAlgName());
                log.info("  Valid from: " + cert.getNotBefore());
                log.info("  Valid until: " + cert.getNotAfter());
                log.info("  Issuer: " + cert.getIssuerDN());
            }
        }
    }
    return keystore;
}

From source file:com.jrummyapps.busybox.utils.ZipSigner.java

/**
 * Tool to sign JAR files (including APKs and OTA updates) in a way compatible with the mincrypt verifier, using
 * SHA1 and RSA keys.//from  ww w .ja  va2  s.c  o  m
 *
 * @param unsignedZip
 *     The path to the APK, ZIP, JAR to sign
 * @param destination
 *     The output file
 * @return true if successfully signed the file
 */
public static boolean signZip(File unsignedZip, File destination) {
    final AssetManager am = App.getContext().getAssets();
    JarArchiveOutputStream outputJar = null;
    JarFile inputJar = null;

    try {
        X509Certificate publicKey = readPublicKey(am.open(PUBLIC_KEY));
        PrivateKey privateKey = readPrivateKey(am.open(PRIVATE_KEY));

        // Assume the certificate is valid for at least an hour.
        long timestamp = publicKey.getNotBefore().getTime() + 3600L * 1000;

        inputJar = new JarFile(unsignedZip, false); // Don't verify.
        FileOutputStream stream = new FileOutputStream(destination);
        outputJar = new JarArchiveOutputStream(stream);
        outputJar.setLevel(9);

        // MANIFEST.MF
        Manifest manifest = addDigestsToManifest(inputJar);
        JarArchiveEntry je = new JarArchiveEntry(JarFile.MANIFEST_NAME);
        je.setTime(timestamp);
        outputJar.putArchiveEntry(je);
        manifest.write(outputJar);

        ZipSignature signature1 = new ZipSignature();
        signature1.initSign(privateKey);

        ByteArrayOutputStream out = new ByteArrayOutputStream();
        writeSignatureFile(manifest, out);

        // CERT.SF
        Signature signature = Signature.getInstance("SHA1withRSA");
        signature.initSign(privateKey);
        je = new JarArchiveEntry(CERT_SF_NAME);
        je.setTime(timestamp);
        outputJar.putArchiveEntry(je);
        byte[] sfBytes = writeSignatureFile(manifest, new SignatureOutputStream(outputJar, signature));

        signature1.update(sfBytes);
        byte[] signatureBytes = signature1.sign();

        // CERT.RSA
        je = new JarArchiveEntry(CERT_RSA_NAME);
        je.setTime(timestamp);
        outputJar.putArchiveEntry(je);

        outputJar.write(readContentAsBytes(am.open(TEST_KEY)));
        outputJar.write(signatureBytes);

        copyFiles(manifest, inputJar, outputJar, timestamp);
    } catch (Exception e) {
        Crashlytics.logException(e);
        return false;
    } finally {
        IoUtils.closeQuietly(inputJar);
        IoUtils.closeQuietly(outputJar);
    }
    return true;
}