-
Notifications
You must be signed in to change notification settings - Fork 1
/
dn_string.c
166 lines (156 loc) · 3.15 KB
/
dn_string.c
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
#include <ctype.h>
#include "x509cert.h"
#define LEN(a) (sizeof(a) / sizeof((a)[0]))
size_t
x509cert_dn_string_rdn_len(const char *s)
{
size_t len;
if (!*s)
return 0;
len = 1;
for (; *s; ++s) {
switch (*s) {
case '\\': if (s[1]) ++s; break;
case ',': ++len; break;
}
}
return len;
}
static const unsigned char *
keyword(const char *str, size_t len)
{
static const struct {
char key[7];
const unsigned char *oid;
} keywords[] = {
{"CN", x509cert_oid_CN},
{"L", x509cert_oid_L},
{"ST", x509cert_oid_ST},
{"O", x509cert_oid_O},
{"OU", x509cert_oid_OU},
{"C", x509cert_oid_C},
{"STREET", x509cert_oid_STREET},
{"DC", x509cert_oid_DC},
{"UID", x509cert_oid_UID},
};
for (size_t i = 0; i < LEN(keywords); ++i) {
if (strncmp(keywords[i].key, str, len) == 0 && !keywords[i].key[len])
return keywords[i].oid;
}
return NULL;
}
static int
hexval(int c)
{
if ('0' <= c && c <= '9')
return c - '0';
switch (c) {
case 'A': case 'a': return 10;
case 'B': case 'b': return 11;
case 'C': case 'c': return 12;
case 'D': case 'd': return 13;
case 'E': case 'e': return 14;
case 'F': case 'f': return 15;
}
return -1;
}
static int
hexpair(const char *s)
{
int n1, n2;
if ((n1 = hexval(s[0])) == -1 || (n2 = hexval(s[1])) == -1)
return -1;
return n1 << 4 | n2;
}
int
x509cert_parse_dn_string(struct x509cert_rdn *rdn, char *s)
{
const char *key;
unsigned char *buf = (unsigned char *)s, *oid, root;
unsigned long sub;
int i, j, n, space;
while (*s) {
rdn->oid = NULL;
if (isalpha((unsigned char)*s)) {
key = s;
while (isalnum((unsigned char)*++s) || *s == '-')
;
rdn->oid = keyword(key, s - key);
} else if (isdigit((unsigned char)*s)) {
/* parse numeric OID */
for (i = 0;; ++i, ++s) {
for (sub = 0; '0' <= *s && *s <= '9'; ++s)
sub = sub * 10 + *s - '0';
switch (i) {
case 0:
if (sub > 3)
return 0;
root = sub;
break;
case 1:
rdn->oid = oid = buf;
if (root < 3 && sub > 39)
return 0;
*buf++ = X509CERT_ASN1_OID;
*buf++ = 1;
*buf++ = 40 * root + sub;
break;
default:
for (j = 1; sub >> 7 * j; ++j)
;
if (0xff - oid[1] < j)
return 0;
oid[1] += j;
while (--j)
*buf++ = 0x80 | (sub >> 7 * j & 0x7f);
*buf++ = sub & 0x7f;
}
if (*s != '.')
break;
}
}
if (!rdn->oid || *s != '=')
return 0;
++s;
rdn->val.val = buf;
rdn->val.enc = NULL;
switch (*s) {
case ' ':
return 0;
case '#':
rdn->val.tag = 0;
++s;
while ((n = hexpair(s)) != -1)
*buf++ = n, s += 2;
break;
default:
rdn->val.tag = X509CERT_ASN1_UTF8STRING;
space = 0;
while (*s && *s != ',') {
if (strchr("\"+;<>", *s))
return 0;
space = *s == ' ';
if (*s == '\\') {
n = hexpair(++s);
if (n != -1) {
*buf++ = n;
s += 2;
continue;
}
if (!strchr("\\\"+,;<> #=", *s))
return 0;
}
*buf++ = *s++;
}
if (space)
return 0;
}
rdn->val.len = buf - (unsigned char *)rdn->val.val;
++rdn;
/* multi-valued RDNs are not supported; assume no '+' */
if (*s != ',')
break;
++s;
}
return !*s;
}