Example usage for org.bouncycastle.asn1 DERGeneralizedTime DERGeneralizedTime

List of usage examples for org.bouncycastle.asn1 DERGeneralizedTime DERGeneralizedTime

Introduction

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

Prototype

public DERGeneralizedTime(String time) 

Source Link

Usage

From source file:AAModulePackage.ACHelper.java

public static X509AttributeCertificateHolder generateAttributeCertificate(X509CertificateHolder issuerCert,
        X509CertificateHolder associatedCert, PrivateKey pk, String role, String record_id,
        String record_subject, String[] record_types, String[] actions_taken) {
    //Set up the validity period.
    Date startDate = new Date(System.currentTimeMillis() - 24 * 60 * 60 * 1000);
    Date endDate = new Date(System.currentTimeMillis() + 365 * 24 * 60 * 60 * 1000);

    //AttributeCertificateHolder is a wrapper class for AttributeCertificates, courtesy of the Legion of Bouncy Castle.
    AttributeCertificateIssuer certIssuer = new AttributeCertificateIssuer(issuerCert.getSubject());

    /*/*from  w w  w . j a v a 2s . c om*/
    Please note the distinction between AttributeCertificateHolder which appears to be the
    Entity in possession of the certificate, while X509AttributeCertificateHolder is a
    wrapper class for the actual certificate itself.
     */

    AttributeCertificateHolder holder = new AttributeCertificateHolder(associatedCert);
    X509v2AttributeCertificateBuilder builder = new X509v2AttributeCertificateBuilder(holder, certIssuer,
            BigInteger.valueOf(System.currentTimeMillis()), startDate, endDate);

    builder.addAttribute(NewAttributeIdentifiers.role, new DERGeneralString(role));
    builder.addAttribute(NewAttributeIdentifiers.record_id, new DERGeneralString(record_id));
    builder.addAttribute(NewAttributeIdentifiers.record_subject, new DERGeneralString(record_subject));
    builder.addAttribute(NewAttributeIdentifiers.time_stamp, new DERGeneralizedTime(new Date()));

    //record_types
    ArrayList<ASN1Encodable> rts = new ArrayList();
    for (String s : record_types) {
        rts.add(new DERGeneralString(s));
    }
    ASN1Encodable[] recTypes = rts.toArray(new DERGeneralString[rts.size()]);

    builder.addAttribute(NewAttributeIdentifiers.record_type, recTypes);

    //actions_taken
    ArrayList<ASN1Encodable> acts = new ArrayList();
    for (String s : actions_taken) {
        acts.add(new DERGeneralString(s));
    }
    ASN1Encodable[] actionsTaken = acts.toArray(new DERGeneralString[acts.size()]);
    builder.addAttribute(NewAttributeIdentifiers.actions_taken, actionsTaken);

    //Build the certificate
    X509AttributeCertificateHolder attrCert = null;
    try {
        //builds the attribute certificate, and signs it with the owner's private key.
        attrCert = builder
                .build(new JcaContentSignerBuilder("SHA256withRSAEncryption").setProvider("BC").build(pk));
    } catch (OperatorCreationException e) {
        e.printStackTrace();
    }

    System.out.println("ATTRIBUTE CERTIFICATE Successfully generated.");

    return attrCert;
}

From source file:com.jlocksmith.util.ExtensionUtil.java

License:Open Source License

/**
 * Get Private Key Usage Period//  w  w w  .  j  a  va  2 s .co m
 * 
 * @param bytes
 * 
 * @return String
 * 
 * @throws IOException
 * @throws ParseException
 */
private String getPrivateKeyUsagePeriod(byte[] bytes) throws IOException, ParseException {
    ASN1Sequence times = (ASN1Sequence) toDERObject(bytes);

    StringBuffer strBuff = new StringBuffer();

    for (int i = 0, len = times.size(); i < len; i++) {
        DERTaggedObject derTag = (DERTaggedObject) times.getObjectAt(i);
        DEROctetString dOct = (DEROctetString) derTag.getObject();
        DERGeneralizedTime dTime = new DERGeneralizedTime(new String(dOct.getOctets()));

        strBuff.append(MessageFormat.format(localeUtil.getString("PrivateKeyUsagePeriod." + derTag.getTagNo()),
                new Object[] { formatGeneralizedTime(dTime) }));

        strBuff.append('\n');
    }

    return strBuff.toString();
}

From source file:com.otterca.common.crypto.X509CertificateBuilderImpl.java

License:Apache License

/**
 * @see com.otterca.common.crypto.X509CertificateBuilder#setPrivateKeyUsagePeriod(Date,
 *      Date)//from   www .  ja v a2  s . c om
 */
