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