From 8da8a0534888c8733b5c1ef6ebacbfb71a6b1201 Mon Sep 17 00:00:00 2001
From: Steffan Karger <steffan@karger.me>
Date: Thu, 20 Jul 2017 20:35:24 +0200
Subject: [PATCH 1/2] Add tls_cipher_list_for_each() helper function
We regularly have to walk through colon-separated lists, such as
--tls-cipher of --ncp-ciphers lists. Create a helper function to
reduce code duplication.
This is preparing for a commit that performs more accurate worst case
crypto overhead calculation based on the --ncp-ciphers list.
Signed-off-by: Steffan Karger <steffan@karger.me>
---
src/openvpn/ssl.c | 44 ++++++++++++++++++++++++++++++++++++++++----
1 file changed, 40 insertions(+), 4 deletions(-)
diff --git a/src/openvpn/ssl.c b/src/openvpn/ssl.c
index 8440f27..7a255c7 100644
a
|
b
|
key_ctx_update_implicit_iv(struct key_ctx *ctx, uint8_t *key, size_t key_len) |
1887 | 1887 | } |
1888 | 1888 | } |
1889 | 1889 | |
1890 | | bool |
1891 | | tls_item_in_cipher_list(const char *item, const char *list) |
| 1890 | |
| 1891 | /** |
| 1892 | * Function prototype to use with tls_cipher_list_for_each. |
| 1893 | * |
| 1894 | * @param ciphername The current cipher name. |
| 1895 | * @param ctx The function context. |
| 1896 | * |
| 1897 | * @return true stops iterating. |
| 1898 | */ |
| 1899 | typedef bool (*foreach_func)(const char *ciphername, void *ctx); |
| 1900 | |
| 1901 | /** |
| 1902 | * Call func for each item in colon-separated list. |
| 1903 | */ |
| 1904 | static void |
| 1905 | tls_cipher_list_for_each(const char *list, foreach_func func, void *func_ctx) |
1892 | 1906 | { |
1893 | 1907 | char *tmp_ciphers = string_alloc(list, NULL); |
1894 | 1908 | char *tmp_ciphers_orig = tmp_ciphers; |
… |
… |
tls_item_in_cipher_list(const char *item, const char *list) |
1896 | 1910 | const char *token = strtok(tmp_ciphers, ":"); |
1897 | 1911 | while (token) |
1898 | 1912 | { |
1899 | | if (0 == strcmp(token, item)) |
| 1913 | if (func(token, func_ctx)) |
1900 | 1914 | { |
1901 | 1915 | break; |
1902 | 1916 | } |
1903 | 1917 | token = strtok(NULL, ":"); |
1904 | 1918 | } |
1905 | 1919 | free(tmp_ciphers_orig); |
| 1920 | } |
| 1921 | |
| 1922 | struct cipher_equal_ctx { |
| 1923 | const char *match; |
| 1924 | bool found; |
| 1925 | }; |
| 1926 | static bool cipher_equal(const char *ciphername, void *vctx) |
| 1927 | { |
| 1928 | struct cipher_equal_ctx *ctx = vctx; |
| 1929 | ctx->found = 0 == strcmp(ciphername, ctx->match); |
| 1930 | return ctx->found; |
| 1931 | } |
| 1932 | |
| 1933 | bool |
| 1934 | tls_item_in_cipher_list(const char *item, const char *list) |
| 1935 | { |
| 1936 | struct cipher_equal_ctx ctx = (struct cipher_equal_ctx) { |
| 1937 | .match = item, |
| 1938 | .found = false, |
| 1939 | }; |
| 1940 | |
| 1941 | tls_cipher_list_for_each(list, cipher_equal, &ctx); |
1906 | 1942 | |
1907 | | return token != NULL; |
| 1943 | return ctx.found; |
1908 | 1944 | } |
1909 | 1945 | |
1910 | 1946 | void |