Skip to content
This repository was archived by the owner on Sep 1, 2023. It is now read-only.

Commit 82d5f5c

Browse files
committed
Allow only sign with non-repudiation certificate
IB-6797 Signed-off-by: Raul Metsma <raul@metsma.ee>
1 parent 25809dd commit 82d5f5c

23 files changed

Lines changed: 132 additions & 148 deletions

‎VERSION.mk‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@
1818

1919
MAJOR_VERSION=1
2020
MINOR_VERSION=1
21-
RELEASE_VERSION=2
21+
RELEASE_VERSION=3
2222
VERSION=$(MAJOR_VERSION).$(MINOR_VERSION).$(RELEASE_VERSION)
2323
VERSIONEX=$(VERSION).$(BUILD_NUMBER)

‎host-linux/CertificateSelection.h‎

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535

3636
class CertificateSelection: public QDialog {
3737
public:
38-
static QVariantMap getCert(bool forSigning)
38+
static QVariantMap getCert()
3939
{
4040
try {
4141
QList<QStringList> certs;
@@ -66,8 +66,8 @@ class CertificateSelection: public QDialog {
6666
}
6767
}
6868

69-
if (forSigning != isNonRepudiation) {
70-
_log("certificate is non-repu: %u, requesting signing certificate %u, moving on to next token...", isNonRepudiation, forSigning);
69+
if (!isNonRepudiation) {
70+
_log("certificate is not non-repu: moving on to next token...");
7171
continue;
7272
}
7373

@@ -78,10 +78,10 @@ class CertificateSelection: public QDialog {
7878
}
7979
if (certs.empty())
8080
return {{"result", "no_certificates"}};
81-
CertificateSelection dialog(certs);
82-
if (dialog.exec() == 0)
81+
int result = CertificateSelection(certs).exec();
82+
if (result == -1)
8383
return {{"result", "user_cancel"}};
84-
return {{"cert", certs.at(dialog.table->currentIndex().row())[3]}};
84+
return {{"cert", certs.at(result)[3]}};
8585
} catch (const BaseException &e) {
8686
qDebug() << e.what();
8787
return {{"result", QString::fromStdString(e.getErrorCode())}};
@@ -98,48 +98,39 @@ class CertificateSelection: public QDialog {
9898
}
9999

100100
CertificateSelection(const QList<QStringList> &certs)
101-
: message(new QLabel(this))
102-
, table(new QTreeWidget(this))
103-
, buttons(new QDialogButtonBox(this))
104101
{
105102
QVBoxLayout *layout = new QVBoxLayout(this);
106-
layout->addWidget(message);
107-
layout->addWidget(table);
108-
layout->addWidget(buttons);
103+
layout->addWidget(new QLabel(Labels::l10n.get("cert info").c_str()));
109104

110105
setWindowFlags(Qt::WindowStaysOnTopHint);
111106
setWindowTitle(Labels::l10n.get("select certificate").c_str());
112-
message->setText(Labels::l10n.get("cert info").c_str());
113107

108+
QTreeWidget *table = new QTreeWidget(this);
109+
layout->addWidget(table);
114110
table->setColumnCount(3);
115111
table->setRootIsDecorated(false);
116-
table->setHeaderLabels(QStringList()
117-
<< Labels::l10n.get("certificate").c_str()
118-
<< Labels::l10n.get("type").c_str()
119-
<< Labels::l10n.get("valid to").c_str());
112+
table->setHeaderLabels({
113+
Labels::l10n.get("certificate").c_str(),
114+
Labels::l10n.get("type").c_str(),
115+
Labels::l10n.get("valid to").c_str()});
120116
table->header()->setStretchLastSection(false);
121117
table->header()->setSectionResizeMode(QHeaderView::ResizeToContents);
122118
table->header()->setSectionResizeMode(0, QHeaderView::Stretch);
123119
for(const QStringList &row: certs)
124120
table->insertTopLevelItem(0, new QTreeWidgetItem(table, row));
125121
table->setCurrentIndex(table->model()->index(0, 0));
126122

127-
ok = buttons->addButton(Labels::l10n.get("select").c_str(), QDialogButtonBox::AcceptRole);
128-
cancel = buttons->addButton(Labels::l10n.get("cancel").c_str(), QDialogButtonBox::RejectRole);
129-
connect(buttons, &QDialogButtonBox::accepted, this, &QDialog::accept);
130-
connect(buttons, &QDialogButtonBox::rejected, this, &QDialog::reject);
131-
connect(table, &QTreeWidget::clicked, [&]{
132-
ok->setEnabled(true);
133-
});
123+
QDialogButtonBox *buttons = new QDialogButtonBox(this);
124+
layout->addWidget(buttons);
125+
QPushButton *ok = buttons->addButton(Labels::l10n.get("select").c_str(), QDialogButtonBox::AcceptRole);
126+
buttons->addButton(Labels::l10n.get("cancel").c_str(), QDialogButtonBox::RejectRole);
127+
connect(buttons, &QDialogButtonBox::accepted, this, [this, table] { done(table->currentIndex().row()); });
128+
connect(buttons, &QDialogButtonBox::rejected, this, [this] { done(-1); });
129+
connect(table, &QTreeWidget::clicked, [ok] { ok->setEnabled(true); });
134130

135131
show();
136132
// Make sure window is in foreground and focus
137133
raise();
138134
activateWindow();
139135
}
140-
141-
QLabel *message;
142-
QTreeWidget *table;
143-
QDialogButtonBox *buttons;
144-
QPushButton *ok = nullptr, *cancel = nullptr;
145136
};

‎host-linux/Signer.h‎

Lines changed: 25 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,21 @@ class Signer: public QDialog {
4747
public:
4848
static QVariantMap sign(const QString &hash, const QString &cert) {
4949
std::vector<unsigned char> data = fromHex(cert);
50+
QSslCertificate c(QByteArray::fromRawData((const char*)data.data(), int(data.size())), QSsl::Der);
51+
bool isNonRepudiation = false;
52+
for(const QSslCertificateExtension &ex: c.extensions())
53+
{
54+
if(ex.name() == QStringLiteral("keyUsage"))
55+
{
56+
for(const QVariant &item: ex.value().toList())
57+
if(item.toString() == QStringLiteral("Non Repudiation"))
58+
isNonRepudiation = true;
59+
}
60+
}
61+
if (!isNonRepudiation) {
62+
return {{"result", "invalid_argument"}};
63+
}
64+
5065
PKCS11CardManager::Token selected;
5166
PKCS11Path::Params p11 = PKCS11Path::getPkcs11ModulePath();
5267
PKCS11CardManager pkcs11(p11.path);
@@ -65,26 +80,8 @@ class Signer: public QDialog {
6580
if(selected.cert.empty())
6681
return {{"result", "invalid_argument"}};
6782

68-
QSslCertificate c(QByteArray::fromRawData((const char*)data.data(), int(data.size())), QSsl::Der);
69-
bool isNonRepudiation = false;
70-
for(const QSslCertificateExtension &ex: c.extensions())
71-
{
72-
if(ex.name() == QStringLiteral("keyUsage"))
73-
{
74-
for(const QVariant &item: ex.value().toList())
75-
if(item.toString() == QStringLiteral("Non Repudiation"))
76-
isNonRepudiation = true;
77-
}
78-
}
79-
QString label;
80-
if (isNonRepudiation) {
81-
label = Labels::l10n.get(selected.pinpad ? "sign PIN pinpad" : "sign PIN").c_str();
82-
label.replace("@PIN@", p11.signPINLabel.c_str());
83-
}
84-
else {
85-
label = Labels::l10n.get(selected.pinpad ? "auth PIN pinpad" : "auth PIN").c_str();
86-
label.replace("@PIN@", p11.authPINLabel.c_str());
87-
}
83+
QString label = Labels::l10n.get(selected.pinpad ? "sign PIN pinpad" : "sign PIN").c_str();
84+
label.replace("@PIN@", p11.signPINLabel.c_str());
8885

8986
bool isInitialCheck = true;
9087
for (int retriesLeft = selected.retry; retriesLeft > 0; ) {
@@ -172,48 +169,46 @@ class Signer: public QDialog {
172169

173170
Signer(const QString &label, unsigned long minPinLen, bool isPinpad)
174171
: nameLabel(new QLabel(this))
175-
, pinLabel(new QLabel(this))
176172
, errorLabel(new QLabel(this))
177173
{
178174
QVBoxLayout *layout = new QVBoxLayout(this);
179175
layout->addWidget(errorLabel);
180176
layout->addWidget(nameLabel);
181-
layout->addWidget(pinLabel);
177+
layout->addWidget(new QLabel(label, this));
182178

183179
setMinimumWidth(400);
184180
setWindowFlags(Qt::WindowStaysOnTopHint);
185-
pinLabel->setText(label);
186181
errorLabel->setTextFormat(Qt::RichText);
187182
errorLabel->hide();
188183

189184
if(isPinpad) {
190185
setWindowFlags((windowFlags()|Qt::CustomizeWindowHint) & ~Qt::WindowCloseButtonHint);
191-
progress = new QProgressBar(this);
186+
QProgressBar *progress = new QProgressBar(this);
192187
progress->setRange(0, 30);
193188
progress->setValue(progress->maximum());
194189
progress->setTextVisible(false);
195190

196-
statusTimer = new QTimeLine(progress->maximum() * 1000, this);
191+
QTimeLine *statusTimer = new QTimeLine(progress->maximum() * 1000, this);
197192
statusTimer->setCurveShape(QTimeLine::LinearCurve);
198193
statusTimer->setFrameRange(progress->maximum(), progress->minimum());
199194
connect(statusTimer, &QTimeLine::frameChanged, progress, &QProgressBar::setValue);
200195
statusTimer->start();
201196

202197
layout->addWidget(progress);
203198
} else {
204-
buttons = new QDialogButtonBox(this);
199+
QDialogButtonBox *buttons = new QDialogButtonBox(this);
205200
connect(buttons, &QDialogButtonBox::accepted, this, &QDialog::accept);
206201
connect(buttons, &QDialogButtonBox::rejected, this, &QDialog::reject);
207-
cancel = buttons->addButton(Labels::l10n.get("cancel").c_str(), QDialogButtonBox::RejectRole);
208-
ok = buttons->addButton("OK", QDialogButtonBox::AcceptRole);
202+
buttons->addButton(Labels::l10n.get("cancel").c_str(), QDialogButtonBox::RejectRole);
203+
QPushButton *ok = buttons->addButton("OK", QDialogButtonBox::AcceptRole);
209204
ok->setEnabled(false);
210205

211206
pin = new QLineEdit(this);
212207
pin->setEchoMode(QLineEdit::Password);
213208
pin->setFocus();
214209
pin->setValidator(new QRegExpValidator(QRegExp(QString("\\d{%1,12}").arg(minPinLen)), pin));
215210
pin->setMaxLength(12);
216-
connect(pin, &QLineEdit::textEdited, [=](const QString &text){
211+
connect(pin, &QLineEdit::textEdited, ok, [=](const QString &text) {
217212
ok->setEnabled(text.size() >= int(minPinLen));
218213
});
219214

@@ -223,10 +218,6 @@ class Signer: public QDialog {
223218
show();
224219
}
225220

226-
QLabel *nameLabel, *pinLabel, *errorLabel;
227-
QDialogButtonBox *buttons = nullptr;
228-
QPushButton *ok = nullptr, *cancel = nullptr;
221+
QLabel *nameLabel, *errorLabel;
229222
QLineEdit *pin = nullptr;
230-
QProgressBar *progress = nullptr;
231-
QTimeLine *statusTimer = nullptr;
232223
};

‎host-linux/chrome-host.cpp‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,12 @@ void Application::parse()
117117
resp = Signer::sign(json.value("hash").toString(), cert);
118118
}
119119
} else if (type == "CERT") {
120-
resp = CertificateSelection::getCert(json.value("filter").toString() != "AUTH");
121-
cert = resp.value("cert").toString();
120+
if (json.value("filter").toString() == "AUTH") {
121+
resp = {{"result", "invalid_argument"}};
122+
} else {
123+
resp = CertificateSelection::getCert();
124+
cert = resp.value("cert").toString();
125+
}
122126
} else {
123127
resp = {{"result", "invalid_argument"}};
124128
}

‎host-linux/chrome-token-signing.pro‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ TEMPLATE = app
22
CONFIG += console c++11 link_pkgconfig
33
CONFIG -= app_bundle
44
QT += widgets network
5-
isEmpty(VERSION):VERSION=1.1.2.0
5+
isEmpty(VERSION):VERSION=1.1.3.0
66
PKGCONFIG += libpcsclite
77
INCLUDEPATH += ../host-shared
88
LIBS += -ldl

‎host-osx/CertificateSelection.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@
2020

2121
@interface CertificateSelection : NSObject
2222

23-
+ (NSDictionary *)show:(bool)forSigning;
23+
+ (NSDictionary *)show;
2424

2525
@end

‎host-osx/CertificateSelection.mm‎

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ - (bool)isDuplicate:(NSString*)certificate {
5656
return false;
5757
}
5858

59-
- (instancetype)init:(bool)forSigning
59+
- (instancetype)init
6060
{
6161
if (self = [super init]) {
6262
certificates = [[NSMutableArray alloc] init];
@@ -73,9 +73,8 @@ - (instancetype)init:(bool)forSigning
7373
CFRelease(cert);
7474

7575
NSNumber *ku = dict[(__bridge id)kSecOIDKeyUsage][(__bridge id)kSecPropertyKeyValue];
76-
const bool isNonRepudiation = ku.unsignedIntValue & kSecKeyUsageNonRepudiation;
77-
if (forSigning != isNonRepudiation) {
78-
_log("certificate is non-repu: %u, requesting signing certificate %u, moving on to next token...", isNonRepudiation, forSigning);
76+
if ((ku.unsignedIntValue & kSecKeyUsageNonRepudiation) == 0) {
77+
_log("certificate is not non-repu: moving on to next token...");
7978
continue;
8079
}
8180

@@ -125,7 +124,9 @@ - (instancetype)init:(bool)forSigning
125124
return self;
126125
}
127126

128-
window.touchBar = [self makeTouchBar];
127+
if (@available(macOS 10_12_2, *)) {
128+
window.touchBar = [self makeTouchBar];
129+
}
129130
window.title = _L("select certificate");
130131
cancelButton.title = _L("cancel");
131132
okButton.title = _L("select");
@@ -141,10 +142,10 @@ - (instancetype)init:(bool)forSigning
141142
return self;
142143
}
143144

144-
+ (NSDictionary *)show:(bool)forSigning
145+
+ (NSDictionary *)show
145146
{
146147
try {
147-
CertificateSelection *dialog = [[CertificateSelection alloc] init:forSigning];
148+
CertificateSelection *dialog = [[CertificateSelection alloc] init];
148149
if (!dialog) {
149150
return @{@"result": @"technical_error"};
150151
}
@@ -209,7 +210,7 @@ - (IBAction)changeSelection:(id)sender
209210

210211
#pragma mark - NSTouchBarProvider
211212

212-
- (NSTouchBar *)makeTouchBar
213+
- (NSTouchBar *)makeTouchBar NS_AVAILABLE_MAC(10_12_2)
213214
{
214215
NSTouchBar *touchBar = [[NSTouchBar alloc] init];
215216
touchBar.delegate = self;
@@ -220,7 +221,7 @@ - (NSTouchBar *)makeTouchBar
220221

221222
#pragma mark - NSTouchBarDelegate
222223

223-
- (NSTouchBarItem *)touchBar:(NSTouchBar *)touchBar makeItemForIdentifier:(NSTouchBarItemIdentifier)identifier
224+
- (NSTouchBarItem *)touchBar:(NSTouchBar *)touchBar makeItemForIdentifier:(NSTouchBarItemIdentifier)identifier NS_AVAILABLE_MAC(10_12_2)
224225
{
225226
if ([identifier isEqualToString:touchBarItemGroupId])
226227
{

‎host-osx/PINDialog.mm‎

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,9 @@ - (instancetype)init:(NSString*)label pinpad:(BOOL)pinpad
131131
else {
132132
cancelButton.title = _L("cancel");
133133
}
134-
window.touchBar = [self makeTouchBar];
134+
if (@available(macOS 10_12_2, *)) {
135+
window.touchBar = [self makeTouchBar];
136+
}
135137
pinFieldLabel.stringValue = label;
136138
}
137139
return self;
@@ -186,18 +188,15 @@ + (NSDictionary *)show:(NSDictionary*)params cert:(NSString *)cert
186188
CFRelease(data);
187189
NSDictionary *dict = CFBridgingRelease(SecCertificateCopyValues(x509, nil, nil));
188190
CFRelease(x509);
191+
NSNumber *ku = dict[(__bridge id)kSecOIDKeyUsage][(__bridge id)kSecPropertyKeyValue];
192+
if ((ku.unsignedIntValue & kSecKeyUsageNonRepudiation) == 0) {
193+
return @{@"result": @"invalid_argument"};
194+
}
189195

190196
bool isInitialCheck = true;
191197
for (int retriesLeft = selected.retry; retriesLeft > 0; )
192198
{
193-
NSString *label;
194-
NSNumber *ku = dict[(__bridge id)kSecOIDKeyUsage][(__bridge id)kSecPropertyKeyValue];
195-
if (ku.unsignedIntValue & kSecKeyUsageNonRepudiation) {
196-
label = [_L(selected.pinpad ? "sign PIN pinpad" : "sign PIN") stringByReplacingOccurrencesOfString:@"@PIN@" withString:@(p11.signPINLabel.c_str())];
197-
}
198-
else {
199-
label = [_L(selected.pinpad ? "auth PIN pinpad" : "auth PIN") stringByReplacingOccurrencesOfString:@"@PIN@" withString:@(p11.authPINLabel.c_str())];
200-
}
199+
NSString *label = [_L(selected.pinpad ? "sign PIN pinpad" : "sign PIN") stringByReplacingOccurrencesOfString:@"@PIN@" withString:@(p11.signPINLabel.c_str())];
201200
PINPanel *dialog = [[PINPanel alloc] init:label pinpad:selected.pinpad];
202201
if (!dialog) {
203202
return @{@"result": @"technical_error"};
@@ -310,7 +309,9 @@ - (void)controlTextDidChange:(NSNotification*)notification;
310309
if (okButton.enabled != pinField.stringValue.length >= minPinLen)
311310
{
312311
okButton.enabled = pinField.stringValue.length >= minPinLen;
313-
window.touchBar = [self makeTouchBar];
312+
if (@available(macOS 10_12_2, *)) {
313+
window.touchBar = [self makeTouchBar];
314+
}
314315
}
315316
}
316317

@@ -325,7 +326,7 @@ - (void)handleTimerTick:(NSTimer*)timer
325326

326327
#pragma mark - NSTouchBarProvider
327328

328-
- (NSTouchBar *)makeTouchBar
329+
- (NSTouchBar *)makeTouchBar NS_AVAILABLE_MAC(10_12_2)
329330
{
330331
NSTouchBar *touchBar = [[NSTouchBar alloc] init];
331332
touchBar.delegate = self;
@@ -336,7 +337,7 @@ - (NSTouchBar *)makeTouchBar
336337

337338
#pragma mark - NSTouchBarDelegate
338339

339-
- (NSTouchBarItem *)touchBar:(NSTouchBar *)touchBar makeItemForIdentifier:(NSTouchBarItemIdentifier)identifier
340+
- (NSTouchBarItem *)touchBar:(NSTouchBar *)touchBar makeItemForIdentifier:(NSTouchBarItemIdentifier)identifier NS_AVAILABLE_MAC(10_12_2)
340341
{
341342
if ([identifier isEqualToString:touchBarItemGroupId])
342343
{

‎host-osx/chrome-host.mm‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,12 @@ int main(int argc, const char * argv[]) {
126126
result = @{@"result": @"not_allowed"};
127127
}
128128
else if([dict[@"type"] isEqualToString:@"CERT"]) {
129-
result = [CertificateSelection show:![@"AUTH" isEqualToString:dict[@"filter"]]];
130-
cert = (NSString*)result[@"cert"];
129+
if ([@"AUTH" isEqualToString:dict[@"filter"]]) {
130+
result = @{@"result": @"invalid_argument"};
131+
} else {
132+
result = [CertificateSelection show];
133+
cert = (NSString*)result[@"cert"];
134+
}
131135
}
132136
else if ([dict[@"type"] isEqualToString:@"SIGN"]) {
133137
result = [PINPanel show:dict cert:cert];

0 commit comments

Comments
 (0)