1 /* vi: set sw=4 ts=4:
2  *
3  * Copyright (C) 2010 - 2015 Christian Hohnstaedt.
4  *
5  * All rights reserved.
6  */
7 
8 #ifndef __HEADERLIST_H
9 #define __HEADERLIST_H
10 
11 #include <QString>
12 #include <QList>
13 #include <QAction>
14 #include <QHeaderView>
15 #include <QDebug>
16 #include <openssl/objects.h>
17 #include "db.h"
18 #include "settings.h"
19 #include "func.h"
20 
21 #define HD_undef NID_undef
22 #define HD_internal_name -2
23 #define HD_subject_name -3
24 #define HD_subject_hash -4
25 #define HD_x509key_name -5
26 #define HD_counter -6
27 #define HD_x509_sigalg -7
28 #define HD_creation -8
29 #define HD_comment -9
30 #define HD_source -100
31 #define HD_primary_key -101
32 
33 #define HD_cert_serial -10
34 #define HD_cert_notBefore -11
35 #define HD_cert_notAfter -12
36 //#define HD_cert_trust -13
37 #define HD_cert_revocation -14
38 #define HD_cert_ca	 -15
39 #define HD_cert_md5fp	 -16
40 #define HD_cert_sha1fp   -17
41 #define HD_cert_sha256fp -18
42 #define HD_cert_crl_expire -19
43 
44 #define HD_req_signed -20
45 #define HD_req_unstr_name -21
46 #define HD_req_chall_pass -22
47 #define HD_req_certs -23
48 //#define HD_temp_type -30
49 
50 #define HD_crl_signer -40
51 #define HD_crl_revoked -42
52 #define HD_crl_lastUpdate -43
53 #define HD_crl_nextUpdate -44
54 #define HD_crl_crlnumber  -45
55 
56 #define HD_key_type -50
57 #define HD_key_size -51
58 #define HD_key_use  -52
59 #define HD_key_passwd -53
60 #define HD_key_curve -54
61 
62 class dbheader
63 {
64     protected:
init()65 	void init()
66 	{
67 		id = HD_undef;
68 		action = NULL;
69 		show = showDefault = false;
70 		size = -1;
71 		visualIndex = -1;
72 		sortIndicator = -1;
73 		type = hd_default;
74 	}
75 	QString name, tooltip;
76 
77     public:
78 	enum hdr_type {
79 		hd_default,
80 		hd_x509name,
81 		hd_v3ext,
82 		hd_v3ext_ns,
83 		hd_number,
84 		hd_asn1time,
85 		hd_key,
86 	};
87 	int id;
88 	bool show;
89 	bool showDefault;
getName()90 	virtual QString getName() { return name; }
getTooltip()91 	virtual QString getTooltip() { return tooltip; }
92 	QAction *action;
93 	int size;
94 	int visualIndex;
95 	int sortIndicator;
96 	enum hdr_type type;
97 
98 	dbheader(QString aname = QString())
99 	{
100 		init();
101 		name = aname;
102 	}
103 	dbheader(int aid, bool ashow = false,
104 		QString aname = QString(), QString atip = QString())
105 	{
106 		init();
107 		id = aid;
108 		name = aname;
109 		tooltip = atip;
110 		show = showDefault = ashow;
111 	}
~dbheader()112 	virtual ~dbheader() { }
113 
mustSave()114 	bool mustSave()
115 	{
116 		return  size != -1 ||
117 			visualIndex != -1 ||
118 			sortIndicator != -1 ||
119 			show != showDefault;
120 	}
121 	bool operator == (const dbheader *h) const
122 	{
123 		if (h->id == HD_undef)
124 			return name == h->name;
125 		return id == h->id;
126 	}
isNumeric()127 	bool isNumeric()
128 	{
129 		switch (id) {
130 		case NID_subject_key_identifier:
131 		case NID_authority_key_identifier:
132 		case HD_key_size:
133 			return true;
134 		}
135 		return type == hd_number;
136 	}
toData()137 	QString toData()
138 	{
139 		QStringList sl; sl
140 		<< QString::number(visualIndex)
141 		<< QString::number(sortIndicator)
142 		<< QString::number(size)
143 		<< QString::number(show);
144 		return sl.join(" ");
145 	}
fromData(QString s)146 	void fromData(QString s)
147 	{
148 		QStringList sl = s.split(" ");
149 		if (sl.count() != 4) {
150 			qCritical() << "Invalid header data for" <<
151 					id << name << s;
152 			return;
153 		}
154 		visualIndex = sl[0].toInt();
155 		if (visualIndex < -1)
156 			visualIndex = -1;
157 		sortIndicator = sl[1].toInt();
158 		if (sortIndicator != Qt::AscendingOrder &&
159 		    sortIndicator != Qt::DescendingOrder)
160 			sortIndicator = -1;
161 		size = sl[2].toInt();
162 		if (size == 0)
163 			size = -1;
164 		show = sl[3].toInt();
165 	}
setupHeaderView(int sect,QHeaderView * hv)166 	void setupHeaderView(int sect, QHeaderView *hv)
167 	{
168 		hv->setSectionHidden(sect, !show);
169 		if (size != -1)
170 			hv->resizeSection(sect, size);
171 		if (sortIndicator != -1) {
172 			hv->setSortIndicator(sect, sortIndicator ?
173 				Qt::DescendingOrder : Qt::AscendingOrder);
174 		}
175 	}
reset()176 	void reset()
177 	{
178 		action = NULL;
179 		show = showDefault;
180 		size = -1;
181 		visualIndex = -1;
182 		sortIndicator = -1;
183 	}
184 };
185 
186 class nid_dbheader : public dbheader
187 {
188     private:
189 	QString sn;
190 
191     public:
nid_dbheader(int aid,enum hdr_type atype)192 	nid_dbheader(int aid, enum hdr_type atype)
193 		: dbheader(aid, aid == NID_commonName)
194 	{
195 		type = atype;
196 		tooltip = dn_translations[id];
197 		name = OBJ_nid2ln(id);
198 		sn = OBJ_nid2sn(id);
199 		if (tooltip.isEmpty())
200 			tooltip = name;
201 	}
getName()202 	QString getName()
203 	{
204 		return Settings["translate_dn"] ? tooltip : name;
205 	}
getTooltip()206 	QString getTooltip()
207 	{
208 		return QString("[%1] %2").arg(sn)
209 			.arg(Settings["translate_dn"] ? name : tooltip);
210 	}
211 };
212 
213 class num_dbheader : public dbheader
214 {
215     public:
216 	num_dbheader(int aid, bool ashow = false,
217 		QString aname = QString(), QString atip = QString())
dbheader(aid,ashow,aname,atip)218 		: dbheader(aid, ashow, aname, atip)
219 	{
220 		type = hd_number;
221 	}
222 };
223 
224 class date_dbheader : public dbheader
225 {
226     public:
227 	date_dbheader(int aid, bool ashow = false,
228 		QString aname = QString(), QString atip = QString())
dbheader(aid,ashow,aname,atip)229 		: dbheader(aid, ashow, aname, atip)
230 	{
231 		type = hd_asn1time;
232 	}
233 };
234 
235 class key_dbheader : public dbheader
236 {
237     public:
key_dbheader(int aid,QString aname)238 	key_dbheader(int aid, QString aname)
239 		: dbheader(aid, false, aname)
240 	{
241 		type = hd_key;
242 	}
243 };
244 
245 class dbheaderList: public QList<dbheader*>
246 {
247     public:
248 
dbheaderList(dbheader * h)249 	dbheaderList(dbheader *h) :QList<dbheader*>() {
250 		append(h);
251 	}
dbheaderList()252 	dbheaderList() :QList<dbheader*>() {
253 	}
toData()254 	QString toData()
255 	{
256 		QStringList sl;
257 		for (int i=0; i<count(); i++) {
258 			QStringList seq;
259 			dbheader *h = at(i);
260 			if (!h->mustSave())
261 				continue;
262 			seq << QString("%1").arg(h->id);
263 			if (h->id > 0) {
264 				seq << OBJ_obj2QString(
265 					OBJ_nid2obj(h->id), 1);
266 			}
267 			seq << h->toData();
268 			sl << seq.join(":");
269 		}
270 		return sl.join(",");
271 	}
fromData(QString s)272 	void fromData(QString s)
273 	{
274 		QStringList sl = s.split(",");
275 		foreach(QString hd, sl) {
276 			QStringList sl1 = hd.split(":");
277 			int id = sl1.takeFirst().toInt();
278 			if (id > 0) {
279 				id = OBJ_txt2nid(CCHAR(sl1.takeFirst()));
280 			}
281 			for (int i=0; i<count(); i++) {
282 				dbheader *h = at(i);
283 				if (h->id == id) {
284 					h->fromData(sl1.takeFirst());
285 					break;
286 				}
287 			}
288 		}
289 	}
290 };
291 #endif
292