#include "internal/common.h"
#include <openssl/ebcdic.h>
#include <openssl/err.h>
#include <openssl/params.h>
#include <openssl/buffer.h>
static int prepare_from_text(const OSSL_PARAM *paramdefs, const char *key,
const char *value, size_t value_n,
const OSSL_PARAM **paramdef, int *ishex,
size_t *buf_n, BIGNUM **tmpbn, int *found)
{
const OSSL_PARAM *p;
size_t buf_bits;
int r;
*ishex = CHECK_AND_SKIP_PREFIX(key, "hex");
p = *paramdef = OSSL_PARAM_locate_const(paramdefs, key);
if (found != NULL)
*found = p != NULL;
if (p == NULL)
return 0;
switch (p->data_type) {
case OSSL_PARAM_INTEGER:
case OSSL_PARAM_UNSIGNED_INTEGER:
if (*ishex)
r = BN_hex2bn(tmpbn, value);
else
r = BN_asc2bn(tmpbn, value);
if (r == 0 || *tmpbn == NULL)
return 0;
if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER
&& BN_is_negative(*tmpbn)) {
ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_INVALID_NEGATIVE_VALUE);
return 0;
}
if (p->data_type == OSSL_PARAM_INTEGER && BN_is_negative(*tmpbn)
&& !BN_add_word(*tmpbn, 1)) {
return 0;
}
buf_bits = (size_t)BN_num_bits(*tmpbn);
if (p->data_type == OSSL_PARAM_INTEGER && buf_bits % 8 == 0)
buf_bits += 8;
*buf_n = (buf_bits + 7) / 8;
if (p->data_size > 0) {
if (buf_bits > p->data_size * 8) {
ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_TOO_SMALL_BUFFER);
return 0;
}
*buf_n = p->data_size;
}
break;
case OSSL_PARAM_UTF8_STRING:
if (*ishex) {
ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_INVALID_ARGUMENT);
return 0;
}
*buf_n = strlen(value) + 1;
break;
case OSSL_PARAM_OCTET_STRING:
if (*ishex) {
size_t hexdigits = strlen(value);
if ((hexdigits % 2) != 0) {
ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_ODD_NUMBER_OF_DIGITS);
return 0;
}
*buf_n = hexdigits >> 1;
} else {
*buf_n = value_n;
}
break;
}
return 1;
}
static int construct_from_text(OSSL_PARAM *to, const OSSL_PARAM *paramdef,
const char *value, size_t value_n, int ishex,
void *buf, size_t buf_n, BIGNUM *tmpbn)
{
if (buf == NULL)
return 0;
if (buf_n > 0) {
switch (paramdef->data_type) {
case OSSL_PARAM_INTEGER:
case OSSL_PARAM_UNSIGNED_INTEGER:
BN_bn2nativepad(tmpbn, buf, buf_n);
if (paramdef->data_type == OSSL_PARAM_INTEGER
&& BN_is_negative(tmpbn)) {
unsigned char *cp;
size_t i = buf_n;
for (cp = buf; i-- > 0; cp++)
*cp ^= 0xFF;
}
break;
case OSSL_PARAM_UTF8_STRING:
#ifdef CHARSET_EBCDIC
ebcdic2ascii(buf, value, buf_n);
#else
strncpy(buf, value, buf_n);
#endif
buf_n--;
break;
case OSSL_PARAM_OCTET_STRING:
if (ishex) {
size_t l = 0;
if (!OPENSSL_hexstr2buf_ex(buf, buf_n, &l, value, ':'))
return 0;
} else {
memcpy(buf, value, buf_n);
}
break;
}
}
*to = *paramdef;
to->data = buf;
to->data_size = buf_n;
to->return_size = OSSL_PARAM_UNMODIFIED;
return 1;
}
int OSSL_PARAM_print_to_bio(const OSSL_PARAM *p, BIO *bio, int print_values)
{
int64_t i;
uint64_t u;
BIGNUM *bn;
#ifndef OPENSSL_SYS_UEFI
double d;
int dok;
#endif
int ok = -1;
for (; p->key != NULL; p++) {
ok = -1;
ok = BIO_printf(bio, "%s: ", p->key);
if (ok == -1)
goto end;
if (print_values == 0) {
BIO_printf(bio, "\n");
continue;
}
switch (p->data_type) {
case OSSL_PARAM_UNSIGNED_INTEGER:
if (p->data_size > sizeof(int64_t)) {
if (OSSL_PARAM_get_BN(p, &bn))
ok = BN_print(bio, bn);
else
ok = BIO_printf(bio, "error getting value\n");
} else {
if (OSSL_PARAM_get_uint64(p, &u))
ok = BIO_printf(bio, "%llu\n", (unsigned long long int)u);
else
ok = BIO_printf(bio, "error getting value\n");
}
break;
case OSSL_PARAM_INTEGER:
if (p->data_size > sizeof(int64_t)) {
if (OSSL_PARAM_get_BN(p, &bn))
ok = BN_print(bio, bn);
else
ok = BIO_printf(bio, "error getting value\n");
} else {
if (OSSL_PARAM_get_int64(p, &i))
ok = BIO_printf(bio, "%lld\n", (long long int)i);
else
ok = BIO_printf(bio, "error getting value\n");
}
break;
case OSSL_PARAM_UTF8_PTR:
ok = BIO_dump(bio, p->data, p->data_size);
break;
case OSSL_PARAM_UTF8_STRING:
ok = BIO_dump(bio, (char *)p->data, p->data_size);
break;
case OSSL_PARAM_OCTET_PTR:
case OSSL_PARAM_OCTET_STRING:
ok = BIO_dump(bio, (char *)p->data, p->data_size);
break;
#ifndef OPENSSL_SYS_UEFI
case OSSL_PARAM_REAL:
dok = 0;
dok = OSSL_PARAM_get_double(p, &d);
if (dok == 1)
ok = BIO_printf(bio, "%f\n", d);
else
ok = BIO_printf(bio, "error getting value\n");
break;
#endif
default:
ok = BIO_printf(bio, "unknown type (%u) of %zu bytes\n",
p->data_type, p->data_size);
break;
}
if (ok == -1)
goto end;
}
end:
return ok == -1 ? 0 : 1;
}
int OSSL_PARAM_allocate_from_text(OSSL_PARAM *to,
const OSSL_PARAM *paramdefs,
const char *key, const char *value,
size_t value_n, int *found)
{
const OSSL_PARAM *paramdef = NULL;
int ishex = 0;
void *buf = NULL;
size_t buf_n = 0;
BIGNUM *tmpbn = NULL;
int ok = 0;
if (to == NULL || paramdefs == NULL)
return 0;
if (!prepare_from_text(paramdefs, key, value, value_n,
¶mdef, &ishex, &buf_n, &tmpbn, found))
goto err;
if ((buf = OPENSSL_zalloc(buf_n > 0 ? buf_n : 1)) == NULL)
goto err;
ok = construct_from_text(to, paramdef, value, value_n, ishex,
buf, buf_n, tmpbn);
BN_free(tmpbn);
if (!ok)
OPENSSL_free(buf);
return ok;
err:
BN_free(tmpbn);
return 0;
}