2015-05-03 19:32:37 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2014-04-28 07:49:57 +00:00
|
|
|
/* 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 "WifiCertService.h"
|
|
|
|
|
|
|
|
#include "mozilla/ClearOnShutdown.h"
|
2016-05-22 20:31:11 +00:00
|
|
|
#include "mozilla/EndianUtils.h"
|
2014-04-28 07:49:57 +00:00
|
|
|
#include "mozilla/ModuleUtils.h"
|
2015-10-18 05:24:48 +00:00
|
|
|
#include "mozilla/RefPtr.h"
|
2015-05-19 14:36:37 +00:00
|
|
|
#include "mozilla/dom/File.h"
|
2014-06-11 19:38:55 +00:00
|
|
|
#include "mozilla/dom/ToJSValue.h"
|
2014-04-28 07:49:57 +00:00
|
|
|
#include "cert.h"
|
|
|
|
#include "certdb.h"
|
|
|
|
#include "CryptoTask.h"
|
2015-05-18 13:52:26 +00:00
|
|
|
#include "nsIDOMBlob.h"
|
2014-04-28 07:49:57 +00:00
|
|
|
#include "nsIWifiService.h"
|
|
|
|
#include "nsNetUtil.h"
|
2015-07-07 02:17:00 +00:00
|
|
|
#include "nsIInputStream.h"
|
2014-04-28 07:49:57 +00:00
|
|
|
#include "nsServiceManagerUtils.h"
|
|
|
|
#include "nsXULAppAPI.h"
|
|
|
|
#include "ScopedNSSTypes.h"
|
|
|
|
|
|
|
|
#define NS_WIFICERTSERVICE_CID \
|
|
|
|
{ 0x83585afd, 0x0e11, 0x43aa, {0x83, 0x46, 0xf3, 0x4d, 0x97, 0x5e, 0x46, 0x77} }
|
|
|
|
|
|
|
|
using namespace mozilla;
|
|
|
|
using namespace mozilla::dom;
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
// The singleton Wifi Cert service, to be used on the main thread.
|
|
|
|
StaticRefPtr<WifiCertService> gWifiCertService;
|
|
|
|
|
2015-03-21 16:28:04 +00:00
|
|
|
class ImportCertTask final: public CryptoTask
|
2014-04-28 07:49:57 +00:00
|
|
|
{
|
|
|
|
public:
|
2015-05-19 14:36:37 +00:00
|
|
|
ImportCertTask(int32_t aId, Blob* aCertBlob,
|
2014-04-28 07:49:57 +00:00
|
|
|
const nsAString& aCertPassword,
|
|
|
|
const nsAString& aCertNickname)
|
|
|
|
: mBlob(aCertBlob)
|
|
|
|
, mPassword(aCertPassword)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
mResult.mId = aId;
|
|
|
|
mResult.mStatus = 0;
|
|
|
|
mResult.mUsageFlag = 0;
|
|
|
|
mResult.mNickname = aCertNickname;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
virtual void ReleaseNSSResources() {}
|
|
|
|
|
2015-03-21 16:28:04 +00:00
|
|
|
virtual nsresult CalculateResult() override
|
2014-04-28 07:49:57 +00:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
|
|
|
|
|
|
|
// read data from blob.
|
|
|
|
nsCString blobBuf;
|
|
|
|
nsresult rv = ReadBlob(blobBuf);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
char* buf;
|
|
|
|
uint32_t size = blobBuf.GetMutableData(&buf);
|
|
|
|
if (size == 0) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
2015-02-28 13:54:16 +00:00
|
|
|
// Try import as DER format first.
|
|
|
|
rv = ImportDERBlob(buf, size);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try import as PKCS#12 format.
|
|
|
|
return ImportPKCS12Blob(buf, size, mPassword);
|
2014-04-28 07:49:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void CallCallback(nsresult rv)
|
|
|
|
{
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
mResult.mStatus = -1;
|
|
|
|
}
|
|
|
|
gWifiCertService->DispatchResult(mResult);
|
|
|
|
}
|
|
|
|
|
2015-02-28 13:54:16 +00:00
|
|
|
nsresult ImportDERBlob(char* buf, uint32_t size)
|
2014-04-28 07:49:57 +00:00
|
|
|
{
|
|
|
|
// Create certificate object.
|
|
|
|
ScopedCERTCertificate cert(CERT_DecodeCertFromPackage(buf, size));
|
|
|
|
if (!cert) {
|
|
|
|
return MapSECStatus(SECFailure);
|
|
|
|
}
|
|
|
|
|
2015-02-28 13:54:16 +00:00
|
|
|
// Import certificate.
|
|
|
|
return ImportCert(cert);
|
|
|
|
}
|
|
|
|
|
|
|
|
static SECItem*
|
|
|
|
HandleNicknameCollision(SECItem* aOldNickname, PRBool* aCancel, void* aWincx)
|
|
|
|
{
|
|
|
|
const char* dummyName = "Imported User Cert";
|
|
|
|
const size_t dummyNameLen = strlen(dummyName);
|
|
|
|
SECItem* newNick = ::SECITEM_AllocItem(nullptr, nullptr, dummyNameLen + 1);
|
|
|
|
if (!newNick) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
newNick->type = siAsciiString;
|
|
|
|
// Dummy name, will be renamed later.
|
|
|
|
memcpy(newNick->data, dummyName, dummyNameLen + 1);
|
|
|
|
newNick->len = dummyNameLen;
|
|
|
|
|
|
|
|
return newNick;
|
|
|
|
}
|
|
|
|
|
|
|
|
static SECStatus
|
|
|
|
HandleNicknameUpdate(const CERTCertificate *aCert,
|
|
|
|
const SECItem *default_nickname,
|
|
|
|
SECItem **new_nickname,
|
|
|
|
void *arg)
|
|
|
|
{
|
|
|
|
WifiCertServiceResultOptions *result = (WifiCertServiceResultOptions *)arg;
|
|
|
|
|
|
|
|
nsCString userNickname;
|
|
|
|
CopyUTF16toUTF8(result->mNickname, userNickname);
|
|
|
|
|
|
|
|
nsCString fullNickname;
|
|
|
|
if (aCert->isRoot && (aCert->nsCertType & NS_CERT_TYPE_SSL_CA)) {
|
|
|
|
// Accept self-signed SSL CA as server certificate.
|
|
|
|
fullNickname.AssignLiteral("WIFI_SERVERCERT_");
|
|
|
|
fullNickname += userNickname;
|
|
|
|
result->mUsageFlag |= nsIWifiCertService::WIFI_CERT_USAGE_FLAG_SERVER;
|
|
|
|
} else if (aCert->nsCertType & NS_CERT_TYPE_SSL_CLIENT) {
|
|
|
|
// User Certificate
|
|
|
|
fullNickname.AssignLiteral("WIFI_USERCERT_");
|
|
|
|
fullNickname += userNickname;
|
|
|
|
result->mUsageFlag |= nsIWifiCertService::WIFI_CERT_USAGE_FLAG_USER;
|
|
|
|
}
|
|
|
|
char* nickname;
|
|
|
|
uint32_t length = fullNickname.GetMutableData(&nickname);
|
|
|
|
|
|
|
|
SECItem* newNick = ::SECITEM_AllocItem(nullptr, nullptr, length + 1);
|
|
|
|
if (!newNick) {
|
|
|
|
return SECFailure;
|
|
|
|
}
|
|
|
|
|
|
|
|
newNick->type = siAsciiString;
|
|
|
|
memcpy(newNick->data, nickname, length + 1);
|
|
|
|
newNick->len = length;
|
|
|
|
|
|
|
|
*new_nickname = newNick;
|
|
|
|
return SECSuccess;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult ImportPKCS12Blob(char* buf, uint32_t size, const nsAString& aPassword)
|
|
|
|
{
|
|
|
|
nsString password(aPassword);
|
|
|
|
|
|
|
|
// password is null-terminated wide-char string.
|
|
|
|
// passwordItem is required to be big-endian form of password, stored in char
|
|
|
|
// array, including the null-termination.
|
|
|
|
uint32_t length = password.Length() + 1;
|
|
|
|
ScopedSECItem passwordItem(
|
|
|
|
::SECITEM_AllocItem(nullptr, nullptr, length * sizeof(nsString::char_type)));
|
|
|
|
|
|
|
|
if (!passwordItem) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
mozilla::NativeEndian::copyAndSwapToBigEndian(passwordItem->data,
|
|
|
|
password.BeginReading(),
|
|
|
|
length);
|
|
|
|
// Create a decoder.
|
|
|
|
ScopedSEC_PKCS12DecoderContext p12dcx(SEC_PKCS12DecoderStart(
|
|
|
|
passwordItem, nullptr, nullptr,
|
|
|
|
nullptr, nullptr, nullptr, nullptr,
|
|
|
|
nullptr));
|
|
|
|
|
|
|
|
if (!p12dcx) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assign data to decorder.
|
|
|
|
SECStatus srv = SEC_PKCS12DecoderUpdate(p12dcx,
|
|
|
|
reinterpret_cast<unsigned char*>(buf),
|
|
|
|
size);
|
|
|
|
if (srv != SECSuccess) {
|
|
|
|
return MapSECStatus(srv);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify certificates.
|
|
|
|
srv = SEC_PKCS12DecoderVerify(p12dcx);
|
|
|
|
if (srv != SECSuccess) {
|
|
|
|
return MapSECStatus(srv);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set certificate nickname and usage flag.
|
|
|
|
srv = SEC_PKCS12DecoderRenameCertNicknames(p12dcx, HandleNicknameUpdate,
|
|
|
|
&mResult);
|
|
|
|
|
|
|
|
// Validate certificates.
|
|
|
|
srv = SEC_PKCS12DecoderValidateBags(p12dcx, HandleNicknameCollision);
|
|
|
|
if (srv != SECSuccess) {
|
|
|
|
return MapSECStatus(srv);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize slot.
|
|
|
|
ScopedPK11SlotInfo slot(PK11_GetInternalKeySlot());
|
|
|
|
if (!slot) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
if (PK11_NeedLogin(slot) && PK11_NeedUserInit(slot)) {
|
|
|
|
srv = PK11_InitPin(slot, "", "");
|
|
|
|
if (srv != SECSuccess) {
|
|
|
|
return MapSECStatus(srv);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Import cert and key.
|
|
|
|
srv = SEC_PKCS12DecoderImportBags(p12dcx);
|
|
|
|
if (srv != SECSuccess) {
|
|
|
|
return MapSECStatus(srv);
|
|
|
|
}
|
|
|
|
|
|
|
|
// User certificate must be imported from PKCS#12.
|
|
|
|
return (mResult.mUsageFlag & nsIWifiCertService::WIFI_CERT_USAGE_FLAG_USER)
|
|
|
|
? NS_OK : NS_ERROR_FAILURE;
|
2014-04-28 07:49:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult ReadBlob(/*out*/ nsCString& aBuf)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(mBlob);
|
|
|
|
|
|
|
|
static const uint64_t MAX_FILE_SIZE = 16384;
|
2015-05-19 14:36:37 +00:00
|
|
|
|
|
|
|
ErrorResult rv;
|
|
|
|
uint64_t size = mBlob->GetSize(rv);
|
|
|
|
if (NS_WARN_IF(rv.Failed())) {
|
|
|
|
return rv.StealNSResult();
|
2014-04-28 07:49:57 +00:00
|
|
|
}
|
2015-05-19 14:36:37 +00:00
|
|
|
|
2014-04-28 07:49:57 +00:00
|
|
|
if (size > MAX_FILE_SIZE) {
|
|
|
|
return NS_ERROR_FILE_TOO_BIG;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIInputStream> inputStream;
|
2015-05-19 14:36:37 +00:00
|
|
|
mBlob->GetInternalStream(getter_AddRefs(inputStream), rv);
|
|
|
|
if (NS_WARN_IF(rv.Failed())) {
|
|
|
|
return rv.StealNSResult();
|
2014-04-28 07:49:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rv = NS_ReadInputStreamToString(inputStream, aBuf, (uint32_t)size);
|
2015-05-19 14:36:37 +00:00
|
|
|
if (NS_WARN_IF(rv.Failed())) {
|
|
|
|
return rv.StealNSResult();
|
2014-04-28 07:49:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-02-28 13:54:16 +00:00
|
|
|
nsresult ImportCert(CERTCertificate* aCert)
|
2014-04-28 07:49:57 +00:00
|
|
|
{
|
|
|
|
nsCString userNickname, fullNickname;
|
|
|
|
|
2015-02-28 13:54:16 +00:00
|
|
|
CopyUTF16toUTF8(mResult.mNickname, userNickname);
|
2014-04-28 07:49:57 +00:00
|
|
|
// Determine certificate nickname by adding prefix according to its type.
|
|
|
|
if (aCert->isRoot && (aCert->nsCertType & NS_CERT_TYPE_SSL_CA)) {
|
|
|
|
// Accept self-signed SSL CA as server certificate.
|
|
|
|
fullNickname.AssignLiteral("WIFI_SERVERCERT_");
|
|
|
|
fullNickname += userNickname;
|
2015-02-28 13:54:16 +00:00
|
|
|
mResult.mUsageFlag |= nsIWifiCertService::WIFI_CERT_USAGE_FLAG_SERVER;
|
|
|
|
} else if (aCert->nsCertType & NS_CERT_TYPE_SSL_CLIENT) {
|
|
|
|
// User Certificate
|
|
|
|
fullNickname.AssignLiteral("WIFI_USERCERT_");
|
|
|
|
fullNickname += userNickname;
|
|
|
|
mResult.mUsageFlag |= nsIWifiCertService::WIFI_CERT_USAGE_FLAG_USER;
|
2014-04-28 07:49:57 +00:00
|
|
|
} else {
|
|
|
|
return NS_ERROR_ABORT;
|
|
|
|
}
|
|
|
|
|
|
|
|
char* nickname;
|
|
|
|
uint32_t length;
|
|
|
|
length = fullNickname.GetMutableData(&nickname);
|
|
|
|
if (length == 0) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Import certificate, duplicated nickname will cause error.
|
2015-02-28 13:54:16 +00:00
|
|
|
SECStatus srv = CERT_AddTempCertToPerm(aCert, nickname, nullptr);
|
2014-04-28 07:49:57 +00:00
|
|
|
if (srv != SECSuccess) {
|
|
|
|
return MapSECStatus(srv);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<Blob> mBlob;
|
2014-04-28 07:49:57 +00:00
|
|
|
nsString mPassword;
|
|
|
|
WifiCertServiceResultOptions mResult;
|
|
|
|
};
|
|
|
|
|
2015-03-21 16:28:04 +00:00
|
|
|
class DeleteCertTask final: public CryptoTask
|
2014-04-30 11:58:49 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
DeleteCertTask(int32_t aId, const nsAString& aCertNickname)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
mResult.mId = aId;
|
|
|
|
mResult.mStatus = 0;
|
|
|
|
mResult.mUsageFlag = 0;
|
|
|
|
mResult.mNickname = aCertNickname;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
virtual void ReleaseNSSResources() {}
|
|
|
|
|
2015-03-21 16:28:04 +00:00
|
|
|
virtual nsresult CalculateResult() override
|
2014-04-30 11:58:49 +00:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
|
|
|
|
|
|
|
nsCString userNickname;
|
|
|
|
CopyUTF16toUTF8(mResult.mNickname, userNickname);
|
|
|
|
|
|
|
|
// Delete server certificate.
|
|
|
|
nsCString serverCertName("WIFI_SERVERCERT_", 16);
|
|
|
|
serverCertName += userNickname;
|
2014-10-31 06:42:21 +00:00
|
|
|
nsresult rv = deleteCert(serverCertName);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2014-04-30 11:58:49 +00:00
|
|
|
|
2014-10-31 06:42:21 +00:00
|
|
|
// Delete user certificate and private key.
|
|
|
|
nsCString userCertName("WIFI_USERCERT_", 14);
|
|
|
|
userCertName += userNickname;
|
|
|
|
rv = deleteCert(userCertName);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult deleteCert(const nsCString &aCertNickname)
|
|
|
|
{
|
2014-04-30 11:58:49 +00:00
|
|
|
ScopedCERTCertificate cert(
|
2014-10-31 06:42:21 +00:00
|
|
|
CERT_FindCertByNickname(CERT_GetDefaultCertDB(), aCertNickname.get())
|
2014-04-30 11:58:49 +00:00
|
|
|
);
|
2014-10-31 06:42:21 +00:00
|
|
|
// Because we delete certificates in blind, so it's acceptable to delete
|
|
|
|
// a non-exist certificate.
|
2014-04-30 11:58:49 +00:00
|
|
|
if (!cert) {
|
2014-10-31 06:42:21 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
ScopedPK11SlotInfo slot(
|
|
|
|
PK11_KeyForCertExists(cert, nullptr, nullptr)
|
|
|
|
);
|
|
|
|
|
|
|
|
SECStatus srv;
|
|
|
|
if (slot) {
|
|
|
|
// Delete private key along with certificate.
|
|
|
|
srv = PK11_DeleteTokenCertAndKey(cert, nullptr);
|
|
|
|
} else {
|
|
|
|
srv = SEC_DeletePermCertificate(cert);
|
2014-04-30 11:58:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (srv != SECSuccess) {
|
|
|
|
return MapSECStatus(srv);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void CallCallback(nsresult rv)
|
|
|
|
{
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
mResult.mStatus = -1;
|
|
|
|
}
|
|
|
|
gWifiCertService->DispatchResult(mResult);
|
|
|
|
}
|
|
|
|
|
|
|
|
WifiCertServiceResultOptions mResult;
|
|
|
|
};
|
|
|
|
|
2014-04-28 07:49:57 +00:00
|
|
|
NS_IMPL_ISUPPORTS(WifiCertService, nsIWifiCertService)
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
WifiCertService::Start(nsIWifiEventListener* aListener)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aListener);
|
|
|
|
|
|
|
|
mListener = aListener;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
WifiCertService::Shutdown()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2014-07-31 02:31:18 +00:00
|
|
|
|
|
|
|
mListener = nullptr;
|
|
|
|
|
2014-04-28 07:49:57 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
WifiCertService::DispatchResult(const WifiCertServiceResultOptions& aOptions)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
mozilla::AutoSafeJSContext cx;
|
|
|
|
JS::RootedValue val(cx);
|
|
|
|
nsCString dummyInterface;
|
|
|
|
|
2014-06-11 19:38:55 +00:00
|
|
|
if (!ToJSValue(cx, aOptions, &val)) {
|
2014-04-28 07:49:57 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Certll the listener with a JS value.
|
|
|
|
mListener->OnCommand(val, dummyInterface);
|
|
|
|
}
|
|
|
|
|
|
|
|
WifiCertService::WifiCertService()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(!gWifiCertService);
|
|
|
|
}
|
|
|
|
|
|
|
|
WifiCertService::~WifiCertService()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!gWifiCertService);
|
2016-01-22 22:49:39 +00:00
|
|
|
|
|
|
|
nsNSSShutDownPreventionLock locker;
|
|
|
|
if (isAlreadyShutDown()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
shutdown(calledFromObject);
|
2014-04-28 07:49:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<WifiCertService>
|
|
|
|
WifiCertService::FactoryCreate()
|
|
|
|
{
|
2015-07-04 01:29:00 +00:00
|
|
|
if (!XRE_IsParentProcess()) {
|
2014-04-28 07:49:57 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
if (!gWifiCertService) {
|
|
|
|
gWifiCertService = new WifiCertService();
|
|
|
|
ClearOnShutdown(&gWifiCertService);
|
|
|
|
}
|
|
|
|
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<WifiCertService> service = gWifiCertService.get();
|
2014-04-28 07:49:57 +00:00
|
|
|
return service.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
WifiCertService::ImportCert(int32_t aId, nsIDOMBlob* aCertBlob,
|
|
|
|
const nsAString& aCertPassword,
|
|
|
|
const nsAString& aCertNickname)
|
|
|
|
{
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<Blob> blob = static_cast<Blob*>(aCertBlob);
|
|
|
|
RefPtr<CryptoTask> task = new ImportCertTask(aId, blob, aCertPassword,
|
2014-04-28 07:49:57 +00:00
|
|
|
aCertNickname);
|
|
|
|
return task->Dispatch("WifiImportCert");
|
|
|
|
}
|
|
|
|
|
2014-04-30 11:58:49 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
WifiCertService::DeleteCert(int32_t aId, const nsAString& aCertNickname)
|
|
|
|
{
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<CryptoTask> task = new DeleteCertTask(aId, aCertNickname);
|
2014-04-30 11:58:49 +00:00
|
|
|
return task->Dispatch("WifiDeleteCert");
|
|
|
|
}
|
|
|
|
|
2015-02-28 13:54:28 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
WifiCertService::HasPrivateKey(const nsAString& aCertNickname, bool *aHasKey)
|
|
|
|
{
|
|
|
|
*aHasKey = false;
|
|
|
|
|
|
|
|
nsNSSShutDownPreventionLock locker;
|
|
|
|
if (isAlreadyShutDown()) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCString certNickname;
|
|
|
|
CopyUTF16toUTF8(aCertNickname, certNickname);
|
|
|
|
|
|
|
|
ScopedCERTCertificate cert(
|
|
|
|
CERT_FindCertByNickname(CERT_GetDefaultCertDB(), certNickname.get())
|
|
|
|
);
|
|
|
|
if (!cert) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
ScopedPK11SlotInfo slot(
|
|
|
|
PK11_KeyForCertExists(cert, nullptr, nullptr)
|
|
|
|
);
|
|
|
|
if (slot) {
|
|
|
|
*aHasKey = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-04-28 07:49:57 +00:00
|
|
|
NS_GENERIC_FACTORY_SINGLETON_CONSTRUCTOR(WifiCertService,
|
|
|
|
WifiCertService::FactoryCreate)
|
|
|
|
|
|
|
|
NS_DEFINE_NAMED_CID(NS_WIFICERTSERVICE_CID);
|
|
|
|
|
|
|
|
static const mozilla::Module::CIDEntry kWifiCertServiceCIDs[] = {
|
|
|
|
{ &kNS_WIFICERTSERVICE_CID, false, nullptr, WifiCertServiceConstructor },
|
|
|
|
{ nullptr }
|
|
|
|
};
|
|
|
|
|
|
|
|
static const mozilla::Module::ContractIDEntry kWifiCertServiceContracts[] = {
|
|
|
|
{ "@mozilla.org/wifi/certservice;1", &kNS_WIFICERTSERVICE_CID },
|
|
|
|
{ nullptr }
|
|
|
|
};
|
|
|
|
|
|
|
|
static const mozilla::Module kWifiCertServiceModule = {
|
|
|
|
mozilla::Module::kVersion,
|
|
|
|
kWifiCertServiceCIDs,
|
|
|
|
kWifiCertServiceContracts,
|
|
|
|
nullptr
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
NSMODULE_DEFN(WifiCertServiceModule) = &kWifiCertServiceModule;
|