Example usage for org.bouncycastle.asn1.x509 PrivateKeyUsagePeriod getInstance

List of usage examples for org.bouncycastle.asn1.x509 PrivateKeyUsagePeriod getInstance

Introduction

In this page you can find the example usage for org.bouncycastle.asn1.x509 PrivateKeyUsagePeriod getInstance.

Prototype

public static PrivateKeyUsagePeriod getInstance(Object obj) 

Source Link

Usage

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

License:Apache License

/**
 * @see com.otterca.common.crypto.X509CertificateBuilder#setPrivateKeyUsagePeriod(Date,
 *      Date)/*from w  w w . j av  a2 s.  c o m*/
 */
@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:net.sf.keystore_explorer.crypto.x509.X509Ext.java

License:Open Source License

private String getPrivateKeyUsagePeriodStringValue(byte[] value) throws IOException {
    // @formatter:off

    /*/*from  w ww. java 2 s.  c  o  m*/
     * PrivateKeyUsagePeriod ::= ASN1Sequence { notBefore [0]
     * ASN1GeneralizedTime OPTIONAL, notAfter [1] ASN1GeneralizedTime OPTIONAL }
     */

    // @formatter:on

    StringBuilder sb = new StringBuilder();

    PrivateKeyUsagePeriod privateKeyUsagePeriod = PrivateKeyUsagePeriod.getInstance(value);

    ASN1GeneralizedTime notBefore = privateKeyUsagePeriod.getNotBefore();
    ASN1GeneralizedTime notAfter = privateKeyUsagePeriod.getNotAfter();

    if (notBefore != null) {
        sb.append(MessageFormat.format(res.getString("NotBeforePrivateKeyUsagePeriod"),
                getGeneralizedTimeString(notBefore)));
    } else {
        sb.append(MessageFormat.format(res.getString("NotBeforePrivateKeyUsagePeriod"),
                res.getString("NoValue")));
    }
    sb.append(NEWLINE);

    if (notAfter != null) {
        sb.append(MessageFormat.format(res.getString("NotAfterPrivateKeyUsagePeriod"),
                getGeneralizedTimeString(notAfter)));
    } else {
        sb.append(
                MessageFormat.format(res.getString("NotAfterPrivateKeyUsagePeriod"), res.getString("NoValue")));
    }
    sb.append(NEWLINE);

    return sb.toString();
}

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

License:Open Source License

private void prepopulateWithValue(byte[] value) throws IOException {
    PrivateKeyUsagePeriod privateKeyUsagePeriod = PrivateKeyUsagePeriod.getInstance(value);

    ASN1GeneralizedTime notBefore = privateKeyUsagePeriod.getNotBefore();

    if (notBefore != null) {
        try {/*  w ww .j av  a 2s . c o m*/
            jdtNotBefore.setDateTime(notBefore.getDate());
        } catch (ParseException e) {
            throw new IOException(e);
        }
    }

    ASN1GeneralizedTime notAfter = privateKeyUsagePeriod.getNotAfter();

    if (notAfter != null) {
        try {
            jdtNotAfter.setDateTime(notAfter.getDate());
        } catch (ParseException e) {
            throw new IOException(e);
        }
    }
}

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 w w  w .ja  va  2s  .com
    }

    // 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:net.sf.portecle.crypto.X509Ext.java

License:Open Source License

/**
 * Get Private Key Usage Period (2.5.29.16) extension value as a string.
 * /*from  www.  j a va 2s  .c o  m*/
 * <pre>
 * PrivateKeyUsagePeriod ::= SEQUENCE {
 *       notBefore       [0]     GeneralizedTime OPTIONAL,
 *       notAfter        [1]     GeneralizedTime OPTIONAL }
 * </pre>
 * 
 * @param bValue The octet string value
 * @return Extension value as a string
 * @throws IOException If an I/O problem occurs
 * @throws ParseException If a date formatting problem occurs
 */
private String getPrivateKeyUsagePeriod(byte[] bValue) throws IOException, ParseException {
    PrivateKeyUsagePeriod pkup = PrivateKeyUsagePeriod.getInstance(bValue);

    StringBuilder strBuff = new StringBuilder();
    ASN1GeneralizedTime dTime;

    if ((dTime = pkup.getNotBefore()) != null) {
        strBuff.append(MessageFormat.format(RB.getString("PrivateKeyUsagePeriodNotBefore"),
                formatGeneralizedTime(dTime)));
    }

    if ((dTime = pkup.getNotAfter()) != null) {
        if (strBuff.length() != 0) {
            strBuff.append("<br><br>");
        }
        strBuff.append(MessageFormat.format(RB.getString("PrivateKeyUsagePeriodNotAfter"),
                formatGeneralizedTime(dTime)));
    }

    return strBuff.toString();
}

From source file:org.cesecore.util.CertTools.java

License:Open Source License

/** Reads PrivateKeyUsagePeriod extension from a certificate
 * //www  .  j a  va 2  s.co  m
 */
