1 /*
2  * Copyright 2017-2020 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9 
10 #include "../testutil.h"
11 #include "output.h"
12 #include "tu_local.h"
13 
14 #include <string.h>
15 #include <ctype.h>
16 #include "internal/nelem.h"
17 
18 /* The size of memory buffers to display on failure */
19 #define MEM_BUFFER_SIZE     (2000)
20 #define MAX_STRING_WIDTH    (80)
21 #define BN_OUTPUT_SIZE      (8)
22 
23 /* Output a diff header */
test_diff_header(const char * left,const char * right)24 static void test_diff_header(const char *left, const char *right)
25 {
26     test_printf_stderr("--- %s\n", left);
27     test_printf_stderr("+++ %s\n", right);
28 }
29 
30 /* Formatted string output routines */
test_string_null_empty(const char * m,char c)31 static void test_string_null_empty(const char *m, char c)
32 {
33     if (m == NULL)
34         test_printf_stderr("%4s %c NULL\n", "", c);
35     else
36         test_printf_stderr("%4u:%c ''\n", 0u, c);
37 }
38 
test_fail_string_common(const char * prefix,const char * file,int line,const char * type,const char * left,const char * right,const char * op,const char * m1,size_t l1,const char * m2,size_t l2)39 static void test_fail_string_common(const char *prefix, const char *file,
40                                     int line, const char *type,
41                                     const char *left, const char *right,
42                                     const char *op, const char *m1, size_t l1,
43                                     const char *m2, size_t l2)
44 {
45     const size_t width =
46         (MAX_STRING_WIDTH - BIO_get_indent(bio_err) - 12) / 16 * 16;
47     char b1[MAX_STRING_WIDTH + 1], b2[MAX_STRING_WIDTH + 1];
48     char bdiff[MAX_STRING_WIDTH + 1];
49     size_t n1, n2, i;
50     unsigned int cnt = 0, diff;
51 
52     test_fail_message_prefix(prefix, file, line, type, left, right, op);
53     if (m1 == NULL)
54         l1 = 0;
55     if (m2 == NULL)
56         l2 = 0;
57     if (l1 == 0 && l2 == 0) {
58         if ((m1 == NULL) == (m2 == NULL)) {
59             test_string_null_empty(m1, ' ');
60         } else {
61             test_diff_header(left, right);
62             test_string_null_empty(m1, '-');
63             test_string_null_empty(m2, '+');
64         }
65         goto fin;
66     }
67 
68     if (l1 != l2 || strncmp(m1, m2, l1) != 0)
69         test_diff_header(left, right);
70 
71     while (l1 > 0 || l2 > 0) {
72         n1 = n2 = 0;
73         if (l1 > 0) {
74             b1[n1 = l1 > width ? width : l1] = 0;
75             for (i = 0; i < n1; i++)
76                 b1[i] = isprint((unsigned char)m1[i]) ? m1[i] : '.';
77         }
78         if (l2 > 0) {
79             b2[n2 = l2 > width ? width : l2] = 0;
80             for (i = 0; i < n2; i++)
81                 b2[i] = isprint((unsigned char)m2[i]) ? m2[i] : '.';
82         }
83         diff = 0;
84         i = 0;
85         if (n1 > 0 && n2 > 0) {
86             const size_t j = n1 < n2 ? n1 : n2;
87 
88             for (; i < j; i++)
89                 if (m1[i] == m2[i]) {
90                     bdiff[i] = ' ';
91                 } else {
92                     bdiff[i] = '^';
93                     diff = 1;
94                 }
95             bdiff[i] = '\0';
96         }
97         if (n1 == n2 && !diff) {
98             test_printf_stderr("%4u:  '%s'\n", cnt, n2 > n1 ? b2 : b1);
99         } else {
100             if (cnt == 0 && (m1 == NULL || *m1 == '\0'))
101                 test_string_null_empty(m1, '-');
102             else if (n1 > 0)
103                 test_printf_stderr("%4u:- '%s'\n", cnt, b1);
104             if (cnt == 0 && (m2 == NULL || *m2 == '\0'))
105                test_string_null_empty(m2, '+');
106             else if (n2 > 0)
107                 test_printf_stderr("%4u:+ '%s'\n", cnt, b2);
108             if (diff && i > 0)
109                 test_printf_stderr("%4s    %s\n", "", bdiff);
110         }
111         if (m1 != NULL)
112             m1 += n1;
113         if (m2 != NULL)
114             m2 += n2;
115         l1 -= n1;
116         l2 -= n2;
117         cnt += width;
118     }
119 fin:
120     test_flush_stderr();
121 }
122 
123 /*
124  * Wrapper routines so that the underlying code can be shared.
125  * The first is the call from inside the test utilities when a conditional
126  * fails.  The second is the user's call to dump a string.
127  */
test_fail_string_message(const char * prefix,const char * file,int line,const char * type,const char * left,const char * right,const char * op,const char * m1,size_t l1,const char * m2,size_t l2)128 void test_fail_string_message(const char *prefix, const char *file,
129                               int line, const char *type,
130                               const char *left, const char *right,
131                               const char *op, const char *m1, size_t l1,
132                               const char *m2, size_t l2)
133 {
134     test_fail_string_common(prefix, file, line, type, left, right, op,
135                             m1, l1, m2, l2);
136     test_printf_stderr("\n");
137 }
138 
test_output_string(const char * name,const char * m,size_t l)139 void test_output_string(const char *name, const char *m, size_t l)
140 {
141     test_fail_string_common("string", NULL, 0, NULL, NULL, NULL, name,
142                             m, l, m, l);
143 }
144 
145 /* BIGNUM formatted output routines */
146 
147 /*
148  * A basic memory byte to hex digit converter with allowance for spacing
149  * every so often.
150  */
hex_convert_memory(const unsigned char * m,size_t n,char * b,size_t width)151 static void hex_convert_memory(const unsigned char *m, size_t n, char *b,
152                                size_t width)
153 {
154     size_t i;
155 
156     for (i = 0; i < n; i++) {
157         const unsigned char c = *m++;
158 
159         *b++ = "0123456789abcdef"[c >> 4];
160         *b++ = "0123456789abcdef"[c & 15];
161         if (i % width == width - 1 && i != n - 1)
162             *b++ = ' ';
163     }
164     *b = '\0';
165 }
166 
167 /*
168  * Constants to define the number of bytes to display per line and the number
169  * of characters these take.
170  */
171 static const int bn_bytes = (MAX_STRING_WIDTH - 9) / (BN_OUTPUT_SIZE * 2 + 1)
172                             * BN_OUTPUT_SIZE;
173 static const int bn_chars = (MAX_STRING_WIDTH - 9) / (BN_OUTPUT_SIZE * 2 + 1)
174                             * (BN_OUTPUT_SIZE * 2 + 1) - 1;
175 
176 /*
177  * Output the header line for the bignum
178  */
test_bignum_header_line(void)179 static void test_bignum_header_line(void)
180 {
181     test_printf_stderr(" %*s\n", bn_chars + 6, "bit position");
182 }
183 
test_bignum_zero_null(const BIGNUM * bn)184 static const char *test_bignum_zero_null(const BIGNUM *bn)
185 {
186     if (bn != NULL)
187         return BN_is_negative(bn) ? "-0" : "0";
188     return "NULL";
189 }
190 
191 /*
192  * Print a bignum zero taking care to include the correct sign.
193  * This routine correctly deals with a NULL bignum pointer as input.
194  */
test_bignum_zero_print(const BIGNUM * bn,char sep)195 static void test_bignum_zero_print(const BIGNUM *bn, char sep)
196 {
197     const char *v = test_bignum_zero_null(bn);
198     const char *suf = bn != NULL ? ":    0" : "";
199 
200     test_printf_stderr("%c%*s%s\n", sep, bn_chars, v, suf);
201 }
202 
203 /*
204  * Convert a section of memory from inside a bignum into a displayable
205  * string with appropriate visual aid spaces inserted.
206  */
convert_bn_memory(const unsigned char * in,size_t bytes,char * out,int * lz,const BIGNUM * bn)207 static int convert_bn_memory(const unsigned char *in, size_t bytes,
208                              char *out, int *lz, const BIGNUM *bn)
209 {
210     int n = bytes * 2, i;
211     char *p = out, *q = NULL;
212     const char *r;
213 
214     if (bn != NULL && !BN_is_zero(bn)) {
215         hex_convert_memory(in, bytes, out, BN_OUTPUT_SIZE);
216         if (*lz) {
217             for (; *p == '0' || *p == ' '; p++)
218                 if (*p == '0') {
219                     q = p;
220                     *p = ' ';
221                     n--;
222                 }
223             if (*p == '\0') {
224                 /*
225                  * in[bytes] is defined because we're converting a non-zero
226                  * number and we've not seen a non-zero yet.
227                  */
228                 if ((in[bytes] & 0xf0) != 0 && BN_is_negative(bn)) {
229                     *lz = 0;
230                     *q = '-';
231                     n++;
232                 }
233             } else {
234                 *lz = 0;
235                 if (BN_is_negative(bn)) {
236                     /*
237                      * This is valid because we always convert more digits than
238                      * the number holds.
239                      */
240                     *q = '-';
241                     n++;
242                 }
243             }
244         }
245        return n;
246     }
247 
248     for (i = 0; i < n; i++) {
249         *p++ = ' ';
250         if (i % (2 * BN_OUTPUT_SIZE) == 2 * BN_OUTPUT_SIZE - 1 && i != n - 1)
251             *p++ = ' ';
252     }
253     *p = '\0';
254     if (bn == NULL)
255         r = "NULL";
256     else
257         r = BN_is_negative(bn) ? "-0" : "0";
258     strcpy(p - strlen(r), r);
259     return 0;
260 }
261 
262 /*
263  * Common code to display either one or two bignums, including the diff
264  * pointers for changes (only when there are two).
265  */
test_fail_bignum_common(const char * prefix,const char * file,int line,const char * type,const char * left,const char * right,const char * op,const BIGNUM * bn1,const BIGNUM * bn2)266 static void test_fail_bignum_common(const char *prefix, const char *file,
267                                     int line, const char *type,
268                                     const char *left, const char *right,
269                                     const char *op,
270                                     const BIGNUM *bn1, const BIGNUM *bn2)
271 {
272     const size_t bytes = bn_bytes;
273     char b1[MAX_STRING_WIDTH + 1], b2[MAX_STRING_WIDTH + 1];
274     char *p, bdiff[MAX_STRING_WIDTH + 1];
275     size_t l1, l2, n1, n2, i, len;
276     unsigned int cnt, diff, real_diff;
277     unsigned char *m1 = NULL, *m2 = NULL;
278     int lz1 = 1, lz2 = 1;
279     unsigned char buffer[MEM_BUFFER_SIZE * 2], *bufp = buffer;
280 
281     test_fail_message_prefix(prefix, file, line, type, left, right, op);
282     l1 = bn1 == NULL ? 0 : (BN_num_bytes(bn1) + (BN_is_negative(bn1) ? 1 : 0));
283     l2 = bn2 == NULL ? 0 : (BN_num_bytes(bn2) + (BN_is_negative(bn2) ? 1 : 0));
284     if (l1 == 0 && l2 == 0) {
285         if ((bn1 == NULL) == (bn2 == NULL)) {
286             test_bignum_header_line();
287             test_bignum_zero_print(bn1, ' ');
288         } else {
289             test_diff_header(left, right);
290             test_bignum_header_line();
291             test_bignum_zero_print(bn1, '-');
292             test_bignum_zero_print(bn2, '+');
293         }
294         goto fin;
295     }
296 
297     if (l1 != l2 || bn1 == NULL || bn2 == NULL || BN_cmp(bn1, bn2) != 0)
298         test_diff_header(left, right);
299     test_bignum_header_line();
300 
301     len = ((l1 > l2 ? l1 : l2) + bytes - 1) / bytes * bytes;
302 
303     if (len > MEM_BUFFER_SIZE && (bufp = OPENSSL_malloc(len * 2)) == NULL) {
304         bufp = buffer;
305         len = MEM_BUFFER_SIZE;
306         test_printf_stderr("WARNING: these BIGNUMs have been truncated\n");
307     }
308 
309     if (bn1 != NULL) {
310         m1 = bufp;
311         BN_bn2binpad(bn1, m1, len);
312     }
313     if (bn2 != NULL) {
314         m2 = bufp + len;
315         BN_bn2binpad(bn2, m2, len);
316     }
317 
318     while (len > 0) {
319         cnt = 8 * (len - bytes);
320         n1 = convert_bn_memory(m1, bytes, b1, &lz1, bn1);
321         n2 = convert_bn_memory(m2, bytes, b2, &lz2, bn2);
322 
323         diff = real_diff = 0;
324         i = 0;
325         p = bdiff;
326         for (i=0; b1[i] != '\0'; i++)
327             if (b1[i] == b2[i] || b1[i] == ' ' || b2[i] == ' ') {
328                 *p++ = ' ';
329                 diff |= b1[i] != b2[i];
330             } else {
331                 *p++ = '^';
332                 real_diff = diff = 1;
333             }
334         *p++ = '\0';
335         if (!diff) {
336             test_printf_stderr(" %s:% 5d\n", n2 > n1 ? b2 : b1, cnt);
337         } else {
338             if (cnt == 0 && bn1 == NULL)
339                 test_printf_stderr("-%s\n", b1);
340             else if (cnt == 0 || n1 > 0)
341                 test_printf_stderr("-%s:% 5d\n", b1, cnt);
342             if (cnt == 0 && bn2 == NULL)
343                 test_printf_stderr("+%s\n", b2);
344             else if (cnt == 0 || n2 > 0)
345                 test_printf_stderr("+%s:% 5d\n", b2, cnt);
346             if (real_diff && (cnt == 0 || (n1 > 0 && n2 > 0))
347                     && bn1 != NULL && bn2 != NULL)
348                 test_printf_stderr(" %s\n", bdiff);
349         }
350         if (m1 != NULL)
351             m1 += bytes;
352         if (m2 != NULL)
353             m2 += bytes;
354         len -= bytes;
355     }
356 fin:
357     test_flush_stderr();
358     if (bufp != buffer)
359         OPENSSL_free(bufp);
360 }
361 
362 /*
363  * Wrapper routines so that the underlying code can be shared.
364  * The first two are calls from inside the test utilities when a conditional
365  * fails.  The third is the user's call to dump a bignum.
366  */
test_fail_bignum_message(const char * prefix,const char * file,int line,const char * type,const char * left,const char * right,const char * op,const BIGNUM * bn1,const BIGNUM * bn2)367 void test_fail_bignum_message(const char *prefix, const char *file,
368                               int line, const char *type,
369                               const char *left, const char *right,
370                               const char *op,
371                               const BIGNUM *bn1, const BIGNUM *bn2)
372 {
373     test_fail_bignum_common(prefix, file, line, type, left, right, op, bn1, bn2);
374     test_printf_stderr("\n");
375 }
376 
test_fail_bignum_mono_message(const char * prefix,const char * file,int line,const char * type,const char * left,const char * right,const char * op,const BIGNUM * bn)377 void test_fail_bignum_mono_message(const char *prefix, const char *file,
378                                    int line, const char *type,
379                                    const char *left, const char *right,
380                                    const char *op, const BIGNUM *bn)
381 {
382     test_fail_bignum_common(prefix, file, line, type, left, right, op, bn, bn);
383     test_printf_stderr("\n");
384 }
385 
test_output_bignum(const char * name,const BIGNUM * bn)386 void test_output_bignum(const char *name, const BIGNUM *bn)
387 {
388     if (bn == NULL || BN_is_zero(bn)) {
389         test_printf_stderr("bignum: '%s' = %s\n", name,
390                            test_bignum_zero_null(bn));
391     } else if (BN_num_bytes(bn) <= BN_OUTPUT_SIZE) {
392         unsigned char buf[BN_OUTPUT_SIZE];
393         char out[2 * sizeof(buf) + 1];
394         char *p = out;
395         int n = BN_bn2bin(bn, buf);
396 
397         hex_convert_memory(buf, n, p, BN_OUTPUT_SIZE);
398         while (*p == '0' && *++p != '\0')
399             ;
400         test_printf_stderr("bignum: '%s' = %s0x%s\n", name,
401                            BN_is_negative(bn) ? "-" : "", p);
402     } else {
403         test_fail_bignum_common("bignum", NULL, 0, NULL, NULL, NULL, name,
404                                 bn, bn);
405     }
406 }
407 
408 /* Memory output routines */
409 
410 /*
411  * Handle zero length blocks of memory or NULL pointers to memory
412  */
test_memory_null_empty(const unsigned char * m,char c)413 static void test_memory_null_empty(const unsigned char *m, char c)
414 {
415     if (m == NULL)
416         test_printf_stderr("%4s %c%s\n", "", c, "NULL");
417     else
418         test_printf_stderr("%04x %c%s\n", 0u, c, "empty");
419 }
420 
421 /*
422  * Common code to display one or two blocks of memory.
423  */
test_fail_memory_common(const char * prefix,const char * file,int line,const char * type,const char * left,const char * right,const char * op,const unsigned char * m1,size_t l1,const unsigned char * m2,size_t l2)424 static void test_fail_memory_common(const char *prefix, const char *file,
425                                     int line, const char *type,
426                                     const char *left, const char *right,
427                                     const char *op,
428                                     const unsigned char *m1, size_t l1,
429                                     const unsigned char *m2, size_t l2)
430 {
431     const size_t bytes = (MAX_STRING_WIDTH - 9) / 17 * 8;
432     char b1[MAX_STRING_WIDTH + 1], b2[MAX_STRING_WIDTH + 1];
433     char *p, bdiff[MAX_STRING_WIDTH + 1];
434     size_t n1, n2, i;
435     unsigned int cnt = 0, diff;
436 
437     test_fail_message_prefix(prefix, file, line, type, left, right, op);
438     if (m1 == NULL)
439         l1 = 0;
440     if (m2 == NULL)
441         l2 = 0;
442     if (l1 == 0 && l2 == 0) {
443         if ((m1 == NULL) == (m2 == NULL)) {
444             test_memory_null_empty(m1, ' ');
445         } else {
446             test_diff_header(left, right);
447             test_memory_null_empty(m1, '-');
448             test_memory_null_empty(m2, '+');
449         }
450         goto fin;
451     }
452 
453     if (l1 != l2 || (m1 != m2 && memcmp(m1, m2, l1) != 0))
454         test_diff_header(left, right);
455 
456     while (l1 > 0 || l2 > 0) {
457         n1 = n2 = 0;
458         if (l1 > 0) {
459             n1 = l1 > bytes ? bytes : l1;
460             hex_convert_memory(m1, n1, b1, 8);
461         }
462         if (l2 > 0) {
463             n2 = l2 > bytes ? bytes : l2;
464             hex_convert_memory(m2, n2, b2, 8);
465         }
466 
467         diff = 0;
468         i = 0;
469         p = bdiff;
470         if (n1 > 0 && n2 > 0) {
471             const size_t j = n1 < n2 ? n1 : n2;
472 
473             for (; i < j; i++) {
474                 if (m1[i] == m2[i]) {
475                     *p++ = ' ';
476                     *p++ = ' ';
477                 } else {
478                     *p++ = '^';
479                     *p++ = '^';
480                     diff = 1;
481                 }
482                 if (i % 8 == 7 && i != j - 1)
483                     *p++ = ' ';
484             }
485             *p++ = '\0';
486         }
487 
488         if (n1 == n2 && !diff) {
489             test_printf_stderr("%04x: %s\n", cnt, b1);
490         } else {
491             if (cnt == 0 && (m1 == NULL || l1 == 0))
492                 test_memory_null_empty(m1, '-');
493             else if (n1 > 0)
494                 test_printf_stderr("%04x:-%s\n", cnt, b1);
495             if (cnt == 0 && (m2 == NULL || l2 == 0))
496                 test_memory_null_empty(m2, '+');
497             else if (n2 > 0)
498                 test_printf_stderr("%04x:+%s\n", cnt, b2);
499             if (diff && i > 0)
500                 test_printf_stderr("%4s  %s\n", "", bdiff);
501         }
502         if (m1 != NULL)
503             m1 += n1;
504         if (m2 != NULL)
505             m2 += n2;
506         l1 -= n1;
507         l2 -= n2;
508         cnt += bytes;
509     }
510 fin:
511     test_flush_stderr();
512 }
513 
514 /*
515  * Wrapper routines so that the underlying code can be shared.
516  * The first is the call from inside the test utilities when a conditional
517  * fails.  The second is the user's call to dump memory.
518  */
test_fail_memory_message(const char * prefix,const char * file,int line,const char * type,const char * left,const char * right,const char * op,const unsigned char * m1,size_t l1,const unsigned char * m2,size_t l2)519 void test_fail_memory_message(const char *prefix, const char *file,
520                               int line, const char *type,
521                               const char *left, const char *right,
522                               const char *op,
523                               const unsigned char *m1, size_t l1,
524                               const unsigned char *m2, size_t l2)
525 {
526     test_fail_memory_common(prefix, file, line, type, left, right, op,
527                             m1, l1, m2, l2);
528     test_printf_stderr("\n");
529 }
530 
test_output_memory(const char * name,const unsigned char * m,size_t l)531 void test_output_memory(const char *name, const unsigned char *m, size_t l)
532 {
533     test_fail_memory_common("memory", NULL, 0, NULL, NULL, NULL, name,
534                             m, l, m, l);
535 }
536