pki tool supports single letter short options
[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 <debug.h>
20
21 /**
22 * Default debug level
23 */
24 int dbg_level = 1;
25
26 /**
27 * Logging to stderr with configurable debug level
28 */
29 void dbg_pki(int level, char *fmt, ...)
30 {
31 if (level <= dbg_level)
32 {
33 va_list args;
34
35 va_start(args, fmt);
36 vfprintf(stderr, fmt, args);
37 fprintf(stderr, "\n");
38 va_end(args);
39 }
40 }
41
42 /**
43 * Convert a form string to a encoding type
44 */
45 bool get_form(char *form, key_encoding_type_t *type, bool pub)
46 {
47 if (streq(form, "der"))
48 {
49 /* der encoded keys usually contain the complete SubjectPublicKeyInfo */
50 *type = pub ? KEY_PUB_SPKI_ASN1_DER : KEY_PRIV_ASN1_DER;
51 }
52 else if (streq(form, "pem"))
53 {
54 *type = pub ? KEY_PUB_PEM : KEY_PRIV_PEM;
55 }
56 else if (streq(form, "pgp"))
57 {
58 *type = pub ? KEY_PUB_PGP : KEY_PRIV_PGP;
59 }
60 else
61 {
62 return FALSE;
63 }
64 return TRUE;
65 }
66
67 /**
68 * Convert a digest string to a hash algorithm
69 */
70 hash_algorithm_t get_digest(char *name)
71 {
72 if (streq(name, "md5"))
73 {
74 return HASH_MD5;
75 }
76 if (streq(name, "sha1"))
77 {
78 return HASH_SHA1;
79 }
80 if (streq(name, "sha224"))
81 {
82 return HASH_SHA224;
83 }
84 if (streq(name, "sha256"))
85 {
86 return HASH_SHA256;
87 }
88 if (streq(name, "sha384"))
89 {
90 return HASH_SHA384;
91 }
92 if (streq(name, "sha512"))
93 {
94 return HASH_SHA512;
95 }
96 return HASH_UNKNOWN;
97 }
98
99 /**
100 * Library initialization and operation parsing
101 */
102 int main(int argc, char *argv[])
103 {
104 dbg = dbg_pki;
105
106 atexit(library_deinit);
107 if (!library_init(NULL))
108 {
109 exit(SS_RC_LIBSTRONGSWAN_INTEGRITY);
110 }
111 if (lib->integrity &&
112 !lib->integrity->check_file(lib->integrity, "pki", argv[0]))
113 {
114 fprintf(stderr, "integrity check of pki failed\n");
115 exit(SS_RC_DAEMON_INTEGRITY);
116 }
117 if (!lib->plugins->load(lib->plugins, NULL,
118 lib->settings->get_str(lib->settings, "pki.load", PLUGINS)))
119 {
120 exit(SS_RC_INITIALIZATION_FAILED);
121 }
122 return command_dispatch(argc, argv);
123 }
124