diff --git a/src/dyn_crypto.c b/src/dyn_crypto.c index 745c0bd31..6aba54a44 100644 --- a/src/dyn_crypto.c +++ b/src/dyn_crypto.c @@ -388,12 +388,13 @@ dyn_aes_decrypt(unsigned char *enc_msg, size_t enc_msg_len, struct mbuf *mbuf, u * */ rstatus_t -dyn_aes_encrypt_msg(struct msg *msg, unsigned char *arg_aes_key) +dyn_aes_encrypt_msg(struct msg *msg, unsigned char *arg_aes_key, size_t* outlen) { struct mhdr mhdr_tem; int count = 0; if (STAILQ_EMPTY(&msg->mhdr)) { + // 'msg' is empty. Nothing to encrypt. return DN_ERROR; } @@ -409,8 +410,9 @@ dyn_aes_encrypt_msg(struct msg *msg, unsigned char *arg_aes_key) struct mbuf *nbuf = mbuf_get(); if (nbuf == NULL) { + // Unable to obtain an 'mbuf'. mbuf_put(mbuf); - return DN_ERROR; + return DN_ENOMEM; } int n = dyn_aes_encrypt(mbuf->pos, mbuf_length(mbuf), nbuf, arg_aes_key); @@ -438,7 +440,8 @@ dyn_aes_encrypt_msg(struct msg *msg, unsigned char *arg_aes_key) } } - return count; + *outlen = count; + return DN_OK; } diff --git a/src/dyn_crypto.h b/src/dyn_crypto.h index ac58c247d..a0c7c9e65 100644 --- a/src/dyn_crypto.h +++ b/src/dyn_crypto.h @@ -41,7 +41,7 @@ rstatus_t dyn_aes_encrypt(const unsigned char *msg, size_t msgLen, rstatus_t dyn_aes_decrypt(unsigned char *encMsg, size_t encMsgLen, struct mbuf *mbuf, unsigned char *aes_key); -rstatus_t dyn_aes_encrypt_msg(struct msg *msg, unsigned char *aes_key); +rstatus_t dyn_aes_encrypt_msg(struct msg *msg, unsigned char *aes_key, size_t* outlen); unsigned char* generate_aes_key(void); int dyn_rsa_size(void); diff --git a/src/dyn_dnode_client.c b/src/dyn_dnode_client.c index b437065f3..1d5a51fd0 100644 --- a/src/dyn_dnode_client.c +++ b/src/dyn_dnode_client.c @@ -454,16 +454,21 @@ dnode_rsp_send_next(struct context *ctx, struct conn *conn) } if (ENCRYPTION) { - status = dyn_aes_encrypt_msg(rsp, conn->aes_key); - if (status == DN_ERROR) { - loga("OOM to obtain an mbuf for encryption!"); - mbuf_put(header_buf); - rsp_put(rsp); - return NULL; + size_t encrypted_bytes; + status = dyn_aes_encrypt_msg(rsp, conn->aes_key, &encrypted_bytes); + if (status != DN_OK) { + if (status == DN_ENOMEM) { + loga("OOM to obtain an mbuf for encryption!"); + } else if (status == DN_ERROR) { + loga("Encryption failed: Empty message"); + } + mbuf_put(header_buf); + rsp_put(rsp); + return NULL; } if (log_loggable(LOG_VVERB)) { - log_debug(LOG_VERB, "#encrypted bytes : %d", status); + log_debug(LOG_VERB, "#encrypted bytes : %d", encrypted_bytes); } dmsg_write(header_buf, msg_id, msg_type, conn, msg_length(rsp)); diff --git a/src/dyn_dnode_request.c b/src/dyn_dnode_request.c index 8148fa36c..9a4cd1234 100644 --- a/src/dyn_dnode_request.c +++ b/src/dyn_dnode_request.c @@ -102,15 +102,20 @@ dnode_peer_req_forward(struct context *ctx, struct conn *c_conn, //write dnode header if (ENCRYPTION) { - status = dyn_aes_encrypt_msg(req, p_conn->aes_key); - if (status == DN_ERROR) { - loga("OOM to obtain an mbuf for encryption!"); + size_t encrypted_bytes; + status = dyn_aes_encrypt_msg(req, p_conn->aes_key, &encrypted_bytes); + if (status != DN_OK) { + if (status == DN_ENOMEM) { + loga("OOM to obtain an mbuf for encryption!"); + } else if (status == DN_ERROR) { + loga("Encryption failed: Empty message"); + } + *dyn_error_code = status; mbuf_put(header_buf); - *dyn_error_code = DN_ENOMEM; return status; } - log_debug(LOG_VVERB, "#encrypted bytes : %d", status); + log_debug(LOG_VVERB, "#encrypted bytes : %d", encrypted_bytes); dmsg_write(header_buf, req->id, msg_type, p_conn, msg_length(req)); } else {