4
0
mirror of https://github.com/QuasarApp/qca.git synced 2025-05-14 11:29:33 +00:00

port to Qt 4

svn path=/trunk/kdesupport/qca/; revision=393410
This commit is contained in:
Justin Karneges 2005-02-27 01:12:26 +00:00
parent 7f26e0d08a
commit 7dc68f5e61
23 changed files with 368 additions and 554 deletions

27
TODO

@ -1,6 +1,6 @@
* Additional unit tests
* Bundle root certificates to be used on platforms without a system store
(we need to choose a reliable source, maybe mozilla or debian?)
(we need to choose a reliable source, maybe mozilla or debian? kde??)
* API documentation
clean up Algorithm
@ -13,20 +13,25 @@
think about documenting the various providers (qcaprovider.h)
* Considerations
threading (safety, usability) in API and plugins
threading (safety, usability) in API and plugins. look for singletons
it's possible we use QSecureArray in some unnecessary places
don't forget to QCA_EXPORT everything
ensure plugin system is upgradable this time. no createProvider3(), please..
standardize on count() vs size() when iterating?
* finish API:
do something about qhostaddress in SASL. try to remove QtNetwork dependency?
move to qt 4
use qplugin
use qshareddata for thread-safe objects
turn ambiguous bool args into enums
create include files for all class names?
* qt 4 upgrade:
new include styles
qobject has no name parameter
use qmake .prf for auto-discovery by applications
use qplugin
use qshareddata for thread-safe objects
turn ambiguous bool args into enums
* build system:
get qt 4 support into qconf
on non-win/mac, allow specifying of flatfile store, else install/use built-in
make sure it installs properly
create qt4 qmake .prf for auto-discovery by applications
* finish code for APIs:
cipher - needs to handling padding
@ -40,10 +45,6 @@
default provider should have built-in sha1 and md5 (?)
system store: all added certs need to be flagged as trusted
* build system:
on non-win/mac, allow specifying of flatfile store, else install/use built-in
make sure it installs properly
* examples
create example for BigInteger
update cipher example (after API sorted out)

@ -53,7 +53,8 @@ namespace QCA
* How much entropy to use for the random numbers that
* are required.
*/
enum Quality {
enum Quality
{
Nonce, ///< Low quality, will become public
PublicValue,///< Will become public
SessionKey, ///< Intended to remain private
@ -297,13 +298,6 @@ namespace QCA
*/
QSecureArray hash(const QSecureArray &array);
/**
* \overload
*
* \param cs the QCString to hash
*/
QSecureArray hash(const QCString &cs);
/**
* %Hash a byte array, returning it as a printable
* string
@ -321,13 +315,6 @@ namespace QCA
*/
QString hashToString(const QSecureArray &array);
/**
* \overload
*
* \param cs the QCString to hash
*/
QString hashToString(const QCString &cs);
protected:
/**
* Constructor to override in sub-classes.
@ -497,6 +484,7 @@ namespace QCA
the length of the plaintext.
*/
Cipher(const QString &type, Mode m, Direction dir, const SymmetricKey &key, const InitializationVector &iv, Padding pad, const QString &provider);
private:
class Private;
Private *d;

@ -22,7 +22,7 @@
#ifndef QCA_CERT_H
#define QCA_CERT_H
#include <qmap.h>
#include <QMap>
#include "qca_core.h"
#include "qca_publickey.h"
@ -112,7 +112,7 @@ namespace QCA
};
typedef QMap<CertificateInfoType, QString> CertificateInfo;
typedef QValueList<ConstraintType> Constraints;
typedef QList<ConstraintType> Constraints;
// note: in SPKAC mode, all options are ignored except for challenge
class QCA_EXPORT CertificateOptions
@ -195,7 +195,7 @@ namespace QCA
friend class TLS;
};
class QCA_EXPORT CertificateChain : public QValueList<Certificate>
class QCA_EXPORT CertificateChain : public QList<Certificate>
{
public:
CertificateChain();
@ -278,7 +278,7 @@ namespace QCA
QDateTime thisUpdate() const;
QDateTime nextUpdate() const;
QValueList<CRLEntry> revoked() const;
QList<CRLEntry> revoked() const;
SignatureAlgorithm signatureAlgorithm() const;
@ -302,7 +302,7 @@ namespace QCA
Certificate signRequest(const CertificateRequest &req, const QDateTime &notValidAfter) const;
Certificate createCertificate(const PublicKey &key, const CertificateOptions &opts) const;
CRL createCRL(const QDateTime &nextUpdate) const;
CRL updateCRL(const CRL &crl, const QValueList<CRLEntry> &entries, const QDateTime &nextUpdate) const;
CRL updateCRL(const CRL &crl, const QList<CRLEntry> &entries, const QDateTime &nextUpdate) const;
};
class QCA_EXPORT Store : public Algorithm
@ -314,8 +314,8 @@ namespace QCA
void addCRL(const CRL &crl);
Validity validate(const Certificate &cert, UsageMode u = UsageAny) const;
QValueList<Certificate> certificates() const;
QValueList<CRL> crls() const;
QList<Certificate> certificates() const;
QList<CRL> crls() const;
// import / export
static bool canUsePKCS7(const QString &provider = QString());

@ -24,8 +24,9 @@
#define QCA_VERSION 0x020000
#include <qptrlist.h>
#include <qstringlist.h>
#include <QString>
#include <QStringList>
#include <QList>
#include "qca_export.h"
#include "qca_tools.h"
@ -47,7 +48,7 @@ namespace QCA
* \sa ProviderListIterator
* \sa providers()
*/
typedef QPtrList<Provider> ProviderList;
typedef QList<Provider*> ProviderList;
/**
* Convenience representation for iterator for the plugin providers
@ -66,7 +67,7 @@ namespace QCA
* \sa ProviderList
* \sa providers()
*/
typedef QPtrListIterator<Provider> ProviderListIterator;
//typedef QPtrListIterator<Provider> ProviderListIterator;
/**
* Mode settings for memory allocation
@ -816,13 +817,6 @@ namespace QCA
*/
SymmetricKey(const QSecureArray &a);
/**
* Construct a key from a provided string
*
* \param cs the QCString to copy
*/
SymmetricKey(const QCString &cs);
/**
* Test for weak DES keys
*
@ -857,13 +851,6 @@ namespace QCA
\param a the byte array to copy
*/
InitializationVector(const QSecureArray &a);
/**
Construct an initialisaton vector from a provided string
\param cs the QCString to copy
*/
InitializationVector(const QCString &cs);
};
}

@ -21,7 +21,7 @@
#ifndef QCA_EXPORT_H
#define QCA_EXPORT_H
#include <qglobal.h>
#include <QtGlobal>
#ifdef Q_OS_WIN32
# ifndef QCA_STATIC

