2 * Copyright (C) 2009 Martin Willi
3 * Hochschule fuer Technik Rapperswil
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>.
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
22 #include <credentials/sets/callback_cred.h>
25 * Convert a form string to a encoding type
27 bool get_form(char *form
, cred_encoding_type_t
*enc
, credential_type_t type
)
29 if (streq(form
, "der"))
33 case CRED_CERTIFICATE
:
36 case CRED_PRIVATE_KEY
:
37 *enc
= PRIVKEY_ASN1_DER
;
40 /* der encoded keys usually contain the complete
41 * SubjectPublicKeyInfo */
42 *enc
= PUBKEY_SPKI_ASN1_DER
;
48 else if (streq(form
, "pem"))
52 case CRED_CERTIFICATE
:
55 case CRED_PRIVATE_KEY
:
65 else if (streq(form
, "pgp"))
69 case CRED_PRIVATE_KEY
:
83 * Convert a digest string to a hash algorithm
85 hash_algorithm_t
get_digest(char *name
)
87 if (streq(name
, "md5"))
91 if (streq(name
, "sha1"))
95 if (streq(name
, "sha224"))
99 if (streq(name
, "sha256"))
103 if (streq(name
, "sha384"))
107 if (streq(name
, "sha512"))
115 * Callback credential set pki uses
117 static callback_cred_t
*cb_set
;
120 * Callback function to receive credentials
122 static shared_key_t
* cb(void *data
, shared_key_type_t type
,
123 identification_t
*me
, identification_t
*other
,
124 id_match_t
*match_me
, id_match_t
*match_other
)
126 char buf
[64], *label
, *secret
;
131 label
= "Smartcard PIN";
133 case SHARED_PRIVATE_KEY_PASS
:
134 label
= "Private key passphrase";
139 snprintf(buf
, sizeof(buf
), "%s: ", label
);
140 secret
= getpass(buf
);
145 *match_me
= ID_MATCH_PERFECT
;
149 *match_other
= ID_MATCH_NONE
;
151 return shared_key_create(type
,
152 chunk_clone(chunk_create(secret
, strlen(secret
))));
158 * Register PIN/Passphrase callback function
160 static void add_callback()
162 cb_set
= callback_cred_create_shared(cb
, NULL
);
163 lib
->credmgr
->add_set(lib
->credmgr
, &cb_set
->set
);
167 * Unregister PIN/Passphrase callback function
169 static void remove_callback()
171 lib
->credmgr
->remove_set(lib
->credmgr
, &cb_set
->set
);
172 cb_set
->destroy(cb_set
);
176 * Library initialization and operation parsing
178 int main(int argc
, char *argv
[])
180 atexit(library_deinit
);
181 if (!library_init(NULL
))
183 exit(SS_RC_LIBSTRONGSWAN_INTEGRITY
);
185 if (lib
->integrity
&&
186 !lib
->integrity
->check_file(lib
->integrity
, "pki", argv
[0]))
188 fprintf(stderr
, "integrity check of pki failed\n");
189 exit(SS_RC_DAEMON_INTEGRITY
);
191 if (!lib
->plugins
->load(lib
->plugins
, NULL
,
192 lib
->settings
->get_str(lib
->settings
, "pki.load", PLUGINS
)))
194 exit(SS_RC_INITIALIZATION_FAILED
);
198 atexit(remove_callback
);
199 return command_dispatch(argc
, argv
);