summaryrefslogtreecommitdiff
path: root/cpp/src/IceSSL/RFC2253.cpp
blob: 2dda9d67cd7dffa5aac756798968e1561f75d650 (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
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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
// **********************************************************************
//
// Copyright (c) 2003-2015 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.
//
// **********************************************************************

#include <IceUtil/StringUtil.h>

#include <IceSSL/Plugin.h>
#include <IceSSL/RFC2253.h>
#include <Ice/Object.h>

using namespace std;
using namespace IceSSL;

namespace
{

//
// See RFC 2253 and RFC 1779.
//

string special = ",=+<>#;";
string hexvalid = "0123456789abcdefABCDEF";

}

static char unescapeHex(const string&, size_t);
static pair<string,string> parseNameComponent(const string&, size_t&);
static pair<string,string> parseAttributeTypeAndValue(const string&, size_t&);
static string parseAttributeType(const string&, size_t&);
static string parseAttributeValue(const string&, size_t&);
static string parsePair(const string&, size_t&);
static string parseHexPair(const string&, size_t&, bool);
static void eatWhite(const string&, size_t&);

IceSSL::RFC2253::RDNEntrySeq
IceSSL::RFC2253::parse(const string& data)
{
    RDNEntrySeq results;
    RDNEntry current;
    current.negate = false;
    size_t pos = 0;
    while(pos < data.size())
    {
        eatWhite(data, pos);
        if(pos < data.size() && data[pos] == '!')
        {
            if(!current.rdn.empty())
            {
                throw ParseException(__FILE__, __LINE__, "negation symbol '!' must appear at start of list");
            }
            ++pos;
            current.negate = true;
        }
        current.rdn.push_back(parseNameComponent(data, pos));
        eatWhite(data, pos);
        if(pos < data.size() && data[pos] == ',')
        {
            ++pos;
        }
        else if(pos < data.size() && data[pos] == ';')
        {
            ++pos;
            results.push_back(current);
            current.rdn.clear();
            current.negate = false;
        }
        else if(pos < data.size())
        {
            throw ParseException(__FILE__, __LINE__, "expected ',' or ';' at `" + data.substr(pos) + "'");
        }
    }
    if(!current.rdn.empty())
    {
        results.push_back(current);
    }

    return results;
}

IceSSL::RFC2253::RDNSeq
IceSSL::RFC2253::parseStrict(const string& data)
{
    RDNSeq results;
    size_t pos = 0;
    while(pos < data.size())
    {
        results.push_back(parseNameComponent(data, pos));
        eatWhite(data, pos);
        if(pos < data.size() && (data[pos] == ',' || data[pos] == ';'))
        {
            ++pos;
        }
        else if(pos < data.size())
        {
            throw ParseException(__FILE__, __LINE__, "expected ',' or ';' at `" + data.substr(pos) + "'");
        }
    }
    return results;
}

string
IceSSL::RFC2253::unescape(const string& data)
{
    if(data.size() == 0)
    {
        return data;
    }

    if(data[0] == '"')
    {
        if(data[data.size() - 1] != '"')
        {
            throw ParseException(__FILE__, __LINE__, "unescape: missing \"");
        }

        //
        // Return the string without quotes.
        //
        return data.substr(1, data.size() - 2);
    }

    //
    // Unescape the entire string.
    //
    string result;
    if(data[0] == '#')
    {
        size_t pos = 1;
        while(pos < data.size())
        {
            result += unescapeHex(data, pos);
            pos += 2;
        }
    }
    else
    {
        size_t pos = 0;
        while(pos < data.size())
        {
            if(data[pos] != '\\')
            {
                result += data[pos];
                ++pos;
            }
            else
            {
                ++pos;
                if(pos >= data.size())
                {
                    throw ParseException(__FILE__, __LINE__, "unescape: invalid escape sequence");
                }
                if(special.find(data[pos]) != string::npos || data[pos] != '\\' || data[pos] != '"')
                {
                    result += data[pos];
                    ++pos;
                }
                else
                {
                    result += unescapeHex(data, pos);
                    pos += 2;
                }
            }
        }
    }

    return result;
}

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 ParseException(__FILE__, __LINE__, "unescape: invalid hex pair");
    return 0; // To satisfy the compiler.
}

