summaryrefslogtreecommitdiffstats
path: root/luni/src/main/java/org/apache/harmony/security/asn1/ObjectIdentifier.java
blob: 24140e5a24a4527534871ac9b484c3614e9a16cc (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
/*
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

/**
* @author Stepan M. Mishura
* @version $Revision$
*/

package org.apache.harmony.security.asn1;

import java.util.Arrays;

/**
 * Instance of this class represents ObjectIdentifier (OID).
 *
 * According to X.690:
 * OID is represented as a sequence of subidentifier.
 * Each subidentifier is represented as non negative integer value.
 * There are at least 2 subidentifiers in the sequence.
 *
 * Valid values for first subidentifier are 0, 1 and 2.
 * If the first subidentifier has 0 or 1 value the second
 * subidentifier must be less then 40.
 *
 * @see <a href="http://asn1.elibel.tm.fr/en/standards/index.htm">ASN.1</a>
 */
public final class ObjectIdentifier {

    /** OID as array of integers */
    private final int[] oid;

    /** OID as string */
    private String soid;

    /**
     * Creates ObjectIdentifier(OID) from array of integers.
     *
     * @param oid array of integers
     * @throws IllegalArgumentException if oid is invalid or null
     */
    public ObjectIdentifier(int[] oid) {
        validate(oid);
        this.oid = oid;
    }

    /**
     * Creates ObjectIdentifier(OID) from string representation.
     *
     * @param strOid oid string
     * @throws IllegalArgumentException if oid string is invalid or null
     */
    public ObjectIdentifier(String strOid) {
        this.oid = toIntArray(strOid);
        this.soid = strOid;
    }

    @Override public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || this.getClass() != o.getClass()) {
            return false;
        }
        return Arrays.equals(oid, ((ObjectIdentifier) o).oid);
    }

    @Override public String toString() {
        if (soid == null) {
            soid = toString(oid);
        }
        return soid;
    }

    @Override public int hashCode() {
        // FIXME change me to Arrays.hashCode(int[])
        int intHash = 0;
        for (int i = 0; i < oid.length && i < 4; i++) {
            intHash += oid[i] << (8 * i); //TODO what about to find better one?
        }
        return intHash & 0x7FFFFFFF; // only positive
    }

    /**
     * Validates ObjectIdentifier (OID).
     *
     * @param oid oid as array of integers
     * @throws IllegalArgumentException if oid is invalid or null
     */
    public static void validate(int[] oid) {
        if (oid == null) {
            throw new IllegalArgumentException("oid == null");
        }

        if (oid.length < 2) {
            throw new IllegalArgumentException("OID MUST have at least 2 subidentifiers");
        }

        if (oid[0] > 2) {
            throw new IllegalArgumentException(
                    "Valid values for first subidentifier are 0, 1 and 2");
        } else if (oid[0] != 2 && oid[1] > 39) {
            throw new IllegalArgumentException("If the first subidentifier has 0 or 1 value the "
                    + "second subidentifier value MUST be less than 40");
        }

        for (int anOid : oid) {
            if (anOid < 0) {
                throw new IllegalArgumentException("Subidentifier MUST have positive value");
            }
        }
    }

    /**
     * Returns string representation of OID.
     *
     * Note: it is supposed that passed array of integers
     * contains valid OID value, so no checks are performed.
     *
     * @param oid oid as array of integers
     * @return oid string representation
     */
    public static String toString(int[] oid) {
        StringBuilder sb = new StringBuilder(3 * oid.length);

        for (int i = 0; i < oid.length - 1; ++i) {
            sb.append(oid[i]);
            sb.append('.');
        }
        sb.append(oid[oid.length - 1]);
        return sb.toString();
    }

    /**
     * Gets ObjectIdentifier (OID) from string representation.
     *
     * String representation is defined by the following syntax:
     *     OID = subidentifier 1*("." subidentifier)
     *     subidentifier = 1*(digit)
     *
     * @param str string representation of OID
     * @return oid as array of integers
     * @throws IllegalArgumentException if oid string is invalid or null
     */
    public static int[] toIntArray(String str) {
        return toIntArray(str, true);
    }

    /**
     * Returns whether the given string is a valid ObjectIdentifier
     * (OID) representation.
     *
     * String representation is defined as for {@link #toIntArray}.
     *
     * @param str string representation of OID
     * @return true if oidString has valid syntax or false if not
     */
    public static boolean isOID(String str) {
        return toIntArray(str, false) != null;
    }

    /**
     * Gets ObjectIdentifier (OID) from string representation.
     *
     * String representation is defined by the following syntax:
     *     OID = subidentifier 1*("." subidentifier)
     *     subidentifier = 1*(digit)
     *
     * @param str string representation of OID
     * @return oid as array of integers or null if the oid string is
     * invalid or null and shouldThrow is false
     * @throws IllegalArgumentException if oid string is invalid or null and
     * shouldThrow is true
     */
    private static int[] toIntArray(String str, boolean shouldThrow) {
        if (str == null) {
            if (! shouldThrow) {
                return null;
            }
            throw new IllegalArgumentException();
        }

        int length = str.length();
        if (length == 0) {
            if (! shouldThrow) {
                return null;
            }
            throw new IllegalArgumentException("Incorrect syntax");
        }

        int count = 1; // number of subidentifiers
        boolean wasDot = true; // indicates whether char before was dot or not.
        char c; // current char
        for (int i = 0; i < length; i++) {
            c = str.charAt(i);
            if (c == '.') {
                if (wasDot) {
                    if (! shouldThrow) {
                        return null;
                    }
                    throw new IllegalArgumentException("Incorrect syntax");
                }
                wasDot = true;
                count++;
            } else if (c >= '0' && c <= '9') {
                wasDot = false;
            } else {
                if (! shouldThrow) {
                    return null;
                }
                throw new IllegalArgumentException("Incorrect syntax");
            }
        }

        if (wasDot) {
            // the last char is dot
            if (! shouldThrow) {
                return null;
            }
            throw new IllegalArgumentException("Incorrect syntax");
        }

        if (count < 2) {
            if (! shouldThrow) {
                return null;
            }
            throw new IllegalArgumentException("Incorrect syntax");
        }

        int[] oid = new int[count];
        for (int i = 0, j = 0; i < length; i++) {
            c = str.charAt(i);
            if (c == '.') {
                j++;
            } else {
                oid[j] = oid[j] * 10 + c - 48; // '0' = 48
            }
        }

        if (oid[0] > 2) {
            if (! shouldThrow) {
                return null;
            }
            throw new IllegalArgumentException("Incorrect syntax");
        } else if (oid[0] != 2 && oid[1] > 39) {
            if (! shouldThrow) {
                return null;
            }
            throw new IllegalArgumentException("Incorrect syntax");
        }

        return oid;
    }
}