Class UtilsCertificateTsl


  • public final class UtilsCertificateTsl
    extends java.lang.Object

    Class that provides methods for managing certificates.

    Project:

    Library for the integration with the services of @Firma, eVisor and TS@.

    19/09/2022.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String X509_TYPE
      Constant that represents a "X.509" Certificate type.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.String canonicalizarIdCertificado​(java.lang.String idCertificado)
      Method that canonicalizes the identifier of a certificate.
      static boolean equals​(java.security.cert.X509Certificate cert1, java.security.cert.X509Certificate cert2)
      Method that indicates whether some other certificate is "equal to" this one (true) or not (false).
      static org.bouncycastle.asn1.x509.Certificate getBouncyCastleCertificate​(byte[] certificate)
      Creates a BouncyCastle X509Certificate given its content.
      static org.bouncycastle.asn1.x509.Certificate getBouncyCastleCertificate​(java.security.cert.Certificate x509cert)
      Creates a BouncyCastle X509Certificate from a java X509Certificate.
      static java.lang.String getCertificateId​(java.security.cert.X509Certificate cert)
      Gets certificate´s identifier (canonicalized subject).
      static java.lang.String getCertificateIssuerId​(java.security.cert.X509Certificate cert)
      Method that obtains the canonicalized identifier of the issuer of a certificate.
      static java.lang.String getCountryOfTheCertificateString​(java.security.cert.X509Certificate x509cert)
      Gets the country specified in the subject name of the input certificate if it is a CA certificate, or from the issuer if it is a end certificate.
      static java.lang.String getIssuerCountryOfTheCertificateString​(java.security.cert.X509Certificate x509cert)
      Gets the country specified in the issuer name of the input certificate.
      static java.lang.String getRDNFirstValueFromX500Name​(org.bouncycastle.asn1.x500.X500Name x500Name, org.bouncycastle.asn1.ASN1ObjectIdentifier rdnAsn1ObjectIdentifier)
      Gets the RDN First ocurrence value with the OID indicated from the input X500Name.
      static java.lang.String getRDNFirstValueFromX500Principal​(javax.security.auth.x500.X500Principal x500Principal, org.bouncycastle.asn1.ASN1ObjectIdentifier rdnAsn1ObjectIdentifier)
      Gets the RDN First ocurrence value with the OID indicated from the input X.500 Principal.
      static java.lang.String getSubjectCountryOfTheCertificateString​(java.security.cert.X509Certificate x509cert)
      Gets the country specified in the subject name of the input certificate.
      static java.security.cert.X509Certificate getX509Certificate​(byte[] certificate)
      Creates a X509Certificate given its content.
      static boolean hasCertKeyPurposeTimeStamping​(java.security.cert.X509Certificate cert)
      Checks if the input certificate has the key purpose for TimeStamping.
      static boolean isCA​(java.security.cert.X509Certificate cert)
      Checks if the input certificate is a CA (it has the flag in the basic constraint).
      static boolean isSelfSigned​(java.security.cert.X509Certificate cert)
      Method that checks whether a certificate is self-signed (true) or not (false).
      static boolean verify​(java.security.cert.X509Certificate certIssuer, java.security.cert.X509Certificate cert)
      Checks if a given certificate is issued by another one.
      static boolean verify​(java.security.PublicKey publicKey, java.security.cert.X509Certificate cert)
      Checks if a given public key corresponds to the private key that signed the input certificate.
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • X509_TYPE

        public static final java.lang.String X509_TYPE
        Constant that represents a "X.509" Certificate type.
        See Also:
        Constant Field Values
    • Method Detail

      • getX509Certificate

        public static java.security.cert.X509Certificate getX509Certificate​(byte[] certificate)
                                                                     throws CommonUtilsException
        Creates a X509Certificate given its content.
        Parameters:
        certificate - Certificate content.
        Returns:
        X509Certificate jce X509Certificate.
        Throws:
        CommonUtilsException - Exception thrown if there is any problem creating the certificate.
      • getRDNFirstValueFromX500Principal

        public static java.lang.String getRDNFirstValueFromX500Principal​(javax.security.auth.x500.X500Principal x500Principal,
                                                                         org.bouncycastle.asn1.ASN1ObjectIdentifier rdnAsn1ObjectIdentifier)
        Gets the RDN First ocurrence value with the OID indicated from the input X.500 Principal.
        Parameters:
        x500Principal - X.500 Principal to analyze.
        rdnAsn1ObjectIdentifier - Object Identifier that represents the RDN to search.
        Returns:
        the RDN First ocurrence value with the OID indicated from the input X.500 Principal. null if some of the input parameters are null or there is some error analyzing the X.500 Principal.
      • getRDNFirstValueFromX500Name

        public static java.lang.String getRDNFirstValueFromX500Name​(org.bouncycastle.asn1.x500.X500Name x500Name,
                                                                    org.bouncycastle.asn1.ASN1ObjectIdentifier rdnAsn1ObjectIdentifier)
        Gets the RDN First ocurrence value with the OID indicated from the input X500Name.
        Parameters:
        x500Name - X.500 Name to analyze.
        rdnAsn1ObjectIdentifier - Object Identifier that represents the RDN to search.
        Returns:
        the RDN First ocurrence value with the OID indicated from the input X500Name. null if some of the input parameters are null or there is some error analyzing the X.500 Name.
      • equals

        public static boolean equals​(java.security.cert.X509Certificate cert1,
                                     java.security.cert.X509Certificate cert2)
                              throws CommonUtilsException
        Method that indicates whether some other certificate is "equal to" this one (true) or not (false).
        Parameters:
        cert1 - Parameter that represents the first certificate to compare.
        cert2 - Parameter that represents the second certificate to compare.
        Returns:
        a boolean that indicates whether some other certificate is "equal to" this one (true) or not (false).
        Throws:
        CommonUtilsException - If there is some error getting de issuer information from the input certificates.
      • getCertificateIssuerId

        public static java.lang.String getCertificateIssuerId​(java.security.cert.X509Certificate cert)
                                                       throws CommonUtilsException
        Method that obtains the canonicalized identifier of the issuer of a certificate.
        Parameters:
        cert - Parameter that represents the certificate.
        Returns:
        the canonicalized identifier of the issuer of the certificate.
        Throws:
        CommonUtilsException - If the method fails.
      • canonicalizarIdCertificado

        public static java.lang.String canonicalizarIdCertificado​(java.lang.String idCertificado)
        Method that canonicalizes the identifier of a certificate.
        Parameters:
        idCertificado - Parameter that represents the identifier of a certificate.
        Returns:
        the canonicalized identifier of the certificate.
      • verify

        public static boolean verify​(java.security.PublicKey publicKey,
                                     java.security.cert.X509Certificate cert)
                              throws CommonUtilsException
        Checks if a given public key corresponds to the private key that signed the input certificate.
        Parameters:
        publicKey - Public key to use to verify the certificate.
        cert - Certificate to check.
        Returns:
        true if the public key verifies the certificate.
        Throws:
        CommonUtilsException - if there is any problem verifying the certificate.
      • verify

        public static boolean verify​(java.security.cert.X509Certificate certIssuer,
                                     java.security.cert.X509Certificate cert)
                              throws CommonUtilsException
        Checks if a given certificate is issued by another one.
        Parameters:
        certIssuer - Issuer to check
        cert - Certificate to be checked.
        Returns:
        true if the input certificate is issued by the specified issuer certificate, otherwise false.
        Throws:
        CommonUtilsException - Exception thrown if there is any problem verifying the certificates.
      • getCertificateId

        public static java.lang.String getCertificateId​(java.security.cert.X509Certificate cert)
                                                 throws CommonUtilsException
        Gets certificate´s identifier (canonicalized subject).
        Parameters:
        cert - Certificate to get the identifier.
        Returns:
        Certificate identifier.
        Throws:
        CommonUtilsException - Exception thrown if there is any problem creating the certificate.
      • isSelfSigned

        public static boolean isSelfSigned​(java.security.cert.X509Certificate cert)
        Method that checks whether a certificate is self-signed (true) or not (false).
        Parameters:
        cert - Parameter that represents the certificate.
        Returns:
        a boolean that indicates whether a certificate is self-signed (true) or not (false).
      • hasCertKeyPurposeTimeStamping

        public static boolean hasCertKeyPurposeTimeStamping​(java.security.cert.X509Certificate cert)
                                                     throws CommonUtilsException
        Checks if the input certificate has the key purpose for TimeStamping.
        Parameters:
        cert - X509v3 certificate to check.
        Returns:
        true if the input certificate has the key purpose for timestamping, otherwise false.
        Throws:
        CommonUtilsException - In case of some error extracting the keyPurpose extension from the certificate.
      • isCA

        public static boolean isCA​(java.security.cert.X509Certificate cert)
        Checks if the input certificate is a CA (it has the flag in the basic constraint).
        Parameters:
        cert - X509v3 certificate that must be checked.
        Returns:
        true if the input certificate has the BasicConstraint extension with the CA flag, otherwise false (including if the input is null).
      • getCountryOfTheCertificateString

        public static java.lang.String getCountryOfTheCertificateString​(java.security.cert.X509Certificate x509cert)
        Gets the country specified in the subject name of the input certificate if it is a CA certificate, or from the issuer if it is a end certificate.
        Parameters:
        x509cert - X509 Certificate to analyze to obtain the country.
        Returns:
        String with the representation of the country of the certificate. in ISO 3166-1. null in case of some error or the country is not defined.
      • getSubjectCountryOfTheCertificateString

        public static java.lang.String getSubjectCountryOfTheCertificateString​(java.security.cert.X509Certificate x509cert)
        Gets the country specified in the subject name of the input certificate.
        Parameters:
        x509cert - X509 Certificate to analyze to obtain the country of its subject name.
        Returns:
        String with the representation of the country of the subject certificate. in ISO 3166-1. null in case of some error or the country is not defined.
      • getIssuerCountryOfTheCertificateString

        public static java.lang.String getIssuerCountryOfTheCertificateString​(java.security.cert.X509Certificate x509cert)
        Gets the country specified in the issuer name of the input certificate.
        Parameters:
        x509cert - X509 Certificate to analyze to obtain the country of its issuer name.
        Returns:
        String with the representation of the country of the certificate issuer. in ISO 3166-1. null in case of some error or the country is not defined.
      • getBouncyCastleCertificate

        public static org.bouncycastle.asn1.x509.Certificate getBouncyCastleCertificate​(java.security.cert.Certificate x509cert)
                                                                                 throws CommonUtilsException
        Creates a BouncyCastle X509Certificate from a java X509Certificate.
        Parameters:
        x509cert - Certificate to transform.
        Returns:
        BouncyCastle X509Certificate.
        Throws:
        CommonUtilsException - Exception thrown if there is any problem creating the certificate.
      • getBouncyCastleCertificate

        public static org.bouncycastle.asn1.x509.Certificate getBouncyCastleCertificate​(byte[] certificate)
                                                                                 throws CommonUtilsException
        Creates a BouncyCastle X509Certificate given its content.
        Parameters:
        certificate - Certificate content.
        Returns:
        BouncyCastle X509Certificate.
        Throws:
        CommonUtilsException - Exception thrown if there is any problem creating the certificate.