aheinecke@21: #include "certificate.h" aheinecke@82: #include andre@186: #include aheinecke@21: #include aheinecke@21: aheinecke@94: #include aheinecke@94: andre@186: #define POLARSSL_INFO_BUF_SIZE 2000 andre@186: aheinecke@83: Certificate::Certificate(const QString& b64Line) : andre@186: mValid(false) aheinecke@81: { aheinecke@82: int ret = -1; andre@186: char buf[POLARSSL_INFO_BUF_SIZE]; andre@186: aheinecke@94: x509_crt x509cert; aheinecke@81: andre@204: if (b64Line.isEmpty()) { andre@204: return; andre@204: } andre@204: aheinecke@83: /* Cut of the first two chars (e.g. I: and decode) */ aheinecke@83: QByteArray asn1data = QByteArray::fromBase64( aheinecke@83: b64Line.right(b64Line.size() - 2).toLatin1()); aheinecke@83: aheinecke@94: x509_crt_init(&x509cert); aheinecke@94: ret = x509_crt_parse(&x509cert, aheinecke@82: (const unsigned char*) asn1data.constData(), aheinecke@82: asn1data.size()); aheinecke@82: if (ret != 0) { aheinecke@82: qDebug() << "Parsing certificate failed with error: " << ret; andre@204: qDebug() << "Failed cert: " << asn1data.toBase64(); aheinecke@94: x509_crt_free(&x509cert); aheinecke@82: return; aheinecke@82: } aheinecke@82: andre@186: /* Get a full details string */ andre@186: ret = x509_crt_info(buf, POLARSSL_INFO_BUF_SIZE, "", &x509cert); aheinecke@82: aheinecke@82: if (ret <= 0) { aheinecke@82: qDebug() << "Getting certificate info failed with error: " << ret; aheinecke@82: return; aheinecke@82: } aheinecke@82: aheinecke@82: /* In case of success the return value is the size of the information andre@186: * written into buf */ aheinecke@83: mDetails = QString::fromUtf8(buf, ret); aheinecke@83: andre@186: /* Get the subject */ andre@186: ret = x509_dn_gets(buf, POLARSSL_INFO_BUF_SIZE, &(x509cert.subject)); andre@186: andre@186: if (ret <= 0) { andre@186: qDebug() << "Getting certificate subject failed with error: " << ret; andre@186: return; andre@186: } andre@186: andre@186: /* TODO check that all asn encodings are handled */ andre@186: QString subject = QString::fromUtf8(buf, ret); andre@186: andre@186: /* TODO check that escaped , are not possible */ andre@186: QStringList attrs = subject.split(", "); andre@186: andre@186: foreach (const QString& attr, attrs) { andre@186: QStringList kv = attr.split("="); andre@186: if (kv.size() != 2) { andre@186: qDebug() << "Failed to parse subject element: " << attr; andre@186: continue; andre@186: } andre@186: mSubjectAttrs.insert(kv[0], kv[1]); andre@186: } andre@186: andre@186: /* For more information to get from a x509_crt see andre@186: * https://polarssl.org/api/x509_8h.html */ andre@186: andre@186: x509_crt_free(&x509cert); aheinecke@82: aheinecke@82: mValid = true; aheinecke@83: aheinecke@83: mBaseLine = b64Line; aheinecke@81: } andre@186: andre@186: QString Certificate::getSubjectAttr (const QString& attrName) const { andre@186: return mSubjectAttrs.value(attrName); andre@186: } andre@186: andre@186: QString Certificate::shortDescription() const { andre@186: return getSubjectAttr("CN"); andre@186: }