Added enum names for measurement algorithms
[strongswan.git] / src / libpts / pts / pts_meas_algo.c
1 /*
2 * Copyright (C) 2011 Andreas Steffen
3 * HSR 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 "pts_meas_algo.h"
17
18 #include <debug.h>
19
20 ENUM(pts_meas_algorithm_names, PTS_MEAS_ALGO_NONE, PTS_MEAS_ALGO_SHA384,
21 "None",
22 "SHA1",
23 "SHA256",
24 "SHA384"
25 );
26
27 /**
28 * Described in header.
29 */
30 bool pts_meas_algo_probe(pts_meas_algorithms_t *algorithms)
31 {
32 enumerator_t *enumerator;
33 hash_algorithm_t hash_alg;
34 const char *plugin_name;
35 char format1[] = " %s PTS measurement algorithm %N[%s] available";
36 char format2[] = " %s PTS measurement algorithm %N not available";
37
38 *algorithms = 0;
39
40 enumerator = lib->crypto->create_hasher_enumerator(lib->crypto);
41 while (enumerator->enumerate(enumerator, &hash_alg, &plugin_name))
42 {
43 if (hash_alg == HASH_SHA1)
44 {
45 *algorithms |= PTS_MEAS_ALGO_SHA1;
46 DBG2(DBG_PTS, format1, "mandatory", hash_algorithm_names, hash_alg,
47 plugin_name);
48 }
49 else if (hash_alg == HASH_SHA256)
50 {
51 *algorithms |= PTS_MEAS_ALGO_SHA256;
52 DBG2(DBG_PTS, format1, "mandatory", hash_algorithm_names, hash_alg,
53 plugin_name);
54 }
55 else if (hash_alg == HASH_SHA384)
56 {
57 *algorithms |= PTS_MEAS_ALGO_SHA384;
58 DBG2(DBG_PTS, format1, "optional ", hash_algorithm_names, hash_alg,
59 plugin_name);
60 }
61 }
62 enumerator->destroy(enumerator);
63
64 if (!(*algorithms & PTS_MEAS_ALGO_SHA384))
65 {
66 DBG1(DBG_PTS, format2, "optional ", hash_algorithm_names, HASH_SHA384);
67 }
68 if ((*algorithms & PTS_MEAS_ALGO_SHA1) &&
69 (*algorithms & PTS_MEAS_ALGO_SHA256))
70 {
71 return TRUE;
72 }
73 if (!(*algorithms & PTS_MEAS_ALGO_SHA1))
74 {
75 DBG1(DBG_PTS, format2, "mandatory", hash_algorithm_names, HASH_SHA1);
76 }
77 if (!(*algorithms & PTS_MEAS_ALGO_SHA256))
78 {
79 DBG1(DBG_PTS, format2, "mandatory", hash_algorithm_names, HASH_SHA256);
80 }
81 return FALSE;
82 }
83
84 /**
85 * Described in header.
86 */
87 bool pts_meas_algo_update(char *hash_alg, pts_meas_algorithms_t *algorithms)
88 {
89 if (strcaseeq(hash_alg, "sha384") || strcaseeq(hash_alg, "sha2_384"))
90 {
91 /* nothing to update, all algorithms are supported */
92 return TRUE;
93 }
94 if (strcaseeq(hash_alg, "sha256") || strcaseeq(hash_alg, "sha2_256"))
95 {
96 /* remove SHA384algorithm */
97 *algorithms &= ~PTS_MEAS_ALGO_SHA384;
98 return TRUE;
99 }
100 if (strcaseeq(hash_alg, "sha1"))
101 {
102 /* remove SHA384 and SHA256 algorithms */
103 *algorithms &= ~(PTS_MEAS_ALGO_SHA384 | PTS_MEAS_ALGO_SHA256);
104 return TRUE;
105 }
106 DBG1(DBG_PTS, "unknown hash algorithm '%s' configured", hash_alg);
107 return FALSE;
108 }
109
110 /**
111 * Described in header.
112 */
113 pts_meas_algorithms_t pts_meas_algo_select(pts_meas_algorithms_t supported_algos,
114 pts_meas_algorithms_t offered_algos)
115 {
116 if ((supported_algos & PTS_MEAS_ALGO_SHA384) &&
117 (offered_algos & PTS_MEAS_ALGO_SHA384))
118 {
119 return PTS_MEAS_ALGO_SHA384;
120 }
121 if ((supported_algos & PTS_MEAS_ALGO_SHA256) &&
122 (offered_algos & PTS_MEAS_ALGO_SHA256))
123 {
124 return PTS_MEAS_ALGO_SHA256;
125 }
126 if ((supported_algos & PTS_MEAS_ALGO_SHA1) &&
127 (offered_algos & PTS_MEAS_ALGO_SHA1))
128 {
129 return PTS_MEAS_ALGO_SHA1;
130 }
131 return PTS_MEAS_ALGO_NONE;
132 }
133
134 /**
135 * Described in header.
136 */
137 hash_algorithm_t pts_meas_algo_to_hash(pts_meas_algorithms_t algorithm)
138 {
139 switch (algorithm)
140 {
141 case PTS_MEAS_ALGO_SHA1:
142 return HASH_SHA1;
143 case PTS_MEAS_ALGO_SHA256:
144 return HASH_SHA256;
145 case PTS_MEAS_ALGO_SHA384:
146 return HASH_SHA384;
147 default:
148 return HASH_UNKNOWN;
149 }
150 }