Skip to content

Commit c0927d0

Browse files
committed
tlshd: GNUTTLS_CIPHER_* are not C macros
"#if defined" does not work on an enum. The original commit did not generate a compilation error, but does not work when tested on a system with a non-kTLS-enabled GnuTLS. To enable compilation to work on platforms with an older GnuTLS library, remove support for SM4 ciphers. The explicit switch in tlshd_initialize_ktls() is meant as a fallback for when GnuTLS is not built with kTLS support, so perhaps SM4 cipher support is not necessary in tlshd. Fixes: a38e1f8 ("Initial commit") Signed-off-by: Chuck Lever <[email protected]>
1 parent 63b09a2 commit c0927d0

File tree

1 file changed

+8
-98
lines changed

1 file changed

+8
-98
lines changed

src/tlshd/ktls.c

Lines changed: 8 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939

4040
#include "tlshd.h"
4141

42-
#if defined(TLS_CIPHER_AES_GCM_128) && defined(GNUTLS_CIPHER_AES_128_GCM)
42+
#if defined(TLS_CIPHER_AES_GCM_128)
4343
static bool tlshd_set_aes_gcm128_info(gnutls_session_t session, int sock,
4444
unsigned read)
4545
{
@@ -81,7 +81,7 @@ static bool tlshd_set_aes_gcm128_info(gnutls_session_t session, int sock,
8181
}
8282
#endif
8383

84-
#if defined(TLS_CIPHER_AES_GCM_256) && defined(GNUTLS_CIPHER_AES_256_GCM)
84+
#if defined(TLS_CIPHER_AES_GCM_256)
8585
static bool tlshd_set_aes_gcm256_info(gnutls_session_t session, int sock,
8686
unsigned read)
8787
{
@@ -123,7 +123,7 @@ static bool tlshd_set_aes_gcm256_info(gnutls_session_t session, int sock,
123123
}
124124
#endif
125125

126-
#if defined(TLS_CIPHER_AES_CCM_128) && defined(GNUTLS_CIPHER_AES_128_CCM)
126+
#if defined(TLS_CIPHER_AES_CCM_128)
127127
static bool tlshd_set_aes_ccm128_info(gnutls_session_t session, int sock,
128128
unsigned read)
129129
{
@@ -165,7 +165,7 @@ static bool tlshd_set_aes_ccm128_info(gnutls_session_t session, int sock,
165165
}
166166
#endif
167167

168-
#if defined(TLS_CIPHER_CHACHA20_POLY1305) && defined(GNUTLS_CIPHER_CHACHA20_POLY1305)
168+
#if defined(TLS_CIPHER_CHACHA20_POLY1305)
169169
static bool tlshd_set_chacha20_poly1305_info(gnutls_session_t session, int sock,
170170
unsigned read)
171171
{
@@ -203,84 +203,6 @@ static bool tlshd_set_chacha20_poly1305_info(gnutls_session_t session, int sock,
203203
}
204204
#endif
205205

206-
#if defined(TLS_CIPHER_SM4_GCM) && defined(GNUTLS_CIPHER_SM4_GCM)
207-
static bool tlshd_set_sm4_gcm_info(gnutls_session_t session, int sock,
208-
unsigned read)
209-
{
210-
struct tls12_crypto_info_sm4_gcm info = {
211-
.info.version = TLS_1_3_VERSION,
212-
.info.cipher_type = TLS_CIPHER_SM4_GCM,
213-
};
214-
unsigned char seq_number[8];
215-
gnutls_datum_t cipher_key;
216-
gnutls_datum_t mac_key;
217-
gnutls_datum_t iv;
218-
int ret;
219-
220-
ret = gnutls_record_get_state(session, read, &mac_key, &iv,
221-
&cipher_key, seq_number);
222-
if (ret != GNUTLS_E_SUCCESS) {
223-
tlshd_log_gnutls_error(ret);
224-
return false;
225-
}
226-
227-
if (gnutls_protocol_get_version(session) == GNUTLS_TLS1_2)
228-
info.info.version = TLS_1_2_VERSION;
229-
230-
memcpy(info.iv, iv.data + TLS_CIPHER_SM4_GCM_SALT_SIZE,
231-
TLS_CIPHER_SM4_GCM_IV_SIZE);
232-
memcpy(info.key, cipher_key.data, TLS_CIPHER_SM4_GCM_KEY_SIZE);
233-
memcpy(info.rec_seq, seq_number, TLS_CIPHER_SM4_GCM_REC_SEQ_SIZE);
234-
235-
if (setsockopt(sock, SOL_TLS, read ? TLS_RX : TLS_TX,
236-
&info, sizeof(info)) == -1) {
237-
tlshd_log_perror("setsockopt");
238-
return false;
239-
}
240-
241-
return true;
242-
}
243-
#endif
244-
245-
#if defined(TLS_CIPHER_SM4_CCM) && defined(GNUTLS_CIPHER_SM4_CCM)
246-
static bool tlshd_set_sm4_ccm_info(gnutls_session_t session, int sock,
247-
unsigned read)
248-
{
249-
struct tls12_crypto_info_sm4_gcm info = {
250-
.info.version = TLS_1_3_VERSION,
251-
.info.cipher_type = TLS_CIPHER_SM4_CCM,
252-
};
253-
unsigned char seq_number[8];
254-
gnutls_datum_t cipher_key;
255-
gnutls_datum_t mac_key;
256-
gnutls_datum_t iv;
257-
int ret;
258-
259-
ret = gnutls_record_get_state(session, read, &mac_key, &iv,
260-
&cipher_key, seq_number);
261-
if (ret != GNUTLS_E_SUCCESS) {
262-
tlshd_log_gnutls_error(ret);
263-
return false;
264-
}
265-
266-
if (gnutls_protocol_get_version(session) == GNUTLS_TLS1_2)
267-
info.info.version = TLS_1_2_VERSION;
268-
269-
memcpy(info.iv, iv.data + TLS_CIPHER_SM4_CCM_SALT_SIZE,
270-
TLS_CIPHER_SM4_CCM_IV_SIZE);
271-
memcpy(info.key, cipher_key.data, TLS_CIPHER_SM4_CCM_KEY_SIZE);
272-
memcpy(info.rec_seq, seq_number, TLS_CIPHER_SM4_CCM_REC_SEQ_SIZE);
273-
274-
if (setsockopt(sock, SOL_TLS, read ? TLS_RX : TLS_TX,
275-
&info, sizeof(info)) == -1) {
276-
tlshd_log_perror("setsockopt");
277-
return false;
278-
}
279-
280-
return true;
281-
}
282-
#endif
283-
284206
/**
285207
* tlshd_initialize_ktls - Initialize socket for use by kTLS
286208
* @session: TLS session descriptor
@@ -307,41 +229,29 @@ int tlshd_initialize_ktls(gnutls_session_t session)
307229
gnutls_transport_get_int2(session, &sockin, &sockout);
308230

309231
switch (gnutls_cipher_get(session)) {
310-
#if defined(TLS_CIPHER_AES_GCM_128) && defined(GNUTLS_CIPHER_AES_128_GCM)
232+
#if defined(TLS_CIPHER_AES_GCM_128)
311233
case GNUTLS_CIPHER_AES_128_GCM:
312234
tlshd_log_debug("Negotiated cipher: AES_GCM_128");
313235
return tlshd_set_aes_gcm128_info(session, sockout, 0) &&
314236
tlshd_set_aes_gcm128_info(session, sockin, 1) ? 0 : -EACCES;
315237
#endif
316-
#if defined(TLS_CIPHER_AES_GCM_256) && defined(GNUTLS_CIPHER_AES_256_GCM)
238+
#if defined(TLS_CIPHER_AES_GCM_256)
317239
case GNUTLS_CIPHER_AES_256_GCM:
318240
tlshd_log_debug("Negotiated cipher: AES_GCM_256");
319241
return tlshd_set_aes_gcm256_info(session, sockout, 0) &&
320242
tlshd_set_aes_gcm256_info(session, sockin, 1) ? 0 : -EACCES;
321243
#endif
322-
#if defined(TLS_CIPHER_AES_CCM_128) && defined(GNUTLS_CIPHER_AES_128_CCM)
244+
#if defined(TLS_CIPHER_AES_CCM_128)
323245
case GNUTLS_CIPHER_AES_128_CCM:
324246
tlshd_log_debug("Negotiated cipher: AES_CCM_128");
325247
return tlshd_set_aes_ccm128_info(session, sockout, 0) &&
326248
tlshd_set_aes_ccm128_info(session, sockin, 1) ? 0 : -EACCES;
327249
#endif
328-
#if defined(TLS_CIPHER_CHACHA20_POLY1305) && defined(GNUTLS_CIPHER_CHACHA20_POLY1305)
250+
#if defined(TLS_CIPHER_CHACHA20_POLY1305)
329251
case GNUTLS_CIPHER_CHACHA20_POLY1305:
330252
tlshd_log_debug("Negotiated cipher: ChaCha20_Poly1305");
331253
return tlshd_set_chacha20_poly1305_info(session, sockout, 0) &&
332254
tlshd_set_chacha20_poly1305_info(session, sockin, 1) ? 0 : -EACCES;
333-
#endif
334-
#if defined(TLS_CIPHER_SM4_GCM) && defined(GNUTLS_CIPHER_SM4_GCM)
335-
case GNUTLS_CIPHER_SM4_GCM:
336-
tlshd_log_debug("Negotiated cipher: SM4_GCM");
337-
return tlshd_set_sm4_gcm_info(session, sockout, 0) &&
338-
tlshd_set_sm4_gcm_info(session, sockin, 1) ? 0 : -EACCES;
339-
#endif
340-
#if defined(TLS_CIPHER_SM4_CCM) && defined(GNUTLS_CIPHER_SM4_CCM)
341-
case GNUTLS_CIPHER_SM4_CCM:
342-
tlshd_log_debug("Negotiated cipher: SM4_CCM");
343-
return tlshd_set_sm4_ccm_info(session, sockout, 0) &&
344-
tlshd_set_sm4_ccm_info(session, sockin, 1) ? 0 : -EACCES;
345255
#endif
346256
default:
347257
tlshd_log_error("tlshd does not support the requested cipher.");

0 commit comments

Comments
 (0)