Search in sources :

Example 31 with ASN1TaggedObject

use of org.bouncycastle.asn1.ASN1TaggedObject in project certmgr by hdecarne.

the class GeneralName method decode.

/**
 * Decode {@code GeneralName} object from an ASN.1 data object.
 *
 * @param primitive The ASN.1 data object to decode.
 * @return The decoded name object.
 * @throws IOException if an I/O error occurs during decoding.
 */
public static GeneralName decode(ASN1Primitive primitive) throws IOException {
    ASN1TaggedObject taggedObject = decodePrimitive(primitive, ASN1TaggedObject.class);
    int nameTypeTag = taggedObject.getTagNo();
    GeneralName name;
    switch(nameTypeTag) {
        case GeneralNameType.OTHER_NAME_TAG:
            name = OtherName.decode(primitive);
            break;
        case GeneralNameType.RFC822_NAME_TAG:
            name = StringName.decode(GeneralNameType.RFC822_NAME, primitive);
            break;
        case GeneralNameType.DNS_NAME_TAG:
            name = StringName.decode(GeneralNameType.DNS_NAME, primitive);
            break;
        case GeneralNameType.X400_ADDRESS_TAG:
            name = GenericName.decode(GeneralNameType.X400_ADDRESS, primitive);
            break;
        case GeneralNameType.DIRECTORY_NAME_TAG:
            name = DirectoryName.decode(primitive);
            break;
        case GeneralNameType.EDI_PARTY_NAME_TAG:
            name = GenericName.decode(GeneralNameType.EDI_PARTY_NAME, primitive);
            break;
        case GeneralNameType.UNIFORM_RESOURCE_IDENTIFIER_TAG:
            name = StringName.decode(GeneralNameType.UNIFORM_RESOURCE_IDENTIFIER, primitive);
            break;
        case GeneralNameType.IP_ADDRESS_TAG:
            name = IPAddressName.decode(primitive);
            break;
        case GeneralNameType.REGISTERED_ID_TAG:
            name = RegisteredIDName.decode(primitive);
            break;
        default:
            throw new IOException("Unsupported general name type: " + nameTypeTag);
    }
    return name;
}
Also used : ASN1TaggedObject(org.bouncycastle.asn1.ASN1TaggedObject) IOException(java.io.IOException)

Example 32 with ASN1TaggedObject

use of org.bouncycastle.asn1.ASN1TaggedObject in project certmgr by hdecarne.

the class RegisteredIDName method decode.

/**
 * Decode {@code RegisteredIDName} from an ASN.1 data object.
 *
 * @param primitive The ASN.1 data object to decode.
 * @return The decoded registered ID object.
 * @throws IOException if an I/O error occurs during decoding.
 */
public static RegisteredIDName decode(ASN1Primitive primitive) throws IOException {
    decodeTagged(primitive, GeneralNameType.REGISTERED_ID_TAG);
    ASN1TaggedObject taggedObject = decodePrimitive(primitive, ASN1TaggedObject.class);
    String oid = ASN1ObjectIdentifier.getInstance(taggedObject, false).getId();
    return new RegisteredIDName(oid);
}
Also used : ASN1TaggedObject(org.bouncycastle.asn1.ASN1TaggedObject)

Example 33 with ASN1TaggedObject

use of org.bouncycastle.asn1.ASN1TaggedObject in project candlepin by candlepin.

the class X509CRLStreamWriter method offsetNextUpdate.

/**
 * 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
 * @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);
    ASN1Object oldTime = null;
    if (tagNo == UTC_TIME) {
        ASN1TaggedObject t = new DERTaggedObject(UTC_TIME, new DEROctetString(oldBytes));
        oldTime = ASN1UTCTime.getInstance(t, false);
    } else {
        ASN1TaggedObject t = new DERTaggedObject(GENERALIZED_TIME, new DEROctetString(oldBytes));
        oldTime = ASN1GeneralizedTime.getInstance(t, false);
    }
    /* Determine the time between the old thisUpdate and old nextUpdate and add it
        /* to the new nextUpdate. */
    Date oldNextUpdate = Time.getInstance(oldTime).getDate();
    long delta = oldNextUpdate.getTime() - oldThisUpdate.getTime();
    Date newNextUpdate = new Date(new Date().getTime() + delta);
    ASN1Object newTime = null;
    if (tagNo == UTC_TIME) {
        newTime = new DERUTCTime(newNextUpdate);
    } else {
        newTime = new DERGeneralizedTime(newNextUpdate);
    }
    writeNewTime(out, newTime, originalLength);
}
Also used : DERGeneralizedTime(org.bouncycastle.asn1.DERGeneralizedTime) DERUTCTime(org.bouncycastle.asn1.DERUTCTime) DERTaggedObject(org.bouncycastle.asn1.DERTaggedObject) ASN1TaggedObject(org.bouncycastle.asn1.ASN1TaggedObject) ASN1Object(org.bouncycastle.asn1.ASN1Object) DEROctetString(org.bouncycastle.asn1.DEROctetString) Date(java.util.Date)

Example 34 with ASN1TaggedObject

use of org.bouncycastle.asn1.ASN1TaggedObject in project candlepin by candlepin.

the class X509CRLStreamWriter method updateExtensions.

/**
 * This method updates the crlNumber and authorityKeyIdentifier extensions.  Any
 * other extensions are copied over unchanged.
 * @param obj
 * @return
 * @throws IOException
 */
