Bug Summary

File:lib/algorithms/mac.c
Location:line 165, column 22
Description:Value stored to 'i' is never read

Annotated Source Code

1/*
2 * Copyright (C) 2011-2012 Free Software Foundation, Inc.
3 *
4 * Author: Nikos Mavrogiannopoulos
5 *
6 * This file is part of GnuTLS.
7 *
8 * The GnuTLS is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public License
10 * as published by the Free Software Foundation; either version 3 of
11 * the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>
20 *
21 */
22
23#include <gnutls_int.h>
24#include <algorithms.h>
25#include <gnutls_errors.h>
26#include <x509/common.h>
27
28struct gnutls_hash_entry
29{
30 const char *name;
31 const char *oid;
32 gnutls_mac_algorithm_t id;
33 size_t key_size; /* in case of mac */
34 unsigned placeholder; /* if set, then not a real MAC */
35};
36typedef struct gnutls_hash_entry gnutls_hash_entry;
37
38static const gnutls_hash_entry hash_algorithms[] = {
39 {"SHA1", HASH_OID_SHA1"1.3.14.3.2.26", GNUTLS_MAC_SHA1, 20, 0},
40 {"MD5", HASH_OID_MD5"1.2.840.113549.2.5", GNUTLS_MAC_MD5, 16, 0},
41 {"SHA256", HASH_OID_SHA256"2.16.840.1.101.3.4.2.1", GNUTLS_MAC_SHA256, 32, 0},
42 {"SHA384", HASH_OID_SHA384"2.16.840.1.101.3.4.2.2", GNUTLS_MAC_SHA384, 48, 0},
43 {"SHA512", HASH_OID_SHA512"2.16.840.1.101.3.4.2.3", GNUTLS_MAC_SHA512, 64, 0},
44 {"SHA224", HASH_OID_SHA224"2.16.840.1.101.3.4.2.4", GNUTLS_MAC_SHA224, 28, 0},
45 {"AEAD", NULL((void*)0), GNUTLS_MAC_AEAD, 0, 1},
46 {"MD2", HASH_OID_MD2"1.2.840.113549.2.2", GNUTLS_MAC_MD2, 0, 0}, /* not used as MAC */
47 {"RIPEMD160", HASH_OID_RMD160"1.3.36.3.2.1", GNUTLS_MAC_RMD160, 20, 0},
48 {"MAC-NULL", NULL((void*)0), GNUTLS_MAC_NULL, 0, 0},
49 {0, 0, 0, 0}
50};
51
52
53#define GNUTLS_HASH_LOOP(b)const gnutls_hash_entry *p; for(p = hash_algorithms; p->name
!= ((void*)0); p++) { b ; }
\
54 const gnutls_hash_entry *p; \
55 for(p = hash_algorithms; p->name != NULL((void*)0); p++) { b ; }
56
57#define GNUTLS_HASH_ALG_LOOP(a)const gnutls_hash_entry *p; for(p = hash_algorithms; p->name
!= ((void*)0); p++) { if(p->id == algorithm) { a; break; }
; }
\
58 GNUTLS_HASH_LOOP( if(p->id == algorithm) { a; break; } )const gnutls_hash_entry *p; for(p = hash_algorithms; p->name
!= ((void*)0); p++) { if(p->id == algorithm) { a; break; }
; }
59
60int
61_gnutls_mac_priority (gnutls_session_t session,
62 gnutls_mac_algorithm_t algorithm)
63{ /* actually returns the priority */
64 unsigned int i;
65 for (i = 0; i < session->internals.priorities.mac.algorithms; i++)
66 {
67 if (session->internals.priorities.mac.priority[i] == algorithm)
68 return i;
69 }
70 return -1;
71}
72
73/**
74 * gnutls_mac_get_name:
75 * @algorithm: is a MAC algorithm
76 *
77 * Convert a #gnutls_mac_algorithm_t value to a string.
78 *
79 * Returns: a string that contains the name of the specified MAC
80 * algorithm, or %NULL.
81 **/
82const char *
83gnutls_mac_get_name (gnutls_mac_algorithm_t algorithm)
84{
85 const char *ret = NULL((void*)0);
86
87 /* avoid prefix */
88 GNUTLS_HASH_ALG_LOOP (ret = p->name)const gnutls_hash_entry *p; for(p = hash_algorithms; p->name
!= ((void*)0); p++) { if(p->id == algorithm) { ret = p->
name; break; } ; }
;
89
90 return ret;
91}
92
93/**
94 * gnutls_mac_get_id:
95 * @name: is a MAC algorithm name
96 *
97 * Convert a string to a #gnutls_mac_algorithm_t value. The names are
98 * compared in a case insensitive way.
99 *
100 * Returns: a #gnutls_mac_algorithm_t id of the specified MAC
101 * algorithm string, or %GNUTLS_MAC_UNKNOWN on failures.
102 **/
103gnutls_mac_algorithm_t
104gnutls_mac_get_id (const char *name)
105{
106 gnutls_mac_algorithm_t ret = GNUTLS_MAC_UNKNOWN;
107
108 GNUTLS_HASH_LOOP (const gnutls_hash_entry *p; for(p = hash_algorithms; p->name
!= ((void*)0); p++) { if (strcasecmp (p->name, name) == 0
) { ret = p->id; break; } ; }
109 if (strcasecmp (p->name, name) == 0)const gnutls_hash_entry *p; for(p = hash_algorithms; p->name
!= ((void*)0); p++) { if (strcasecmp (p->name, name) == 0
) { ret = p->id; break; } ; }
110 {const gnutls_hash_entry *p; for(p = hash_algorithms; p->name
!= ((void*)0); p++) { if (strcasecmp (p->name, name) == 0
) { ret = p->id; break; } ; }
111 ret = p->id;const gnutls_hash_entry *p; for(p = hash_algorithms; p->name
!= ((void*)0); p++) { if (strcasecmp (p->name, name) == 0
) { ret = p->id; break; } ; }
112 break;const gnutls_hash_entry *p; for(p = hash_algorithms; p->name
!= ((void*)0); p++) { if (strcasecmp (p->name, name) == 0
) { ret = p->id; break; } ; }
113 }const gnutls_hash_entry *p; for(p = hash_algorithms; p->name
!= ((void*)0); p++) { if (strcasecmp (p->name, name) == 0
) { ret = p->id; break; } ; }
114 )const gnutls_hash_entry *p; for(p = hash_algorithms; p->name
!= ((void*)0); p++) { if (strcasecmp (p->name, name) == 0
) { ret = p->id; break; } ; }
;
115
116 return ret;
117}
118
119/**
120 * gnutls_mac_get_key_size:
121 * @algorithm: is an encryption algorithm
122 *
123 * Get size of MAC key.
124 *
125 * Returns: length (in bytes) of the given MAC key size, or 0 if the
126 * given MAC algorithm is invalid.
127 **/
128size_t
129gnutls_mac_get_key_size (gnutls_mac_algorithm_t algorithm)
130{
131 size_t ret = 0;
132
133 /* avoid prefix */
134 GNUTLS_HASH_ALG_LOOP (ret = p->key_size)const gnutls_hash_entry *p; for(p = hash_algorithms; p->name
!= ((void*)0); p++) { if(p->id == algorithm) { ret = p->
key_size; break; } ; }
;
135
136 return ret;
137}
138
139/**
140 * gnutls_mac_list:
141 *
142 * Get a list of hash algorithms for use as MACs. Note that not
143 * necessarily all MACs are supported in TLS cipher suites. For
144 * example, MD2 is not supported as a cipher suite, but is supported
145 * for other purposes (e.g., X.509 signature verification or similar).
146 *
147 * This function is not thread safe.
148 *
149 * Returns: Return a (0)-terminated list of #gnutls_mac_algorithm_t
150 * integers indicating the available MACs.
151 **/
152const gnutls_mac_algorithm_t *
153gnutls_mac_list (void)
154{
155static gnutls_mac_algorithm_t supported_macs[MAX_ALGOS32] = { 0 };
156
157 if (supported_macs[0] == 0)
158 {
159 int i = 0;
160
161 GNUTLS_HASH_LOOP (const gnutls_hash_entry *p; for(p = hash_algorithms; p->name
!= ((void*)0); p++) { if (p->placeholder != 0 || _gnutls_hmac_exists
(p->id)) supported_macs[i++]=p->id; ; }
162 if (p->placeholder != 0 || _gnutls_hmac_exists(p->id))const gnutls_hash_entry *p; for(p = hash_algorithms; p->name
!= ((void*)0); p++) { if (p->placeholder != 0 || _gnutls_hmac_exists
(p->id)) supported_macs[i++]=p->id; ; }
163 supported_macs[i++]=p->id;const gnutls_hash_entry *p; for(p = hash_algorithms; p->name
!= ((void*)0); p++) { if (p->placeholder != 0 || _gnutls_hmac_exists
(p->id)) supported_macs[i++]=p->id; ; }
164 )const gnutls_hash_entry *p; for(p = hash_algorithms; p->name
!= ((void*)0); p++) { if (p->placeholder != 0 || _gnutls_hmac_exists
(p->id)) supported_macs[i++]=p->id; ; }
;
165 supported_macs[i++]=0;
Value stored to 'i' is never read
166 }
167
168 return supported_macs;
169}
170
171const char *
172_gnutls_x509_mac_to_oid (gnutls_mac_algorithm_t algorithm)
173{
174 const char *ret = NULL((void*)0);
175
176 /* avoid prefix */
177 GNUTLS_HASH_ALG_LOOP (ret = p->oid)const gnutls_hash_entry *p; for(p = hash_algorithms; p->name
!= ((void*)0); p++) { if(p->id == algorithm) { ret = p->
oid; break; } ; }
;
178
179 return ret;
180}
181
182gnutls_mac_algorithm_t
183_gnutls_x509_oid2mac_algorithm (const char *oid)
184{
185 gnutls_mac_algorithm_t ret = 0;
186
187 GNUTLS_HASH_LOOP (if (p->oid && strcmp (oid, p->oid) == 0)const gnutls_hash_entry *p; for(p = hash_algorithms; p->name
!= ((void*)0); p++) { if (p->oid && strcmp (oid, p
->oid) == 0) { ret = p->id; break;} ; }
188 {const gnutls_hash_entry *p; for(p = hash_algorithms; p->name
!= ((void*)0); p++) { if (p->oid && strcmp (oid, p
->oid) == 0) { ret = p->id; break;} ; }
189 ret = p->id; break;}const gnutls_hash_entry *p; for(p = hash_algorithms; p->name
!= ((void*)0); p++) { if (p->oid && strcmp (oid, p
->oid) == 0) { ret = p->id; break;} ; }
190 )const gnutls_hash_entry *p; for(p = hash_algorithms; p->name
!= ((void*)0); p++) { if (p->oid && strcmp (oid, p
->oid) == 0) { ret = p->id; break;} ; }
;
191
192 if (ret == 0)
193 return GNUTLS_MAC_UNKNOWN;
194 return ret;
195}
196
197const char *
198_gnutls_x509_digest_to_oid (gnutls_digest_algorithm_t algorithm)
199{
200 return _gnutls_x509_mac_to_oid ((gnutls_mac_algorithm_t) algorithm);
201}
202
203gnutls_digest_algorithm_t
204_gnutls_x509_oid2digest_algorithm (const char *oid)
205{
206 return (gnutls_digest_algorithm_t) _gnutls_x509_oid2mac_algorithm (oid);
207}
208
209const char *
210_gnutls_digest_get_name (gnutls_digest_algorithm_t algorithm)
211{
212 return gnutls_mac_get_name ((gnutls_digest_algorithm_t) algorithm);
213}
214
215int
216_gnutls_mac_is_ok (gnutls_mac_algorithm_t algorithm)
217{
218 ssize_t ret = -1;
219 GNUTLS_HASH_ALG_LOOP (ret = p->id)const gnutls_hash_entry *p; for(p = hash_algorithms; p->name
!= ((void*)0); p++) { if(p->id == algorithm) { ret = p->
id; break; } ; }
;
220 if (ret >= 0)
221 ret = 0;
222 else
223 ret = 1;
224 return ret;
225}