]> git.ipfire.org Git - thirdparty/strongswan.git/commitdiff
botan: Replace calls to deprecated botan_privkey|pubkey_export()
authorTobias Brunner <tobias@strongswan.org>
Mon, 12 May 2025 14:14:17 +0000 (16:14 +0200)
committerTobias Brunner <tobias@strongswan.org>
Mon, 28 Jul 2025 08:28:15 +0000 (10:28 +0200)
src/libstrongswan/plugins/botan/botan_kem.c
src/libstrongswan/plugins/botan/botan_util.c
src/libstrongswan/plugins/botan/botan_util.h

index 8a1219dcc19b2fa25f278baf94539512f092ffde..17d8549dec551410c595c80100bf81dcf8ffcc62 100644 (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.
  */
index 76c3e65a693145ceed6acea9ec36173d8f76f94d..56307cd2e98d670ac6cb40e300b673511b50d3e9 100644 (file)
@@ -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;
        }
index 7da01cbdb80b7a584c9f13815f7222a5dda60735..46bf24638d9d8ec81c90cccc2eb2689f4a49f8e8 100644 (file)
  */
 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.
  *