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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/*
* Dialog services for PIP.
*/
#include "nsNSSDialogs.h"
#include "mozIDOMWindow.h"
#include "nsArray.h"
#include "nsEmbedCID.h"
#include "nsHashPropertyBag.h"
#include "nsIDialogParamBlock.h"
#include "nsIInterfaceRequestor.h"
#include "nsIInterfaceRequestorUtils.h"
#include "nsIKeygenThread.h"
#include "nsIPromptService.h"
#include "nsIProtectedAuthThread.h"
#include "nsIWindowWatcher.h"
#include "nsIX509CertDB.h"
#include "nsIX509Cert.h"
#include "nsNSSDialogHelper.h"
#include "nsString.h"
#include "nsVariant.h"
#define PIPSTRING_BUNDLE_URL "chrome://pippki/locale/pippki.properties"
nsNSSDialogs::nsNSSDialogs()
{
}
nsNSSDialogs::~nsNSSDialogs()
{
}
NS_IMPL_ISUPPORTS(nsNSSDialogs, nsITokenPasswordDialogs,
nsICertificateDialogs,
nsIClientAuthDialogs,
nsITokenDialogs,
nsIGeneratingKeypairInfoDialogs)
nsresult
nsNSSDialogs::Init()
{
nsresult rv;
nsCOMPtr<nsIStringBundleService> service =
do_GetService(NS_STRINGBUNDLE_CONTRACTID, &rv);
if (NS_FAILED(rv)) return rv;
rv = service->CreateBundle(PIPSTRING_BUNDLE_URL,
getter_AddRefs(mPIPStringBundle));
return rv;
}
nsresult
nsNSSDialogs::SetPassword(nsIInterfaceRequestor *ctx,
const char16_t *tokenName, bool* _canceled)
{
nsresult rv;
*_canceled = false;
// Get the parent window for the dialog
nsCOMPtr<mozIDOMWindowProxy> parent = do_GetInterface(ctx);
nsCOMPtr<nsIDialogParamBlock> block =
do_CreateInstance(NS_DIALOGPARAMBLOCK_CONTRACTID);
if (!block) return NS_ERROR_FAILURE;
rv = block->SetString(1, tokenName);
if (NS_FAILED(rv)) return rv;
rv = nsNSSDialogHelper::openDialog(parent,
"chrome://pippki/content/changepassword.xul",
block);
if (NS_FAILED(rv)) return rv;
int32_t status;
rv = block->GetInt(1, &status);
if (NS_FAILED(rv)) return rv;
*_canceled = (status == 0)?true:false;
return rv;
}
NS_IMETHODIMP
nsNSSDialogs::ConfirmDownloadCACert(nsIInterfaceRequestor* ctx,
nsIX509Cert* cert,
/*out*/ uint32_t* trust,
/*out*/ bool* importConfirmed)
{
// |ctx| is allowed to be null.
NS_ENSURE_ARG(cert);
NS_ENSURE_ARG(trust);
NS_ENSURE_ARG(importConfirmed);
nsCOMPtr<nsIMutableArray> argArray = nsArrayBase::Create();
if (!argArray) {
return NS_ERROR_FAILURE;
}
nsresult rv = argArray->AppendElement(cert, false);
if (NS_FAILED(rv)) {
return rv;
}
nsCOMPtr<nsIWritablePropertyBag2> retVals = new nsHashPropertyBag();
rv = argArray->AppendElement(retVals, false);
if (NS_FAILED(rv)) {
return rv;
}
// Get the parent window for the dialog
nsCOMPtr<mozIDOMWindowProxy> parent = do_GetInterface(ctx);
rv = nsNSSDialogHelper::openDialog(parent,
"chrome://pippki/content/downloadcert.xul",
argArray);
if (NS_FAILED(rv)) {
return rv;
}
rv = retVals->GetPropertyAsBool(NS_LITERAL_STRING("importConfirmed"),
importConfirmed);
if (NS_FAILED(rv)) {
return rv;
}
*trust = nsIX509CertDB::UNTRUSTED;
if (!*importConfirmed) {
return NS_OK;
}
bool trustForSSL = false;
rv = retVals->GetPropertyAsBool(NS_LITERAL_STRING("trustForSSL"),
&trustForSSL);
if (NS_FAILED(rv)) {
return rv;
}
bool trustForEmail = false;
rv = retVals->GetPropertyAsBool(NS_LITERAL_STRING("trustForEmail"),
&trustForEmail);
if (NS_FAILED(rv)) {
return rv;
}
bool trustForObjSign = false;
rv = retVals->GetPropertyAsBool(NS_LITERAL_STRING("trustForObjSign"),
&trustForObjSign);
if (NS_FAILED(rv)) {
return rv;
}
*trust |= trustForSSL ? nsIX509CertDB::TRUSTED_SSL : 0;
*trust |= trustForEmail ? nsIX509CertDB::TRUSTED_EMAIL : 0;
*trust |= trustForObjSign ? nsIX509CertDB::TRUSTED_OBJSIGN : 0;
return NS_OK;
}
NS_IMETHODIMP
nsNSSDialogs::ChooseCertificate(nsIInterfaceRequestor* ctx,
const nsACString& hostname,
int32_t port,
const nsACString& organization,
const nsACString& issuerOrg,
nsIArray* certList,
/*out*/ uint32_t* selectedIndex,
/*out*/ bool* certificateChosen)
{
NS_ENSURE_ARG_POINTER(ctx);
NS_ENSURE_ARG_POINTER(certList);
NS_ENSURE_ARG_POINTER(selectedIndex);
NS_ENSURE_ARG_POINTER(certificateChosen);
*certificateChosen = false;
nsCOMPtr<nsIMutableArray> argArray = nsArrayBase::Create();
if (!argArray) {
return NS_ERROR_FAILURE;
}
nsCOMPtr<nsIWritableVariant> hostnameVariant = new nsVariant();
nsresult rv = hostnameVariant->SetAsAUTF8String(hostname);
if (NS_FAILED(rv)) {
return rv;
}
rv = argArray->AppendElement(hostnameVariant, false);
if (NS_FAILED(rv)) {
return rv;
}
nsCOMPtr<nsIWritableVariant> organizationVariant = new nsVariant();
rv = organizationVariant->SetAsAUTF8String(organization);
if (NS_FAILED(rv)) {
return rv;
}
rv = argArray->AppendElement(organizationVariant, false);
if (NS_FAILED(rv)) {
return rv;
}
nsCOMPtr<nsIWritableVariant> issuerOrgVariant = new nsVariant();
rv = issuerOrgVariant->SetAsAUTF8String(issuerOrg);
if (NS_FAILED(rv)) {
return rv;
}
rv = argArray->AppendElement(issuerOrgVariant, false);
if (NS_FAILED(rv)) {
return rv;
}
nsCOMPtr<nsIWritableVariant> portVariant = new nsVariant();
rv = portVariant->SetAsInt32(port);
if (NS_FAILED(rv)) {
return rv;
}
rv = argArray->AppendElement(portVariant, false);
if (NS_FAILED(rv)) {
return rv;
}
rv = argArray->AppendElement(certList, false);
if (NS_FAILED(rv)) {
return rv;
}
nsCOMPtr<nsIWritablePropertyBag2> retVals = new nsHashPropertyBag();
rv = argArray->AppendElement(retVals, false);
if (NS_FAILED(rv)) {
return rv;
}
rv = nsNSSDialogHelper::openDialog(nullptr,
"chrome://pippki/content/clientauthask.xul",
argArray);
if (NS_FAILED(rv)) {
return rv;
}
nsCOMPtr<nsIClientAuthUserDecision> extraResult = do_QueryInterface(ctx);
if (extraResult) {
bool rememberSelection = false;
rv = retVals->GetPropertyAsBool(NS_LITERAL_STRING("rememberSelection"),
&rememberSelection);
if (NS_SUCCEEDED(rv)) {
extraResult->SetRememberClientAuthCertificate(rememberSelection);
}
}
rv = retVals->GetPropertyAsBool(NS_LITERAL_STRING("certChosen"),
certificateChosen);
if (NS_FAILED(rv)) {
return rv;
}
if (*certificateChosen) {
rv = retVals->GetPropertyAsUint32(NS_LITERAL_STRING("selectedIndex"),
selectedIndex);
if (NS_FAILED(rv)) {
return rv;
}
}
return NS_OK;
}
NS_IMETHODIMP
nsNSSDialogs::SetPKCS12FilePassword(nsIInterfaceRequestor* ctx,
/*out*/ nsAString& password,
/*out*/ bool* confirmedPassword)
{
// |ctx| is allowed to be null.
NS_ENSURE_ARG(confirmedPassword);
// Get the parent window for the dialog
nsCOMPtr<mozIDOMWindowProxy> parent = do_GetInterface(ctx);
nsCOMPtr<nsIWritablePropertyBag2> retVals = new nsHashPropertyBag();
nsresult rv =
nsNSSDialogHelper::openDialog(parent,
"chrome://pippki/content/setp12password.xul",
retVals);
if (NS_FAILED(rv)) {
return rv;
}
rv = retVals->GetPropertyAsBool(NS_LITERAL_STRING("confirmedPassword"),
confirmedPassword);
if (NS_FAILED(rv)) {
return rv;
}
if (!*confirmedPassword) {
return NS_OK;
}
return retVals->GetPropertyAsAString(NS_LITERAL_STRING("password"), password);
}
NS_IMETHODIMP
nsNSSDialogs::GetPKCS12FilePassword(nsIInterfaceRequestor* ctx,
nsAString& _password,
bool* _retval)
{
*_retval = false;
nsCOMPtr<nsIPromptService> promptSvc(
do_GetService(NS_PROMPTSERVICE_CONTRACTID));
if (!promptSvc) {
return NS_ERROR_FAILURE;
}
nsAutoString msg;
nsresult rv = mPIPStringBundle->GetStringFromName(
u"getPKCS12FilePasswordMessage", getter_Copies(msg));
if (NS_FAILED(rv)) {
return rv;
}
// Get the parent window for the dialog
nsCOMPtr<mozIDOMWindowProxy> parent = do_GetInterface(ctx);
bool ignored = false;
char16_t* pwTemp = nullptr;
rv = promptSvc->PromptPassword(parent, nullptr, msg.get(), &pwTemp, nullptr,
&ignored, _retval);
if (NS_FAILED(rv)) {
return rv;
}
if (*_retval) {
_password.Assign(pwTemp);
free(pwTemp);
}
return NS_OK;
}
NS_IMETHODIMP
nsNSSDialogs::ViewCert(nsIInterfaceRequestor* ctx, nsIX509Cert* cert)
{
// |ctx| is allowed to be null.
NS_ENSURE_ARG(cert);
// Get the parent window for the dialog
nsCOMPtr<mozIDOMWindowProxy> parent = do_GetInterface(ctx);
return nsNSSDialogHelper::openDialog(parent,
"chrome://pippki/content/certViewer.xul",
cert,
false /*modal*/);
}
NS_IMETHODIMP
nsNSSDialogs::DisplayGeneratingKeypairInfo(nsIInterfaceRequestor *aCtx, nsIKeygenThread *runnable)
{
nsresult rv;
// Get the parent window for the dialog
nsCOMPtr<mozIDOMWindowProxy> parent = do_GetInterface(aCtx);
rv = nsNSSDialogHelper::openDialog(parent,
"chrome://pippki/content/createCertInfo.xul",
runnable);
return rv;
}
NS_IMETHODIMP
nsNSSDialogs::ChooseToken(nsIInterfaceRequestor *aCtx, const char16_t **aTokenList, uint32_t aCount, char16_t **aTokenChosen, bool *aCanceled) {
nsresult rv;
uint32_t i;
*aCanceled = false;
nsCOMPtr<nsIDialogParamBlock> block =
do_CreateInstance(NS_DIALOGPARAMBLOCK_CONTRACTID);
if (!block) return NS_ERROR_FAILURE;
block->SetNumberStrings(aCount);
for (i = 0; i < aCount; i++) {
rv = block->SetString(i, aTokenList[i]);
if (NS_FAILED(rv)) return rv;
}
rv = block->SetInt(0, aCount);
if (NS_FAILED(rv)) return rv;
rv = nsNSSDialogHelper::openDialog(nullptr,
"chrome://pippki/content/choosetoken.xul",
block);
if (NS_FAILED(rv)) return rv;
int32_t status;
rv = block->GetInt(0, &status);
if (NS_FAILED(rv)) return rv;
*aCanceled = (status == 0)?true:false;
if (!*aCanceled) {
// retrieve the nickname
rv = block->GetString(0, aTokenChosen);
}
return rv;
}
NS_IMETHODIMP
nsNSSDialogs::DisplayProtectedAuth(nsIInterfaceRequestor *aCtx, nsIProtectedAuthThread *runnable)
{
// We cannot use nsNSSDialogHelper here. We cannot allow close widget
// in the window because protected authentication is interruptible
// from user interface and changing nsNSSDialogHelper's static variable
// would not be thread-safe
nsresult rv = NS_ERROR_FAILURE;
// Get the parent window for the dialog
nsCOMPtr<mozIDOMWindowProxy> parent = do_GetInterface(aCtx);
nsCOMPtr<nsIWindowWatcher> windowWatcher =
do_GetService("@mozilla.org/embedcomp/window-watcher;1", &rv);
if (NS_FAILED(rv))
return rv;
if (!parent) {
windowWatcher->GetActiveWindow(getter_AddRefs(parent));
}
nsCOMPtr<mozIDOMWindowProxy> newWindow;
rv = windowWatcher->OpenWindow(parent,
"chrome://pippki/content/protectedAuth.xul",
"_blank",
"centerscreen,chrome,modal,titlebar,close=no",
runnable,
getter_AddRefs(newWindow));
return rv;
}
|