summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/x509v3/v3_asid.c
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/lib/libcrypto/x509v3/v3_asid.c1354
1 files changed, 696 insertions, 658 deletions
diff --git a/src/lib/libcrypto/x509v3/v3_asid.c b/src/lib/libcrypto/x509v3/v3_asid.c
index 325c8e0406..6335a31d19 100644
--- a/src/lib/libcrypto/x509v3/v3_asid.c
+++ b/src/lib/libcrypto/x509v3/v3_asid.c
@@ -10,7 +10,7 @@
10 * are met: 10 * are met:
11 * 11 *
12 * 1. Redistributions of source code must retain the above copyright 12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer. 13 * notice, this list of conditions and the following disclaimer.
14 * 14 *
15 * 2. Redistributions in binary form must reproduce the above copyright 15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in 16 * notice, this list of conditions and the following disclaimer in
@@ -76,23 +76,23 @@
76 */ 76 */
77 77
78ASN1_SEQUENCE(ASRange) = { 78ASN1_SEQUENCE(ASRange) = {
79 ASN1_SIMPLE(ASRange, min, ASN1_INTEGER), 79 ASN1_SIMPLE(ASRange, min, ASN1_INTEGER),
80 ASN1_SIMPLE(ASRange, max, ASN1_INTEGER) 80 ASN1_SIMPLE(ASRange, max, ASN1_INTEGER)
81} ASN1_SEQUENCE_END(ASRange) 81} ASN1_SEQUENCE_END(ASRange)
82 82
83ASN1_CHOICE(ASIdOrRange) = { 83ASN1_CHOICE(ASIdOrRange) = {
84 ASN1_SIMPLE(ASIdOrRange, u.id, ASN1_INTEGER), 84 ASN1_SIMPLE(ASIdOrRange, u.id, ASN1_INTEGER),
85 ASN1_SIMPLE(ASIdOrRange, u.range, ASRange) 85 ASN1_SIMPLE(ASIdOrRange, u.range, ASRange)
86} ASN1_CHOICE_END(ASIdOrRange) 86} ASN1_CHOICE_END(ASIdOrRange)
87 87
88ASN1_CHOICE(ASIdentifierChoice) = { 88ASN1_CHOICE(ASIdentifierChoice) = {
89 ASN1_SIMPLE(ASIdentifierChoice, u.inherit, ASN1_NULL), 89 ASN1_SIMPLE(ASIdentifierChoice, u.inherit, ASN1_NULL),
90 ASN1_SEQUENCE_OF(ASIdentifierChoice, u.asIdsOrRanges, ASIdOrRange) 90 ASN1_SEQUENCE_OF(ASIdentifierChoice, u.asIdsOrRanges, ASIdOrRange)
91} ASN1_CHOICE_END(ASIdentifierChoice) 91} ASN1_CHOICE_END(ASIdentifierChoice)
92 92
93ASN1_SEQUENCE(ASIdentifiers) = { 93ASN1_SEQUENCE(ASIdentifiers) = {
94 ASN1_EXP_OPT(ASIdentifiers, asnum, ASIdentifierChoice, 0), 94 ASN1_EXP_OPT(ASIdentifiers, asnum, ASIdentifierChoice, 0),
95 ASN1_EXP_OPT(ASIdentifiers, rdi, ASIdentifierChoice, 1) 95 ASN1_EXP_OPT(ASIdentifiers, rdi, ASIdentifierChoice, 1)
96} ASN1_SEQUENCE_END(ASIdentifiers) 96} ASN1_SEQUENCE_END(ASIdentifiers)
97 97
98IMPLEMENT_ASN1_FUNCTIONS(ASRange) 98IMPLEMENT_ASN1_FUNCTIONS(ASRange)
@@ -103,628 +103,662 @@ IMPLEMENT_ASN1_FUNCTIONS(ASIdentifiers)
103/* 103/*
104 * i2r method for an ASIdentifierChoice. 104 * i2r method for an ASIdentifierChoice.
105 */ 105 */
106static int i2r_ASIdentifierChoice(BIO *out, 106static int
107 ASIdentifierChoice *choice, 107i2r_ASIdentifierChoice(BIO *out, ASIdentifierChoice *choice, int indent,
108 int indent, 108 const char *msg)
109 const char *msg)
110{ 109{
111 int i; 110 int i;
112 char *s; 111 char *s;
113 if (choice == NULL) 112
114 return 1; 113 if (choice == NULL)
115 BIO_printf(out, "%*s%s:\n", indent, "", msg); 114 return 1;
116 switch (choice->type) { 115 BIO_printf(out, "%*s%s:\n", indent, "", msg);
117 case ASIdentifierChoice_inherit: 116 switch (choice->type) {
118 BIO_printf(out, "%*sinherit\n", indent + 2, ""); 117 case ASIdentifierChoice_inherit:
119 break; 118 BIO_printf(out, "%*sinherit\n", indent + 2, "");
120 case ASIdentifierChoice_asIdsOrRanges: 119 break;
121 for (i = 0; i < sk_ASIdOrRange_num(choice->u.asIdsOrRanges); i++) { 120 case ASIdentifierChoice_asIdsOrRanges:
122 ASIdOrRange *aor = sk_ASIdOrRange_value(choice->u.asIdsOrRanges, i); 121 for (i = 0; i < sk_ASIdOrRange_num(choice->u.asIdsOrRanges);
123 switch (aor->type) { 122 i++) {
124 case ASIdOrRange_id: 123 ASIdOrRange *aor =
125 if ((s = i2s_ASN1_INTEGER(NULL, aor->u.id)) == NULL) 124 sk_ASIdOrRange_value(choice->u.asIdsOrRanges, i);
126 return 0; 125 switch (aor->type) {
127 BIO_printf(out, "%*s%s\n", indent + 2, "", s); 126 case ASIdOrRange_id:
128 free(s); 127 if ((s = i2s_ASN1_INTEGER(NULL, aor->u.id)) ==
129 break; 128 NULL)
130 case ASIdOrRange_range: 129 return 0;
131 if ((s = i2s_ASN1_INTEGER(NULL, aor->u.range->min)) == NULL) 130 BIO_printf(out, "%*s%s\n", indent + 2, "", s);
132 return 0; 131 free(s);
133 BIO_printf(out, "%*s%s-", indent + 2, "", s); 132 break;
134 free(s); 133 case ASIdOrRange_range:
135 if ((s = i2s_ASN1_INTEGER(NULL, aor->u.range->max)) == NULL) 134 if ((s = i2s_ASN1_INTEGER(NULL,
136 return 0; 135 aor->u.range->min)) == NULL)
137 BIO_printf(out, "%s\n", s); 136 return 0;
138 free(s); 137 BIO_printf(out, "%*s%s-", indent + 2, "", s);
139 break; 138 free(s);
140 default: 139 if ((s = i2s_ASN1_INTEGER(NULL,
141 return 0; 140 aor->u.range->max)) == NULL)
142 } 141 return 0;
143 } 142 BIO_printf(out, "%s\n", s);
144 break; 143 free(s);
145 default: 144 break;
146 return 0; 145 default:
147 } 146 return 0;
148 return 1; 147 }
148 }
149 break;
150
151 default:
152 return 0;
153 }
154 return 1;
149} 155}
150 156
151/* 157/*
152 * i2r method for an ASIdentifier extension. 158 * i2r method for an ASIdentifier extension.
153 */ 159 */
154static int i2r_ASIdentifiers(const X509V3_EXT_METHOD *method, 160static int
155 void *ext, 161i2r_ASIdentifiers(const X509V3_EXT_METHOD *method, void *ext, BIO *out,
156 BIO *out, 162 int indent)
157 int indent)
158{ 163{
159 ASIdentifiers *asid = ext; 164 ASIdentifiers *asid = ext;
160 return (i2r_ASIdentifierChoice(out, asid->asnum, indent, 165
161 "Autonomous System Numbers") && 166 return (i2r_ASIdentifierChoice(out, asid->asnum, indent,
162 i2r_ASIdentifierChoice(out, asid->rdi, indent, 167 "Autonomous System Numbers") &&
163 "Routing Domain Identifiers")); 168 i2r_ASIdentifierChoice(out, asid->rdi, indent,
169 "Routing Domain Identifiers"));
164} 170}
165 171
166/* 172/*
167 * Sort comparision function for a sequence of ASIdOrRange elements. 173 * Sort comparision function for a sequence of ASIdOrRange elements.
168 */ 174 */
169static int ASIdOrRange_cmp(const ASIdOrRange * const *a_, 175static int
170 const ASIdOrRange * const *b_) 176ASIdOrRange_cmp(const ASIdOrRange * const *a_, const ASIdOrRange * const *b_)
171{ 177{
172 const ASIdOrRange *a = *a_, *b = *b_; 178 const ASIdOrRange *a = *a_, *b = *b_;
173 179
174 OPENSSL_assert((a->type == ASIdOrRange_id && a->u.id != NULL) || 180 OPENSSL_assert((a->type == ASIdOrRange_id && a->u.id != NULL) ||
175 (a->type == ASIdOrRange_range && a->u.range != NULL && 181 (a->type == ASIdOrRange_range && a->u.range != NULL &&
176 a->u.range->min != NULL && a->u.range->max != NULL)); 182 a->u.range->min != NULL && a->u.range->max != NULL));
177 183
178 OPENSSL_assert((b->type == ASIdOrRange_id && b->u.id != NULL) || 184 OPENSSL_assert((b->type == ASIdOrRange_id && b->u.id != NULL) ||
179 (b->type == ASIdOrRange_range && b->u.range != NULL && 185 (b->type == ASIdOrRange_range && b->u.range != NULL &&
180 b->u.range->min != NULL && b->u.range->max != NULL)); 186 b->u.range->min != NULL && b->u.range->max != NULL));
181 187
182 if (a->type == ASIdOrRange_id && b->type == ASIdOrRange_id) 188 if (a->type == ASIdOrRange_id && b->type == ASIdOrRange_id)
183 return ASN1_INTEGER_cmp(a->u.id, b->u.id); 189 return ASN1_INTEGER_cmp(a->u.id, b->u.id);
184 190
185 if (a->type == ASIdOrRange_range && b->type == ASIdOrRange_range) { 191 if (a->type == ASIdOrRange_range && b->type == ASIdOrRange_range) {
186 int r = ASN1_INTEGER_cmp(a->u.range->min, b->u.range->min); 192 int r = ASN1_INTEGER_cmp(a->u.range->min, b->u.range->min);
187 return r != 0 ? r : ASN1_INTEGER_cmp(a->u.range->max, b->u.range->max); 193 return r != 0 ? r :
188 } 194 ASN1_INTEGER_cmp(a->u.range->max, b->u.range->max);
195 }
189 196
190 if (a->type == ASIdOrRange_id) 197 if (a->type == ASIdOrRange_id)
191 return ASN1_INTEGER_cmp(a->u.id, b->u.range->min); 198 return ASN1_INTEGER_cmp(a->u.id, b->u.range->min);
192 else 199 else
193 return ASN1_INTEGER_cmp(a->u.range->min, b->u.id); 200 return ASN1_INTEGER_cmp(a->u.range->min, b->u.id);
194} 201}
195 202
196/* 203/*
197 * Add an inherit element. 204 * Add an inherit element.
198 */ 205 */
199int v3_asid_add_inherit(ASIdentifiers *asid, int which) 206int
207v3_asid_add_inherit(ASIdentifiers *asid, int which)
200{ 208{
201 ASIdentifierChoice **choice; 209 ASIdentifierChoice **choice;
202 if (asid == NULL) 210
203 return 0; 211 if (asid == NULL)
204 switch (which) { 212 return 0;
205 case V3_ASID_ASNUM: 213 switch (which) {
206 choice = &asid->asnum; 214 case V3_ASID_ASNUM:
207 break; 215 choice = &asid->asnum;
208 case V3_ASID_RDI: 216 break;
209 choice = &asid->rdi; 217 case V3_ASID_RDI:
210 break; 218 choice = &asid->rdi;
211 default: 219 break;
212 return 0; 220 default:
213 } 221 return 0;
214 if (*choice == NULL) { 222 }
215 if ((*choice = ASIdentifierChoice_new()) == NULL) 223 if (*choice == NULL) {
216 return 0; 224 if ((*choice = ASIdentifierChoice_new()) == NULL)
217 OPENSSL_assert((*choice)->u.inherit == NULL); 225 return 0;
218 if (((*choice)->u.inherit = ASN1_NULL_new()) == NULL) 226 OPENSSL_assert((*choice)->u.inherit == NULL);
219 return 0; 227 if (((*choice)->u.inherit = ASN1_NULL_new()) == NULL)
220 (*choice)->type = ASIdentifierChoice_inherit; 228 return 0;
221 } 229 (*choice)->type = ASIdentifierChoice_inherit;
222 return (*choice)->type == ASIdentifierChoice_inherit; 230 }
231 return (*choice)->type == ASIdentifierChoice_inherit;
223} 232}
224 233
225/* 234/*
226 * Add an ID or range to an ASIdentifierChoice. 235 * Add an ID or range to an ASIdentifierChoice.
227 */ 236 */
228int v3_asid_add_id_or_range(ASIdentifiers *asid, 237int
229 int which, 238v3_asid_add_id_or_range(ASIdentifiers *asid, int which, ASN1_INTEGER *min,
230 ASN1_INTEGER *min, 239 ASN1_INTEGER *max)
231 ASN1_INTEGER *max)
232{ 240{
233 ASIdentifierChoice **choice; 241 ASIdentifierChoice **choice;
234 ASIdOrRange *aor; 242 ASIdOrRange *aor;
235 if (asid == NULL) 243
236 return 0; 244 if (asid == NULL)
237 switch (which) { 245 return 0;
238 case V3_ASID_ASNUM: 246 switch (which) {
239 choice = &asid->asnum; 247 case V3_ASID_ASNUM:
240 break; 248 choice = &asid->asnum;
241 case V3_ASID_RDI: 249 break;
242 choice = &asid->rdi; 250 case V3_ASID_RDI:
243 break; 251 choice = &asid->rdi;
244 default: 252 break;
245 return 0; 253 default:
246 } 254 return 0;
247 if (*choice != NULL && (*choice)->type == ASIdentifierChoice_inherit) 255 }
248 return 0; 256 if (*choice != NULL && (*choice)->type == ASIdentifierChoice_inherit)
249 if (*choice == NULL) { 257 return 0;
250 if ((*choice = ASIdentifierChoice_new()) == NULL) 258 if (*choice == NULL) {
251 return 0; 259 if ((*choice = ASIdentifierChoice_new()) == NULL)
252 OPENSSL_assert((*choice)->u.asIdsOrRanges == NULL); 260 return 0;
253 (*choice)->u.asIdsOrRanges = sk_ASIdOrRange_new(ASIdOrRange_cmp); 261 OPENSSL_assert((*choice)->u.asIdsOrRanges == NULL);
254 if ((*choice)->u.asIdsOrRanges == NULL) 262 (*choice)->u.asIdsOrRanges =
255 return 0; 263 sk_ASIdOrRange_new(ASIdOrRange_cmp);
256 (*choice)->type = ASIdentifierChoice_asIdsOrRanges; 264 if ((*choice)->u.asIdsOrRanges == NULL)
257 } 265 return 0;
258 if ((aor = ASIdOrRange_new()) == NULL) 266 (*choice)->type = ASIdentifierChoice_asIdsOrRanges;
259 return 0; 267 }
260 if (max == NULL) { 268 if ((aor = ASIdOrRange_new()) == NULL)
261 aor->type = ASIdOrRange_id; 269 return 0;
262 aor->u.id = min; 270 if (max == NULL) {
263 } else { 271 aor->type = ASIdOrRange_id;
264 aor->type = ASIdOrRange_range; 272 aor->u.id = min;
265 if ((aor->u.range = ASRange_new()) == NULL) 273 } else {
266 goto err; 274 aor->type = ASIdOrRange_range;
267 ASN1_INTEGER_free(aor->u.range->min); 275 if ((aor->u.range = ASRange_new()) == NULL)
268 aor->u.range->min = min; 276 goto err;
269 ASN1_INTEGER_free(aor->u.range->max); 277 ASN1_INTEGER_free(aor->u.range->min);
270 aor->u.range->max = max; 278 aor->u.range->min = min;
271 } 279 ASN1_INTEGER_free(aor->u.range->max);
272 if (!(sk_ASIdOrRange_push((*choice)->u.asIdsOrRanges, aor))) 280 aor->u.range->max = max;
273 goto err; 281 }
274 return 1; 282 if (!(sk_ASIdOrRange_push((*choice)->u.asIdsOrRanges, aor)))
275 283 goto err;
276 err: 284 return 1;
277 ASIdOrRange_free(aor); 285
278 return 0; 286err:
287 ASIdOrRange_free(aor);
288 return 0;
279} 289}
280 290
281/* 291/*
282 * Extract min and max values from an ASIdOrRange. 292 * Extract min and max values from an ASIdOrRange.
283 */ 293 */
284static void extract_min_max(ASIdOrRange *aor, 294static void
285 ASN1_INTEGER **min, 295extract_min_max(ASIdOrRange *aor, ASN1_INTEGER **min, ASN1_INTEGER **max)
286 ASN1_INTEGER **max)
287{ 296{
288 OPENSSL_assert(aor != NULL && min != NULL && max != NULL); 297 OPENSSL_assert(aor != NULL && min != NULL && max != NULL);
289 switch (aor->type) { 298
290 case ASIdOrRange_id: 299 switch (aor->type) {
291 *min = aor->u.id; 300 case ASIdOrRange_id:
292 *max = aor->u.id; 301 *min = aor->u.id;
293 return; 302 *max = aor->u.id;
294 case ASIdOrRange_range: 303 return;
295 *min = aor->u.range->min; 304 case ASIdOrRange_range:
296 *max = aor->u.range->max; 305 *min = aor->u.range->min;
297 return; 306 *max = aor->u.range->max;
298 } 307 return;
308 }
299} 309}
300 310
301/* 311/*
302 * Check whether an ASIdentifierChoice is in canonical form. 312 * Check whether an ASIdentifierChoice is in canonical form.
303 */ 313 */
304static int ASIdentifierChoice_is_canonical(ASIdentifierChoice *choice) 314static int
315ASIdentifierChoice_is_canonical(ASIdentifierChoice *choice)
305{ 316{
306 ASN1_INTEGER *a_max_plus_one = NULL; 317 ASN1_INTEGER *a_max_plus_one = NULL;
307 BIGNUM *bn = NULL; 318 BIGNUM *bn = NULL;
308 int i, ret = 0; 319 int i, ret = 0;
309 320
310 /* 321 /*
311 * Empty element or inheritance is canonical. 322 * Empty element or inheritance is canonical.
312 */ 323 */
313 if (choice == NULL || choice->type == ASIdentifierChoice_inherit) 324 if (choice == NULL || choice->type == ASIdentifierChoice_inherit)
314 return 1; 325 return 1;
315 326
316 /* 327 /*
317 * If not a list, or if empty list, it's broken. 328 * If not a list, or if empty list, it's broken.
318 */ 329 */
319 if (choice->type != ASIdentifierChoice_asIdsOrRanges || 330 if (choice->type != ASIdentifierChoice_asIdsOrRanges ||
320 sk_ASIdOrRange_num(choice->u.asIdsOrRanges) == 0) 331 sk_ASIdOrRange_num(choice->u.asIdsOrRanges) == 0)
321 return 0; 332 return 0;
322 333
323 /* 334 /*
324 * It's a list, check it. 335 * It's a list, check it.
325 */ 336 */
326 for (i = 0; i < sk_ASIdOrRange_num(choice->u.asIdsOrRanges) - 1; i++) { 337 for (i = 0; i < sk_ASIdOrRange_num(choice->u.asIdsOrRanges) - 1; i++) {
327 ASIdOrRange *a = sk_ASIdOrRange_value(choice->u.asIdsOrRanges, i); 338 ASIdOrRange *a =
328 ASIdOrRange *b = sk_ASIdOrRange_value(choice->u.asIdsOrRanges, i + 1); 339 sk_ASIdOrRange_value(choice->u.asIdsOrRanges, i);
329 ASN1_INTEGER *a_min, *a_max, *b_min, *b_max; 340 ASIdOrRange *b =
330 341 sk_ASIdOrRange_value(choice->u.asIdsOrRanges, i + 1);
331 extract_min_max(a, &a_min, &a_max); 342 ASN1_INTEGER *a_min, *a_max, *b_min, *b_max;
332 extract_min_max(b, &b_min, &b_max); 343
333 344 extract_min_max(a, &a_min, &a_max);
334 /* 345 extract_min_max(b, &b_min, &b_max);
335 * Punt misordered list, overlapping start, or inverted range. 346
336 */ 347 /*
337 if (ASN1_INTEGER_cmp(a_min, b_min) >= 0 || 348 * Punt misordered list, overlapping start, or inverted range.
338 ASN1_INTEGER_cmp(a_min, a_max) > 0 || 349 */
339 ASN1_INTEGER_cmp(b_min, b_max) > 0) 350 if (ASN1_INTEGER_cmp(a_min, b_min) >= 0 ||
340 goto done; 351 ASN1_INTEGER_cmp(a_min, a_max) > 0 ||
341 352 ASN1_INTEGER_cmp(b_min, b_max) > 0)
342 /* 353 goto done;
343 * Calculate a_max + 1 to check for adjacency. 354
344 */ 355 /*
345 if ((bn == NULL && (bn = BN_new()) == NULL) || 356 * Calculate a_max + 1 to check for adjacency.
346 ASN1_INTEGER_to_BN(a_max, bn) == NULL || 357 */
347 !BN_add_word(bn, 1) || 358 if ((bn == NULL && (bn = BN_new()) == NULL) ||
348 (a_max_plus_one = BN_to_ASN1_INTEGER(bn, a_max_plus_one)) == NULL) { 359 ASN1_INTEGER_to_BN(a_max, bn) == NULL ||
349 X509V3err(X509V3_F_ASIDENTIFIERCHOICE_IS_CANONICAL, 360 !BN_add_word(bn, 1) || (a_max_plus_one =
350 ERR_R_MALLOC_FAILURE); 361 BN_to_ASN1_INTEGER(bn, a_max_plus_one)) == NULL) {
351 goto done; 362 X509V3err(X509V3_F_ASIDENTIFIERCHOICE_IS_CANONICAL,
352 } 363 ERR_R_MALLOC_FAILURE);
353 364 goto done;
354 /* 365 }
355 * Punt if adjacent or overlapping. 366
356 */ 367 /*
357 if (ASN1_INTEGER_cmp(a_max_plus_one, b_min) >= 0) 368 * Punt if adjacent or overlapping.
358 goto done; 369 */
359 } 370 if (ASN1_INTEGER_cmp(a_max_plus_one, b_min) >= 0)
360 371 goto done;
361 /* 372 }
362 * Check for inverted range. 373
363 */ 374 /*
364 i = sk_ASIdOrRange_num(choice->u.asIdsOrRanges) - 1; 375 * Check for inverted range.
365 { 376 */
366 ASIdOrRange *a = sk_ASIdOrRange_value(choice->u.asIdsOrRanges, i); 377 i = sk_ASIdOrRange_num(choice->u.asIdsOrRanges) - 1;
367 ASN1_INTEGER *a_min, *a_max; 378 {
368 if (a != NULL && a->type == ASIdOrRange_range) { 379 ASIdOrRange *a =
369 extract_min_max(a, &a_min, &a_max); 380 sk_ASIdOrRange_value(choice->u.asIdsOrRanges, i);
370 if (ASN1_INTEGER_cmp(a_min, a_max) > 0) 381 ASN1_INTEGER *a_min, *a_max;
371 goto done; 382
372 } 383 if (a != NULL && a->type == ASIdOrRange_range) {
373 } 384 extract_min_max(a, &a_min, &a_max);
374 385 if (ASN1_INTEGER_cmp(a_min, a_max) > 0)
375 ret = 1; 386 goto done;
376 387 }
377 done: 388 }
378 ASN1_INTEGER_free(a_max_plus_one); 389
379 BN_free(bn); 390 ret = 1;
380 return ret; 391
392done:
393 ASN1_INTEGER_free(a_max_plus_one);
394 BN_free(bn);
395 return ret;
381} 396}
382 397
383/* 398/*
384 * Check whether an ASIdentifier extension is in canonical form. 399 * Check whether an ASIdentifier extension is in canonical form.
385 */ 400 */
386int v3_asid_is_canonical(ASIdentifiers *asid) 401int
402v3_asid_is_canonical(ASIdentifiers *asid)
387{ 403{
388 return (asid == NULL || 404 return (asid == NULL ||
389 (ASIdentifierChoice_is_canonical(asid->asnum) && 405 (ASIdentifierChoice_is_canonical(asid->asnum) &&
390 ASIdentifierChoice_is_canonical(asid->rdi))); 406 ASIdentifierChoice_is_canonical(asid->rdi)));
391} 407}
392 408
393/* 409/*
394 * Whack an ASIdentifierChoice into canonical form. 410 * Whack an ASIdentifierChoice into canonical form.
395 */ 411 */
396static int ASIdentifierChoice_canonize(ASIdentifierChoice *choice) 412static int
413ASIdentifierChoice_canonize(ASIdentifierChoice *choice)
397{ 414{
398 ASN1_INTEGER *a_max_plus_one = NULL; 415 ASN1_INTEGER *a_max_plus_one = NULL;
399 BIGNUM *bn = NULL; 416 BIGNUM *bn = NULL;
400 int i, ret = 0; 417 int i, ret = 0;
401 418
402 /* 419 /*
403 * Nothing to do for empty element or inheritance. 420 * Nothing to do for empty element or inheritance.
404 */ 421 */
405 if (choice == NULL || choice->type == ASIdentifierChoice_inherit) 422 if (choice == NULL || choice->type == ASIdentifierChoice_inherit)
406 return 1; 423 return 1;
407 424
408 /* 425 /*
409 * If not a list, or if empty list, it's broken. 426 * If not a list, or if empty list, it's broken.
410 */ 427 */
411 if (choice->type != ASIdentifierChoice_asIdsOrRanges || 428 if (choice->type != ASIdentifierChoice_asIdsOrRanges ||
412 sk_ASIdOrRange_num(choice->u.asIdsOrRanges) == 0) { 429 sk_ASIdOrRange_num(choice->u.asIdsOrRanges) == 0) {
413 X509V3err(X509V3_F_ASIDENTIFIERCHOICE_CANONIZE, 430 X509V3err(X509V3_F_ASIDENTIFIERCHOICE_CANONIZE,
414 X509V3_R_EXTENSION_VALUE_ERROR);
415 return 0;
416 }
417
418 /*
419 * We have a non-empty list. Sort it.
420 */
421 sk_ASIdOrRange_sort(choice->u.asIdsOrRanges);
422
423 /*
424 * Now check for errors and suboptimal encoding, rejecting the
425 * former and fixing the latter.
426 */
427 for (i = 0; i < sk_ASIdOrRange_num(choice->u.asIdsOrRanges) - 1; i++) {
428 ASIdOrRange *a = sk_ASIdOrRange_value(choice->u.asIdsOrRanges, i);
429 ASIdOrRange *b = sk_ASIdOrRange_value(choice->u.asIdsOrRanges, i + 1);
430 ASN1_INTEGER *a_min, *a_max, *b_min, *b_max;
431
432 extract_min_max(a, &a_min, &a_max);
433 extract_min_max(b, &b_min, &b_max);
434
435 /*
436 * Make sure we're properly sorted (paranoia).
437 */
438 OPENSSL_assert(ASN1_INTEGER_cmp(a_min, b_min) <= 0);
439
440 /*
441 * Punt inverted ranges.
442 */
443 if (ASN1_INTEGER_cmp(a_min, a_max) > 0 ||
444 ASN1_INTEGER_cmp(b_min, b_max) > 0)
445 goto done;
446
447 /*
448 * Check for overlaps.
449 */
450 if (ASN1_INTEGER_cmp(a_max, b_min) >= 0) {
451 X509V3err(X509V3_F_ASIDENTIFIERCHOICE_CANONIZE,
452 X509V3_R_EXTENSION_VALUE_ERROR); 431 X509V3_R_EXTENSION_VALUE_ERROR);
453 goto done; 432 return 0;
454 } 433 }
455 434
456 /* 435 /*
457 * Calculate a_max + 1 to check for adjacency. 436 * We have a non-empty list. Sort it.
458 */ 437 */
459 if ((bn == NULL && (bn = BN_new()) == NULL) || 438 sk_ASIdOrRange_sort(choice->u.asIdsOrRanges);
460 ASN1_INTEGER_to_BN(a_max, bn) == NULL || 439
461 !BN_add_word(bn, 1) || 440 /*
462 (a_max_plus_one = BN_to_ASN1_INTEGER(bn, a_max_plus_one)) == NULL) { 441 * Now check for errors and suboptimal encoding, rejecting the
463 X509V3err(X509V3_F_ASIDENTIFIERCHOICE_CANONIZE, ERR_R_MALLOC_FAILURE); 442 * former and fixing the latter.
464 goto done; 443 */
465 } 444 for (i = 0; i < sk_ASIdOrRange_num(choice->u.asIdsOrRanges) - 1; i++) {
466 445 ASIdOrRange *a =
467 /* 446 sk_ASIdOrRange_value(choice->u.asIdsOrRanges, i);
468 * If a and b are adjacent, merge them. 447 ASIdOrRange *b =
469 */ 448 sk_ASIdOrRange_value(choice->u.asIdsOrRanges, i + 1);
470 if (ASN1_INTEGER_cmp(a_max_plus_one, b_min) == 0) { 449 ASN1_INTEGER *a_min, *a_max, *b_min, *b_max;
471 ASRange *r; 450
472 switch (a->type) { 451 extract_min_max(a, &a_min, &a_max);
473 case ASIdOrRange_id: 452 extract_min_max(b, &b_min, &b_max);
474 if ((r = malloc(sizeof(ASRange))) == NULL) { 453
475 X509V3err(X509V3_F_ASIDENTIFIERCHOICE_CANONIZE, 454 /*
476 ERR_R_MALLOC_FAILURE); 455 * Make sure we're properly sorted (paranoia).
477 goto done; 456 */
457 OPENSSL_assert(ASN1_INTEGER_cmp(a_min, b_min) <= 0);
458
459 /*
460 * Punt inverted ranges.
461 */
462 if (ASN1_INTEGER_cmp(a_min, a_max) > 0 ||
463 ASN1_INTEGER_cmp(b_min, b_max) > 0)
464 goto done;
465
466 /*
467 * Check for overlaps.
468 */
469 if (ASN1_INTEGER_cmp(a_max, b_min) >= 0) {
470 X509V3err(X509V3_F_ASIDENTIFIERCHOICE_CANONIZE,
471 X509V3_R_EXTENSION_VALUE_ERROR);
472 goto done;
473 }
474
475 /*
476 * Calculate a_max + 1 to check for adjacency.
477 */
478 if ((bn == NULL && (bn = BN_new()) == NULL) ||
479 ASN1_INTEGER_to_BN(a_max, bn) == NULL ||
480 !BN_add_word(bn, 1) || (a_max_plus_one =
481 BN_to_ASN1_INTEGER(bn, a_max_plus_one)) == NULL) {
482 X509V3err(X509V3_F_ASIDENTIFIERCHOICE_CANONIZE,
483 ERR_R_MALLOC_FAILURE);
484 goto done;
485 }
486
487 /*
488 * If a and b are adjacent, merge them.
489 */
490 if (ASN1_INTEGER_cmp(a_max_plus_one, b_min) == 0) {
491 ASRange *r;
492 switch (a->type) {
493 case ASIdOrRange_id:
494 if ((r = malloc(sizeof(ASRange))) == NULL) {
495 X509V3err(X509V3_F_ASIDENTIFIERCHOICE_CANONIZE,
496 ERR_R_MALLOC_FAILURE);
497 goto done;
498 }
499 r->min = a_min;
500 r->max = b_max;
501 a->type = ASIdOrRange_range;
502 a->u.range = r;
503 break;
504 case ASIdOrRange_range:
505 ASN1_INTEGER_free(a->u.range->max);
506 a->u.range->max = b_max;
507 break;
508 }
509 switch (b->type) {
510 case ASIdOrRange_id:
511 b->u.id = NULL;
512 break;
513 case ASIdOrRange_range:
514 b->u.range->max = NULL;
515 break;
516 }
517 ASIdOrRange_free(b);
518 (void) sk_ASIdOrRange_delete(
519 choice->u.asIdsOrRanges, i + 1);
520 i--;
521 continue;
522 }
523 }
524
525 /*
526 * Check for final inverted range.
527 */
528 i = sk_ASIdOrRange_num(choice->u.asIdsOrRanges) - 1;
529 {
530 ASIdOrRange *a =
531 sk_ASIdOrRange_value(choice->u.asIdsOrRanges, i);
532 ASN1_INTEGER *a_min, *a_max;
533 if (a != NULL && a->type == ASIdOrRange_range) {
534 extract_min_max(a, &a_min, &a_max);
535 if (ASN1_INTEGER_cmp(a_min, a_max) > 0)
536 goto done;
537 }
478 } 538 }
479 r->min = a_min; 539
480 r->max = b_max; 540 OPENSSL_assert(ASIdentifierChoice_is_canonical(choice)); /* Paranoia */
481 a->type = ASIdOrRange_range; 541
482 a->u.range = r; 542 ret = 1;
483 break; 543
484 case ASIdOrRange_range: 544done:
485 ASN1_INTEGER_free(a->u.range->max); 545 ASN1_INTEGER_free(a_max_plus_one);
486 a->u.range->max = b_max; 546 BN_free(bn);
487 break; 547 return ret;
488 }
489 switch (b->type) {
490 case ASIdOrRange_id:
491 b->u.id = NULL;
492 break;
493 case ASIdOrRange_range:
494 b->u.range->max = NULL;
495 break;
496 }
497 ASIdOrRange_free(b);
498 (void) sk_ASIdOrRange_delete(choice->u.asIdsOrRanges, i + 1);
499 i--;
500 continue;
501 }
502 }
503
504 /*
505 * Check for final inverted range.
506 */
507 i = sk_ASIdOrRange_num(choice->u.asIdsOrRanges) - 1;
508 {
509 ASIdOrRange *a = sk_ASIdOrRange_value(choice->u.asIdsOrRanges, i);
510 ASN1_INTEGER *a_min, *a_max;
511 if (a != NULL && a->type == ASIdOrRange_range) {
512 extract_min_max(a, &a_min, &a_max);
513 if (ASN1_INTEGER_cmp(a_min, a_max) > 0)
514 goto done;
515 }
516 }
517
518 OPENSSL_assert(ASIdentifierChoice_is_canonical(choice)); /* Paranoia */
519
520 ret = 1;
521
522 done:
523 ASN1_INTEGER_free(a_max_plus_one);
524 BN_free(bn);
525 return ret;
526} 548}
527 549
528/* 550/*
529 * Whack an ASIdentifier extension into canonical form. 551 * Whack an ASIdentifier extension into canonical form.
530 */ 552 */
531int v3_asid_canonize(ASIdentifiers *asid) 553int
554v3_asid_canonize(ASIdentifiers *asid)
532{ 555{
533 return (asid == NULL || 556 return (asid == NULL ||
534 (ASIdentifierChoice_canonize(asid->asnum) && 557 (ASIdentifierChoice_canonize(asid->asnum) &&
535 ASIdentifierChoice_canonize(asid->rdi))); 558 ASIdentifierChoice_canonize(asid->rdi)));
536} 559}
537 560
538/* 561/*
539 * v2i method for an ASIdentifier extension. 562 * v2i method for an ASIdentifier extension.
540 */ 563 */
541static void *v2i_ASIdentifiers(const struct v3_ext_method *method, 564static void *
542 struct v3_ext_ctx *ctx, 565v2i_ASIdentifiers(const struct v3_ext_method *method, struct v3_ext_ctx *ctx,
543 STACK_OF(CONF_VALUE) *values) 566 STACK_OF(CONF_VALUE) *values)
544{ 567{
545 ASN1_INTEGER *min = NULL, *max = NULL; 568 ASN1_INTEGER *min = NULL, *max = NULL;
546 ASIdentifiers *asid = NULL; 569 ASIdentifiers *asid = NULL;
547 int i; 570 int i;
548 571
549 if ((asid = ASIdentifiers_new()) == NULL) { 572 if ((asid = ASIdentifiers_new()) == NULL) {
550 X509V3err(X509V3_F_V2I_ASIDENTIFIERS, ERR_R_MALLOC_FAILURE); 573 X509V3err(X509V3_F_V2I_ASIDENTIFIERS, ERR_R_MALLOC_FAILURE);
551 return NULL; 574 return NULL;
552 } 575 }
553 576
554 for (i = 0; i < sk_CONF_VALUE_num(values); i++) { 577 for (i = 0; i < sk_CONF_VALUE_num(values); i++) {
555 CONF_VALUE *val = sk_CONF_VALUE_value(values, i); 578 CONF_VALUE *val = sk_CONF_VALUE_value(values, i);
556 int i1, i2, i3, is_range, which; 579 int i1, i2, i3, is_range, which;
557 580
558 /* 581 /*
559 * Figure out whether this is an AS or an RDI. 582 * Figure out whether this is an AS or an RDI.
560 */ 583 */
561 if ( !name_cmp(val->name, "AS")) { 584 if (!name_cmp(val->name, "AS")) {
562 which = V3_ASID_ASNUM; 585 which = V3_ASID_ASNUM;
563 } else if (!name_cmp(val->name, "RDI")) { 586 } else if (!name_cmp(val->name, "RDI")) {
564 which = V3_ASID_RDI; 587 which = V3_ASID_RDI;
565 } else { 588 } else {
566 X509V3err(X509V3_F_V2I_ASIDENTIFIERS, X509V3_R_EXTENSION_NAME_ERROR); 589 X509V3err(X509V3_F_V2I_ASIDENTIFIERS,
567 X509V3_conf_err(val); 590 X509V3_R_EXTENSION_NAME_ERROR);
568 goto err; 591 X509V3_conf_err(val);
569 } 592 goto err;
570 593 }
571 /* 594
572 * Handle inheritance. 595 /*
573 */ 596 * Handle inheritance.
574 if (!strcmp(val->value, "inherit")) { 597 */
575 if (v3_asid_add_inherit(asid, which)) 598 if (!strcmp(val->value, "inherit")) {
576 continue; 599 if (v3_asid_add_inherit(asid, which))
577 X509V3err(X509V3_F_V2I_ASIDENTIFIERS, X509V3_R_INVALID_INHERITANCE); 600 continue;
578 X509V3_conf_err(val); 601 X509V3err(X509V3_F_V2I_ASIDENTIFIERS,
579 goto err; 602 X509V3_R_INVALID_INHERITANCE);
580 } 603 X509V3_conf_err(val);
581 604 goto err;
582 /* 605 }
583 * Number, range, or mistake, pick it apart and figure out which. 606
584 */ 607 /*
585 i1 = strspn(val->value, "0123456789"); 608 * Number, range, or mistake, pick it apart and figure out which.
586 if (val->value[i1] == '\0') { 609 */
587 is_range = 0; 610 i1 = strspn(val->value, "0123456789");
588 } else { 611 if (val->value[i1] == '\0') {
589 is_range = 1; 612 is_range = 0;
590 i2 = i1 + strspn(val->value + i1, " \t"); 613 } else {
591 if (val->value[i2] != '-') { 614 is_range = 1;
592 X509V3err(X509V3_F_V2I_ASIDENTIFIERS, X509V3_R_INVALID_ASNUMBER); 615 i2 = i1 + strspn(val->value + i1, " \t");
593 X509V3_conf_err(val); 616 if (val->value[i2] != '-') {
594 goto err; 617 X509V3err(X509V3_F_V2I_ASIDENTIFIERS,
595 } 618 X509V3_R_INVALID_ASNUMBER);
596 i2++; 619 X509V3_conf_err(val);
597 i2 = i2 + strspn(val->value + i2, " \t"); 620 goto err;
598 i3 = i2 + strspn(val->value + i2, "0123456789"); 621 }
599 if (val->value[i3] != '\0') { 622 i2++;
600 X509V3err(X509V3_F_V2I_ASIDENTIFIERS, X509V3_R_INVALID_ASRANGE); 623 i2 = i2 + strspn(val->value + i2, " \t");
601 X509V3_conf_err(val); 624 i3 = i2 + strspn(val->value + i2, "0123456789");
602 goto err; 625 if (val->value[i3] != '\0') {
603 } 626 X509V3err(X509V3_F_V2I_ASIDENTIFIERS,
604 } 627 X509V3_R_INVALID_ASRANGE);
605 628 X509V3_conf_err(val);
606 /* 629 goto err;
607 * Syntax is ok, read and add it. 630 }
608 */ 631 }
609 if (!is_range) { 632
610 if (!X509V3_get_value_int(val, &min)) { 633 /*
611 X509V3err(X509V3_F_V2I_ASIDENTIFIERS, ERR_R_MALLOC_FAILURE); 634 * Syntax is ok, read and add it.
612 goto err; 635 */
613 } 636 if (!is_range) {
614 } else { 637 if (!X509V3_get_value_int(val, &min)) {
615 char *s = BUF_strdup(val->value); 638 X509V3err(X509V3_F_V2I_ASIDENTIFIERS,
616 if (s == NULL) { 639 ERR_R_MALLOC_FAILURE);
617 X509V3err(X509V3_F_V2I_ASIDENTIFIERS, ERR_R_MALLOC_FAILURE); 640 goto err;
618 goto err; 641 }
619 } 642 } else {
620 s[i1] = '\0'; 643 char *s = BUF_strdup(val->value);
621 min = s2i_ASN1_INTEGER(NULL, s); 644 if (s == NULL) {
622 max = s2i_ASN1_INTEGER(NULL, s + i2); 645 X509V3err(X509V3_F_V2I_ASIDENTIFIERS,
623 free(s); 646 ERR_R_MALLOC_FAILURE);
624 if (min == NULL || max == NULL) { 647 goto err;
625 X509V3err(X509V3_F_V2I_ASIDENTIFIERS, ERR_R_MALLOC_FAILURE); 648 }
626 goto err; 649 s[i1] = '\0';
627 } 650 min = s2i_ASN1_INTEGER(NULL, s);
628 if (ASN1_INTEGER_cmp(min, max) > 0) { 651 max = s2i_ASN1_INTEGER(NULL, s + i2);
629 X509V3err(X509V3_F_V2I_ASIDENTIFIERS, X509V3_R_EXTENSION_VALUE_ERROR); 652 free(s);
630 goto err; 653 if (min == NULL || max == NULL) {
631 } 654 X509V3err(X509V3_F_V2I_ASIDENTIFIERS,
632 } 655 ERR_R_MALLOC_FAILURE);
633 if (!v3_asid_add_id_or_range(asid, which, min, max)) { 656 goto err;
634 X509V3err(X509V3_F_V2I_ASIDENTIFIERS, ERR_R_MALLOC_FAILURE); 657 }
635 goto err; 658 if (ASN1_INTEGER_cmp(min, max) > 0) {
636 } 659 X509V3err(X509V3_F_V2I_ASIDENTIFIERS,
637 min = max = NULL; 660 X509V3_R_EXTENSION_VALUE_ERROR);
638 } 661 goto err;
639 662 }
640 /* 663 }
641 * Canonize the result, then we're done. 664 if (!v3_asid_add_id_or_range(asid, which, min, max)) {
642 */ 665 X509V3err(X509V3_F_V2I_ASIDENTIFIERS,
643 if (!v3_asid_canonize(asid)) 666 ERR_R_MALLOC_FAILURE);
644 goto err; 667 goto err;
645 return asid; 668 }
646 669 min = max = NULL;
647 err: 670 }
648 ASIdentifiers_free(asid); 671
649 ASN1_INTEGER_free(min); 672 /*
650 ASN1_INTEGER_free(max); 673 * Canonize the result, then we're done.
651 return NULL; 674 */
675 if (!v3_asid_canonize(asid))
676 goto err;
677 return asid;
678
679err:
680 ASIdentifiers_free(asid);
681 ASN1_INTEGER_free(min);
682 ASN1_INTEGER_free(max);
683 return NULL;
652} 684}
653 685
654/* 686/*
655 * OpenSSL dispatch. 687 * OpenSSL dispatch.
656 */ 688 */
657const X509V3_EXT_METHOD v3_asid = { 689const X509V3_EXT_METHOD v3_asid = {
658 NID_sbgp_autonomousSysNum, /* nid */ 690 NID_sbgp_autonomousSysNum, /* nid */
659 0, /* flags */ 691 0, /* flags */
660 ASN1_ITEM_ref(ASIdentifiers), /* template */ 692 ASN1_ITEM_ref(ASIdentifiers), /* template */
661 0, 0, 0, 0, /* old functions, ignored */ 693 0, 0, 0, 0, /* old functions, ignored */
662 0, /* i2s */ 694 0, /* i2s */
663 0, /* s2i */ 695 0, /* s2i */
664 0, /* i2v */ 696 0, /* i2v */
665 v2i_ASIdentifiers, /* v2i */ 697 v2i_ASIdentifiers, /* v2i */
666 i2r_ASIdentifiers, /* i2r */ 698 i2r_ASIdentifiers, /* i2r */
667 0, /* r2i */ 699 0, /* r2i */
668 NULL /* extension-specific data */ 700 NULL /* extension-specific data */
669}; 701};
670 702
671/* 703/*
672 * Figure out whether extension uses inheritance. 704 * Figure out whether extension uses inheritance.
673 */ 705 */
674int v3_asid_inherits(ASIdentifiers *asid) 706int
707v3_asid_inherits(ASIdentifiers *asid)
675{ 708{
676 return (asid != NULL && 709 return (asid != NULL &&
677 ((asid->asnum != NULL && 710 ((asid->asnum != NULL &&
678 asid->asnum->type == ASIdentifierChoice_inherit) || 711 asid->asnum->type == ASIdentifierChoice_inherit) ||
679 (asid->rdi != NULL && 712 (asid->rdi != NULL &&
680 asid->rdi->type == ASIdentifierChoice_inherit))); 713 asid->rdi->type == ASIdentifierChoice_inherit)));
681} 714}
682 715
683/* 716/*
684 * Figure out whether parent contains child. 717 * Figure out whether parent contains child.
685 */ 718 */
686static int asid_contains(ASIdOrRanges *parent, ASIdOrRanges *child) 719static int
720asid_contains(ASIdOrRanges *parent, ASIdOrRanges *child)
687{ 721{
688 ASN1_INTEGER *p_min, *p_max, *c_min, *c_max; 722 ASN1_INTEGER *p_min, *p_max, *c_min, *c_max;
689 int p, c; 723 int p, c;
690 724
691 if (child == NULL || parent == child) 725 if (child == NULL || parent == child)
692 return 1; 726 return 1;
693 if (parent == NULL) 727 if (parent == NULL)
694 return 0; 728 return 0;
695 729
696 p = 0; 730 p = 0;
697 for (c = 0; c < sk_ASIdOrRange_num(child); c++) { 731 for (c = 0; c < sk_ASIdOrRange_num(child); c++) {
698 extract_min_max(sk_ASIdOrRange_value(child, c), &c_min, &c_max); 732 extract_min_max(sk_ASIdOrRange_value(child, c),
699 for (;; p++) { 733 &c_min, &c_max);
700 if (p >= sk_ASIdOrRange_num(parent)) 734 for (; ; p++) {
701 return 0; 735 if (p >= sk_ASIdOrRange_num(parent))
702 extract_min_max(sk_ASIdOrRange_value(parent, p), &p_min, &p_max); 736 return 0;
703 if (ASN1_INTEGER_cmp(p_max, c_max) < 0) 737 extract_min_max(sk_ASIdOrRange_value(parent, p),
704 continue; 738 &p_min, &p_max);
705 if (ASN1_INTEGER_cmp(p_min, c_min) > 0) 739 if (ASN1_INTEGER_cmp(p_max, c_max) < 0)
706 return 0; 740 continue;
707 break; 741 if (ASN1_INTEGER_cmp(p_min, c_min) > 0)
708 } 742 return 0;
709 } 743 break;
744 }
745 }
710 746
711 return 1; 747 return 1;
712} 748}
713 749
714/* 750/*
715 * Test whether a is a subet of b. 751 * Test whether a is a subet of b.
716 */ 752 */
717int v3_asid_subset(ASIdentifiers *a, ASIdentifiers *b) 753int
754v3_asid_subset(ASIdentifiers *a, ASIdentifiers *b)
718{ 755{
719 return (a == NULL || 756 return (a == NULL || a == b ||
720 a == b || 757 (b != NULL && !v3_asid_inherits(a) && !v3_asid_inherits(b) &&
721 (b != NULL && 758 asid_contains(b->asnum->u.asIdsOrRanges,
722 !v3_asid_inherits(a) && 759 a->asnum->u.asIdsOrRanges) &&
723 !v3_asid_inherits(b) && 760 asid_contains(b->rdi->u.asIdsOrRanges,
724 asid_contains(b->asnum->u.asIdsOrRanges, 761 a->rdi->u.asIdsOrRanges)));
725 a->asnum->u.asIdsOrRanges) &&
726 asid_contains(b->rdi->u.asIdsOrRanges,
727 a->rdi->u.asIdsOrRanges)));
728} 762}
729 763
730/* 764/*
@@ -747,117 +781,120 @@ int v3_asid_subset(ASIdentifiers *a, ASIdentifiers *b)
747/* 781/*
748 * Core code for RFC 3779 3.3 path validation. 782 * Core code for RFC 3779 3.3 path validation.
749 */ 783 */
750static int v3_asid_validate_path_internal(X509_STORE_CTX *ctx, 784static int
751 STACK_OF(X509) *chain, 785v3_asid_validate_path_internal(X509_STORE_CTX *ctx, STACK_OF(X509) *chain,
752 ASIdentifiers *ext) 786 ASIdentifiers *ext)
753{ 787{
754 ASIdOrRanges *child_as = NULL, *child_rdi = NULL; 788 ASIdOrRanges *child_as = NULL, *child_rdi = NULL;
755 int i, ret = 1, inherit_as = 0, inherit_rdi = 0; 789 int i, ret = 1, inherit_as = 0, inherit_rdi = 0;
756 X509 *x; 790 X509 *x;
757 791
758 OPENSSL_assert(chain != NULL && sk_X509_num(chain) > 0); 792 OPENSSL_assert(chain != NULL && sk_X509_num(chain) > 0);
759 OPENSSL_assert(ctx != NULL || ext != NULL); 793 OPENSSL_assert(ctx != NULL || ext != NULL);
760 OPENSSL_assert(ctx == NULL || ctx->verify_cb != NULL); 794 OPENSSL_assert(ctx == NULL || ctx->verify_cb != NULL);
761 795
762 /* 796 /*
763 * Figure out where to start. If we don't have an extension to 797 * Figure out where to start. If we don't have an extension to
764 * check, we're done. Otherwise, check canonical form and 798 * check, we're done. Otherwise, check canonical form and
765 * set up for walking up the chain. 799 * set up for walking up the chain.
766 */ 800 */
767 if (ext != NULL) { 801 if (ext != NULL) {
768 i = -1; 802 i = -1;
769 x = NULL; 803 x = NULL;
770 } else { 804 } else {
771 i = 0; 805 i = 0;
772 x = sk_X509_value(chain, i); 806 x = sk_X509_value(chain, i);
773 OPENSSL_assert(x != NULL); 807 OPENSSL_assert(x != NULL);
774 if ((ext = x->rfc3779_asid) == NULL) 808 if ((ext = x->rfc3779_asid) == NULL)
775 goto done; 809 goto done;
776 } 810 }
777 if (!v3_asid_is_canonical(ext)) 811 if (!v3_asid_is_canonical(ext))
778 validation_err(X509_V_ERR_INVALID_EXTENSION); 812 validation_err(X509_V_ERR_INVALID_EXTENSION);
779 if (ext->asnum != NULL) { 813 if (ext->asnum != NULL) {
780 switch (ext->asnum->type) { 814 switch (ext->asnum->type) {
781 case ASIdentifierChoice_inherit: 815 case ASIdentifierChoice_inherit:
782 inherit_as = 1; 816 inherit_as = 1;
783 break; 817 break;
784 case ASIdentifierChoice_asIdsOrRanges: 818 case ASIdentifierChoice_asIdsOrRanges:
785 child_as = ext->asnum->u.asIdsOrRanges; 819 child_as = ext->asnum->u.asIdsOrRanges;
786 break; 820 break;
787 } 821 }
788 } 822 }
789 if (ext->rdi != NULL) { 823 if (ext->rdi != NULL) {
790 switch (ext->rdi->type) { 824 switch (ext->rdi->type) {
791 case ASIdentifierChoice_inherit: 825 case ASIdentifierChoice_inherit:
792 inherit_rdi = 1; 826 inherit_rdi = 1;
793 break; 827 break;
794 case ASIdentifierChoice_asIdsOrRanges: 828 case ASIdentifierChoice_asIdsOrRanges:
795 child_rdi = ext->rdi->u.asIdsOrRanges; 829 child_rdi = ext->rdi->u.asIdsOrRanges;
796 break; 830 break;
797 } 831 }
798 } 832 }
799 833
800 /* 834 /*
801 * Now walk up the chain. Extensions must be in canonical form, no 835 * Now walk up the chain. Extensions must be in canonical form, no
802 * cert may list resources that its parent doesn't list. 836 * cert may list resources that its parent doesn't list.
803 */ 837 */
804 for (i++; i < sk_X509_num(chain); i++) { 838 for (i++; i < sk_X509_num(chain); i++) {
805 x = sk_X509_value(chain, i); 839 x = sk_X509_value(chain, i);
806 OPENSSL_assert(x != NULL); 840 OPENSSL_assert(x != NULL);
807 if (x->rfc3779_asid == NULL) { 841 if (x->rfc3779_asid == NULL) {
808 if (child_as != NULL || child_rdi != NULL) 842 if (child_as != NULL || child_rdi != NULL)
809 validation_err(X509_V_ERR_UNNESTED_RESOURCE); 843 validation_err(X509_V_ERR_UNNESTED_RESOURCE);
810 continue; 844 continue;
811 } 845 }
812 if (!v3_asid_is_canonical(x->rfc3779_asid)) 846 if (!v3_asid_is_canonical(x->rfc3779_asid))
813 validation_err(X509_V_ERR_INVALID_EXTENSION); 847 validation_err(X509_V_ERR_INVALID_EXTENSION);
814 if (x->rfc3779_asid->asnum == NULL && child_as != NULL) { 848 if (x->rfc3779_asid->asnum == NULL && child_as != NULL) {
815 validation_err(X509_V_ERR_UNNESTED_RESOURCE); 849 validation_err(X509_V_ERR_UNNESTED_RESOURCE);
816 child_as = NULL; 850 child_as = NULL;
817 inherit_as = 0; 851 inherit_as = 0;
818 } 852 }
819 if (x->rfc3779_asid->asnum != NULL && 853 if (x->rfc3779_asid->asnum != NULL &&
820 x->rfc3779_asid->asnum->type == ASIdentifierChoice_asIdsOrRanges) { 854 x->rfc3779_asid->asnum->type ==
821 if (inherit_as || 855 ASIdentifierChoice_asIdsOrRanges) {
822 asid_contains(x->rfc3779_asid->asnum->u.asIdsOrRanges, child_as)) { 856 if (inherit_as || asid_contains(
823 child_as = x->rfc3779_asid->asnum->u.asIdsOrRanges; 857 x->rfc3779_asid->asnum->u.asIdsOrRanges,
824 inherit_as = 0; 858 child_as)) {
825 } else { 859 child_as = x->rfc3779_asid->asnum->u.asIdsOrRanges;
826 validation_err(X509_V_ERR_UNNESTED_RESOURCE); 860 inherit_as = 0;
827 } 861 } else {
828 } 862 validation_err(X509_V_ERR_UNNESTED_RESOURCE);
829 if (x->rfc3779_asid->rdi == NULL && child_rdi != NULL) { 863 }
830 validation_err(X509_V_ERR_UNNESTED_RESOURCE); 864 }
831 child_rdi = NULL; 865 if (x->rfc3779_asid->rdi == NULL && child_rdi != NULL) {
832 inherit_rdi = 0; 866 validation_err(X509_V_ERR_UNNESTED_RESOURCE);
833 } 867 child_rdi = NULL;
834 if (x->rfc3779_asid->rdi != NULL && 868 inherit_rdi = 0;
835 x->rfc3779_asid->rdi->type == ASIdentifierChoice_asIdsOrRanges) { 869 }
836 if (inherit_rdi || 870 if (x->rfc3779_asid->rdi != NULL &&
837 asid_contains(x->rfc3779_asid->rdi->u.asIdsOrRanges, child_rdi)) { 871 x->rfc3779_asid->rdi->type ==
838 child_rdi = x->rfc3779_asid->rdi->u.asIdsOrRanges; 872 ASIdentifierChoice_asIdsOrRanges) {
839 inherit_rdi = 0; 873 if (inherit_rdi || asid_contains(
840 } else { 874 x->rfc3779_asid->rdi->u.asIdsOrRanges, child_rdi)) {
841 validation_err(X509_V_ERR_UNNESTED_RESOURCE); 875 child_rdi = x->rfc3779_asid->rdi->u.asIdsOrRanges;
842 } 876 inherit_rdi = 0;
843 } 877 } else {
844 } 878 validation_err(X509_V_ERR_UNNESTED_RESOURCE);
845 879 }
846 /* 880 }
847 * Trust anchor can't inherit. 881 }
848 */ 882
849 OPENSSL_assert(x != NULL); 883 /*
850 if (x->rfc3779_asid != NULL) { 884 * Trust anchor can't inherit.
851 if (x->rfc3779_asid->asnum != NULL && 885 */
852 x->rfc3779_asid->asnum->type == ASIdentifierChoice_inherit) 886 OPENSSL_assert(x != NULL);
853 validation_err(X509_V_ERR_UNNESTED_RESOURCE); 887 if (x->rfc3779_asid != NULL) {
854 if (x->rfc3779_asid->rdi != NULL && 888 if (x->rfc3779_asid->asnum != NULL &&
855 x->rfc3779_asid->rdi->type == ASIdentifierChoice_inherit) 889 x->rfc3779_asid->asnum->type == ASIdentifierChoice_inherit)
856 validation_err(X509_V_ERR_UNNESTED_RESOURCE); 890 validation_err(X509_V_ERR_UNNESTED_RESOURCE);
857 } 891 if (x->rfc3779_asid->rdi != NULL &&
858 892 x->rfc3779_asid->rdi->type == ASIdentifierChoice_inherit)
859 done: 893 validation_err(X509_V_ERR_UNNESTED_RESOURCE);
860 return ret; 894 }
895
896done:
897 return ret;
861} 898}
862 899
863#undef validation_err 900#undef validation_err
@@ -865,26 +902,27 @@ static int v3_asid_validate_path_internal(X509_STORE_CTX *ctx,
865/* 902/*
866 * RFC 3779 3.3 path validation -- called from X509_verify_cert(). 903 * RFC 3779 3.3 path validation -- called from X509_verify_cert().
867 */ 904 */
868int v3_asid_validate_path(X509_STORE_CTX *ctx) 905int
906v3_asid_validate_path(X509_STORE_CTX *ctx)
869{ 907{
870 return v3_asid_validate_path_internal(ctx, ctx->chain, NULL); 908 return v3_asid_validate_path_internal(ctx, ctx->chain, NULL);
871} 909}
872 910
873/* 911/*
874 * RFC 3779 3.3 path validation of an extension. 912 * RFC 3779 3.3 path validation of an extension.
875 * Test whether chain covers extension. 913 * Test whether chain covers extension.
876 */ 914 */
877int v3_asid_validate_resource_set(STACK_OF(X509) *chain, 915int
878 ASIdentifiers *ext, 916v3_asid_validate_resource_set(STACK_OF(X509) *chain, ASIdentifiers *ext,
879 int allow_inheritance) 917 int allow_inheritance)
880{ 918{
881 if (ext == NULL) 919 if (ext == NULL)
882 return 1; 920 return 1;
883 if (chain == NULL || sk_X509_num(chain) == 0) 921 if (chain == NULL || sk_X509_num(chain) == 0)
884 return 0; 922 return 0;
885 if (!allow_inheritance && v3_asid_inherits(ext)) 923 if (!allow_inheritance && v3_asid_inherits(ext))
886 return 0; 924 return 0;
887 return v3_asid_validate_path_internal(NULL, chain, ext); 925 return v3_asid_validate_path_internal(NULL, chain, ext);
888} 926}
889 927
890#endif /* OPENSSL_NO_RFC3779 */ 928#endif /* OPENSSL_NO_RFC3779 */