@Override
public X509CertificateBuilder setPrivateKeyUsagePeriod(@Nullable Date notBefore, @Nullable Date notAfter) {

    if ((notBefore == null) && (notAfter == null)) {
        return this;
    }

    DERGeneralizedTime gtNotBefore = (notBefore != null) ? new DERGeneralizedTime(notBefore) : null;
    DERGeneralizedTime gtNotAfter = (notAfter != null) ? new DERGeneralizedTime(notAfter) : null;

    DERSequence seq = null;
    if ((gtNotBefore != null) && (gtNotAfter != null)) {
        seq = new DERSequence(new DERTaggedObject[] { new DERTaggedObject(0, gtNotBefore),
                new DERTaggedObject(1, gtNotAfter) });
    } else if (gtNotBefore != null) {
        seq = new DERSequence(new DERTaggedObject[] { new DERTaggedObject(0, gtNotBefore) });
    } else {
        seq = new DERSequence(new DERTaggedObject[] { new DERTaggedObject(1, gtNotAfter) });
    }

    this.privateKeyUsagePeriod = PrivateKeyUsagePeriod.getInstance(seq);
    return this;
}

From source file:eu.europa.ec.markt.dss.signature.cades.CAdESProfileC.java

License:Open Source License

/**
 * Create a reference on a OCSPResp/*from w ww .  j  a  v a2 s . c  o  m*/
 * 
 * @param ocspResp
 * @return
 * @throws NoSuchAlgorithmException
 * @throws OCSPException
 * @throws IOException
 */
private OcspResponsesID makeOcspResponsesID(BasicOCSPResp ocspResp)
        throws NoSuchAlgorithmException, OCSPException, IOException {
    /*
     * We hash the complete response, this is not clear in the TS but the issue was addressed here:
     * http://lists.iaik.tugraz.at/pipermail/jce-general/2007-January/005914.html
     */
    MessageDigest sha1digest = MessageDigest.getInstance(X509ObjectIdentifiers.id_SHA1.getId(),
            new BouncyCastleProvider());

    byte[] digestValue = sha1digest.digest(ocspResp.getEncoded());
    OtherHash hash = new OtherHash(digestValue);

    OcspResponsesID ocsprespid = new OcspResponsesID(new OcspIdentifier(
            ocspResp.getResponderId().toASN1Object(), new DERGeneralizedTime(ocspResp.getProducedAt())), hash);

    LOG.info("Incorporate OcspResponseId[hash=" + Hex.encodeHexString(digestValue) + ",producedAt="
            + ocspResp.getProducedAt());

    return ocsprespid;
}

From source file:io.aos.crypto.spl05.MyStructure.java

License:Apache License

/**
 * Constructor from corresponding Java objects and primitives.
 *//*from   ww  w.j a v  a  2  s  .c  o  m*/
public MyStructure(int version, Date created, byte[] baseData, String extraData, String commentData) {
    this.version = new DERInteger(version);
    this.created = new DERGeneralizedTime(created);
    this.baseData = new DEROctetString(baseData);

    if (extraData != null) {
        this.extraData = new DERUTF8String(extraData);
    }

    if (commentData != null) {
        this.commentData = new DERUTF8String(commentData);
    }
}

From source file:net.java.bd.tools.security.X509BDJEntryConverter.java

License:Open Source License

public DERObject getConvertedValue(DERObjectIdentifier oid, String value) {

    if (value.length() != 0 && value.charAt(0) == '#') {
        try {/*from  www. j  a  v  a 2 s .c o  m*/
            return convertHexEncoded(value, 1);
        } catch (IOException e) {
            throw new RuntimeException("can't recode value for oid " + oid.getId());
        }
    } else if (oid.equals(X509Name.EmailAddress) || oid.equals(X509Name.DC)) {
        return new DERIA5String(value);
    } else if (oid.equals(X509Name.DATE_OF_BIRTH)) {
        return new DERGeneralizedTime(value);
        //} else if (oid.equals(X509Name.C) || oid.equals(X509Name.SN) || oid.equals(X509Name.DN_QUALIFIER)){
        // Blu-ray Specific, require UTF8String.  MHP 12.5.6.
    } else if (oid.equals(X509Name.SN) || oid.equals(X509Name.DN_QUALIFIER)) {
        return new DERPrintableString(value);
    }

    return new DERUTF8String(value);
}

From source file:net.sf.keystore_explorer.gui.dialogs.extensions.DPrivateKeyUsagePeriod.java

License:Open Source License

