29ca101302fe66833df9b7455b16ade6bb6e7ef3
[strongswan.git] / src / libstrongswan / plugins / plugin_feature.c
1 /*
2 * Copyright (C) 2011 Martin Willi
3 * Copyright (C) 2011 revosec AG
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 #define _GNU_SOURCE
17 #include <stdio.h>
18
19 #include "plugin_feature.h"
20
21 ENUM(plugin_feature_names, FEATURE_NONE, FEATURE_CUSTOM,
22 "NONE",
23 "CRYPTER",
24 "AEAD",
25 "SIGNER",
26 "HASHER",
27 "PRF",
28 "DH",
29 "RNG",
30 "PRIVKEY",
31 "PRIVKEY_GEN",
32 "PRIVKEY_SIGN",
33 "PRIVKEY_DECRYPT",
34 "PUBKEY",
35 "PUBKEY_VERIFY",
36 "PUBKEY_ENCRYPT",
37 "CERT_DECODE",
38 "CERT_ENCODE",
39 "EAP_SERVER",
40 "EAP_CLIENT",
41 "DATABASE",
42 "FETCHER",
43 "CUSTOM",
44 );
45
46 /**
47 * See header.
48 */
49 bool plugin_feature_matches(plugin_feature_t *a, plugin_feature_t *b)
50 {
51 if (a->type == b->type)
52 {
53 switch (a->type)
54 {
55 case FEATURE_NONE:
56 return FALSE;
57 case FEATURE_CRYPTER:
58 return a->crypter.alg == b->crypter.alg &&
59 a->crypter.key_size == b->crypter.key_size;
60 case FEATURE_AEAD:
61 return a->aead.alg == b->aead.alg &&
62 a->aead.key_size == b->aead.key_size;
63 case FEATURE_SIGNER:
64 return a->signer == b->signer;
65 case FEATURE_HASHER:
66 return a->hasher == b->hasher;
67 case FEATURE_PRF:
68 return a->prf == b->prf;
69 case FEATURE_DH:
70 return a->dh_group == b->dh_group;
71 case FEATURE_RNG:
72 return a->rng_quality <= b->rng_quality;
73 case FEATURE_PRIVKEY:
74 case FEATURE_PRIVKEY_GEN:
75 case FEATURE_PUBKEY:
76 return a->privkey == b->privkey;
77 case FEATURE_PRIVKEY_SIGN:
78 case FEATURE_PUBKEY_VERIFY:
79 return a->privkey_sign == b->privkey_sign;
80 case FEATURE_PRIVKEY_DECRYPT:
81 case FEATURE_PUBKEY_ENCRYPT:
82 return a->privkey_decrypt == b->privkey_decrypt;
83 case FEATURE_CERT_DECODE:
84 case FEATURE_CERT_ENCODE:
85 return a->cert == b->cert;
86 case FEATURE_EAP_SERVER:
87 case FEATURE_EAP_PEER:
88 return a->eap == b->eap;
89 case FEATURE_DATABASE:
90 return a->database == DB_ANY || a->database == b->database;
91 case FEATURE_FETCHER:
92 return a->fetcher == NULL || streq(a->fetcher, b->fetcher);
93 case FEATURE_CUSTOM:
94 return streq(a->custom, b->custom);
95 }
96 }
97 return FALSE;
98 }
99
100 /**
101 * See header.
102 */
103 char* plugin_feature_get_string(plugin_feature_t *feature)
104 {
105 char *str = NULL;
106
107 if (feature->kind == FEATURE_REGISTER)
108 {
109 return strdup("(register function)");
110 }
111 switch (feature->type)
112 {
113 case FEATURE_NONE:
114 return strdup("NONE");
115 case FEATURE_CRYPTER:
116 if (asprintf(&str, "%N:%N-%d", plugin_feature_names, feature->type,
117 encryption_algorithm_names, feature->crypter.alg,
118 feature->crypter.key_size) > 0)
119 {
120 return str;
121 }
122 break;
123 case FEATURE_AEAD:
124 if (asprintf(&str, "%N:%N-%d", plugin_feature_names, feature->type,
125 encryption_algorithm_names, feature->aead.alg,
126 feature->aead.key_size) > 0)
127 {
128 return str;
129 }
130 break;
131 case FEATURE_SIGNER:
132 if (asprintf(&str, "%N:%N", plugin_feature_names, feature->type,
133 integrity_algorithm_names, feature->signer) > 0)
134 {
135 return str;
136 }
137 break;
138 case FEATURE_HASHER:
139 if (asprintf(&str, "%N:%N", plugin_feature_names, feature->type,
140 hash_algorithm_names, feature->hasher) > 0)
141 {
142 return str;
143 }
144 break;
145 case FEATURE_PRF:
146 if (asprintf(&str, "%N:%N", plugin_feature_names, feature->type,
147 pseudo_random_function_names, feature->prf) > 0)
148 {
149 return str;
150 }
151 break;
152 case FEATURE_DH:
153 if (asprintf(&str, "%N:%N", plugin_feature_names, feature->type,
154 diffie_hellman_group_names, feature->dh_group) > 0)
155 {
156 return str;
157 }
158 break;
159 case FEATURE_RNG:
160 if (asprintf(&str, "%N:%N", plugin_feature_names, feature->type,
161 rng_quality_names, feature->rng_quality) > 0)
162 {
163 return str;
164 }
165 break;
166 case FEATURE_PRIVKEY:
167 case FEATURE_PRIVKEY_GEN:
168 case FEATURE_PUBKEY:
169 if (asprintf(&str, "%N:%N", plugin_feature_names, feature->type,
170 key_type_names, feature->privkey) > 0)
171 {
172 return str;
173 }
174 break;
175 case FEATURE_PRIVKEY_SIGN:
176 case FEATURE_PUBKEY_VERIFY:
177 if (asprintf(&str, "%N:%N", plugin_feature_names, feature->type,
178 signature_scheme_names, feature->privkey_sign) > 0)
179 {
180 return str;
181 }
182 break;
183 case FEATURE_PRIVKEY_DECRYPT:
184 case FEATURE_PUBKEY_ENCRYPT:
185 if (asprintf(&str, "%N:%N", plugin_feature_names, feature->type,
186 encryption_scheme_names, feature->privkey_decrypt) > 0)
187 {
188 return str;
189 }
190 break;
191 case FEATURE_CERT_DECODE:
192 case FEATURE_CERT_ENCODE:
193 if (asprintf(&str, "%N:%N", plugin_feature_names, feature->type,
194 certificate_type_names, feature->cert) > 0)
195 {
196 return str;
197 }
198 break;
199 case FEATURE_EAP_SERVER:
200 case FEATURE_EAP_PEER:
201 if (asprintf(&str, "%N:%N", plugin_feature_names, feature->type,
202 eap_type_short_names, feature->eap) > 0)
203 {
204 return str;
205 }
206 break;
207 case FEATURE_DATABASE:
208 if (asprintf(&str, "%N:%N", plugin_feature_names, feature->type,
209 db_driver_names, feature->database) > 0)
210 {
211 return str;
212 }
213 break;
214 case FEATURE_FETCHER:
215 if (asprintf(&str, "%N:%s", plugin_feature_names, feature->type,
216 feature->fetcher) > 0)
217 {
218 return str;
219 }
220 break;
221 case FEATURE_CUSTOM:
222 if (asprintf(&str, "%N:%s", plugin_feature_names, feature->type,
223 feature->custom) > 0)
224 {
225 return str;
226 }
227 break;
228 }
229 if (!str)
230 {
231 str = strdup("(invalid)");
232 }
233 return str;
234 }