File: CertDetail.cpp

package info (click to toggle)
xca 2.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,328 kB
  • sloc: cpp: 30,584; sh: 341; xml: 74; makefile: 56; python: 34
file content (331 lines) | stat: -rw-r--r-- 8,391 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/* vi: set sw=4 ts=4:
 *
 * Copyright (C) 2001 - 2014 Christian Hohnstaedt.
 *
 * All rights reserved.
 */


#include "CertDetail.h"
#include "KeyDetail.h"
#include "MainWindow.h"
#include "distname.h"
#include "clicklabel.h"
#include "Help.h"
#include "OidResolver.h"
#include "lib/func_base.h"
#include "lib/func.h"
#include "lib/XcaWarningCore.h"
#include <QLabel>
#include <QPushButton>
#include <QLineEdit>
#include <QMessageBox>

CertDetail::CertDetail(QWidget *w) : XcaDetail(w)
{
	setupUi(this);
	tabwidget->setCurrentIndex(0);
}

void CertDetail::on_showExt_clicked()
{
	if (showConf) {
		showConf = false;
		v3extensions->document()->setHtml(exts);
		showExt->setText(tr("Show config"));
	} else {
		showConf = true;
		v3extensions->document()->setPlainText(conf);
		showExt->setText(tr("Show extensions"));
	}

}

int CertDetail::tabIndexByName(const QString &tabname) const
{
	for (int i=0; i<tabwidget->count(); i++) {
		if (tabwidget->widget(i)->objectName() == tabname)
			return i;
	}
	return -1;
}

void CertDetail::setX509super(pki_x509super *x)
{
	description->setText(x->getIntName());
	thisSqlId = x->getSqlItemId();

	connect_pki(x);
	// examine the key
	myPubKey = x->getRefKey();
	if (myPubKey) {
		privKey->setText(myPubKey->getIntName());
		privKey->setClickText(myPubKey->getSqlItemId().toString());
		if (myPubKey->isPrivKey()) {
			privKey->setGreen();
		} else {
			privKey->setRed();
		}
	} else {
		tmpPubKey = myPubKey = x->getPubKey();
		privKey->setText(tr("Show public key"));
		privKey->setRed();
		myPubKey->setIntName(x->getIntName());
		myPubKey->setComment(tr("This key is not in the database."));
	}

	if (!myPubKey) {
		privKey->setText(tr("Not available"));
		privKey->setDisabled(true);
		privKey->disableToolTip();
	} else {
		keySqlId = myPubKey->getSqlItemId();
	}
	connect(privKey, SIGNAL(doubleClicked(QString)),
		this, SLOT(showPubKey()));

	// details of the subject
	subject->setX509name(x->getSubject());

	// V3 extensions
	extList el = x->getV3ext();
	if (el.count() == 0) {
		tabwidget->removeTab(tabIndexByName("extensionsTab"));
		tabwidget->removeTab(tabIndexByName("validationTab"));
	} else {
		exts = el.getHtml("<br>");
		el.genGenericConf(&conf);
		v3extensions->document()->setHtml(exts);
	}

	// Algorithm
	sigAlgo->setText(x->getSigAlg());
	connect(sigAlgo, SIGNAL(doubleClicked(QString)),
		MainWindow::getResolver(), SLOT(searchOid(QString)));

	// Comment
	comment->setPlainText(x->getComment());

	setCert(dynamic_cast<pki_x509*>(x));
	setReq(dynamic_cast<pki_x509req*>(x));
}

void CertDetail::setCert(pki_x509 *cert)
{
	if (!cert)
		return;
	QList<int> errors;
	init("certdetail", ":certImg");
	errors = cert->ossl_verify();
	QString html;
	for (int err : errors) {
		html += QString("<li><b><u>%1:</u></b><br/>%2</li>\n")
					.arg(get_ossl_verify_error(err))
					.arg(X509_verify_cert_error_string(err));
	}
	if (html.isEmpty())
		html = tr("No verification errors found.");
	else
		html = "<ul>\n" + html + "</ul>\n";

	validation->setHtml(html);
	QList<X509_PURPOSE*> purposes = cert->purposes();
	for (X509_PURPOSE *purp : purposes) {
		QString purpname = X509_PURPOSE_get0_name(purp);
		int id = X509_PURPOSE_get_id(purp);
		qDebug() << "Purpose: " << purpname << " (" << id << ")";
		purposeList->addItem(QString("%1 (%2)").arg(purpname).arg(id));
	}
	headerLabel->setText(tr("Details of the Certificate"));
	try {
		// No attributes
		tabwidget->removeTab(tabIndexByName("attributesTab"));
		if (cert->isCA()) {
			tabwidget->removeTab(tabIndexByName("validationTab"));
		} else {
			if (errors.size() > 0)
				tabwidget->tabBar()->setTabTextColor(tabIndexByName("validationTab"),Qt::red);
		}

		// examine the signature
		if (cert->getSigner() == NULL) {
			signature->setText(tr("Signer unknown"));
			signature->setDisabled(true);
			signature->disableToolTip();
		} else if (cert == cert->getSigner())  {
			signature->setText(tr("Self signed"));
			signature->setGreen();
			signature->disableToolTip();
		} else {
			pki_x509 *issuer = cert->getSigner();
			signature->setText(issuer->getIntName());
			signature->setClickText(issuer->getSqlItemId().toString());
			signature->setGreen();
			issuerSqlId = issuer->getSqlItemId();

			connect(signature, SIGNAL(doubleClicked(QString)),
				this, SLOT(showIssuer()));
		}

		// the serial
		serialNr->setText(cert->getSerial());

		// details of the issuer
		issuer->setX509name(cert->getIssuerName());

		// The dates
		notBefore->setText(cert->getNotBefore().toPretty());
		notBefore->setToolTip(cert->getNotBefore().toPrettyGMT());
		notAfter->setText(cert->getNotAfter().toPretty());
		notAfter->setToolTip(cert->getNotAfter().toPrettyGMT());

		// validation of the Date
		dateValid->disableToolTip();
		if (cert->isRevoked()) {
			x509rev rev = cert->getRevocation();
			dateValid->setText(tr("Revoked at %1")
				.arg(rev.getDate().toPretty()));
			dateValid->setRed();
			dateValid->setToolTip(rev.getDate().toPrettyGMT());
		} else if (!cert->checkDate()) {
			dateValid->setText(tr("Not valid"));
			dateValid->setRed();
		} else {
			dateValid->setGreen();
			dateValid->setText(tr("Valid"));
		}
		// the fingerprints
		fpMD5->setText(cert->fingerprint(EVP_md5()));
		fpSHA1->setText(cert->fingerprint(EVP_sha1()));
		QString fp = cert->fingerprint(EVP_sha256());
		int x = fp.size() / 2;
		fp = fp.mid(0,x) + "\n" + fp.mid(x+1, -1);
		fpSHA256->setText(fp);

		openssl_error();
	} catch (errorEx &err) {
		XCA_WARN(err.getString());
	}
}

