File: v3ext.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 (182 lines) | stat: -rw-r--r-- 3,965 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
/* vi: set sw=4 ts=4:
 *
 * Copyright (C) 2005 - 2014 Christian Hohnstaedt.
 *
 * All rights reserved.
 */

#include "v3ext.h"
#include <QLabel>
#include <QListWidget>
#include <QComboBox>
#include <QLineEdit>
#include <QHeaderView>
#include <QStringList>
#include <QMessageBox>
#include <QValidator>
#include "XcaWarning.h"
#include "lib/exception.h"
#include "lib/ipvalidator.h"
#include "lib/x509v3ext.h"

#include <openssl/err.h>


v3ext::v3ext(QWidget *parent)
	:QDialog(parent)
{
	setupUi(this);
	setWindowTitle(XCA_TITLE);
	tab->horizontalHeader()->setDefaultSectionSize(80);
}

void v3ext::addInfo(QLineEdit *myle, const QStringList &sl, int n,
		X509V3_CTX *ctx)
{
	nid = n;
	le = myle;
	ext_ctx = ctx;
	tab->setKeys(sl);
	keys = sl;
	tab->setInfoLabel(infoLabel);
	connect(tab->itemDelegateForColumn(1),
		SIGNAL(setupLineEdit(const QString &, QLineEdit *)),
		this, SLOT(setupLineEdit(const QString &, QLineEdit *)));
	if (le && !le->text().trimmed().isEmpty())
		addItem(le->text());
	if (n != NID_subject_alt_name)
		copy_cn->hide();
}

void v3ext::addItem(const QString &list)
{
	int i;
	QStringList sl;
	sl = list.split(',');
	if (sl[0] == "critical") {
		sl.takeFirst();
		critical->setChecked(true);
	}
	for (i=0; i< sl.count(); i++) {
		if (sl[i] == "DNS:copycn" && nid == NID_subject_alt_name)
			copy_cn->setChecked(true);
		else
			addEntry(sl[i]);
	}
}

void v3ext::setupLineEdit(const QString &s, QLineEdit *l)
{
	QString tt;
	QValidator *v = NULL;

	if (s == "email") {
		if (nid == NID_subject_alt_name)
			tt = tr("An email address or 'copy'");
		else
			tt = tr("An email address");
	} else if (s == "RID") {
		tt = tr("A registered ID: OBJECT IDENTIFIER");
		QRegularExpression rx("[a-zA-Z0-9.]+");
		v = new QRegularExpressionValidator(rx, this);
	} else if (s == "URI") {
		tt = tr("A uniform resource indicator");
		QRegularExpression rx("[a-z][a-z0-9\\.\\+\\-]*://.*");
		v = new QRegularExpressionValidator(rx, this);
	} else if (s == "DNS") {
		if (nid == NID_subject_alt_name)
			tt = tr("A DNS domain name or 'copycn'");
		else
			tt = tr("A DNS domain name");
	} else if (s == "IP") {
		tt = tr("An IP address");
		v = new ipValidator();
	} else if (s == "otherName") {
		tt = tr("Syntax: <OID>;TYPE:text like '1.2.3.4:UTF8:name'");
		QRegularExpression rx("[a-zA-Z0-9.]+;.*");
		v = new QRegularExpressionValidator(rx, this);
	} else if (s == "issuer") {
		tt = tr("No editing. Only 'copy' allowed here");
		l->setText(QString("copy"));
		l->setReadOnly(true);
		QRegularExpression rx("copy");
		v = new QRegularExpressionValidator(rx, this);
	}
	l->setToolTip(tt);
	l->setValidator(v);
}

/* for one TYPE:Content String */
void v3ext::addEntry(const QString &l)
{
	int idx;
	QString type, value, line(l);

	line = line.trimmed();
	idx = line.indexOf(':');

	if (idx == -1) {
		value = line;
	} else {
		type = line.left(idx);
		value = line.mid(idx+1);
	}
	if (!keys.contains(type)) {
		type = keys[0];
		value = line;
	}
	tab->addRow(QStringList(type) << value);
}

QString v3ext::toString()
{
	QStringList str;
	int i, row = tab->rowCount();

	if (critical->isChecked())
		str << "critical";
	if (copy_cn->isChecked())
		str << "DNS:copycn";

	for (i=0; i<row; i++) {
		QStringList s = tab->getRow(i);
		str += s[0] + ":" +s[1];
	}
	return str.join(", ");
}

void v3ext::on_apply_clicked()
{
	__validate(false);
	if (le)
		le->setText(toString());
	accept();
}

bool v3ext::__validate(bool showSuccess)
{
	x509v3ext ext;
	QString str, error;
	validate->setFocus(Qt::OtherFocusReason);
	str = prefix + toString();
	ext.create(nid, str, ext_ctx);
	while (int i = ERR_get_error() ) {
		error += ERR_error_string(i ,NULL);
		error += "\n";
	}
	if (!error.isEmpty()) {
		XCA_WARN(tr("Validation failed:\n'%1'\n%2").
			arg(str).arg(error));
		return false;
	}
	if (showSuccess) {
		XCA_INFO(tr("Validation successful:\n'%1'").
			arg(ext.getValue()));
	}
	return true;
}

void v3ext::on_validate_clicked()
{
	__validate(true);
}