Example usage for java.security.cert X509Certificate getSerialNumber

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

Introduction

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

Prototype

public abstract BigInteger getSerialNumber();

Source Link

Document

Gets the serialNumber value from the certificate.

Usage

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();//  w w w  .jav  a  2  s  . co  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: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:org.apache.xml.security.keys.storage.implementations.CertsInFilesystemDirectoryResolver.java

/**
 * Method main/*from   www  .j  av  a  2 s  .  c o  m*/
 *
 * @param unused
 * @throws Exception
 */
public static void main(String unused[]) throws Exception {

    CertsInFilesystemDirectoryResolver krs = new CertsInFilesystemDirectoryResolver(
            "data/ie/baltimore/merlin-examples/merlin-xmldsig-eighteen/certs");

    for (Iterator<Certificate> i = krs.getIterator(); i.hasNext();) {
        X509Certificate cert = (X509Certificate) i.next();
        byte[] ski = org.apache.xml.security.keys.content.x509.XMLX509SKI.getSKIBytesFromCert(cert);

        System.out.println();
        System.out.println("Base64(SKI())=                 \"" + Base64.encode(ski) + "\"");
        System.out.println("cert.getSerialNumber()=        \"" + cert.getSerialNumber().toString() + "\"");
        System.out.println("cert.getSubjectDN().getName()= \"" + cert.getSubjectDN().getName() + "\"");
        System.out.println("cert.getIssuerDN().getName()=  \"" + cert.getIssuerDN().getName() + "\"");
    }
}

From source file:wptools.cmds.DumpCerts.java

private static void dumpCert(X509Certificate cert) {
    System.out.println("Serial No.: " + formatFing(cert.getSerialNumber().toByteArray()));
    try {/*from w  w w  .  j  a  v a  2 s . c om*/
        for (String ftype : FTYPES) {
            MessageDigest md = MessageDigest.getInstance(ftype);
            md.reset();
            System.out.format("%s: %s%n", ftype, formatFing(md.digest(cert.getEncoded())));
        }
    } catch (NoSuchAlgorithmException | CertificateException e) {
        Misc.die(e.getMessage());
    }
    System.out.println("Issued To: " + cert.getSubjectX500Principal());
    System.out.println("Issued By: " + cert.getIssuerX500Principal());
    System.out.format("Valid: from %tFT%<tT%<tz to %tFT%<tT%<tz%n%n", cert.getNotBefore(), cert.getNotAfter());
}

From source file:org.jasig.cas.adaptors.x509.util.CertUtils.java

/**
 * Creates a unique and human-readable representation of the given certificate.
 *
 * @param cert Certificate.//w  w w . ja  va 2 s. co  m
 *
 * @return String representation of a certificate that includes the subject and serial number.
 */
public static String toString(final X509Certificate cert) {
    return String.format("%s, SerialNumber=%s", cert.getSubjectDN(), cert.getSerialNumber());
}

From source file:com.nesscomputing.tinyhttp.ssl.HttpsTrustManagerFactory.java

@Nonnull
private static X509TrustManager trustManagerFromKeystore(final KeyStore keystore)
        throws GeneralSecurityException {
    final TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance("PKIX", "SunJSSE");
    trustManagerFactory.init(keystore);/*from  w w w.j a v  a  2  s .c  o m*/

    final TrustManager[] tms = trustManagerFactory.getTrustManagers();

    for (final TrustManager tm : tms) {
        if (tm instanceof X509TrustManager) {
            final X509TrustManager manager = X509TrustManager.class.cast(tm);
            final X509Certificate[] acceptedIssuers = manager.getAcceptedIssuers();
            LOG.debug("Found TrustManager with %d authorities.", acceptedIssuers.length);
            for (int i = 0; i < acceptedIssuers.length; i++) {
                X509Certificate issuer = acceptedIssuers[i];
                LOG.trace("Issuer #%d, subject DN=<%s>, serial=<%s>", i, issuer.getSubjectDN(),
                        issuer.getSerialNumber());
            }

            return manager;
        }
    }
    throw new IllegalStateException("Could not locate X509TrustManager!");
}

From source file:com.nesscomputing.httpclient.internal.HttpClientTrustManagerFactory.java

@Nonnull
private static X509TrustManager trustManagerFromKeystore(final KeyStore keystore)
        throws GeneralSecurityException {
    final TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance("PKIX", "SunJSSE");
    trustManagerFactory.init(keystore);//from ww  w .j ava 2s. c om

    final TrustManager[] tms = trustManagerFactory.getTrustManagers();

    for (TrustManager tm : tms) {
        if (tm instanceof X509TrustManager) {
            final X509TrustManager manager = (X509TrustManager) tm;
            X509Certificate[] acceptedIssuers = manager.getAcceptedIssuers();
            LOG.debug("Found TrustManager with %d authorities.", acceptedIssuers.length);
            for (int i = 0; i < acceptedIssuers.length; i++) {
                X509Certificate issuer = acceptedIssuers[i];

                LOG.trace("Issuer #%d, subject DN=<%s>, serial=<%s>", i, issuer.getSubjectDN(),
                        issuer.getSerialNumber());
            }

            return manager;
        }
    }
    throw new IllegalStateException("Could not find an X509TrustManager");
}

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

public static BigInteger getSerialNumber(X509Certificate certificate) {
    return certificate.getSerialNumber();
}

From source file:eu.europa.esig.dss.DSSRevocationUtils.java

/**
 * Returns the {@code CertificateID} for the given certificate and its
 * issuer's certificate./*from   w  w  w . j av a 2 s  .  c o  m*/
 *
 * @param cert
 *            {@code X509Certificate} for which the id is created
 * @param issuerCert
 *            {@code X509Certificate} issuer certificate of the {@code cert}
 * @return {@code CertificateID}
 * @throws eu.europa.esig.dss.DSSException
 */
public static CertificateID getOCSPCertificateID(final X509Certificate cert, final X509Certificate issuerCert)
        throws DSSException {
    try {
        final BigInteger serialNumber = cert.getSerialNumber();
        final DigestCalculator digestCalculator = getSHA1DigestCalculator();
        final X509CertificateHolder x509CertificateHolder = DSSUtils
                .getX509CertificateHolder(new CertificateToken(issuerCert));
        final CertificateID certificateID = new CertificateID(digestCalculator, x509CertificateHolder,
                serialNumber);
        return certificateID;
    } catch (OCSPException e) {
        throw new DSSException(e);
    }
}

From source file:com.toft.widgets.login.LoginAction.java

/**
 * ????// w  ww  .j a  va  2  s. com
 * 
 * @param request
 * @return string:???
 */
private static String getPublicKeySerialNumber(HttpServletRequest request) {
    X509Certificate[] chain = (X509Certificate[]) request.getAttribute("javax.servlet.request.X509Certificate");
    if (chain != null) {
        X509Certificate cert = chain[0];
        String certSerialNumber = cert.getSerialNumber().toString(16);
        //         certSerialNumber = org.apache.commons.lang.StringUtils.leftPad(certSerialNumber, (certSerialNumber.length()+1)/2*2, '0');
        return certSerialNumber;
    }
    return null;
}