From: Tobias Brunner Date: Mon, 12 May 2025 14:14:17 +0000 (+0200) Subject: botan: Replace calls to deprecated botan_privkey|pubkey_export() X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e0bc4423f9b50f01382314cd4e300d6c02856c83;p=thirdparty%2Fstrongswan.git botan: Replace calls to deprecated botan_privkey|pubkey_export() --- diff --git a/src/libstrongswan/plugins/botan/botan_kem.c b/src/libstrongswan/plugins/botan/botan_kem.c index 8a1219dcc1..17d8549dec 100644 --- a/src/libstrongswan/plugins/botan/botan_kem.c +++ b/src/libstrongswan/plugins/botan/botan_kem.c @@ -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. */ diff --git a/src/libstrongswan/plugins/botan/botan_util.c b/src/libstrongswan/plugins/botan/botan_util.c index 76c3e65a69..56307cd2e9 100644 --- a/src/libstrongswan/plugins/botan/botan_util.c +++ b/src/libstrongswan/plugins/botan/botan_util.c @@ -48,6 +48,25 @@ 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. + */ +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,18 +140,8 @@ 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) - { - return FALSE; - } - - *encoding = chunk_alloc(encoding->len); - if (botan_pubkey_export(pubkey, encoding->ptr, &encoding->len, - BOTAN_PRIVKEY_EXPORT_FLAG_DER)) + if (botan_pubkey_view_der(pubkey, encoding, botan_view_to_chunk)) { - chunk_free(encoding); return FALSE; } @@ -182,28 +191,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; } diff --git a/src/libstrongswan/plugins/botan/botan_util.h b/src/libstrongswan/plugins/botan/botan_util.h index 7da01cbdb8..46bf24638d 100644 --- a/src/libstrongswan/plugins/botan/botan_util.h +++ b/src/libstrongswan/plugins/botan/botan_util.h @@ -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. *