File: RevocationList.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 (244 lines) | stat: -rw-r--r-- 5,916 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
/* vi: set sw=4 ts=4:
 *
 * Copyright (C) 2015 Christian Hohnstaedt.
 *
 * All rights reserved.
 */

#include <QRegularExpression>
#include <QRegularExpressionValidator>
#include "RevocationList.h"
#include "MainWindow.h"
#include "NewCrl.h"
#include "Help.h"
#include "lib/asn1int.h"
#include "lib/pki_x509.h"

enum revCol { Cnumber, Cserial, Cdate, Creason, CiDate, Cmax };

class revListItem : public QTreeWidgetItem
{
  public:
	revListItem(QTreeWidget *w) : QTreeWidgetItem(w) { };
	bool operator < (const QTreeWidgetItem &other) const
	{
		int col = treeWidget()->sortColumn();
		switch (col) {
		case Cserial: {
			return a1int(text(Cserial)) <
				a1int(other.text(Cserial));
		}
		case Cnumber:
			return text(Cnumber).toLong() <
				other.text(Cnumber).toLong();
		default:
			return QTreeWidgetItem::operator < (other);
		}
	}
};

static void setup_revRevItem(QTreeWidgetItem *item, const x509rev &revit,
			const pki_x509 *iss)
{
	pki_x509 *rev = iss ? iss->getBySerial(revit.getSerial()) : NULL;
	if (rev != NULL) {
		for (int i = 0; i < Cmax; i++)
			item->setToolTip(i, rev->getIntName());
	}
	item->setText(Cserial, revit.getSerial());
	item->setText(Cdate, revit.getDate().toSortable());
	item->setText(Creason, revit.getReason());

	item->setTextAlignment(Cnumber, Qt::AlignRight);
	item->setTextAlignment(Cserial, Qt::AlignRight);

	a1time a = revit.getInvalDate();
	if (!a.isUndefined())
		item->setText(CiDate, a.toSortable());
}

static void addRevItem(QTreeWidget *certList, const x509rev &revit,
			int no, const pki_x509 *iss)
{
	revListItem *current;
	current = new revListItem(certList);
	current->setText(Cnumber, QString("%1").arg(no));
	setup_revRevItem(current, revit, iss);
}

void RevocationList::setupRevocationView(QTreeWidget *certList,
			const x509revList &revList, const pki_x509 *iss)
{
	QStringList sl;
	int cols, i;

	certList->clear();

	sl << tr("No.") << tr("Serial") << tr("Revocation") << tr("Reason") <<
		tr("Invalidation");

	cols = sl.size();
	certList->setColumnCount(cols);
	certList->setHeaderLabels(sl);
	certList->setItemsExpandable(false);
	certList->setRootIsDecorated(false);
	certList->sortItems(Cnumber, Qt::AscendingOrder);

	i=1;
	foreach(x509rev revit, revList) {
		addRevItem(certList, revit, i++, iss);
	}
	for (i=0; i<cols; i++)
		certList->resizeColumnToContents(i);
	certList->setSortingEnabled(true);
	certList->setSelectionBehavior(QAbstractItemView::SelectRows);
	certList->setSelectionMode(QAbstractItemView::ExtendedSelection);
}

RevocationList::RevocationList(QWidget *w)
	: QDialog(w && w->isVisible() ? w : nullptr)
{
	QPushButton *genCrl;
	setupUi(this);
	setWindowTitle(XCA_TITLE);
	image->setPixmap(QPixmap(":revImg"));
	mainwin->helpdlg->register_ctxhelp_button(this, "crlmanage");

	genCrl = buttonBox->addButton(tr("Generate CRL"),
				QDialogButtonBox::ActionRole);

	connect(genCrl, SIGNAL(clicked()), this, SLOT(gencrl()));
}

void RevocationList::gencrl()
{
	issuer->setRevocations(getRevList());
	NewCrl::newCrl(this, issuer);
}

void RevocationList::setRevList(const x509revList &rl, pki_x509 *iss)
{
	issuer = iss;
	revList = rl;
	setupRevocationView(certList, revList, issuer);
}

const x509revList &RevocationList::getRevList()
{
	return revList;
}

void RevocationList::on_addRev_clicked()
{
	Revocation *revoke = new Revocation(QModelIndexList(), this);
	if (revoke->exec()) {
		x509rev revit = revoke->getRevocation();
		revList << revit;
		addRevItem(certList, revit, revList.size(), issuer);
	}
}

void RevocationList::on_delRev_clicked()
{
	QTreeWidgetItem *current = certList->currentItem();
	x509rev rev;
	int idx;

	if (!current)
		return;
	idx = certList->indexOfTopLevelItem(current);
	certList->takeTopLevelItem(idx);
	rev.setSerial(a1int(current->text(Cserial)));
	idx = revList.indexOf(rev);
	if (idx != -1)
		revList.takeAt(idx);
}

void RevocationList::on_editRev_clicked()
{
	on_certList_itemDoubleClicked(certList->currentItem());
}

void RevocationList::on_certList_itemDoubleClicked(QTreeWidgetItem *current)
{
	x509rev rev;
	int idx;

	if (!current)
		return;

	rev.setSerial(a1int(current->text(Cserial)));
	idx = revList.indexOf(rev);
	if (idx == -1)
		return;

	rev = revList[idx];

	Revocation *revoke = new Revocation(QModelIndexList(), this);
	revoke->setRevocation(rev);
	if (revoke->exec()) {
		a1time a1 = rev.getDate();
		rev = revoke->getRevocation();
		rev.setDate(a1);
		revList[idx] = rev;
		setup_revRevItem(current, rev, issuer);
	}
	delete revoke;
}

Revocation::Revocation(QModelIndexList indexes, QWidget *w) : QDialog(w ? w : mainwin)
{
	setupUi(this);
	setWindowTitle(XCA_TITLE);
	mainwin->helpdlg->register_ctxhelp_button(this, "crlrevocation");

	reason->addItems(x509rev::crlreasons());
	invalid->setNow();

	if (indexes.size() > 1) {
		QList<a1int> serials;
		QStringList sl;
		serial->setText(QString("Batch revocation of %1 Certificates").
				arg(indexes.size()));
		foreach(QModelIndex idx, indexes) {
			pki_x509 *cert = db_base::fromIndex<pki_x509>(idx);
			if (cert)
				serials << cert->getSerial();
		}
		std::sort(serials.begin(), serials.end());
		foreach(a1int a, serials)
			sl << a;
		serial->setToolTip(sl.join("\n"));
		serial->setEnabled(false);
	} else if (indexes.size() == 1) {
		pki_x509 *cert = db_base::fromIndex<pki_x509>(indexes[0]);
		serial->setText(cert->getSerial());
		serial->setEnabled(false);
	} else {
		serial->setValidator(
			new QRegularExpressionValidator(
					QRegularExpression("[A-Fa-f0-9]+"), serial));
	}
}

x509rev Revocation::getRevocation()
{
	x509rev r;

	r.setSerial(a1int(serial->text()));
	r.setInvalDate(invalid->getDate());
	r.setDate(a1time());
	r.setCrlNo(0);
	r.setReason(reason->currentText());
	return r;
}

void Revocation::setRevocation(x509rev r)
{
	serial->setText(r.getSerial());
	invalid->setDate(r.getInvalDate());
	int i = reason->findText(r.getReason());
	if (i == -1)
		i = 0;
	reason->setCurrentIndex(i);
}