Example usage for org.bouncycastle.asn1 DERUTCTime getDate

List of usage examples for org.bouncycastle.asn1 DERUTCTime getDate

Introduction

In this page you can find the example usage for org.bouncycastle.asn1 DERUTCTime getDate.

Prototype

public Date getDate() throws ParseException 

Source Link

Document

Return the time as a date based on whatever a 2 digit year will return.

Usage

From source file:es.gob.afirma.applet.CMSInformation.java

License:Open Source License

/**
 * Obtiene los atributos obligatorios de una firma.
 *
 * @param attributes    Grupo de atributos opcionales
 * @param binarySignType   Identifica el tipo de firma binaria (CMS o CADES)
 * @return              lista de atributos concatenados.
 *///from  ww w  . j ava 2  s  .c  o m
private static String getsignedAttributes(final ASN1Set attributes, final int binarySignType) {
    String attributos = ""; //$NON-NLS-1$

    final Enumeration<?> e = attributes.getObjects();

    while (e.hasMoreElements()) {
        final ASN1Sequence a = (ASN1Sequence) e.nextElement();
        final ASN1ObjectIdentifier derIden = (ASN1ObjectIdentifier) a.getObjectAt(0);
        // tipo de contenido de la firma.
        if (derIden.equals(CMSAttributes.contentType)) {
            attributos = attributos + TB + TB + AppletMessages.getString("CMSInformation.22") + SP //$NON-NLS-1$
                    + a.getObjectAt(1) + CR;
        }
        //Message digest de  la firma
        if (derIden.equals(CMSAttributes.messageDigest)) {
            attributos = attributos + TB + TB + AppletMessages.getString("CMSInformation.43") + CR; //$NON-NLS-1$
        }
        //la fecha de firma. obtenemos y casteamos a algo legible.
        if (derIden.equals(CMSAttributes.signingTime)) {
            final ASN1Set time = (ASN1Set) a.getObjectAt(1);
            final DERUTCTime d = (DERUTCTime) time.getObjectAt(0);
            Date date = null;
            try {
                date = d.getDate();
            } catch (final ParseException ex) {
                Logger.getLogger("es.gob.afirma").warning("No es posible convertir la fecha"); //$NON-NLS-1$ //$NON-NLS-2$
            }
            final SimpleDateFormat formatter = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss"); //$NON-NLS-1$
            final String ds = formatter.format(date);

            attributos = attributos + TB + TB + AppletMessages.getString("CMSInformation.39") + SP + ds + CR; //$NON-NLS-1$
        }
        if (binarySignType == BINARY_SIGN_CADES) {
            //atributo signing certificate v2
            if (derIden.equals(PKCSObjectIdentifiers.id_aa_signingCertificateV2)) {
                attributos = attributos + TB + TB + AppletMessages.getString("CMSInformation.40") + CR; //$NON-NLS-1$
            }
            //Politica de firma.
            if (derIden.equals(PKCSObjectIdentifiers.id_aa_ets_sigPolicyId)) {
                attributos = attributos + TB + TB + AppletMessages.getString("CMSInformation.41") + CR; //$NON-NLS-1$
            }
        }
    }
    return attributos;
}

From source file:es.gob.afirma.applet.CMSInformation.java

License:Open Source License

/**
 * Obtiene los atributos opcionales de una firma cualquiera.
 * En caso de ser EncryptedData, usar el otro metodo, ya que por construccion
 * no es posible utilizar este.//  w  w w.  j a  v  a2 s.  c om
 *
 * @param attributes    Grupo de atributos opcionales
 * @return              lista de atributos concatenados.
 */
private static String getUnSignedAttributes(final Enumeration<?> e) {
    String attributos = ""; //$NON-NLS-1$

    while (e.hasMoreElements()) {
        final ASN1Sequence a = (ASN1Sequence) e.nextElement();
        final ASN1ObjectIdentifier derIden = (ASN1ObjectIdentifier) a.getObjectAt(0);
        // tipo de contenido de la firma.
        if (derIden.equals(CMSAttributes.contentType)) {
            attributos = attributos + TB + AppletMessages.getString("CMSInformation.22") + SP + a.getObjectAt(1) //$NON-NLS-1$
                    + CR;
        }
        //Message digest de  la firma
        if (derIden.equals(CMSAttributes.messageDigest)) {
            attributos = attributos + TB + AppletMessages.getString("CMSInformation.43") + CR; //$NON-NLS-1$
        }
        //la fecha de firma. obtenemos y casteamos a algo legible.
        if (derIden.equals(CMSAttributes.signingTime)) {
            final ASN1Set time = (ASN1Set) a.getObjectAt(1);
            final DERUTCTime d = (DERUTCTime) time.getObjectAt(0);
            Date date = null;
            try {
                date = d.getDate();
            } catch (final ParseException ex) {
                Logger.getLogger("es.gob.afirma").warning("No es posible convertir la fecha"); //$NON-NLS-1$ //$NON-NLS-2$
            }
            final SimpleDateFormat formatter = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss"); //$NON-NLS-1$
            final String ds = formatter.format(date);

            attributos = attributos + TB + AppletMessages.getString("CMSInformation.39") + SP + ds + CR; //$NON-NLS-1$
        }
        //contrafirma de la firma.
        if (derIden.equals(CMSAttributes.counterSignature)) {
            attributos = attributos + TB + AppletMessages.getString("CMSInformation.45") + CR; //$NON-NLS-1$
        }

    }
    return attributos;
}

