mirror of
https://gitlab.kit.edu/kit/scc/sys/mail/exim-encrypt-dlfunc.git
synced 2025-12-06 07:23:56 +01:00
89 lines
2.5 KiB
C
89 lines
2.5 KiB
C
#define _GNU_SOURCE
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <stdbool.h>
|
|
#include <sodium.h>
|
|
#include "common.h"
|
|
|
|
//#define MIN_KEY_SIZE (crypto_box_SECRETKEYBYTES < crypto_box_PUBLICKEYBYTES ? crypto_box_SECRETKEYBYTES : crypto_box_PUBLICKEYBYTES)
|
|
//#define MAX_KEY_SIZE (crypto_box_SECRETKEYBYTES > crypto_box_PUBLICKEYBYTES ? crypto_box_SECRETKEYBYTES : crypto_box_PUBLICKEYBYTES)
|
|
|
|
char *read_first_line(const char *filename) {
|
|
FILE *stream;
|
|
char *cipherstring;
|
|
size_t len = 0;
|
|
ssize_t nread;
|
|
bool input_is_stdin = false;
|
|
|
|
// open file (use stdin for '-')
|
|
if (!strncmp(filename, "-", 1)) {
|
|
stream = stdin;
|
|
input_is_stdin = true;
|
|
} else {
|
|
stream = fopen(filename, "r");
|
|
if (stream == NULL) {
|
|
fprintf(stderr, "[ERROR] Error opening file %s\n\n", filename);
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
}
|
|
|
|
nread = getline(&cipherstring, &len, stream);
|
|
|
|
if (nread == -1 && !feof(stream)) {
|
|
perror("getline: ");
|
|
}
|
|
if (input_is_stdin == false) {
|
|
fclose(stream);
|
|
}
|
|
|
|
// remove trailing newline
|
|
cipherstring[strcspn(cipherstring, "\r\n")] = 0;
|
|
|
|
return cipherstring;
|
|
}
|
|
|
|
char *read_password_file(const char *filename, size_t keysize, size_t *length) {
|
|
FILE *stream;
|
|
char *contents;
|
|
ssize_t nread;
|
|
bool input_is_stdin = false;
|
|
|
|
contents = malloc(keysize + 1);
|
|
sodium_memzero(contents, keysize + 1);
|
|
|
|
// open file (use stdin for '-')
|
|
if (!strncmp(filename, "-", 1)) {
|
|
stream = stdin;
|
|
input_is_stdin = true;
|
|
} else {
|
|
stream = fopen(filename, "r");
|
|
if (stream == NULL) {
|
|
fprintf(stderr, "[ERROR] Error opening file %s\n\n", filename);
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
}
|
|
|
|
nread = fread(contents, sizeof(char), keysize, stream);
|
|
|
|
if (nread < 0) {
|
|
fprintf(stderr, "[ERROR] reading from %s failed\n\n", filename);
|
|
exit(EXIT_FAILURE);
|
|
} else {
|
|
*length = (size_t) nread;
|
|
}
|
|
|
|
if (input_is_stdin == false) {
|
|
fclose(stream);
|
|
}
|
|
return contents;
|
|
}
|
|
|
|
int base64_decode_string(const char *input, unsigned char **outstring, size_t *outlen) {
|
|
size_t input_len = strlen(input);
|
|
size_t outmaxlen = input_len / 4 * 3;
|
|
*outstring = malloc(outmaxlen * sizeof(unsigned char));
|
|
return sodium_base642bin(*outstring, outmaxlen, (const char *) input, input_len,
|
|
NULL, outlen, NULL, sodium_base64_VARIANT_ORIGINAL);
|
|
} |