diff options
author | Jack Lloyd <[email protected]> | 2021-04-04 19:23:04 -0400 |
---|---|---|
committer | Jack Lloyd <[email protected]> | 2021-04-04 19:23:04 -0400 |
commit | 262681832f7b58287b17a3d33754f5dc4699b660 (patch) | |
tree | 9c61bc65f42157e2d23d80c4f9cdf1677fe30769 | |
parent | ef0d37c0ea444dac22a5ba51482a37a435af8d55 (diff) | |
parent | f05011a8d5e61752d98a58318ec155f15e749ee3 (diff) |
Merge GH #2696 Avoid raw pointers in FFI internals
-rw-r--r-- | src/lib/ffi/ffi_block.cpp | 4 | ||||
-rw-r--r-- | src/lib/ffi/ffi_cert.cpp | 16 | ||||
-rw-r--r-- | src/lib/ffi/ffi_cipher.cpp | 5 | ||||
-rw-r--r-- | src/lib/ffi/ffi_fpe.cpp | 2 | ||||
-rw-r--r-- | src/lib/ffi/ffi_hash.cpp | 6 | ||||
-rw-r--r-- | src/lib/ffi/ffi_hotp.cpp | 4 | ||||
-rw-r--r-- | src/lib/ffi/ffi_mac.cpp | 2 | ||||
-rw-r--r-- | src/lib/ffi/ffi_mp.cpp | 3 | ||||
-rw-r--r-- | src/lib/ffi/ffi_pk_op.cpp | 10 | ||||
-rw-r--r-- | src/lib/ffi/ffi_pkey.cpp | 8 | ||||
-rw-r--r-- | src/lib/ffi/ffi_pkey_algs.cpp | 62 | ||||
-rw-r--r-- | src/lib/ffi/ffi_rng.cpp | 4 | ||||
-rw-r--r-- | src/lib/ffi/ffi_totp.cpp | 5 | ||||
-rw-r--r-- | src/lib/ffi/ffi_util.h | 10 |
14 files changed, 81 insertions, 60 deletions
diff --git a/src/lib/ffi/ffi_block.cpp b/src/lib/ffi/ffi_block.cpp index fa5c25c57..eee540697 100644 --- a/src/lib/ffi/ffi_block.cpp +++ b/src/lib/ffi/ffi_block.cpp @@ -22,11 +22,11 @@ int botan_block_cipher_init(botan_block_cipher_t* bc, const char* bc_name) *bc = nullptr; - std::unique_ptr<Botan::BlockCipher> cipher(Botan::BlockCipher::create(bc_name)); + auto cipher = Botan::BlockCipher::create(bc_name); if(cipher == nullptr) return BOTAN_FFI_ERROR_NOT_IMPLEMENTED; - *bc = new botan_block_cipher_struct(cipher.release()); + *bc = new botan_block_cipher_struct(std::move(cipher)); return BOTAN_FFI_SUCCESS; }); } diff --git a/src/lib/ffi/ffi_cert.cpp b/src/lib/ffi/ffi_cert.cpp index 1baaa18d6..cc18ed602 100644 --- a/src/lib/ffi/ffi_cert.cpp +++ b/src/lib/ffi/ffi_cert.cpp @@ -33,8 +33,8 @@ int botan_x509_cert_load_file(botan_x509_cert_t* cert_obj, const char* cert_path #if defined(BOTAN_HAS_X509_CERTIFICATES) && defined(BOTAN_TARGET_OS_HAS_FILESYSTEM) return ffi_guard_thunk(__func__, [=]() -> int { - std::unique_ptr<Botan::X509_Certificate> c(new Botan::X509_Certificate(cert_path)); - *cert_obj = new botan_x509_cert_struct(c.release()); + auto c = std::make_unique<Botan::X509_Certificate>(cert_path); + *cert_obj = new botan_x509_cert_struct(std::move(c)); return BOTAN_FFI_SUCCESS; }); @@ -52,7 +52,7 @@ int botan_x509_cert_dup(botan_x509_cert_t* cert_obj, botan_x509_cert_t cert) return ffi_guard_thunk(__func__, [=]() -> int { std::unique_ptr<Botan::X509_Certificate> c(new Botan::X509_Certificate(safe_get(cert))); - *cert_obj = new botan_x509_cert_struct(c.release()); + *cert_obj = new botan_x509_cert_struct(std::move(c)); return BOTAN_FFI_SUCCESS; }); @@ -71,7 +71,7 @@ int botan_x509_cert_load(botan_x509_cert_t* cert_obj, const uint8_t cert_bits[], return ffi_guard_thunk(__func__, [=]() -> int { Botan::DataSource_Memory bits(cert_bits, cert_bits_len); std::unique_ptr<Botan::X509_Certificate> c(new Botan::X509_Certificate(bits)); - *cert_obj = new botan_x509_cert_struct(c.release()); + *cert_obj = new botan_x509_cert_struct(std::move(c)); return BOTAN_FFI_SUCCESS; }); #else @@ -89,8 +89,8 @@ int botan_x509_cert_get_public_key(botan_x509_cert_t cert, botan_pubkey_t* key) #if defined(BOTAN_HAS_X509_CERTIFICATES) return ffi_guard_thunk(__func__, [=]() -> int { - std::unique_ptr<Botan::Public_Key> publicKey = safe_get(cert).load_subject_public_key(); - *key = new botan_pubkey_struct(publicKey.release()); + std::unique_ptr<Botan::Public_Key> public_key = safe_get(cert).load_subject_public_key(); + *key = new botan_pubkey_struct(std::move(public_key)); return BOTAN_FFI_SUCCESS; }); #else @@ -365,7 +365,7 @@ int botan_x509_crl_load_file(botan_x509_crl_t* crl_obj, const char* crl_path) return ffi_guard_thunk(__func__, [=]() -> int { std::unique_ptr<Botan::X509_CRL> c(new Botan::X509_CRL(crl_path)); - *crl_obj = new botan_x509_crl_struct(c.release()); + *crl_obj = new botan_x509_crl_struct(std::move(c)); return BOTAN_FFI_SUCCESS; }); @@ -383,7 +383,7 @@ int botan_x509_crl_load(botan_x509_crl_t* crl_obj, const uint8_t crl_bits[], siz return ffi_guard_thunk(__func__, [=]() -> int { Botan::DataSource_Memory bits(crl_bits, crl_bits_len); std::unique_ptr<Botan::X509_CRL> c(new Botan::X509_CRL(bits)); - *crl_obj = new botan_x509_crl_struct(c.release()); + *crl_obj = new botan_x509_crl_struct(std::move(c)); return BOTAN_FFI_SUCCESS; }); #else diff --git a/src/lib/ffi/ffi_cipher.cpp b/src/lib/ffi/ffi_cipher.cpp index 17f443c89..5c2973bf5 100644 --- a/src/lib/ffi/ffi_cipher.cpp +++ b/src/lib/ffi/ffi_cipher.cpp @@ -14,7 +14,8 @@ using namespace Botan_FFI; struct botan_cipher_struct final : public botan_struct<Botan::Cipher_Mode, 0xB4A2BF9C> { - explicit botan_cipher_struct(Botan::Cipher_Mode* x) : botan_struct(x) {} + explicit botan_cipher_struct(std::unique_ptr<Botan::Cipher_Mode> x) : + botan_struct(std::move(x)) {} Botan::secure_vector<uint8_t> m_buf; }; @@ -26,7 +27,7 @@ int botan_cipher_init(botan_cipher_t* cipher, const char* cipher_name, uint32_t std::unique_ptr<Botan::Cipher_Mode> mode(Botan::Cipher_Mode::create(cipher_name, dir)); if(!mode) return BOTAN_FFI_ERROR_NOT_IMPLEMENTED; - *cipher = new botan_cipher_struct(mode.release()); + *cipher = new botan_cipher_struct(std::move(mode)); return BOTAN_FFI_SUCCESS; }); } diff --git a/src/lib/ffi/ffi_fpe.cpp b/src/lib/ffi/ffi_fpe.cpp index 01706ea20..43ef35f56 100644 --- a/src/lib/ffi/ffi_fpe.cpp +++ b/src/lib/ffi/ffi_fpe.cpp @@ -46,7 +46,7 @@ int botan_fpe_fe1_init(botan_fpe_t* fpe, botan_mp_t n, fpe_obj->set_key(key, key_len); - *fpe = new botan_fpe_struct(fpe_obj.release()); + *fpe = new botan_fpe_struct(std::move(fpe_obj)); return BOTAN_FFI_SUCCESS; }); #else diff --git a/src/lib/ffi/ffi_hash.cpp b/src/lib/ffi/ffi_hash.cpp index 12eb92301..6020d60bd 100644 --- a/src/lib/ffi/ffi_hash.cpp +++ b/src/lib/ffi/ffi_hash.cpp @@ -22,11 +22,11 @@ int botan_hash_init(botan_hash_t* hash, const char* hash_name, uint32_t flags) if(flags != 0) return BOTAN_FFI_ERROR_BAD_FLAG; - std::unique_ptr<Botan::HashFunction> h = Botan::HashFunction::create(hash_name); + auto h = Botan::HashFunction::create(hash_name); if(h == nullptr) return BOTAN_FFI_ERROR_NOT_IMPLEMENTED; - *hash = new botan_hash_struct(h.release()); + *hash = new botan_hash_struct(std::move(h)); return BOTAN_FFI_SUCCESS; }); } @@ -76,7 +76,7 @@ int botan_hash_final(botan_hash_t hash, uint8_t out[]) int botan_hash_copy_state(botan_hash_t* dest, const botan_hash_t source) { return BOTAN_FFI_DO(Botan::HashFunction, source, src, { - *dest = new botan_hash_struct(src.copy_state().release()); }); + *dest = new botan_hash_struct(src.copy_state()); }); } int botan_hash_name(botan_hash_t hash, char* name, size_t* name_len) diff --git a/src/lib/ffi/ffi_hotp.cpp b/src/lib/ffi/ffi_hotp.cpp index 74f059d50..cbbdb5a57 100644 --- a/src/lib/ffi/ffi_hotp.cpp +++ b/src/lib/ffi/ffi_hotp.cpp @@ -34,8 +34,8 @@ int botan_hotp_init(botan_hotp_t* hotp, #if defined(BOTAN_HAS_HOTP) return ffi_guard_thunk(__func__, [=]() -> int { - *hotp = new botan_hotp_struct( - new Botan::HOTP(key, key_len, hash_algo, digits)); + auto otp = std::make_unique<Botan::HOTP>(key, key_len, hash_algo, digits); + *hotp = new botan_hotp_struct(std::move(otp)); return BOTAN_FFI_SUCCESS; }); diff --git a/src/lib/ffi/ffi_mac.cpp b/src/lib/ffi/ffi_mac.cpp index 3b6cc3bef..10ff548aa 100644 --- a/src/lib/ffi/ffi_mac.cpp +++ b/src/lib/ffi/ffi_mac.cpp @@ -26,7 +26,7 @@ int botan_mac_init(botan_mac_t* mac, const char* mac_name, uint32_t flags) if(m == nullptr) return BOTAN_FFI_ERROR_NOT_IMPLEMENTED; - *mac = new botan_mac_struct(m.release()); + *mac = new botan_mac_struct(std::move(m)); return BOTAN_FFI_SUCCESS; }); } diff --git a/src/lib/ffi/ffi_mp.cpp b/src/lib/ffi/ffi_mp.cpp index b6645c2cc..96ef9998f 100644 --- a/src/lib/ffi/ffi_mp.cpp +++ b/src/lib/ffi/ffi_mp.cpp @@ -23,7 +23,8 @@ int botan_mp_init(botan_mp_t* mp_out) if(mp_out == nullptr) return BOTAN_FFI_ERROR_NULL_POINTER; - *mp_out = new botan_mp_struct(new Botan::BigInt); + auto mp = std::make_unique<Botan::BigInt>(); + *mp_out = new botan_mp_struct(std::move(mp)); return BOTAN_FFI_SUCCESS; }); } diff --git a/src/lib/ffi/ffi_pk_op.cpp b/src/lib/ffi/ffi_pk_op.cpp index 98129596d..ea7478166 100644 --- a/src/lib/ffi/ffi_pk_op.cpp +++ b/src/lib/ffi/ffi_pk_op.cpp @@ -36,7 +36,7 @@ int botan_pk_op_encrypt_create(botan_pk_op_encrypt_t* op, *op = nullptr; std::unique_ptr<Botan::PK_Encryptor> pk(new Botan::PK_Encryptor_EME(safe_get(key_obj), Botan::system_rng(), padding)); - *op = new botan_pk_op_encrypt_struct(pk.release()); + *op = new botan_pk_op_encrypt_struct(std::move(pk)); return BOTAN_FFI_SUCCESS; }); } @@ -81,7 +81,7 @@ int botan_pk_op_decrypt_create(botan_pk_op_decrypt_t* op, *op = nullptr; std::unique_ptr<Botan::PK_Decryptor> pk(new Botan::PK_Decryptor_EME(safe_get(key_obj), Botan::system_rng(), padding)); - *op = new botan_pk_op_decrypt_struct(pk.release()); + *op = new botan_pk_op_decrypt_struct(std::move(pk)); return BOTAN_FFI_SUCCESS; }); } @@ -127,7 +127,7 @@ int botan_pk_op_sign_create(botan_pk_op_sign_t* op, auto format = (flags & BOTAN_PUBKEY_DER_FORMAT_SIGNATURE) ? Botan::DER_SEQUENCE : Botan::IEEE_1363; std::unique_ptr<Botan::PK_Signer> pk(new Botan::PK_Signer(safe_get(key_obj), Botan::system_rng(), hash, format)); - *op = new botan_pk_op_sign_struct(pk.release()); + *op = new botan_pk_op_sign_struct(std::move(pk)); return BOTAN_FFI_SUCCESS; }); } @@ -172,7 +172,7 @@ int botan_pk_op_verify_create(botan_pk_op_verify_t* op, *op = nullptr; auto format = (flags & BOTAN_PUBKEY_DER_FORMAT_SIGNATURE) ? Botan::DER_SEQUENCE : Botan::IEEE_1363; std::unique_ptr<Botan::PK_Verifier> pk(new Botan::PK_Verifier(safe_get(key_obj), hash, format)); - *op = new botan_pk_op_verify_struct(pk.release()); + *op = new botan_pk_op_verify_struct(std::move(pk)); return BOTAN_FFI_SUCCESS; }); } @@ -213,7 +213,7 @@ int botan_pk_op_key_agreement_create(botan_pk_op_ka_t* op, return ffi_guard_thunk(__func__, [=]() -> int { *op = nullptr; std::unique_ptr<Botan::PK_Key_Agreement> pk(new Botan::PK_Key_Agreement(safe_get(key_obj), Botan::system_rng(), kdf)); - *op = new botan_pk_op_ka_struct(pk.release()); + *op = new botan_pk_op_ka_struct(std::move(pk)); return BOTAN_FFI_SUCCESS; }); } diff --git a/src/lib/ffi/ffi_pkey.cpp b/src/lib/ffi/ffi_pkey.cpp index 19190efa1..6a91c10f7 100644 --- a/src/lib/ffi/ffi_pkey.cpp +++ b/src/lib/ffi/ffi_pkey.cpp @@ -44,7 +44,7 @@ int botan_privkey_create(botan_privkey_t* key_obj, if(key) { - *key_obj = new botan_privkey_struct(key.release()); + *key_obj = new botan_privkey_struct(std::move(key)); return BOTAN_FFI_SUCCESS; } else @@ -78,7 +78,7 @@ int botan_privkey_load(botan_privkey_t* key, botan_rng_t rng_obj, if(pkcs8) { - *key = new botan_privkey_struct(pkcs8.release()); + *key = new botan_privkey_struct(std::move(pkcs8)); return BOTAN_FFI_SUCCESS; } return BOTAN_FFI_ERROR_UNKNOWN_ERROR; @@ -102,7 +102,7 @@ int botan_pubkey_load(botan_pubkey_t* key, if(pubkey == nullptr) return BOTAN_FFI_ERROR_UNKNOWN_ERROR; - *key = new botan_pubkey_struct(pubkey.release()); + *key = new botan_pubkey_struct(std::move(pubkey)); return BOTAN_FFI_SUCCESS; }); } @@ -118,7 +118,7 @@ int botan_privkey_export_pubkey(botan_pubkey_t* pubout, botan_privkey_t key_obj) std::unique_ptr<Botan::Public_Key> pubkey(Botan::X509::load_key(Botan::X509::BER_encode(safe_get(key_obj)))); - *pubout = new botan_pubkey_struct(pubkey.release()); + *pubout = new botan_pubkey_struct(std::move(pubkey)); return BOTAN_FFI_SUCCESS; }); } diff --git a/src/lib/ffi/ffi_pkey_algs.cpp b/src/lib/ffi/ffi_pkey_algs.cpp index fe9456d93..564f51fa8 100644 --- a/src/lib/ffi/ffi_pkey_algs.cpp +++ b/src/lib/ffi/ffi_pkey_algs.cpp @@ -268,9 +268,11 @@ int botan_privkey_load_rsa(botan_privkey_t* key, *key = nullptr; return ffi_guard_thunk(__func__, [=]() -> int { - *key = new botan_privkey_struct(new Botan::RSA_PrivateKey(safe_get(rsa_p), - safe_get(rsa_q), - safe_get(rsa_e))); + + auto rsa = std::make_unique<Botan::RSA_PrivateKey>(safe_get(rsa_p), + safe_get(rsa_q), + safe_get(rsa_e)); + *key = new botan_privkey_struct(std::move(rsa)); return BOTAN_FFI_SUCCESS; }); #else @@ -289,7 +291,8 @@ int botan_privkey_load_rsa_pkcs1(botan_privkey_t* key, Botan::secure_vector<uint8_t> src(bits, bits + len); return ffi_guard_thunk(__func__, [=]() -> int { Botan::AlgorithmIdentifier alg_id("RSA", Botan::AlgorithmIdentifier::USE_NULL_PARAM); - *key = new botan_privkey_struct(new Botan::RSA_PrivateKey(alg_id, src)); + auto rsa = std::make_unique<Botan::RSA_PrivateKey>(alg_id, src); + *key = new botan_privkey_struct(std::move(rsa)); return BOTAN_FFI_SUCCESS; }); #else @@ -304,7 +307,8 @@ int botan_pubkey_load_rsa(botan_pubkey_t* key, #if defined(BOTAN_HAS_RSA) *key = nullptr; return ffi_guard_thunk(__func__, [=]() -> int { - *key = new botan_pubkey_struct(new Botan::RSA_PublicKey(safe_get(n), safe_get(e))); + auto rsa = std::make_unique<Botan::RSA_PublicKey>(safe_get(n), safe_get(e)); + *key = new botan_pubkey_struct(std::move(rsa)); return BOTAN_FFI_SUCCESS; }); #else @@ -392,7 +396,8 @@ int botan_privkey_create_dsa(botan_privkey_t* key, botan_rng_t rng_obj, size_t p return ffi_guard_thunk(__func__, [=]() -> int { Botan::RandomNumberGenerator& rng = safe_get(rng_obj); Botan::DL_Group group(rng, Botan::DL_Group::Prime_Subgroup, pbits, qbits); - *key = new botan_privkey_struct(new Botan::DSA_PrivateKey(rng, group)); + auto dsa = std::make_unique<Botan::DSA_PrivateKey>(rng, group); + *key = new botan_privkey_struct(std::move(dsa)); return BOTAN_FFI_SUCCESS; }); #else @@ -410,7 +415,8 @@ int botan_privkey_load_dsa(botan_privkey_t* key, return ffi_guard_thunk(__func__, [=]() -> int { Botan::Null_RNG null_rng; Botan::DL_Group group(safe_get(p), safe_get(q), safe_get(g)); - *key = new botan_privkey_struct(new Botan::DSA_PrivateKey(null_rng, group, safe_get(x))); + auto dsa = std::make_unique<Botan::DSA_PrivateKey>(null_rng, group, safe_get(x)); + *key = new botan_privkey_struct(std::move(dsa)); return BOTAN_FFI_SUCCESS; }); #else @@ -427,7 +433,8 @@ int botan_pubkey_load_dsa(botan_pubkey_t* key, return ffi_guard_thunk(__func__, [=]() -> int { Botan::DL_Group group(safe_get(p), safe_get(q), safe_get(g)); - *key = new botan_pubkey_struct(new Botan::DSA_PublicKey(group, safe_get(y))); + auto dsa = std::make_unique<Botan::DSA_PublicKey>(group, safe_get(y)); + *key = new botan_pubkey_struct(std::move(dsa)); return BOTAN_FFI_SUCCESS; }); #else @@ -479,7 +486,7 @@ int botan_pubkey_load_ecdsa(botan_pubkey_t* key, int rc = pubkey_load_ec(p_key, safe_get(public_x), safe_get(public_y), curve_name); if(rc == BOTAN_FFI_SUCCESS) - *key = new botan_pubkey_struct(p_key.release()); + *key = new botan_pubkey_struct(std::move(p_key)); return rc; }); @@ -498,7 +505,7 @@ int botan_privkey_load_ecdsa(botan_privkey_t* key, std::unique_ptr<Botan::ECDSA_PrivateKey> p_key; int rc = privkey_load_ec(p_key, safe_get(scalar), curve_name); if(rc == BOTAN_FFI_SUCCESS) - *key = new botan_privkey_struct(p_key.release()); + *key = new botan_privkey_struct(std::move(p_key)); return rc; }); #else @@ -529,7 +536,8 @@ int botan_privkey_create_elgamal(botan_privkey_t* key, return ffi_guard_thunk(__func__, [=]() -> int { Botan::RandomNumberGenerator& rng = safe_get(rng_obj); Botan::DL_Group group(rng, prime_type, pbits, qbits); - *key = new botan_privkey_struct(new Botan::ElGamal_PrivateKey(rng, group)); + auto elg = std::make_unique<Botan::ElGamal_PrivateKey>(rng, group); + *key = new botan_privkey_struct(std::move(elg)); return BOTAN_FFI_SUCCESS; }); #else @@ -545,7 +553,8 @@ int botan_pubkey_load_elgamal(botan_pubkey_t* key, *key = nullptr; return ffi_guard_thunk(__func__, [=]() -> int { Botan::DL_Group group(safe_get(p), safe_get(g)); - *key = new botan_pubkey_struct(new Botan::ElGamal_PublicKey(group, safe_get(y))); + auto elg = std::make_unique<Botan::ElGamal_PublicKey>(group, safe_get(y)); + *key = new botan_pubkey_struct(std::move(elg)); return BOTAN_FFI_SUCCESS; }); #else @@ -562,7 +571,8 @@ int botan_privkey_load_elgamal(botan_privkey_t* key, return ffi_guard_thunk(__func__, [=]() -> int { Botan::Null_RNG null_rng; Botan::DL_Group group(safe_get(p), safe_get(g)); - *key = new botan_privkey_struct(new Botan::ElGamal_PrivateKey(null_rng, group, safe_get(x))); + auto elg = std::make_unique<Botan::ElGamal_PrivateKey>(null_rng, group, safe_get(x)); + *key = new botan_privkey_struct(std::move(elg)); return BOTAN_FFI_SUCCESS; }); #else @@ -586,7 +596,8 @@ int botan_privkey_load_dh(botan_privkey_t* key, return ffi_guard_thunk(__func__, [=]() -> int { Botan::Null_RNG null_rng; Botan::DL_Group group(safe_get(p), safe_get(g)); - *key = new botan_privkey_struct(new Botan::DH_PrivateKey(null_rng, group, safe_get(x))); + auto dh = std::make_unique<Botan::DH_PrivateKey>(null_rng, group, safe_get(x)); + *key = new botan_privkey_struct(std::move(dh)); return BOTAN_FFI_SUCCESS; }); #else @@ -602,7 +613,8 @@ int botan_pubkey_load_dh(botan_pubkey_t* key, *key = nullptr; return ffi_guard_thunk(__func__, [=]() -> int { Botan::DL_Group group(safe_get(p), safe_get(g)); - *key = new botan_pubkey_struct(new Botan::DH_PublicKey(group, safe_get(y))); + auto dh = std::make_unique<Botan::DH_PublicKey>(group, safe_get(y)); + *key = new botan_pubkey_struct(std::move(dh)); return BOTAN_FFI_SUCCESS; }); #else @@ -637,7 +649,7 @@ int botan_pubkey_load_ecdh(botan_pubkey_t* key, int rc = pubkey_load_ec(p_key, safe_get(public_x), safe_get(public_y), curve_name); if(rc == BOTAN_FFI_SUCCESS) - *key = new botan_pubkey_struct(p_key.release()); + *key = new botan_pubkey_struct(std::move(p_key)); return rc; }); #else @@ -655,7 +667,7 @@ int botan_privkey_load_ecdh(botan_privkey_t* key, std::unique_ptr<Botan::ECDH_PrivateKey> p_key; int rc = privkey_load_ec(p_key, safe_get(scalar), curve_name); if(rc == BOTAN_FFI_SUCCESS) - *key = new botan_privkey_struct(p_key.release()); + *key = new botan_privkey_struct(std::move(p_key)); return rc; }); #else @@ -712,7 +724,7 @@ int botan_pubkey_load_sm2(botan_pubkey_t* key, std::unique_ptr<Botan::SM2_PublicKey> p_key; if(!pubkey_load_ec(p_key, safe_get(public_x), safe_get(public_y), curve_name)) { - *key = new botan_pubkey_struct(p_key.release()); + *key = new botan_pubkey_struct(std::move(p_key)); return BOTAN_FFI_SUCCESS; } return BOTAN_FFI_ERROR_UNKNOWN_ERROR; @@ -733,7 +745,7 @@ int botan_privkey_load_sm2(botan_privkey_t* key, int rc = privkey_load_ec(p_key, safe_get(scalar), curve_name); if(rc == BOTAN_FFI_SUCCESS) - *key = new botan_privkey_struct(p_key.release()); + *key = new botan_privkey_struct(std::move(p_key)); return rc; }); #else @@ -766,7 +778,8 @@ int botan_privkey_load_ed25519(botan_privkey_t* key, *key = nullptr; return ffi_guard_thunk(__func__, [=]() -> int { const Botan::secure_vector<uint8_t> privkey_vec(privkey, privkey + 32); - *key = new botan_privkey_struct(new Botan::Ed25519_PrivateKey(privkey_vec)); + auto ed25519 = std::make_unique<Botan::Ed25519_PrivateKey>(privkey_vec); + *key = new botan_privkey_struct(std::move(ed25519)); return BOTAN_FFI_SUCCESS; }); #else @@ -782,7 +795,8 @@ int botan_pubkey_load_ed25519(botan_pubkey_t* key, *key = nullptr; return ffi_guard_thunk(__func__, [=]() -> int { const std::vector<uint8_t> pubkey_vec(pubkey, pubkey + 32); - *key = new botan_pubkey_struct(new Botan::Ed25519_PublicKey(pubkey_vec)); + auto ed25519 = std::make_unique<Botan::Ed25519_PublicKey>(pubkey_vec); + *key = new botan_pubkey_struct(std::move(ed25519)); return BOTAN_FFI_SUCCESS; }); #else @@ -848,7 +862,8 @@ int botan_privkey_load_x25519(botan_privkey_t* key, *key = nullptr; return ffi_guard_thunk(__func__, [=]() -> int { const Botan::secure_vector<uint8_t> privkey_vec(privkey, privkey + 32); - *key = new botan_privkey_struct(new Botan::X25519_PrivateKey(privkey_vec)); + auto x25519 = std::make_unique<Botan::X25519_PrivateKey>(privkey_vec); + *key = new botan_privkey_struct(std::move(x25519)); return BOTAN_FFI_SUCCESS; }); #else @@ -864,7 +879,8 @@ int botan_pubkey_load_x25519(botan_pubkey_t* key, *key = nullptr; return ffi_guard_thunk(__func__, [=]() -> int { const std::vector<uint8_t> pubkey_vec(pubkey, pubkey + 32); - *key = new botan_pubkey_struct(new Botan::X25519_PublicKey(pubkey_vec)); + auto x25519 = std::make_unique<Botan::X25519_PublicKey>(pubkey_vec); + *key = new botan_pubkey_struct(std::move(x25519)); return BOTAN_FFI_SUCCESS; }); #else diff --git a/src/lib/ffi/ffi_rng.cpp b/src/lib/ffi/ffi_rng.cpp index 7bb365a24..aaf718aee 100644 --- a/src/lib/ffi/ffi_rng.cpp +++ b/src/lib/ffi/ffi_rng.cpp @@ -55,7 +55,7 @@ int botan_rng_init(botan_rng_t* rng_out, const char* rng_type) return BOTAN_FFI_ERROR_NOT_IMPLEMENTED; } - *rng_out = new botan_rng_struct(rng.release()); + *rng_out = new botan_rng_struct(std::move(rng)); return BOTAN_FFI_SUCCESS; }); } @@ -150,7 +150,7 @@ return ffi_guard_thunk(__func__,[=]() -> int { std::unique_ptr<Botan::RandomNumberGenerator> rng(new Custom_RNG(rng_name, context, get_cb, add_entropy_cb, destroy_cb)); - *rng_out = new botan_rng_struct(rng.release()); + *rng_out = new botan_rng_struct(std::move(rng)); return BOTAN_FFI_SUCCESS; }); } diff --git a/src/lib/ffi/ffi_totp.cpp b/src/lib/ffi/ffi_totp.cpp index 9ca8e8e6a..212ba1686 100644 --- a/src/lib/ffi/ffi_totp.cpp +++ b/src/lib/ffi/ffi_totp.cpp @@ -34,9 +34,8 @@ int botan_totp_init(botan_totp_t* totp, #if defined(BOTAN_HAS_TOTP) return ffi_guard_thunk(__func__, [=]() -> int { - - *totp = new botan_totp_struct( - new Botan::TOTP(key, key_len, hash_algo, digits, time_step)); + auto otp = std::make_unique<Botan::TOTP>(key, key_len, hash_algo, digits, time_step); + *totp = new botan_totp_struct(std::move(otp)); return BOTAN_FFI_SUCCESS; }); diff --git a/src/lib/ffi/ffi_util.h b/src/lib/ffi/ffi_util.h index 4269aa3e8..7501938c5 100644 --- a/src/lib/ffi/ffi_util.h +++ b/src/lib/ffi/ffi_util.h @@ -36,7 +36,9 @@ template<typename T, uint32_t MAGIC> struct botan_struct { public: - botan_struct(T* obj) : m_magic(MAGIC), m_obj(obj) {} + botan_struct(std::unique_ptr<T> obj) : + m_magic(MAGIC), m_obj(std::move(obj)) {} + virtual ~botan_struct() { m_magic = 0; m_obj.reset(); } bool magic_ok() const { return (m_magic == MAGIC); } @@ -50,8 +52,10 @@ struct botan_struct std::unique_ptr<T> m_obj; }; -#define BOTAN_FFI_DECLARE_STRUCT(NAME, TYPE, MAGIC) \ - struct NAME final : public Botan_FFI::botan_struct<TYPE, MAGIC> { explicit NAME(TYPE* x) : botan_struct(x) {} } +#define BOTAN_FFI_DECLARE_STRUCT(NAME, TYPE, MAGIC) \ + struct NAME final : public Botan_FFI::botan_struct<TYPE, MAGIC> { \ + explicit NAME(std::unique_ptr<TYPE> x) : botan_struct(std::move(x)) {} \ + } // Declared in ffi.cpp int ffi_error_exception_thrown(const char* func_name, const char* exn, |