From 31938846aec24970e4a53771db201a7f0ac9ac3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jeremy=20Lain=C3=A9?= Date: Tue, 2 Sep 2014 09:34:11 +0200 Subject: [PATCH] qasn1element: add QAsn1Element::toInteger This change adds the ability to decode ASN.1 INTEGER fields, provided they represent a positive number of less than 64-bit. This is needed for PKCS#12 decoding. Change-Id: Iafb76f22383278d6773b9e879a8f3ef43c8d2c8f Reviewed-by: Oliver Wolff --- src/network/ssl/qasn1element.cpp | 24 +++++++++++++++++++ src/network/ssl/qasn1element_p.h | 1 + .../ssl/qasn1element/tst_qasn1element.cpp | 8 +++++++ 3 files changed, 33 insertions(+) diff --git a/src/network/ssl/qasn1element.cpp b/src/network/ssl/qasn1element.cpp index d282a02827..cd8ebed501 100644 --- a/src/network/ssl/qasn1element.cpp +++ b/src/network/ssl/qasn1element.cpp @@ -242,6 +242,30 @@ QMultiMap QAsn1Element::toInfo() const return info; } +qint64 QAsn1Element::toInteger(bool *ok) const +{ + if (mType != QAsn1Element::IntegerType || mValue.isEmpty()) { + if (ok) + *ok = false; + return 0; + } + + // NOTE: negative numbers are not handled + if (mValue.at(0) & 0x80) { + if (ok) + *ok = false; + return 0; + } + + qint64 value = mValue.at(0) & 0x7f; + for (int i = 1; i < mValue.size(); ++i) + value = (value << 8) | quint8(mValue.at(i)); + + if (ok) + *ok = true; + return value; +} + QVector QAsn1Element::toVector() const { QVector items; diff --git a/src/network/ssl/qasn1element_p.h b/src/network/ssl/qasn1element_p.h index 6b3179ac35..949fd69875 100644 --- a/src/network/ssl/qasn1element_p.h +++ b/src/network/ssl/qasn1element_p.h @@ -97,6 +97,7 @@ public: QDateTime toDateTime() const; QMultiMap toInfo() const; + qint64 toInteger(bool *ok = 0) const; QVector toVector() const; QByteArray toObjectId() const; QByteArray toObjectName() const; diff --git a/tests/auto/network/ssl/qasn1element/tst_qasn1element.cpp b/tests/auto/network/ssl/qasn1element/tst_qasn1element.cpp index 661d13bc69..5fb4c28282 100644 --- a/tests/auto/network/ssl/qasn1element/tst_qasn1element.cpp +++ b/tests/auto/network/ssl/qasn1element/tst_qasn1element.cpp @@ -122,6 +122,14 @@ void tst_QAsn1Element::integer() QFETCH(QByteArray, encoded); QFETCH(int, value); + // read + bool ok; + QAsn1Element elem; + QVERIFY(elem.read(encoded)); + QCOMPARE(elem.type(), quint8(QAsn1Element::IntegerType)); + QCOMPARE(elem.toInteger(&ok), value); + QVERIFY(ok); + // write QByteArray buffer; QDataStream stream(&buffer, QIODevice::WriteOnly);