diff --git a/c_sharp/NSSCSWrapper/NativeCalls.cs b/c_sharp/NSSCSWrapper/NativeCalls.cs index 112a067c..64f2db38 100644 --- a/c_sharp/NSSCSWrapper/NativeCalls.cs +++ b/c_sharp/NSSCSWrapper/NativeCalls.cs @@ -35,17 +35,17 @@ namespace Novell.CASA { public uint pwordType; public uint pwordLen; // * enhanced protection len & pword to set -// [MarshalAs(UnmanagedType.LPStr, SizeConst = 128)] - [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)] + // [MarshalAs(UnmanagedType.LPStr, SizeConst = 128)] + [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)] public string pword; // * should be passed in # of chars } ; - [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] - public class SSCS_PASSCODE_T - { - public uint passcodeType; - public IntPtr passcodeHandle; - } + [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] + public class SSCS_PASSCODE_T + { + public uint passcodeType; + public IntPtr passcodeHandle; + } [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] public class SSCS_KEYCHAIN_ID_T @@ -80,13 +80,13 @@ namespace Novell.CASA public string id; // * should be passed in # of chars }; - [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] - public class SSCS_HINT_T - { - ulong hintLen; - [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)] - string hint; //* should be passed in # of chars - }; + [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] + public class SSCS_HINT_T + { + ulong hintLen; + [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)] + string hint; //* should be passed in # of chars + }; [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] public class SSCS_EXT_T @@ -166,19 +166,19 @@ namespace Novell.CASA public string hint; //[NSSS_MAX_MP_PWORD_HINT_LEN]]; }; -/* - [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] - public class SSCS_BASIC_CREDENTIAL - { - public uint unFlags; - public uint unLen; - [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)] - public string username; - public uint pwordLen; - [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)] - public string password; - } -*/ + /* + [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] + public class SSCS_BASIC_CREDENTIAL + { + public uint unFlags; + public uint unLen; + [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)] + public string username; + public uint pwordLen; + [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)] + public string password; + } + */ [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] public class SSCS_BASIC_CREDENTIAL_UTF8 { @@ -232,21 +232,21 @@ namespace Novell.CASA [In] IntPtr credential, [In, Out] SSCS_EXT_T ext ); - [DllImport(NDK_LIBRARY)] - public static extern int miCASASetMasterPasscode - ( - [In] uint ssFlags, - [In] SSCS_PASSCODE_T passcode, - [In] SSCS_EXT_T ext - ); + [DllImport(NDK_LIBRARY)] + public static extern int miCASASetMasterPasscode + ( + [In] uint ssFlags, + [In] SSCS_PASSCODE_T passcode, + [In] SSCS_EXT_T ext + ); [DllImport(NDK_LIBRARY)] public static extern int miCASAIsSecretPersistent - ( - [In] uint ssFlags, - [In] SSCS_SECRET_ID_T secretID, - [In, Out] SSCS_EXT_T ext - ); + ( + [In] uint ssFlags, + [In] SSCS_SECRET_ID_T secretID, + [In, Out] SSCS_EXT_T ext + ); [DllImport(NDK_LIBRARY)] public static extern IntPtr miCASAOpenSecretStoreCache @@ -345,12 +345,12 @@ namespace Novell.CASA [DllImport(NDK_LIBRARY)] public static extern int miCASASetMasterPassword - ( - [In] uint ssFlags, - [In] SSCS_PASSWORD_T password, - [In] SSCS_HINT_T hint, - [In, Out] SSCS_EXT_T ext - ); + ( + [In] uint ssFlags, + [In] SSCS_PASSWORD_T password, + [In] SSCS_HINT_T hint, + [In, Out] SSCS_EXT_T ext + ); [DllImport(NDK_LIBRARY)] public static extern int miCASAUnlockSecrets @@ -440,10 +440,10 @@ namespace Novell.CASA [In] SSCS_KEYCHAIN_ID_T keyChainID, [In] SSCS_SH_SECRET_ID_T sharedSecretID, [MarshalAs(UnmanagedType.LPStr)] - string key, //* in /wchar + string key, //* in /wchar [In] uint keyLenBytes, // in bytes [MarshalAs(UnmanagedType.LPStr)] - string val, //* in /uchar + string val, //* in /uchar [In] uint valueLenBytes, // in bytes [In] SSCS_PASSWORD_T password, [In, Out] SSCS_EXT_T ext @@ -465,6 +465,12 @@ namespace Novell.CASA // Methods start here internal IntPtr openSecretStore(string sSecretStoreName) { + + //check params + if (sSecretStoreName == null || sSecretStoreName.Length==0) + throw new miCasaException(miCasaException.NSSCS_E_INVALID_PARAM); + + SSCS_SECRET_STORE_ID_T SSid = new SSCS_SECRET_STORE_ID_T(); SSid.id = sSecretStoreName; SSid.len = sSecretStoreName.Length; @@ -487,9 +493,13 @@ namespace Novell.CASA uint iFlags, string sSharedSecretID, int iSecretType, - string sEPPassword) - - { + string sEPPassword) + { + + if (pHsc == IntPtr.Zero || sKeyChainID == null || sSharedSecretID == null + || sKeyChainID.Length == 0 || sSharedSecretID.Length == 0) + throw new miCasaException(miCasaException.NSSCS_E_INVALID_PARAM); + // setup structures SSCS_KEYCHAIN_ID_T keyChainID = new SSCS_KEYCHAIN_ID_T(); keyChainID.keychainID = sKeyChainID; @@ -610,119 +620,119 @@ namespace Novell.CASA return rcode; } -/* - internal int setSecret( - IntPtr pHsc, - string sKeyChainID, - uint iFlags, - Secret secret, - int iSecretType) - { - int rcode; + /* + internal int setSecret( + IntPtr pHsc, + string sKeyChainID, + uint iFlags, + Secret secret, + int iSecretType) + { + int rcode; - if (iSecretType == Secret.SS_BINARY) - { - rcode = miCASA_AddSHSEntry( - secret.m_secretHandle, - "SS_Binary", - (uint)secret.getBinaryValue().Length, - secret.getBinaryValue().ToString()); - } - else - { - NameValueCollection nvc = secret.getKeyValueCollection(); - if (nvc != null) - { - for (int i=0; i 0) - { - // first write out all key/value pairs - rcode = miCASA_AddSHSEntry( - secret.m_secretHandle, - sKey, //sKey, - (uint)(sValue.Length+1) * 2, - sValue); //sValue); - } - + rcode = miCASA_AddSHSEntry( + secret.m_secretHandle, + "SS_Binary", + (uint)secret.getBinaryValue().Length, + secret.getBinaryValue().ToString()); } + else + { + NameValueCollection nvc = secret.getKeyValueCollection(); + if (nvc != null) + { + for (int i=0; i 0) + { + // first write out all key/value pairs + rcode = miCASA_AddSHSEntry( + secret.m_secretHandle, + sKey, //sKey, + (uint)(sValue.Length+1) * 2, + sValue); //sValue); + } + + } + } + } + + // now call miCASAWriteSecret + SSCS_KEYCHAIN_ID_T keyChainID = new SSCS_KEYCHAIN_ID_T(); + keyChainID.keychainID = sKeyChainID; + + SSCS_SH_SECRET_ID_T sharedSecretID = new SSCS_SH_SECRET_ID_T(); + SSCS_PASSWORD_T epPassword = new SSCS_PASSWORD_T(); + SSCS_EXT_T ext =new SSCS_EXT_T(); + ext.extID = 0; + ext.version = 0; + + + // setup keychainid + keyChainID.len = sKeyChainID.Length + 1; + + String tempStr = secret.getID(); + sharedSecretID.name = tempStr; + sharedSecretID.len = tempStr.Length + 1; + sharedSecretID.type = iSecretType; // TODO: type APP(1), CRED(2), or Binary(4) + + if (secret.getEnhancedProtectionPassword() != null) + { + epPassword.pword = secret.getEnhancedProtectionPassword(); + epPassword.pwordLen = (uint)secret.getEnhancedProtectionPassword().Length + 1; + epPassword.pwordType = 0; + } + else + { + epPassword = null; + } + + ext.ext = Marshal.AllocHGlobal(10); + try + { + rcode = miCASAWriteSecret( + pHsc, + keyChainID, + iFlags, + secret.m_secretHandle, + sharedSecretID, + epPassword, + ext); + } + catch (Exception) + { + //Console.WriteLine(e.ToString()); + rcode = -803; + } + + Marshal.FreeHGlobal(ext.ext); + return rcode; + } + */ + + internal bool KeyInNewList(NameValueCollection nvc, string key) + { + + Console.WriteLine("Checking for " + key ); + if (nvc != null) + { + for (int i = 0; i < nvc.Count; i++) + { + string sKey = nvc.GetKey(i); + if( key == sKey ) + return true; } } - - // now call miCASAWriteSecret - SSCS_KEYCHAIN_ID_T keyChainID = new SSCS_KEYCHAIN_ID_T(); - keyChainID.keychainID = sKeyChainID; - - SSCS_SH_SECRET_ID_T sharedSecretID = new SSCS_SH_SECRET_ID_T(); - SSCS_PASSWORD_T epPassword = new SSCS_PASSWORD_T(); - SSCS_EXT_T ext =new SSCS_EXT_T(); - ext.extID = 0; - ext.version = 0; - - - // setup keychainid - keyChainID.len = sKeyChainID.Length + 1; - - String tempStr = secret.getID(); - sharedSecretID.name = tempStr; - sharedSecretID.len = tempStr.Length + 1; - sharedSecretID.type = iSecretType; // TODO: type APP(1), CRED(2), or Binary(4) - - if (secret.getEnhancedProtectionPassword() != null) - { - epPassword.pword = secret.getEnhancedProtectionPassword(); - epPassword.pwordLen = (uint)secret.getEnhancedProtectionPassword().Length + 1; - epPassword.pwordType = 0; - } - else - { - epPassword = null; - } - - ext.ext = Marshal.AllocHGlobal(10); - try - { - rcode = miCASAWriteSecret( - pHsc, - keyChainID, - iFlags, - secret.m_secretHandle, - sharedSecretID, - epPassword, - ext); - } - catch (Exception) - { - //Console.WriteLine(e.ToString()); - rcode = -803; - } - - Marshal.FreeHGlobal(ext.ext); - return rcode; - } -*/ - - internal bool KeyInNewList(NameValueCollection nvc, string key) - { - - Console.WriteLine("Checking for " + key ); - if (nvc != null) - { - for (int i = 0; i < nvc.Count; i++) - { - string sKey = nvc.GetKey(i); - if( key == sKey ) - return true; - } - } - Console.WriteLine("key = " + key + " is not present in nvc"); - return false; - } + //Console.WriteLine("key = " + key + " is not present in nvc"); + return false; + } internal int setSecret( @@ -734,6 +744,8 @@ namespace Novell.CASA { int rcode = 0; + if (sKeyChainID == null || sKeyChainID.Length == 0) + throw new miCasaException(miCasaException.NSSCS_E_INVALID_PARAM); // now call miCASAWriteSecret SSCS_KEYCHAIN_ID_T keyChainID = new SSCS_KEYCHAIN_ID_T(); @@ -784,7 +796,7 @@ namespace Novell.CASA for (int i=0; i 0) { - try - { - object o = MiCasaRequestReply.Send( - MiCasaRequestReply.VERB_REMOVE_KEY, - sKeyChainID, secretID, keyID, null); - } - catch (Exception e) - { - rcode = -803; - } + try + { + object o = MiCasaRequestReply.Send( + MiCasaRequestReply.VERB_REMOVE_KEY, + sKeyChainID, secretID, keyID, null); + } + catch (Exception e) + { + rcode = -803; + } } - return rcode; - } + return rcode; + } - internal ArrayList GetKeyList( - string sKeyChainID, - string secretID) - { - ArrayList keyList = null; - try - { - keyList = (ArrayList) MiCasaRequestReply.Send( - MiCasaRequestReply.VERB_GET_KEY_LIST, - sKeyChainID, secretID,null,null); - return keyList; + internal ArrayList GetKeyList( + string sKeyChainID, + string secretID) + { + if (sKeyChainID==null || secretID==null + || sKeyChainID.Length==0 || secretID.Length==0) + throw new miCasaException(miCasaException.NSSCS_E_INVALID_PARAM); + + ArrayList keyList = null; + try + { + keyList = (ArrayList) MiCasaRequestReply.Send( + MiCasaRequestReply.VERB_GET_KEY_LIST, + sKeyChainID, secretID,null,null); + return keyList; #if false if( null != keyList ) @@ -1276,44 +1332,50 @@ namespace Novell.CASA } #endif - } - catch(Exception e) - { - Console.WriteLine(e.ToString()); - keyList = null; - } - return keyList; - } + } + catch(Exception e) + { + Console.WriteLine(e.ToString()); + keyList = null; + } + return keyList; + } internal string ReadKey( string sKeyChainID, - string secretID, - string keyID) + string secretID, + string keyID) { + if (sKeyChainID==null || secretID==null || keyID==null + || sKeyChainID.Length==0 || secretID.Length==0 || keyID.Length==0) + throw new miCasaException(miCasaException.NSSCS_E_INVALID_PARAM); int rcode = 0; - string value = null; + string value = null; if (keyID.Length > 0) { - try - { - value = (String)MiCasaRequestReply.Send( - MiCasaRequestReply.VERB_REMOVE_KEY, - sKeyChainID, secretID, keyID, null); - } - catch (Exception e) - { -// rcode = -803; - Console.WriteLine(e.ToString()); - } + try + { + value = (String)MiCasaRequestReply.Send( + MiCasaRequestReply.VERB_REMOVE_KEY, + sKeyChainID, secretID, keyID, null); + } + catch (Exception e) + { + // rcode = -803; + Console.WriteLine(e.ToString()); + } } - return value; - } + return value; + } private string EscapeReservedChars(string origString) { + if (origString==null) + return origString; + StringBuilder sb = new StringBuilder(); for (int i=0; i