nsNSSDialogs.cpp (5760B)
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- 2 * 3 * This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 /* 8 * Dialog services for PIP. 9 */ 10 11 #include "nsNSSDialogs.h" 12 13 #include "mozIDOMWindow.h" 14 #include "nsArray.h" 15 #include "nsComponentManagerUtils.h" 16 #include "nsEmbedCID.h" 17 #include "nsHashPropertyBag.h" 18 #include "nsIDialogParamBlock.h" 19 #include "nsIInterfaceRequestor.h" 20 #include "nsIInterfaceRequestorUtils.h" 21 #include "nsIPK11Token.h" 22 #include "nsIPromptService.h" 23 #include "nsIWindowWatcher.h" 24 #include "nsIX509CertDB.h" 25 #include "nsIX509Cert.h" 26 #include "nsNSSDialogHelper.h" 27 #include "nsPromiseFlatString.h" 28 #include "nsServiceManagerUtils.h" 29 #include "nsString.h" 30 #include "nsVariant.h" 31 32 #define PIPSTRING_BUNDLE_URL "chrome://pippki/locale/pippki.properties" 33 34 nsNSSDialogs::nsNSSDialogs() = default; 35 36 nsNSSDialogs::~nsNSSDialogs() = default; 37 38 NS_IMPL_ISUPPORTS(nsNSSDialogs, nsITokenPasswordDialogs, nsICertificateDialogs) 39 40 nsresult nsNSSDialogs::Init() { 41 nsresult rv; 42 43 nsCOMPtr<nsIStringBundleService> service = 44 do_GetService(NS_STRINGBUNDLE_CONTRACTID, &rv); 45 if (NS_FAILED(rv)) return rv; 46 47 rv = service->CreateBundle(PIPSTRING_BUNDLE_URL, 48 getter_AddRefs(mPIPStringBundle)); 49 return rv; 50 } 51 52 NS_IMETHODIMP 53 nsNSSDialogs::SetPassword(nsIInterfaceRequestor* ctx, nsIPK11Token* token, 54 /*out*/ bool* canceled) { 55 // |ctx| is allowed to be null. 56 NS_ENSURE_ARG(canceled); 57 58 *canceled = false; 59 60 // Get the parent window for the dialog 61 nsCOMPtr<mozIDOMWindowProxy> parent = do_GetInterface(ctx); 62 63 nsCOMPtr<nsIDialogParamBlock> block = 64 do_CreateInstance(NS_DIALOGPARAMBLOCK_CONTRACTID); 65 if (!block) return NS_ERROR_FAILURE; 66 67 nsCOMPtr<nsIMutableArray> objects = nsArrayBase::Create(); 68 if (!objects) { 69 return NS_ERROR_FAILURE; 70 } 71 nsresult rv = objects->AppendElement(token); 72 if (NS_FAILED(rv)) { 73 return rv; 74 } 75 rv = block->SetObjects(objects); 76 if (NS_FAILED(rv)) { 77 return rv; 78 } 79 80 rv = nsNSSDialogHelper::openDialog( 81 parent, "chrome://pippki/content/changepassword.xhtml", block); 82 83 if (NS_FAILED(rv)) return rv; 84 85 int32_t status; 86 87 rv = block->GetInt(1, &status); 88 if (NS_FAILED(rv)) return rv; 89 90 *canceled = (status == 0); 91 92 return rv; 93 } 94 95 NS_IMETHODIMP 96 nsNSSDialogs::ConfirmDownloadCACert(nsIInterfaceRequestor* ctx, 97 nsIX509Cert* cert, 98 /*out*/ uint32_t* trust, 99 /*out*/ bool* importConfirmed) { 100 // |ctx| is allowed to be null. 101 NS_ENSURE_ARG(cert); 102 NS_ENSURE_ARG(trust); 103 NS_ENSURE_ARG(importConfirmed); 104 105 nsCOMPtr<nsIMutableArray> argArray = nsArrayBase::Create(); 106 if (!argArray) { 107 return NS_ERROR_FAILURE; 108 } 109 110 nsresult rv = argArray->AppendElement(cert); 111 if (NS_FAILED(rv)) { 112 return rv; 113 } 114 115 nsCOMPtr<nsIWritablePropertyBag2> retVals = new nsHashPropertyBag(); 116 rv = argArray->AppendElement(retVals); 117 if (NS_FAILED(rv)) { 118 return rv; 119 } 120 121 // Get the parent window for the dialog 122 nsCOMPtr<mozIDOMWindowProxy> parent = do_GetInterface(ctx); 123 rv = nsNSSDialogHelper::openDialog( 124 parent, "chrome://pippki/content/downloadcert.xhtml", argArray); 125 if (NS_FAILED(rv)) { 126 return rv; 127 } 128 129 rv = retVals->GetPropertyAsBool(u"importConfirmed"_ns, importConfirmed); 130 if (NS_FAILED(rv)) { 131 return rv; 132 } 133 134 *trust = nsIX509CertDB::UNTRUSTED; 135 if (!*importConfirmed) { 136 return NS_OK; 137 } 138 139 bool trustForSSL = false; 140 rv = retVals->GetPropertyAsBool(u"trustForSSL"_ns, &trustForSSL); 141 if (NS_FAILED(rv)) { 142 return rv; 143 } 144 bool trustForEmail = false; 145 rv = retVals->GetPropertyAsBool(u"trustForEmail"_ns, &trustForEmail); 146 if (NS_FAILED(rv)) { 147 return rv; 148 } 149 150 *trust |= trustForSSL ? nsIX509CertDB::TRUSTED_SSL : 0; 151 *trust |= trustForEmail ? nsIX509CertDB::TRUSTED_EMAIL : 0; 152 153 return NS_OK; 154 } 155 156 NS_IMETHODIMP 157 nsNSSDialogs::SetPKCS12FilePassword(nsIInterfaceRequestor* ctx, 158 /*out*/ nsAString& password, 159 /*out*/ bool* confirmedPassword) { 160 // |ctx| is allowed to be null. 161 NS_ENSURE_ARG(confirmedPassword); 162 163 // Get the parent window for the dialog 164 nsCOMPtr<mozIDOMWindowProxy> parent = do_GetInterface(ctx); 165 nsCOMPtr<nsIWritablePropertyBag2> retVals = new nsHashPropertyBag(); 166 nsresult rv = nsNSSDialogHelper::openDialog( 167 parent, "chrome://pippki/content/setp12password.xhtml", retVals); 168 if (NS_FAILED(rv)) { 169 return rv; 170 } 171 172 rv = retVals->GetPropertyAsBool(u"confirmedPassword"_ns, confirmedPassword); 173 if (NS_FAILED(rv)) { 174 return rv; 175 } 176 177 if (!*confirmedPassword) { 178 return NS_OK; 179 } 180 181 return retVals->GetPropertyAsAString(u"password"_ns, password); 182 } 183 184 NS_IMETHODIMP 185 nsNSSDialogs::GetPKCS12FilePassword(nsIInterfaceRequestor* ctx, 186 nsAString& _password, bool* _retval) { 187 *_retval = false; 188 189 nsCOMPtr<nsIPromptService> promptSvc( 190 do_GetService(NS_PROMPTSERVICE_CONTRACTID)); 191 if (!promptSvc) { 192 return NS_ERROR_FAILURE; 193 } 194 195 nsAutoString msg; 196 nsresult rv = 197 mPIPStringBundle->GetStringFromName("getPKCS12FilePasswordMessage", msg); 198 if (NS_FAILED(rv)) { 199 return rv; 200 } 201 202 // Get the parent window for the dialog 203 nsCOMPtr<mozIDOMWindowProxy> parent = do_GetInterface(ctx); 204 char16_t* pwTemp = nullptr; 205 rv = promptSvc->PromptPassword(parent, nullptr, msg.get(), &pwTemp, _retval); 206 if (NS_FAILED(rv)) { 207 return rv; 208 } 209 210 if (*_retval) { 211 _password.Assign(pwTemp); 212 free(pwTemp); 213 } 214 215 return NS_OK; 216 }