void CertDetail::setReq(pki_x509req *req)
{
	if (!req)
		return;
	init("csrdetail", ":csrImg");
	headerLabel->setText(tr("Details of the certificate signing request"));
	try {
		// No issuer
		tabwidget->removeTab(tabIndexByName("issuerTab"));
		tabwidget->removeTab(tabIndexByName("validationTab"));

		// verification
		if (!req->verify() ) {
			signature->setRed();
			signature->setText("Failed");
		} else {
			signature->setGreen();
			signature->setText("PKCS#10");
		}
		signature->disableToolTip();
		fingerprints->hide();
		validity->hide();
		serialLabel->hide();
		serialNr->hide();

		// The non extension attributes
		int cnt = X509_REQ_get_attr_count(req->getReq());
		int added = 0;
		QGridLayout *attrLayout = new QGridLayout(attributes);
		attrLayout->setAlignment(Qt::AlignTop);
		attrLayout->setSpacing(6);
		attrLayout->setContentsMargins(11, 11, 11, 11);

		for (int i = 0, ii = 0; i<cnt; i++) {
			int nid;
			QLabel *label;
			QString trans;
			X509_ATTRIBUTE *att = X509_REQ_get_attr(req->getReq(), i);

			nid = OBJ_obj2nid(X509_ATTRIBUTE_get0_object(att));

			if (X509_REQ_extension_nid(nid)) {
				continue;
			}
			label = new QLabel(this);
			trans = dn_translations[nid];
			if (Settings["translate_dn"] && !trans.isEmpty()) {
				label->setText(trans);
				label->setToolTip(QString(OBJ_nid2sn(nid)));
			} else {
				label->setText(QString(OBJ_nid2ln(nid)));
				label->setToolTip(trans);
			}

			label->setText(QString(OBJ_nid2ln(nid)));
			label->setToolTip(QString(OBJ_nid2sn(nid)));
			attrLayout->addWidget(label, ii, 0);
			added++;

			int count = X509_ATTRIBUTE_count(att);
			for (int j=0; j<count; j++) {
				ASN1_TYPE *at = X509_ATTRIBUTE_get0_type(att, j);
				label = labelFromAsn1String(at->value.asn1_string);
				attrLayout->addWidget(label, ii, j +1);
			}
			ii++;
		}
		if (!added) {
			tabwidget->removeTab(tabIndexByName("attributesTab"));
		}
		openssl_error();
	} catch (errorEx &err) {
		XCA_WARN(err.getString());
	}
}

QLabel *CertDetail::labelFromAsn1String(ASN1_STRING *s)
{
	QLabel *label;
	label = new CopyLabel(this);
	label->setText(asn1ToQString(s));
	label->setToolTip(QString(ASN1_tag2str(s->type)));
	return label;
}

void CertDetail::itemChanged(pki_base *pki)
{
	QVariant pkiSqlId = pki->getSqlItemId();

	if (pkiSqlId == keySqlId)
		privKey->setText(pki->getIntName());
	if (pkiSqlId == issuerSqlId)
		signature->setText(pki->getIntName());
	if (pkiSqlId == thisSqlId)
		description->setText(pki->getIntName());
}

void CertDetail::showPubKey()
{
	KeyDetail::showKey(this, myPubKey, keySqlId.isValid());
}

void CertDetail::showIssuer()
{
	showCert(this, Store.lookupPki<pki_x509>(issuerSqlId));
}

void CertDetail::showCert(QWidget *parent, pki_x509super *x)
{
	if (!x)
		return;
	CertDetail *dlg = new CertDetail(parent);
	dlg->setX509super(x);
	dlg->exec();
	delete dlg;
}

CertDetail::~CertDetail()
{
	delete tmpPubKey;
}