@ -22,7 +22,7 @@
#ifndef QCA_PUBLICKEY_H
#define QCA_PUBLICKEY_H
#include <qobject.h>
#include <QObject>
#include "qca_core.h"
namespace QCA
@ -104,7 +104,7 @@ namespace QCA
PKey & operator=(const PKey &from);
static QValueList<Type> supportedTypes(const QString &provider = QString());
static QList<Type> supportedTypes(const QString &provider = QString());
bool isNull() const;
Type type() const;
@ -218,7 +218,7 @@ namespace QCA
{
Q_OBJECT
public:
KeyGenerator(QObject *parent = 0, const char *name = 0);
KeyGenerator(QObject *parent = 0);
~KeyGenerator();
bool blocking() const;

@ -22,12 +22,12 @@
#ifndef QCA_SECURELAYER_H
#define QCA_SECURELAYER_H
#include <qobject.h>
#include <QObject>
#include "qca_core.h"
#include "qca_publickey.h"
#include "qca_cert.h"
class QHostAddress;
//class QHostAddress;
namespace QCA
{
@ -80,7 +80,7 @@ namespace QCA
{
Q_OBJECT
public:
SecureLayer(QObject *parent = 0, const char *name = 0);
SecureLayer(QObject *parent = 0);
void setStatefulOnly(bool b);
@ -123,7 +123,7 @@ namespace QCA
NoCert ///< identity unknown
};
TLS(QObject *parent = 0, const char *name = 0, const QString &provider = QString());
TLS(QObject *parent = 0, const QString &provider = QString());
~TLS();
void reset();
@ -211,7 +211,7 @@ namespace QCA
RequireAuthzidSupport = 0x20 // server-only
};
SASL(QObject *parent = 0, const char *name = 0, const QString &provider = QString());
SASL(QObject *parent = 0, const QString &provider = QString());
~SASL();
void reset();
@ -219,8 +219,8 @@ namespace QCA
// configuration
void setConstraints(AuthFlags f, SecurityLevel s = SL_None);
void setConstraints(AuthFlags f, int minSSF, int maxSSF);
void setLocalAddr(const QHostAddress &addr, Q_UINT16 port);
void setRemoteAddr(const QHostAddress &addr, Q_UINT16 port);
//void setLocalAddr(const QHostAddress &addr, Q_UINT16 port);
//void setRemoteAddr(const QHostAddress &addr, Q_UINT16 port);
void setExternalAuthId(const QString &authid);
void setExternalSSF(int);

@ -22,7 +22,7 @@
#ifndef QCA_SECUREMESSAGE_H
#define QCA_SECUREMESSAGE_H
#include <qobject.h>
#include <QObject>
#include "qca_core.h"
#include "qca_publickey.h"
#include "qca_cert.h"
@ -70,7 +70,7 @@ namespace QCA
class Private;
Private *d;
};
typedef QValueList<SecureMessageKey> SecureMessageKeyList;
typedef QList<SecureMessageKey> SecureMessageKeyList;
class SecureMessageSignature
{
@ -80,7 +80,7 @@ namespace QCA
Valid, // indentity is verified, matches signature
Invalid, // valid key provided, but signature failed
BadKey, // invalid key provided
NoKey, // identity unknown
NoKey // identity unknown
};
SecureMessageSignature();
@ -97,7 +97,7 @@ namespace QCA
class Private;
Private *d;
};
typedef QValueList<SecureMessageSignature> SecureMessageSignatureList;
typedef QList<SecureMessageSignature> SecureMessageSignatureList;
class SecureMessage : public QObject
{
@ -188,7 +188,7 @@ namespace QCA
{
Q_OBJECT
public:
SecureMessageSystem(QObject *parent = 0, const char *name = 0);
SecureMessageSystem(QObject *parent = 0);
~SecureMessageSystem();
};
@ -196,7 +196,7 @@ namespace QCA
{
Q_OBJECT
public:
OpenPGP(QObject *parent = 0, const char *name = 0, const QString &provider = QString());
OpenPGP(QObject *parent = 0, const QString &provider = QString());
~OpenPGP();
void setAllowAgent(bool);
@ -216,11 +216,11 @@ namespace QCA
{
Q_OBJECT
public:
SMIME(QObject *parent = 0, const char *name = 0, const QString &provider = QString());
SMIME(QObject *parent = 0, const QString &provider = QString());
~SMIME();
void setStore(const Store &store);
void setPrivateKeys(const QValueList<PrivateKey> &keys);
void setPrivateKeys(const QList<PrivateKey> &keys);
};
}

@ -22,9 +22,13 @@
#ifndef QCA_TOOLS_H
#define QCA_TOOLS_H
#include <QSharedData>
#include <QSharedDataPointer>
#include "qca_export.h"
#include <qcstring.h>
#include <qstring.h>
class QString;
class QByteArray;
class QTextStream;
// Direct secure memory access. For interfacing with C libraries if needed.
QCA_EXPORT void *qca_secure_alloc(int bytes);
@ -43,7 +47,7 @@ QCA_EXPORT void qca_secure_free(void *p);
cleared first.
Note that this class is implicitly shared (that is, copy on write).
**/
**/
class QCA_EXPORT QSecureArray
{
public:
@ -59,6 +63,13 @@ public:
*/
QSecureArray(int size);
/**
* Construct a secure byte array from a string
*
* Note that this copies, rather than references the source array
*/
QSecureArray(const char *str);
/**
* Construct a secure byte array from a QByteArray
*
@ -68,15 +79,6 @@ public:
*/
QSecureArray(const QByteArray &a);
/**
* Construct a secure byte array from a string
*
* Note that this copies, rather than references the source array
*
* \sa operator=()
*/
QSecureArray(const QCString &cs);
/**
* Construct a (shallow) copy of another secure byte array
*
@ -101,11 +103,9 @@ public:
QSecureArray & operator=(const QByteArray &a);
/**
* Creates a copy, rather than references
*
* \param cs the string to copy from
* Clears the contents of the array and makes it empty
*/
QSecureArray & operator=(const QCString &cs);
void clear();
/**
* Returns a reference to the byte at the index position
@ -121,6 +121,16 @@ public:
*/
const char & operator[](int index) const;
/**
* Pointer to the data in the secure array
*
* You can use this for memcpy and similar functions. If you are trying
* to obtain data at a particular offset, you might be better off using
* at() or operator[]
*
*/
char *data();
/**
* Pointer to the data in the secure array
*
@ -139,26 +149,26 @@ public:
* at() or operator[]
*
*/
char *data();
const char *constData() const;
/**
* Returns a reference to the byte at the index position
*
* \param index the zero-based offset to obtain
*/
const char & at(uint index) const;
char & at(int index);
/**
* Returns a reference to the byte at the index position
*
* \param index the zero-based offset to obtain
*/
char & at(uint index);
const char & at(int index) const;
/**
* Returns the number of bytes in the array
*/
uint size() const;
int size() const;
/**
* Test if the array contains any bytes.
@ -179,7 +189,7 @@ public:
*
* \param size the new length
*/
bool resize(uint size);
bool resize(int size);
/**
* Fill the data array with a specified character
@ -198,26 +208,7 @@ public:
*/
void fill(char fillChar, int fillToPosition = -1);
/**
* creates a deep copy, rather than a reference
* if you want a reference then you should use operator=()
*/
QSecureArray copy() const;
/**
* If the current array is shared, this conducts a deep copy,
* otherwise it has no effect.
*
* \code
* QSecureArray myArray = anotherArray; // currently the same
* myArray.detach(); // no longer the same data, but a copy
* // anything here that affects anotherArray does not affect
* // myArray; and vice versa.
* \endcode
*/
void detach();
/**
* Copy the contents of the secure array out to a
* standard QByteArray. Note that this performs a deep copy
* of the data.
@ -228,6 +219,7 @@ public:
* Append a secure byte array to the end of this array
*/
QSecureArray & append(const QSecureArray &a);
protected:
/**
* Assign the contents of a provided byte array to this
@ -236,19 +228,18 @@ protected:
* \param from the byte array to copy
*/
void set(const QSecureArray &from);
/**
* Assign the contents of a provided string to this
* Assign the contents of a provided byte array to this
* object.
*
* \param cs the QCString to copy
* \param from the byte array to copy
*/
void set(const QCString &cs);
void set(const QByteArray &from);
private:
class Private;
Private *d;
void reset();
QSharedDataPointer<Private> d;
};
/**
@ -450,7 +441,7 @@ public:
private:
class Private;
Private *d;
QSharedDataPointer<Private> d;
};
/**

@ -22,10 +22,7 @@
#ifndef QCAPROVIDER_H
#define QCAPROVIDER_H
#include <qstring.h>
#include <qdatetime.h>
#include <qobject.h>
#include <qhostaddress.h>
#include <QtCore>
#include "qca_core.h"
#include "qca_basic.h"
#include "qca_cert.h"
@ -264,8 +261,8 @@ class SASLContext : public Provider::Context
public:
struct HostPort
{
QHostAddress addr;
Q_UINT16 port;
//QHostAddress addr;
//Q_UINT16 port;
};
struct AuthParams
{

@ -1,8 +1,9 @@
# qca qmake profile
TEMPLATE = lib
CONFIG += qt thread release
TARGET = qca
CONFIG += release
QT -= gui
TARGET = qca
MOC_DIR = .moc
OBJECTS_DIR = .obj

@ -21,6 +21,7 @@
#include "qca_basic.h"
#include <QtCore>
#include "qcaprovider.h"
namespace QCA {
@ -93,10 +94,7 @@ void Hash::update(const char *data, int len)
if ( 0 == len )
return;
QByteArray bArray;
bArray.setRawData( data, len );
update( bArray );
bArray.resetRawData( data, len );
update(QByteArray::fromRawData(data, len));
}
// Reworked from KMD5, from KDE's kdelibs
@ -105,7 +103,7 @@ void Hash::update(QIODevice &file)
char buffer[1024];
int len;
while ((len=file.readBlock(reinterpret_cast<char*>(buffer), sizeof(buffer))) > 0)
while ((len=file.read(reinterpret_cast<char*>(buffer), sizeof(buffer))) > 0)
update(buffer, len);
}
@ -120,23 +118,11 @@ QSecureArray Hash::hash(const QSecureArray &a)
return process(a);
}
QSecureArray Hash::hash(const QCString &cs)
{
QByteArray a(cs.length());
memcpy(a.data(), cs.data(), a.size());
return hash(a);
}
QString Hash::hashToString(const QSecureArray &a)
{
return arrayToHex(hash(a));
}
QString Hash::hashToString(const QCString &cs)
{
return arrayToHex(hash(cs));
}
//----------------------------------------------------------------------------
// Cipher
//----------------------------------------------------------------------------

@ -21,8 +21,7 @@
#include "qca_cert.h"
#include <qdatetime.h>
#include <qregexp.h>
#include <QtCore>
#include "qca_publickey.h"
#include "qcaprovider.h"
@ -155,11 +154,11 @@ void CertificateOptions::setValidityPeriod(const QDateTime &start, const QDateTi
// (adapted from kdelibs) -- Justin
static bool cnMatchesAddress(const QString &_cn, const QString &peerHost)
{
QString cn = _cn.stripWhiteSpace().lower();
QString cn = _cn.trimmed().toLower();
QRegExp rx;
// Check for invalid characters
if(QRegExp("[^a-zA-Z0-9\\.\\*\\-]").search(cn) >= 0)
if(QRegExp("[^a-zA-Z0-9\\.\\*\\-]").indexIn(cn) >= 0)
return false;
// Domains can legally end with '.'s. We don't need them though.
@ -180,28 +179,27 @@ static bool cnMatchesAddress(const QString &_cn, const QString &peerHost)
if(rx.exactMatch(peerHost))
return peerHost == cn;
if(cn.contains('*')) {
if(cn.contains('*'))
{
// First make sure that there are at least two valid parts
// after the wildcard (*).
QStringList parts = QStringList::split('.', cn, false);
QStringList parts = cn.split('.', QString::SkipEmptyParts);
while(parts.count() > 2)
parts.remove(parts.begin());
parts.removeFirst();
if(parts.count() != 2) {
if(parts.count() != 2)
return false; // we don't allow *.root - that's bad
}
if(parts[0].contains('*') || parts[1].contains('*')) {
if(parts[0].contains('*') || parts[1].contains('*'))
return false;
}
// RFC2818 says that *.example.com should match against
// foo.example.com but not bar.foo.example.com
// (ie. they must have the same number of parts)
if(QRegExp(cn, false, true).exactMatch(peerHost) &&
QStringList::split('.', cn, false).count() ==
QStringList::split('.', peerHost, false).count())
if(QRegExp(cn, Qt::CaseInsensitive, QRegExp::Wildcard).exactMatch(peerHost) &&
cn.split('.', QString::SkipEmptyParts).count() ==
peerHost.split('.', QString::SkipEmptyParts).count())
return true;
return false;
@ -347,10 +345,10 @@ Certificate Certificate::fromPEMFile(const QString &fileName, ConvertResult *res
bool Certificate::matchesHostname(const QString &realHost) const
{
QString peerHost = realHost.stripWhiteSpace();
QString peerHost = realHost.trimmed();
while(peerHost.endsWith("."))
peerHost.truncate(peerHost.length()-1);
peerHost = peerHost.lower();
peerHost = peerHost.toLower();
if(cnMatchesAddress(commonName(), peerHost))
return true;
@ -570,9 +568,9 @@ QDateTime CRL::nextUpdate() const
return QDateTime();
}
QValueList<CRLEntry> CRL::revoked() const
QList<CRLEntry> CRL::revoked() const
{
return QValueList<CRLEntry>();
return QList<CRLEntry>();
}
SignatureAlgorithm CRL::signatureAlgorithm() const
@ -636,7 +634,7 @@ CRL CertificateAuthority::createCRL(const QDateTime &nextUpdate) const
return CRL();
}
CRL CertificateAuthority::updateCRL(const CRL &crl, const QValueList<CRLEntry> &entries, const QDateTime &nextUpdate) const
CRL CertificateAuthority::updateCRL(const CRL &crl, const QList<CRLEntry> &entries, const QDateTime &nextUpdate) const
{
Q_UNUSED(crl);
Q_UNUSED(entries);
@ -667,14 +665,14 @@ Validity Store::validate(const Certificate &cert, UsageMode u) const
return ((StoreContext *)context())->validate(*((CertContext *)cert.context()), u);
}
QValueList<Certificate> Store::certificates() const
QList<Certificate> Store::certificates() const
{
return QValueList<Certificate>();
return QList<Certificate>();
}
QValueList<CRL> Store::crls() const
QList<CRL> Store::crls() const
{
return QValueList<CRL>();
return QList<CRL>();
}
bool Store::canUsePKCS7(const QString &provider)

@ -21,7 +21,7 @@
#include "qca_core.h"
#include "qca_tools.h"
#include <QtCore>
#include "qca_plugin.h"
#include "qca_textfilter.h"
#include "qca_cert.h"
@ -133,7 +133,7 @@ bool isSupported(const QStringList &features)
bool isSupported(const char *features)
{
return isSupported(QStringList::split(',', QString(features)));
return isSupported(QString(features).split(',', QString::SkipEmptyParts));
}
QStringList supportedFeatures()
@ -265,7 +265,7 @@ Provider::Context *getContext(const QString &type, const QString &provider)
if(!p)
{
// try using some other provider
p = manager->findFor(QString::null, type);
p = manager->findFor(QString(), type);
if((!p || p->name() == "default") && !scanned)
{
// maybe there are new providers, so scan and try again
@ -591,11 +591,6 @@ SymmetricKey::SymmetricKey(const QSecureArray &a)
set(a);
}
SymmetricKey::SymmetricKey(const QCString &cs)
{
set(cs);
}
/* from libgcrypt-1.2.0 */
static unsigned char desWeakKeyTable[64][8] =
{
@ -699,9 +694,4 @@ InitializationVector::InitializationVector(const QSecureArray &a)
set(a);
}
InitializationVector::InitializationVector(const QCString &cs)
{
set(cs);
}
}

