Moved data structures to new collections subfolder
[strongswan.git] / src / pki / pki.c
1 /*
2 * Copyright (C) 2009 Martin Willi
3 * Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include "command.h"
17 #include "pki.h"
18
19 #include <unistd.h>
20
21 #include <debug.h>
22 #include <credentials/sets/callback_cred.h>
23
24 /**
25 * Convert a form string to a encoding type
26 */
27 bool get_form(char *form, cred_encoding_type_t *enc, credential_type_t type)
28 {
29 if (streq(form, "der"))
30 {
31 switch (type)
32 {
33 case CRED_CERTIFICATE:
34 *enc = CERT_ASN1_DER;
35 return TRUE;
36 case CRED_PRIVATE_KEY:
37 *enc = PRIVKEY_ASN1_DER;
38 return TRUE;
39 case CRED_PUBLIC_KEY:
40 /* der encoded keys usually contain the complete
41 * SubjectPublicKeyInfo */
42 *enc = PUBKEY_SPKI_ASN1_DER;
43 return TRUE;
44 default:
45 return FALSE;
46 }
47 }
48 else if (streq(form, "pem"))
49 {
50 switch (type)
51 {
52 case CRED_CERTIFICATE:
53 *enc = CERT_PEM;
54 return TRUE;
55 case CRED_PRIVATE_KEY:
56 *enc = PRIVKEY_PEM;
57 return TRUE;
58 case CRED_PUBLIC_KEY:
59 *enc = PUBKEY_PEM;
60 return TRUE;
61 default:
62 return FALSE;
63 }
64 }
65 else if (streq(form, "pgp"))
66 {
67 switch (type)
68 {
69 case CRED_PRIVATE_KEY:
70 *enc = PRIVKEY_PGP;
71 return TRUE;
72 case CRED_PUBLIC_KEY:
73 *enc = PUBKEY_PGP;
74 return TRUE;
75 default:
76 return FALSE;
77 }
78 }
79 return FALSE;
80 }
81
82 /**
83 * Callback credential set pki uses
84 */
85 static callback_cred_t *cb_set;
86
87 /**
88 * Callback function to receive credentials
89 */
90 static shared_key_t* cb(void *data, shared_key_type_t type,
91 identification_t *me, identification_t *other,
92 id_match_t *match_me, id_match_t *match_other)
93 {
94 char buf[64], *label, *secret;
95
96 switch (type)
97 {
98 case SHARED_PIN:
99 label = "Smartcard PIN";
100 break;
101 case SHARED_PRIVATE_KEY_PASS:
102 label = "Private key passphrase";
103 break;
104 default:
105 return NULL;
106 }
107 snprintf(buf, sizeof(buf), "%s: ", label);
108 secret = getpass(buf);
109 if (secret)
110 {
111 if (match_me)
112 {
113 *match_me = ID_MATCH_PERFECT;
114 }
115 if (match_other)
116 {
117 *match_other = ID_MATCH_NONE;
118 }
119 return shared_key_create(type,
120 chunk_clone(chunk_create(secret, strlen(secret))));
121 }
122 return NULL;
123 }
124
125 /**
126 * Register PIN/Passphrase callback function
127 */
128 static void add_callback()
129 {
130 cb_set = callback_cred_create_shared(cb, NULL);
131 lib->credmgr->add_set(lib->credmgr, &cb_set->set);
132 }
133
134 /**
135 * Unregister PIN/Passphrase callback function
136 */
137 static void remove_callback()
138 {
139 lib->credmgr->remove_set(lib->credmgr, &cb_set->set);
140 cb_set->destroy(cb_set);
141 }
142
143 /**
144 * Library initialization and operation parsing
145 */
146 int main(int argc, char *argv[])
147 {
148 atexit(library_deinit);
149 if (!library_init(NULL))
150 {
151 exit(SS_RC_LIBSTRONGSWAN_INTEGRITY);
152 }
153 if (lib->integrity &&
154 !lib->integrity->check_file(lib->integrity, "pki", argv[0]))
155 {
156 fprintf(stderr, "integrity check of pki failed\n");
157 exit(SS_RC_DAEMON_INTEGRITY);
158 }
159 if (!lib->plugins->load(lib->plugins, NULL,
160 lib->settings->get_str(lib->settings, "pki.load", PLUGINS)))
161 {
162 exit(SS_RC_INITIALIZATION_FAILED);
163 }
164
165 add_callback();
166 atexit(remove_callback);
167 return command_dispatch(argc, argv);
168 }
169