Raise an error on syscall failure in tls_retry_write_records
[openssl.git] / apps / lib / app_params.c
1 /*
2  * Copyright 2019-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 "apps.h"
11 #include "app_params.h"
12
13 static int describe_param_type(char *buf, size_t bufsz, const OSSL_PARAM *param)
14 {
15     const char *type_mod = "";
16     const char *type = NULL;
17     int show_type_number = 0;
18     int printed_len;
19
20     switch (param->data_type) {
21     case OSSL_PARAM_UNSIGNED_INTEGER:
22         type_mod = "unsigned ";
23         /* FALLTHRU */
24     case OSSL_PARAM_INTEGER:
25         type = "integer";
26         break;
27     case OSSL_PARAM_UTF8_PTR:
28         type_mod = "pointer to a ";
29         /* FALLTHRU */
30     case OSSL_PARAM_UTF8_STRING:
31         type = "UTF8 encoded string";
32         break;
33     case OSSL_PARAM_OCTET_PTR:
34         type_mod = "pointer to an ";
35         /* FALLTHRU */
36     case OSSL_PARAM_OCTET_STRING:
37         type = "octet string";
38         break;
39     default:
40         type = "unknown type";
41         show_type_number = 1;
42         break;
43     }
44
45     printed_len = BIO_snprintf(buf, bufsz, "%s: ", param->key);
46     if (printed_len > 0) {
47         buf += printed_len;
48         bufsz -= printed_len;
49     }
50     printed_len = BIO_snprintf(buf, bufsz, "%s%s", type_mod, type);
51     if (printed_len > 0) {
52         buf += printed_len;
53         bufsz -= printed_len;
54     }
55     if (show_type_number) {
56         printed_len = BIO_snprintf(buf, bufsz, " [%d]", param->data_type);
57         if (printed_len > 0) {
58             buf += printed_len;
59             bufsz -= printed_len;
60         }
61     }
62     if (param->data_size == 0)
63         printed_len = BIO_snprintf(buf, bufsz, " (arbitrary size)");
64     else
65         printed_len = BIO_snprintf(buf, bufsz, " (max %zu bytes large)",
66                                    param->data_size);
67     if (printed_len > 0) {
68         buf += printed_len;
69         bufsz -= printed_len;
70     }
71     *buf = '\0';
72     return 1;
73 }
74
75 int print_param_types(const char *thing, const OSSL_PARAM *pdefs, int indent)
76 {
77     if (pdefs == NULL) {
78         return 1;
79     } else if (pdefs->key == NULL) {
80         /*
81          * An empty list?  This shouldn't happen, but let's just make sure to
82          * say something if there's a badly written provider...
83          */
84         BIO_printf(bio_out, "%*sEmpty list of %s (!!!)\n", indent, "", thing);
85     } else {
86         BIO_printf(bio_out, "%*s%s:\n", indent, "", thing);
87         for (; pdefs->key != NULL; pdefs++) {
88             char buf[200];       /* This should be ample space */
89
90             describe_param_type(buf, sizeof(buf), pdefs);
91             BIO_printf(bio_out, "%*s  %s\n", indent, "", buf);
92         }
93     }
94     return 1;
95 }
96
97 void print_param_value(const OSSL_PARAM *p, int indent)
98 {
99     int64_t i;
100     uint64_t u;
101
102     printf("%*s%s: ", indent, "", p->key);
103     switch (p->data_type) {
104     case OSSL_PARAM_UNSIGNED_INTEGER:
105         if (OSSL_PARAM_get_uint64(p, &u))
106             BIO_printf(bio_out, "%llu\n", (unsigned long long int)u);
107         else
108             BIO_printf(bio_out, "error getting value\n");
109         break;
110     case OSSL_PARAM_INTEGER:
111         if (OSSL_PARAM_get_int64(p, &i))
112             BIO_printf(bio_out, "%lld\n", (long long int)i);
113         else
114             BIO_printf(bio_out, "error getting value\n");
115         break;
116     case OSSL_PARAM_UTF8_PTR:
117         BIO_printf(bio_out, "'%s'\n", *(char **)(p->data));
118         break;
119     case OSSL_PARAM_UTF8_STRING:
120         BIO_printf(bio_out, "'%s'\n", (char *)p->data);
121         break;
122     case OSSL_PARAM_OCTET_PTR:
123     case OSSL_PARAM_OCTET_STRING:
124         BIO_printf(bio_out, "<%zu bytes>\n", p->data_size);
125         break;
126     default:
127         BIO_printf(bio_out, "unknown type (%u) of %zu bytes\n",
128                    p->data_type, p->data_size);
129         break;
130     }
131 }
132