All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.bouncycastle.cert.jcajce.JcaX500NameUtil Maven / Gradle / Ivy

Go to download

The Bouncy Castle Java APIs for CMS, PKCS, EAC, TSP, CMP, CRMF, OCSP, and certificate generation. The APIs are designed primarily to be used in conjunction with the BC FIPS provider. The APIs may also be used with other providers although if being used in a FIPS context it is the responsibility of the user to ensure that any other providers used are FIPS certified.

There is a newer version: 2.0.7
Show newest version
package org.bouncycastle.cert.jcajce;

import java.security.cert.X509Certificate;

import javax.security.auth.x500.X500Principal;

import org.bouncycastle.asn1.x500.X500Name;
import org.bouncycastle.asn1.x500.X500NameStyle;

public class JcaX500NameUtil
{
    public static X500Name getIssuer(X509Certificate certificate)
    {
//        if (certificate instanceof BCX509Certificate)
//        {
//            return notNull(((BCX509Certificate)certificate).getIssuerX500Name());
//        }
        return getX500Name(certificate.getIssuerX500Principal());
    }

    public static X500Name getIssuer(X500NameStyle style, X509Certificate certificate)
    {
//        if (certificate instanceof BCX509Certificate)
//        {
//            return X500Name.getInstance(style, notNull(((BCX509Certificate)certificate).getIssuerX500Name()));
//        }
        return getX500Name(style, certificate.getIssuerX500Principal());
    }

    public static X500Name getSubject(X509Certificate certificate)
    {
//        if (certificate instanceof BCX509Certificate)
//        {
//            return notNull(((BCX509Certificate)certificate).getSubjectX500Name());
//        }
        return getX500Name(certificate.getSubjectX500Principal());
    }

    public static X500Name getSubject(X500NameStyle style, X509Certificate certificate)
    {
//        if (certificate instanceof BCX509Certificate)
//        {
//            return X500Name.getInstance(style, notNull(((BCX509Certificate)certificate).getSubjectX500Name()));
//        }
        return getX500Name(style, certificate.getSubjectX500Principal());
    }

    public static X500Name getX500Name(X500Principal principal)
    {
        return X500Name.getInstance(getEncoded(principal));
    }

    public static X500Name getX500Name(X500NameStyle style, X500Principal principal)
    {
        return X500Name.getInstance(style, getEncoded(principal));
    }

    private static X500Name notNull(X500Name name)
    {
        if (null == name)
        {
            throw new IllegalStateException();
        }
        return name;
    }

    private static X500Principal notNull(X500Principal principal)
    {
        if (null == principal)
        {
            throw new IllegalStateException();
        }
        return principal;
    }

    private static byte[] getEncoded(X500Principal principal)
    {
        return notNull(principal).getEncoded();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy