001package org.apache.commons.ssl.org.bouncycastle.asn1.x509;
002
003import java.math.BigInteger;
004import java.util.Enumeration;
005import java.util.Vector;
006
007import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1EncodableVector;
008import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1Integer;
009import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1Object;
010import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1Primitive;
011import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1Sequence;
012import org.apache.commons.ssl.org.bouncycastle.asn1.DERSequence;
013
014/**
015 * <code>NoticeReference</code> class, used in
016 * <code>CertificatePolicies</code> X509 V3 extensions
017 * (in policy qualifiers).
018 * 
019 * <pre>
020 *  NoticeReference ::= SEQUENCE {
021 *      organization     DisplayText,
022 *      noticeNumbers    SEQUENCE OF INTEGER }
023 *
024 * </pre> 
025 * 
026 * @see PolicyQualifierInfo
027 * @see PolicyInformation
028 */
029public class NoticeReference 
030    extends ASN1Object
031{
032    private DisplayText organization;
033    private ASN1Sequence noticeNumbers;
034
035    private static ASN1EncodableVector convertVector(Vector numbers)
036    {
037        ASN1EncodableVector av = new ASN1EncodableVector();
038
039        Enumeration it = numbers.elements();
040
041        while (it.hasMoreElements())
042        {
043            Object o = it.nextElement();
044            ASN1Integer di;
045
046            if (o instanceof BigInteger)
047            {
048                di = new ASN1Integer((BigInteger)o);
049            }
050            else if (o instanceof Integer)
051            {
052                di = new ASN1Integer(((Integer)o).intValue());
053            }
054            else
055            {
056                throw new IllegalArgumentException();
057            }
058
059            av.add(di);
060        }
061        return av;
062    }
063
064   /**
065    * Creates a new <code>NoticeReference</code> instance.
066    *
067    * @param organization a <code>String</code> value
068    * @param numbers a <code>Vector</code> value
069    */
070   public NoticeReference(
071       String organization,
072       Vector numbers) 
073   {
074       this(organization, convertVector(numbers));
075   }
076
077    /**
078    * Creates a new <code>NoticeReference</code> instance.
079    *
080    * @param organization a <code>String</code> value
081    * @param noticeNumbers an <code>ASN1EncodableVector</code> value
082    */
083   public NoticeReference(
084       String organization,
085       ASN1EncodableVector noticeNumbers)
086   {
087       this(new DisplayText(organization), noticeNumbers);
088   }
089
090   /**
091    * Creates a new <code>NoticeReference</code> instance.
092    *
093    * @param organization displayText
094    * @param noticeNumbers an <code>ASN1EncodableVector</code> value
095    */
096   public NoticeReference(
097       DisplayText  organization,
098       ASN1EncodableVector noticeNumbers)
099   {
100       this.organization = organization;
101       this.noticeNumbers = new DERSequence(noticeNumbers);
102   }
103
104   /**
105    * Creates a new <code>NoticeReference</code> instance.
106    * <p>Useful for reconstructing a <code>NoticeReference</code>
107    * instance from its encodable/encoded form. 
108    *
109    * @param as an <code>ASN1Sequence</code> value obtained from either
110    * calling @{link toASN1Primitive()} for a <code>NoticeReference</code>
111    * instance or from parsing it from a DER-encoded stream. 
112    */
113   private NoticeReference(
114       ASN1Sequence as) 
115   {
116       if (as.size() != 2)
117       {
118            throw new IllegalArgumentException("Bad sequence size: "
119                    + as.size());
120       }
121
122       organization = DisplayText.getInstance(as.getObjectAt(0));
123       noticeNumbers = ASN1Sequence.getInstance(as.getObjectAt(1));
124   }
125
126   public static NoticeReference getInstance(
127       Object as) 
128   {
129      if (as instanceof NoticeReference)
130      {
131          return (NoticeReference)as;
132      }
133      else if (as != null)
134      {
135          return new NoticeReference(ASN1Sequence.getInstance(as));
136      }
137
138      return null;
139   }
140   
141   public DisplayText getOrganization()
142   {
143       return organization;
144   }
145   
146   public ASN1Integer[] getNoticeNumbers()
147   {
148       ASN1Integer[] tmp = new ASN1Integer[noticeNumbers.size()];
149
150       for (int i = 0; i != noticeNumbers.size(); i++)
151       {
152           tmp[i] = ASN1Integer.getInstance(noticeNumbers.getObjectAt(i));
153       }
154
155       return tmp;
156   }
157   
158   /**
159    * Describe <code>toASN1Object</code> method here.
160    *
161    * @return a <code>ASN1Primitive</code> value
162    */
163   public ASN1Primitive toASN1Primitive()
164   {
165      ASN1EncodableVector av = new ASN1EncodableVector();
166      av.add (organization);
167      av.add (noticeNumbers);
168      return new DERSequence (av);
169   }
170}