splitted PKI tool to a file per command
[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 /**
20 * Convert a form string to a encoding type
21 */
22 bool get_form(char *form, key_encoding_type_t *type, bool pub)
23 {
24 if (streq(form, "der"))
25 {
26 /* der encoded keys usually contain the complete SubjectPublicKeyInfo */
27 *type = pub ? KEY_PUB_SPKI_ASN1_DER : KEY_PRIV_ASN1_DER;
28 }
29 else if (streq(form, "pem"))
30 {
31 *type = pub ? KEY_PUB_PEM : KEY_PRIV_PEM;
32 }
33 else if (streq(form, "pgp"))
34 {
35 *type = pub ? KEY_PUB_PGP : KEY_PRIV_PGP;
36 }
37 else
38 {
39 return FALSE;
40 }
41 return TRUE;
42 }
43
44 /**
45 * Convert a digest string to a hash algorithm
46 */
47 hash_algorithm_t get_digest(char *name)
48 {
49 if (streq(name, "md5"))
50 {
51 return HASH_MD5;
52 }
53 if (streq(name, "sha1"))
54 {
55 return HASH_SHA1;
56 }
57 if (streq(name, "sha224"))
58 {
59 return HASH_SHA224;
60 }
61 if (streq(name, "sha256"))
62 {
63 return HASH_SHA256;
64 }
65 if (streq(name, "sha384"))
66 {
67 return HASH_SHA384;
68 }
69 if (streq(name, "sha512"))
70 {
71 return HASH_SHA512;
72 }
73 return HASH_UNKNOWN;
74 }
75
76 /**
77 * Library initialization and operation parsing
78 */
79 int main(int argc, char *argv[])
80 {
81 atexit(library_deinit);
82 if (!library_init(STRONGSWAN_CONF))
83 {
84 exit(SS_RC_LIBSTRONGSWAN_INTEGRITY);
85 }
86 if (lib->integrity &&
87 !lib->integrity->check_file(lib->integrity, "pki", argv[0]))
88 {
89 fprintf(stderr, "integrity check of pki failed\n");
90 exit(SS_RC_DAEMON_INTEGRITY);
91 }
92 if (!lib->plugins->load(lib->plugins, PLUGINDIR,
93 lib->settings->get_str(lib->settings, "pki.load", PLUGINS)))
94 {
95 exit(SS_RC_INITIALIZATION_FAILED);
96 }
97 return command_dispatch(argc, argv);
98 }
99