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
|
/* v3_asid.c */
/* $Id$ */
/*
* Initial attempt to implement RFC 3779 section 3. I'd be very
* surprised if this even compiled yet, as I'm still figuring out
* OpenSSL's ASN.1 template goop.
*/
#include <stdio.h>
#include <assert.h>
#include "cryptlib.h"
#include <openssl/conf.h>
#include <openssl/asn1.h>
#include <openssl/asn1t.h>
#include <openssl/x509v3.h>
/* RFC 3779 AS ID */
ASN1_SEQUENCE(ASRange) = {
ASN1_SIMPLE(ASRange, min, ASN1_INTEGER),
ASN1_SIMPLE(ASRange, max, ASN1_INTEGER)
} ASN1_SEQUENCE_END(ASRange)
ASN1_CHOICE(ASIdOrRange) = {
ASN1_SIMPLE(ASIdOrRange, u.id, ASN1_INTEGER),
ASN1_SIMPLE(ASIdOrRange, u.range, ASRange)
} ASN1_CHOICE_END(ASIdOrRange)
ASN1_CHOICE(ASIdentiferChoice) = {
ASN1_IMP(ASIdentiferChoice, u.inherit, ASN1_NULL),
ASN1_IMP_SEQUENCE_OF(ASIdentiferChoice, u.asIdsOrRanges, ASIdOrRange)
} ASN1_CHOICE_END(ASIdentiferChoice)
ASN1_SEQUENCE(ASIdentifiers) = {
ASN1_EXP_OPT(ASIdentifiers, asnum, ASIdentiferChoice, 0),
ASN1_EXP_OPT(ASIdentifiers, rdi, ASIdentiferChoice, 1)
} ASN1_SEQUENCE_END(ASIdentifiers)
IMPLEMENT_ASN1_FUNCTIONS(ASRange)
IMPLEMENT_ASN1_FUNCTIONS(ASIdOrRange)
IMPLEMENT_ASN1_FUNCTIONS(ASIdentiferChoice)
IMPLEMENT_ASN1_FUNCTIONS(ASIdentifiers)
/*
* Write human-readable dump of ASIdentifiers extension.
* ASIdentifiers is just a wrapper for two ASIdentifierChoices, so we
* do almost all the work in i2r_ASIdentifierChoice().
*/
static int i2r_ASIdentifierChoice(BIO *out, ASIdentiferChoice *choice, int indent, const char *msg)
{
int i;
char *s;
if (choice == NULL)
return 1;
BIO_printf(out, "%*s%s: ", indent, "", msg);
switch (choice->type) {
case ASIdentifierChoice_inherit:
BIO_puts(out, "inherit");
break;
case ASIdentifierChoice_asIdsOrRanges:
for (i = 0; i < sk_ASIdOrRange_num(choice->u.asIdsOrRanges); i++) {
ASIdOrRange *aor = sk_ASIdOrRange_value(choice->u.asIdsOrRanges, i);
if (i > 0)
BIO_puts(out, ", ");
switch (aor->type) {
case ASIdOrRange_id:
if ((s = i2s_ASN1_INTEGER(NULL, aor->u.id)) == NULL)
return 0;
BIO_puts(out, s);
OPENSSL_free(s);
break;
case ASIdOrRange_range:
if ((s = i2s_ASN1_INTEGER(NULL, aor->u.range->min)) == NULL)
return 0;
BIO_puts(out, s);
OPENSSL_free(s);
BIO_puts(out, " - ");
if ((s = i2s_ASN1_INTEGER(NULL, aor->u.range->max)) == NULL)
return 0;
BIO_puts(out, s);
OPENSSL_free(s);
break;
default:
return 0;
}
}
break;
default:
return 0;
}
BIO_puts(out, "\n");
return 1;
}
static int i2r_ASIdentifiers(X509V3_EXT_METHOD *method, void *ext, BIO *out, int indent)
{
ASIdentifiers *asid = ext;
return (i2r_ASIdentifierChoice(out, asid->asnum, indent, "Autonomous System Numbers") &&
i2r_ASIdentifierChoice(out, asid->rdi, indent, "Routing Domain Identifiers"));
}
/*
* Comparision function for stack sorting.
*/
static int ASIdOrRange_cmp(const ASIdOrRange * const *a,
const ASIdOrRange * const *b)
{
assert((a->type == ASIdOrRange_id && a->u.id != NULL) ||
(a->type == ASIdOrRange_range && a->u.range != NULL &&
a->u.range->min != NULL && a->u.range->max != NULL));
assert((b->type == ASIdOrRange_id && b->u.id != NULL) ||
(b->type == ASIdOrRange_range && b->u.range != NULL &&
b->u.range->min != NULL && b->u.range->max != NULL));
if (a->type == ASIdOrRange_id && b->type == ASIdOrRange_id)
return ASN1_INTEGER_cmp(a->u.id, b->u.id);
if (a->type == ASIdOrRange_range && b->type == ASIdOrRange_range) {
int r = ASN1_INTEGER_cmp(a->u.range->min, b->u.range->min);
return r != 0 ? r : ASN1_INTEGER_cmp(a->u.range->max, b->u.range->max);
}
if (a->type == ASIdOrRange_id)
return ASN1_INTEGER_cmp(a->u.id, b->u.range->min);
else
return ASN1_INTEGER_cmp(a->u.range->min, b->u.id);
}
/*
* Some of the following helper routines might want to become globals eventually.
*/
static int asid_add_inherit(ASIdentifierChoice **choice)
{
if (*choice == NULL) {
if ((*choice = ASIdentifierChoice_new()) == NULL)
return 0;
memset(*choice, 0, sizeof(**choice));
if (((*choice)->u.inherit = ASN1_NULL_new()) == NULL)
return 0;
(*choice)->type = ASIdentifierChoice_inherit;
}
return (*choice)->type == ASIdentifierChoice_inherit;
}
static int asid_add_id_or_range(ASIdentifierChoice **choice, ASN1_INTEGER *min, ASN1_INTEGER *max)
{
ASIdOrRange *aor;
if (*choice != NULL && (*choice)->type == ASIdentifierChoice_inherit)
return 0;
if (*choice == NULL) {
if ((*choice = ASIdentifierChoice_new()) == NULL)
return 0;
memset(*choice, 0, sizeof(**choice));
if (((*choice)->u.asIdsOrRanges = sk_ASIdOrRange_new(ASIdOrRange_cmp)) == NULL)
return 0;
(*choice)->type = ASIdentifierChoice_asIdsOrRanges;
}
if ((aor = ASIdOrRange_new()) == NULL)
return 0;
memset(aor, 0, sizeof(*aor));
if (max == NULL) {
aor->type = ASIdOrRange_id;
aor->u.id = min;
} else {
aor->type = ASIdOrRange_range;
if ((aor->u.range = ASRange_new()) == NULL)
goto err;
aor->u.range->min = min;
aor->u.range->max = max;
}
if (!(sk_ASIdOrRange_push((*choice)->u.asIdsOrRanges, aor)))
goto err;
return 1;
err:
if (aor->u.range != NULL)
ASRange_free(aor->u.range);
ASIdOrRange_free(aor);
return 0;
}
static void asid_canonize(ASIdentifierChoice *choice)
{
int i;
/*
* Nothing to do for empty element or inheritance.
*/
if (choice == NULL || choice->type == ASIdentifierChoice_inherit)
return 1;
/*
* We have a list. Sort it.
*/
assert(choice->type == ASIdentifierChoice_asIdsOrRanges);
sk_ASIdOrRange_sort(choice->u.asIdsOrRanges);
/*
* Now resolve any duplicates or overlaps.
*/
for (i = 0; i < sk_ASIdOrRange_num(choice->u.asIdsOrRanges) - 1; i++) {
ASIdOrRange *a = sk_ASIdOrRange_num(choice->u.asIdsOrRanges, i);
ASIdOrRange *b = sk_ASIdOrRange_num(choice->u.asIdsOrRanges, i + 1);
if (a->type == ASIdOrRange_id && b->type == ASIdOrRange_id) {
if (ASN1_INTEGER_cmp(a->u.id, b->u.id) == 0) {
sk_ASIdOrRange_delete(choice->u.asIdsOrRanges, i);
ASN1_INTEGER_free(a->u.id);
ASIdOrRange_free(a);
i--;
}
continue;
}
if (a->type == ASIdOrRange_id) {
if (ASN1_INTEGER_cmp(a->u.id, b->u.range->min) >= 0 &&
ASN1_INTEGER_cmp(a->u.id, b->u.range->max) <= 0) {
sk_ASIdOrRange_delete(choice->u.asIdsOrRanges, i);
ASN1_INTEGER_free(a->u.id);
ASIdOrRange_free(a);
i--;
}
continue;
}
if (b->type == ASIdOrRange_id) {
if (ASN1_INTEGER_cmp(b->u.id, a->u.range->min) >= 0 &&
ASN1_INTEGER_cmp(b->u.id, a->u.range->max) <= 0) {
sk_ASIdOrRange_delete(choice->u.asIdsOrRanges, i + 1);
ASN1_INTEGER_free(b->u.id);
ASIdOrRange_free(b);
i--;
}
continue;
}
if (ASN1_INTEGER_cmp(a->u.range->max, b->u.range->min) >= 0) {
ASN1_INTEGER_free(a->u.range->max);
ASN1_INTEGER_free(b->u.range->min);
b->u.range->min = a->u.range->min;
sk_ASIdOrRange_delete(choice->u.asIdsOrRanges, i);
ASRange_free(a->u.range);
ASIdOrRange_free(a);
i--;
}
}
}
#warning Check all of the following code for memory leaks
#error this function does not check anywhere near enough error returns
static void *v2i_ASIdentifiers(struct v3_ext_method *method, struct v3_ext_ctx *ctx, STACK_OF(CONF_VALUE) *values)
{
ASIdentifiers *asid = NULL;
ASIdentifierChoice **choice;
ASN1_INTEGER *min, *max;
CONF_VALUE *val;
char *s;
int i;
if ((asid = ASIdentifiers_new()) == NULL) {
X509V3err(X509V3_F_V2I_ASIdentifiers, ERR_R_MALLOC_FAILURE);
return NULL;
}
memset(asid, 0, sizeof(*asid));
for (i = 0; i < sk_CONF_VALUE_num(values); i++) {
val = sk_CONF_VALUE_value(values, i);
/*
* Figure out whether this is an AS or an RDI.
*/
if (!strcmp(val->name, "as")) {
choice = &asid->asnum;
} else if (!strcmp(val->name, "rdi")) {
choice = &asid->rdi;
} else {
X509V3err(blah, blah);
X509V3_conf_err(val);
goto err;
}
/*
* Handle inheritance.
*/
if (!strcmp(val->value, "inherit")) {
if (asid_add_inherit(choice))
continue;
X509V3err(blah, blah);
X509V3_conf_err(val);
goto err;
}
/*
* Number or range. Add it to the list, we'll sort the list later.
*/
if (!X509V3_get_value_int(val, &min))
goto err;
if ((s = strchr(val->value, '-')) == NULL) {
max = NULL;
} else if ((max = s2i_ASN1_INTEGER(NULL, s + 1)) == NULL) {
X509V3_conf_err(val);
goto err;
}
if (!asid_add_id_or_range(choice, min, max))
goto err;
}
/*
* Canonize the result, then we're done.
*/
asid_canonize(asid->asnum);
asid_canonize(asid->rdi);
return asid;
err:
#warning this almost certainly leaks memory
ASIdentifiers_free(asid);
return NULL;
}
X509V3_EXT_METHOD v3_asid = {
NID_ASIdentifiers, /* nid */
0, /* flags */
ASN1_ITEM_ref(ASIdentifiers), /* template */
0, 0, 0, 0, /* old functions, ignored */
0, /* i2s */
0, /* s2i */
0, /* i2v */
v2i_ASIdentifiers, /* v2i */
i2r_ASIdentifiers, /* i2r */
0, /* r2i */
NULL /* extension-specific data */
};
|