~brian-thomason/+junk/bouncycastle

« back to all changes in this revision

Viewing changes to src/org/bouncycastle/asn1/esf/SignerLocation.java

  • Committer: Brian Thomason
  • Date: 2011-12-20 17:20:32 UTC
  • Revision ID: brian.thomason@canonical.com-20111220172032-rdtm13jgdxtksacr
Initial import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.bouncycastle.asn1.esf;
 
2
 
 
3
import java.util.Enumeration;
 
4
 
 
5
import org.bouncycastle.asn1.ASN1Encodable;
 
6
import org.bouncycastle.asn1.ASN1EncodableVector;
 
7
import org.bouncycastle.asn1.ASN1Sequence;
 
8
import org.bouncycastle.asn1.DERObject;
 
9
import org.bouncycastle.asn1.DERSequence;
 
10
import org.bouncycastle.asn1.DERTaggedObject;
 
11
import org.bouncycastle.asn1.DERUTF8String;
 
12
import org.bouncycastle.asn1.x500.DirectoryString;
 
13
 
 
14
/**
 
15
 * Signer-Location attribute (RFC3126).
 
16
 * 
 
17
 * <pre>
 
18
 *   SignerLocation ::= SEQUENCE {
 
19
 *       countryName        [0] DirectoryString OPTIONAL,
 
20
 *       localityName       [1] DirectoryString OPTIONAL,
 
21
 *       postalAddress      [2] PostalAddress OPTIONAL }
 
22
 *
 
23
 *   PostalAddress ::= SEQUENCE SIZE(1..6) OF DirectoryString
 
24
 * </pre>
 
25
 */
 
26
public class SignerLocation
 
27
    extends ASN1Encodable 
 
28
{
 
29
    private DERUTF8String   countryName;
 
30
    private DERUTF8String   localityName;
 
31
    private ASN1Sequence    postalAddress;
 
32
    
 
33
    public SignerLocation(
 
34
        ASN1Sequence seq)
 
35
    {
 
36
        Enumeration     e = seq.getObjects();
 
37
 
 
38
        while (e.hasMoreElements())
 
39
        {
 
40
            DERTaggedObject o = (DERTaggedObject)e.nextElement();
 
41
 
 
42
            switch (o.getTagNo())
 
43
            {
 
44
            case 0:
 
45
                DirectoryString countryNameDirectoryString = DirectoryString.getInstance(o, true);
 
46
                this.countryName = new DERUTF8String(countryNameDirectoryString.getString());
 
47
                break;
 
48
            case 1:
 
49
                DirectoryString localityNameDirectoryString = DirectoryString.getInstance(o, true);
 
50
                this.localityName = new DERUTF8String(localityNameDirectoryString.getString());
 
51
                break;
 
52
            case 2:
 
53
                if (o.isExplicit())
 
54
                {
 
55
                    this.postalAddress = ASN1Sequence.getInstance(o, true);
 
56
                }
 
57
                else    // handle erroneous implicitly tagged sequences
 
58
                {
 
59
                    this.postalAddress = ASN1Sequence.getInstance(o, false);
 
60
                }
 
61
                if (postalAddress != null && postalAddress.size() > 6)
 
62
                {
 
63
                    throw new IllegalArgumentException("postal address must contain less than 6 strings");
 
64
                }
 
65
                break;
 
66
            default:
 
67
                throw new IllegalArgumentException("illegal tag");
 
68
            }
 
69
        }
 
70
    }
 
71
 
 
72
    public SignerLocation(
 
73
        DERUTF8String   countryName,
 
74
        DERUTF8String   localityName,
 
75
        ASN1Sequence    postalAddress)
 
76
    {
 
77
        if (postalAddress != null && postalAddress.size() > 6)
 
78
        {
 
79
            throw new IllegalArgumentException("postal address must contain less than 6 strings");
 
80
        }
 
81
 
 
82
        if (countryName != null)
 
83
        {
 
84
            this.countryName = DERUTF8String.getInstance(countryName.toASN1Object());
 
85
        }
 
86
 
 
87
        if (localityName != null)
 
88
        {
 
89
            this.localityName = DERUTF8String.getInstance(localityName.toASN1Object());
 
90
        }
 
91
 
 
92
        if (postalAddress != null)
 
93
        {
 
94
            this.postalAddress = ASN1Sequence.getInstance(postalAddress.toASN1Object());
 
95
        }
 
96
    }
 
97
 
 
98
    public static SignerLocation getInstance(
 
99
        Object obj)
 
100
    {
 
101
        if (obj == null || obj instanceof SignerLocation)
 
102
        {
 
103
            return (SignerLocation)obj;
 
104
        }
 
105
 
 
106
        return new SignerLocation(ASN1Sequence.getInstance(obj));
 
107
    }
 
108
 
 
109
    public DERUTF8String getCountryName()
 
110
    {
 
111
        return countryName;
 
112
    }
 
113
 
 
114
    public DERUTF8String getLocalityName()
 
115
    {
 
116
        return localityName;
 
117
    }
 
118
 
 
119
    public ASN1Sequence getPostalAddress()
 
120
    {
 
121
        return postalAddress;
 
122
    }
 
123
 
 
124
    /**
 
125
     * <pre>
 
126
     *   SignerLocation ::= SEQUENCE {
 
127
     *       countryName        [0] DirectoryString OPTIONAL,
 
128
     *       localityName       [1] DirectoryString OPTIONAL,
 
129
     *       postalAddress      [2] PostalAddress OPTIONAL }
 
130
     *
 
131
     *   PostalAddress ::= SEQUENCE SIZE(1..6) OF DirectoryString
 
132
     *   
 
133
     *   DirectoryString ::= CHOICE {
 
134
     *         teletexString           TeletexString (SIZE (1..MAX)),
 
135
     *         printableString         PrintableString (SIZE (1..MAX)),
 
136
     *         universalString         UniversalString (SIZE (1..MAX)),
 
137
     *         utf8String              UTF8String (SIZE (1.. MAX)),
 
138
     *         bmpString               BMPString (SIZE (1..MAX)) }
 
139
     * </pre>
 
140
     */
 
141
    public DERObject toASN1Object()
 
142
    {
 
143
        ASN1EncodableVector  v = new ASN1EncodableVector();
 
144
 
 
145
        if (countryName != null)
 
146
        {
 
147
            v.add(new DERTaggedObject(true, 0, countryName));
 
148
        }
 
149
 
 
150
        if (localityName != null)
 
151
        {
 
152
            v.add(new DERTaggedObject(true, 1, localityName));
 
153
        }
 
154
 
 
155
        if (postalAddress != null)
 
156
        {
 
157
            v.add(new DERTaggedObject(true, 2, postalAddress));
 
158
        }
 
159
 
 
160
        return new DERSequence(v);
 
161
    }
 
162
}
 
 
b'\\ No newline at end of file'