001package org.apache.commons.ssl.org.bouncycastle.asn1.smime;
002
003import java.util.Enumeration;
004import java.util.Vector;
005
006import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1Object;
007import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1ObjectIdentifier;
008import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1Primitive;
009import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1Sequence;
010import org.apache.commons.ssl.org.bouncycastle.asn1.cms.Attribute;
011import org.apache.commons.ssl.org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers;
012
013/**
014 * Handler class for dealing with S/MIME Capabilities
015 */
016public class SMIMECapabilities
017    extends ASN1Object
018{
019    /**
020     * general preferences
021     */
022    public static final ASN1ObjectIdentifier preferSignedData = PKCSObjectIdentifiers.preferSignedData;
023    public static final ASN1ObjectIdentifier canNotDecryptAny = PKCSObjectIdentifiers.canNotDecryptAny;
024    public static final ASN1ObjectIdentifier sMIMECapabilitesVersions = PKCSObjectIdentifiers.sMIMECapabilitiesVersions;
025
026    /**
027     * encryption algorithms preferences
028     */
029    public static final ASN1ObjectIdentifier dES_CBC = new ASN1ObjectIdentifier("1.3.14.3.2.7");
030    public static final ASN1ObjectIdentifier dES_EDE3_CBC = PKCSObjectIdentifiers.des_EDE3_CBC;
031    public static final ASN1ObjectIdentifier rC2_CBC = PKCSObjectIdentifiers.RC2_CBC;
032    
033    private ASN1Sequence         capabilities;
034
035    /**
036     * return an Attribute object from the given object.
037     *
038     * @param o the object we want converted.
039     * @exception IllegalArgumentException if the object cannot be converted.
040     */
041    public static SMIMECapabilities getInstance(
042        Object o)
043    {
044        if (o == null || o instanceof SMIMECapabilities)
045        {
046            return (SMIMECapabilities)o;
047        }
048        
049        if (o instanceof ASN1Sequence)
050        {
051            return new SMIMECapabilities((ASN1Sequence)o);
052        }
053
054        if (o instanceof Attribute)
055        {
056            return new SMIMECapabilities(
057                (ASN1Sequence)(((Attribute)o).getAttrValues().getObjectAt(0)));
058        }
059
060        throw new IllegalArgumentException("unknown object in factory: " + o.getClass().getName());
061    }
062    
063    public SMIMECapabilities(
064        ASN1Sequence seq)
065    {
066        capabilities = seq;
067    }
068
069    /**
070     * returns a vector with 0 or more objects of all the capabilities
071     * matching the passed in capability OID. If the OID passed is null the
072     * entire set is returned.
073     */
074    public Vector getCapabilities(
075        ASN1ObjectIdentifier capability)
076    {
077        Enumeration e = capabilities.getObjects();
078        Vector      list = new Vector();
079
080        if (capability == null)
081        {
082            while (e.hasMoreElements())
083            {
084                SMIMECapability  cap = SMIMECapability.getInstance(e.nextElement());
085
086                list.addElement(cap);
087            }
088        }
089        else
090        {
091            while (e.hasMoreElements())
092            {
093                SMIMECapability  cap = SMIMECapability.getInstance(e.nextElement());
094
095                if (capability.equals(cap.getCapabilityID()))
096                {
097                    list.addElement(cap);
098                }
099            }
100        }
101
102        return list;
103    }
104
105    /** 
106     * Produce an object suitable for an ASN1OutputStream.
107     * <pre>
108     * SMIMECapabilities ::= SEQUENCE OF SMIMECapability
109     * </pre>
110     */
111    public ASN1Primitive toASN1Primitive()
112    {
113        return capabilities;
114    }
115}