File: loadcertwiz.cpp

package info (click to toggle)
trustedqsl 2.3-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 29,116 kB
  • ctags: 3,577
  • sloc: cpp: 24,858; xml: 5,344; ansic: 626; sh: 88; python: 18; makefile: 11
file content (355 lines) | stat: -rw-r--r-- 12,347 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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/***************************************************************************
                          loadcertwiz.cpp  -  description
                             -------------------
    begin                : Wed Aug 6 2003
    copyright            : (C) 2003 by ARRL
    author               : Jon Bloom
    email                : jbloom@arrl.org
    revision             : $Id$
 ***************************************************************************/

#include <curl/curl.h>
#ifdef HAVE_CONFIG_H
#include "sysconfig.h"
#endif

#include "loadcertwiz.h"
#include "getpassword.h"
#include "tqslctrls.h"
#include "tqsllib.h"
#include "tqslerrno.h"
#include "tqslapp.h"
#include "tqsltrace.h"

wxString
notifyData::Message() const {
	tqslTrace(NULL, "%s\n"
			"Root Certificates:\t\tLoaded: %d  Duplicate: %d  Error: %d\n"
			"CA Certificates:\t\tLoaded: %d  Duplicate: %d  Error: %d\n"
			"Callsign Certificates:\tLoaded: %d  Duplicate: %d  Error: %d\n"
			"Private Keys:\t\t\tLoaded: %d  Duplicate: %d  Error: %d\n"
			"Configuration Data:\tLoaded: %d  Duplicate: %d  Error: %d\n",
			S(status),
			root.loaded, root.duplicate, root.error,
			ca.loaded, ca.duplicate, ca.error,
			user.loaded, user.duplicate, user.error,
			pkey.loaded, pkey.duplicate, pkey.error,
			config.loaded, config.duplicate, config.error);
	if (status.IsEmpty()) {
		wxString msg = wxT("\n");
		msg += _("Import completed successfully");
		return msg;
	}
	return status;
}

int
notifyImport(int type, const char *message, void *data) {
	tqslTrace("notifyImport", "type=%d, message=%s, data=0x%lx", type, message, reinterpret_cast<void *>(data));
	if (TQSL_CERT_CB_RESULT_TYPE(type) == TQSL_CERT_CB_PROMPT) {
		const char *nametype = 0;
		const char *configkey = 0;
		bool default_prompt = false;
		switch (TQSL_CERT_CB_CERT_TYPE(type)) {
			case TQSL_CERT_CB_ROOT:
				nametype = "Trusted root";
				configkey = "NotifyNewRoot";
				break;
			case TQSL_CERT_CB_CA:
				nametype = "Certificate Authority";
				configkey = "NotifyCA";
				break;
			case TQSL_CERT_CB_USER:
				nametype = "Callsign";
				configkey = "NotifyUser";
				break;
		}
		if (!nametype)
			return 0;
		wxConfig *config = reinterpret_cast<wxConfig *>(wxConfig::Get());
		bool b;
		config->Read(wxString::FromUTF8(configkey), &b, default_prompt);
		if (!b)
			return 0;
		wxString s(_("OK to install "));
		s = s + wxString::FromUTF8(nametype) + wxT(" ") + _("certificate?") + wxT("\n\n") + wxString::FromUTF8(message);
		if (wxMessageBox(s, _("Install Certificate"), wxYES_NO | wxICON_QUESTION) == wxYES)
			return 0;
		return 1;
	} // end TQSL_CERT_CB_PROMPT
	if (TQSL_CERT_CB_CALL_TYPE(type) == TQSL_CERT_CB_RESULT && data) {
		// Keep count
		notifyData *nd = reinterpret_cast<notifyData *>(data);
		notifyData::counts *counts = 0;
		switch (TQSL_CERT_CB_CERT_TYPE(type)) {
			case TQSL_CERT_CB_ROOT:
				counts = &(nd->root);
				break;
			case TQSL_CERT_CB_CA:
				counts = &(nd->ca);
				break;
			case TQSL_CERT_CB_USER:
				counts = &(nd->user);
				break;
			case TQSL_CERT_CB_PKEY:
				counts = &(nd->pkey);
				break;
			case TQSL_CERT_CB_CONFIG:
				counts = &(nd->config);
				break;
		}
		if (counts) {
			switch (TQSL_CERT_CB_RESULT_TYPE(type)) {
				case TQSL_CERT_CB_DUPLICATE:
					if (TQSL_CERT_CB_CERT_TYPE(type) == TQSL_CERT_CB_USER) {
						if (message) {
							nd->status = nd->status + wxString::FromUTF8(message) + wxT("\n");
						} else {
							nd->status = nd->status + _("This callsign certificate is already installed");
						}
					}
					counts->duplicate++;
					break;
				case TQSL_CERT_CB_ERROR:
					if (message)
						nd->status = nd->status + wxString::FromUTF8(message) + wxT("\n");
					counts->error++;
					// wxMessageBox(wxString::FromUTF8(message), _("Error"));
					break;
				case TQSL_CERT_CB_LOADED:
					if (TQSL_CERT_CB_CERT_TYPE(type) == TQSL_CERT_CB_USER)
						nd->status = nd->status + wxString::FromUTF8("Callsign Certificate ") +
							wxString::FromUTF8(message) + wxT("\n");
					counts->loaded++;
					break;
			}
		}
	}
	if (TQSL_CERT_CB_RESULT_TYPE(type) == TQSL_CERT_CB_ERROR)
		return 1;	// Errors get posted later
	if (TQSL_CERT_CB_CALL_TYPE(type) == TQSL_CERT_CB_RESULT
		|| TQSL_CERT_CB_RESULT_TYPE(type) == TQSL_CERT_CB_DUPLICATE) {
//		wxMessageBox(message, "Certificate Notice");
		return 0;
	}
	return 1;
}

