botan: Replace calls to deprecated botan_privkey|pubkey_export()

This commit is contained in:
Tobias Brunner 2025-05-12 16:14:17 +02:00
parent 5e4ff88849
commit 8139256aae
3 changed files with 34 additions and 39 deletions

View File

@ -114,16 +114,6 @@ static bool get_rng(private_key_exchange_t *this, botan_rng_t *rng)
return botan_get_rng(rng, RNG_STRONG);
}
/**
* Convert the given "view" to a chunk.
*/
CALLBACK(botan_view_to_chunk, int,
chunk_t *chunk, const uint8_t *data, size_t len)
{
*chunk = chunk_clone(chunk_create((u_char*)data, len));
return 0;
}
/**
* Generate a key pair as initiator.
*/

View File

@ -48,6 +48,26 @@ bool chunk_to_botan_mp(chunk_t value, botan_mp_t *mp)
return TRUE;
}
/*
* Described in header
*/
int botan_view_to_chunk(botan_view_ctx ctx, const uint8_t *data, size_t len)
{
chunk_t *chunk = (chunk_t*)ctx;
*chunk = chunk_clone(chunk_create((u_char*)data, len));
return 0;
}
/**
* Version of the above for PEM version of the view functions.
*/
static int botan_view_str_to_chunk(botan_view_ctx ctx, const char *data,
size_t len)
{
return botan_view_to_chunk(ctx, (const uint8_t*)data, len);
}
/*
* Described in header
*/
@ -121,21 +141,11 @@ bool botan_get_encoding(botan_pubkey_t pubkey, cred_encoding_type_t type,
bool success = FALSE;
encoding->len = 0;
if (botan_pubkey_export(pubkey, NULL, &encoding->len,
BOTAN_PRIVKEY_EXPORT_FLAG_DER)
!= BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE)
if (botan_pubkey_view_der(pubkey, encoding, botan_view_to_chunk))
{
return FALSE;
}
*encoding = chunk_alloc(encoding->len);
if (botan_pubkey_export(pubkey, encoding->ptr, &encoding->len,
BOTAN_PRIVKEY_EXPORT_FLAG_DER))
{
chunk_free(encoding);
return FALSE;
}
if (type == PUBKEY_SPKI_ASN1_DER)
{
return TRUE;
@ -182,28 +192,12 @@ bool botan_get_encoding(botan_pubkey_t pubkey, cred_encoding_type_t type,
bool botan_get_privkey_encoding(botan_privkey_t key, cred_encoding_type_t type,
chunk_t *encoding)
{
uint32_t format = BOTAN_PRIVKEY_EXPORT_FLAG_DER;
switch (type)
{
case PRIVKEY_PEM:
format = BOTAN_PRIVKEY_EXPORT_FLAG_PEM;
/* fall-through */
return !botan_privkey_view_pem(key, encoding, botan_view_str_to_chunk);
case PRIVKEY_ASN1_DER:
encoding->len = 0;
if (botan_privkey_export(key, NULL, &encoding->len, format)
!= BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE)
{
return FALSE;
}
*encoding = chunk_alloc(encoding->len);
if (botan_privkey_export(key, encoding->ptr, &encoding->len,
format))
{
chunk_free(encoding);
return FALSE;
}
return TRUE;
return !botan_privkey_view_der(key, encoding, botan_view_to_chunk);
default:
return FALSE;
}

View File

@ -44,6 +44,17 @@
*/
bool chunk_to_botan_mp(chunk_t value, botan_mp_t *mp);
/**
* Callback for botan_pubkey_view_*() to convert the data to an allocated
* chunk.
*
* @param ctx pointer to the resulting chunk
* @param data "viewed" data
* @param len length of data
* @return 0 if successful
*/
int botan_view_to_chunk(botan_view_ctx ctx, const uint8_t *data, size_t len);
/**
* Get the Botan string identifier for the given hash algorithm.
*