pki: Add support to encode public keys in SSH key format
[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 <utils/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 else if (streq(form, "dnskey"))
80 {
81 switch (type)
82 {
83 case CRED_PUBLIC_KEY:
84 *enc = PUBKEY_DNSKEY;
85 return TRUE;
86 default:
87 return FALSE;
88 }
89 }
90 else if (streq(form, "sshkey"))
91 {
92 switch (type)
93 {
94 case CRED_PUBLIC_KEY:
95 *enc = PUBKEY_SSHKEY;
96 return TRUE;
97 default:
98 return FALSE;
99 }
100 }
101 return FALSE;
102 }
103
104 /**
105 * Callback credential set pki uses
106 */
107 static callback_cred_t *cb_set;
108
109 /**
110 * Callback function to receive credentials
111 */
112 static shared_key_t* cb(void *data, shared_key_type_t type,
113 identification_t *me, identification_t *other,
114 id_match_t *match_me, id_match_t *match_other)
115 {
116 char buf[64], *label, *secret;
117
118 switch (type)
119 {
120 case SHARED_PIN:
121 label = "Smartcard PIN";
122 break;
123 case SHARED_PRIVATE_KEY_PASS:
124 label = "Private key passphrase";
125 break;
126 default:
127 return NULL;
128 }
129 snprintf(buf, sizeof(buf), "%s: ", label);
130 secret = getpass(buf);
131 if (secret)
132 {
133 if (match_me)
134 {
135 *match_me = ID_MATCH_PERFECT;
136 }
137 if (match_other)
138 {
139 *match_other = ID_MATCH_NONE;
140 }
141 return shared_key_create(type,
142 chunk_clone(chunk_create(secret, strlen(secret))));
143 }
144 return NULL;
145 }
146
147 /**
148 * Register PIN/Passphrase callback function
149 */
150 static void add_callback()
151 {
152 cb_set = callback_cred_create_shared(cb, NULL);
153 lib->credmgr->add_set(lib->credmgr, &cb_set->set);
154 }
155
156 /**
157 * Unregister PIN/Passphrase callback function
158 */
159 static void remove_callback()
160 {
161 lib->credmgr->remove_set(lib->credmgr, &cb_set->set);
162 cb_set->destroy(cb_set);
163 }
164
165 /**
166 * Library initialization and operation parsing
167 */
168 int main(int argc, char *argv[])
169 {
170 atexit(library_deinit);
171 if (!library_init(NULL))
172 {
173 exit(SS_RC_LIBSTRONGSWAN_INTEGRITY);
174 }
175 if (lib->integrity &&
176 !lib->integrity->check_file(lib->integrity, "pki", argv[0]))
177 {
178 fprintf(stderr, "integrity check of pki failed\n");
179 exit(SS_RC_DAEMON_INTEGRITY);
180 }
181 if (!lib->plugins->load(lib->plugins,
182 lib->settings->get_str(lib->settings, "pki.load", PLUGINS)))
183 {
184 exit(SS_RC_INITIALIZATION_FAILED);
185 }
186
187 add_callback();
188 atexit(remove_callback);
189 return command_dispatch(argc, argv);
190 }
191