static wxHtmlHelpController *pw_help = 0;
static wxString pw_helpfile;

static int
GetNewPassword(char *buf, int bufsiz, void *) {
	tqslTrace("GetNewPassword", NULL);
	wxString msg = _("Enter a password for this callsign certificate.");
		msg += wxT("\n\n");
		msg += _("If you are using a computer system that is "
		"shared with others, you should specify a "
		"password to protect this certificate. However, if "
		"you are using a computer in a private residence "
		"no password need be specified.");
		msg += wxT("\n\n");
		msg += _("This password will have to be entered each time "
		"you use this callsign certificate for signing or "
		"when saving the key.");
		msg += wxT("\n\n");
		msg += _("Leave the password blank and click 'OK' unless you want "
		"to use a password.");
		msg += wxT("\n\n");
	GetNewPasswordDialog dial(0, _("New Password"),
		msg, true, pw_help, pw_helpfile);
	if (dial.ShowModal() == wxID_OK) {
		strncpy(buf, dial.Password().ToUTF8(), bufsiz);
		buf[bufsiz-1] = 0;
		return 0;
	}
	return 1;
}

static void
export_new_cert(ExtWizard *_parent, const char *filename) {
	tqslTrace("export_new_cert", "_parent=0x%lx, filename=%s", _parent, filename);
	long newserial;
	if (!tqsl_getSerialFromTQSLFile(filename, &newserial)) {
		MyFrame *frame = reinterpret_cast<MyFrame *>(((reinterpret_cast<LoadCertWiz *>(_parent))->Parent()));
		TQ_WXCOOKIE cookie;
		int nproviders = frame->cert_tree->GetNumIssuers();		// Number of certificate issuers - currently 1
		wxTreeItemId root = frame->cert_tree->GetRootItem();
		wxTreeItemId item, prov;
		if (nproviders > 1) {
			prov = frame->cert_tree->GetFirstChild(root, cookie); // First child is the providers
			item = frame->cert_tree->GetFirstChild(prov, cookie);// Then it's certs
		} else {
			item = frame->cert_tree->GetFirstChild(root, cookie); // First child is the certs
		}

		while (item.IsOk()) {
			tQSL_Cert cert;
			CertTreeItemData *id = frame->cert_tree->GetItemData(item);
			if (id && (cert = id->getCert())) {
				long serial;
				if (!tqsl_getCertificateSerial(cert, &serial)) {
					if (serial == newserial) {
						wxCommandEvent e;
						wxString msg = _("You will not be able to use this tq6 file to recover your "
							"callsign certificate if it gets lost. For security purposes, you should "
							"back up your certificate on removable media for safe-keeping.");
							msg += wxT("\n\n");
							msg += _("Would you like to back up your callsign certificate now?");
						if (wxMessageBox(msg, _("Warning"), wxYES_NO | wxICON_QUESTION, _parent) == wxNO) {
							return;
						}
						frame->cert_tree->SelectItem(item);
						frame->OnCertExport(e);
						break;
					}
				}
			}
			if (nproviders > 1) {
				item = frame->cert_tree->GetNextChild(prov, cookie);
			} else {
				item = frame->cert_tree->GetNextChild(root, cookie);
			}
		}
	}
}