static char
unescapeHex(const string& data, size_t pos)
{
    assert(pos < data.size());
    if(pos + 2 >= data.size())
    {
        throw ParseException(__FILE__, __LINE__, "unescape: invalid hex pair");
    }
    return static_cast<char>(hexToInt(data[pos]) * 16 + hexToInt(data[pos + 1]));
}

static pair<string,string>
parseNameComponent(const string& data, size_t& pos)
{
    pair<string, string> final = parseAttributeTypeAndValue(data, pos);
    while(pos < data.size())
    {
        eatWhite(data, pos);
        if(pos < data.size() && data[pos] == '+')
        {
            ++pos;
        }
        else
        {
            break;
        }
        pair<string, string> p = parseAttributeTypeAndValue(data, pos);
        final.second += "+";
        final.second += p.first;
        final.second += '=';
        final.second += p.second;
    }
    return final;
}

static pair<string,string>
parseAttributeTypeAndValue(const string& data, size_t& pos)
{
    pair<string, string> p;
    p.first = parseAttributeType(data, pos);
    eatWhite(data, pos);

    if(pos >= data.size())
    {
        throw ParseException(__FILE__, __LINE__, "invalid attribute type/value pair (unexpected end of data)");
    }
    if(data[pos] != '=')
    {
        throw ParseException(__FILE__, __LINE__, "invalid attribute type/value pair (missing =)");
    }
    ++pos;
    p.second = parseAttributeValue(data, pos);
    return p;
}

