polarssl: add easy logging for PolarSSL errors
PATCH 1/2 polarssl: add easy logging for PolarSSL errors
Add the functions polar_log_err(), polar_log_func_line() and a macro polar_ok(), to easily log human-readable PolarSSL errors from polarssl-specific code.
This does not provide the full logging interface as msg(), because I would have to add a lot more of macro-magic to achieve that on the various supported compilers and platforms, and this suffices too (for now at least).
--- src/openvpn/crypto_polarssl.c | 27 +++++++++++++++++++++++++++ src/openvpn/crypto_polarssl.h | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+)
src/openvpn/crypto_polarssl.c +27 -0
@@ -46,6 +46,7 @@#include "misc.h" #include <polarssl/des.h> +#include <polarssl/error.h> #include <polarssl/md5.h> #include <polarssl/cipher.h> #include <polarssl/havege.h>
@@ -86,6 +87,32 @@ crypto_clear_error (void) { } +bool polar_log_err (unsigned int flags,int errval,const char *prefix) +{ +if (0 != errval) + { +char errstr[256 ]; +polarssl_strerror (errval, errstr,sizeof (errstr)); + +if (NULL == prefix) prefix ="PolarSSL error" ; +msg (flags,"%s: %s" , prefix, errstr); + } + +return 0 == errval; +} + +bool polar_log_func_line (unsigned int flags,int errval,const char *func, +int line) +{ +char prefix[256 ]; + +if (!openvpn_snprintf (prefix,sizeof (prefix),"%s:%d" , func, line)) +return polar_log_err (flags, errval, func); + +return polar_log_err (flags, errval, prefix); +} + +#ifdef DMALLOC void crypto_init_dmalloc (void )
src/openvpn/crypto_polarssl.h +40 -0
@@ -91,4 +91,44 @@ ctr_drbg_context * rand_ctx_get();void rand_ctx_enable_prediction_resistance ();#endif +/** +* Log the supplied PolarSSL error, prefixed by supplied prefix. +* +* @param flags Flags to indicate error type and priority. +* @param errval PolarSSL error code to convert to error message. +* @param prefix Prefix to PolarSSL error message. +* +* @returns true if no errors are detected, false otherwise. +*/ +bool polar_log_err (unsigned int flags,int errval,const char *prefix); + +/** +* Log the supplied PolarSSL error, prefixed by function name and line number. +* +* @param flags Flags to indicate error type and priority. +* @param errval PolarSSL error code to convert to error message. +* @param func Function name where error was reported. +* @param line Line number where error was reported. +* +* @returns true if no errors are detected, false otherwise. +*/ +bool polar_log_func_line (unsigned int flags,int errval,const char *func, +int line); + +/** +* Check errval and log on error. +* +* Convenience wrapper to put around polarssl library calls, e.g. +* if (!polar_ok(polarssl_func())) return 0; +* or +* ASSERT (polar_ok(polarssl_func())); +* +* @param errval PolarSSL error code to convert to error message. +* +* @returns true if no errors are detected, false otherwise. +*/ +#define polar_ok (errval) \ + polar_log_func_line(D_CRYPT_ERRORS, errval, __func__, __LINE__) + +#endif /* CRYPTO_POLARSSL_H_ */
Signature · 1 line
2.1.0
PATCH 2/2 polarssl: Improve PolarSSL logging
Use the new polar_log_err() and polar_ok() functions introduced in the previous commit to provide more log/debug output for polarssl errors.
--- src/openvpn/crypto_polarssl.c | 35 ++++++++------- src/openvpn/ssl_polarssl.c | 92 +++++++++++++++++---------------------- src/openvpn/ssl_verify_polarssl.c | 26 +++-------- 3 files changed, 66 insertions(+), 87 deletions(-)
src/openvpn/crypto_polarssl.c +19 -16
@@ -261,7 +261,8 @@ ctr_drbg_context * rand_ctx_get()/* Initialise PolarSSL RNG, and built-in entropy sources */ entropy_init (&ec); -if (0 !=ctr_drbg_init (&cd_ctx, entropy_func, &ec,BPTR (&pers_string),BLEN (&pers_string))) +if (!polar_ok (ctr_drbg_init (&cd_ctx, entropy_func, &ec, +BPTR (&pers_string),BLEN (&pers_string))))msg (M_FATAL ,"Failed to initialize random generator" );gc_free (&gc);
@@ -472,10 +473,10 @@ cipher_ctx_init (cipher_context_t *ctx, uint8_t *key, int key_len,CLEAR (*ctx); -if (0 !=cipher_init_ctx (ctx, kt)) +if (!polar_ok (cipher_init_ctx (ctx, kt)))msg (M_FATAL ,"PolarSSL cipher context init #1" ); -if (0 !=cipher_setkey (ctx, key, key_len*8 , enc)) +if (!polar_ok (cipher_setkey (ctx, key, key_len*8 , enc)))msg (M_FATAL ,"PolarSSL cipher set key" );/* make sure we used a big enough key */
@@ -484,7 +485,7 @@ cipher_ctx_init (cipher_context_t *ctx, uint8_t *key, int key_len,void cipher_ctx_cleanup (cipher_context_t *ctx) { -cipher_free_ctx (ctx); +polar_ok (cipher_free_ctx (ctx)); }int cipher_ctx_iv_length (const cipher_context_t *ctx)
@@ -514,36 +515,38 @@ cipher_ctx_get_cipher_kt (const cipher_ctx_t *ctx)int cipher_ctx_reset (cipher_context_t *ctx,uint8_t *iv_buf) { -int retval =cipher_reset (ctx); +if (!polar_ok (cipher_reset (ctx))) +return 0 ; -if (0 == retval) - retval =cipher_set_iv (ctx, iv_buf, ctx->cipher_info->iv_size); +if (!polar_ok (cipher_set_iv (ctx, iv_buf, ctx->cipher_info->iv_size))) +return 0 ; -return 0 == retval; +return 1 ; }int cipher_ctx_update (cipher_context_t *ctx,uint8_t *dst,int *dst_len,uint8_t *src,int src_len) { -int retval =0 ;size_t s_dst_len = *dst_len; - retval =cipher_update (ctx, src, (size_t )src_len, dst, &s_dst_len); +if (!polar_ok (cipher_update (ctx, src, (size_t )src_len, dst, &s_dst_len))) +return 0 ; *dst_len = s_dst_len; -return 0 == retval; +return 1 ; }int cipher_ctx_final (cipher_context_t *ctx,uint8_t *dst,int *dst_len) { -int retval =0 ;size_t s_dst_len = *dst_len; - retval =cipher_finish (ctx, dst, &s_dst_len); +if (!polar_ok (cipher_finish (ctx, dst, &s_dst_len))) +return 0 ; + *dst_len = s_dst_len; -return 0 == retval; +return 1 ; }void
@@ -553,8 +556,8 @@ cipher_des_encrypt_ecb (const unsigned char key[DES_KEY_LENGTH], {des_context ctx; -des_setkey_enc (&ctx, key); -des_crypt_ecb (&ctx, src, dst); +ASSERT (polar_ok (des_setkey_enc (&ctx, key))); +ASSERT (polar_ok (des_crypt_ecb (&ctx, src, dst))); }
src/openvpn/ssl_polarssl.c +40 -52
231 lines not on this page · raw message
src/openvpn/ssl_verify_polarssl.c +7 -19
@@ -134,17 +134,12 @@ backend_x509_get_serial (openvpn_x509_cert_t *cert, struct gc_arena *gc)char *buf =NULL ;size_t buflen =0 ;mpi serial_mpi = {0 }; -int retval =0 ;/* Transform asn1 integer serial into PolarSSL MPI */ mpi_init (&serial_mpi); - retval =mpi_read_binary (&serial_mpi, cert->serial.p, cert->serial.len); -if (retval <0 ) +if (!polar_ok (mpi_read_binary (&serial_mpi, cert->serial.p, cert->serial.len))) { -char errbuf[128 ]; -polarssl_strerror (retval, errbuf,sizeof (errbuf)); - -msg (M_WARN ,"Failed to retrieve serial from certificate: %s." , errbuf); +msg (M_WARN ,"Failed to retrieve serial from certificate." );return NULL ; }
@@ -153,13 +148,9 @@ backend_x509_get_serial (openvpn_x509_cert_t *cert, struct gc_arena *gc) buf =gc_malloc (buflen, true, gc);/* Write MPI serial as decimal string into buffer */ - retval =mpi_write_string (&serial_mpi,10 , buf, &buflen); -if (retval <0 ) +if (!polar_ok (mpi_write_string (&serial_mpi,10 , buf, &buflen))) { -char errbuf[128 ]; -polarssl_strerror (retval, errbuf,sizeof (errbuf)); - -msg (M_WARN ,"Failed to write serial to string: %s." , errbuf); +msg (M_WARN ,"Failed to write serial to string." );return NULL ; }
@@ -374,12 +365,9 @@ x509_verify_crl(const char *crl_file, x509_crt *cert, const char *subject)result_t retval =FAILURE ;x509_crl crl = {0 }; -int polar_retval =x509_crl_parse_file (&crl, crl_file); -if (polar_retval !=0 ) +if (!polar_ok (x509_crl_parse_file (&crl, crl_file))) { -char errstr[128 ]; -polarssl_strerror (polar_retval, errstr,sizeof (errstr)); -msg (M_WARN ,"CRL: cannot read CRL from file %s (%s)" , crl_file, errstr); +msg (M_WARN ,"CRL: cannot read CRL from file %s" , crl_file); goto end; }
@@ -392,7 +380,7 @@ x509_verify_crl(const char *crl_file, x509_crt *cert, const char *subject) goto end; } -if (0 !=x509_crt_revoked (cert, &crl)) +if (!polar_ok (x509_crt_revoked (cert, &crl))) {msg (D_HANDSHAKE ,"CRL CHECK FAILED: %s is REVOKED" , subject); goto end;
Signature · 1 line
2.1.0
Re: [Openvpn-devel] [PATCH 2/2] polarssl: Improve PolarSSL logging
Hi,
Quoted 2 lines · Steffan Karger
Use the new
polar_log_err()andpolar_ok()functions introduced in the previous commit to provide more log/debug output for polarssl errors.
For the record on the list - this one looks good, but got left in the sun for so long that it's not applying any longer :(
13:10 <@syzzer> I can send rebased versions when I get home tonight
"please" :-)
gert
Signature · 4 lines
USENET is *not* the non-clickable part of WWW!
//www.muc.de/~gert/
Gert Doering - Munich, Germany [email protected]...
fax: +49-89-35655025 [email protected]...PATCH 1/1 polarssl: Improve PolarSSL logging
Use the new polar_log_err() and polar_ok() functions introduced in the previous commit to provide more log/debug output for polarssl errors.
--- src/openvpn/crypto_polarssl.c | 35 ++++++++------- src/openvpn/ssl_polarssl.c | 89 +++++++++++++++++---------------------- src/openvpn/ssl_verify_polarssl.c | 26 +++--------- 3 files changed, 64 insertions(+), 86 deletions(-)
src/openvpn/crypto_polarssl.c +19 -16
@@ -261,7 +261,8 @@ ctr_drbg_context * rand_ctx_get()/* Initialise PolarSSL RNG, and built-in entropy sources */ entropy_init (&ec); -if (0 !=ctr_drbg_init (&cd_ctx, entropy_func, &ec,BPTR (&pers_string),BLEN (&pers_string))) +if (!polar_ok (ctr_drbg_init (&cd_ctx, entropy_func, &ec, +BPTR (&pers_string),BLEN (&pers_string))))msg (M_FATAL ,"Failed to initialize random generator" );gc_free (&gc);
@@ -472,10 +473,10 @@ cipher_ctx_init (cipher_context_t *ctx, uint8_t *key, int key_len,CLEAR (*ctx); -if (0 !=cipher_init_ctx (ctx, kt)) +if (!polar_ok (cipher_init_ctx (ctx, kt)))msg (M_FATAL ,"PolarSSL cipher context init #1" ); -if (0 !=cipher_setkey (ctx, key, key_len*8 , enc)) +if (!polar_ok (cipher_setkey (ctx, key, key_len*8 , enc)))msg (M_FATAL ,"PolarSSL cipher set key" );/* make sure we used a big enough key */
@@ -484,7 +485,7 @@ cipher_ctx_init (cipher_context_t *ctx, uint8_t *key, int key_len,void cipher_ctx_cleanup (cipher_context_t *ctx) { -cipher_free_ctx (ctx); +ASSERT (polar_ok (cipher_free_ctx (ctx))); }int cipher_ctx_iv_length (const cipher_context_t *ctx)
@@ -514,36 +515,38 @@ cipher_ctx_get_cipher_kt (const cipher_ctx_t *ctx)int cipher_ctx_reset (cipher_context_t *ctx,uint8_t *iv_buf) { -int retval =cipher_reset (ctx); +if (!polar_ok (cipher_reset (ctx))) +return 0 ; -if (0 == retval) - retval =cipher_set_iv (ctx, iv_buf, ctx->cipher_info->iv_size); +if (!polar_ok (cipher_set_iv (ctx, iv_buf, ctx->cipher_info->iv_size))) +return 0 ; -return 0 == retval; +return 1 ; }int cipher_ctx_update (cipher_context_t *ctx,uint8_t *dst,int *dst_len,uint8_t *src,int src_len) { -int retval =0 ;size_t s_dst_len = *dst_len; - retval =cipher_update (ctx, src, (size_t )src_len, dst, &s_dst_len); +if (!polar_ok (cipher_update (ctx, src, (size_t )src_len, dst, &s_dst_len))) +return 0 ; *dst_len = s_dst_len; -return 0 == retval; +return 1 ; }int cipher_ctx_final (cipher_context_t *ctx,uint8_t *dst,int *dst_len) { -int retval =0 ;size_t s_dst_len = *dst_len; - retval =cipher_finish (ctx, dst, &s_dst_len); +if (!polar_ok (cipher_finish (ctx, dst, &s_dst_len))) +return 0 ; + *dst_len = s_dst_len; -return 0 == retval; +return 1 ; }void
@@ -553,8 +556,8 @@ cipher_des_encrypt_ecb (const unsigned char key[DES_KEY_LENGTH], {des_context ctx; -des_setkey_enc (&ctx, key); -des_crypt_ecb (&ctx, src, dst); +ASSERT (polar_ok (des_setkey_enc (&ctx, key))); +ASSERT (polar_ok (des_crypt_ecb (&ctx, src, dst))); }
src/openvpn/ssl_polarssl.c +38 -51
222 lines not on this page · raw message
src/openvpn/ssl_verify_polarssl.c +7 -19
@@ -134,17 +134,12 @@ backend_x509_get_serial (openvpn_x509_cert_t *cert, struct gc_arena *gc)char *buf =NULL ;size_t buflen =0 ;mpi serial_mpi = {0 }; -int retval =0 ;/* Transform asn1 integer serial into PolarSSL MPI */ mpi_init (&serial_mpi); - retval =mpi_read_binary (&serial_mpi, cert->serial.p, cert->serial.len); -if (retval <0 ) +if (!polar_ok (mpi_read_binary (&serial_mpi, cert->serial.p, cert->serial.len))) { -char errbuf[128 ]; -polarssl_strerror (retval, errbuf,sizeof (errbuf)); - -msg (M_WARN ,"Failed to retrieve serial from certificate: %s." , errbuf); +msg (M_WARN ,"Failed to retrieve serial from certificate." );return NULL ; }
@@ -153,13 +148,9 @@ backend_x509_get_serial (openvpn_x509_cert_t *cert, struct gc_arena *gc) buf =gc_malloc (buflen, true, gc);/* Write MPI serial as decimal string into buffer */ - retval =mpi_write_string (&serial_mpi,10 , buf, &buflen); -if (retval <0 ) +if (!polar_ok (mpi_write_string (&serial_mpi,10 , buf, &buflen))) { -char errbuf[128 ]; -polarssl_strerror (retval, errbuf,sizeof (errbuf)); - -msg (M_WARN ,"Failed to write serial to string: %s." , errbuf); +msg (M_WARN ,"Failed to write serial to string." );return NULL ; }
@@ -376,12 +367,9 @@ x509_verify_crl(const char *crl_file, x509_crt *cert, const char *subject)struct gc_arena gc =gc_new ();char *serial; -int polar_retval =x509_crl_parse_file (&crl, crl_file); -if (polar_retval !=0 ) +if (!polar_ok (x509_crl_parse_file (&crl, crl_file))) { -char errstr[128 ]; -polarssl_strerror (polar_retval, errstr,sizeof (errstr)); -msg (M_WARN ,"CRL: cannot read CRL from file %s (%s)" , crl_file, errstr); +msg (M_WARN ,"CRL: cannot read CRL from file %s" , crl_file); goto end; }
@@ -394,7 +382,7 @@ x509_verify_crl(const char *crl_file, x509_crt *cert, const char *subject) goto end; } -if (0 !=x509_crt_revoked (cert, &crl)) +if (!polar_ok (x509_crt_revoked (cert, &crl))) { serial =backend_x509_get_serial_hex (cert, &gc);msg (D_HANDSHAKE ,"CRL CHECK FAILED: %s (serial %s) is REVOKED" , subject, (serial ? serial :"NOT AVAILABLE" ));
Signature · 1 line
2.1.4
[Openvpn-devel] [PATCH applied] Re: polarssl: Improve PolarSSL logging
ACK. Lots of small changes, but they all look correct to me, and make the code in total easier to read (at least, if the reader understands what polar_ok() does :-) ).
Your patch has been applied to the master branch.
commit d17d362dfec1abc5bedcea2f1154470018c82eca
Author: Steffan Karger Date: Tue Oct 6 18:20:40 2015 +0200
polarssl: Improve PolarSSL logging
Signed-off-by: Steffan Karger <[email protected]...>
Acked-by: Gert Doering <[email protected]...>
Message-Id: <[email protected]...>
URL: http://article.gmane.org/gmane.network.openvpn.devel/10197
Signed-off-by: Gert Doering <[email protected]...>--
kind regards,
Gert Doering
ACK. This one is nicely trivial, as it just adds easily verifiable stuff without *changing* anything.
Your patch has been applied to the master branch.
commit 6ef5df14917500f107fd843a6dba61355edaeea0
Author: Steffan Karger Date: Sun Mar 8 11:20:04 2015 +0100
polarssl: add easy logging for PolarSSL errors
Signed-off-by: Steffan Karger <[email protected]...>
Acked-by: Gert Doering <[email protected]...>
Message-Id: <[email protected]...>
URL: http://article.gmane.org/gmane.network.openvpn.devel/9528
Signed-off-by: Gert Doering <[email protected]...>--
kind regards,
Gert Doering