0

I have this strange error in botan 1.10.9. When I want to store the private key bytes vector and the publics key byte vector i get an std::bad_alloc error. Could it be that is not possible to initialize a std::vector from the SecureVector from botan?

Botan::LibraryInitializer init;

Botan::AutoSeeded_RNG rng;
rng.reseed(10096);

Botan::RSA_PrivateKey rsaPrivate(rng, 1024);

std::vector<unsigned char> privateArray(rsaPrivate.pkcs8_private_key().begin(), rsaPrivate.pkcs8_private_key().end());
std::vector<unsigned char> publicArray(rsaPrivate.x509_subject_public_key().begin(), rsaPrivate.x509_subject_public_key().end());

If i encode the keys, then the operation works fine:

Botan::SecureVector<Botan::byte> publicBytes  = std::move(Botan::X509::BER_encode(rsaPrivate));
Botan::SecureVector<Botan::byte> privateBytes = std::move(Botan::PKCS8::BER_encode(rsaPrivate, rng, info.passphrase()));

std::vector<unsigned char> publicArray(publicBytes.begin(), publicBytes.end());
std::vector<unsigned char> privateArray(privateBytes.begin(), privateBytes.end());

Any ideas why this could be happening? The weird thing is that if i remove one of the vectors initialization, soooooometimes it works but most of the time i get the crash.

Sebastian
  • 1,243
  • 1
  • 18
  • 34
  • 1
    I've never even heard of Botan before reading this question, but I strongly suspect that `rsaPrivate.pkcs8_private_key()` (respectively `rsaPrivate.x509_subject_public_key()`) returns a temporary container, and you are therefore passing the `begin` iterator from one temporary and the `end` iterator from another to the `std::vector` constructor. – Casey Mar 31 '15 at 21:42
  • uhggg now that i took a break, reading your comment makes a lot of sense. I'll check botan's source, but is probably that. Thanks for taking the time! – Sebastian Apr 01 '15 at 14:42

1 Answers1

0

A little bit old of a reply, but for Botan the unlock function turns a Botan::secure_vector<T> into a std::vector<T>.

Dabo
  • 21
  • 5