@SuppressWarnings("rawtypes")
protected byte[] updateExtensions(byte[] obj) throws IOException {
    ASN1TaggedObject taggedExts = (ASN1TaggedObject) new ASN1InputStream(obj).readObject();
    ASN1Sequence seq = (ASN1Sequence) taggedExts.getObject();
    ASN1EncodableVector modifiedExts = new ASN1EncodableVector();
    // Now we need to read the extensions and find the CRL number and increment it,
    // and determine if its length changed.
    Enumeration objs = seq.getObjects();
    while (objs.hasMoreElements()) {
        ASN1Sequence ext = (ASN1Sequence) objs.nextElement();
        ASN1ObjectIdentifier oid = (ASN1ObjectIdentifier) ext.getObjectAt(0);
        if (Extension.cRLNumber.equals(oid)) {
            ASN1OctetString s = (ASN1OctetString) ext.getObjectAt(1);
            ASN1Integer i = (ASN1Integer) new ASN1InputStream(s.getOctets()).readObject();
            ASN1Integer newCrlNumber = new ASN1Integer(i.getValue().add(BigInteger.ONE));
            Extension newNumberExt = new Extension(Extension.cRLNumber, false, new DEROctetString(newCrlNumber.getEncoded()));
            ASN1EncodableVector crlNumber = new ASN1EncodableVector();
            crlNumber.add(Extension.cRLNumber);
            crlNumber.add(newNumberExt.getExtnValue());
            modifiedExts.add(new DERSequence(crlNumber));
        } else if (Extension.authorityKeyIdentifier.equals(oid)) {
            Extension newAuthorityKeyExt = new Extension(Extension.authorityKeyIdentifier, false, aki.getEncoded());
            ASN1EncodableVector aki = new ASN1EncodableVector();
            aki.add(Extension.authorityKeyIdentifier);
            aki.add(newAuthorityKeyExt.getExtnValue());
            modifiedExts.add(new DERSequence(aki));
        } else {
            modifiedExts.add(ext);
        }
    }
    ASN1Sequence seqOut = new DERSequence(modifiedExts);
    ASN1TaggedObject out = new DERTaggedObject(true, 0, seqOut);
    return out.getEncoded();
}
Also used : ASN1OctetString(org.bouncycastle.asn1.ASN1OctetString) Extension(org.bouncycastle.asn1.x509.Extension) ASN1InputStream(org.bouncycastle.asn1.ASN1InputStream) ASN1Sequence(org.bouncycastle.asn1.ASN1Sequence) Enumeration(java.util.Enumeration) DERSequence(org.bouncycastle.asn1.DERSequence) DERTaggedObject(org.bouncycastle.asn1.DERTaggedObject) ASN1TaggedObject(org.bouncycastle.asn1.ASN1TaggedObject) ASN1EncodableVector(org.bouncycastle.asn1.ASN1EncodableVector) ASN1Integer(org.bouncycastle.asn1.ASN1Integer) ASN1ObjectIdentifier(org.bouncycastle.asn1.ASN1ObjectIdentifier) DEROctetString(org.bouncycastle.asn1.DEROctetString)

Example 35 with ASN1TaggedObject

use of org.bouncycastle.asn1.ASN1TaggedObject in project candlepin by candlepin.

the class X509CRLStreamWriter method readAndReplaceTime.

/**
 * Replace a time in the ASN1 with the current time.
 *
 * @param out
 * @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);
    ASN1Object oldTime;
    ASN1Object newTime;
    if (tagNo == UTC_TIME) {
        ASN1TaggedObject t = new DERTaggedObject(UTC_TIME, new DEROctetString(oldBytes));
        oldTime = ASN1UTCTime.getInstance(t, false);
        newTime = new DERUTCTime(new Date());
    } else {
        ASN1TaggedObject t = new DERTaggedObject(GENERALIZED_TIME, new DEROctetString(oldBytes));
        oldTime = ASN1GeneralizedTime.getInstance(t, false);
        newTime = new DERGeneralizedTime(new Date());
    }
    writeNewTime(out, newTime, originalLength);
    return Time.getInstance(oldTime).getDate();
}
Also used : DERGeneralizedTime(org.bouncycastle.asn1.DERGeneralizedTime) DERUTCTime(org.bouncycastle.asn1.DERUTCTime) DERTaggedObject(org.bouncycastle.asn1.DERTaggedObject) ASN1TaggedObject(org.bouncycastle.asn1.ASN1TaggedObject) ASN1Object(org.bouncycastle.asn1.ASN1Object) DEROctetString(org.bouncycastle.asn1.DEROctetString) Date(java.util.Date)

Aggregations

ASN1TaggedObject (org.bouncycastle.asn1.ASN1TaggedObject)35 IOException (java.io.IOException)23 ASN1Sequence (org.bouncycastle.asn1.ASN1Sequence)20 ASN1Encodable (org.bouncycastle.asn1.ASN1Encodable)13 ASN1OctetString (org.bouncycastle.asn1.ASN1OctetString)13 ASN1Primitive (org.bouncycastle.asn1.ASN1Primitive)11 Enumeration (java.util.Enumeration)10 ASN1ObjectIdentifier (org.bouncycastle.asn1.ASN1ObjectIdentifier)10 DERIA5String (org.bouncycastle.asn1.DERIA5String)10 DEROctetString (org.bouncycastle.asn1.DEROctetString)10 X509Certificate (java.security.cert.X509Certificate)9 List (java.util.List)8 CRLDistPoint (org.bouncycastle.asn1.x509.CRLDistPoint)8 DistributionPoint (org.bouncycastle.asn1.x509.DistributionPoint)8 BigInteger (java.math.BigInteger)6 GeneralSecurityException (java.security.GeneralSecurityException)6 CertPathBuilderException (java.security.cert.CertPathBuilderException)6 CertPathValidatorException (java.security.cert.CertPathValidatorException)6 CertificateExpiredException (java.security.cert.CertificateExpiredException)6 CertificateNotYetValidException (java.security.cert.CertificateNotYetValidException)6