static string
parseAttributeType(const string& data, size_t& pos)
{
    eatWhite(data, pos);
    if(pos >= data.size())
    {
        throw ParseException(__FILE__, __LINE__, "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(IceUtilInternal::isDigit(data[pos]) ||
       (data.size() - pos >= 4 && (data.substr(pos, 4) == "oid." || data.substr(pos, 4) == "OID.")))
    {
        if(!IceUtilInternal::isDigit(data[pos]))
        {
            result += data.substr(pos, 4);
            pos += 4;
        }

        while(true)
        {
            // 1*DIGIT
            while(pos < data.size() && IceUtilInternal::isDigit(data[pos]))
            {
                result += data[pos];
                ++pos;
            }
            // "." 1*DIGIT
            if(pos < data.size() && data[pos] == '.')
            {
                result += data[pos];
                ++pos;
                // 1*DIGIT must follow "."
                if(pos < data.size() && !IceUtilInternal::isDigit(data[pos]))
                {
                    throw ParseException(__FILE__, __LINE__, "invalid attribute type (expected end of data)");
                }
            }
            else
            {
                break;
            }
        }
    }
    else if(IceUtilInternal::isAlpha(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.size() && 
              (IceUtilInternal::isAlpha(data[pos]) || IceUtilInternal::isDigit(data[pos]) || data[pos] == '-'))
        {
            result += data[pos];
            ++pos;
        }
    }
    else
    {
        throw ParseException(__FILE__, __LINE__, "invalid attribute type");
    }
    return result;
}

static string
parseAttributeValue(const string& data, size_t& pos)
{
    eatWhite(data, pos);
    string result;
    if(pos >= data.size())
    {
        return result;
    }

    //
    // RFC 2253
    // # hexstring
    //
    if(data[pos] == '#')
    {
        result += data[pos];
        ++pos;
        while(true)
        {
            string h = parseHexPair(data, pos, true);
            if(h.size() == 0)
            {
                break;
            }
            result += h;
        }
    }
    //
    // RFC 2253
    // QUOTATION *( quotechar | pair ) QUOTATION ; only from v2
    // quotechar     = <any character except "\" or QUOTATION >
    //
    else if(data[pos] == '"')
    {
        result += data[pos];
        ++pos;
        while(true)
        {
            if(pos >= data.size())
            {
                throw ParseException(__FILE__, __LINE__, "invalid attribute value (unexpected end of data)");
            }
            // final terminating "
            if(data[pos] == '"')
            {
                result += data[pos];
                ++pos;
                break;
            }
            // any character except '\'
            else if(data[pos] != '\\')
            {
                result += data[pos];
                ++pos;
            }
            // pair '\'
            else
            {
                result += parsePair(data, pos);
            }
        }
    }
    //
    // RFC 2253
    // * (stringchar | pair)
    // stringchar = <any character except one of special, "\" or QUOTATION >
    //
    else
    {
        while(pos < data.size())
        {
            if(data[pos] == '\\')
            {
                result += parsePair(data, pos);
            }
            else if(special.find(data[pos]) == string::npos && data[pos] != '"')
            {
                result += data[pos];
                ++pos;
            }
            else
            {
                break;
            }
        }
    }
    return result;
}

//
// RFC2253:
// pair       = "\" ( special | "\" | QUOTATION | hexpair )
//
static string
parsePair(const string& data, size_t& pos)
{
    string result;

    assert(data[pos] == '\\');
    result += data[pos];
    ++pos;

    if(pos >= data.size())
    {
        throw ParseException(__FILE__, __LINE__, "invalid escape format (unexpected end of data)");
    }

    if(special.find(data[pos]) != string::npos || data[pos] != '\\' || data[pos] != '"')
    {
        result += data[pos];
        ++pos;
        return result;
    }
    return parseHexPair(data, pos, false);
}
    
//
// RFC 2253
// hexpair    = hexchar hexchar
//
static string
parseHexPair(const string& data, size_t& pos, bool allowEmpty)
{
    string result;
    if(pos < data.size() && hexvalid.find(data[pos]) != string::npos)
    {
        result += data[pos];
        ++pos;
    }
    if(pos < data.size() && hexvalid.find(data[pos]) != string::npos)
    {
        result += data[pos];
        ++pos;
    }
    if(result.size() != 2)
    {
        if(allowEmpty && result.size() == 0)
        {
            return result;
        }
        throw ParseException(__FILE__, __LINE__, "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.
//
static void
eatWhite(const string& data, size_t& pos)
{
    while(pos < data.size() && data[pos] == ' ')
    {
        ++pos;
    }
}

#ifdef never
void
print(const list< list<pair<string, string> > >& r)
{
    if(r.size() > 1)
    {
        cout << "result: " << r.size() << " DNs" << endl;
    }
    for(list< list<pair<string, string> > >::const_iterator q = r.begin(); q != r.end(); ++q)
    {
        list<pair<string, string> > l = *q;
        cout << "result: " << l.size() << " RDNs" << endl;
        for(list<pair<string, string> >::const_iterator p = l.begin(); p != l.end(); ++p)
        {
            cout << "\t\"" << p->first << "\"=\"" << p->second << "\"" << endl;
        }
    }
}

int
main()
{
    string examples[] = {
        "CN=Steve Kille,O=Isode Limited,C=GB",
        "OU=Sales+CN=J. Smith,O=Widget Inc.,C=US",
        "CN=L. Eagle,O=Sue\\, Grabbit and Runn,C=GB",
        "CN=Before\\0DAfter,O=Test,C=GB",
        "1.3.6.1.4.1.1466.0=#04024869,O=Test,C=GB",
        "SN=Lu\\C4\\8Di\\C4\\87",
    };
    try
    {
        for(int i = 0; i < sizeof(examples)/sizeof(examples[0]); ++i)
        {
            cout << "string: " << examples[i] << endl;
            print(RFC2253::parse(examples[i]));
        }
    }
    catch(const RFC2253::ParseException& e)
    {
        cout << "error: " << e.reason << endl;
    }
}
#endif