cloning %any ID without zero-byte memleak
[strongswan.git] / src / libstrongswan / utils / identification.c
1 /**
2 * @file identification.c
3 *
4 * @brief Implementation of identification_t.
5 *
6 */
7
8 /*
9 * Copyright (C) 2005-2006 Martin Willi
10 * Copyright (C) 2005 Jan Hutter
11 * Hochschule fuer Technik Rapperswil
12 *
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2 of the License, or (at your
16 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
17 *
18 * This program is distributed in the hope that it will be useful, but
19 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 * for more details.
22 */
23
24 #define _GNU_SOURCE
25 #include <sys/socket.h>
26 #include <netinet/in.h>
27 #include <arpa/inet.h>
28 #include <string.h>
29 #include <stdio.h>
30 #include <ctype.h>
31 #include <printf.h>
32
33 #include "identification.h"
34
35 #include <asn1/asn1.h>
36
37 ENUM_BEGIN(id_type_names, ID_ANY, ID_KEY_ID,
38 "ID_ANY",
39 "ID_IPV4_ADDR",
40 "ID_FQDN",
41 "ID_RFC822_ADDR",
42 "ID_IPV4_ADDR_SUBNET",
43 "ID_IPV6_ADDR",
44 "ID_IPV6_ADDR_SUBNET",
45 "ID_IPV4_ADDR_RANGE",
46 "ID_IPV6_ADDR_RANGE",
47 "ID_DER_ASN1_DN",
48 "ID_DER_ASN1_GN",
49 "ID_KEY_ID");
50 ENUM_NEXT(id_type_names, ID_DER_ASN1_GN_URI, ID_DER_ASN1_GN_URI, ID_KEY_ID,
51 "ID_DER_ASN1_GN_URI");
52 ENUM_END(id_type_names, ID_DER_ASN1_GN_URI);
53
54
55 /**
56 * X.501 acronyms for well known object identifiers (OIDs)
57 */
58 static u_char oid_ND[] = {
59 0x02, 0x82, 0x06, 0x01, 0x0A, 0x07, 0x14
60 };
61 static u_char oid_UID[] = {
62 0x09, 0x92, 0x26, 0x89, 0x93, 0xF2, 0x2C, 0x64, 0x01, 0x01
63 };
64 static u_char oid_DC[] = {
65 0x09, 0x92, 0x26, 0x89, 0x93, 0xF2, 0x2C, 0x64, 0x01, 0x19
66 };
67 static u_char oid_CN[] = {
68 0x55, 0x04, 0x03
69 };
70 static u_char oid_S[] = {
71 0x55, 0x04, 0x04
72 };
73 static u_char oid_SN[] = {
74 0x55, 0x04, 0x05
75 };
76 static u_char oid_C[] = {
77 0x55, 0x04, 0x06
78 };
79 static u_char oid_L[] = {
80 0x55, 0x04, 0x07
81 };
82 static u_char oid_ST[] = {
83 0x55, 0x04, 0x08
84 };
85 static u_char oid_O[] = {
86 0x55, 0x04, 0x0A
87 };
88 static u_char oid_OU[] = {
89 0x55, 0x04, 0x0B
90 };
91 static u_char oid_T[] = {
92 0x55, 0x04, 0x0C
93 };
94 static u_char oid_D[] = {
95 0x55, 0x04, 0x0D
96 };
97 static u_char oid_N[] = {
98 0x55, 0x04, 0x29
99 };
100 static u_char oid_G[] = {
101 0x55, 0x04, 0x2A
102 };
103 static u_char oid_I[] = {
104 0x55, 0x04, 0x2B
105 };
106 static u_char oid_ID[] = {
107 0x55, 0x04, 0x2D
108 };
109 static u_char oid_EN[] = {
110 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x42, 0x03, 0x01, 0x03
111 };
112 static u_char oid_E[] = {
113 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09, 0x01
114 };
115 static u_char oid_UN[] = {
116 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09, 0x02
117 };
118 static u_char oid_TCGID[] = {
119 0x2B, 0x06, 0x01, 0x04, 0x01, 0x89, 0x31, 0x01, 0x01, 0x02, 0x02, 0x4B
120 };
121
122 /**
123 * coding of X.501 distinguished name
124 */
125 typedef struct {
126 const u_char *name;
127 chunk_t oid;
128 u_char type;
129 } x501rdn_t;
130
131 static const x501rdn_t x501rdns[] = {
132 {"ND", {oid_ND, 7}, ASN1_PRINTABLESTRING},
133 {"UID", {oid_UID, 10}, ASN1_PRINTABLESTRING},
134 {"DC", {oid_DC, 10}, ASN1_PRINTABLESTRING},
135 {"CN", {oid_CN, 3}, ASN1_PRINTABLESTRING},
136 {"S", {oid_S, 3}, ASN1_PRINTABLESTRING},
137 {"SN", {oid_SN, 3}, ASN1_PRINTABLESTRING},
138 {"serialNumber", {oid_SN, 3}, ASN1_PRINTABLESTRING},
139 {"C", {oid_C, 3}, ASN1_PRINTABLESTRING},
140 {"L", {oid_L, 3}, ASN1_PRINTABLESTRING},
141 {"ST", {oid_ST, 3}, ASN1_PRINTABLESTRING},
142 {"O", {oid_O, 3}, ASN1_PRINTABLESTRING},
143 {"OU", {oid_OU, 3}, ASN1_PRINTABLESTRING},
144 {"T", {oid_T, 3}, ASN1_PRINTABLESTRING},
145 {"D", {oid_D, 3}, ASN1_PRINTABLESTRING},
146 {"N", {oid_N, 3}, ASN1_PRINTABLESTRING},
147 {"G", {oid_G, 3}, ASN1_PRINTABLESTRING},
148 {"I", {oid_I, 3}, ASN1_PRINTABLESTRING},
149 {"ID", {oid_ID, 3}, ASN1_PRINTABLESTRING},
150 {"EN", {oid_EN, 10}, ASN1_PRINTABLESTRING},
151 {"employeeNumber", {oid_EN, 10}, ASN1_PRINTABLESTRING},
152 {"E", {oid_E, 9}, ASN1_IA5STRING},
153 {"Email", {oid_E, 9}, ASN1_IA5STRING},
154 {"emailAddress", {oid_E, 9}, ASN1_IA5STRING},
155 {"UN", {oid_UN, 9}, ASN1_IA5STRING},
156 {"unstructuredName",{oid_UN, 9}, ASN1_IA5STRING},
157 {"TCGID", {oid_TCGID, 12}, ASN1_PRINTABLESTRING}
158 };
159 #define X501_RDN_ROOF 26
160
161 /**
162 * maximum number of RDNs in atodn()
163 */
164 #define RDN_MAX 20
165
166
167 typedef struct private_identification_t private_identification_t;
168
169 /**
170 * Private data of an identification_t object.
171 */
172 struct private_identification_t {
173 /**
174 * Public interface.
175 */
176 identification_t public;
177
178 /**
179 * Encoded representation of this ID.
180 */
181 chunk_t encoded;
182
183 /**
184 * Type of this ID.
185 */
186 id_type_t type;
187 };
188
189 static private_identification_t *identification_create(void);
190
191 /**
192 * updates a chunk (!????)
193 * TODO: We should reconsider this stuff, its not really clear
194 */
195 static void update_chunk(chunk_t *ch, int n)
196 {
197 n = (n > -1 && n < (int)ch->len)? n : (int)ch->len-1;
198 ch->ptr += n; ch->len -= n;
199 }
200
201 /**
202 * Prints a binary string in hexadecimal form
203 */
204 void hex_str(chunk_t bin, chunk_t *str)
205 {
206 u_int i;
207 update_chunk(str, snprintf(str->ptr,str->len,"0x"));
208 for (i = 0; i < bin.len; i++)
209 {
210 update_chunk(str, snprintf(str->ptr,str->len,"%02X",*bin.ptr++));
211 }
212 }
213
214 /**
215 * Remove any malicious characters from a chunk. We are very restrictive, but
216 * whe use these strings only to present it to the user.
217 */
218 static chunk_t sanitize_chunk(chunk_t chunk)
219 {
220 char *pos;
221 chunk_t clone = chunk_clone(chunk);
222
223 for (pos = clone.ptr; pos < (char*)(clone.ptr + clone.len); pos++)
224 {
225 switch (*pos)
226 {
227 case '\0':
228 case ' ':
229 case '*':
230 case '-':
231 case '.':
232 case '/':
233 case '0' ... '9':
234 case ':':
235 case '=':
236 case '@':
237 case 'A' ... 'Z':
238 case '_':
239 case 'a' ... 'z':
240 break;
241 default:
242 *pos = '?';
243 }
244 }
245 return clone;
246 }
247
248 /**
249 * Pointer is set to the first RDN in a DN
250 */
251 static status_t init_rdn(chunk_t dn, chunk_t *rdn, chunk_t *attribute, bool *next)
252 {
253 *rdn = chunk_empty;
254 *attribute = chunk_empty;
255
256 /* a DN is a SEQUENCE OF RDNs */
257 if (*dn.ptr != ASN1_SEQUENCE)
258 {
259 /* DN is not a SEQUENCE */
260 return FAILED;
261 }
262
263 rdn->len = asn1_length(&dn);
264
265 if (rdn->len == ASN1_INVALID_LENGTH)
266 {
267 /* Invalid RDN length */
268 return FAILED;
269 }
270
271 rdn->ptr = dn.ptr;
272
273 /* are there any RDNs ? */
274 *next = rdn->len > 0;
275
276 return SUCCESS;
277 }
278
279 /**
280 * Fetches the next RDN in a DN
281 */
282 static status_t get_next_rdn(chunk_t *rdn, chunk_t * attribute, chunk_t *oid, chunk_t *value, asn1_t *type, bool *next)
283 {
284 chunk_t body;
285
286 /* initialize return values */
287 *oid = chunk_empty;
288 *value = chunk_empty;
289
290 /* if all attributes have been parsed, get next rdn */
291 if (attribute->len <= 0)
292 {
293 /* an RDN is a SET OF attributeTypeAndValue */
294 if (*rdn->ptr != ASN1_SET)
295 {
296 /* RDN is not a SET */
297 return FAILED;
298 }
299 attribute->len = asn1_length(rdn);
300 if (attribute->len == ASN1_INVALID_LENGTH)
301 {
302 /* Invalid attribute length */
303 return FAILED;
304 }
305 attribute->ptr = rdn->ptr;
306 /* advance to start of next RDN */
307 rdn->ptr += attribute->len;
308 rdn->len -= attribute->len;
309 }
310
311 /* an attributeTypeAndValue is a SEQUENCE */
312 if (*attribute->ptr != ASN1_SEQUENCE)
313 {
314 /* attributeTypeAndValue is not a SEQUENCE */
315 return FAILED;
316 }
317
318 /* extract the attribute body */
319 body.len = asn1_length(attribute);
320
321 if (body.len == ASN1_INVALID_LENGTH)
322 {
323 /* Invalid attribute body length */
324 return FAILED;
325 }
326
327 body.ptr = attribute->ptr;
328
329 /* advance to start of next attribute */
330 attribute->ptr += body.len;
331 attribute->len -= body.len;
332
333 /* attribute type is an OID */
334 if (*body.ptr != ASN1_OID)
335 {
336 /* attributeType is not an OID */
337 return FAILED;
338 }
339 /* extract OID */
340 oid->len = asn1_length(&body);
341
342 if (oid->len == ASN1_INVALID_LENGTH)
343 {
344 /* Invalid attribute OID length */
345 return FAILED;
346 }
347 oid->ptr = body.ptr;
348
349 /* advance to the attribute value */
350 body.ptr += oid->len;
351 body.len -= oid->len;
352
353 /* extract string type */
354 *type = *body.ptr;
355
356 /* extract string value */
357 value->len = asn1_length(&body);
358
359 if (value->len == ASN1_INVALID_LENGTH)
360 {
361 /* Invalid attribute string length */
362 return FAILED;
363 }
364 value->ptr = body.ptr;
365
366 /* are there any RDNs left? */
367 *next = rdn->len > 0 || attribute->len > 0;
368 return SUCCESS;
369 }
370
371 /**
372 * Parses an ASN.1 distinguished name int its OID/value pairs
373 */
374 static status_t dntoa(chunk_t dn, chunk_t *str)
375 {
376 chunk_t rdn, oid, attribute, value, proper;
377 asn1_t type;
378 int oid_code;
379 bool next;
380 bool first = TRUE;
381
382 status_t status = init_rdn(dn, &rdn, &attribute, &next);
383
384 if (status != SUCCESS)
385 return status;
386
387 while (next)
388 {
389 status = get_next_rdn(&rdn, &attribute, &oid, &value, &type, &next);
390
391 if (status != SUCCESS)
392 return status;
393
394 if (first)
395 { /* first OID/value pair */
396 first = FALSE;
397 }
398 else
399 { /* separate OID/value pair by a comma */
400 update_chunk(str, snprintf(str->ptr,str->len,", "));
401 }
402
403 /* print OID */
404 oid_code = known_oid(oid);
405 if (oid_code == OID_UNKNOWN)
406 { /* OID not found in list */
407 hex_str(oid, str);
408 }
409 else
410 {
411 update_chunk(str, snprintf(str->ptr,str->len,"%s", oid_names[oid_code].name));
412 }
413 /* print value */
414 proper = sanitize_chunk(value);
415 update_chunk(str, snprintf(str->ptr,str->len,"=%.*s", (int)proper.len, proper.ptr));
416 chunk_free(&proper);
417 }
418 return SUCCESS;
419 }
420
421 /**
422 * compare two distinguished names by
423 * comparing the individual RDNs
424 */
425 static bool same_dn(chunk_t a, chunk_t b)
426 {
427 chunk_t rdn_a, rdn_b, attribute_a, attribute_b;
428 chunk_t oid_a, oid_b, value_a, value_b;
429 asn1_t type_a, type_b;
430 bool next_a, next_b;
431
432 /* same lengths for the DNs */
433 if (a.len != b.len)
434 return FALSE;
435
436 /* try a binary comparison first */
437 if (memeq(a.ptr, b.ptr, b.len))
438 return TRUE;
439
440 /* initialize DN parsing */
441 if (init_rdn(a, &rdn_a, &attribute_a, &next_a) != SUCCESS
442 || init_rdn(b, &rdn_b, &attribute_b, &next_b) != SUCCESS)
443 {
444 return FALSE;
445 }
446
447 /* fetch next RDN pair */
448 while (next_a && next_b)
449 {
450 /* parse next RDNs and check for errors */
451 if (get_next_rdn(&rdn_a, &attribute_a, &oid_a, &value_a, &type_a, &next_a) != SUCCESS
452 || get_next_rdn(&rdn_b, &attribute_b, &oid_b, &value_b, &type_b, &next_b) != SUCCESS)
453 {
454 return FALSE;
455 }
456
457 /* OIDs must agree */
458 if (oid_a.len != oid_b.len || memcmp(oid_a.ptr, oid_b.ptr, oid_b.len) != 0)
459 return FALSE;
460
461 /* same lengths for values */
462 if (value_a.len != value_b.len)
463 return FALSE;
464
465 /* printableStrings and email RDNs require uppercase comparison */
466 if (type_a == type_b && (type_a == ASN1_PRINTABLESTRING
467 || (type_a == ASN1_IA5STRING && known_oid(oid_a) == OID_PKCS9_EMAIL)))
468 {
469 if (strncasecmp(value_a.ptr, value_b.ptr, value_b.len) != 0)
470 return FALSE;
471 }
472 else
473 {
474 if (strncmp(value_a.ptr, value_b.ptr, value_b.len) != 0)
475 return FALSE;
476 }
477 }
478 /* both DNs must have same number of RDNs */
479 if (next_a || next_b)
480 return FALSE;
481
482 /* the two DNs are equal! */
483 return TRUE;
484 }
485
486
487 /**
488 * compare two distinguished names by comparing the individual RDNs.
489 * A single'*' character designates a wildcard RDN in DN b.
490 * TODO: Add support for different RDN order in DN !!
491 */
492 bool match_dn(chunk_t a, chunk_t b, int *wildcards)
493 {
494 chunk_t rdn_a, rdn_b, attribute_a, attribute_b;
495 chunk_t oid_a, oid_b, value_a, value_b;
496 asn1_t type_a, type_b;
497 bool next_a, next_b;
498
499 /* initialize wildcard counter */
500 if (wildcards)
501 {
502 *wildcards = 0;
503 }
504
505 /* initialize DN parsing */
506 if (init_rdn(a, &rdn_a, &attribute_a, &next_a) != SUCCESS
507 || init_rdn(b, &rdn_b, &attribute_b, &next_b) != SUCCESS)
508 {
509 return FALSE;
510 }
511
512 /* fetch next RDN pair */
513 while (next_a && next_b)
514 {
515 /* parse next RDNs and check for errors */
516 if (get_next_rdn(&rdn_a, &attribute_a, &oid_a, &value_a, &type_a, &next_a) != SUCCESS
517 || get_next_rdn(&rdn_b, &attribute_b, &oid_b, &value_b, &type_b, &next_b) != SUCCESS)
518 {
519 return FALSE;
520 }
521 /* OIDs must agree */
522 if (oid_a.len != oid_b.len || memcmp(oid_a.ptr, oid_b.ptr, oid_b.len) != 0)
523 return FALSE;
524
525 /* does rdn_b contain a wildcard? */
526 if (value_b.len == 1 && *value_b.ptr == '*')
527 {
528 if (wildcards)
529 {
530 (*wildcards)++;
531 }
532 continue;
533 }
534 /* same lengths for values */
535 if (value_a.len != value_b.len)
536 return FALSE;
537
538 /* printableStrings and email RDNs require uppercase comparison */
539 if (type_a == type_b && (type_a == ASN1_PRINTABLESTRING
540 || (type_a == ASN1_IA5STRING && known_oid(oid_a) == OID_PKCS9_EMAIL)))
541 {
542 if (strncasecmp(value_a.ptr, value_b.ptr, value_b.len) != 0)
543 return FALSE;
544 }
545 else
546 {
547 if (strncmp(value_a.ptr, value_b.ptr, value_b.len) != 0)
548 return FALSE;
549 }
550 }
551 /* both DNs must have same number of RDNs */
552 if (next_a || next_b)
553 {
554 return FALSE;
555 }
556
557 /* the two DNs match! */
558 if (wildcards)
559 {
560 *wildcards = min(*wildcards, MAX_WILDCARDS);
561 }
562 return TRUE;
563 }
564
565 /**
566 * Converts an LDAP-style human-readable ASCII-encoded
567 * ASN.1 distinguished name into binary DER-encoded format
568 */
569 static status_t atodn(char *src, chunk_t *dn)
570 {
571 /* finite state machine for atodn */
572 typedef enum {
573 SEARCH_OID = 0,
574 READ_OID = 1,
575 SEARCH_NAME = 2,
576 READ_NAME = 3,
577 UNKNOWN_OID = 4
578 } state_t;
579
580 chunk_t oid = chunk_empty;
581 chunk_t name = chunk_empty;
582 chunk_t rdns[RDN_MAX];
583 int rdn_count = 0;
584 int dn_len = 0;
585 int whitespace = 0;
586 int i = 0;
587 asn1_t rdn_type;
588 state_t state = SEARCH_OID;
589 status_t status = SUCCESS;
590
591 do
592 {
593 switch (state)
594 {
595 case SEARCH_OID:
596 if (*src != ' ' && *src != '/' && *src != ',')
597 {
598 oid.ptr = src;
599 oid.len = 1;
600 state = READ_OID;
601 }
602 break;
603 case READ_OID:
604 if (*src != ' ' && *src != '=')
605 {
606 oid.len++;
607 }
608 else
609 {
610 for (i = 0; i < X501_RDN_ROOF; i++)
611 {
612 if (strlen(x501rdns[i].name) == oid.len
613 && strncasecmp(x501rdns[i].name, oid.ptr, oid.len) == 0)
614 {
615 break; /* found a valid OID */
616 }
617 }
618 if (i == X501_RDN_ROOF)
619 {
620 status = NOT_SUPPORTED;
621 state = UNKNOWN_OID;
622 break;
623 }
624 /* reset oid and change state */
625 oid = chunk_empty;
626 state = SEARCH_NAME;
627 }
628 break;
629 case SEARCH_NAME:
630 if (*src != ' ' && *src != '=')
631 {
632 name.ptr = src;
633 name.len = 1;
634 whitespace = 0;
635 state = READ_NAME;
636 }
637 break;
638 case READ_NAME:
639 if (*src != ',' && *src != '/' && *src != '\0')
640 {
641 name.len++;
642 if (*src == ' ')
643 whitespace++;
644 else
645 whitespace = 0;
646 }
647 else
648 {
649 name.len -= whitespace;
650 rdn_type = (x501rdns[i].type == ASN1_PRINTABLESTRING
651 && !is_printablestring(name))? ASN1_T61STRING : x501rdns[i].type;
652
653 if (rdn_count < RDN_MAX)
654 {
655 rdns[rdn_count] =
656 asn1_wrap(ASN1_SET, "m",
657 asn1_wrap(ASN1_SEQUENCE, "mm",
658 asn1_wrap(ASN1_OID, "c", x501rdns[i].oid),
659 asn1_wrap(rdn_type, "c", name)
660 )
661 );
662 dn_len += rdns[rdn_count++].len;
663 }
664 else
665 {
666 status = OUT_OF_RES;
667 }
668 /* reset name and change state */
669 name = chunk_empty;
670 state = SEARCH_OID;
671 }
672 break;
673 case UNKNOWN_OID:
674 break;
675 }
676 } while (*src++ != '\0');
677
678 /* build the distinguished name sequence */
679 {
680 int i;
681 u_char *pos = build_asn1_object(dn, ASN1_SEQUENCE, dn_len);
682
683 for (i = 0; i < rdn_count; i++)
684 {
685 memcpy(pos, rdns[i].ptr, rdns[i].len);
686 pos += rdns[i].len;
687 free(rdns[i].ptr);
688 }
689 }
690
691 if (status != SUCCESS)
692 {
693 free(dn->ptr);
694 *dn = chunk_empty;
695 }
696 return status;
697 }
698
699 /**
700 * Implementation of identification_t.get_encoding.
701 */
702 static chunk_t get_encoding(private_identification_t *this)
703 {
704 return this->encoded;
705 }
706
707 /**
708 * Implementation of identification_t.get_type.
709 */
710 static id_type_t get_type(private_identification_t *this)
711 {
712 return this->type;
713 }
714
715 /**
716 * Implementation of identification_t.contains_wildcards.
717 */
718 static bool contains_wildcards(private_identification_t *this)
719 {
720 switch (this->type)
721 {
722 case ID_ANY:
723 return TRUE;
724 case ID_FQDN:
725 case ID_RFC822_ADDR:
726 return memchr(this->encoded.ptr, '*', this->encoded.len) != NULL;
727 case ID_DER_ASN1_DN:
728 /* TODO */
729 default:
730 return FALSE;
731
732 }
733 }
734
735 /**
736 * Default implementation of identification_t.equals.
737 * compares encoded chunk for equality.
738 */
739 static bool equals_binary(private_identification_t *this, private_identification_t *other)
740 {
741 if (this->type == other->type)
742 {
743 if (this->type == ID_ANY)
744 {
745 return TRUE;
746 }
747 return chunk_equals(this->encoded, other->encoded);
748 }
749 return FALSE;
750 }
751
752 /**
753 * Special implementation of identification_t.equals for ID_DER_ASN1_DN.
754 */
755 static bool equals_dn(private_identification_t *this,
756 private_identification_t *other)
757 {
758 return same_dn(this->encoded, other->encoded);
759 }
760
761 /**
762 * Default implementation of identification_t.matches.
763 */
764 static bool matches_binary(private_identification_t *this,
765 private_identification_t *other, int *wildcards)
766 {
767 if (other->type == ID_ANY)
768 {
769 if (wildcards)
770 {
771 *wildcards = MAX_WILDCARDS;
772 }
773 return TRUE;
774 }
775 if (wildcards)
776 {
777 *wildcards = 0;
778 }
779 return this->type == other->type &&
780 chunk_equals(this->encoded, other->encoded);
781 }
782
783 /**
784 * Special implementation of identification_t.matches for ID_RFC822_ADDR/ID_FQDN.
785 * Checks for a wildcard in other-string, and compares it against this-string.
786 */
787 static bool matches_string(private_identification_t *this,
788 private_identification_t *other, int *wildcards)
789 {
790 u_int len = other->encoded.len;
791
792 if (other->type == ID_ANY)
793 {
794 if (wildcards)
795 {
796 *wildcards = MAX_WILDCARDS;
797 }
798 return TRUE;
799 }
800
801 if (this->type != other->type)
802 return FALSE;
803
804 /* try a binary comparison first */
805 if (equals_binary(this, other))
806 {
807 if (wildcards)
808 {
809 *wildcards = 0;
810 }
811 return TRUE;
812 }
813
814 if (len == 0 || this->encoded.len < len)
815 return FALSE;
816
817 /* check for single wildcard at the head of the string */
818 if (*other->encoded.ptr == '*')
819 {
820 if (wildcards)
821 {
822 *wildcards = 1;
823 }
824
825 /* single asterisk matches any string */
826 if (len-- == 1)
827 return TRUE;
828
829 if (memeq(this->encoded.ptr + this->encoded.len - len, other->encoded.ptr + 1, len))
830 return TRUE;
831 }
832
833 return FALSE;
834 }
835
836 /**
837 * Special implementation of identification_t.matches for ID_ANY.
838 * ANY matches only another ANY, but nothing other
839 */
840 static bool matches_any(private_identification_t *this,
841 private_identification_t *other, int *wildcards)
842 {
843 if (wildcards)
844 {
845 *wildcards = 0;
846 }
847 return other->type == ID_ANY;
848 }
849
850 /**
851 * Special implementation of identification_t.matches for ID_DER_ASN1_DN.
852 * ANY matches any, even ANY, thats why its there...
853 */
854 static bool matches_dn(private_identification_t *this,
855 private_identification_t *other, int *wildcards)
856 {
857 if (other->type == ID_ANY)
858 {
859 if (wildcards)
860 {
861 *wildcards = MAX_WILDCARDS;
862 }
863 return TRUE;
864 }
865
866 if (this->type == other->type)
867 {
868 return match_dn(this->encoded, other->encoded, wildcards);
869 }
870 return FALSE;
871 }
872
873 /**
874 * output handler in printf()
875 */
876 static int print(FILE *stream, const struct printf_info *info,
877 const void *const *args)
878 {
879 private_identification_t *this = *((private_identification_t**)(args[0]));
880 char buf[BUF_LEN];
881 chunk_t proper, buf_chunk = chunk_from_buf(buf);
882 int written;
883
884 if (this == NULL)
885 {
886 return fprintf(stream, "(null)");
887 }
888
889 switch (this->type)
890 {
891 case ID_ANY:
892 return fprintf(stream, "%%any");
893 case ID_IPV4_ADDR:
894 if (this->encoded.len < sizeof(struct in_addr) ||
895 inet_ntop(AF_INET, this->encoded.ptr, buf, sizeof(buf)) == NULL)
896 {
897 return fprintf(stream, "(invalid ID_IPV4_ADDR)");
898 }
899 else
900 {
901 return fprintf(stream, "%s", buf);
902 }
903 case ID_IPV6_ADDR:
904 if (this->encoded.len < sizeof(struct in6_addr) ||
905 inet_ntop(AF_INET6, this->encoded.ptr, buf, INET6_ADDRSTRLEN) == NULL)
906 {
907 return fprintf(stream, "(invalid ID_IPV6_ADDR)");
908 }
909 else
910 {
911 return fprintf(stream, "%s", buf);
912 }
913 case ID_FQDN:
914 {
915 proper = sanitize_chunk(this->encoded);
916 written = fprintf(stream, "@%.*s", proper.len, proper.ptr);
917 chunk_free(&proper);
918 return written;
919 }
920 case ID_RFC822_ADDR:
921 {
922 proper = sanitize_chunk(this->encoded);
923 written = fprintf(stream, "%.*s", proper.len, proper.ptr);
924 chunk_free(&proper);
925 return written;
926 }
927 case ID_DER_ASN1_DN:
928 {
929 snprintf(buf, sizeof(buf), "%.*s", this->encoded.len, this->encoded.ptr);
930 /* TODO: whats returned on failure?*/
931 dntoa(this->encoded, &buf_chunk);
932 return fprintf(stream, "%s", buf);
933 }
934 case ID_DER_ASN1_GN:
935 return fprintf(stream, "(ASN.1 general Name");
936 case ID_KEY_ID:
937 return fprintf(stream, "(KEY_ID)");
938 case ID_DER_ASN1_GN_URI:
939 {
940 proper = sanitize_chunk(this->encoded);
941 written = fprintf(stream, "%.*s", proper.len, proper.ptr);
942 chunk_free(&proper);
943 return written;
944 }
945 default:
946 return fprintf(stream, "(unknown ID type: %d)", this->type);
947 }
948 }
949
950 /**
951 * register printf() handlers
952 */
953 static void __attribute__ ((constructor))print_register()
954 {
955 register_printf_function(PRINTF_IDENTIFICATION, print, arginfo_ptr);
956 }
957
958 /**
959 * Implementation of identification_t.clone.
960 */
961 static identification_t *clone_(private_identification_t *this)
962 {
963 private_identification_t *clone = identification_create();
964
965 clone->type = this->type;
966 if (this->encoded.len)
967 {
968 clone->encoded = chunk_clone(this->encoded);
969 }
970 clone->public.equals = this->public.equals;
971 clone->public.matches = this->public.matches;
972
973 return &clone->public;
974 }
975
976 /**
977 * Implementation of identification_t.destroy.
978 */
979 static void destroy(private_identification_t *this)
980 {
981 chunk_free(&this->encoded);
982 free(this);
983 }
984
985 /**
986 * Generic constructor used for the other constructors.
987 */
988 static private_identification_t *identification_create(void)
989 {
990 private_identification_t *this = malloc_thing(private_identification_t);
991
992 this->public.get_encoding = (chunk_t (*) (identification_t*))get_encoding;
993 this->public.get_type = (id_type_t (*) (identification_t*))get_type;
994 this->public.contains_wildcards = (bool (*) (identification_t *this))contains_wildcards;
995 this->public.clone = (identification_t* (*) (identification_t*))clone_;
996 this->public.destroy = (void (*) (identification_t*))destroy;
997 /* we use these as defaults, the may be overloaded for special ID types */
998 this->public.equals = (bool (*) (identification_t*,identification_t*))equals_binary;
999 this->public.matches = (bool (*) (identification_t*,identification_t*,int*))matches_binary;
1000
1001 this->encoded = chunk_empty;
1002
1003 return this;
1004 }
1005
1006 /*
1007 * Described in header.
1008 */
1009 identification_t *identification_create_from_string(char *string)
1010 {
1011 private_identification_t *this = identification_create();
1012
1013 if (string == NULL)
1014 {
1015 string = "%any";
1016 }
1017 if (strchr(string, '=') != NULL)
1018 {
1019 /* we interpret this as an ASCII X.501 ID_DER_ASN1_DN.
1020 * convert from LDAP style or openssl x509 -subject style to ASN.1 DN
1021 */
1022 if (atodn(string, &this->encoded) != SUCCESS)
1023 {
1024 free(this);
1025 return NULL;
1026 }
1027 this->type = ID_DER_ASN1_DN;
1028 this->public.equals = (bool (*) (identification_t*,identification_t*))equals_dn;
1029 this->public.matches = (bool (*) (identification_t*,identification_t*,int*))matches_dn;
1030 return &this->public;
1031 }
1032 else if (strchr(string, '@') == NULL)
1033 {
1034 if (streq(string, "%any")
1035 || streq(string, "0.0.0.0")
1036 || streq(string, "*")
1037 || streq(string, "::")
1038 || streq(string, "0::0"))
1039 {
1040 /* any ID will be accepted */
1041 this->type = ID_ANY;
1042 this->public.matches = (bool (*)
1043 (identification_t*,identification_t*,int*))matches_any;
1044 return &this->public;
1045 }
1046 else
1047 {
1048 if (strchr(string, ':') == NULL)
1049 {
1050 /* try IPv4 */
1051 struct in_addr address;
1052 chunk_t chunk = {(void*)&address, sizeof(address)};
1053
1054 if (inet_pton(AF_INET, string, &address) <= 0)
1055 {
1056 free(this);
1057 return NULL;
1058 }
1059 this->encoded = chunk_clone(chunk);
1060 this->type = ID_IPV4_ADDR;
1061 return &(this->public);
1062 }
1063 else
1064 {
1065 /* try IPv6 */
1066 struct in6_addr address;
1067 chunk_t chunk = {(void*)&address, sizeof(address)};
1068
1069 if (inet_pton(AF_INET6, string, &address) <= 0)
1070 {
1071 free(this);
1072 return NULL;
1073 }
1074 this->encoded = chunk_clone(chunk);
1075 this->type = ID_IPV6_ADDR;
1076 return &(this->public);
1077 }
1078 }
1079 }
1080 else
1081 {
1082 if (*string == '@')
1083 {
1084 if (*(string + 1) == '#')
1085 {
1086 /* TODO: Pluto handles '#' as hex encoded ID_KEY_ID. */
1087 free(this);
1088 return NULL;
1089 }
1090 else
1091 {
1092 this->type = ID_FQDN;
1093 this->encoded.ptr = strdup(string + 1);
1094 this->encoded.len = strlen(string + 1);
1095 this->public.matches = (bool (*)
1096 (identification_t*,identification_t*,int*))matches_string;
1097 return &(this->public);
1098 }
1099 }
1100 else
1101 {
1102 this->type = ID_RFC822_ADDR;
1103 this->encoded.ptr = strdup(string);
1104 this->encoded.len = strlen(string);
1105 this->public.matches = (bool (*)
1106 (identification_t*,identification_t*,int*))matches_string;
1107 return &(this->public);
1108 }
1109 }
1110 }
1111
1112 /*
1113 * Described in header.
1114 */
1115 identification_t *identification_create_from_encoding(id_type_t type, chunk_t encoded)
1116 {
1117 private_identification_t *this = identification_create();
1118 this->type = type;
1119 switch (type)
1120 {
1121 case ID_ANY:
1122 this->public.matches = (bool (*)
1123 (identification_t*,identification_t*,int*))matches_any;
1124 break;
1125 case ID_FQDN:
1126 this->public.matches = (bool (*)
1127 (identification_t*,identification_t*,int*))matches_string;
1128 break;
1129 case ID_RFC822_ADDR:
1130 this->public.matches = (bool (*)
1131 (identification_t*,identification_t*,int*))matches_string;
1132 break;
1133 case ID_DER_ASN1_DN:
1134 this->public.equals = (bool (*)
1135 (identification_t*,identification_t*))equals_dn;
1136 this->public.matches = (bool (*)
1137 (identification_t*,identification_t*,int*))matches_dn;
1138 break;
1139 case ID_IPV4_ADDR:
1140 case ID_IPV6_ADDR:
1141 case ID_DER_ASN1_GN:
1142 case ID_KEY_ID:
1143 case ID_DER_ASN1_GN_URI:
1144 default:
1145 break;
1146 }
1147
1148 /* apply encoded chunk */
1149 if (type != ID_ANY)
1150 {
1151 this->encoded = chunk_clone(encoded);
1152 }
1153 return &(this->public);
1154 }