summaryrefslogtreecommitdiffstats
path: root/pki/base/util/src/netscape/security/x509/RDN.java
blob: c5e8765a06e8d81e9c5e6f4a91f3ebf980d32b3c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
// --- BEGIN COPYRIGHT BLOCK ---
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; version 2 of the License.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
// (C) 2007 Red Hat, Inc.
// All rights reserved.
// --- END COPYRIGHT BLOCK ---
package netscape.security.x509;

import java.io.IOException;
import java.util.Enumeration;
import java.util.Vector;

import netscape.security.util.DerInputStream;
import netscape.security.util.DerOutputStream;
import netscape.security.util.DerValue;
import netscape.security.util.ObjectIdentifier;

/**
 * RDNs are a set of {attribute = value} assertions. Some of those
 * attributes are "distinguished" (unique w/in context). Order is
 * never relevant.
 * 
 * Some X.500 names include only a single distinguished attribute
 * per RDN. This style is currently common.
 * 
 * Note that DER-encoded RDNs sort AVAs by assertion OID ... so that
 * when we parse this data we don't have to worry about canonicalizing
 * it, but we'll need to sort them when we expose the RDN class more.
 * 
 * @see X500Name
 * @see AVA
 * @see LdapDNStrConverter
 */

public class RDN {
    // public constructors 

    /**
     * Constructs a RDN from a Ldap DN String with one RDN component
     * using the global default LdapDNStrConverter.
     * 
     * @see LdapDNStrConverter
     * @param rdnString a Ldap DN string with one RDN component, e.g. as
     *            defined in RFC1779.
     * @exception IOException if error occurs while parsing the string.
     */
    public RDN(String rdnString)
            throws IOException {
        RDN rdn = LdapDNStrConverter.getDefault().parseRDN(rdnString);
        assertion = rdn.getAssertion();
    }

    /**
     * Like RDN(String) with a DER encoding order given as argument for
     * Directory Strings.
     */
    public RDN(String rdnString, byte[] tags)
            throws IOException {
        RDN rdn = LdapDNStrConverter.getDefault().parseRDN(rdnString, tags);
        assertion = rdn.getAssertion();
    }

    /**
     * Constructs a RDN from a Ldap DN string with one RDN component
     * using the specified Ldap DN Str converter.
     * For example, RFC1779StrConverter can be passed to parse a Ldap
     * DN string in RFC1779 format.
     * 
     * @see LdapDNStrConverter
     * @param rdnString Ldap DN string.
     * @param ldapDNStrConverter a LdapDNStrConverter.
     */
    public RDN(String rdnString, LdapDNStrConverter ldapDNStrConverter)
            throws IOException {
        RDN rdn = ldapDNStrConverter.parseRDN(rdnString);
        assertion = rdn.getAssertion();
    }

    /**
     * Constructs a RDN from a DerValue.
     * 
     * @param set Der value of a set of AVAs.
     */
    public RDN(DerValue set) throws IOException {
        if (set.tag != DerValue.tag_Set)
            throw new CertParseError("X500 RDN");

        int j_max = 50; // XXX j_max = f(data)!!
        int j;
        int i;

        AVA[] avas = new AVA[j_max];

        // create a temporary array big enough for a huge set of AVA's
        for (j = 0; j < j_max; j++) {
            avas[j] = new AVA(set.data);
            if (set.data.available() == 0)
                break;
        }

        // copy the elements into it
        if (j >= j_max - 1) {
            assertion = new AVA[j + 1];
        } else {
            assertion = new AVA[j + 1];
            for (i = 0; i < (j + 1); i++) {
                assertion[i] = avas[i];
            }
        }

        /*
        if (set.data.available () != 0)
            // throw new CertParseError ("X500 RDN 2");
            System.out.println ("  ... RDN parse, ignored bytes = "
        	    + set.data.available ());
        */
    }

