Files
palemoon27/security/manager/ssl/NSSErrorsService.cpp
T
roytam1 d9d6a919f9 import changes from `dev' branch of rmottola/Arctic-Fox:
- Bug 1124608 Support D3E EventModifierInit r=smaug (8238935214)
- Bug 1164981 - Add MouseEvent.movementX/Y. r=masayuki, r=ehsan (5e59178c38)
- Bug 1204439 part.1 Add NS_EVENT_MESSAGE_FIRST_LAST() into EventMessageList.h for defining e*EventFirst and e*EventLast r=smaug (c5750576b4)
- Bug 1204439 part.2 NS_EVENT_MESSAGE() shouldn't take specific value for the event message r=smaug (eaea28b6b7)
- update like 895274 (d17845605a)
- Bug 1203364 IMEContentObserver should notify IME of selection change with the latest change reason r=smaug (59d9c7d3b7)
- Bug 1203381 part.1 IMEContentObserver shouldn't clear mTextChangeData until immediately before sending a text change notification r=smaug (770aa44c43)
- Bug 1203381 part.2 Merge all IME notification sending events of IMEContentObserver to a runnable class r=smaug (d4faa0e5c8)
- Bug 1203381 part.3 IMEContentObserver::mIsFlushingPendingNotifications should be cleared when all pending notifications are sent to IME r=smaug (6f74f02106)
- Bug 1203381 part.4 IMENotificationSender should keep the order of notifications even when a notification causes another change r=smaug (d6a411c1e9)
- Bug 1203381 part.5 IMENotificationSender shouldn't send notification recursively r=smaug (602cffffb0)
- Bug 1203381 part.6 IMEContentObserver shouldn't post position change event if Reflow() is called during handling a query content event and sending NOTIFY_IME_OF_POSITION_CHANGE since the result of query content event includes the latest layout information r=smaug (e3f843d991)
- Bug 1203381 part.7 Rename IMEContentObserver::mIs*ChangeEventPending to IMEContentObserver::mNeedsToNotifyIMEOf*Change r=smaug (9fddde18a6)
- Bug 1204439 part.3 Create methods to get enum item name r=smaug (2be37da179)
-  Bug 1182551 - Updating nsSecureBrowserUIImpl so that insecure pages with mixed content iframes don't get marked as broken. r=keeler (a46e2cf3ac)
- Bug 1198669 - Add nsIMultiPartChannel.originalResponseHeader support. r=valentin (7c28524a6d)
- bug 496234 - use stdint types in md4 implementation r=mayhemer (721f86c2b1)
- bug 496234 - fix md4 implementation by appending the input length as a 64-bit number r=mayhemer (51637a359f)
- revert PM specific patch, checked against FF 60 & 68 (c133d93f93)
- some let->var, code style (d5c6b316da)
2022-05-06 11:54:12 +08:00

206 lines
5.7 KiB
C++

/* 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/. */
#include "NSSErrorsService.h"
#include "nsNSSComponent.h"
#include "nsServiceManagerUtils.h"
#include "pkix/pkixnss.h"
#include "secerr.h"
#include "sslerr.h"
#define PIPNSS_STRBUNDLE_URL "chrome://pipnss/locale/pipnss.properties"
#define NSSERR_STRBUNDLE_URL "chrome://pipnss/locale/nsserrors.properties"
namespace mozilla {
namespace psm {
static_assert(mozilla::pkix::ERROR_BASE ==
nsINSSErrorsService::MOZILLA_PKIX_ERROR_BASE,
"MOZILLA_PKIX_ERROR_BASE and "
"nsINSSErrorsService::MOZILLA_PKIX_ERROR_BASE do not match.");
static_assert(mozilla::pkix::ERROR_LIMIT ==
nsINSSErrorsService::MOZILLA_PKIX_ERROR_LIMIT,
"MOZILLA_PKIX_ERROR_LIMIT and "
"nsINSSErrorsService::MOZILLA_PKIX_ERROR_LIMIT do not match.");
static bool
IsPSMError(PRErrorCode error)
{
return (error >= mozilla::pkix::ERROR_BASE &&
error < mozilla::pkix::ERROR_LIMIT);
}
NS_IMPL_ISUPPORTS(NSSErrorsService, nsINSSErrorsService)
NSSErrorsService::~NSSErrorsService() { }
nsresult
NSSErrorsService::Init()
{
nsresult rv;
nsCOMPtr<nsIStringBundleService> bundleService(do_GetService(NS_STRINGBUNDLE_CONTRACTID, &rv));
if (NS_FAILED(rv) || !bundleService)
return NS_ERROR_FAILURE;
bundleService->CreateBundle(PIPNSS_STRBUNDLE_URL,
getter_AddRefs(mPIPNSSBundle));
if (!mPIPNSSBundle)
rv = NS_ERROR_FAILURE;
bundleService->CreateBundle(NSSERR_STRBUNDLE_URL,
getter_AddRefs(mNSSErrorsBundle));
if (!mNSSErrorsBundle)
rv = NS_ERROR_FAILURE;
return rv;
}
#define EXPECTED_SEC_ERROR_BASE (-0x2000)
#define EXPECTED_SSL_ERROR_BASE (-0x3000)
#if SEC_ERROR_BASE != EXPECTED_SEC_ERROR_BASE || SSL_ERROR_BASE != EXPECTED_SSL_ERROR_BASE
#error "Unexpected change of error code numbers in lib NSS, please adjust the mapping code"
/*
* Please ensure the NSS error codes are mapped into the positive range 0x1000 to 0xf000
* Search for NS_ERROR_MODULE_SECURITY to ensure there are no conflicts.
* The current code also assumes that NSS library error codes are negative.
*/
#endif
bool
IsNSSErrorCode(PRErrorCode code)
{
return IS_SEC_ERROR(code) || IS_SSL_ERROR(code) || IsPSMError(code);
}
nsresult
GetXPCOMFromNSSError(PRErrorCode code)
{
if (!code) {
MOZ_CRASH("Function failed without calling PR_GetError");
}
// The error codes within each module must be a 16 bit value.
// For simplicity we use the positive value of the NSS code.
return (nsresult)NS_ERROR_GENERATE_FAILURE(NS_ERROR_MODULE_SECURITY,
-1 * code);
}
NS_IMETHODIMP
NSSErrorsService::IsNSSErrorCode(int32_t aNSPRCode, bool *_retval)
{
if (!_retval) {
return NS_ERROR_INVALID_ARG;
}
*_retval = mozilla::psm::IsNSSErrorCode(aNSPRCode);
return NS_OK;
}
NS_IMETHODIMP
NSSErrorsService::GetXPCOMFromNSSError(int32_t aNSPRCode, nsresult *aXPCOMErrorCode)
{
if (!aXPCOMErrorCode) {
return NS_ERROR_INVALID_ARG;
}
if (!mozilla::psm::IsNSSErrorCode(aNSPRCode)) {
return NS_ERROR_INVALID_ARG;
}
*aXPCOMErrorCode = mozilla::psm::GetXPCOMFromNSSError(aNSPRCode);
return NS_OK;
}
NS_IMETHODIMP
NSSErrorsService::GetErrorClass(nsresult aXPCOMErrorCode, uint32_t *aErrorClass)
{
NS_ENSURE_ARG(aErrorClass);
if (NS_ERROR_GET_MODULE(aXPCOMErrorCode) != NS_ERROR_MODULE_SECURITY ||
NS_ERROR_GET_SEVERITY(aXPCOMErrorCode) != NS_ERROR_SEVERITY_ERROR) {
return NS_ERROR_FAILURE;
}
int32_t aNSPRCode = -1 * NS_ERROR_GET_CODE(aXPCOMErrorCode);
if (!mozilla::psm::IsNSSErrorCode(aNSPRCode)) {
return NS_ERROR_FAILURE;
}
if (mozilla::psm::ErrorIsOverridable(aNSPRCode)) {
*aErrorClass = ERROR_CLASS_BAD_CERT;
} else {
*aErrorClass = ERROR_CLASS_SSL_PROTOCOL;
}
return NS_OK;
}
bool
ErrorIsOverridable(PRErrorCode code)
{
switch (code)
{
// Overridable errors.
case mozilla::pkix::MOZILLA_PKIX_ERROR_CA_CERT_USED_AS_END_ENTITY:
case mozilla::pkix::MOZILLA_PKIX_ERROR_INADEQUATE_KEY_SIZE:
case mozilla::pkix::MOZILLA_PKIX_ERROR_NOT_YET_VALID_CERTIFICATE:
case mozilla::pkix::MOZILLA_PKIX_ERROR_NOT_YET_VALID_ISSUER_CERTIFICATE:
case mozilla::pkix::MOZILLA_PKIX_ERROR_V1_CERT_USED_AS_CA:
case SEC_ERROR_CA_CERT_INVALID:
case SEC_ERROR_CERT_SIGNATURE_ALGORITHM_DISABLED:
case SEC_ERROR_EXPIRED_CERTIFICATE:
case SEC_ERROR_EXPIRED_ISSUER_CERTIFICATE:
case SEC_ERROR_INVALID_TIME:
case SEC_ERROR_UNKNOWN_ISSUER:
case SSL_ERROR_BAD_CERT_DOMAIN:
return true;
// Non-overridable errors.
default:
return false;
}
}
NS_IMETHODIMP
NSSErrorsService::GetErrorMessage(nsresult aXPCOMErrorCode, nsAString &aErrorMessage)
{
if (NS_ERROR_GET_MODULE(aXPCOMErrorCode) != NS_ERROR_MODULE_SECURITY ||
NS_ERROR_GET_SEVERITY(aXPCOMErrorCode) != NS_ERROR_SEVERITY_ERROR) {
return NS_ERROR_FAILURE;
}
int32_t aNSPRCode = -1 * NS_ERROR_GET_CODE(aXPCOMErrorCode);
if (!mozilla::psm::IsNSSErrorCode(aNSPRCode)) {
return NS_ERROR_FAILURE;
}
nsCOMPtr<nsIStringBundle> theBundle = mPIPNSSBundle;
const char *id_str = nsNSSErrors::getOverrideErrorStringName(aNSPRCode);
if (!id_str) {
id_str = nsNSSErrors::getDefaultErrorStringName(aNSPRCode);
theBundle = mNSSErrorsBundle;
}
if (!id_str || !theBundle) {
return NS_ERROR_FAILURE;
}
nsAutoString msg;
nsresult rv =
theBundle->GetStringFromName(NS_ConvertASCIItoUTF16(id_str).get(),
getter_Copies(msg));
if (NS_SUCCEEDED(rv)) {
aErrorMessage = msg;
}
return rv;
}
} // namespace psm
} // namespace mozilla