tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

nsNSSCertHelper.cpp (2637B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 #include "nsNSSCertHelper.h"
      6 
      7 #include "ScopedNSSTypes.h"
      8 #include "mozilla/Assertions.h"
      9 #include "mozilla/Sprintf.h"
     10 #include "mozilla/Utf8.h"
     11 #include "mozilla/net/DNS.h"
     12 #include "nsCOMPtr.h"
     13 #include "nsIStringBundle.h"
     14 #include "nsNSSCertificate.h"
     15 #include "nsReadableUtils.h"
     16 #include "nsServiceManagerUtils.h"
     17 #include "nsThreadUtils.h"
     18 #include "prerror.h"
     19 #include "prnetdb.h"
     20 #include "secder.h"
     21 
     22 using namespace mozilla;
     23 
     24 static nsresult GetPIPNSSBundle(nsIStringBundle** pipnssBundle) {
     25  nsCOMPtr<nsIStringBundleService> bundleService(
     26      do_GetService(NS_STRINGBUNDLE_CONTRACTID));
     27  if (!bundleService) {
     28    return NS_ERROR_NOT_AVAILABLE;
     29  }
     30  return bundleService->CreateBundle("chrome://pipnss/locale/pipnss.properties",
     31                                     pipnssBundle);
     32 }
     33 
     34 nsresult GetPIPNSSBundleString(const char* stringName, nsAString& result) {
     35  MOZ_ASSERT(NS_IsMainThread());
     36  if (!NS_IsMainThread()) {
     37    return NS_ERROR_NOT_SAME_THREAD;
     38  }
     39  MOZ_ASSERT(stringName);
     40  if (!stringName) {
     41    return NS_ERROR_INVALID_ARG;
     42  }
     43  nsCOMPtr<nsIStringBundle> pipnssBundle;
     44  nsresult rv = GetPIPNSSBundle(getter_AddRefs(pipnssBundle));
     45  if (NS_FAILED(rv)) {
     46    return rv;
     47  }
     48  result.Truncate();
     49  return pipnssBundle->GetStringFromName(stringName, result);
     50 }
     51 
     52 nsresult GetPIPNSSBundleString(const char* stringName, nsACString& result) {
     53  nsAutoString tmp;
     54  nsresult rv = GetPIPNSSBundleString(stringName, tmp);
     55  if (NS_FAILED(rv)) {
     56    return rv;
     57  }
     58  result.Assign(NS_ConvertUTF16toUTF8(tmp));
     59  return NS_OK;
     60 }
     61 
     62 nsresult PIPBundleFormatStringFromName(const char* stringName,
     63                                       const nsTArray<nsString>& params,
     64                                       nsAString& result) {
     65  MOZ_ASSERT(stringName);
     66  MOZ_ASSERT(!params.IsEmpty());
     67  if (!stringName || params.IsEmpty()) {
     68    return NS_ERROR_INVALID_ARG;
     69  }
     70  nsCOMPtr<nsIStringBundle> pipnssBundle;
     71  nsresult rv = GetPIPNSSBundle(getter_AddRefs(pipnssBundle));
     72  if (NS_FAILED(rv)) {
     73    return rv;
     74  }
     75  result.Truncate();
     76  return pipnssBundle->FormatStringFromName(stringName, params, result);
     77 }
     78 
     79 void LossyUTF8ToUTF16(const char* str, uint32_t len,
     80                      /*out*/ nsAString& result) {
     81  auto span = Span(str, len);
     82  if (IsUtf8(span)) {
     83    CopyUTF8toUTF16(span, result);
     84  } else {
     85    // Actually Latin1 despite ASCII in the legacy name
     86    CopyASCIItoUTF16(span, result);
     87  }
     88 }