LoadCertWiz::LoadCertWiz(wxWindow *parent, wxHtmlHelpController *help, const wxString& title, const wxString& ext)
	: ExtWizard(parent, help, title), _nd(0) {
	tqslTrace("LoadCertWiz::LoadCertWiz", "parent=0x%lx, title=%s, ext=%s", reinterpret_cast<void *>(parent), S(title), S(ext));

	LCW_FinalPage *final = new LCW_FinalPage(this);
	LCW_P12PasswordPage *p12pw = new LCW_P12PasswordPage(this);
	wxWizardPageSimple::Chain(p12pw, final);
	_first = p12pw;
	_parent = parent;
	_final = final;
	_p12pw = p12pw;

	wxConfig *config = reinterpret_cast<wxConfig *>(wxConfig::Get());
#if !defined(__APPLE__) && !defined(_WIN32)
	wxString wild(_("Callsign Certificate container files (*.p12;*.P12)|*.p12;*.P12|Certificate Request response files (*.tq6;*.TQ6)|*.tq6;*.TQ6"));
#else
	wxString wild(_("Callsign Certificate container files (*.p12)|*.p12|Certificate Request response files (*.tq6)|*.tq6"));
#endif
	wild += _("|All files (*.*)|*.*");

	tQSL_ImportCall[0] = '\0';
	wxString path = config->Read(wxT("CertFilePath"), wxT(""));
	wxString filename = wxFileSelector(_("Select Certificate File"), path,
		wxT(""), ext, wild, wxFD_OPEN|wxFD_FILE_MUST_EXIST);
	if (filename == wxT("")) {
		// Cancelled!
		_first = 0;
	} else {
		ResetNotifyData();
		wxString path, basename, ext;
		wxFileName::SplitPath(filename, &path, &basename, &ext);

		config->Write(wxT("CertFilePath"), path);
		if (ext.MakeLower() == wxT("tq6")) {
			_first = _final;
			_final->SetPrev(0);
			if (tqsl_importTQSLFile(filename.ToUTF8(), notifyImport, GetNotifyData())) {
				if (tQSL_Error != TQSL_CERT_ERROR) {  // if not already reported by the callback
					wxMessageBox(getLocalizedErrorString(), _("Error"), wxOK | wxICON_ERROR, _parent);
				}
			} else {
				if (tQSL_ImportCall[0] != '\0') {
					wxString call = wxString::FromUTF8(tQSL_ImportCall);
					wxString pending = config->Read(wxT("RequestPending"));
					pending.Replace(call, wxT(""), true);
					wxString rest;
					while (pending.StartsWith(wxT(","), &rest))
						pending = rest;
					while (pending.EndsWith(wxT(","), &rest))
						pending = rest;
					config->Write(wxT("RequestPending"), pending);
				}
				export_new_cert(this, filename.ToUTF8());
			}
		} else {
			// First try with no password
			if (!tqsl_importPKCS12File(filename.ToUTF8(), "", 0, GetNewPassword, notifyImport, GetNotifyData()) || tQSL_Error == TQSL_CERT_ERROR) {
				_first = _final;
				_final->SetPrev(0);
			} else {
				if (tQSL_Error == TQSL_PASSWORD_ERROR) {
					_first = _p12pw;
					_p12pw->SetPrev(0);
					p12pw->SetFilename(filename);
				} else {
					_first = 0;	// Cancel
				}
			}
		}
	}
	AdjustSize();
	CenterOnParent();
}