From source file:net.ripe.rpki.commons.provisioning.cms.ProvisioningCmsObject.java

License:BSD License

/**
 * This is used to check against replay attacks, see <a
 * href="http://tools.ietf.org/html/draft-ietf-sidr-rescerts-provisioning-09#section-3.1.2"
 * >http://tools.ietf.org/html/draft-ietf-sidr-rescerts-provisioning-09#section-3.1.2</a><br >
 *//*w w w .  jav a2 s.c  o  m*/
public DateTime getSigningTime() {
    try {
        CMSSignedData cmsSignedData = new CMSSignedData(encodedContent);
        SignerInformationStore sis = cmsSignedData.getSignerInfos();

        @SuppressWarnings("unchecked")
        Collection<SignerInformation> signers = sis.getSigners();
        for (SignerInformation signerInformation : signers) {
            AttributeTable signedAttributes = signerInformation.getSignedAttributes();
            Attribute signingTime = signedAttributes.get(CMSAttributes.signingTime);

            @SuppressWarnings("unchecked")
            Enumeration<Object> en = signingTime.getAttrValues().getObjects();
            while (en.hasMoreElements()) {
                Object obj = en.nextElement();
                if (obj instanceof DERUTCTime) {
                    DERUTCTime derTime = (DERUTCTime) obj;
                    return new DateTime(derTime.getDate());
                }
            }
        }
        throw new IllegalArgumentException("Malformed encoded cms content");
    } catch (CMSException e) {
        throw new IllegalArgumentException("Malformed encoded cms content", e);
    } catch (ParseException e) {
        throw new IllegalArgumentException("Malformed encoded cms content", e);
    }
}

From source file:net.ripe.rpki.commons.provisioning.cms.ProvisioningCmsObjectBuilderTest.java

License:BSD License

/**
 * http://tools.ietf.org/html/draft-ietf-sidr-rescerts-provisioning-09#section-3.1.1.6.4.3
 *//*  w w  w  .  j av  a 2s.  c o  m*/
@Test
public void shouldCmsObjectHaveSigningTimeSignedAttribute() throws Exception {
    Collection<?> signers = signedDataParser.getSignerInfos().getSigners();
    SignerInformation signer = (SignerInformation) signers.iterator().next();
    AttributeTable attributeTable = signer.getSignedAttributes();
    Attribute signingTimeAttr = attributeTable.get(CMSAttributes.signingTime);

    assertNotNull(signingTimeAttr);
    assertEquals(1, signingTimeAttr.getAttrValues().size());
    DERUTCTime signingTime = (DERUTCTime) signingTimeAttr.getAttrValues().getObjectAt(0);
    assertEquals(this.signingTime, signingTime.getDate().getTime());
}

From source file:org.demoiselle.signer.policy.engine.asn1.icpb.Time.java

License:Open Source License

@Override
public void parse(ASN1Primitive derObject) {
    if (derObject instanceof ASN1GeneralizedTime) {
        ASN1GeneralizedTime derGeneralizedTime = (ASN1GeneralizedTime) derObject;
        try {//  w  ww  .ja  v a  2  s  .  c o m
            this.setTime(derGeneralizedTime.getDate());
        } catch (ParseException ex) {
            this.setTime(null);
        }
    } else if (derObject instanceof DERUTCTime) {
        DERUTCTime derUTCTime = (DERUTCTime) derObject;
        try {
            this.setTime(derUTCTime.getDate());
        } catch (ParseException exception) {
            this.setTime(null);
        }
    }
}

From source file:org.votingsystem.signature.smime.SMIMESignedValidator.java

License:Open Source License

public static Date getSigningTime(SignerInformation signerInformation) {
    AttributeTable signedAttr = signerInformation.getSignedAttributes();
    Attribute signingTime = signedAttr.get(CMSAttributes.signingTime);
    if (signingTime != null) {
        try {//from   w ww.j  a  va 2  s.  com
            Enumeration en = signingTime.getAttrValues().getObjects();
            while (en.hasMoreElements()) {
                Object obj = en.nextElement();
                if (obj instanceof ASN1UTCTime) {
                    ASN1UTCTime asn1Time = (ASN1UTCTime) obj;
                    return asn1Time.getDate();
                } else if (obj instanceof DERUTCTime) {
                    DERUTCTime derTime = (DERUTCTime) obj;
                    return derTime.getDate();
                }
            }
        } catch (Exception ex) {
            log.log(Level.SEVERE, ex.getMessage(), ex);
        }
    }
    return null;
}