cleaned up apidoc
[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 return this->type == other->type &&
742 chunk_equals(this->encoded, other->encoded);
743 }
744
745 /**
746 * Special implementation of identification_t.equals for ID_DER_ASN1_DN.
747 */
748 static bool equals_dn(private_identification_t *this,
749 private_identification_t *other)
750 {
751 return same_dn(this->encoded, other->encoded);
752 }
753
754 /**
755 * Default implementation of identification_t.matches.
756 */
757 static bool matches_binary(private_identification_t *this,
758 private_identification_t *other, int *wildcards)
759 {
760 if (other->type == ID_ANY)
761 {
762 if (wildcards)
763 {
764 *wildcards = MAX_WILDCARDS;
765 }
766 return TRUE;
767 }
768 if (wildcards)
769 {
770 *wildcards = 0;
771 }
772 return this->type == other->type &&
773 chunk_equals(this->encoded, other->encoded);
774 }
775
776 /**
777 * Special implementation of identification_t.matches for ID_RFC822_ADDR/ID_FQDN.
778 * Checks for a wildcard in other-string, and compares it against this-string.
779 */
780 static bool matches_string(private_identification_t *this,
781 private_identification_t *other, int *wildcards)
782 {
783 u_int len = other->encoded.len;
784
785 if (other->type == ID_ANY)
786 {
787 if (wildcards)
788 {
789 *wildcards = MAX_WILDCARDS;
790 }
791 return TRUE;
792 }
793
794 if (this->type != other->type)
795 return FALSE;
796
797 /* try a binary comparison first */
798 if (equals_binary(this, other))
799 {
800 if (wildcards)
801 {
802 *wildcards = 0;
803 }
804 return TRUE;
805 }
806
807 if (len == 0 || this->encoded.len < len)
808 return FALSE;
809
810 /* check for single wildcard at the head of the string */
811 if (*other->encoded.ptr == '*')
812 {
813 if (wildcards)
814 {
815 *wildcards = 1;
816 }
817
818 /* single asterisk matches any string */
819 if (len-- == 1)
820 return TRUE;
821
822 if (memeq(this->encoded.ptr + this->encoded.len - len, other->encoded.ptr + 1, len))
823 return TRUE;
824 }
825
826 return FALSE;
827 }
828
829 /**
830 * Special implementation of identification_t.matches for ID_ANY.
831 * ANY matches only another ANY, but nothing other
832 */
833 static bool matches_any(private_identification_t *this,
834 private_identification_t *other, int *wildcards)
835 {
836 if (wildcards)
837 {
838 *wildcards = 0;
839 }
840 return other->type == ID_ANY;
841 }
842
843 /**
844 * Special implementation of identification_t.matches for ID_DER_ASN1_DN.
845 * ANY matches any, even ANY, thats why its there...
846 */
847 static bool matches_dn(private_identification_t *this,
848 private_identification_t *other, int *wildcards)
849 {
850 if (other->type == ID_ANY)
851 {
852 if (wildcards)
853 {
854 *wildcards = MAX_WILDCARDS;
855 }
856 return TRUE;
857 }
858
859 if (this->type == other->type)
860 {
861 return match_dn(this->encoded, other->encoded, wildcards);
862 }
863 return FALSE;
864 }
865
866 /**
867 * output handler in printf()
868 */
869 static int print(FILE *stream, const struct printf_info *info,
870 const void *const *args)
871 {
872 private_identification_t *this = *((private_identification_t**)(args[0]));
873 char buf[BUF_LEN];
874 chunk_t proper, buf_chunk = chunk_from_buf(buf);
875 int written;
876
877 if (this == NULL)
878 {
879 return fprintf(stream, "(null)");
880 }
881
882 switch (this->type)
883 {
884 case ID_ANY:
885 return fprintf(stream, "%%any");
886 case ID_IPV4_ADDR:
887 if (this->encoded.len < sizeof(struct in_addr) ||
888 inet_ntop(AF_INET, this->encoded.ptr, buf, sizeof(buf)) == NULL)
889 {
890 return fprintf(stream, "(invalid ID_IPV4_ADDR)");
891 }
892 else
893 {
894 return fprintf(stream, "%s", buf);
895 }
896 case ID_IPV6_ADDR:
897 if (this->encoded.len < sizeof(struct in6_addr) ||
898 inet_ntop(AF_INET6, this->encoded.ptr, buf, INET6_ADDRSTRLEN) == NULL)
899 {
900 return fprintf(stream, "(invalid ID_IPV6_ADDR)");
901 }
902 else
903 {
904 return fprintf(stream, "%s", buf);
905 }
906 case ID_FQDN:
907 {
908 proper = sanitize_chunk(this->encoded);
909 written = fprintf(stream, "@%.*s", proper.len, proper.ptr);
910 chunk_free(&proper);
911 return written;
912 }
913 case ID_RFC822_ADDR:
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_DER_ASN1_DN:
921 {
922 snprintf(buf, sizeof(buf), "%.*s", this->encoded.len, this->encoded.ptr);
923 /* TODO: whats returned on failure?*/
924 dntoa(this->encoded, &buf_chunk);
925 return fprintf(stream, "%s", buf);
926 }
927 case ID_DER_ASN1_GN:
928 return fprintf(stream, "(ASN.1 general Name");
929 case ID_KEY_ID:
930 return fprintf(stream, "(KEY_ID)");
931 case ID_DER_ASN1_GN_URI:
932 {
933 proper = sanitize_chunk(this->encoded);
934 written = fprintf(stream, "%.*s", proper.len, proper.ptr);
935 chunk_free(&proper);
936 return written;
937 }
938 default:
939 return fprintf(stream, "(unknown ID type: %d)", this->type);
940 }
941 }
942
943 /**
944 * register printf() handlers
945 */
946 static void __attribute__ ((constructor))print_register()
947 {
948 register_printf_function(PRINTF_IDENTIFICATION, print, arginfo_ptr);
949 }
950
951 /**
952 * Implementation of identification_t.clone.
953 */
954 static identification_t *clone_(private_identification_t *this)
955 {
956 private_identification_t *clone = identification_create();
957
958 clone->type = this->type;
959 clone->encoded = chunk_clone(this->encoded);
960 clone->public.equals = this->public.equals;
961 clone->public.matches = this->public.matches;
962
963 return &clone->public;
964 }
965
966 /**
967 * Implementation of identification_t.destroy.
968 */
969 static void destroy(private_identification_t *this)
970 {
971 chunk_free(&this->encoded);
972 free(this);
973 }
974
975 /**
976 * Generic constructor used for the other constructors.
977 */
978 static private_identification_t *identification_create(void)
979 {
980 private_identification_t *this = malloc_thing(private_identification_t);
981
982 this->public.get_encoding = (chunk_t (*) (identification_t*))get_encoding;
983 this->public.get_type = (id_type_t (*) (identification_t*))get_type;
984 this->public.contains_wildcards = (bool (*) (identification_t *this))contains_wildcards;
985 this->public.clone = (identification_t* (*) (identification_t*))clone_;
986 this->public.destroy = (void (*) (identification_t*))destroy;
987 /* we use these as defaults, the may be overloaded for special ID types */
988 this->public.equals = (bool (*) (identification_t*,identification_t*))equals_binary;
989 this->public.matches = (bool (*) (identification_t*,identification_t*,int*))matches_binary;
990
991 this->encoded = chunk_empty;
992
993 return this;
994 }
995
996 /*
997 * Described in header.
998 */
999 identification_t *identification_create_from_string(char *string)
1000 {
1001 private_identification_t *this = identification_create();
1002
1003 if (string == NULL)
1004 {
1005 string = "%any";
1006 }
1007 if (strchr(string, '=') != NULL)
1008 {
1009 /* we interpret this as an ASCII X.501 ID_DER_ASN1_DN.
1010 * convert from LDAP style or openssl x509 -subject style to ASN.1 DN
1011 */
1012 if (atodn(string, &this->encoded) != SUCCESS)
1013 {
1014 free(this);
1015 return NULL;
1016 }
1017 this->type = ID_DER_ASN1_DN;
1018 this->public.equals = (bool (*) (identification_t*,identification_t*))equals_dn;
1019 this->public.matches = (bool (*) (identification_t*,identification_t*,int*))matches_dn;
1020 return &this->public;
1021 }
1022 else if (strchr(string, '@') == NULL)
1023 {
1024 if (streq(string, "%any")
1025 || streq(string, "0.0.0.0")
1026 || streq(string, "*")
1027 || streq(string, "::")
1028 || streq(string, "0::0"))
1029 {
1030 /* any ID will be accepted */
1031 this->type = ID_ANY;
1032 this->public.matches = (bool (*)
1033 (identification_t*,identification_t*,int*))matches_any;
1034 return &this->public;
1035 }
1036 else
1037 {
1038 if (strchr(string, ':') == NULL)
1039 {
1040 /* try IPv4 */
1041 struct in_addr address;
1042 chunk_t chunk = {(void*)&address, sizeof(address)};
1043
1044 if (inet_pton(AF_INET, string, &address) <= 0)
1045 {
1046 free(this);
1047 return NULL;
1048 }
1049 this->encoded = chunk_clone(chunk);
1050 this->type = ID_IPV4_ADDR;
1051 return &(this->public);
1052 }
1053 else
1054 {
1055 /* try IPv6 */
1056 struct in6_addr address;
1057 chunk_t chunk = {(void*)&address, sizeof(address)};
1058
1059 if (inet_pton(AF_INET6, string, &address) <= 0)
1060 {
1061 free(this);
1062 return NULL;
1063 }
1064 this->encoded = chunk_clone(chunk);
1065 this->type = ID_IPV6_ADDR;
1066 return &(this->public);
1067 }
1068 }
1069 }
1070 else
1071 {
1072 if (*string == '@')
1073 {
1074 if (*(string + 1) == '#')
1075 {
1076 /* TODO: Pluto handles '#' as hex encoded ID_KEY_ID. */
1077 free(this);
1078 return NULL;
1079 }
1080 else
1081 {
1082 this->type = ID_FQDN;
1083 this->encoded.ptr = strdup(string + 1);
1084 this->encoded.len = strlen(string + 1);
1085 this->public.matches = (bool (*)
1086 (identification_t*,identification_t*,int*))matches_string;
1087 return &(this->public);
1088 }
1089 }
1090 else
1091 {
1092 this->type = ID_RFC822_ADDR;
1093 this->encoded.ptr = strdup(string);
1094 this->encoded.len = strlen(string);
1095 this->public.matches = (bool (*)
1096 (identification_t*,identification_t*,int*))matches_string;
1097 return &(this->public);
1098 }
1099 }
1100 }
1101
1102 /*
1103 * Described in header.
1104 */
1105 identification_t *identification_create_from_encoding(id_type_t type, chunk_t encoded)
1106 {
1107 private_identification_t *this = identification_create();
1108 this->type = type;
1109 switch (type)
1110 {
1111 case ID_ANY:
1112 this->public.matches = (bool (*)
1113 (identification_t*,identification_t*,int*))matches_any;
1114 break;
1115 case ID_FQDN:
1116 this->public.matches = (bool (*)
1117 (identification_t*,identification_t*,int*))matches_string;
1118 break;
1119 case ID_RFC822_ADDR:
1120 this->public.matches = (bool (*)
1121 (identification_t*,identification_t*,int*))matches_string;
1122 break;
1123 case ID_DER_ASN1_DN:
1124 this->public.equals = (bool (*)
1125 (identification_t*,identification_t*))equals_dn;
1126 this->public.matches = (bool (*)
1127 (identification_t*,identification_t*,int*))matches_dn;
1128 break;
1129 case ID_IPV4_ADDR:
1130 case ID_IPV6_ADDR:
1131 case ID_DER_ASN1_GN:
1132 case ID_KEY_ID:
1133 case ID_DER_ASN1_GN_URI:
1134 default:
1135 break;
1136 }
1137
1138 /* apply encoded chunk */
1139 if (type != ID_ANY)
1140 {
1141 this->encoded = chunk_clone(encoded);
1142 }
1143 return &(this->public);
1144 }