moved very stroke specific x509 flag handling out of core library
[strongswan.git] / src / charon / plugins / stroke / stroke_cred.c
1 /*
2 * Copyright (C) 2008 Tobias Brunner
3 * Copyright (C) 2008 Martin Willi
4 * Hochschule fuer Technik Rapperswil
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 */
16
17 #include <sys/stat.h>
18 #include <limits.h>
19
20 #include "stroke_cred.h"
21 #include "stroke_shared_key.h"
22
23 #include <credentials/certificates/x509.h>
24 #include <credentials/certificates/crl.h>
25 #include <credentials/certificates/ac.h>
26 #include <utils/linked_list.h>
27 #include <utils/lexparser.h>
28 #include <utils/mutex.h>
29 #include <asn1/pem.h>
30 #include <daemon.h>
31
32 /* configuration directories and files */
33 #define CONFIG_DIR IPSEC_CONFDIR
34 #define IPSEC_D_DIR CONFIG_DIR "/ipsec.d"
35 #define PRIVATE_KEY_DIR IPSEC_D_DIR "/private"
36 #define CERTIFICATE_DIR IPSEC_D_DIR "/certs"
37 #define CA_CERTIFICATE_DIR IPSEC_D_DIR "/cacerts"
38 #define AA_CERTIFICATE_DIR IPSEC_D_DIR "/aacerts"
39 #define ATTR_CERTIFICATE_DIR IPSEC_D_DIR "/acerts"
40 #define OCSP_CERTIFICATE_DIR IPSEC_D_DIR "/ocspcerts"
41 #define CRL_DIR IPSEC_D_DIR "/crls"
42 #define SECRETS_FILE CONFIG_DIR "/ipsec.secrets"
43
44 typedef struct private_stroke_cred_t private_stroke_cred_t;
45
46 /**
47 * private data of stroke_cred
48 */
49 struct private_stroke_cred_t {
50
51 /**
52 * public functions
53 */
54 stroke_cred_t public;
55
56 /**
57 * list of trusted peer/signer/CA certificates (certificate_t)
58 */
59 linked_list_t *certs;
60
61 /**
62 * list of shared secrets (private_shared_key_t)
63 */
64 linked_list_t *shared;
65
66 /**
67 * list of private keys (private_key_t)
68 */
69 linked_list_t *private;
70
71 /**
72 * read-write lock to lists
73 */
74 rwlock_t *lock;
75
76 /**
77 * cache CRLs to disk?
78 */
79 bool cachecrl;
80 };
81
82 /**
83 * data to pass to various filters
84 */
85 typedef struct {
86 private_stroke_cred_t *this;
87 identification_t *id;
88 } id_data_t;
89
90 /**
91 * destroy id enumerator data and unlock list
92 */
93 static void id_data_destroy(id_data_t *data)
94 {
95 data->this->lock->unlock(data->this->lock);
96 free(data);
97 }
98
99 /**
100 * filter function for private key enumerator
101 */
102 static bool private_filter(id_data_t *data,
103 private_key_t **in, private_key_t **out)
104 {
105 identification_t *candidate;
106 id_type_t type;
107
108 if (data->id == NULL)
109 {
110 *out = *in;
111 return TRUE;
112 }
113 type = data->id->get_type(data->id);
114 if (type == ID_KEY_ID)
115 { /* handle ID_KEY_ID as a ID_PUBKEY_SHA1 */
116 type = ID_PUBKEY_SHA1;
117 }
118 candidate = (*in)->get_id(*in, type);
119 if (candidate &&
120 chunk_equals(candidate->get_encoding(candidate),
121 data->id->get_encoding(data->id)))
122 {
123 *out = *in;
124 return TRUE;
125 }
126 return FALSE;
127 }
128
129 /**
130 * Implements credential_set_t.create_private_enumerator
131 */
132 static enumerator_t* create_private_enumerator(private_stroke_cred_t *this,
133 key_type_t type, identification_t *id)
134 {
135 id_data_t *data;
136
137 data = malloc_thing(id_data_t);
138 data->this = this;
139 data->id = id;
140
141 this->lock->read_lock(this->lock);
142 return enumerator_create_filter(this->private->create_enumerator(this->private),
143 (void*)private_filter, data,
144 (void*)id_data_destroy);
145 }
146
147 /**
148 * filter function for certs enumerator
149 */
150 static bool certs_filter(id_data_t *data, certificate_t **in, certificate_t **out)
151 {
152 public_key_t *public;
153 identification_t *candidate;
154 certificate_t *cert = *in;
155 certificate_type_t type = cert->get_type(cert);
156
157 if (type == CERT_X509_CRL || type == CERT_X509_AC)
158 {
159 return FALSE;
160 }
161
162 if (data->id == NULL || cert->has_subject(cert, data->id))
163 {
164 *out = *in;
165 return TRUE;
166 }
167
168 public = (cert)->get_public_key(cert);
169 if (public)
170 {
171 candidate = public->get_id(public, data->id->get_type(data->id));
172 if (candidate && data->id->equals(data->id, candidate))
173 {
174 public->destroy(public);
175 *out = *in;
176 return TRUE;
177 }
178 public->destroy(public);
179 }
180 return FALSE;
181 }
182
183 /**
184 * filter function for crl enumerator
185 */
186 static bool crl_filter(id_data_t *data, certificate_t **in, certificate_t **out)
187 {
188 certificate_t *cert = *in;
189
190 if (cert->get_type(cert) != CERT_X509_CRL)
191 {
192 return FALSE;
193 }
194
195 if (data->id == NULL || cert->has_issuer(cert, data->id))
196 {
197 *out = *in;
198 return TRUE;
199 }
200 return FALSE;
201 }
202
203 /**
204 * filter function for attribute certificate enumerator
205 */
206 static bool ac_filter(id_data_t *data, certificate_t **in, certificate_t **out)
207 {
208 certificate_t *cert = *in;
209
210 if (cert->get_type(cert) != CERT_X509_AC)
211 {
212 return FALSE;
213 }
214
215 if (data->id == NULL || cert->has_subject(cert, data->id))
216 {
217 *out = *in;
218 return TRUE;
219 }
220 return FALSE;
221 }
222
223 /**
224 * Implements credential_set_t.create_cert_enumerator
225 */
226 static enumerator_t* create_cert_enumerator(private_stroke_cred_t *this,
227 certificate_type_t cert, key_type_t key,
228 identification_t *id, bool trusted)
229 {
230 id_data_t *data;
231
232 if (cert == CERT_X509_CRL || cert == CERT_X509_AC)
233 {
234 if (trusted)
235 {
236 return NULL;
237 }
238 data = malloc_thing(id_data_t);
239 data->this = this;
240 data->id = id;
241
242 this->lock->read_lock(this->lock);
243 return enumerator_create_filter(this->certs->create_enumerator(this->certs),
244 (cert == CERT_X509_CRL)? (void*)crl_filter : (void*)ac_filter,
245 data, (void*)id_data_destroy);
246 }
247 if (cert != CERT_X509 && cert != CERT_ANY)
248 { /* we only have X509 certificates. TODO: ACs? */
249 return NULL;
250 }
251 data = malloc_thing(id_data_t);
252 data->this = this;
253 data->id = id;
254
255 this->lock->read_lock(this->lock);
256 return enumerator_create_filter(this->certs->create_enumerator(this->certs),
257 (void*)certs_filter, data,
258 (void*)id_data_destroy);
259 }
260
261 typedef struct {
262 private_stroke_cred_t *this;
263 identification_t *me;
264 identification_t *other;
265 shared_key_type_t type;
266 } shared_data_t;
267
268 /**
269 * free shared key enumerator data and unlock list
270 */
271 static void shared_data_destroy(shared_data_t *data)
272 {
273 data->this->lock->unlock(data->this->lock);
274 free(data);
275 }
276
277 /**
278 * filter function for certs enumerator
279 */
280 static bool shared_filter(shared_data_t *data,
281 stroke_shared_key_t **in, shared_key_t **out,
282 void **unused1, id_match_t *me,
283 void **unused2, id_match_t *other)
284 {
285 id_match_t my_match, other_match;
286 stroke_shared_key_t *stroke = *in;
287 shared_key_t *shared = &stroke->shared;
288
289 if (data->type != SHARED_ANY && shared->get_type(shared) != data->type)
290 {
291 return FALSE;
292 }
293
294 my_match = stroke->has_owner(stroke, data->me);
295 other_match = stroke->has_owner(stroke, data->other);
296 if (!my_match && !other_match)
297 {
298 return FALSE;
299 }
300 *out = shared;
301 if (me)
302 {
303 *me = my_match;
304 }
305 if (other)
306 {
307 *other = other_match;
308 }
309 return TRUE;
310 }
311
312 /**
313 * Implements credential_set_t.create_shared_enumerator
314 */
315 static enumerator_t* create_shared_enumerator(private_stroke_cred_t *this,
316 shared_key_type_t type, identification_t *me,
317 identification_t *other)
318 {
319 shared_data_t *data = malloc_thing(shared_data_t);
320
321 data->this = this;
322 data->me = me;
323 data->other = other;
324 data->type = type;
325 this->lock->read_lock(this->lock);
326 return enumerator_create_filter(this->shared->create_enumerator(this->shared),
327 (void*)shared_filter, data,
328 (void*)shared_data_destroy);
329 }
330
331 /**
332 * Add a certificate to chain
333 */
334 static certificate_t* add_cert(private_stroke_cred_t *this, certificate_t *cert)
335 {
336 certificate_t *current;
337 enumerator_t *enumerator;
338 bool new = TRUE;
339
340 this->lock->read_lock(this->lock);
341 enumerator = this->certs->create_enumerator(this->certs);
342 while (enumerator->enumerate(enumerator, (void**)&current))
343 {
344 if (current->equals(current, cert))
345 {
346 /* cert already in queue */
347 cert->destroy(cert);
348 cert = current;
349 new = FALSE;
350 break;
351 }
352 }
353 enumerator->destroy(enumerator);
354
355 if (new)
356 {
357 this->certs->insert_last(this->certs, cert);
358 }
359 this->lock->unlock(this->lock);
360 return cert;
361 }
362
363 /**
364 * Implementation of stroke_cred_t.load_ca.
365 */
366 static certificate_t* load_ca(private_stroke_cred_t *this, char *filename)
367 {
368 certificate_t *cert;
369 char path[PATH_MAX];
370
371 if (*filename == '/')
372 {
373 snprintf(path, sizeof(path), "%s", filename);
374 }
375 else
376 {
377 snprintf(path, sizeof(path), "%s/%s", CA_CERTIFICATE_DIR, filename);
378 }
379
380 cert = lib->creds->create(lib->creds,
381 CRED_CERTIFICATE, CERT_X509,
382 BUILD_FROM_FILE, path,
383 BUILD_END);
384 if (cert)
385 {
386 x509_t *x509 = (x509_t*)cert;
387
388 if (!(x509->get_flags(x509) & X509_CA))
389 {
390 cert->destroy(cert);
391 DBG1(DBG_CFG, " ca certificate must have ca basic constraint set, "
392 "discarded");
393 return NULL;
394 }
395 return (certificate_t*)add_cert(this, cert);
396 }
397 return NULL;
398 }
399
400 /**
401 * Add X.509 CRL to chain
402 */
403 static bool add_crl(private_stroke_cred_t *this, crl_t* crl)
404 {
405 certificate_t *current, *cert = &crl->certificate;
406 enumerator_t *enumerator;
407 bool new = TRUE, found = FALSE;
408
409 this->lock->write_lock(this->lock);
410 enumerator = this->certs->create_enumerator(this->certs);
411 while (enumerator->enumerate(enumerator, (void**)&current))
412 {
413 if (current->get_type(current) == CERT_X509_CRL)
414 {
415 crl_t *crl_c = (crl_t*)current;
416 identification_t *authkey = crl->get_authKeyIdentifier(crl);
417 identification_t *authkey_c = crl_c->get_authKeyIdentifier(crl_c);
418
419 /* if compare authorityKeyIdentifiers if available */
420 if (authkey != NULL && authkey_c != NULL &&
421 authkey->equals(authkey, authkey_c))
422 {
423 found = TRUE;
424 }
425 else
426 {
427 identification_t *issuer = cert->get_issuer(cert);
428 identification_t *issuer_c = current->get_issuer(current);
429
430 /* otherwise compare issuer distinguished names */
431 if (issuer->equals(issuer, issuer_c))
432 {
433 found = TRUE;
434 }
435 }
436 if (found)
437 {
438 new = cert->is_newer(cert, current);
439 if (new)
440 {
441 this->certs->remove_at(this->certs, enumerator);
442 }
443 else
444 {
445 cert->destroy(cert);
446 }
447 break;
448 }
449 }
450 }
451 enumerator->destroy(enumerator);
452
453 if (new)
454 {
455 this->certs->insert_last(this->certs, cert);
456 }
457 this->lock->unlock(this->lock);
458 return new;
459 }
460
461 /**
462 * Add X.509 attribute certificate to chain
463 */
464 static bool add_ac(private_stroke_cred_t *this, ac_t* ac)
465 {
466 certificate_t *cert = &ac->certificate;
467
468 this->lock->write_lock(this->lock);
469 this->certs->insert_last(this->certs, cert);
470 this->lock->unlock(this->lock);
471 return TRUE;
472 }
473
474 /**
475 * Implementation of stroke_cred_t.load_peer.
476 */
477 static certificate_t* load_peer(private_stroke_cred_t *this, char *filename)
478 {
479 certificate_t *cert;
480 char path[PATH_MAX];
481
482 if (*filename == '/')
483 {
484 snprintf(path, sizeof(path), "%s", filename);
485 }
486 else
487 {
488 snprintf(path, sizeof(path), "%s/%s", CERTIFICATE_DIR, filename);
489 }
490
491 cert = lib->creds->create(lib->creds,
492 CRED_CERTIFICATE, CERT_X509,
493 BUILD_FROM_FILE, path,
494 BUILD_X509_FLAG, 0,
495 BUILD_END);
496 if (cert)
497 {
498 cert = add_cert(this, cert);
499 return cert->get_ref(cert);
500 }
501 return NULL;
502 }
503
504 /**
505 * load trusted certificates from a directory
506 */
507 static void load_certdir(private_stroke_cred_t *this, char *path,
508 certificate_type_t type, x509_flag_t flag)
509 {
510 struct stat st;
511 char *file;
512
513 enumerator_t *enumerator = enumerator_create_directory(path);
514
515 if (!enumerator)
516 {
517 DBG1(DBG_CFG, " reading directory failed");
518 return;
519 }
520
521 while (enumerator->enumerate(enumerator, NULL, &file, &st))
522 {
523 certificate_t *cert;
524
525 if (!S_ISREG(st.st_mode))
526 {
527 /* skip special file */
528 continue;
529 }
530 switch (type)
531 {
532 case CERT_X509:
533 if (flag & X509_CA)
534 { /* for CA certificates, we strictly require CA
535 * basicconstraints to be set */
536 cert = lib->creds->create(lib->creds,
537 CRED_CERTIFICATE, CERT_X509,
538 BUILD_FROM_FILE, file, BUILD_END);
539 if (cert)
540 {
541 x509_t *x509 = (x509_t*)cert;
542
543 if (!(x509->get_flags(x509) & X509_CA))
544 {
545 DBG1(DBG_CFG, " ca certificate must have ca "
546 "basic constraint set, discarded");
547 cert->destroy(cert);
548 cert = NULL;
549 }
550 }
551 }
552 else
553 { /* for all other flags, we add them to the certificate. */
554 cert = lib->creds->create(lib->creds,
555 CRED_CERTIFICATE, CERT_X509,
556 BUILD_FROM_FILE, file,
557 BUILD_X509_FLAG, flag, BUILD_END);
558 }
559 if (cert)
560 {
561 add_cert(this, cert);
562 }
563 break;
564 case CERT_X509_CRL:
565 cert = lib->creds->create(lib->creds,
566 CRED_CERTIFICATE, CERT_X509_CRL,
567 BUILD_FROM_FILE, file,
568 BUILD_END);
569 if (cert)
570 {
571 add_crl(this, (crl_t*)cert);
572 }
573 break;
574 case CERT_X509_AC:
575 cert = lib->creds->create(lib->creds,
576 CRED_CERTIFICATE, CERT_X509_AC,
577 BUILD_FROM_FILE, file,
578 BUILD_END);
579 if (cert)
580 {
581 add_ac(this, (ac_t*)cert);
582 }
583 break;
584 default:
585 break;
586 }
587 }
588 enumerator->destroy(enumerator);
589 }
590
591 /**
592 * Implementation of credential_set_t.cache_cert.
593 */
594 static void cache_cert(private_stroke_cred_t *this, certificate_t *cert)
595 {
596 if (cert->get_type(cert) == CERT_X509_CRL && this->cachecrl)
597 {
598 /* CRLs get written to /etc/ipsec.d/crls/<authkeyId>.crl */
599 crl_t *crl = (crl_t*)cert;
600
601 cert->get_ref(cert);
602 if (add_crl(this, crl))
603 {
604 char buf[BUF_LEN];
605 chunk_t chunk, hex;
606 identification_t *id;
607
608 id = crl->get_authKeyIdentifier(crl);
609 chunk = id->get_encoding(id);
610 hex = chunk_to_hex(chunk, NULL, FALSE);
611 snprintf(buf, sizeof(buf), "%s/%s.crl", CRL_DIR, hex);
612 free(hex.ptr);
613
614 chunk = cert->get_encoding(cert);
615 chunk_write(chunk, buf, "crl", 022, TRUE);
616 free(chunk.ptr);
617 }
618 }
619 }
620
621 /**
622 * Implementation of stroke_cred_t.cachecrl.
623 */
624 static void cachecrl(private_stroke_cred_t *this, bool enabled)
625 {
626 DBG1(DBG_CFG, "crl caching to %s %s",
627 CRL_DIR, enabled ? "enabled" : "disabled");
628 this->cachecrl = enabled;
629 }
630
631
632 /**
633 * Convert a string of characters into a binary secret
634 * A string between single or double quotes is treated as ASCII characters
635 * A string prepended by 0x is treated as HEX and prepended by 0s as Base64
636 */
637 static err_t extract_secret(chunk_t *secret, chunk_t *line)
638 {
639 chunk_t raw_secret;
640 char delimiter = ' ';
641 bool quotes = FALSE;
642
643 if (!eat_whitespace(line))
644 {
645 return "missing secret";
646 }
647
648 if (*line->ptr == '\'' || *line->ptr == '"')
649 {
650 quotes = TRUE;
651 delimiter = *line->ptr;
652 line->ptr++; line->len--;
653 }
654
655 if (!extract_token(&raw_secret, delimiter, line))
656 {
657 if (delimiter == ' ')
658 {
659 raw_secret = *line;
660 }
661 else
662 {
663 return "missing second delimiter";
664 }
665 }
666
667 if (quotes)
668 {
669 /* treat as an ASCII string */
670 *secret = chunk_clone(raw_secret);
671 return NULL;
672 }
673 /* treat 0x as hex, 0s as base64 */
674 if (raw_secret.len > 2)
675 {
676 if (strncasecmp("0x", raw_secret.ptr, 2) == 0)
677 {
678 *secret = chunk_from_hex(chunk_skip(raw_secret, 2), NULL);
679 return NULL;
680 }
681 if (strncasecmp("0s", raw_secret.ptr, 2) == 0)
682 {
683 *secret = chunk_from_base64(chunk_skip(raw_secret, 2), NULL);
684 return NULL;
685 }
686 }
687 *secret = chunk_clone(raw_secret);
688 return NULL;
689 }
690
691 /**
692 * reload ipsec.secrets
693 */
694 static void load_secrets(private_stroke_cred_t *this)
695 {
696 size_t bytes;
697 int line_nr = 0;
698 chunk_t chunk, src, line;
699 FILE *fd;
700 private_key_t *private;
701 shared_key_t *shared;
702
703 DBG1(DBG_CFG, "loading secrets from '%s'", SECRETS_FILE);
704
705 fd = fopen(SECRETS_FILE, "r");
706 if (fd == NULL)
707 {
708 DBG1(DBG_CFG, "opening secrets file '%s' failed");
709 return;
710 }
711
712 /* TODO: do error checks */
713 fseek(fd, 0, SEEK_END);
714 chunk.len = ftell(fd);
715 rewind(fd);
716 chunk.ptr = malloc(chunk.len);
717 bytes = fread(chunk.ptr, 1, chunk.len, fd);
718 fclose(fd);
719 src = chunk;
720
721 this->lock->write_lock(this->lock);
722 while (this->shared->remove_last(this->shared,
723 (void**)&shared) == SUCCESS)
724 {
725 shared->destroy(shared);
726 }
727 while (this->private->remove_last(this->private,
728 (void**)&private) == SUCCESS)
729 {
730 private->destroy(private);
731 }
732
733 while (fetchline(&src, &line))
734 {
735 chunk_t ids, token;
736 shared_key_type_t type;
737
738 line_nr++;
739
740 if (!eat_whitespace(&line))
741 {
742 continue;
743 }
744 if (line.len > 2 && strneq(": ", line.ptr, 2))
745 {
746 /* no ids, skip the ':' */
747 ids = chunk_empty;
748 line.ptr++;
749 line.len--;
750 }
751 else if (extract_token_str(&ids, " : ", &line))
752 {
753 /* NULL terminate the extracted id string */
754 *(ids.ptr + ids.len) = '\0';
755 }
756 else
757 {
758 DBG1(DBG_CFG, "line %d: missing ' : ' separator", line_nr);
759 goto error;
760 }
761
762 if (!eat_whitespace(&line) || !extract_token(&token, ' ', &line))
763 {
764 DBG1(DBG_CFG, "line %d: missing token", line_nr);
765 goto error;
766 }
767 if (match("RSA", &token) || match("ECDSA", &token))
768 {
769 char path[PATH_MAX];
770 chunk_t filename;
771 chunk_t secret = chunk_empty;
772 private_key_t *key;
773 bool pgp = FALSE;
774 chunk_t chunk = chunk_empty;
775 key_type_t key_type = match("RSA", &token) ? KEY_RSA : KEY_ECDSA;
776
777 err_t ugh = extract_value(&filename, &line);
778
779 if (ugh != NULL)
780 {
781 DBG1(DBG_CFG, "line %d: %s", line_nr, ugh);
782 goto error;
783 }
784 if (filename.len == 0)
785 {
786 DBG1(DBG_CFG, "line %d: empty filename", line_nr);
787 goto error;
788 }
789 if (*filename.ptr == '/')
790 {
791 /* absolute path name */
792 snprintf(path, sizeof(path), "%.*s", filename.len, filename.ptr);
793 }
794 else
795 {
796 /* relative path name */
797 snprintf(path, sizeof(path), "%s/%.*s", PRIVATE_KEY_DIR,
798 filename.len, filename.ptr);
799 }
800
801 /* check for optional passphrase */
802 if (eat_whitespace(&line))
803 {
804 ugh = extract_secret(&secret, &line);
805 if (ugh != NULL)
806 {
807 DBG1(DBG_CFG, "line %d: malformed passphrase: %s", line_nr, ugh);
808 goto error;
809 }
810 }
811
812 if (pem_asn1_load_file(path, &secret, &chunk, &pgp))
813 {
814 key = lib->creds->create(lib->creds, CRED_PRIVATE_KEY, key_type,
815 BUILD_BLOB_ASN1_DER, chunk, BUILD_END);
816 free(chunk.ptr);
817 if (key)
818 {
819 DBG1(DBG_CFG, " loaded private key file '%s'", path);
820 this->private->insert_last(this->private, key);
821 }
822 }
823 chunk_clear(&secret);
824 }
825 else if (match("PIN", &token))
826 {
827 chunk_t sc = chunk_empty, secret = chunk_empty;
828 char smartcard[32], keyid[22], pin[32];
829 private_key_t *key;
830 u_int slot;
831
832 err_t ugh = extract_value(&sc, &line);
833
834 if (ugh != NULL)
835 {
836 DBG1(DBG_CFG, "line %d: %s", line_nr, ugh);
837 goto error;
838 }
839 if (sc.len == 0)
840 {
841 DBG1(DBG_CFG, "line %d: expected %%smartcard specifier", line_nr);
842 goto error;
843 }
844 snprintf(smartcard, sizeof(smartcard), "%.*s", sc.len, sc.ptr);
845 smartcard[sizeof(smartcard) - 1] = '\0';
846
847 /* parse slot and key id. only two formats are supported.
848 * first try %smartcard<slot>:<keyid> */
849 if (sscanf(smartcard, "%%smartcard%u:%s", &slot, keyid) == 2)
850 {
851 snprintf(smartcard, sizeof(smartcard), "%u:%s", slot, keyid);
852 }
853 /* then try %smartcard:<keyid> */
854 else if (sscanf(smartcard, "%%smartcard:%s", keyid) == 1)
855 {
856 snprintf(smartcard, sizeof(smartcard), "%s", keyid);
857 }
858 else
859 {
860 DBG1(DBG_CFG, "line %d: the given %%smartcard specifier is not"
861 " supported or invalid", line_nr);
862 goto error;
863 }
864
865 if (!eat_whitespace(&line))
866 {
867 DBG1(DBG_CFG, "line %d: expected PIN", line_nr);
868 goto error;
869 }
870 ugh = extract_secret(&secret, &line);
871 if (ugh != NULL)
872 {
873 DBG1(DBG_CFG, "line %d: malformed PIN: %s", line_nr, ugh);
874 goto error;
875 }
876 snprintf(pin, sizeof(pin), "%.*s", secret.len, secret.ptr);
877 pin[sizeof(pin) - 1] = '\0';
878
879 /* we assume an RSA key */
880 key = lib->creds->create(lib->creds, CRED_PRIVATE_KEY, KEY_RSA,
881 BUILD_SMARTCARD_KEYID, smartcard,
882 BUILD_SMARTCARD_PIN, pin, BUILD_END);
883
884 if (key)
885 {
886 DBG1(DBG_CFG, " loaded private key from %.*s", sc.len, sc.ptr);
887 this->private->insert_last(this->private, key);
888 }
889 memset(pin, 0, sizeof(pin));
890 chunk_clear(&secret);
891 }
892 else if ((match("PSK", &token) && (type = SHARED_IKE)) ||
893 (match("EAP", &token) && (type = SHARED_EAP)) ||
894 (match("XAUTH", &token) && (type = SHARED_EAP)))
895 {
896 stroke_shared_key_t *shared_key;
897 chunk_t secret = chunk_empty;
898 bool any = TRUE;
899
900 err_t ugh = extract_secret(&secret, &line);
901 if (ugh != NULL)
902 {
903 DBG1(DBG_CFG, "line %d: malformed secret: %s", line_nr, ugh);
904 goto error;
905 }
906 shared_key = stroke_shared_key_create(type, secret);
907 DBG1(DBG_CFG, " loaded %N secret for %s", shared_key_type_names, type,
908 ids.len > 0 ? (char*)ids.ptr : "%any");
909 DBG4(DBG_CFG, " secret: %#B", &secret);
910
911 this->shared->insert_last(this->shared, shared_key);
912 while (ids.len > 0)
913 {
914 chunk_t id;
915 identification_t *peer_id;
916
917 ugh = extract_value(&id, &ids);
918 if (ugh != NULL)
919 {
920 DBG1(DBG_CFG, "line %d: %s", line_nr, ugh);
921 goto error;
922 }
923 if (id.len == 0)
924 {
925 continue;
926 }
927
928 /* NULL terminate the ID string */
929 *(id.ptr + id.len) = '\0';
930 peer_id = identification_create_from_string(id.ptr);
931 if (peer_id->get_type(peer_id) == ID_ANY)
932 {
933 peer_id->destroy(peer_id);
934 continue;
935 }
936
937 shared_key->add_owner(shared_key, peer_id);
938 any = FALSE;
939 }
940 if (any)
941 {
942 shared_key->add_owner(shared_key,
943 identification_create_from_encoding(ID_ANY, chunk_empty));
944 }
945 }
946 else
947 {
948 DBG1(DBG_CFG, "line %d: token must be either "
949 "RSA, ECDSA, PSK, EAP, XAUTH or PIN", line_nr);
950 goto error;
951 }
952 }
953 error:
954 this->lock->unlock(this->lock);
955 chunk_clear(&chunk);
956 }
957
958 /**
959 * load all certificates from ipsec.d
960 */
961 static void load_certs(private_stroke_cred_t *this)
962 {
963 DBG1(DBG_CFG, "loading ca certificates from '%s'",
964 CA_CERTIFICATE_DIR);
965 load_certdir(this, CA_CERTIFICATE_DIR, CERT_X509, X509_CA);
966
967 DBG1(DBG_CFG, "loading aa certificates from '%s'",
968 AA_CERTIFICATE_DIR);
969 load_certdir(this, AA_CERTIFICATE_DIR, CERT_X509, X509_AA);
970
971 DBG1(DBG_CFG, "loading ocsp signer certificates from '%s'",
972 OCSP_CERTIFICATE_DIR);
973 load_certdir(this, OCSP_CERTIFICATE_DIR, CERT_X509, X509_OCSP_SIGNER);
974
975 DBG1(DBG_CFG, "loading attribute certificates from '%s'",
976 ATTR_CERTIFICATE_DIR);
977 load_certdir(this, ATTR_CERTIFICATE_DIR, CERT_X509_AC, 0);
978
979 DBG1(DBG_CFG, "loading crls from '%s'",
980 CRL_DIR);
981 load_certdir(this, CRL_DIR, CERT_X509_CRL, 0);
982 }
983
984 /**
985 * Implementation of stroke_cred_t.reread.
986 */
987 static void reread(private_stroke_cred_t *this, stroke_msg_t *msg)
988 {
989 if (msg->reread.flags & REREAD_SECRETS)
990 {
991 DBG1(DBG_CFG, "rereading secrets");
992 load_secrets(this);
993 }
994 if (msg->reread.flags & REREAD_CACERTS)
995 {
996 DBG1(DBG_CFG, "rereading ca certificates from '%s'",
997 CA_CERTIFICATE_DIR);
998 load_certdir(this, CA_CERTIFICATE_DIR, CERT_X509, X509_CA);
999 }
1000 if (msg->reread.flags & REREAD_OCSPCERTS)
1001 {
1002 DBG1(DBG_CFG, "rereading ocsp signer certificates from '%s'",
1003 OCSP_CERTIFICATE_DIR);
1004 load_certdir(this, OCSP_CERTIFICATE_DIR, CERT_X509,
1005 X509_OCSP_SIGNER);
1006 }
1007 if (msg->reread.flags & REREAD_AACERTS)
1008 {
1009 DBG1(DBG_CFG, "rereading aa certificates from '%s'",
1010 AA_CERTIFICATE_DIR);
1011 load_certdir(this, AA_CERTIFICATE_DIR, CERT_X509, X509_AA);
1012 }
1013 if (msg->reread.flags & REREAD_ACERTS)
1014 {
1015 DBG1(DBG_CFG, "rereading attribute certificates from '%s'",
1016 ATTR_CERTIFICATE_DIR);
1017 load_certdir(this, ATTR_CERTIFICATE_DIR, CERT_X509_AC, 0);
1018 }
1019 if (msg->reread.flags & REREAD_CRLS)
1020 {
1021 DBG1(DBG_CFG, "rereading crls from '%s'",
1022 CRL_DIR);
1023 load_certdir(this, CRL_DIR, CERT_X509_CRL, 0);
1024 }
1025 }
1026
1027 /**
1028 * Implementation of stroke_cred_t.destroy
1029 */
1030 static void destroy(private_stroke_cred_t *this)
1031 {
1032 this->certs->destroy_offset(this->certs, offsetof(certificate_t, destroy));
1033 this->shared->destroy_offset(this->shared, offsetof(shared_key_t, destroy));
1034 this->private->destroy_offset(this->private, offsetof(private_key_t, destroy));
1035 this->lock->destroy(this->lock);
1036 free(this);
1037 }
1038
1039 /*
1040 * see header file
1041 */
1042 stroke_cred_t *stroke_cred_create()
1043 {
1044 private_stroke_cred_t *this = malloc_thing(private_stroke_cred_t);
1045
1046 this->public.set.create_private_enumerator = (void*)create_private_enumerator;
1047 this->public.set.create_cert_enumerator = (void*)create_cert_enumerator;
1048 this->public.set.create_shared_enumerator = (void*)create_shared_enumerator;
1049 this->public.set.create_cdp_enumerator = (void*)return_null;
1050 this->public.set.cache_cert = (void*)cache_cert;
1051 this->public.reread = (void(*)(stroke_cred_t*, stroke_msg_t *msg))reread;
1052 this->public.load_ca = (certificate_t*(*)(stroke_cred_t*, char *filename))load_ca;
1053 this->public.load_peer = (certificate_t*(*)(stroke_cred_t*, char *filename))load_peer;
1054 this->public.cachecrl = (void(*)(stroke_cred_t*, bool enabled))cachecrl;
1055 this->public.destroy = (void(*)(stroke_cred_t*))destroy;
1056
1057 this->certs = linked_list_create();
1058 this->shared = linked_list_create();
1059 this->private = linked_list_create();
1060 this->lock = rwlock_create(RWLOCK_DEFAULT);
1061
1062 load_certs(this);
1063 load_secrets(this);
1064
1065 this->cachecrl = FALSE;
1066
1067 return &this->public;
1068 }
1069