@ -21,8 +21,7 @@
#include "qca_core.h"
#include <qdatetime.h>
#include <qstringlist.h>
#include <QtCore>
#include <stdlib.h>
#include "qcaprovider.h"
@ -189,7 +188,7 @@ public:
if (padding.size() > 0 ) {
padding[0] = 0x80;
if (padding.size() > 1 ) {
for (i = 1; i < padding.size(); ++i) {
for (i = 1; i < (unsigned int)padding.size(); ++i) {
padding[i] = 0x00;
}
}
@ -220,69 +219,69 @@ public:
private:
/* Define the state of the MD5 Algorithm. */
Q_UINT64 m_count; /* message length in bits */
Q_UINT32 a, b, c, d; /* digest buffer */
quint64 m_count; /* message length in bits */
quint32 a, b, c, d; /* digest buffer */
QSecureArray buf; /* accumulate block */
Q_UINT32 rotateLeft(Q_UINT32 x, Q_UINT32 n)
quint32 rotateLeft(quint32 x, quint32 n)
{
return (((x) << (n)) | ((x) >> (32 - (n))));
}
Q_UINT32 F(Q_UINT32 x, Q_UINT32 y, Q_UINT32 z)
quint32 F(quint32 x, quint32 y, quint32 z)
{
return (((x) & (y)) | (~(x) & (z)));
}
Q_UINT32 G(Q_UINT32 x, Q_UINT32 y, Q_UINT32 z)
quint32 G(quint32 x, quint32 y, quint32 z)
{
return (((x) & (z)) | ((y) & ~(z)));
}
Q_UINT32 H(Q_UINT32 x, Q_UINT32 y, Q_UINT32 z)
quint32 H(quint32 x, quint32 y, quint32 z)
{
return ((x) ^ (y) ^ (z));
}
Q_UINT32 I(Q_UINT32 x, Q_UINT32 y, Q_UINT32 z)
quint32 I(quint32 x, quint32 y, quint32 z)
{
return ((y) ^ ((x) | ~(z)));
}
Q_UINT32 round1(Q_UINT32 a, Q_UINT32 b, Q_UINT32 c, Q_UINT32 d, Q_UINT32 Xk, Q_UINT32 s, Q_UINT32 Ti)
quint32 round1(quint32 a, quint32 b, quint32 c, quint32 d, quint32 Xk, quint32 s, quint32 Ti)
{
Q_UINT32 t = a + F(b,c,d) + Xk + Ti;
quint32 t = a + F(b,c,d) + Xk + Ti;
return ( rotateLeft(t, s) + b );
}
Q_UINT32 round2(Q_UINT32 a, Q_UINT32 b, Q_UINT32 c, Q_UINT32 d, Q_UINT32 Xk, Q_UINT32 s, Q_UINT32 Ti)
quint32 round2(quint32 a, quint32 b, quint32 c, quint32 d, quint32 Xk, quint32 s, quint32 Ti)
{
Q_UINT32 t = a + G(b,c,d) + Xk + Ti;
quint32 t = a + G(b,c,d) + Xk + Ti;
return ( rotateLeft(t, s) + b );
}
Q_UINT32 round3(Q_UINT32 a, Q_UINT32 b, Q_UINT32 c, Q_UINT32 d, Q_UINT32 Xk, Q_UINT32 s, Q_UINT32 Ti)
quint32 round3(quint32 a, quint32 b, quint32 c, quint32 d, quint32 Xk, quint32 s, quint32 Ti)
{
Q_UINT32 t = a + H(b,c,d) + Xk + Ti;
quint32 t = a + H(b,c,d) + Xk + Ti;
return ( rotateLeft(t, s) + b );
}
Q_UINT32 round4(Q_UINT32 a, Q_UINT32 b, Q_UINT32 c, Q_UINT32 d, Q_UINT32 Xk, Q_UINT32 s, Q_UINT32 Ti)
quint32 round4(quint32 a, quint32 b, quint32 c, quint32 d, quint32 Xk, quint32 s, quint32 Ti)
{
Q_UINT32 t = a + I(b,c,d) + Xk + Ti;
quint32 t = a + I(b,c,d) + Xk + Ti;
return ( rotateLeft(t, s) + b );
}
void md5_process(QSecureArray data)
{
Q_UINT32 aSaved, bSaved, cSaved, dSaved;
quint32 aSaved, bSaved, cSaved, dSaved;
aSaved = a;
bSaved = b;
cSaved = c;
dSaved = d;
Q_UINT32 xbuf[16];
const Q_UINT32 *X;
quint32 xbuf[16];
const quint32 *X;
if (Q_BYTE_ORDER == Q_LITTLE_ENDIAN)
{
@ -292,7 +291,7 @@ private:
*/
if (!((data.data() - (const char *)0) & 3)) {
/* data are properly aligned */
X = (const Q_UINT32 *)data.data();
X = (const quint32 *)data.data();
} else {
/* not aligned */
memcpy(xbuf, data.data(), 64);

@ -20,10 +20,7 @@
#include "qca_plugin.h"
#include <qapplication.h>
#include <qdir.h>
#include <qfileinfo.h>
#include <qlibrary.h>
#include <QtCore>
#include "qcaprovider.h"
#if defined(Q_OS_WIN32)
@ -117,16 +114,9 @@ public:
return i;
}
static ProviderItem *fromClass(QCAProvider *p)
{
ProviderItem *i = new ProviderItem(0, p);
return i;
}
~ProviderItem()
{
delete p;
//delete p_old;
delete lib;
}
@ -135,10 +125,7 @@ public:
if(init_done)
return;
init_done = true;
if(p)
p->init();
//else
// p_old->init();
p->init();
}
private:
@ -149,22 +136,12 @@ private:
{
lib = _lib;
p = _p;
//p_old = 0;
init_done = false;
}
ProviderItem(QLibrary *_lib, QCAProvider *_p_old)
{
lib = _lib;
p = 0;
//p_old = _p_old;
init_done = false;
}
};
ProviderManager::ProviderManager()
{
providerItemList.setAutoDelete(true);
def = 0;
}
@ -175,7 +152,7 @@ ProviderManager::~ProviderManager()
void ProviderManager::scan()
{
QStringList dirs = QApplication::libraryPaths();
QStringList dirs = QCoreApplication::libraryPaths();
for(QStringList::ConstIterator it = dirs.begin(); it != dirs.end(); ++it)
{
QDir libpath(*it);
@ -189,14 +166,14 @@ void ProviderManager::scan()
QFileInfo fi(dir.filePath(*it));
if(fi.isDir())
continue;
if(fi.extension() != PLUGIN_EXT)
if(fi.suffix() != PLUGIN_EXT)
continue;
QString fname = fi.filePath();
ProviderItem *i = ProviderItem::load(fname);
if(!i)
continue;
if(i->version != QCA_PLUGIN_VERSION)// && i->version != 1)
if(i->version != QCA_PLUGIN_VERSION)
{
delete i;
continue;
@ -223,30 +200,24 @@ bool ProviderManager::add(QCA::Provider *p, int priority)
return true;
}
void ProviderManager::add(QCAProvider *p)
{
ProviderItem *i = ProviderItem::fromClass(p);
addItem(i, 0); // prepend
}
void ProviderManager::unload(const QString &name)
{
QPtrListIterator<ProviderItem> it(providerItemList);
ProviderListIterator pit(providerList);
for(ProviderItem *i; (i = it.current()); ++it)
for(int n = 0; n < providerItemList.count(); ++n)
{
ProviderItem *i = providerItemList[n];
if(i->p && i->p->name() == name)
{
providerItemList.removeRef(i);
providerList.removeRef(*pit);
delete i;
providerItemList.removeAt(n);
providerList.removeAt(n);
return;
}
++pit;
}
}
void ProviderManager::unloadAll()
{
qDeleteAll(providerItemList);
providerItemList.clear();
providerList.clear();
}
@ -265,9 +236,9 @@ QCA::Provider *ProviderManager::find(const QString &name) const
if(def && name == def->name())
return def;
QPtrListIterator<ProviderItem> it(providerItemList);
for(ProviderItem *i; (i = it.current()); ++it)
for(int n = 0; n < providerItemList.count(); ++n)
{
ProviderItem *i = providerItemList[n];
if(i->p && i->p->name() == name)
{
i->ensureInit();
@ -282,9 +253,9 @@ QCA::Provider *ProviderManager::findFor(const QString &name, const QString &type
if(name.isEmpty())
{
// find the first one that can do it
QPtrListIterator<ProviderItem> it(providerItemList);
for(ProviderItem *i; (i = it.current()); ++it)
for(int n = 0; n < providerItemList.count(); ++n)
{
ProviderItem *i = providerItemList[n];
i->ensureInit();
if(i->p && i->p->features().contains(type))
return i->p;
@ -305,25 +276,13 @@ QCA::Provider *ProviderManager::findFor(const QString &name, const QString &type
}
}
QCAProvider *ProviderManager::findFor(int) const
{
// find the first one that can do it
QPtrListIterator<ProviderItem> it(providerItemList);
for(ProviderItem *i; (i = it.current()); ++it)
{
i->ensureInit();
//if(i->p_old && i->p_old->capabilities() & cap)
// return i->p_old;
}
return 0;
}
void ProviderManager::changePriority(const QString &name, int priority)
{
QPtrListIterator<ProviderItem> it(providerItemList);
ProviderItem *i = 0;
for(ProviderItem *pi; (pi = it.current()); ++it)
int n = 0;
for(; n < providerItemList.count(); ++n)
{
ProviderItem *pi = providerItemList[n];
if(pi->p && pi->p->name() == name)
{
i = pi;
@ -333,20 +292,18 @@ void ProviderManager::changePriority(const QString &name, int priority)
if(!i)
return;
providerItemList.setAutoDelete(false);
providerItemList.removeRef(i);
providerItemList.setAutoDelete(true);
providerList.removeRef(i->p);
providerItemList.removeAt(n);
providerList.removeAt(n);
addItem(i, priority);
}
int ProviderManager::getPriority(const QString &name)
{
QPtrListIterator<ProviderItem> it(providerItemList);
ProviderItem *i = 0;
for(ProviderItem *pi; (pi = it.current()); ++it)
for(int n = 0; n < providerItemList.count(); ++n)
{
ProviderItem *pi = providerItemList[n];
if(pi->p && pi->p->name() == name)
{
i = pi;
@ -359,38 +316,23 @@ int ProviderManager::getPriority(const QString &name)
return i->priority;
}
QStringList ProviderManager::allFeatures(bool includeOld) const
QStringList ProviderManager::allFeatures() const
{
QStringList list;
if(def)
list = def->features();
QPtrListIterator<ProviderItem> it(providerItemList);
for(ProviderItem *i; (i = it.current()); ++it)
for(int n = 0; n < providerItemList.count(); ++n)
{
ProviderItem *i = providerItemList[n];
if(i->p)
mergeFeatures(&list, i->p->features());
}
if(includeOld)
mergeFeatures(&list, capsToStringList(caps()));
return list;
}
int ProviderManager::caps() const
{
int caps = 0;
QPtrListIterator<ProviderItem> it(providerItemList);
for(ProviderItem *i; (i = it.current()); ++it)
{
//if(i->p_old)
// caps |= i->p_old->capabilities();
}
return caps;
}
const ProviderList & ProviderManager::providers() const
{
return providerList;
@ -401,9 +343,11 @@ void ProviderManager::addItem(ProviderItem *item, int priority)
if(priority < 0)
{
// for -1, make the priority the same as the last item
ProviderItem *last = providerItemList.getLast();
if(last)
if(!providerItemList.isEmpty())
{
ProviderItem *last = providerItemList.last();
item->priority = last->priority;
}
else
item->priority = 0;
@ -413,18 +357,17 @@ void ProviderManager::addItem(ProviderItem *item, int priority)
else
{
// place the item before any other items with same or greater priority
int at = 0;
QPtrListIterator<ProviderItem> it(providerItemList);
for(ProviderItem *i; (i = it.current()); ++it)
int n = 0;
for(; n < providerItemList.count(); ++n)
{
ProviderItem *i = providerItemList[n];
if(i->priority >= priority)
break;
++at;
}
item->priority = priority;
providerItemList.insert(at, item);
providerList.insert(at, item->p);
providerItemList.insert(n, item);
providerList.insert(n, item->p);
}
}
@ -442,24 +385,4 @@ void ProviderManager::mergeFeatures(QStringList *a, const QStringList &b)
}
}
QStringList ProviderManager::capsToStringList(int)
{
QStringList list;
/*if(cap & CAP_SHA0)
list.append("sha0");
if(cap & CAP_SHA1)
list.append("sha1");
if(cap & CAP_SHA256)
list.append("sha256");
if(cap & CAP_MD2)
list.append("md2");
if(cap & CAP_MD4)
list.append("md4");
if(cap & CAP_MD5)
list.append("md5");
if(cap & CAP_RIPEMD160)
list.append("ripemd160");*/
return list;
}
}

@ -24,12 +24,8 @@
// NOTE: this API is private to QCA
#include <qptrlist.h>
#include <qstringlist.h>
#include "qca_core.h"
class QCAProvider;
namespace QCA
{
class ProviderItem;
@ -42,24 +38,20 @@ namespace QCA
void scan();
bool add(QCA::Provider *p, int priority);
void add(QCAProvider *p); // to be obsoleted
void unload(const QString &name);
void unloadAll();
void setDefault(QCA::Provider *p);
QCA::Provider *find(const QString &name) const;
QCA::Provider *findFor(const QString &name, const QString &type) const;
QCAProvider *findFor(int cap) const; // to be obsoleted
void changePriority(const QString &name, int priority);
int getPriority(const QString &name);
QStringList allFeatures(bool includeOld = true) const;
int caps() const; // to be obsoleted
QStringList allFeatures() const;
const ProviderList & providers() const;
static void mergeFeatures(QStringList *a, const QStringList &b);
static QStringList capsToStringList(int cap);
private:
QPtrList<ProviderItem> providerItemList;
QList<ProviderItem*> providerItemList;
QCA::ProviderList providerList;
QCA::Provider *def;
void addItem(ProviderItem *i, int priority);

@ -21,7 +21,7 @@
#include "qca_publickey.h"
#include <qptrdict.h>
#include <QtCore>
#include "qcaprovider.h"
namespace QCA {
@ -72,10 +72,10 @@ void PKey::set(const PKey &k)
*this = k;
}
QValueList<PKey::Type> PKey::supportedTypes(const QString &provider)
QList<PKey::Type> PKey::supportedTypes(const QString &provider)
{
Q_UNUSED(provider);
return QValueList<PKey::Type>();
return QList<PKey::Type>();
}
bool PKey::isNull() const
@ -499,7 +499,7 @@ public:
QString provider;
PKeyBase *k;
static QPtrDict<KeyGenerator> *list;
static QHash<PKeyBase*, KeyGenerator*> *list;
Private(KeyGenerator *_parent)
{
@ -508,23 +508,28 @@ public:
~Private()
{
list_del();
delete k;
if(k)
{
list_del(k);
delete k;
}
}
void list_add(PKeyBase *c)
{
if(!list)
list = new QPtrDict<KeyGenerator>;
list = new QHash<PKeyBase*, KeyGenerator*>;
list->insert(c, parent);
}
void list_del()
void list_del(PKeyBase *c)
{
if(!list)
return;
list->remove(parent);
if(list->isEmpty()) {
if(list->contains(c))
list->remove(c);
if(list->isEmpty())
{
delete list;
list = 0;
}
@ -532,33 +537,36 @@ public:
static void rsa_cb(RSAContext *c)
{
KeyGenerator *self = list->find(c);
if(!self)
return;
self->done();
if(list->contains(c))
{
KeyGenerator *self = list->value(c);
self->done();
}
}
static void dsa_cb(DSAContext *c)
{
KeyGenerator *self = list->find(c);
if(!self)
return;
self->done();
if(list->contains(c))
{
KeyGenerator *self = list->value(c);
self->done();
}
}
static void dh_cb(DHContext *c)
{
KeyGenerator *self = list->find(c);
if(!self)
return;
self->done();
if(list->contains(c))
{
KeyGenerator *self = list->value(c);
self->done();
}
}
};
QPtrDict<KeyGenerator> *KeyGenerator::Private::list = 0;
QHash<PKeyBase*, KeyGenerator*> *KeyGenerator::Private::list = 0;
KeyGenerator::KeyGenerator(QObject *parent, const char *name)
:QObject(parent, name)
KeyGenerator::KeyGenerator(QObject *parent)
:QObject(parent)
{
d = new Private(this);
d->blocking = true;
@ -646,7 +654,7 @@ PrivateKey KeyGenerator::result() const
void KeyGenerator::done()
{
if(!d->wasBlocking)
d->list_del();
d->list_del(d->k);
PrivateKey key;

@ -21,11 +21,8 @@
#include "qca_securelayer.h"
#include <qtimer.h>
#include <qhostaddress.h>
#include <qguardedptr.h>
#include "qca_publickey.h"
#include "qca_cert.h"
#include <QtCore>
//#include <qhostaddress.h>
#include "qcaprovider.h"
namespace QCA {
@ -59,8 +56,8 @@ QSecureArray SecureFilter::readUnprocessed()
//----------------------------------------------------------------------------
// SecureLayer
//----------------------------------------------------------------------------
SecureLayer::SecureLayer(QObject *parent, const char *name)
:QObject(parent, name)
SecureLayer::SecureLayer(QObject *parent)
:QObject(parent)
{
_signals = true;
}
@ -143,8 +140,8 @@ public:
Store *store;
};
TLS::TLS(QObject *parent, const char *name, const QString &provider)
:SecureLayer(parent, name), Algorithm("tls", provider)
TLS::TLS(QObject *parent, const QString &provider)
:SecureLayer(parent), Algorithm("tls", provider)
{
d = new Private;
d->c = (TLSContext *)context();
@ -333,7 +330,7 @@ void TLS::write(const QSecureArray &a)
QSecureArray TLS::read()
{
QByteArray a = d->in.copy();
QByteArray a = d->in;
d->in.resize(0);
return a;
}
@ -347,14 +344,14 @@ void TLS::writeIncoming(const QByteArray &a)
QByteArray TLS::readOutgoing(int *plainBytes)
{
Q_UNUSED(plainBytes);
QByteArray a = d->to_net.copy();
QByteArray a = d->to_net;
d->to_net.resize(0);
return a;
}
QSecureArray TLS::readUnprocessed()
{
QByteArray a = d->from_net.copy();
QByteArray a = d->from_net;
d->from_net.resize(0);
return a;
}
@ -379,7 +376,7 @@ public:
bool tried;
SASLContext *c;
QHostAddress localAddr, remoteAddr;
//QHostAddress localAddr, remoteAddr;
int localPort, remotePort;
QByteArray stepData;
bool allowCSF;
@ -389,8 +386,8 @@ public:
QByteArray inbuf, outbuf;
};
SASL::SASL(QObject *parent, const char *name, const QString &provider)
:SecureLayer(parent, name), Algorithm("sasl", provider)
SASL::SASL(QObject *parent, const QString &provider)
:SecureLayer(parent), Algorithm("sasl", provider)
{
d = new Private;
d->c = (SASLContext *)context();
@ -466,7 +463,7 @@ void SASL::setExternalSSF(int x)
d->ext_ssf = x;
}
void SASL::setLocalAddr(const QHostAddress &addr, Q_UINT16 port)
/*void SASL::setLocalAddr(const QHostAddress &addr, Q_UINT16 port)
{
d->localAddr = addr;
d->localPort = port;
@ -476,19 +473,19 @@ void SASL::setRemoteAddr(const QHostAddress &addr, Q_UINT16 port)
{
d->remoteAddr = addr;
d->remotePort = port;
}
}*/
bool SASL::startClient(const QString &service, const QString &host, const QStringList &mechlist, bool allowClientSendFirst)
{
SASLContext::HostPort la, ra;
if(d->localPort != -1) {
/*if(d->localPort != -1) {
la.addr = d->localAddr;
la.port = d->localPort;
}
if(d->remotePort != -1) {
ra.addr = d->remoteAddr;
ra.port = d->remotePort;
}
}*/
d->allowCSF = allowClientSendFirst;
d->c->setCoreProps(service, host, d->localPort != -1 ? &la : 0, d->remotePort != -1 ? &ra : 0);
@ -508,14 +505,14 @@ bool SASL::startServer(const QString &service, const QString &host, const QStrin
Q_UNUSED(allowServerSendLast);
SASLContext::HostPort la, ra;
if(d->localPort != -1) {
/*if(d->localPort != -1) {
la.addr = d->localAddr;
la.port = d->localPort;
}
if(d->remotePort != -1) {
ra.addr = d->remoteAddr;
ra.port = d->remotePort;
}
}*/
d->c->setCoreProps(service, host, d->localPort != -1 ? &la : 0, d->remotePort != -1 ? &ra : 0);
d->setSecurityProps();
@ -548,7 +545,7 @@ void SASL::putServerFirstStep(const QString &mech, const QByteArray &clientInit)
void SASL::putStep(const QByteArray &stepData)
{
d->stepData = stepData.copy();
d->stepData = stepData;
//tryAgain();
}

@ -21,9 +21,7 @@
#include "qca_securemessage.h"
#include <qdatetime.h>
#include "qca_publickey.h"
#include "qca_cert.h"
#include <QtCore>
namespace QCA {
@ -285,8 +283,8 @@ SecureMessageSignatureList SecureMessage::signers() const
//----------------------------------------------------------------------------
// SecureMessageSystem
//----------------------------------------------------------------------------
SecureMessageSystem::SecureMessageSystem(QObject *parent, const char *name)
:QObject(parent, name)
SecureMessageSystem::SecureMessageSystem(QObject *parent)
:QObject(parent)
{
}
@ -297,8 +295,8 @@ SecureMessageSystem::~SecureMessageSystem()
//----------------------------------------------------------------------------
// OpenPGP
//----------------------------------------------------------------------------
OpenPGP::OpenPGP(QObject *parent, const char *name, const QString &provider)
:SecureMessageSystem(parent, name), Algorithm("openpgp", provider)
OpenPGP::OpenPGP(QObject *parent, const QString &provider)
:SecureMessageSystem(parent), Algorithm("openpgp", provider)
{
}
@ -333,8 +331,8 @@ QString OpenPGP::diagnosticText() const
//----------------------------------------------------------------------------
// SMIME
//----------------------------------------------------------------------------
SMIME::SMIME(QObject *parent, const char *name, const QString &provider)
:SecureMessageSystem(parent, name), Algorithm("smime", provider)
SMIME::SMIME(QObject *parent, const QString &provider)
:SecureMessageSystem(parent), Algorithm("smime", provider)
{
}
@ -347,7 +345,7 @@ void SMIME::setStore(const Store &store)
Q_UNUSED(store);
}
void SMIME::setPrivateKeys(const QValueList<PrivateKey> &keys)
void SMIME::setPrivateKeys(const QList<PrivateKey> &keys)
{
Q_UNUSED(keys);
}

@ -20,16 +20,14 @@
#include "qca_systemstore.h"
#include <qfile.h>
#include <qfileinfo.h>
#include <qtextstream.h>
#include <QtCore>
namespace QCA {
bool qca_have_systemstore()
{
QFile f(QCA_SYSTEMSTORE_PATH);
return f.open(IO_ReadOnly);
return f.open(QFile::ReadOnly);
}
static QString readNextPem(QTextStream *ts)
@ -67,7 +65,7 @@ Store qca_get_systemstore(const QString &provider)
{
Store store(provider);
QFile f(QCA_SYSTEMSTORE_PATH);
if(!f.open(IO_ReadOnly))
if(!f.open(QFile::ReadOnly))
return store;
QTextStream ts(&f);
while(1)

@ -21,6 +21,8 @@
#include "qca_textfilter.h"
#include <QtCore>
namespace QCA {
//----------------------------------------------------------------------------
@ -50,40 +52,24 @@ QSecureArray TextFilter::decode(const QSecureArray &a)
QString TextFilter::arrayToString(const QSecureArray &a)
{
QByteArray b = encode(a).toByteArray();
QCString c;
c.resize(b.size() + 1);
memcpy(c.data(), b.data(), b.size());
return QString::fromLatin1(c);
return QString::fromLatin1(encode(a).toByteArray());
}
QSecureArray TextFilter::stringToArray(const QString &s)
{
if(s.isEmpty())
return QSecureArray();
const char *c = s.latin1();
int len = strlen(c);
QSecureArray b(len);
memcpy(b.data(), c, len);
return decode(b);
return decode(s.toLatin1());
}
QString TextFilter::encodeString(const QString &s)
{
QCString c = s.utf8();
int len = c.length();
QSecureArray b(len);
memcpy(b.data(), c.data(), len);
return arrayToString(b);
return arrayToString(s.toUtf8());
}
QString TextFilter::decodeString(const QString &s)
{
QSecureArray a = stringToArray(s);
QCString c;
c.resize(a.size() + 1);
memcpy(c.data(), a.data(), a.size());
return QString::fromUtf8(c);
return QString::fromUtf8(stringToArray(s).toByteArray());
}
//----------------------------------------------------------------------------
@ -165,7 +151,7 @@ QSecureArray Hex::update(const QSecureArray &a)
flag = true;
}
QByteArray out(a.size() / 2);
QByteArray out(a.size() / 2, 0);
int at = 0;
int c;
for(int n = 0; n < (int)a.size(); ++n)

@ -21,8 +21,7 @@
#include "qca_tools.h"
#include <qptrdict.h>
#include <qtextstream.h>
#include <QtCore>
#ifdef Q_OS_UNIX
# include <stdlib.h>
@ -60,7 +59,7 @@ static void add_mmap()
// Botan shouldn't throw any exceptions in our init/deinit.
static const Botan::SecureAllocator *alloc = 0;
static QPtrDict<int> *memtable = 0;
static QHash<void *, int> *memtable = 0;
bool botan_init(int prealloc, bool mmap)
{
@ -89,8 +88,7 @@ bool botan_init(int prealloc, bool mmap)
}
alloc = Botan::get_allocator("default");
memtable = new QPtrDict<int>;
memtable->setAutoDelete(true);
memtable = new QHash<void *, int>;
return secmem;
}
@ -120,16 +118,16 @@ void botan_secure_free(void *p, int bytes)
void *qca_secure_alloc(int bytes)
{
void *p = QCA::botan_secure_alloc(bytes);
QCA::memtable->insert(p, new int(bytes));
QCA::memtable->insert(p, bytes);
return p;
}
void qca_secure_free(void *p)
{
int *bytes = QCA::memtable->find(p);
if(bytes)
if(QCA::memtable->contains(p))
{
QCA::botan_secure_free(p, *bytes);
int bytes = QCA::memtable->value(p);
QCA::botan_secure_free(p, bytes);
QCA::memtable->remove(p);
}
}
@ -139,14 +137,48 @@ using namespace QCA;
//----------------------------------------------------------------------------
// QSecureArray
//----------------------------------------------------------------------------
class QSecureArray::Private
class QSecureArray::Private : public QSharedData
{
public:
Private(uint size) : buf((Botan::u32bit)size), refs(1) {}
Private(const Botan::SecureVector<Botan::byte> &a) : buf(a), refs(1) {}
Botan::SecureVector<Botan::byte> *buf;
Botan::SecureVector<Botan::byte> buf;
int refs;
Private()
{
buf = new Botan::SecureVector<Botan::byte>;
}
Private(uint size)
{
buf = new Botan::SecureVector<Botan::byte>((Botan::u32bit)size);
}
Private(const QByteArray &from)
{
buf = new Botan::SecureVector<Botan::byte>((Botan::u32bit)from.size());
Botan::byte *p = (Botan::byte *)(*buf);
memcpy(p, from.data(), from.size());
}
Private(const Private &from) : QSharedData(from)
{
buf = new Botan::SecureVector<Botan::byte>(*(from.buf));
}
~Private()
{
delete buf;
}
bool resize(int size)
{
Botan::SecureVector<Botan::byte> *new_buf = new Botan::SecureVector<Botan::byte>((Botan::u32bit)size);
Botan::byte *new_p = (Botan::byte *)(*new_buf);
const Botan::byte *old_p = (const Botan::byte *)(*buf);
memcpy(new_p, old_p, qMin(new_buf->size(), buf->size()));
delete buf;
buf = new_buf;
return true;
}
};
QSecureArray::QSecureArray()
@ -162,18 +194,21 @@ QSecureArray::QSecureArray(int size)
d = 0;
}
QSecureArray::QSecureArray(const char *str)
{
QByteArray a = QByteArray::fromRawData(str, strlen(str));
if(a.size() > 0)
d = new Private(a);
else
d = 0;
}
QSecureArray::QSecureArray(const QByteArray &a)
{
d = 0;
*this = a;
}
QSecureArray::QSecureArray(const QCString &cs)
{
d = 0;
*this = cs;
}
QSecureArray::QSecureArray(const QSecureArray &from)
{
d = 0;
@ -182,74 +217,49 @@ QSecureArray::QSecureArray(const QSecureArray &from)
QSecureArray::~QSecureArray()
{
reset();
}
void QSecureArray::reset()
{
if(d)
{
--d->refs;
if(d->refs == 0)
delete d;
d = 0;
}
}
void QSecureArray::fill(char fillChar, int fillToPosition)
{
detach();
if(!d)
return;
int len;
if ( (fillToPosition = -1)|| (fillToPosition > (int)size() ) ) {
len = size();
} else {
len = fillToPosition;
}
memset( d->buf, (int)fillChar, len );
}
QSecureArray & QSecureArray::operator=(const QSecureArray &from)
{
reset();
if(from.d)
{
d = from.d;
++d->refs;
}
d = from.d;
return *this;
}
QSecureArray & QSecureArray::operator=(const QByteArray &from)
{
reset();
d = 0;
if(!from.isEmpty())
{
d = new Private(from.size());
Botan::byte *p = (Botan::byte *)d->buf;
memcpy(p, from.data(), from.size());
}
d = new Private(from);
return *this;
}
QSecureArray & QSecureArray::operator=(const QCString &cs)
void QSecureArray::clear()
{
reset();
d = 0;
}
if(!cs.isEmpty())
bool QSecureArray::resize(int size)
{
int cur_size = (d ? d->buf->size() : 0);
if(cur_size == size)
return true;
if(size > 0)
{
int size = cs.length();
d = new Private(size);
Botan::byte *p = (Botan::byte *)d->buf;
memcpy(p, cs.data(), size);
if(d)
{
if(!d->resize(size))
return false;
}
else
d = new Private(size);
}
return *this;
else
{
d = 0;
}
return true;
}
char & QSecureArray::operator[](int index)
@ -262,37 +272,40 @@ const char & QSecureArray::operator[](int index) const
return at(index);
}
const char & QSecureArray::at(uint index) const
char & QSecureArray::at(int index)
{
return (char &)(*((Botan::byte *)d->buf + index));
return (char &)(*((Botan::byte *)(*d->buf) + index));
}
char & QSecureArray::at(uint index)
const char & QSecureArray::at(int index) const
{
detach();
return (char &)(*((Botan::byte *)d->buf + index));
return (const char &)(*((const Botan::byte *)(*d->buf) + index));
}
char *QSecureArray::data()
{
if(!d)
return 0;
Botan::byte *p = (Botan::byte *)(*d->buf);
return ((char *)p);
}
const char *QSecureArray::data() const
{
if(!d)
return 0;
const Botan::byte *p = (const Botan::byte *)d->buf;
const Botan::byte *p = (const Botan::byte *)(*d->buf);
return ((const char *)p);
}
char *QSecureArray::data()
const char *QSecureArray::constData() const
{
detach();
if(!d)
return 0;
Botan::byte *p = (Botan::byte *)d->buf;
return ((char *)p);
return data();
}
uint QSecureArray::size() const
int QSecureArray::size() const
{
return (d ? d->buf.size() : 0);
return (d ? d->buf->size() : 0);
}
bool QSecureArray::isEmpty() const
@ -300,76 +313,39 @@ bool QSecureArray::isEmpty() const
return (size() == 0);
}
bool QSecureArray::resize(uint size)
{
int cur_size = (d ? d->buf.size() : 0);
if(cur_size == (int)size)
return true;
detach();
if(size > 0)
{
Private *d2 = new Private(size);
Botan::byte *p2 = (Botan::byte *)d2->buf;
if(d)
{
Botan::byte *p = (Botan::byte *)d->buf;
memcpy(p2, p, QMIN((int)size, cur_size));
delete d;
}
d = d2;
}
else
{
delete d;
d = 0;
}
return true;
}
QSecureArray QSecureArray::copy() const
{
QSecureArray a = *this;
a.detach();
return a;
}
void QSecureArray::detach()
{
if(!d || d->refs <= 1)
return;
--d->refs;
d = new Private(d->buf);
}
QByteArray QSecureArray::toByteArray() const
{
if(isEmpty())
return QByteArray();
QByteArray buf(size());
QByteArray buf(size(), 0);
memcpy(buf.data(), data(), size());
return buf;
}
QSecureArray & QSecureArray::append(const QSecureArray &a)
{
int oldsize = size();
resize(oldsize + a.size());
memcpy(data() + oldsize, a.data(), a.size());
return *this;
}
void QSecureArray::fill(char fillChar, int fillToPosition)
{
int len = (fillToPosition == -1) ? size() : qMin(fillToPosition, size());
if(len > 0)
memset(data(), (int)fillChar, len);
}
void QSecureArray::set(const QSecureArray &from)
{
*this = from;
}
void QSecureArray::set(const QCString &cs)
void QSecureArray::set(const QByteArray &from)
{
*this = cs;
}
QSecureArray & QSecureArray::append(const QSecureArray &a)
{
detach();
int oldsize = size();
resize( oldsize + a.size() );
memcpy( data() + oldsize, a.data(), a.size() );
return *this;
*this = from;
}
bool operator==(const QSecureArray &a, const QSecureArray &b)
@ -409,7 +385,7 @@ static void negate_binary(char *a, int size)
}
}
class QBigInteger::Private
class QBigInteger::Private : public QSharedData
{
public:
Botan::BigInt n;
@ -455,12 +431,11 @@ QBigInteger::QBigInteger(const QBigInteger &from)
QBigInteger::~QBigInteger()
{
delete d;
}
QBigInteger & QBigInteger::operator=(const QBigInteger &from)
{
d->n = from.d->n;
d = from.d;
return *this;
}
@ -549,12 +524,11 @@ void QBigInteger::fromArray(const QSecureArray &_a)
QString QBigInteger::toString() const
{
QCString cs;
QByteArray cs;
try
{
QByteArray a(d->n.encoded_size(Botan::BigInt::Decimal));
Botan::BigInt::encode((Botan::byte *)a.data(), d->n, Botan::BigInt::Decimal);
cs = QCString(a.data(), a.size() + 1);
cs.resize(d->n.encoded_size(Botan::BigInt::Decimal));
Botan::BigInt::encode((Botan::byte *)cs.data(), d->n, Botan::BigInt::Decimal);
}
catch(std::exception &)
{
@ -572,7 +546,7 @@ bool QBigInteger::fromString(const QString &s)
{
if(s.isEmpty())
return false;
QCString cs = s.latin1();
QByteArray cs = s.toLatin1();
bool neg = false;
if(s[0] == '-')