Don't use union members directly in plugin_feature_t.
[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->arg.crypter.alg == b->arg.crypter.alg &&
59 a->arg.crypter.key_size == b->arg.crypter.key_size;
60 case FEATURE_AEAD:
61 return a->arg.aead.alg == b->arg.aead.alg &&
62 a->arg.aead.key_size == b->arg.aead.key_size;
63 case FEATURE_SIGNER:
64 return a->arg.signer == b->arg.signer;
65 case FEATURE_HASHER:
66 return a->arg.hasher == b->arg.hasher;
67 case FEATURE_PRF:
68 return a->arg.prf == b->arg.prf;
69 case FEATURE_DH:
70 return a->arg.dh_group == b->arg.dh_group;
71 case FEATURE_RNG:
72 return a->arg.rng_quality <= b->arg.rng_quality;
73 case FEATURE_PRIVKEY:
74 case FEATURE_PRIVKEY_GEN:
75 case FEATURE_PUBKEY:
76 return a->arg.privkey == b->arg.privkey;
77 case FEATURE_PRIVKEY_SIGN:
78 case FEATURE_PUBKEY_VERIFY:
79 return a->arg.privkey_sign == b->arg.privkey_sign;
80 case FEATURE_PRIVKEY_DECRYPT:
81 case FEATURE_PUBKEY_ENCRYPT:
82 return a->arg.privkey_decrypt == b->arg.privkey_decrypt;
83 case FEATURE_CERT_DECODE:
84 case FEATURE_CERT_ENCODE:
85 return a->arg.cert == b->arg.cert;
86 case FEATURE_EAP_SERVER:
87 case FEATURE_EAP_PEER:
88 return a->arg.eap == b->arg.eap;
89 case FEATURE_DATABASE:
90 return a->arg.database == DB_ANY ||
91 a->arg.database == b->arg.database;
92 case FEATURE_FETCHER:
93 return a->arg.fetcher == NULL ||
94 streq(a->arg.fetcher, b->arg.fetcher);
95 case FEATURE_CUSTOM:
96 return streq(a->arg.custom, b->arg.custom);
97 }
98 }
99 return FALSE;
100 }
101
102 /**
103 * See header.
104 */
105 char* plugin_feature_get_string(plugin_feature_t *feature)
106 {
107 char *str = NULL;
108
109 if (feature->kind == FEATURE_REGISTER)
110 {
111 return strdup("(register function)");
112 }
113 switch (feature->type)
114 {
115 case FEATURE_NONE:
116 return strdup("NONE");
117 case FEATURE_CRYPTER:
118 if (asprintf(&str, "%N:%N-%d", plugin_feature_names, feature->type,
119 encryption_algorithm_names, feature->arg.crypter.alg,
120 feature->arg.crypter.key_size) > 0)
121 {
122 return str;
123 }
124 break;
125 case FEATURE_AEAD:
126 if (asprintf(&str, "%N:%N-%d", plugin_feature_names, feature->type,
127 encryption_algorithm_names, feature->arg.aead.alg,
128 feature->arg.aead.key_size) > 0)
129 {
130 return str;
131 }
132 break;
133 case FEATURE_SIGNER:
134 if (asprintf(&str, "%N:%N", plugin_feature_names, feature->type,
135 integrity_algorithm_names, feature->arg.signer) > 0)
136 {
137 return str;
138 }
139 break;
140 case FEATURE_HASHER:
141 if (asprintf(&str, "%N:%N", plugin_feature_names, feature->type,
142 hash_algorithm_names, feature->arg.hasher) > 0)
143 {
144 return str;
145 }
146 break;
147 case FEATURE_PRF:
148 if (asprintf(&str, "%N:%N", plugin_feature_names, feature->type,
149 pseudo_random_function_names, feature->arg.prf) > 0)
150 {
151 return str;
152 }
153 break;
154 case FEATURE_DH:
155 if (asprintf(&str, "%N:%N", plugin_feature_names, feature->type,
156 diffie_hellman_group_names, feature->arg.dh_group) > 0)
157 {
158 return str;
159 }
160 break;
161 case FEATURE_RNG:
162 if (asprintf(&str, "%N:%N", plugin_feature_names, feature->type,
163 rng_quality_names, feature->arg.rng_quality) > 0)
164 {
165 return str;
166 }
167 break;
168 case FEATURE_PRIVKEY:
169 case FEATURE_PRIVKEY_GEN:
170 case FEATURE_PUBKEY:
171 if (asprintf(&str, "%N:%N", plugin_feature_names, feature->type,
172 key_type_names, feature->arg.privkey) > 0)
173 {
174 return str;
175 }
176 break;
177 case FEATURE_PRIVKEY_SIGN:
178 case FEATURE_PUBKEY_VERIFY:
179 if (asprintf(&str, "%N:%N", plugin_feature_names, feature->type,
180 signature_scheme_names, feature->arg.privkey_sign) > 0)
181 {
182 return str;
183 }
184 break;
185 case FEATURE_PRIVKEY_DECRYPT:
186 case FEATURE_PUBKEY_ENCRYPT:
187 if (asprintf(&str, "%N:%N", plugin_feature_names, feature->type,
188 encryption_scheme_names, feature->arg.privkey_decrypt) > 0)
189 {
190 return str;
191 }
192 break;
193 case FEATURE_CERT_DECODE:
194 case FEATURE_CERT_ENCODE:
195 if (asprintf(&str, "%N:%N", plugin_feature_names, feature->type,
196 certificate_type_names, feature->arg.cert) > 0)
197 {
198 return str;
199 }
200 break;
201 case FEATURE_EAP_SERVER:
202 case FEATURE_EAP_PEER:
203 if (asprintf(&str, "%N:%N", plugin_feature_names, feature->type,
204 eap_type_short_names, feature->arg.eap) > 0)
205 {
206 return str;
207 }
208 break;
209 case FEATURE_DATABASE:
210 if (asprintf(&str, "%N:%N", plugin_feature_names, feature->type,
211 db_driver_names, feature->arg.database) > 0)
212 {
213 return str;
214 }
215 break;
216 case FEATURE_FETCHER:
217 if (asprintf(&str, "%N:%s", plugin_feature_names, feature->type,
218 feature->arg.fetcher) > 0)
219 {
220 return str;
221 }
222 break;
223 case FEATURE_CUSTOM:
224 if (asprintf(&str, "%N:%s", plugin_feature_names, feature->type,
225 feature->arg.custom) > 0)
226 {
227 return str;
228 }
229 break;
230 }
231 if (!str)
232 {
233 str = strdup("(invalid)");
234 }
235 return str;
236 }