All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.bouncycastle.openssl.CertificateTrustBlock Maven / Gradle / Ivy

Go to download

The Bouncy Castle Java APIs for CMS, PKCS, EAC, TSP, CMP, CRMF, OCSP, and certificate generation. This jar contains APIs for JDK 1.5 to JDK 1.8. The APIs can be used in conjunction with a JCE/JCA provider such as the one provided with the Bouncy Castle Cryptography APIs.

The newest version!
package org.bouncycastle.openssl;

import java.util.Collections;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import org.bouncycastle.asn1.ASN1Encodable;
import org.bouncycastle.asn1.ASN1EncodableVector;
import org.bouncycastle.asn1.ASN1ObjectIdentifier;
import org.bouncycastle.asn1.ASN1Sequence;
import org.bouncycastle.asn1.ASN1TaggedObject;
import org.bouncycastle.asn1.ASN1UTF8String;
import org.bouncycastle.asn1.DERSequence;
import org.bouncycastle.asn1.DERTaggedObject;
import org.bouncycastle.asn1.DERUTF8String;

public class CertificateTrustBlock
{
    private ASN1Sequence uses;
    private ASN1Sequence prohibitions;
    private String alias;

    public CertificateTrustBlock(Set uses)
    {
        this(null, uses, null);
    }

    public CertificateTrustBlock(String alias, Set uses)
    {
        this(alias, uses, null);
    }

    public CertificateTrustBlock(String alias, Set uses, Set prohibitions)
    {
        this.alias = alias;
        this.uses = toSequence(uses);
        this.prohibitions = toSequence(prohibitions);
    }

    CertificateTrustBlock(byte[] encoded)
    {
        ASN1Sequence seq = ASN1Sequence.getInstance(encoded);

        for (Enumeration en = seq.getObjects(); en.hasMoreElements();)
        {
            ASN1Encodable obj = (ASN1Encodable)en.nextElement();

            if (obj instanceof ASN1Sequence)
            {
                this.uses = ASN1Sequence.getInstance(obj);
            }
            else if (obj instanceof ASN1TaggedObject)
            {
                this.prohibitions = ASN1Sequence.getInstance((ASN1TaggedObject)obj, false);
            }
            else if (obj instanceof ASN1UTF8String)
            {
                this.alias = ASN1UTF8String.getInstance(obj).getString();
            }
        }
    }

    public String getAlias()
    {
        return alias;
    }

    public Set getUses()
    {
        return toSet(uses);
    }

    public Set getProhibitions()
    {
        return toSet(prohibitions);
    }

    private Set toSet(ASN1Sequence seq)
    {
        if (seq != null)
        {
            Set oids = new HashSet(seq.size());

            for (Enumeration en = seq.getObjects(); en.hasMoreElements(); )
            {
                oids.add(ASN1ObjectIdentifier.getInstance(en.nextElement()));
            }

            return oids;
        }

        return Collections.EMPTY_SET;
    }

    private ASN1Sequence toSequence(Set oids)
    {
        if (oids == null || oids.isEmpty())
        {
            return null;
        }

        ASN1EncodableVector v = new ASN1EncodableVector();

        for (Iterator it = oids.iterator(); it.hasNext();)
        {
           v.add((ASN1Encodable)it.next());
        }

        return new DERSequence(v);
    }

    ASN1Sequence toASN1Sequence()
    {
        ASN1EncodableVector v = new ASN1EncodableVector();

        if (uses != null)
        {
           v.add(uses);
        }
        if (prohibitions != null)
        {
            v.add(new DERTaggedObject(false, 0, prohibitions));
        }
        if (alias != null)
        {
            v.add(new DERUTF8String(alias));
        }

        return new DERSequence(v);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy