CASA/BrowserExtensions/linux/src/MiCASAKeys.cpp

254 lines
7.1 KiB
C++

/***********************************************************************
*
* Copyright (C) 2005-2006 Novell, Inc. All Rights Reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; version 2.1
* of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, Novell, Inc.
*
* To contact Novell about this file by physical or electronic mail,
* you may find current contact information at www.novell.com.
*
***********************************************************************/
#include "MiCASAKeys.h"
#include <nsMemory.h>
#include <nsStringAPI.h>
#include <stdarg.h>
#include <dlfcn.h>
#include <syslog.h>
#include <errno.h>
#define MODULE_NAME "MiCASAKeys.so"
#define VERSION "1.00.0.0"
void _xpcom_log (int error, const char *format, ...)
{
char buffer [1024];
char identification [32];
va_list args;
sprintf (identification, "%s %s ", MODULE_NAME, VERSION);
va_start (args, format);
vsprintf (buffer, format, args);
openlog (identification, LOG_PID, LOG_AUTH);
setlogmask (LOG_UPTO (LOG_DEBUG));
syslog (error, buffer);
closelog ();
//printf("%i\t%s\t%s\n", error, identification, buffer);
}
/* Implementation file */
NS_IMPL_ISUPPORTS1(MiCASAKeys, IMiCASAKeys)
MiCASAKeys::MiCASAKeys()
{
m_bLibraryLoaded = FALSE;
m_pCASAOpen = NULL;
m_pCASAClose = NULL;
m_pCASAWriteKey = NULL;
m_pCASAReadKey = NULL;
_xpcom_log (LOG_INFO, "MiCASAKeys::MiCASAKeys constructor called.");
/* member initializers and constructor code */
m_hCASALibrary = dlopen("libmicasa.so", RTLD_LAZY);
if (m_hCASALibrary)
{
_xpcom_log (LOG_INFO, "libmicasa.so opened successfully.");
m_pCASAOpen = (PCASAOPEN)dlsym(m_hCASALibrary, "miCASAOpenSecretStoreCache");
m_pCASAClose = (PCASACLOSE)dlsym(m_hCASALibrary, "miCASACloseSecretStoreCache");
m_pCASAWriteKey = (PCASAWRITEKEY)dlsym(m_hCASALibrary, "miCASAWriteKey");
m_pCASAReadKey = (PCASAREADKEY)dlsym(m_hCASALibrary, "miCASAReadKey");
m_bLibraryLoaded = TRUE;
}
else
{
_xpcom_log(LOG_ERR, "dlopen on libmicasa.so failed, errno - %d.", errno);
}
}
MiCASAKeys::~MiCASAKeys()
{
/* destructor code */
// if (m_bLibraryLoaded == TRUE)
// {
// dlclose(hMod);
// m_bLibraryLoaded = FALSE;
// }
}
/* PRInt32 miCASAWriteKey (in string secretId, in string key, in string value); */
//NS_IMETHODIMP MiCASAKeys::MiCASAWriteKey(const char *secretId, const char *key, const char *value, PRInt32 *_retval)
/* PRInt32 miCASAWriteKey (in AUTF8String secretId, in AUTF8String key, in AUTF8String value); */
NS_IMETHODIMP MiCASAKeys::MiCASAWriteKey(const nsACString & secretId, const nsACString & key, const nsACString & value, PRInt32 *_retval)
{
_xpcom_log (LOG_INFO, "MiCASAKeys::MiCASAWriteKey called.");
if (m_bLibraryLoaded == TRUE)
{
nsresult result;
SSCS_KEYCHAIN_ID_T sessionKeyChain = {26, "SSCS_SESSION_KEY_CHAIN_ID"};
SSCS_SECRET_ID_T sharedId;
SSCS_SECRETSTORE_T ssId;
HANDLE context;
ssId.version = NSSCS_VERSION_NUMBER;
strncpy((char *)ssId.ssName, (char *)SSCS_DEFAULT_SECRETSTORE_ID, sizeof(ssId.ssName));
result = NSSCS_E_SYSTEM_FAILURE;
if (m_pCASAOpen)
{
context = (*m_pCASAOpen)(
&ssId,
0,
NULL);
if (context)
{
sharedId.len = secretId.Length() + 1;
strncpy((char *)sharedId.id, secretId.BeginReading(), sizeof(sharedId.id));
if (m_pCASAReadKey)
{
result = (*m_pCASAWriteKey)(
context,
0,
&sessionKeyChain,
&sharedId,
(unsigned char *)key.BeginReading(),
key.Length() + 1,
(unsigned char *)value.BeginReading(),
value.Length() + 1,
NULL,
NULL);
}
else
{
_xpcom_log (LOG_INFO, "MiCASAKeys::MiCASAWriteKey - m_pCASAWriteKey is NULL.");
}
(*m_pCASAClose)(
context,
0,
NULL);
}
else
{
_xpcom_log (LOG_INFO, "MiCASAKeys::MiCASAWriteKey - miCASAOpenSecretStoreCache failed.");
}
}
else
{
_xpcom_log (LOG_INFO, "MiCASAKeys::MiCASAWriteKey - m_pCASAOpen is NULL.");
}
*_retval = result;
return result;
}
else
{
_xpcom_log (LOG_INFO, "MiCASAKeys::MiCASAWriteKey library not loaded.");
return NSSCS_E_SERVICE_NOT_FOUND;
}
}
/* PRInt32 miCASAReadKey (in string secretId, in string key, out string value); */
//NS_IMETHODIMP MiCASAKeys::MiCASAReadKey(const char *secretId, const char *key, char **value, PRInt32 *_retval)
/* PRInt32 miCASAReadKey (in AUTF8String secretId, in AUTF8String key, out AUTF8String value); */
NS_IMETHODIMP MiCASAKeys::MiCASAReadKey(const nsACString & secretId, const nsACString & key, nsACString & value, PRInt32 *_retval)
{
_xpcom_log (LOG_INFO, "MiCASAKeys::MiCASAReadKey called.");
if (m_bLibraryLoaded == TRUE)
{
nsresult result;
SSCS_KEYCHAIN_ID_T sessionKeyChain = {26, "SSCS_SESSION_KEY_CHAIN_ID"};
SSCS_SECRET_ID_T sharedId;
SSCS_SECRETSTORE_T ssId;
uint32_t bytesRequired;
HANDLE context;
ssId.version = NSSCS_VERSION_NUMBER;
strncpy((char *)ssId.ssName, (char *)SSCS_DEFAULT_SECRETSTORE_ID, sizeof(ssId.ssName));
result = NSSCS_E_SYSTEM_FAILURE;
if (m_pCASAOpen)
{
context = (*m_pCASAOpen)(
&ssId,
0,
NULL);
if (context)
{
uint32_t valueLength = 256;
unsigned char tempValue[257];
sharedId.len = secretId.Length() + 1;
strncpy((char *)sharedId.id, secretId.BeginReading(), sizeof(sharedId.id));
if (m_pCASAReadKey)
{
result = (*m_pCASAReadKey)(
context,
0,
&sessionKeyChain,
&sharedId,
(unsigned char *)key.BeginReading(),
key.Length() + 1,
tempValue,
&valueLength,
NULL,
&bytesRequired,
NULL);
value.Assign((char *)tempValue, valueLength - 1);
}
else
{
_xpcom_log (LOG_INFO, "MiCASAKeys::MiCASAReadKey - m_pCASAReadKey is NULL.");
}
(*m_pCASAClose)(
context,
0,
NULL);
}
else
{
_xpcom_log (LOG_INFO, "MiCASAKeys::MiCASAReadKey - miCASAOpenSecretStoreCache failed.");
}
}
else
{
_xpcom_log (LOG_INFO, "MiCASAKeys::MiCASAReadKey - m_pCASAOpen is NULL.");
}
*_retval = result;
return result;
}
else
{
_xpcom_log (LOG_INFO, "MiCASAKeys::MiCASAReadKey library not loaded.");
return NSSCS_E_SERVICE_NOT_FOUND;
}
}