2012-05-31 09:33:35 +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/. */
|
2005-06-02 01:14:02 +00:00
|
|
|
|
2016-05-10 01:02:40 +00:00
|
|
|
#include "nsSmartCardMonitor.h"
|
|
|
|
|
|
|
|
#include "ScopedNSSTypes.h"
|
2014-07-24 20:00:46 +00:00
|
|
|
#include "mozilla/Services.h"
|
2012-12-01 00:59:42 +00:00
|
|
|
#include "mozilla/unused.h"
|
2014-07-24 20:00:46 +00:00
|
|
|
#include "nsIObserverService.h"
|
2014-07-24 19:58:14 +00:00
|
|
|
#include "nsServiceManagerUtils.h"
|
2014-08-14 16:38:42 +00:00
|
|
|
#include "nsThreadUtils.h"
|
2016-05-10 01:02:40 +00:00
|
|
|
#include "nspr.h"
|
2014-07-24 19:58:14 +00:00
|
|
|
#include "pk11func.h"
|
2012-12-01 00:59:42 +00:00
|
|
|
|
|
|
|
using namespace mozilla;
|
2005-06-02 01:14:02 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// The SmartCard monitoring thread should start up for each module we load
|
|
|
|
// that has removable tokens. This code calls an NSS function which waits
|
2014-07-24 19:59:32 +00:00
|
|
|
// until there is a change in the token state. NSS uses the
|
2005-06-02 01:14:02 +00:00
|
|
|
// C_WaitForSlotEvent() call in PKCS #11 if the module implements the call,
|
2014-07-24 19:59:32 +00:00
|
|
|
// otherwise NSS will poll the token in a loop with a delay of 'latency'
|
2005-06-02 01:14:02 +00:00
|
|
|
// between polls. Note that the C_WaitForSlotEvent() may wake up on any type
|
|
|
|
// of token event, so it's necessary to filter these events down to just the
|
|
|
|
// insertion and removal events we are looking for.
|
|
|
|
//
|
2014-07-24 19:59:32 +00:00
|
|
|
// Once the event is found, it is dispatched to the main thread to notify
|
|
|
|
// any window where window.crypto.enableSmartCardEvents is true.
|
2014-07-24 20:00:46 +00:00
|
|
|
// Additionally, all observers of the topics "smartcard-insert" and
|
|
|
|
// "smartcard-remove" are notified by the observer service of the appropriate
|
|
|
|
// event.
|
2005-06-02 01:14:02 +00:00
|
|
|
//
|
|
|
|
|
2014-07-24 19:58:14 +00:00
|
|
|
class nsTokenEventRunnable : public nsIRunnable {
|
|
|
|
public:
|
2014-07-24 19:59:32 +00:00
|
|
|
nsTokenEventRunnable(const nsAString& aType, const nsAString& aTokenName)
|
|
|
|
: mType(aType)
|
|
|
|
, mTokenName(aTokenName)
|
|
|
|
{
|
|
|
|
}
|
2014-07-24 19:58:14 +00:00
|
|
|
|
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
2014-07-24 19:59:32 +00:00
|
|
|
NS_DECL_NSIRUNNABLE
|
|
|
|
|
2014-07-24 19:58:14 +00:00
|
|
|
private:
|
2014-07-24 19:59:32 +00:00
|
|
|
virtual ~nsTokenEventRunnable() {}
|
2014-07-24 19:58:14 +00:00
|
|
|
|
|
|
|
nsString mType;
|
|
|
|
nsString mTokenName;
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS(nsTokenEventRunnable, nsIRunnable)
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsTokenEventRunnable::Run()
|
|
|
|
{
|
2014-07-24 19:59:32 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2014-07-24 20:00:46 +00:00
|
|
|
nsCOMPtr<nsIObserverService> observerService =
|
|
|
|
mozilla::services::GetObserverService();
|
|
|
|
if (!observerService) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
// This conversion is safe because mType can only be "smartcard-insert"
|
|
|
|
// or "smartcard-remove".
|
|
|
|
NS_ConvertUTF16toUTF8 eventTypeUTF8(mType);
|
2014-08-14 16:38:42 +00:00
|
|
|
return observerService->NotifyObservers(nullptr, eventTypeUTF8.get(),
|
|
|
|
mTokenName.get());
|
2014-07-24 19:58:14 +00:00
|
|
|
}
|
2005-06-02 01:14:02 +00:00
|
|
|
|
|
|
|
// self linking and removing double linked entry
|
|
|
|
// adopts the thread it is passed.
|
2014-07-24 19:59:32 +00:00
|
|
|
class SmartCardThreadEntry
|
|
|
|
{
|
2005-06-02 01:14:02 +00:00
|
|
|
public:
|
2014-07-24 19:59:32 +00:00
|
|
|
friend class SmartCardThreadList;
|
|
|
|
SmartCardThreadEntry(SmartCardMonitoringThread *thread,
|
|
|
|
SmartCardThreadEntry *next,
|
|
|
|
SmartCardThreadEntry *prev,
|
|
|
|
SmartCardThreadEntry **head)
|
|
|
|
: next(next)
|
|
|
|
, prev(prev)
|
|
|
|
, head(head)
|
|
|
|
, thread(thread)
|
|
|
|
{
|
|
|
|
if (prev) {
|
|
|
|
prev->next = this;
|
|
|
|
} else {
|
|
|
|
*head = this;
|
|
|
|
}
|
|
|
|
if (next) {
|
|
|
|
next->prev = this;
|
|
|
|
}
|
2005-06-02 01:14:02 +00:00
|
|
|
}
|
2014-07-24 19:59:32 +00:00
|
|
|
|
|
|
|
~SmartCardThreadEntry()
|
|
|
|
{
|
|
|
|
if (prev) {
|
|
|
|
prev->next = next;
|
|
|
|
} else {
|
|
|
|
*head = next;
|
|
|
|
}
|
|
|
|
if (next) {
|
|
|
|
next->prev = prev;
|
|
|
|
}
|
2005-06-02 01:14:02 +00:00
|
|
|
// NOTE: automatically stops the thread
|
|
|
|
delete thread;
|
|
|
|
}
|
2014-07-24 19:59:32 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
SmartCardThreadEntry *next;
|
|
|
|
SmartCardThreadEntry *prev;
|
|
|
|
SmartCardThreadEntry **head;
|
|
|
|
SmartCardMonitoringThread *thread;
|
2005-06-02 01:14:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//
|
|
|
|
// SmartCardThreadList is a class to help manage the running threads.
|
|
|
|
// That way new threads could be started and old ones terminated as we
|
|
|
|
// load and unload modules.
|
|
|
|
//
|
|
|
|
SmartCardThreadList::SmartCardThreadList() : head(0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
SmartCardThreadList::~SmartCardThreadList()
|
|
|
|
{
|
|
|
|
// the head is self linking and unlinking, the following
|
|
|
|
// loop removes all entries on the list.
|
|
|
|
// it will also stop the thread if it happens to be running
|
|
|
|
while (head) {
|
|
|
|
delete head;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SmartCardThreadList::Remove(SECMODModule *aModule)
|
|
|
|
{
|
2014-07-24 19:59:32 +00:00
|
|
|
for (SmartCardThreadEntry* current = head; current;
|
|
|
|
current = current->next) {
|
2005-06-02 01:14:02 +00:00
|
|
|
if (current->thread->GetModule() == aModule) {
|
|
|
|
// NOTE: automatically stops the thread and dequeues it from the list
|
|
|
|
delete current;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-01 00:59:42 +00:00
|
|
|
// adopts the thread passed to it. Starts the thread as well
|
2005-06-02 01:14:02 +00:00
|
|
|
nsresult
|
2014-07-24 19:59:32 +00:00
|
|
|
SmartCardThreadList::Add(SmartCardMonitoringThread* thread)
|
2005-06-02 01:14:02 +00:00
|
|
|
{
|
2014-07-24 19:59:32 +00:00
|
|
|
SmartCardThreadEntry* current = new SmartCardThreadEntry(thread, head,
|
|
|
|
nullptr, &head);
|
2012-12-01 00:59:42 +00:00
|
|
|
// OK to forget current here, it's on the list.
|
2015-11-02 05:53:26 +00:00
|
|
|
Unused << current;
|
2012-12-01 00:59:42 +00:00
|
|
|
|
2012-10-17 20:48:36 +00:00
|
|
|
return thread->Start();
|
2005-06-02 01:14:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// We really should have a Unity PL Hash function...
|
2012-09-25 16:18:38 +00:00
|
|
|
static PLHashNumber
|
2014-07-24 19:59:32 +00:00
|
|
|
unity(const void* key) { return PLHashNumber(NS_PTR_TO_INT32(key)); }
|
2005-06-02 01:14:02 +00:00
|
|
|
|
2014-07-24 19:59:32 +00:00
|
|
|
SmartCardMonitoringThread::SmartCardMonitoringThread(SECMODModule* module_)
|
2012-07-30 14:20:58 +00:00
|
|
|
: mThread(nullptr)
|
2005-06-02 01:14:02 +00:00
|
|
|
{
|
|
|
|
mModule = SECMOD_ReferenceModule(module_);
|
|
|
|
// simple hash functions, most modules have less than 3 slots, so 10 buckets
|
|
|
|
// should be plenty
|
2014-07-24 19:59:32 +00:00
|
|
|
mHash = PL_NewHashTable(10, unity, PL_CompareValues, PL_CompareStrings,
|
|
|
|
nullptr, 0);
|
2005-06-02 01:14:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// when we shutdown the thread, be sure to stop it first. If not, it just might
|
|
|
|
// crash when the mModule it is looking at disappears.
|
|
|
|
//
|
|
|
|
SmartCardMonitoringThread::~SmartCardMonitoringThread()
|
|
|
|
{
|
|
|
|
Stop();
|
|
|
|
SECMOD_DestroyModule(mModule);
|
|
|
|
if (mHash) {
|
|
|
|
PL_HashTableDestroy(mHash);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
SmartCardMonitoringThread::Start()
|
|
|
|
{
|
|
|
|
if (!mThread) {
|
|
|
|
mThread = PR_CreateThread(PR_SYSTEM_THREAD, LaunchExecute, this,
|
2014-04-04 02:29:40 +00:00
|
|
|
PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD,
|
2005-06-02 01:14:02 +00:00
|
|
|
PR_JOINABLE_THREAD, 0);
|
|
|
|
}
|
|
|
|
return mThread ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Should only stop if we are through with the module.
|
|
|
|
// CancelWait has the side effect of losing all the keys and
|
|
|
|
// current operations on the module!. (See the comment in
|
|
|
|
// SECMOD_CancelWait for why this is so..).
|
|
|
|
//
|
|
|
|
void SmartCardMonitoringThread::Stop()
|
|
|
|
{
|
|
|
|
SECStatus rv;
|
|
|
|
|
|
|
|
rv = SECMOD_CancelWait(mModule);
|
|
|
|
if (rv != SECSuccess) {
|
2014-07-24 19:59:32 +00:00
|
|
|
// we didn't wake up the Wait, so don't try to join the thread
|
2005-06-02 01:14:02 +00:00
|
|
|
// otherwise we will hang forever...
|
|
|
|
return;
|
|
|
|
}
|
2014-07-24 19:59:32 +00:00
|
|
|
|
2005-06-02 01:14:02 +00:00
|
|
|
// confused about the memory model here? NSPR owns the memory for
|
|
|
|
// threads. non-joinable threads are freed when the thread dies.
|
|
|
|
// joinable threads are freed after the call to PR_JoinThread.
|
|
|
|
// That means if SECMOD_CancelWait fails, we'll leak the mThread
|
|
|
|
// structure. this is considered preferable to hanging (which is
|
|
|
|
// what will happen if we try to join a thread that blocked).
|
|
|
|
if (mThread) {
|
|
|
|
PR_JoinThread(mThread);
|
2014-07-24 19:59:32 +00:00
|
|
|
mThread = 0;
|
2005-06-02 01:14:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// remember the name and series of a token in a particular slot.
|
|
|
|
// This is important because the name is no longer available when
|
|
|
|
// the token is removed. If listeners depended on this information,
|
|
|
|
// They would be out of luck. It also is a handy way of making sure
|
|
|
|
// we don't generate spurious insertion and removal events as the slot
|
|
|
|
// cycles through various states.
|
|
|
|
//
|
|
|
|
void
|
2014-07-24 19:59:32 +00:00
|
|
|
SmartCardMonitoringThread::SetTokenName(CK_SLOT_ID slotid,
|
|
|
|
const char* tokenName, uint32_t series)
|
2005-06-02 01:14:02 +00:00
|
|
|
{
|
|
|
|
if (mHash) {
|
|
|
|
if (tokenName) {
|
|
|
|
int len = strlen(tokenName) + 1;
|
2009-11-15 04:54:46 +00:00
|
|
|
/* this must match the allocator used in
|
|
|
|
* PLHashAllocOps.freeEntry DefaultFreeEntry */
|
2014-07-24 19:59:32 +00:00
|
|
|
char* entry = (char*)PR_Malloc(len + sizeof(uint32_t));
|
2005-06-02 01:14:02 +00:00
|
|
|
|
2014-07-24 19:59:32 +00:00
|
|
|
if (entry) {
|
|
|
|
memcpy(entry, &series, sizeof(uint32_t));
|
|
|
|
memcpy(&entry[sizeof(uint32_t)], tokenName, len);
|
|
|
|
|
|
|
|
PL_HashTableAdd(mHash, (void*)(uintptr_t)slotid, entry); /* adopt */
|
2005-06-02 01:14:02 +00:00
|
|
|
return;
|
|
|
|
}
|
2014-07-24 19:59:32 +00:00
|
|
|
} else {
|
2009-11-15 04:54:46 +00:00
|
|
|
// if tokenName was not provided, remove the old one (implicit delete)
|
2014-07-24 19:59:32 +00:00
|
|
|
PL_HashTableRemove(mHash, (void*)(uintptr_t)slotid);
|
2009-11-15 04:54:46 +00:00
|
|
|
}
|
2005-06-02 01:14:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// retrieve the name saved above
|
2014-07-24 19:59:32 +00:00
|
|
|
const char*
|
2005-06-02 01:14:02 +00:00
|
|
|
SmartCardMonitoringThread::GetTokenName(CK_SLOT_ID slotid)
|
|
|
|
{
|
2014-07-24 19:59:32 +00:00
|
|
|
const char* tokenName = nullptr;
|
|
|
|
const char* entry;
|
2005-06-02 01:14:02 +00:00
|
|
|
|
|
|
|
if (mHash) {
|
2014-07-24 19:59:32 +00:00
|
|
|
entry = (const char*)PL_HashTableLookupConst(mHash,
|
|
|
|
(void*)(uintptr_t)slotid);
|
2005-06-02 01:14:02 +00:00
|
|
|
if (entry) {
|
2012-08-22 15:56:38 +00:00
|
|
|
tokenName = &entry[sizeof(uint32_t)];
|
2005-06-02 01:14:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return tokenName;
|
|
|
|
}
|
|
|
|
|
|
|
|
// retrieve the series saved in SetTokenName above
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t
|
2005-06-02 01:14:02 +00:00
|
|
|
SmartCardMonitoringThread::GetTokenSeries(CK_SLOT_ID slotid)
|
|
|
|
{
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t series = 0;
|
2014-07-24 19:59:32 +00:00
|
|
|
const char* entry;
|
2005-06-02 01:14:02 +00:00
|
|
|
|
|
|
|
if (mHash) {
|
2014-07-24 19:59:32 +00:00
|
|
|
entry = (const char*)PL_HashTableLookupConst(mHash,
|
|
|
|
(void*)(uintptr_t)slotid);
|
2005-06-02 01:14:02 +00:00
|
|
|
if (entry) {
|
2014-07-24 19:59:32 +00:00
|
|
|
memcpy(&series, entry, sizeof(uint32_t));
|
2005-06-02 01:14:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return series;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// helper function to pass the event off to nsNSSComponent.
|
|
|
|
//
|
2014-07-24 19:59:32 +00:00
|
|
|
void
|
|
|
|
SmartCardMonitoringThread::SendEvent(const nsAString& eventType,
|
|
|
|
const char* tokenName)
|
2005-06-02 01:14:02 +00:00
|
|
|
{
|
2014-07-24 19:59:32 +00:00
|
|
|
// The token name should be UTF8, but it's not clear that this is enforced
|
|
|
|
// by NSS. To be safe, we explicitly check here before converting it to
|
|
|
|
// UTF16. If it isn't UTF8, we just use an empty string with the idea that
|
|
|
|
// consumers of these events should at least be notified that something
|
|
|
|
// happened.
|
|
|
|
nsAutoString tokenNameUTF16(NS_LITERAL_STRING(""));
|
|
|
|
if (IsUTF8(nsDependentCString(tokenName))) {
|
|
|
|
tokenNameUTF16.Assign(NS_ConvertUTF8toUTF16(tokenName));
|
|
|
|
}
|
|
|
|
nsCOMPtr<nsIRunnable> runnable(new nsTokenEventRunnable(eventType,
|
|
|
|
tokenNameUTF16));
|
|
|
|
NS_DispatchToMainThread(runnable);
|
2005-06-02 01:14:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// This is the main loop.
|
|
|
|
//
|
|
|
|
void SmartCardMonitoringThread::Execute()
|
|
|
|
{
|
2014-07-24 19:59:32 +00:00
|
|
|
const char* tokenName;
|
2005-06-02 01:14:02 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// populate token names for already inserted tokens.
|
|
|
|
//
|
2014-07-24 19:59:32 +00:00
|
|
|
PK11SlotList* sl = PK11_FindSlotsByNames(mModule->dllName, nullptr, nullptr,
|
|
|
|
true);
|
|
|
|
|
|
|
|
PK11SlotListElement* sle;
|
2005-06-02 01:14:02 +00:00
|
|
|
if (sl) {
|
2014-07-24 19:59:32 +00:00
|
|
|
for (sle = PK11_GetFirstSafe(sl); sle;
|
|
|
|
sle = PK11_GetNextSafe(sl, sle, false)) {
|
|
|
|
SetTokenName(PK11_GetSlotID(sle->slot), PK11_GetTokenName(sle->slot),
|
|
|
|
PK11_GetSlotSeries(sle->slot));
|
2005-06-02 01:14:02 +00:00
|
|
|
}
|
|
|
|
PK11_FreeSlotList(sl);
|
|
|
|
}
|
|
|
|
|
|
|
|
// loop starts..
|
|
|
|
do {
|
2016-05-10 01:02:40 +00:00
|
|
|
UniquePK11SlotInfo slot(
|
|
|
|
SECMOD_WaitForAnyTokenEvent(mModule, 0, PR_SecondsToInterval(1)));
|
2012-10-17 20:48:36 +00:00
|
|
|
if (!slot) {
|
2005-06-02 01:14:02 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// now we have a potential insertion or removal event, see if the slot
|
|
|
|
// is present to determine which it is...
|
2016-05-10 01:02:40 +00:00
|
|
|
if (PK11_IsPresent(slot.get())) {
|
2005-06-02 01:14:02 +00:00
|
|
|
// insertion
|
2016-05-10 01:02:40 +00:00
|
|
|
CK_SLOT_ID slotID = PK11_GetSlotID(slot.get());
|
|
|
|
uint32_t series = PK11_GetSlotSeries(slot.get());
|
2005-06-02 01:14:02 +00:00
|
|
|
|
|
|
|
// skip spurious insertion events...
|
|
|
|
if (series != GetTokenSeries(slotID)) {
|
|
|
|
// if there's a token name, then we have not yet issued a remove
|
|
|
|
// event for the previous token, do so now...
|
|
|
|
tokenName = GetTokenName(slotID);
|
|
|
|
if (tokenName) {
|
2014-06-30 23:02:02 +00:00
|
|
|
SendEvent(NS_LITERAL_STRING("smartcard-remove"), tokenName);
|
2005-06-02 01:14:02 +00:00
|
|
|
}
|
2016-05-10 01:02:40 +00:00
|
|
|
tokenName = PK11_GetTokenName(slot.get());
|
2005-06-02 01:14:02 +00:00
|
|
|
// save the token name and series
|
|
|
|
SetTokenName(slotID, tokenName, series);
|
2014-06-30 23:02:02 +00:00
|
|
|
SendEvent(NS_LITERAL_STRING("smartcard-insert"), tokenName);
|
2005-06-02 01:14:02 +00:00
|
|
|
}
|
|
|
|
} else {
|
2014-07-24 19:59:32 +00:00
|
|
|
// retrieve token name
|
2016-05-10 01:02:40 +00:00
|
|
|
CK_SLOT_ID slotID = PK11_GetSlotID(slot.get());
|
2005-06-02 01:14:02 +00:00
|
|
|
tokenName = GetTokenName(slotID);
|
|
|
|
// if there's not a token name, then the software isn't expecting
|
|
|
|
// a (or another) remove event.
|
|
|
|
if (tokenName) {
|
2014-06-30 23:02:02 +00:00
|
|
|
SendEvent(NS_LITERAL_STRING("smartcard-remove"), tokenName);
|
2005-06-02 01:14:02 +00:00
|
|
|
// clear the token name (after we send it)
|
2012-07-30 14:20:58 +00:00
|
|
|
SetTokenName(slotID, nullptr, 0);
|
2005-06-02 01:14:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// accessor to help searching active Monitoring threads
|
2014-07-24 19:59:32 +00:00
|
|
|
const SECMODModule* SmartCardMonitoringThread::GetModule()
|
2005-06-02 01:14:02 +00:00
|
|
|
{
|
|
|
|
return mModule;
|
|
|
|
}
|
|
|
|
|
|
|
|
// C-like calling sequence to glue into PR_CreateThread.
|
2014-07-24 19:59:32 +00:00
|
|
|
void SmartCardMonitoringThread::LaunchExecute(void* arg)
|
2005-06-02 01:14:02 +00:00
|
|
|
{
|
2012-06-12 17:06:20 +00:00
|
|
|
PR_SetCurrentThreadName("SmartCard");
|
|
|
|
|
2005-06-02 01:14:02 +00:00
|
|
|
((SmartCardMonitoringThread*)arg)->Execute();
|
|
|
|
}
|