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