1package org.bouncycastle.asn1.x509;
2
3import org.bouncycastle.asn1.ASN1Encodable;
4import org.bouncycastle.asn1.ASN1EncodableVector;
5import org.bouncycastle.asn1.ASN1Sequence;
6import org.bouncycastle.asn1.DERObject;
7import org.bouncycastle.asn1.DERSequence;
8
9/**
10 * <code>UserNotice</code> class, used in
11 * <code>CertificatePolicies</code> X509 extensions (in policy
12 * qualifiers).
13 * <pre>
14 * UserNotice ::= SEQUENCE {
15 *      noticeRef        NoticeReference OPTIONAL,
16 *      explicitText     DisplayText OPTIONAL}
17 *
18 * </pre>
19 *
20 * @see PolicyQualifierId
21 * @see PolicyInformation
22 */
23public class UserNotice
24    extends ASN1Encodable
25{
26    private NoticeReference noticeRef;
27    private DisplayText     explicitText;
28
29    /**
30     * Creates a new <code>UserNotice</code> instance.
31     *
32     * @param noticeRef a <code>NoticeReference</code> value
33     * @param explicitText a <code>DisplayText</code> value
34     */
35    public UserNotice(
36        NoticeReference noticeRef,
37        DisplayText explicitText)
38    {
39        this.noticeRef = noticeRef;
40        this.explicitText = explicitText;
41    }
42
43    /**
44     * Creates a new <code>UserNotice</code> instance.
45     *
46     * @param noticeRef a <code>NoticeReference</code> value
47     * @param str the explicitText field as a String.
48     */
49    public UserNotice(
50        NoticeReference noticeRef,
51        String str)
52    {
53        this.noticeRef = noticeRef;
54        this.explicitText = new DisplayText(str);
55    }
56
57    /**
58     * Creates a new <code>UserNotice</code> instance.
59     * <p>Useful from reconstructing a <code>UserNotice</code> instance
60     * from its encodable/encoded form.
61     *
62     * @param as an <code>ASN1Sequence</code> value obtained from either
63     * calling @{link toASN1Object()} for a <code>UserNotice</code>
64     * instance or from parsing it from a DER-encoded stream.
65     */
66    public UserNotice(
67       ASN1Sequence as)
68    {
69       if (as.size() == 2)
70       {
71           noticeRef = NoticeReference.getInstance(as.getObjectAt(0));
72           explicitText = DisplayText.getInstance(as.getObjectAt(1));
73       }
74       else if (as.size() == 1)
75       {
76           if (as.getObjectAt(0).getDERObject() instanceof ASN1Sequence)
77           {
78               noticeRef = NoticeReference.getInstance(as.getObjectAt(0));
79           }
80           else
81           {
82               explicitText = DisplayText.getInstance(as.getObjectAt(0));
83           }
84       }
85       else
86       {
87           throw new IllegalArgumentException("Bad sequence size: " + as.size());
88       }
89    }
90
91    public NoticeReference getNoticeRef()
92    {
93        return noticeRef;
94    }
95
96    public DisplayText getExplicitText()
97    {
98        return explicitText;
99    }
100
101    public DERObject toASN1Object()
102    {
103        ASN1EncodableVector av = new ASN1EncodableVector();
104
105        if (noticeRef != null)
106        {
107            av.add(noticeRef);
108        }
109
110        if (explicitText != null)
111        {
112            av.add(explicitText);
113        }
114
115        return new DERSequence(av);
116    }
117}
118