fix string conversion from QString to ASN1

Synopsis: When using special chars in the DN entries
           they sometimes got corrupted.

 - the toUtf8() method returned a temporary QByteArray
   and the pointer to its memory became invalid befor using it.

 - also checked other invalid uses of temporary QByteArray objects
This commit is contained in:
Christian Hohnstaedt 2010-01-04 20:17:09 +01:00
parent 9ba0dcfa89
commit 766648be49
5 changed files with 10 additions and 21 deletions

View File

@ -120,8 +120,7 @@ QString a1time::toSortable() const
a1time &a1time::set(const QString &s)
{
const char *x = s.toAscii();
ASN1_GENERALIZEDTIME_set_string(time, (char*)x);
ASN1_GENERALIZEDTIME_set_string(time, s.toAscii());
return *this;
}

View File

@ -161,19 +161,6 @@ QByteArray filename2bytearray(const QString &fname)
#endif
}
const char *QString2filename(const QString &fname)
{
static char buf[4096];
QByteArray b = filename2bytearray(fname);
int l = b.size();
if (l>=4096)
l = 4095;
memcpy(buf, b.constData(), l);
buf[l] = 0;
return buf;
}
QString filename2QString(const char *fname)
{
#ifdef WIN32
@ -237,14 +224,15 @@ QString asn1ToQString(const ASN1_STRING *str)
default:
qs = QString::fromLatin1((const char*)str->data, str->length);
}
//printf("Convert %s string to '%s'\n", ASN1_tag2str(str->type),CCHAR(qs));
//printf("Convert %s string to '%s' len %d\n", ASN1_tag2str(str->type),CCHAR(qs), str->length);
return qs;
}
/* returns an encoded ASN1 string from QString for a special nid*/
ASN1_STRING *QStringToAsn1(const QString s, int nid)
{
const unsigned char *utf8 = (const unsigned char *)s.toUtf8().constData();
QByteArray ba = s.toUtf8();
const unsigned char *utf8 = (const unsigned char *)ba.constData();
return ASN1_STRING_set_by_NID(NULL, utf8, -1, MBSTRING_UTF8, nid);
}

View File

@ -22,7 +22,6 @@ QString getUserSettingsDir();
QString getFullFilename(const QString &filename, const QString &selectedFilter);
QByteArray filename2bytearray(const QString &fname);
const char *QString2filename(const QString &fname);
QString filename2QString(const char *fname);
void applyTD(QWidget *parent, int number, int range, bool mnc,
@ -37,4 +36,5 @@ QString changeFilenameSuffix(QString fn, const QStringList &suffixlist,
int selected);
bool mayWriteFile(const QString &fname);
#define QString2filename(str) filename2bytearray(str).constData()
#endif

View File

@ -93,6 +93,10 @@ public:
{
setValue(v, len);
}
pk11_attr_data(unsigned long type, QByteArray ba) :pk11_attribute(type)
{
setValue((const unsigned char *)ba.constData(), ba.size());
}
unsigned long getValue(const unsigned char **ptr)
{
*ptr = (unsigned char*)attr.pValue;

View File

@ -209,7 +209,6 @@ void pki_x509::store_token()
pki_scard *card = (pki_scard *)privkey;
int slot, size, id_size;
unsigned char *p, *p1, *id;
const unsigned char *label;
QList<CK_OBJECT_HANDLE> objects;
if (!privkey || !privkey->isScard())
@ -225,7 +224,6 @@ void pki_x509::store_token()
id_size = card->getIdBin(&id);
openssl_error();
label = (const unsigned char *)desc.toUtf8().constData();
pk11_attlist p11_atts;
p11_atts <<
@ -234,7 +232,7 @@ void pki_x509::store_token()
pk11_attr_bool(CKA_TOKEN, true) <<
pk11_attr_data(CKA_VALUE, p, size) <<
pk11_attr_data(CKA_ID, id, id_size) <<
pk11_attr_data(CKA_LABEL, label, strlen((const char*)label));
pk11_attr_data(CKA_LABEL, desc.toUtf8());
free(p);
free(id);