From ff1dc7b203b8decb7fd320249a7bf17b42198983 Mon Sep 17 00:00:00 2001 From: chris2511 Date: Tue, 25 Nov 2003 15:56:46 +0000 Subject: [PATCH] ASN1_UTF8STRING support and UNICODE support in x509name --- lib/pki_x509req.cpp | 2 + lib/x509name.cpp | 86 ++++++++++++++++++++++++++++++++++--------- lib/x509name.h | 1 + widgets/ReqDetail.cpp | 7 +++- 4 files changed, 77 insertions(+), 19 deletions(-) diff --git a/lib/pki_x509req.cpp b/lib/pki_x509req.cpp index 78432192..560f0baf 100644 --- a/lib/pki_x509req.cpp +++ b/lib/pki_x509req.cpp @@ -91,6 +91,8 @@ pki_x509req::~pki_x509req() { if (request) X509_REQ_free(request); + if (spki) + NETSCAPE_SPKI_free(spki); openssl_error(); } diff --git a/lib/x509name.cpp b/lib/x509name.cpp index 34bf699a..ecfd72ae 100644 --- a/lib/x509name.cpp +++ b/lib/x509name.cpp @@ -92,30 +92,34 @@ QString x509name::oneLine() const QString x509name::getEntryByNid(int nid) const { - QString s; - int len = X509_NAME_get_text_by_NID(xn, nid, NULL, 0) + 1; - if (len < 2) - return s; - char *buf = (char *)OPENSSL_malloc(len); - X509_NAME_get_text_by_NID(xn, nid, buf, len); - s = buf; - OPENSSL_free(buf); - return s; + ASN1_OBJECT *obj; + obj=OBJ_nid2obj(nid); + if (obj == NULL) return QString::null; + int i=X509_NAME_get_index_by_OBJ(xn,obj,-1); + if (i < 0) return QString::null; + return getEntry(i); } QString x509name::getEntry(int i) const { QString s; - char c; ASN1_STRING *d; if ( i<0 || i>entryCount() ) return s; d = X509_NAME_ENTRY_get_data(X509_NAME_get_entry(xn,i)); - i = d->length - 1; - c = d->data[i]; // replace the last char by \0 - d->data[i] = '\0'; - s = (char *)d->data; // strcopy the data - d->data[i] = c; // recover last char - return s + c; + + QString ret; + + if (d->type == V_ASN1_BMPSTRING) { + for (int i=0;ilength;i+=2) + ret+=QChar((unsigned short)d->data[i]*256+ + (unsigned short)d->data[i+1] ); + } + else if (d->type == V_ASN1_UTF8STRING) + ret=QString::fromUtf8((const char *)d->data,d->length); + else + ret=QString::fromLatin1((const char *)d->data,d->length); + + return ret; } void x509name::delEntry(int i) @@ -172,11 +176,57 @@ int x509name::entryCount() const return X509_NAME_entry_count(xn); } +int x509name::getNidByName(const QString &nid_name) +{ + return OBJ_txt2nid((char*)nid_name.latin1()); +} + +static int fix_data(int nid, int *type) +{ + if (nid == NID_pkcs9_emailAddress) + *type=V_ASN1_IA5STRING; + if ((nid == NID_commonName) && (*type == V_ASN1_IA5STRING)) + *type=V_ASN1_T61STRING; + if ((nid == NID_pkcs9_challengePassword) && (*type == V_ASN1_IA5STRING)) + *type=V_ASN1_T61STRING; + if ((nid == NID_pkcs9_unstructuredName) && (*type == V_ASN1_T61STRING)) + return(0); + if (nid == NID_pkcs9_unstructuredName) + *type=V_ASN1_IA5STRING; + return 1; +} + void x509name::addEntryByNid(int nid, const QString entry) { if (entry.isEmpty()) return; - X509_NAME_add_entry_by_NID(xn, nid, - MBSTRING_ASC, (unsigned char*)entry.latin1(),-1,-1,0); + + // check for a UNICODE-String. + bool need_uc=false; + + for (unsigned i=0;i127) { need_uc=true; break; } + + if (need_uc) { + unsigned char *data = (unsigned char *)OPENSSL_malloc(entry.length()*2); + + for (unsigned i=0;i> 8; + data[2*i+1] = entry[i].unicode() & 0xff; + } + + X509_NAME_add_entry_by_NID(xn, nid, V_ASN1_BMPSTRING, + data,entry.length()*2,-1,0); + OPENSSL_free(data); + } + else { + int type=ASN1_PRINTABLE_type((unsigned char *)entry.latin1(),-1); + + if (fix_data(nid, &type) == 0) + return; + + X509_NAME_add_entry_by_NID(xn, nid, type, + (unsigned char*)entry.latin1(),-1,-1,0); + } } X509_NAME *x509name::get() const diff --git a/lib/x509name.h b/lib/x509name.h index 44bc8c9b..3e7f3f7e 100644 --- a/lib/x509name.h +++ b/lib/x509name.h @@ -76,6 +76,7 @@ class x509name int entryCount() const; x509name &operator = (const x509name &x); bool operator == (const x509name &x) const; + static int getNidByName(const QString &nid_name); void addEntryByNid(int nid, const QString entry); void delEntry(int i); X509_NAME *get() const; diff --git a/widgets/ReqDetail.cpp b/widgets/ReqDetail.cpp index 341643ef..1b8f4b7b 100644 --- a/widgets/ReqDetail.cpp +++ b/widgets/ReqDetail.cpp @@ -76,7 +76,12 @@ void ReqDetail::setReq(pki_x509req *req) } else { verify->setGreen(); - verify->setText("Ok"); + if (req->isSpki()) { + verify->setText("SPKAC"); + } + else { + verify->setText("PKCS#10"); + } } // look for the private key pki_key *key =req->getRefKey();