diff --git a/lib/BioByteArray.cpp b/lib/BioByteArray.cpp index 17d8a2b0..849e0923 100644 --- a/lib/BioByteArray.cpp +++ b/lib/BioByteArray.cpp @@ -6,8 +6,24 @@ */ #include "BioByteArray.h" +#include "func_base.h" #include +BioByteArray::BioByteArray(const BIGNUM *bn, int bits) +{ + int len = (bits+7) >> 3; + qDebug() << bits << len; + if (!bn) + return; + store.resize(BN_num_bytes(bn)); + BN_bn2bin(bn, (unsigned char *)store.data()); + openssl_error(); + if (store.size() > 0 && (char)store[0] < 0) + store.prepend('\0'); + if (len > 0 && store.size() < len) + store.prepend(len - store.size(), 0); +} + void BioByteArray::set(const QByteArray &qba) { if (read_write) { diff --git a/lib/BioByteArray.h b/lib/BioByteArray.h index 1cddb3bf..759b4593 100644 --- a/lib/BioByteArray.h +++ b/lib/BioByteArray.h @@ -11,6 +11,7 @@ #include #include #include +#include class BioByteArray { @@ -27,6 +28,7 @@ class BioByteArray public: BioByteArray(const QByteArray &qba) : store(qba) { } BioByteArray(const BioByteArray &bba) : store(bba.byteArray()) { } + BioByteArray(const BIGNUM *bn, int len = 0); BioByteArray() { } ~BioByteArray(); int size() const; diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index 727c198a..28807140 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -66,7 +66,8 @@ macro(Test name) endmacro() if(BUILD_TESTING) - Test(test_biobytearray BioByteArray.cpp BioByteArray.h) + Test(test_biobytearray BioByteArray.cpp BioByteArray.h + func_base.cpp func_base.h) Test(test_asn1int asn1int.cpp asn1int.h func_base.h func_base.cpp) Test(test_asn1time asn1time.cpp asn1time.h func_base.h func_base.cpp) Test(test_entropy entropy.cpp entropy.h func.h func_base.cpp xfile.h) diff --git a/lib/pki_key.cpp b/lib/pki_key.cpp index 787d34fb..aabf42d5 100644 --- a/lib/pki_key.cpp +++ b/lib/pki_key.cpp @@ -382,22 +382,10 @@ void pki_key::writePublic(XFile &file, bool pem) const QString pki_key::BN2QString(const BIGNUM *bn) const { - if (bn == NULL) - return "--"; - QString x=""; - char zs[10]; - int j, size = BN_num_bytes(bn); - unsigned char *buf = (unsigned char *)OPENSSL_malloc(size); - Q_CHECK_PTR(buf); - BN_bn2bin(bn, buf); - for (j = 0; j< size; j++) { - snprintf(zs, sizeof zs, "%02X%c",buf[j], ((j+1)%16 == 0) ? '\n' : - j= 0x80) - big.prepend('\0'); - ssh_key_QBA2data(big, data); + ssh_key_QBA2data(BioByteArray(bn), data); } bool pki_key::SSH2_compatible() const