LCW_FinalPage::LCW_FinalPage(LoadCertWiz *parent) : LCW_Page(parent) {
	tqslTrace("LCW_FinalPage::LCW_FinalPage", "parent=0x%lx", reinterpret_cast<void *>(parent));
	wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);

	wxStaticText *st = new wxStaticText(this, -1, _("Loading complete"));
	sizer->Add(st, 0, wxALL, 10);
	wxSize tsize = getTextSize(this);
	tc_status = new wxTextCtrl(this, -1, wxT(""), wxDefaultPosition, tsize.Scale(40, 16), wxTE_MULTILINE|wxTE_READONLY);
	sizer->Add(tc_status, 1, wxALL|wxEXPAND, 10);
	AdjustPage(sizer);
}

void
LCW_FinalPage::refresh() {
	tqslTrace("LCW_FinalPage::refresh", NULL);
	const notifyData *nd = (reinterpret_cast<LoadCertWiz *>(_parent))->GetNotifyData();
	if (nd)
		tc_status->SetValue(nd->Message());
	else
		tc_status->SetValue(_("No status information available"));
}

LCW_P12PasswordPage::LCW_P12PasswordPage(LoadCertWiz *parent) : LCW_Page(parent) {
	tqslTrace("LCW_P12PasswordPage::LCW_P12PasswordPage", "parent=0x%lx", reinterpret_cast<void *>(parent));
	wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);

	wxStaticText *st = new wxStaticText(this, -1, _("Enter the password to unlock the .p12 file:"));
	sizer->Add(st, 0, wxALL, 10);

	_pwin = new wxTextCtrl(this, -1, wxT(""), wxDefaultPosition, wxDefaultSize, wxTE_PASSWORD);
	sizer->Add(_pwin, 0, wxLEFT|wxRIGHT|wxEXPAND, 10);
	tc_status = new wxStaticText(this, -1, wxT(""));
	sizer->Add(tc_status, 0, wxALL, 10);

	AdjustPage(sizer, wxT("lcf1.htm"));
}

bool
LCW_P12PasswordPage::TransferDataFromWindow() {
	tqslTrace("LCW_P12PasswordPage::TransferDataFromWindow", NULL);
	wxString _pw = _pwin->GetValue();
	pw_help = Parent()->GetHelp();
	pw_helpfile = wxT("lcf2.htm");
	if (tqsl_importPKCS12File(_filename.ToUTF8(), _pw.ToUTF8(), 0, GetNewPassword, notifyImport,
		(reinterpret_cast<LoadCertWiz *>(_parent))->GetNotifyData())) {
		if (tQSL_Error == TQSL_PASSWORD_ERROR) {
			// UTF-8 password didn't work - try converting to UCS-2.
			char unipwd[64];
			utf8_to_ucs2(_pw.ToUTF8(), unipwd, sizeof unipwd);
			if (!tqsl_importPKCS12File(_filename.ToUTF8(), unipwd, 0, GetNewPassword, notifyImport,
					(reinterpret_cast<LoadCertWiz *>(_parent))->GetNotifyData())) {
				tc_status->SetLabel(wxT(""));
				return true;
			}
		}
		if (tQSL_Error == TQSL_PASSWORD_ERROR) {
			tc_status->SetLabel(_("Password error"));
			return false;
		} else {
			wxMessageBox(getLocalizedErrorString(), _("Error"), wxOK | wxICON_ERROR, _parent);
		}
	}
	tc_status->SetLabel(wxT(""));
	return true;
}