Removed debugging statements.

This commit is contained in:
Heiko Reese
2021-08-20 01:04:19 +02:00
parent 4ff77be04a
commit d8b209ba33
3 changed files with 34 additions and 25 deletions

26
src/debug_helpers.c Normal file
View File

@ -0,0 +1,26 @@
/*
* Convert a string to its hexadecimal representation.
*
* Use like this:
* log_write(0, LOG_MAIN, "DEBUG: %s", string2hex(var, var_len));
*/
char * string2hex(unsigned char * input, size_t length) {
const int growth = 3;
char * outstring = store_get(growth*length+1);
memset(outstring, 0, 3*length+1);
for (int i =0; i<length; i++) {
sprintf(outstring+i*growth, "%02x ", input[i]);
}
return outstring;
}
/*
* How to debug this library:
*
* 1. Add this code to the first “breakpoint”:
* log_write(0, LOG_MAIN, "pid: %d", getpid()); int busywait = 0; while (busywait == 0) {}
* 2. Compile.
* 3. Run “exim -be […]” to call the lib; see simple_exim_test.sh for details.
* 4. Read exim pid from log output. Attach to the looping exim process with “gdb -p PID”
* 5. Prepare breakpoints, watches, etc. Set busywait to 1 and continue.
*/

8
src/debug_helpers.h Normal file
View File

@ -0,0 +1,8 @@
//
// Created by sprawl on 20/08/2021.
//
#ifndef EXIM_ENCRYPT_DLFUNC_DEBUG_HELPERS_H
#define EXIM_ENCRYPT_DLFUNC_DEBUG_HELPERS_H
#endif //EXIM_ENCRYPT_DLFUNC_DEBUG_HELPERS_H

View File

@ -9,16 +9,6 @@
/* Exim4 dlfunc API header */
#include <local_scan.h>
char * string2hex(unsigned char * input, size_t length) {
const int growth = 3;
char * outstring = store_get(growth*length+1);
memset(outstring, 0, 3*length+1);
for (int i =0; i<length; i++) {
sprintf(outstring+i*growth, "%02x ", input[i]);
}
return outstring;
}
/*
* Encrypt message using crypto_secretbox_easy().
*
@ -194,14 +184,10 @@ int sodium_crypto_box_seal(uschar **yield, int argc, uschar *argv[]) {
return ERROR;
}
log_write(0, LOG_MAIN, "[encrypt] PK: %s", string2hex(pk, pk_buffer_len));
// get cleartext message
unsigned char *message = argv[1];
size_t messagelen = strlen((const char *) message);
log_write(0, LOG_MAIN, "[encrypt] cleartext: %s", message);
// prepare buffer for ciphertext
unsigned int cipherlen = messagelen + crypto_box_SEALBYTES;
unsigned char *ciphertext = store_get(cipherlen);
@ -213,8 +199,6 @@ int sodium_crypto_box_seal(uschar **yield, int argc, uschar *argv[]) {
return ERROR;
}
log_write(0, LOG_MAIN, "[encrypt] ciphertext: %s", string2hex(ciphertext, cipherlen));
// base64-encode the ciphertext
unsigned int outputsize = sodium_base64_ENCODED_LEN(cipherlen,
sodium_base64_VARIANT_ORIGINAL);
@ -262,7 +246,6 @@ int sodium_crypto_box_seal_open(uschar **yield, int argc, uschar *argv[]) {
*yield = string_copy((unsigned char *) "Error decoding private key");
return ERROR;
}
log_write(0, LOG_MAIN, "[decrypt] SK: %s", string2hex(sk, sk_buffer_len));
// get and convert public key
unsigned char *pkb64 = argv[1];
@ -280,8 +263,6 @@ int sodium_crypto_box_seal_open(uschar **yield, int argc, uschar *argv[]) {
return ERROR;
}
log_write(0, LOG_MAIN, "[decrypt] PK: %s", string2hex(pk, pk_buffer_len));
// get encrypted message
unsigned char *ciphertextb64 = argv[2];
size_t ciphertextb64_len = strlen((const char *) ciphertextb64);
@ -300,18 +281,12 @@ int sodium_crypto_box_seal_open(uschar **yield, int argc, uschar *argv[]) {
return ERROR;
}
log_write(0, LOG_MAIN, "[encrypt] ciphertext: %s", string2hex(ciphertext, ciphertextlen));
// prepare buffer for cleartext
unsigned int cleartextlen = ciphertextlen - crypto_box_SEALBYTES;
unsigned char *cleartext = (unsigned char *) store_get(cleartextlen + 1);
sodium_memzero(cleartext, cleartextlen + 1);
// decrypt message
//#define DEBUG
#ifdef DEBUG
log_write(0, LOG_MAIN, "pid: %d", getpid()); int busywait = 0; while (busywait == 0) {}
#endif
if (crypto_box_seal_open(cleartext, ciphertext, ciphertextlen, pk, sk) != 0) {
*yield = string_copy((unsigned char *) "Decryption error after crypto_box_seal_open()");
return ERROR;