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