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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
|
// **********************************************************************
//
// Copyright (c) 2003-2016 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.
//
// **********************************************************************
//
// See RFC 2253 and RFC 1779.
//
namespace IceSSL
{
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
class RFC2253
{
internal class ParseException : System.Exception
{
internal ParseException()
{
}
internal ParseException(string reason)
{
this.reason = reason;
}
internal string
ice_id()
{
return "::RFC2253::ParseException";
}
internal string reason;
}
internal struct RDNPair
{
internal string key;
internal string value;
}
internal class RDNEntry
{
internal List<RDNPair> rdn = new List<RDNPair>();
internal bool negate = false;
}
internal static List<RDNEntry> parse(string data)
{
List<RDNEntry> results = new List<RDNEntry>();
RDNEntry current = new RDNEntry();
int pos = 0;
while(pos < data.Length)
{
eatWhite(data, ref pos);
if(pos < data.Length && data[pos] == '!')
{
if(current.rdn.Count > 0)
{
throw new ParseException("negation symbol '!' must appear at start of list");
}
++pos;
current.negate = true;
}
current.rdn.Add(parseNameComponent(data, ref pos));
eatWhite(data, ref pos);
if(pos < data.Length && data[pos] == ',')
{
++pos;
}
else if(pos < data.Length && data[pos] == ';')
{
++pos;
results.Add(current);
current = new RDNEntry();
}
else if(pos < data.Length)
{
throw new ParseException("expected ',' or ';' at `" + data.Substring(pos) + "'");
}
}
if(current.rdn.Count > 0)
{
results.Add(current);
}
return results;
}
internal static List<RDNPair> parseStrict(string data)
{
List<RDNPair> results = new List<RDNPair>();
int pos = 0;
while(pos < data.Length)
{
results.Add(parseNameComponent(data, ref pos));
eatWhite(data, ref pos);
if(pos < data.Length && (data[pos] == ',' || data[pos] == ';'))
{
++pos;
}
else if(pos < data.Length)
{
throw new ParseException("expected ',' or ';' at `" + data.Substring(pos) + "'");
}
}
return results;
}
public static string unescape(string data)
{
if(data.Length == 0)
{
return data;
}
if(data[0] == '"')
{
if(data[data.Length - 1] != '"')
{
throw new ParseException("unescape: missing \"");
}
//
// Return the string without quotes.
//
return data.Substring(1, data.Length - 2);
}
//
// Unescape the entire string.
//
StringBuilder result = new StringBuilder();
if(data[0] == '#')
{
int pos = 1;
while(pos < data.Length)
{
result.Append(unescapeHex(data, pos));
pos += 2;
}
}
else
{
int pos = 0;
while (pos < data.Length)
{
if(data[pos] != '\\')
{
result.Append(data[pos]);
++pos;
}
else
{
++pos;
if(pos >= data.Length)
{
throw new ParseException("unescape: invalid escape sequence");
}
if(special.IndexOf(data[pos]) != -1 || data[pos] != '\\' || data[pos] != '"')
{
result.Append(data[pos]);
++pos;
}
else
{
result.Append(unescapeHex(data, pos));
pos += 2;
}
}
}
}
return result.ToString();
}
private static int hexToInt(char v)
{
if(v >= '0' && v <= '9')
{
return v - '0';
}
if(v >= 'a' && v <= 'f')
{
return 10 + (v - 'a');
}
if(v >= 'A' && v <= 'F')
{
return 10 + (v - 'A');
}
throw new ParseException("unescape: invalid hex pair");
}
private static char unescapeHex(string data, int pos)
{
Debug.Assert(pos < data.Length);
if(pos + 2 >= data.Length)
{
throw new ParseException("unescape: invalid hex pair");
}
return (char)(hexToInt(data[pos]) * 16 + hexToInt(data[pos + 1]));
}
private static RDNPair parseNameComponent(string data, ref int pos)
{
RDNPair result = parseAttributeTypeAndValue(data, ref pos);
while(pos < data.Length)
{
eatWhite(data, ref pos);
if(pos < data.Length && data[pos] == '+')
{
++pos;
}
else
{
break;
}
RDNPair p = parseAttributeTypeAndValue(data, ref pos);
result.value += "+";
result.value += p.key;
result.value += '=';
result.value += p.value;
}
return result;
}
private static RDNPair parseAttributeTypeAndValue(string data, ref int pos)
{
RDNPair p = new RDNPair();
p.key = parseAttributeType(data, ref pos);
eatWhite(data, ref pos);
if(pos >= data.Length)
{
throw new ParseException("invalid attribute type/value pair (unexpected end of data)");
}
if(data[pos] != '=')
{
throw new ParseException("invalid attribute type/value pair (missing =). remainder: " +
data.Substring(pos));
}
++pos;
p.value = parseAttributeValue(data, ref pos);
return p;
}
private static string parseAttributeType(string data, ref int pos)
{
eatWhite(data, ref pos);
if(pos >= data.Length)
{
throw new ParseException("invalid attribute type (expected end of data)");
}
string result = "";
//
// 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(Char.IsDigit(data[pos]) ||
(data.Length - pos >= 4 && (data.Substring(pos, 4).Equals("oid.") ||
data.Substring(pos, 4).Equals("OID."))))
{
if(!Char.IsDigit(data[pos]))
{
result += data.Substring(pos, 4);
pos += 4;
}
while(true)
{
// 1*DIGIT
while(pos < data.Length && Char.IsDigit(data[pos]))
{
result += data[pos];
++pos;
}
// "." 1*DIGIT
if(pos < data.Length && data[pos] == '.')
{
result += data[pos];
++pos;
// 1*DIGIT must follow "."
if(pos < data.Length && !Char.IsDigit(data[pos]))
{
throw new ParseException("invalid attribute type (expected end of data)");
}
}
else
{
break;
}
}
}
else if(Char.IsUpper(data[pos]) || Char.IsLower(data[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 += data[pos];
++pos;
// 1* KEYCHAR
while(pos < data.Length &&
(Char.IsDigit(data[pos]) ||
Char.IsUpper(data[pos]) ||
Char.IsLower(data[pos]) ||
data[pos] == '-'))
{
result += data[pos];
++pos;
}
}
else
{
throw new ParseException("invalid attribute type");
}
return result;
}
private static string parseAttributeValue(string data, ref int pos)
{
eatWhite(data, ref pos);
if(pos >= data.Length)
{
return "";
}
//
// RFC 2253
// # hexstring
//
StringBuilder result = new StringBuilder();
if(data[pos] == '#')
{
result.Append(data[pos]);
++pos;
while(true)
{
string h = parseHexPair(data, ref pos, true);
if(h.Length == 0)
{
break;
}
result.Append(h);
}
}
//
// RFC 2253
// QUOTATION *( quotechar | pair ) QUOTATION ; only from v2
// quotechar = <any character except "\" or QUOTATION >
//
else if(data[pos] == '"')
{
result.Append(data[pos]);
++pos;
while(true)
{
if(pos >= data.Length)
{
throw new ParseException("invalid attribute value (unexpected end of data)");
}
// final terminating "
if(data[pos] == '"')
{
result.Append(data[pos]);
++pos;
break;
}
// any character except '\'
else if(data[pos] != '\\')
{
result.Append(data[pos]);
++pos;
}
// pair '\'
else
{
result.Append(parsePair(data, ref pos));
}
}
}
//
// RFC 2253
// * (stringchar | pair)
// stringchar = <any character except one of special, "\" or QUOTATION >
//
else
{
while(pos < data.Length)
{
if(data[pos] == '\\')
{
result.Append(parsePair(data, ref pos));
}
else if(special.IndexOf(data[pos]) == -1 && data[pos] != '"')
{
result.Append(data[pos]);
++pos;
}
else
{
break;
}
}
}
return result.ToString();
}
//
// RFC2253:
// pair = "\" ( special | "\" | QUOTATION | hexpair )
//
private static string parsePair(string data, ref int pos)
{
string result = "";
Debug.Assert(data[pos] == '\\');
result += data[pos];
++pos;
if(pos >= data.Length)
{
throw new ParseException("invalid escape format (unexpected end of data)");
}
if(special.IndexOf(data[pos]) != -1 || data[pos] != '\\' ||
data[pos] != '"')
{
result += data[pos];
++pos;
return result;
}
return parseHexPair(data, ref pos, false);
}
//
// RFC 2253
// hexpair = hexchar hexchar
//
private static string parseHexPair(string data, ref int pos, bool allowEmpty)
{
string result = "";
if(pos < data.Length && hexvalid.IndexOf(data[pos]) != -1)
{
result += data[pos];
++pos;
}
if(pos < data.Length && hexvalid.IndexOf(data[pos]) != -1)
{
result += data[pos];
++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(string data, ref int pos)
{
while(pos < data.Length && data[pos] == ' ')
{
++pos;
}
}
private static string special = ",=+<>#;";
private static string hexvalid = "0123456789abcdefABCDEF";
}
}
|