From eb2b6ae66e27d07cc1461e474c8bc41ecf2c3d55 Mon Sep 17 00:00:00 2001 From: kakwa Date: Mon, 12 Sep 2016 08:39:03 +0200 Subject: [PATCH] code cleaning an reorganization --- inc/http.h | 4 +++ inc/rfc3161.h | 11 ++++---- inc/utils.h | 9 +++++++ src/lib/http.c | 15 ----------- src/lib/rfc3161.c | 64 ++++++++++++++++------------------------------- src/lib/utils.c | 20 ++++++--------- 6 files changed, 47 insertions(+), 76 deletions(-) diff --git a/inc/http.h b/inc/http.h index 61fdd07..1e76c06 100644 --- a/inc/http.h +++ b/inc/http.h @@ -1,4 +1,8 @@ #include #include "utils.h" +struct tuser_data { + char *first_message; +}; + int http_server_start(char *conffile, char *conf_wd, bool stdout_dbg); diff --git a/inc/rfc3161.h b/inc/rfc3161.h index 6d00849..a721bf6 100644 --- a/inc/rfc3161.h +++ b/inc/rfc3161.h @@ -13,7 +13,10 @@ #include "context.h" /* Name of config entry that defines the OID file. */ -#define ENV_OID_FILE "oid_file" +#define OID_SECTION "oids" + +// number of char we get to log for the serial +#define SERIAL_ID_SIZE 8 #define B_FORMAT_TEXT 0x8000 #define FORMAT_UNDEF 0 @@ -23,11 +26,7 @@ static ASN1_OBJECT *txt2obj(const char *oid); /* Reply related functions. */ -static int reply_command(CONF *conf, char *section, char *engine, char *query, - char *passin, char *inkey, const EVP_MD *md, - char *signer, char *chain, const char *policy, - char *in, int token_in, char *out, int token_out, - int text); +static ASN1_INTEGER *serial_cb(TS_RESP_CTX *ctx, void *data); static TS_RESP *read_PKCS7(BIO *in_bio); int create_response(rfc3161_context *ct, char *query, int query_len, TS_RESP_CTX *resp_ctx, size_t *resp_size, diff --git a/inc/utils.h b/inc/utils.h index 5613438..961936d 100644 --- a/inc/utils.h +++ b/inc/utils.h @@ -1,5 +1,12 @@ #include "rfc3161.h" +typedef struct _code { + char *c_name; + int c_val; +} CODE; + +static void signal_handler_general(int sig_num); +static void signal_handler_up(int sig_num); void skeleton_daemon(); int init_pid(char *pidfile_path); int write_pid(char *pidfile_path); @@ -9,5 +16,7 @@ void log_hex(rfc3161_context *ct, int priority, char *id, int set_params(rfc3161_context *ct, char *conf_file, char *conf_wd); static char *rand_string(char *str, size_t size); void free_uts_context(rfc3161_context *ct); + +// some global variable to handle signals int g_uts_sig_up; int g_uts_sig; diff --git a/src/lib/http.c b/src/lib/http.c index 2b8417f..720def2 100644 --- a/src/lib/http.c +++ b/src/lib/http.c @@ -1,8 +1,3 @@ -/* - * "This product includes software developed by the OpenSSL Project - * * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" - */ - #include #include #include @@ -16,10 +11,6 @@ #include #include "http.h" -struct tuser_data { - char *first_message; -}; - static char *rand_string(char *str, size_t size) { const char charset[] = "1234567890ABCDEF"; if (size) { @@ -83,12 +74,6 @@ void log_request_debug(const struct mg_request_info *request_info, request_id, request_info->content_length); uts_logger(context, LOG_DEBUG, "Request[%s], remote_port: %d", request_id, request_info->remote_port); - // uts_logger(context, LOG_DEBUG, "Request[%d], user_data: %s", - // request_id, - // request_info->user_data); - // uts_logger(context, LOG_DEBUG, "Request[%d], conn_data: %s", - // request_id, - // request_info->conn_data); } void log_request(const struct mg_request_info *request_info, char *request_id, diff --git a/src/lib/rfc3161.c b/src/lib/rfc3161.c index 8023a63..fca94c9 100644 --- a/src/lib/rfc3161.c +++ b/src/lib/rfc3161.c @@ -23,28 +23,6 @@ #include #include "utils.h" -#define OID_SECTION "oids" - -// number of char we get to log for the serial -#define SERIAL_ID_SIZE 8 - -/* Reply related functions. */ -static int reply_command(CONF *conf, char *section, char *engine, char *query, - char *passin, char *inkey, const EVP_MD *md, - char *signer, char *chain, const char *policy, - char *in, int token_in, char *out, int token_out, - int text); -static ASN1_INTEGER *serial_cb(TS_RESP_CTX *ctx, void *data); - -#define B_FORMAT_TEXT 0x8000 -#define FORMAT_UNDEF 0 -#define FORMAT_TEXT (1 | B_FORMAT_TEXT) /* Generic text */ -#define FORMAT_ASN1 4 /* ASN.1/DER */ - -/* - * Reply-related method definitions. - */ - int add_oid_section(rfc3161_context *ct, CONF *conf) { char *p; STACK_OF(CONF_VALUE) * sktmp; @@ -113,6 +91,27 @@ ts_resp_ctx_wrapper *get_ctxw(rfc3161_context *ct) { return ret; } +// Build a random serial for each request. +// It's less painful to manage than an incremental serial stored in a file +// and a 150 bits size is more than enough to prevent collision. +static ASN1_INTEGER *serial_cb(TS_RESP_CTX *ctx, void *data42) { + unsigned char data[20] = {0}; + RAND_bytes(data, sizeof(data)); + // data[0] &= 0x7F; + + // build big number from our bytes + BIGNUM *bn = BN_new(); + BN_bin2bn(data, sizeof(data), bn); + + // build the ASN1_INTEGER from our BIGNUM + ASN1_INTEGER *asnInt = ASN1_INTEGER_new(); + BN_to_ASN1_INTEGER(bn, asnInt); + + // cleanup + BN_free(bn); + return asnInt; +} + // create a TS_RESP_CTX (OpenSSL Time-Stamp Response Context) TS_RESP_CTX *create_tsctx(rfc3161_context *ct, CONF *conf, const char *section, const char *policy) { @@ -353,24 +352,3 @@ end: TS_RESP_free(ts_response); return ret; } - -// Build a random serial for each request. -// It's less painful to manage than an incremental serial stored in a file -// and a 150 bits size is more than enough to prevent collision. -static ASN1_INTEGER *serial_cb(TS_RESP_CTX *ctx, void *data42) { - unsigned char data[20] = {0}; - RAND_bytes(data, sizeof(data)); - // data[0] &= 0x7F; - - // build big number from our bytes - BIGNUM *bn = BN_new(); - BN_bin2bn(data, sizeof(data), bn); - - // build the ASN1_INTEGER from our BIGNUM - ASN1_INTEGER *asnInt = ASN1_INTEGER_new(); - BN_to_ASN1_INTEGER(bn, asnInt); - - // cleanup - BN_free(bn); - return asnInt; -} diff --git a/src/lib/utils.c b/src/lib/utils.c index 3818a93..fc93c3f 100644 --- a/src/lib/utils.c +++ b/src/lib/utils.c @@ -14,10 +14,13 @@ #include #include "utils.h" -typedef struct _code { - char *c_name; - int c_val; -} CODE; +static void signal_handler_general(int sig_num) { + g_uts_sig = sig_num; +} + +static void signal_handler_up(int sig_num) { + g_uts_sig_up = sig_num; +} CODE prioritynames[] = {{"alert", LOG_ALERT}, {"crit", LOG_CRIT}, @@ -32,14 +35,6 @@ CODE prioritynames[] = {{"alert", LOG_ALERT}, {"warning", LOG_WARNING}, {NULL, -1}}; -static void signal_handler_general(int sig_num) { - g_uts_sig = sig_num; -} - -static void signal_handler_up(int sig_num) { - g_uts_sig_up = sig_num; -} - int init_pid(char *pidfile_path) { // if pidfile_path is null, the user did not request one // exit success @@ -381,6 +376,7 @@ end: return 0; } +// free the rfc3161_context structure void free_uts_context(rfc3161_context *ct) { for (int i = 0; i < ct->numthreads; i++) { TS_RESP_CTX_free(ct->ts_ctx_pool[i].ts_ctx);