crypto: unify key_type creation code
PATCH 1/1 crypto: unify key_type creation code
At the moment we have tls_crypt_kt() and auth_token_kt that basically do the same thing, but with different algorithms used to inizialise the structure.
In order to avoid code duplication and copy/paste errors, unify code and make it parametric, so that it can be re-used in various places.
--- src/openvpn/auth_token.c | 20 +------------------- src/openvpn/crypto.h | 31 +++++++++++++++++++++++++++++++ src/openvpn/tls_crypt.c | 27 +++------------------------ 3 files changed, 35 insertions(+), 43 deletions(-)
src/openvpn/auth_token.c +1 -19
@@ -30,24 +30,6 @@ const char *auth_token_pem_name = "OpenVPN auth-token server key";/* Size of the data of the token (not b64 encoded and without prefix) */ #define TOKEN_DATA_LEN (2 * sizeof(int64_t) + AUTH_TOKEN_SESSION_ID_LEN + 32) -static struct key_type -auth_token_kt (void ) -{ -struct key_type kt = {0 }; -/* We do not encrypt our session tokens */ - kt.cipher ="none" ; - kt.digest ="SHA256" ; - -if (!md_valid (kt.digest)) - { -msg (M_WARN ,"ERROR: --tls-crypt requires HMAC-SHA-256 support." ); -return (struct key_type ) {0 }; - } - -return kt; -} - -void add_session_token_env (struct tls_session *session,struct tls_multi *multi,const struct user_pass *up)
@@ -138,7 +120,7 @@ void auth_token_init_secret(struct key_ctx *key_ctx, const char *key_file, bool key_inline) { - struct key_type kt = auth_token_kt(); + struct key_type kt = create_kt("none", "SHA256", "auth-gen-token");struct buffer server_secret_key =alloc_buf (2048 );
src/openvpn/crypto.h +31 -0
@@ -547,4 +547,35 @@ key_ctx_bi_defined(const struct key_ctx_bi *key)*/ const char *print_key_filename (const char *str,bool is_inline); +/** +* Creates and validates an instance of struct key_type with the provided +* algs. +* +* @param cipher the cipher algorithm to use (must be a string literal) +* @param md the digest algorithm to use (must be a string literal) +* +* @return the initialized key_type instance +*/ +static inline struct key_type +create_kt (const char *cipher,const char *md,const char *optname) +{ +struct key_type kt; + kt.cipher = cipher; + kt.digest = md; + +if (cipher_defined (kt.cipher) && !cipher_valid (kt.cipher)) + { +msg (M_WARN ,"ERROR: --%s requires %s support." , optname, kt.cipher); +return (struct key_type ) {0 }; + } +if (md_defined (kt.digest) && !md_valid (kt.digest)) + { +msg (M_WARN ,"ERROR: --%s requires %s support." , optname, kt.digest); +return (struct key_type ) {0 }; + } + +return kt; +} + +#endif /* CRYPTO_H */
src/openvpn/tls_crypt.c +3 -24
@@ -47,27 +47,6 @@ static const uint8_t TLS_CRYPT_METADATA_TYPE_USER = 0x00;/** Metadata contains a 64-bit unix timestamp in network byte order */ static const uint8_t TLS_CRYPT_METADATA_TYPE_TIMESTAMP =0x01 ; -static struct key_type -tls_crypt_kt (void ) -{ -struct key_type kt; - kt.cipher ="AES-256-CTR" ; - kt.digest ="SHA256" ; - -if (!cipher_valid (kt.cipher)) - { -msg (M_WARN ,"ERROR: --tls-crypt requires AES-256-CTR support." ); -return (struct key_type ) {0 }; - } -if (!md_valid (kt.digest)) - { -msg (M_WARN ,"ERROR: --tls-crypt requires HMAC-SHA-256 support." ); -return (struct key_type ) {0 }; - } - -return kt; -} -int tls_crypt_buf_overhead (void ) {
@@ -80,7 +59,7 @@ tls_crypt_init_key(struct key_ctx_bi *key, const char *key_file, {const int key_direction = tls_server ?KEY_DIRECTION_NORMAL :KEY_DIRECTION_INVERSE ; -struct key_type kt =tls_crypt_kt (); +struct key_type kt =create_kt ("AES-256-CTR" ,"SHA256" ,"tls-crypt" );if (!kt.cipher || !kt.digest) {msg (M_FATAL ,"ERROR: --tls-crypt not supported" );
@@ -271,7 +250,7 @@ tls_crypt_v2_load_client_key(struct key_ctx_bi *key, const struct key2 *key2, {const int key_direction = tls_server ?KEY_DIRECTION_NORMAL :KEY_DIRECTION_INVERSE ; -struct key_type kt =tls_crypt_kt (); +struct key_type kt =create_kt ("AES-256-CTR" ,"SHA256" ,"tls-crypt" );if (!kt.cipher || !kt.digest) {msg (M_FATAL ,"ERROR: --tls-crypt-v2 not supported" );
@@ -319,7 +298,7 @@ tls_crypt_v2_init_server_key(struct key_ctx *key_ctx, bool encrypt,msg (M_FATAL ,"ERROR: invalid tls-crypt-v2 server key format" ); } -struct key_type kt =tls_crypt_kt (); +struct key_type kt =create_kt ("AES-256-CTR" ,"SHA256" ,"tls-crypt" );if (!kt.cipher || !kt.digest) {msg (M_FATAL ,"ERROR: --tls-crypt-v2 not supported" );
Signature · 1 line
2.34.1
PATCH v2 1/1 crypto: unify key_type creation code
At the moment we have tls_crypt_kt() and auth_token_kt that basically do the same thing, but with different algorithms used to inizialise the structure.
In order to avoid code duplication and copy/paste errors, unify code and make it parametric, so that it can be re-used in various places.
---
Changes from v1:
* added doc for optname param
--- src/openvpn/auth_token.c | 20 +------------------- src/openvpn/crypto.h | 32 ++++++++++++++++++++++++++++++++ src/openvpn/tls_crypt.c | 27 +++------------------------ 3 files changed, 36 insertions(+), 43 deletions(-)
src/openvpn/auth_token.c +1 -19
@@ -30,24 +30,6 @@ const char *auth_token_pem_name = "OpenVPN auth-token server key";/* Size of the data of the token (not b64 encoded and without prefix) */ #define TOKEN_DATA_LEN (2 * sizeof(int64_t) + AUTH_TOKEN_SESSION_ID_LEN + 32) -static struct key_type -auth_token_kt (void ) -{ -struct key_type kt = {0 }; -/* We do not encrypt our session tokens */ - kt.cipher ="none" ; - kt.digest ="SHA256" ; - -if (!md_valid (kt.digest)) - { -msg (M_WARN ,"ERROR: --tls-crypt requires HMAC-SHA-256 support." ); -return (struct key_type ) {0 }; - } - -return kt; -} - -void add_session_token_env (struct tls_session *session,struct tls_multi *multi,const struct user_pass *up)
@@ -138,7 +120,7 @@ void auth_token_init_secret(struct key_ctx *key_ctx, const char *key_file, bool key_inline) { - struct key_type kt = auth_token_kt(); + struct key_type kt = create_kt("none", "SHA256", "auth-gen-token");struct buffer server_secret_key =alloc_buf (2048 );
src/openvpn/crypto.h +32 -0
@@ -547,4 +547,36 @@ key_ctx_bi_defined(const struct key_ctx_bi *key)*/ const char *print_key_filename (const char *str,bool is_inline); +/** +* Creates and validates an instance of struct key_type with the provided +* algs. +* +* @param cipher the cipher algorithm to use (must be a string literal) +* @param md the digest algorithm to use (must be a string literal) +* @param optname the name of the option requiring the key_type object +* +* @return the initialized key_type instance +*/ +static inline struct key_type +create_kt (const char *cipher,const char *md,const char *optname) +{ +struct key_type kt; + kt.cipher = cipher; + kt.digest = md; + +if (cipher_defined (kt.cipher) && !cipher_valid (kt.cipher)) + { +msg (M_WARN ,"ERROR: --%s requires %s support." , optname, kt.cipher); +return (struct key_type ) {0 }; + } +if (md_defined (kt.digest) && !md_valid (kt.digest)) + { +msg (M_WARN ,"ERROR: --%s requires %s support." , optname, kt.digest); +return (struct key_type ) {0 }; + } + +return kt; +} + +#endif /* CRYPTO_H */
src/openvpn/tls_crypt.c +3 -24
@@ -47,27 +47,6 @@ static const uint8_t TLS_CRYPT_METADATA_TYPE_USER = 0x00;/** Metadata contains a 64-bit unix timestamp in network byte order */ static const uint8_t TLS_CRYPT_METADATA_TYPE_TIMESTAMP =0x01 ; -static struct key_type -tls_crypt_kt (void ) -{ -struct key_type kt; - kt.cipher ="AES-256-CTR" ; - kt.digest ="SHA256" ; - -if (!cipher_valid (kt.cipher)) - { -msg (M_WARN ,"ERROR: --tls-crypt requires AES-256-CTR support." ); -return (struct key_type ) {0 }; - } -if (!md_valid (kt.digest)) - { -msg (M_WARN ,"ERROR: --tls-crypt requires HMAC-SHA-256 support." ); -return (struct key_type ) {0 }; - } - -return kt; -} -int tls_crypt_buf_overhead (void ) {
@@ -80,7 +59,7 @@ tls_crypt_init_key(struct key_ctx_bi *key, const char *key_file, {const int key_direction = tls_server ?KEY_DIRECTION_NORMAL :KEY_DIRECTION_INVERSE ; -struct key_type kt =tls_crypt_kt (); +struct key_type kt =create_kt ("AES-256-CTR" ,"SHA256" ,"tls-crypt" );if (!kt.cipher || !kt.digest) {msg (M_FATAL ,"ERROR: --tls-crypt not supported" );
@@ -271,7 +250,7 @@ tls_crypt_v2_load_client_key(struct key_ctx_bi *key, const struct key2 *key2, {const int key_direction = tls_server ?KEY_DIRECTION_NORMAL :KEY_DIRECTION_INVERSE ; -struct key_type kt =tls_crypt_kt (); +struct key_type kt =create_kt ("AES-256-CTR" ,"SHA256" ,"tls-crypt" );if (!kt.cipher || !kt.digest) {msg (M_FATAL ,"ERROR: --tls-crypt-v2 not supported" );
@@ -319,7 +298,7 @@ tls_crypt_v2_init_server_key(struct key_ctx *key_ctx, bool encrypt,msg (M_FATAL ,"ERROR: invalid tls-crypt-v2 server key format" ); } -struct key_type kt =tls_crypt_kt (); +struct key_type kt =create_kt ("AES-256-CTR" ,"SHA256" ,"tls-crypt" );if (!kt.cipher || !kt.digest) {msg (M_FATAL ,"ERROR: --tls-crypt-v2 not supported" );
Signature · 1 line
2.34.1
Am 17.02.22 um 15:32 schrieb Antonio Quartulli:
Quoted 3 lines
At the moment we have
tls_crypt_kt()andauth_token_ktthat basically do the same thing, but with different algorithms used to inizialise the structure.
Should be initialise or initialize depending on your dialect.
Quoted 6 lines
In order to avoid code duplication and copy/paste errors, unify code and make it parametric, so that it can be re-used in various places.
Signed-off-by Antonio Quartulli <[email protected]...>
Hi,
Quoted 8 lines · Antonio Quartulli
At the moment we have
tls_crypt_kt()andauth_token_ktthat basically do the same thing, but with different algorithms used to inizialise the structure.In order to avoid code duplication and copy/paste errors, unify code and make it parametric, so that it can be re-used in various places.
Signed-off-by Antonio Quartulli <[email protected]...>
NAK. Please run "make check" on your patches :-)
../../../../openvpn/tests/unit_tests/openvpn/test_auth_token.c:95:15: warning: implicit declaration of
function 'auth_token_kt' is invalid in C99 [-Wimplicit-function-declaration]
ctx->kt = auth_token_kt();
^
../../../../openvpn/tests/unit_tests/openvpn/test_auth_token.c:95:13: error: assigning to 'struct key_type'
from incompatible type 'int'
ctx->kt = auth_token_kt();
^ ~~~~~~~~~~~~~~~gert
Signature · 6 lines
"If was one thing all people took for granted, was conviction that if you
feed honest figures into a computer, honest figures come out. Never doubted
it myself till I met a computer with a sense of humor."
Robert A. Heinlein, The Moon is a Harsh Mistress
Gert Doering - Munich, Germany [email protected]...PATCH v3 1/1 crypto: unify key_type creation code
In reply to Antonio QuartulliAt the moment we have tls_crypt_kt() and auth_token_kt that basically do the same thing, but with different algorithms used to initialise the structure.
In order to avoid code duplication and copy/paste errors, unify code and make it parametric, so that it can be re-used in various places.
---
Changes from v1:
* added doc for optname param
Changes from v2:
* restore original helper functions and reduce their bodies to calling the generic create_kt(). This way users of those funcs are not harmed.
--- src/openvpn/auth_token.c | 14 +------------- src/openvpn/crypto.h | 31 +++++++++++++++++++++++++++++++ src/openvpn/tls_crypt.c | 17 +---------------- 3 files changed, 33 insertions(+), 29 deletions(-)
src/openvpn/auth_token.c +1 -13
@@ -33,21 +33,9 @@ const char *auth_token_pem_name = "OpenVPN auth-token server key";static struct key_type auth_token_kt (void ) { -struct key_type kt = {0 }; -/* We do not encrypt our session tokens */ - kt.cipher ="none" ; - kt.digest ="SHA256" ; - -if (!md_valid (kt.digest)) - { -msg (M_WARN ,"ERROR: --tls-crypt requires HMAC-SHA-256 support." ); -return (struct key_type ) {0 }; - } - -return kt; +return create_kt ("none" ,"SHA256" ,"auth-gen-token" ); } -void add_session_token_env (struct tls_session *session,struct tls_multi *multi,const struct user_pass *up)
src/openvpn/crypto.h +31 -0
@@ -547,4 +547,35 @@ key_ctx_bi_defined(const struct key_ctx_bi *key)*/ const char *print_key_filename (const char *str,bool is_inline); +/** +* Creates and validates an instance of struct key_type with the provided +* algs. +* +* @param cipher the cipher algorithm to use (must be a string literal) +* @param md the digest algorithm to use (must be a string literal) +* @param optname the name of the option requiring the key_type object +* +* @return the initialized key_type instance +*/ +static inline struct key_type +create_kt (const char *cipher,const char *md,const char *optname) +{ +struct key_type kt; + kt.cipher = cipher; + kt.digest = md; + +if (cipher_defined (kt.cipher) && !cipher_valid (kt.cipher)) + { +msg (M_WARN ,"ERROR: --%s requires %s support." , optname, kt.cipher); +return (struct key_type ) {0 }; + } +if (md_defined (kt.digest) && !md_valid (kt.digest)) + { +msg (M_WARN ,"ERROR: --%s requires %s support." , optname, kt.digest); +return (struct key_type ) {0 }; + } + +return kt; +} +#endif /* CRYPTO_H */
src/openvpn/tls_crypt.c +1 -16
@@ -50,22 +50,7 @@ static const uint8_t TLS_CRYPT_METADATA_TYPE_TIMESTAMP = 0x01;static struct key_type tls_crypt_kt (void ) { -struct key_type kt; - kt.cipher ="AES-256-CTR" ; - kt.digest ="SHA256" ; - -if (!cipher_valid (kt.cipher)) - { -msg (M_WARN ,"ERROR: --tls-crypt requires AES-256-CTR support." ); -return (struct key_type ) {0 }; - } -if (!md_valid (kt.digest)) - { -msg (M_WARN ,"ERROR: --tls-crypt requires HMAC-SHA-256 support." ); -return (struct key_type ) {0 }; - } - -return kt; +return create_kt ("AES-256-CTR" ,"SHA256" ,"tls-crypt" ); }int
Signature · 1 line
2.34.1
Patch looks good, "make check" passes again :-)
Your patch has been applied to the master branch.
commit 2e7ec64fc40cb5d184fd5c47c875ce381ca1b8d3
Author: Antonio Quartulli Date: Thu Feb 17 17:31:59 2022 +0100
crypto: unify key_type creation code
Signed-off-by: Antonio Quartulli <[email protected]...>
Acked-by: Gert Doering <[email protected]...>
Message-Id: <[email protected]...>
URL: https://www.mail-archive.com/[email protected]/msg23831.html
Signed-off-by: Gert Doering <[email protected]...>--
kind regards,
Gert Doering