private void okPressed() {

    Date notBefore = jdtNotBefore.getDateTime();
    Date notAfter = jdtNotAfter.getDateTime();

    if ((notBefore == null) && (notAfter == null)) {
        JOptionPane.showMessageDialog(this, res.getString("DPrivateKeyUsagePeriod.ValueReq.message"),
                getTitle(), JOptionPane.WARNING_MESSAGE);
        return;//from ww w.  j a v  a  2  s.  c o  m
    }

    // BC forgot the value constructor for PrivateKeyUsagePeriod...
    ASN1EncodableVector v = new ASN1EncodableVector();
    if (notBefore != null) {
        DERGeneralizedTime notBeforeGenTime = new DERGeneralizedTime(notBefore);
        v.add(new DERTaggedObject(false, 0, notBeforeGenTime));
    }
    if (notAfter != null) {
        DERGeneralizedTime notAfterGenTime = new DERGeneralizedTime(notAfter);
        v.add(new DERTaggedObject(false, 1, notAfterGenTime));
    }

    PrivateKeyUsagePeriod privateKeyUsagePeriod = PrivateKeyUsagePeriod.getInstance(new DERSequence(v));

    try {
        value = privateKeyUsagePeriod.getEncoded(ASN1Encoding.DER);
    } catch (IOException ex) {
        DError dError = new DError(this, ex);
        dError.setLocationRelativeTo(this);
        dError.setVisible(true);
        return;
    }

    closeDialog();
}

From source file:nl.uva.vlet.grid.voms.VOMSAttributeCertificate.java

License:Apache License

public void setTimes(Date from, Date to) throws Exception {
    try {//from  w w  w  . j  a  va2  s.  com
        this.attrCertValidityPeriod = new AttCertValidityPeriod(new DERGeneralizedTime(from),
                new DERGeneralizedTime(to));
    } catch (Exception e) {
        throw e;
    }
}

From source file:org.candlepin.util.X509CRLStreamWriter.java

License:Open Source License

/**
 * Write a new nextUpdate time that is the same amount of time ahead of the new thisUpdate
 * time as the old nextUpdate was from the old thisUpdate.
 *
 * @param out//from w  w w  .jav a  2 s  .c om
 * @param tagNo
 * @param oldThisUpdate
 * @throws IOException
 */
protected void offsetNextUpdate(OutputStream out, int tagNo, Date oldThisUpdate) throws IOException {
    int originalLength = readLength(crlIn, null);
    byte[] oldBytes = new byte[originalLength];
    readFullyAndTrack(crlIn, oldBytes, null);

    DERObject oldTime = null;
    if (tagNo == UTC_TIME) {
        DERTaggedObject t = new DERTaggedObject(UTC_TIME, new DEROctetString(oldBytes));
        oldTime = DERUTCTime.getInstance(t, false);
    } else {
        DERTaggedObject t = new DERTaggedObject(GENERALIZED_TIME, new DEROctetString(oldBytes));
        oldTime = DERGeneralizedTime.getInstance(t, false);
    }

    /* Determine the time between the old thisUpdate and old nextUpdate and add it
    /* to the new nextUpdate. */
    Date oldNextUpdate = new Time(oldTime).getDate();
    long delta = oldNextUpdate.getTime() - oldThisUpdate.getTime();
    Date newNextUpdate = new Date(new Date().getTime() + delta);

    DERObject newTime = null;
    if (tagNo == UTC_TIME) {
        newTime = new DERUTCTime(newNextUpdate);
    } else {
        newTime = new DERGeneralizedTime(newNextUpdate);
    }
    writeNewTime(out, newTime, originalLength);
}

From source file:org.candlepin.util.X509CRLStreamWriter.java

License:Open Source License

/**
 * Replace a time in the ASN1 with the current time.
 *
 * @param out/*from w  w  w .ja  va 2  s.  c om*/
 * @param tagNo
 * @return the time that was replaced
 * @throws IOException
 */
protected Date readAndReplaceTime(OutputStream out, int tagNo) throws IOException {
    int originalLength = readLength(crlIn, null);
    byte[] oldBytes = new byte[originalLength];
    readFullyAndTrack(crlIn, oldBytes, null);

    DERObject oldTime = null;
    DERObject newTime = null;
    if (tagNo == UTC_TIME) {
        DERTaggedObject t = new DERTaggedObject(UTC_TIME, new DEROctetString(oldBytes));
        oldTime = DERUTCTime.getInstance(t, false);
        newTime = new DERUTCTime(new Date());
    } else {
        DERTaggedObject t = new DERTaggedObject(GENERALIZED_TIME, new DEROctetString(oldBytes));
        oldTime = DERGeneralizedTime.getInstance(t, false);
        newTime = new DERGeneralizedTime(new Date());
    }

    writeNewTime(out, newTime, originalLength);
    return new Time(oldTime).getDate();
}