CASA/c_sharp/NSSCSWrapper/SecretStore.cs

163 lines
4.1 KiB
C#
Raw Normal View History

using System;
using System.Collections.Specialized;
namespace Novell.CASA
{
/// <summary>
/// Represents the SecretStore
/// </summary>
public class SecretStore : MarshalByRefObject
{
private string m_sMasterPassword = null;
private static string SESSION_KEY_CHAIN = "SSCS_SESSION_KEY_CHAIN_ID";
public static string LOCAL_REMOTE_KEY_CHAIN = "SSCS_LOCAL_REMOTE_KEY_CHAIN_ID";
private NativeCalls m_NativeCalls = null;
private IntPtr m_hsc = IntPtr.Zero;
public SecretStore()
{
doSetup();
}
private void doSetup()
{
m_NativeCalls = new NativeCalls();
m_hsc = m_NativeCalls.openSecretStore("SecretStore");
//Console.WriteLine("Session handle: {0}", m_hsc);
}
public static SecretStore getInstance()
{
SecretStore newSS = new SecretStore();
// Test code for new APIS
/*
miCASA.SetCredential(0, "MyTestApp", null, 0, "MyTestAPPName", "MyTestAPPPassword");
BasicCredential bc = miCASA.GetCredential(0, "MyTestApp", "Network", 0);
if (bc != null)
{
Console.WriteLine("------");
Console.WriteLine("Username = " + bc.GetUsername());
Console.WriteLine("Password = " + bc.GetPassword());
Console.WriteLine("------");
}
miCASA.RemoveCredential(0, "MyTestApp", null);
try
{
miCASA.RemoveCredential(0, "MyTestApp", null);
}
catch (Exception e)
{
Console.WriteLine("ClearCredential returned " + e.ToString());
}
*/
// end test code
return newSS;
}
public static void ReleaseInstance()
{
// TODO:
}
internal Secret getSecret(string sKeyChainID, uint ssFlags, string sSecretID, int iSecretType, string sEPPassword)
{
if (m_NativeCalls != null)
return m_NativeCalls.getSecret(m_hsc, sKeyChainID, ssFlags, sSecretID, iSecretType, sEPPassword);
else
return null;
}
public Secret getSecret(string id)
{
string sSecretId = id;
int iSecretType = Secret.SS_BINARY;
Console.WriteLine("GetSecret called for "+id);
if (sSecretId.StartsWith("SS_CredSet"))
{
sSecretId = sSecretId.Substring(11);
iSecretType = Secret.SS_CREDSET;
}
else if (sSecretId.StartsWith("SS_App"))
{
sSecretId = sSecretId.Substring(10);
iSecretType = Secret.SS_APP;
}
return getSecret(0, sSecretId, iSecretType, "");
}
public Secret getSecret(uint ssFlags, string sSecretID, int iSecretType, string sEPPassword)
{
return getSecret(SESSION_KEY_CHAIN, ssFlags, sSecretID, iSecretType, sEPPassword);
}
public void setSecret(uint flags, string sKeyChainID, Secret secret, int iSecretType)
{
m_NativeCalls.setSecret(m_hsc, sKeyChainID, flags, secret, iSecretType);
}
public void setSecret(uint flags, Secret secret, int iSecretType)
{
// call the native calls to write this secret
m_NativeCalls.setSecret(m_hsc, SESSION_KEY_CHAIN, flags, secret, iSecretType);
}
private void setSecret(uint flags, string sSecretID, string sEPPassword)
{
//setSecret(flags, SESSION_KEY_CHAIN, sSecretID, sEPPassword);
}
public void removeSecret(uint ssFlags, string sKeyChainID, string sEPPassword, string sSecretID, int iSecretType)
{
m_NativeCalls.RemoveSecret(m_hsc, ssFlags, sKeyChainID, sEPPassword, sSecretID, iSecretType);
}
public void removeSecret(string sSecretID, int iSecretType)
{
removeSecret(0, SESSION_KEY_CHAIN, "", sSecretID, iSecretType);
}
private void enumerateKeychainIDs(string sSearchSting)
{
if (m_NativeCalls != null)
{
//m_NativeCalls.enumKeychainIDs();
}
}
public void setMasterPassword(string sMasterPassword)
{
m_sMasterPassword = sMasterPassword;
}
public StringCollection enumerateSecretIDs()
{
return enumerateSecretIDs(SESSION_KEY_CHAIN);
}
public StringCollection enumerateSecretIDs(string sKeyChainID)
{
return m_NativeCalls.EnumerateSecretIDs(m_hsc, 0, sKeyChainID, "");
}
public void addKeyChain(string sKeyChainID)
{
m_NativeCalls.AddKeyChain(m_hsc, 0, sKeyChainID);
}
}
}