    /**
     * Constructs a RDN from a Der Input Stream.
     * 
     * @param in a Der Input Stream.
     */
    public RDN(DerInputStream in) throws IOException {
        /* an RDN is a SET of avas */
        DerValue avaset[] = in.getSet(1);
        int i;
        assertion = new AVA[avaset.length];
        for (i = 0; i < assertion.length; i++)
            assertion[i] = new AVA(avaset[i].data);
    }

    /**
     * Constructs a RDN from an array of AVA.
     * 
     * @param avas a AVA Array.
     */
    public RDN(AVA avas[]) {
        assertion = (AVA[]) avas.clone();
    }

    /**
     * convenience method.
     */
    public RDN(Vector<AVA> avaVector) {
        int size = avaVector.size();
        assertion = new AVA[size];
        for (int i = 0; i < size; i++) {
            assertion[i] = avaVector.elementAt(i);
        }
    }

    /**
     * returns an array of AVA in the RDN.
     * 
     * @return array of AVA in this RDN.
     */
    public AVA[] getAssertion() {
        return (AVA[]) assertion.clone();
    }

    /**
     * returns the number of AVAs in the RDN.
     * 
     * @return number of AVAs in this RDN.
     */
    public int getAssertionLength() {
        return assertion.length;
    }

    private AVA assertion[];

    private class AVAEnumerator implements Enumeration<AVA> {
        private int index;

        public AVAEnumerator() {
            index = 0;
        }

        public boolean hasMoreElements() {
            return (index < assertion.length);
        }

        public AVA nextElement() {
            if (index >= assertion.length)
                return null;
            return assertion[index++];
        }
    }

    // other public methods.

    /**
     * Checks if this RDN is the same as another by comparing the AVAs
     * in the RDNs.
     * 
     * @param other the other RDN.
     * @return true iff the other RDN is the same.
     */
    public boolean equals(RDN other) {
        int i;

        if (other == this)
            return true;
        if (assertion.length != other.assertion.length)
            return false;

        for (i = 0; i < assertion.length; i++)
            if (!assertion[i].equals(other.assertion[i]))
                return false;

        return true;
    }

    DerValue findAttribute(ObjectIdentifier oid) {
        int i;

        for (i = 0; i < assertion.length; i++)
            if (assertion[i].oid.equals(oid))
                return assertion[i].value;
        return null;
    }

    /**
     * Encodes this RDN to a Der output stream.
     * 
     * @param out the Der Output Stream.
     */
    public void encode(DerOutputStream out) throws IOException {
        DerOutputStream tmp = new DerOutputStream();
        int i;

        for (i = 0; i < assertion.length; i++)
            assertion[i].encode(tmp);
        out.write(DerValue.tag_Set, tmp);
    }

    /**
     * returns an enumeration of AVAs that make up this RDN.
     * 
     * @return an enumeration of AVAs that make up this RDN.
     */
    public Enumeration<AVA> getAVAs() {
        return new AVAEnumerator();
    }

    /**
     * Returns a Ldap DN string with one RDN component using the
     * global default LdapDNStrConverter.
     * 
     * @see LdapDNStrConverter
     * @return the Ldap DN String of this RDN.
     * @exception IOException if an error occurs during the conversion.
     */
    public String toLdapDNString()
            throws IOException {
        return LdapDNStrConverter.getDefault().encodeRDN(this);
    }

    /**
     * Returns a Ldap DN String with this RDN component using the specified
     * LdapDNStrConverter.
     * 
     * @see LdapDNStrConverter
     * @param ldapDNStrConverter a LdapDNStrConverter.
     * @return a Ldap DN String.
     * @exception IOException if an error occurs in the conversion.
     */
    public String toLdapDNString(LdapDNStrConverter ldapDNStrConverter)
            throws IOException {
        return ldapDNStrConverter.encodeRDN(this);
    }

    /**
     * Returns a Ldap DN string with this RDN component using the global
     * default LdapDNStrConverter.
     * 
     * @see LdapDNStrConverter
     * @return the Ldap DN String with this RDN component, null if an error
     *         occurs in the conversion.
     */
    public String toString() {
        String s;
        try {
            s = toLdapDNString();
        } catch (IOException e) {
            return null;
        }
        return s;
    }

}