summaryrefslogtreecommitdiff
path: root/java/ssl/jdk1.4/IceSSL/RFC2253.java
blob: d7034a9027bfc5e888fef9df7c479cfb7bfa38ad (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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
// **********************************************************************
//
// Copyright (c) 2003-2007 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************

package IceSSL;

//
// See RFC 2253 and RFC 1779.
//
class RFC2253
{
    static class ParseException extends Ice.LocalException
    {
	public ParseException()
	{
	}

	public ParseException(String reason)
	{
	    this.reason = reason;
	}

	public String
	ice_name()
	{
	    return "RFC2253::ParseException";
	}

	public String reason;
    }

    static class RDNPair
    {
	String key;
	String value;
    }

    static private class ParseState
    {
	String data;
	int pos;
    }

    public static java.util.List
    parse(String data)
	throws ParseException
    {
	java.util.List results = new java.util.LinkedList();
	java.util.List current = new java.util.LinkedList();
	ParseState state = new ParseState();
	state.data = data;
	state.pos = 0;
	while(state.pos < state.data.length())
	{
	    current.add(parseNameComponent(state));
	    eatWhite(state);
	    if(state.pos < state.data.length() && state.data.charAt(state.pos) == ',')
	    {
		++state.pos;
	    }
	    else if(state.pos < state.data.length() && state.data.charAt(state.pos) == ';')
	    {
		++state.pos;
		results.add(current);
		current = new java.util.LinkedList();
	    }
	    else if(state.pos < state.data.length())
	    {
		throw new ParseException("expected ',' or ';' at `" + state.data.substring(state.pos) + "'");
	    }
	}
	if(!current.isEmpty())
	{
	    results.add(current);
	}

	return results;
    }

    public static java.util.List
    parseStrict(String data)
	throws ParseException
    {
	java.util.List results = new java.util.LinkedList();
	ParseState state = new ParseState();
	state.data = data;
	state.pos = 0;
	while(state.pos < state.data.length())
	{
	    results.add(parseNameComponent(state));
	    eatWhite(state);
	    if(state.pos < state.data.length() &&
	       (state.data.charAt(state.pos) == ',' || state.data.charAt(state.pos) == ';'))
	    {
		++state.pos;
	    }
	    else if(state.pos < state.data.length())
	    {
		throw new ParseException("expected ',' or ';' at `" + state.data.substring(state.pos) + "'");
	    }
	}
	return results;
    }

    private static RDNPair
    parseNameComponent(ParseState state)
	throws ParseException
    {
	RDNPair result = parseAttributeTypeAndValue(state);
	while(state.pos < state.data.length())
	{
	    eatWhite(state);
	    if(state.pos < state.data.length() && state.data.charAt(state.pos) == '+')
	    {
		++state.pos;
	    }
	    else
	    {
		break;
	    }
	    RDNPair p = parseAttributeTypeAndValue(state);
	    result.value += "+";
	    result.value += p.key;
	    result.value += '=';
	    result.value += p.value;
	}
	return result;
    }

    private static RDNPair
    parseAttributeTypeAndValue(ParseState state)
	throws ParseException
    {
	RDNPair p = new RDNPair();
	p.key = parseAttributeType(state);
	eatWhite(state);
	if(state.pos >= state.data.length())
	{
	    throw new ParseException("invalid attribute type/value pair (unexpected end of state.data)");
	}
	if(state.data.charAt(state.pos) != '=')
	{
	    throw new ParseException("invalid attribute type/value pair (missing =)");
	}
	++state.pos;
	p.value = parseAttributeValue(state);
	return p;
    }

    private static String
    parseAttributeType(ParseState state)
	throws ParseException
    {
	eatWhite(state);
	if(state.pos >= state.data.length())
	{
	    throw new ParseException("invalid attribute type (expected end of state.data)");
	}

	String result = new String();

	//
	// RFC 1779.
	// <key> ::= 1*( <keychar> ) | "OID." <oid> | "oid." <oid>
	// <oid> ::= <digitString> | <digitstring> "." <oid>
	// RFC 2253:
	// attributeType = (ALPHA 1*keychar) | oid
	// keychar    = ALPHA | DIGIT | "-"
	// oid        = 1*DIGIT *("." 1*DIGIT)
	//
	// In section 4 of RFC 2253 the document says:
	// Implementations MUST allow an oid in the attribute type to be
	// prefixed by one of the character Strings "oid." or "OID.".
	//
	// Here we must also check for "oid." and "OID." before parsing
	// according to the ALPHA KEYCHAR* rule.
	// 
	// First the OID case.
	//
	if(Character.isDigit(state.data.charAt(state.pos)) ||
	   (state.data.length() - state.pos >= 4 && (state.data.substring(state.pos, state.pos + 4) == "oid." ||
						   state.data.substring(state.pos, state.pos + 4) == "OID.")))
	{
	    if(!Character.isDigit(state.data.charAt(state.pos)))
	    {
		result += state.data.substring(state.pos, state.pos + 4);
		state.pos += 4;
	    }

	    while(true)
	    {
		// 1*DIGIT
		while(state.pos < state.data.length() && Character.isDigit(state.data.charAt(state.pos)))
		{
		    result += state.data.charAt(state.pos);
		    ++state.pos;
		}
		// "." 1*DIGIT
		if(state.pos < state.data.length() && state.data.charAt(state.pos) == '.')
		{
		    result += state.data.charAt(state.pos);
		    ++state.pos;
		    // 1*DIGIT must follow "."
		    if(state.pos < state.data.length() && !Character.isDigit(state.data.charAt(state.pos)))
		    {
			throw new ParseException("invalid attribute type (expected end of state.data)");
		    }
		}
		else
		{
		    break;
		}
	    }
	}
	else if(Character.isUpperCase(state.data.charAt(state.pos)) ||
		Character.isLowerCase(state.data.charAt(state.pos)))
	{
	    //
	    // The grammar is wrong in this case. It should be ALPHA
	    // KEYCHAR* otherwise it will not accept "O" as a valid
	    // attribute type.
	    //
	    result += state.data.charAt(state.pos);
	    ++state.pos;
	    // 1* KEYCHAR
	    while(state.pos < state.data.length() &&
		  (Character.isDigit(state.data.charAt(state.pos)) ||
		   Character.isUpperCase(state.data.charAt(state.pos)) ||
		   Character.isLowerCase(state.data.charAt(state.pos)) ||
		   state.data.charAt(state.pos) == '-'))
	    {
		result += state.data.charAt(state.pos);
		++state.pos;
	    }
	}
	else
	{
	    throw new ParseException("invalid attribute type");
	}
	return result;
    }

    private static String
    parseAttributeValue(ParseState state)
	throws ParseException
    {
	eatWhite(state);
	String result = new String();
	if(state.pos >= state.data.length())
	{
	    return result;
	}

	//
	// RFC 2253
	// # hexString
	//
	if(state.data.charAt(state.pos) == '#')
	{
	    result += state.data.charAt(state.pos);
	    ++state.pos;
	    while(true)
	    {
		String h = parseHexPair(state, true);
		if(h.length() == 0)
		{
		    break;
		}
		result += h;
	    }
	}
	//
	// RFC 2253
	// QUOTATION *( quotechar | pair ) QUOTATION ; only from v2
	// quotechar     = <any character except "\" or QUOTATION >
	//
	else if(state.data.charAt(state.pos) == '"')
	{
	    result += state.data.charAt(state.pos);
	    ++state.pos;
	    while(true)
	    {
		if(state.pos >= state.data.length())
		{
		    throw new ParseException("invalid attribute value (unexpected end of state.data)");
		}
		// final terminating "
		if(state.data.charAt(state.pos) == '"')
		{
		    result += state.data.charAt(state.pos);
		    ++state.pos;
		    break;
		}
		// any character except '\'
		else if(state.data.charAt(state.pos) != '\\')
		{
		    result += state.data.charAt(state.pos);
		    ++state.pos;
		}
		// pair '\'
		else
		{
		    result += parsePair(state);
		}
	    }
	}
	//
	// RFC 2253
	// * (Stringchar | pair)
	// Stringchar = <any character except one of special, "\" or QUOTATION >
	//
	else
	{
	    while(state.pos < state.data.length())
	    {
		if(state.data.charAt(state.pos) == '\\')
		{
		    result += parsePair(state);
		}
		else if(special.indexOf(state.data.charAt(state.pos)) == -1 && state.data.charAt(state.pos) != '"')
		{
		    result += state.data.charAt(state.pos);
		    ++state.pos;
		}
		else
		{
		    break;
		}
	    }
	}
	return result;
    }

    //
    // RFC2253:
    // pair       = "\" ( special | "\" | QUOTATION | hexpair )
    //
    private static String
    parsePair(ParseState state)
	throws ParseException
    {
	String result = new String();

	assert(state.data.charAt(state.pos) == '\\');
	result += state.data.charAt(state.pos);
	++state.pos;

	if(state.pos >= state.data.length())
	{
	    throw new ParseException("invalid escape format (unexpected end of state.data)");
	}

	if(special.indexOf(state.data.charAt(state.pos)) != -1 || state.data.charAt(state.pos) != '\\' ||
	   state.data.charAt(state.pos) != '"')
	{
	    result += state.data.charAt(state.pos);
	    ++state.pos;
	    return result;
	}
	return parseHexPair(state, false);
    }

    //
    // RFC 2253
    // hexpair    = hexchar hexchar
    //
    private static String
    parseHexPair(ParseState state, boolean allowEmpty)
	throws ParseException
    {
	String result = new String();
	if(state.pos < state.data.length() && hexvalid.indexOf(state.data.charAt(state.pos)) != -1)
	{
	    result += state.data.charAt(state.pos);
	    ++state.pos;
	}
	if(state.pos < state.data.length() && hexvalid.indexOf(state.data.charAt(state.pos)) != -1)
	{
	    result += state.data.charAt(state.pos);
	    ++state.pos;
	}
	if(result.length() != 2)
	{
	    if(allowEmpty && result.length() == 0)
	    {
		return result;
	    }
	    throw new ParseException("invalid hex format");
	}
	return result;
    }

    //
    // RFC 2253:
    //
    // Implementations MUST allow for space (' ' ASCII 32) characters to be
    // present between name-component and ',', between attributeTypeAndValue
    // and '+', between attributeType and '=', and between '=' and
    // attributeValue.  These space characters are ignored when parsing.
    //
    private static void
    eatWhite(ParseState state)
    {
	while(state.pos < state.data.length() && state.data.charAt(state.pos) == ' ')
	{
	    ++state.pos;
	}
    }

    private final static String special = ",=+<>#;";
    private final static String hexvalid = "0123456789abcdefABCDEF";
}