summaryrefslogtreecommitdiffstats
path: root/apps/dgst.c
diff options
context:
space:
mode:
authorPauli <paul.dale@oracle.com>2019-03-30 02:22:51 +0100
committerPauli <paul.dale@oracle.com>2019-03-30 02:22:51 +0100
commitf3448f5481a8d1f6fbf5fd05caaca229af0b87f7 (patch)
treefc295589ac061c441f79b83909261194aacf3141 /apps/dgst.c
parentFix a memleak in apps/verify (diff)
downloadopenssl-f3448f5481a8d1f6fbf5fd05caaca229af0b87f7.tar.xz
openssl-f3448f5481a8d1f6fbf5fd05caaca229af0b87f7.zip
issue-8493: Fix for filenames with newlines using openssl dgst
The output format now matches coreutils *dgst tools. [ edited to remove trailing white space ] Reviewed-by: Richard Levitte <levitte@openssl.org> Reviewed-by: Paul Dale <paul.dale@oracle.com> (Merged from https://github.com/openssl/openssl/pull/8578)
Diffstat (limited to 'apps/dgst.c')
-rw-r--r--apps/dgst.c48
1 files changed, 47 insertions, 1 deletions
diff --git a/apps/dgst.c b/apps/dgst.c
index e38496ee0a..7b43338586 100644
--- a/apps/dgst.c
+++ b/apps/dgst.c
@@ -414,13 +414,52 @@ int dgst_main(int argc, char **argv)
return ret;
}
+/*
+ * The newline_escape_filename function performs newline escaping for any
+ * filename that contains a newline. This function also takes a pointer
+ * to backslash. The backslash pointer is a flag to indicating whether a newline
+ * is present in the filename. If a newline is present, the backslash flag is
+ * set and the output format will contain a backslash at the beginning of the
+ * digest output. This output format is to replicate the output format found
+ * in the '*sum' checksum programs. This aims to preserve backward
+ * compatibility.
+ */
+static const char *newline_escape_filename(const char *file, int * backslash)
+{
+ size_t i, e = 0, length = strlen(file), newline_count = 0, mem_len = 0;
+ char *file_cpy = NULL;
+
+ for (i = 0; i < length; i++)
+ if (file[i] == '\n')
+ newline_count++;
+
+ mem_len = length + newline_count + 1;
+ file_cpy = app_malloc(mem_len, file);
+ i = 0;
+
+ while(e < length) {
+ const char c = file[e];
+ if (c == '\n') {
+ file_cpy[i++] = '\\';
+ file_cpy[i++] = 'n';
+ *backslash = 1;
+ } else {
+ file_cpy[i++] = c;
+ }
+ e++;
+ }
+ file_cpy[i] = '\0';
+ return (const char*)file_cpy;
+}
+
+
int do_fp(BIO *out, unsigned char *buf, BIO *bp, int sep, int binout,
EVP_PKEY *key, unsigned char *sigin, int siglen,
const char *sig_name, const char *md_name,
const char *file)
{
size_t len;
- int i;
+ int i, backslash = 0;
for (;;) {
i = BIO_read(bp, (char *)buf, BUFSIZE);
@@ -468,9 +507,16 @@ int do_fp(BIO *out, unsigned char *buf, BIO *bp, int sep, int binout,
if (binout) {
BIO_write(out, buf, len);
} else if (sep == 2) {
+ file = newline_escape_filename(file, &backslash);
+
+ if (backslash == 1)
+ BIO_puts(out, "\\");
+
for (i = 0; i < (int)len; i++)
BIO_printf(out, "%02x", buf[i]);
+
BIO_printf(out, " *%s\n", file);
+ OPENSSL_free((char *)file);
} else {
if (sig_name != NULL) {
BIO_puts(out, sig_name);