forked from openconnect/openconnect-gui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeypair.cpp
executable file
·163 lines (139 loc) · 3.72 KB
/
keypair.cpp
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
/*
* Copyright (C) 2014, 2015 Red Hat
*
* This file is part of openconnect-gui.
*
* openconnect-gui is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <dialogs.h>
#include <QString>
#include "keypair.h"
#include <gnutls/pkcs12.h>
KeyPair::KeyPair()
{
}
KeyPair::~KeyPair()
{
}
static
int load_pkcs12_file(QWidget * w, Key & key, Cert & cert, QString File,
QString & last_err)
{
gnutls_datum_t raw = { NULL, 0 };
int ret;
gnutls_pkcs12_t pkcs12 = NULL;
bool ok = 0;
QString pass;
int pem = 0;
char *p;
gnutls_x509_privkey_t xkey;
gnutls_x509_crt_t *xcert;
unsigned int xcert_size;
unsigned i;
if (w == NULL || is_url(File)) {
return -1;
}
ret = gnutls_load_file(File.toAscii().data(), &raw);
if (ret < 0) {
last_err = gnutls_strerror(ret);
goto fail;
}
/* check if the file data contain BEGIN PKCS12 */
p = strstr((char *)raw.data, "--- BEGIN ");
if (p != NULL) {
pem = 1;
if (strstr(p, "--- BEGIN PKCS12") == 0)
return -1;
}
ret = gnutls_pkcs12_init(&pkcs12);
if (ret < 0) {
last_err = gnutls_strerror(ret);
goto fail;
}
ret =
gnutls_pkcs12_import(pkcs12, &raw,
(pem !=
0) ? GNUTLS_X509_FMT_PEM : GNUTLS_X509_FMT_DER,
0);
if (ret < 0) {
last_err = gnutls_strerror(ret);
goto fail;
}
pass =
QInputDialog::getText(w, QLatin1String("This file requires a password"),
QLatin1String("Please enter your password"),
QLineEdit::Password, QString(), &ok);
if (!ok)
goto fail;
ret = gnutls_pkcs12_verify_mac(pkcs12, pass.toAscii().data());
if (ret < 0) {
last_err = gnutls_strerror(ret);
goto fail;
}
ret =
gnutls_pkcs12_simple_parse(pkcs12, pass.toAscii().data(), &xkey, &xcert,
&xcert_size, NULL, NULL, NULL, 0);
if (ret < 0) {
last_err = gnutls_strerror(ret);
goto fail;
}
if (xkey)
key.set(xkey);
if (xcert_size > 0)
cert.set(xcert[0]);
for (i = 1; i < xcert_size; i++)
gnutls_x509_crt_deinit(xcert[i]);
ret = 0;
goto cleanup;
fail:
ret = -1;
cleanup:
gnutls_free(raw.data);
if (pkcs12)
gnutls_pkcs12_deinit(pkcs12);
return ret;
}
int KeyPair::import_pfx(QString File)
{
return load_pkcs12_file(this->w, this->key, this->cert, File,
this->last_err);
}
int KeyPair::import_cert(QString File)
{
int ret2 = 0;
ret2 = this->cert.import_file(File);
if (ret2 != 0) {
last_err = cert.last_err;
return -1;
}
return 0;
}
int KeyPair::import_key(QString File)
{
int ret1 = 0;
ret1 = this->key.import_file(File);
if (ret1 != 0) {
last_err = key.last_err;
return -1;
}
return 0;
}
int KeyPair::cert_export(QByteArray & data)
{
return cert.data_export(data);
}
int KeyPair::key_export(QByteArray & data)
{
return key.data_export(data);
}