public static PrivateKeyUsagePeriod getPrivateKeyUsagePeriod(final X509Certificate cert) {
    PrivateKeyUsagePeriod res = null;
    final byte[] extvalue = cert.getExtensionValue(Extension.privateKeyUsagePeriod.getId());
    if ((extvalue != null) && (extvalue.length > 0)) {
        if (log.isTraceEnabled()) {
            log.trace("Found a PrivateKeyUsagePeriod in the certificate with subject: "
                    + cert.getSubjectDN().toString());
        }
        ASN1InputStream extAsn1InputStream = new ASN1InputStream(new ByteArrayInputStream(extvalue));
        try {
            try {
                final DEROctetString oct = (DEROctetString) (extAsn1InputStream.readObject());
                ASN1InputStream octAsn1InputStream = new ASN1InputStream(
                        new ByteArrayInputStream(oct.getOctets()));
                try {
                    res = PrivateKeyUsagePeriod.getInstance((ASN1Sequence) octAsn1InputStream.readObject());
                } finally {
                    octAsn1InputStream.close();
                }
            } finally {
                extAsn1InputStream.close();
            }
        } catch (IOException e) {
            throw new IllegalStateException("Unknown IOException caught when trying to parse certificate.", e);
        }
    }
    return res;
}

From source file:org.ejbca.core.ejb.ca.sign.PrivateKeyUsageSignSessionTest.java

License:Open Source License

private void privateKeyUsageTestStartOffset(final long startOffset) throws Exception {
    X509Certificate cert = privateKeyUsageGetCertificate(true, startOffset, false, 0L, false);
    assertNotNull("Has not the extension", cert.getExtensionValue("2.5.29.16"));
    assertTrue("Extension is non-critical", cert.getNonCriticalExtensionOIDs().contains("2.5.29.16"));
    PrivateKeyUsagePeriod ext = PrivateKeyUsagePeriod
            .getInstance(X509ExtensionUtil.fromExtensionValue(cert.getExtensionValue("2.5.29.16")));
    assertNotNull("Has notBefore", ext.getNotBefore());
    assertNull("Has no notAfter", ext.getNotAfter());
    assertEquals("notBefore " + startOffset + " seconds after ca cert",
            cert.getNotBefore().getTime() + startOffset * 1000, ext.getNotBefore().getDate().getTime());
}

From source file:org.ejbca.core.ejb.ca.sign.PrivateKeyUsageSignSessionTest.java

License:Open Source License

private void privateKeyUsageTestValidityLength(final long length) throws Exception {
    X509Certificate cert = privateKeyUsageGetCertificate(false, 0L, true, length, false);
    assertNotNull("Has the extension", cert.getExtensionValue("2.5.29.16"));
    assertTrue("Extension is non-critical", cert.getNonCriticalExtensionOIDs().contains("2.5.29.16"));
    PrivateKeyUsagePeriod ext = PrivateKeyUsagePeriod
            .getInstance(X509ExtensionUtil.fromExtensionValue(cert.getExtensionValue("2.5.29.16")));
    assertNotNull("Has notAfter", ext.getNotAfter());
    assertNull("Has no notBefore", ext.getNotBefore());
    assertEquals("notAfter " + length + " seconds after issue time",
            cert.getNotBefore().getTime() + length * 1000, ext.getNotAfter().getDate().getTime());
}

From source file:org.ejbca.core.ejb.ca.sign.PrivateKeyUsageSignSessionTest.java

License:Open Source License

private void privateKeyUsageTestBoth(final long startOffset, final long length, boolean allowValidityOverride)
        throws Exception {
    X509Certificate cert = privateKeyUsageGetCertificate(true, startOffset, true, length,
            allowValidityOverride);/*w  w  w  .j  av a2s .  com*/
    assertNotNull("Has the extension", cert.getExtensionValue("2.5.29.16"));
    assertTrue("Extension is non-critical", cert.getNonCriticalExtensionOIDs().contains("2.5.29.16"));
    PrivateKeyUsagePeriod ext = PrivateKeyUsagePeriod
            .getInstance(X509ExtensionUtil.fromExtensionValue(cert.getExtensionValue("2.5.29.16")));
    assertNotNull("Has notBefore", ext.getNotBefore());
    assertNotNull("Has notAfter", ext.getNotAfter());
    assertEquals("notBefore " + startOffset + " seconds after ca cert",
            cert.getNotBefore().getTime() + startOffset * 1000, ext.getNotBefore().getDate().getTime());
    assertEquals("notAfter " + length + " seconds after notBefore",
            ext.getNotBefore().getDate().getTime() + length * 1000, ext.getNotAfter().getDate().getTime());
}

From source file:org.ejbca.core.ejb.ca.sign.SignSessionTest.java

License:Open Source License

private void privateKeyUsageTestStartOffset(final long startOffset) throws Exception {
    X509Certificate cert = privateKeyUsageGetCertificate(true, startOffset, false, 0L);
    assertNotNull("Has the extension", cert.getExtensionValue("2.5.29.16"));
    assertTrue("Extension is non-critical", cert.getNonCriticalExtensionOIDs().contains("2.5.29.16"));
    PrivateKeyUsagePeriod ext = PrivateKeyUsagePeriod
            .getInstance(X509ExtensionUtil.fromExtensionValue(cert.getExtensionValue("2.5.29.16")));
    assertNotNull("Has notBefore", ext.getNotBefore());
    assertNull("Has no notAfter", ext.getNotAfter());
    assertEquals("notBefore " + startOffset + " seconds after ca cert",
            cert.getNotBefore().getTime() + startOffset * 1000, ext.getNotBefore().getDate().getTime());
}