bool IEncryptionDriver.Encrypt(int session, IntPtr Data, int DataLen, IntPtr EncData, ref int EncDataLen) { SessionData ctx = null; try { byte[] encData = null; ctx = ((SessionDriver)this.Hal.Session).GetSessionCtx(session); unsafe { switch (ctx.EncryptCtx.CryptoAlgorithm) { case AlgorithmType.DES3_CBC: case AlgorithmType.DES3_CBC_PAD: case AlgorithmType.AES_CBC: case AlgorithmType.AES_ECB: case AlgorithmType.AES_ECB_PAD: case AlgorithmType.AES_CBC_PAD: { if (EncData == IntPtr.Zero) { int blockSize = ctx.EncryptCtx.CryptoTransform.OutputBlockSize; int mod = DataLen % blockSize; EncDataLen = DataLen + (blockSize - mod); return(true); } if (AlgorithmType.AES_CBC == ctx.EncryptCtx.CryptoAlgorithm || AlgorithmType.AES_ECB == ctx.EncryptCtx.CryptoAlgorithm) { int blockSize = ctx.EncryptCtx.CryptoTransform.OutputBlockSize; int mod = DataLen % blockSize; DataLen = DataLen + (blockSize - mod); } byte[] data = new byte[DataLen]; Marshal.Copy(Data, data, 0, DataLen); encData = ctx.EncryptCtx.CryptoTransform.TransformFinalBlock(data, 0, data.Length); } break; case AlgorithmType.RSA_PKCS: { RSACryptoServiceProvider rsa = ctx.EncryptCtx.CryptoObject as RSACryptoServiceProvider; if (EncData == IntPtr.Zero) { int blockSize = (rsa.KeySize + 7) / 8; int mod = DataLen % blockSize; EncDataLen = DataLen + (blockSize - mod); return(true); } byte[] data = new byte[DataLen]; Marshal.Copy(Data, data, 0, DataLen); encData = rsa.Encrypt(data, false); } break; case AlgorithmType.DSA: case AlgorithmType.DSA_SHA1: { DSACryptoServiceProvider dsa = ctx.EncryptCtx.CryptoObject as DSACryptoServiceProvider; if (EncData == IntPtr.Zero) { int blockSize = (dsa.KeySize + 7) / 8; int mod = DataLen % blockSize; EncDataLen = DataLen + (blockSize - mod); return(true); } byte[] data = new byte[DataLen]; Marshal.Copy(Data, data, 0, DataLen); encData = dsa.SignHash(data, "SHA1"); } break; default: return(false); } } if (encData.Length > EncDataLen) { throw new ArgumentException(); } Marshal.Copy(encData, 0, EncData, encData.Length); EncDataLen = encData.Length; } catch (Exception e) { Debug.Print(e.ToString()); return(false); } finally { if (EncData != IntPtr.Zero && ctx != null) { ctx.EncryptCtx.Clear(); } } return(true); }
bool IEncryptionDriver.EncryptUpdate(int session, IntPtr Part, int PartLen, IntPtr EncData, ref int EncDataLen) { try { SessionData ctx = ((SessionDriver)this.Hal.Session).GetSessionCtx(session); unsafe { switch (ctx.EncryptCtx.CryptoAlgorithm) { case AlgorithmType.DES3_CBC: case AlgorithmType.DES3_CBC_PAD: case AlgorithmType.AES_CBC: case AlgorithmType.AES_ECB: case AlgorithmType.AES_ECB_PAD: case AlgorithmType.AES_CBC_PAD: { byte[] data = new byte[PartLen]; Marshal.Copy(Part, data, 0, PartLen); byte[] encData = new byte[EncDataLen]; int len = ctx.EncryptCtx.CryptoTransform.TransformBlock(data, 0, data.Length, encData, 0); if (len > EncDataLen) { throw new ArgumentException(); } Marshal.Copy(encData, 0, EncData, len); EncDataLen = len; } break; case AlgorithmType.RSA_PKCS: { RSACryptoServiceProvider rsa = ctx.EncryptCtx.CryptoObject as RSACryptoServiceProvider; byte[] data = new byte[PartLen]; Marshal.Copy(Part, data, 0, PartLen); byte[] encData = rsa.Encrypt(data, false); if (encData.Length > EncDataLen) { throw new ArgumentException(); } Marshal.Copy(encData, 0, EncData, encData.Length); EncDataLen = encData.Length; } break; default: return(false); } } } catch (Exception e) { Debug.Print(e.ToString()); return(false); } return(true); }
bool IEncryptionDriver.EncryptInit(int session, int alg, IntPtr algParam, int algParamLen, int hKey) { try { SessionData ctx = ((SessionDriver)this.Hal.Session).GetSessionCtx(session); KeyData kd = null; CryptokiObject obj = ctx.ObjectCtx.GetObject(hKey); if (obj == null) { return(false); } if (obj.Type == CryptokiObjectType.Key) { kd = obj.Data as KeyData; } else if (obj.Type == CryptokiObjectType.Cert) { X509Certificate2 cert = obj.Data as X509Certificate2; AsymmetricAlgorithm encAlg = cert.PublicKey.Key; kd = new KeyData(null, encAlg); } else { return(false); } byte[] keyData = kd.KeyBytes; byte[] IV = null; if (algParam != IntPtr.Zero) { IV = new byte[algParamLen]; Marshal.Copy(algParam, IV, 0, algParamLen); } ctx.EncryptCtx.CryptoAlgorithm = (AlgorithmType)alg; switch ((AlgorithmType)alg) { case AlgorithmType.DES3_CBC_PAD: { TripleDESCryptoServiceProvider des3 = new TripleDESCryptoServiceProvider(); des3.Padding = PaddingMode.PKCS7; ctx.EncryptCtx.CryptoObject = des3; ctx.EncryptCtx.CryptoTransform = des3.CreateEncryptor(keyData, IV); } break; case AlgorithmType.DES3_CBC: { TripleDESCryptoServiceProvider des3 = new TripleDESCryptoServiceProvider(); des3.Padding = PaddingMode.None; ctx.EncryptCtx.CryptoObject = des3; ctx.EncryptCtx.CryptoTransform = des3.CreateEncryptor(keyData, IV); } break; case AlgorithmType.AES_CBC_PAD: { AesCryptoServiceProvider aes = new AesCryptoServiceProvider(); aes.Padding = PaddingMode.PKCS7; aes.Mode = CipherMode.CBC; ctx.EncryptCtx.CryptoObject = aes; ctx.EncryptCtx.CryptoTransform = aes.CreateEncryptor(keyData, IV); } break; case AlgorithmType.AES_ECB_PAD: { AesCryptoServiceProvider aes = new AesCryptoServiceProvider(); aes.Padding = PaddingMode.PKCS7; aes.Mode = CipherMode.ECB; ctx.EncryptCtx.CryptoObject = aes; ctx.EncryptCtx.CryptoTransform = aes.CreateEncryptor(keyData, IV); } break; case AlgorithmType.AES_CBC: { AesCryptoServiceProvider aes = new AesCryptoServiceProvider(); aes.Padding = PaddingMode.None; aes.Mode = CipherMode.CBC; ctx.EncryptCtx.CryptoObject = aes; ctx.EncryptCtx.CryptoTransform = aes.CreateEncryptor(keyData, IV); } break; case AlgorithmType.AES_ECB: { AesCryptoServiceProvider aes = new AesCryptoServiceProvider(); aes.Padding = PaddingMode.None; aes.Mode = CipherMode.ECB; ctx.EncryptCtx.CryptoObject = aes; ctx.EncryptCtx.CryptoTransform = aes.CreateEncryptor(keyData, IV); } break; case AlgorithmType.RSA_PKCS: if (keyData == null) { ctx.EncryptCtx.CryptoObject = kd.KeyCsp as IDisposable; } else { RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); ctx.EncryptCtx.CryptoObject = rsa; rsa.ImportCspBlob(keyData); ctx.EncryptCtx.CryptoTransform = null; } break; case AlgorithmType.DSA: case AlgorithmType.DSA_SHA1: if (keyData == null) { ctx.EncryptCtx.CryptoObject = kd.KeyCsp as IDisposable; } else { DSACryptoServiceProvider dsa = new DSACryptoServiceProvider(); ctx.EncryptCtx.CryptoObject = dsa; ctx.EncryptCtx.CryptoTransform = null; dsa.ImportCspBlob(keyData); } break; default: return(false); } } catch (Exception e) { Debug.Print(e.ToString()); return(false); } return(true); }
public bool SetAttributeValue(int session, int hObject, IntPtr pTemplate, int ulCount) { try { SessionData ctx = ((SessionDriver)this.Hal.Session).GetSessionCtx(session); CryptokiObject obj = ctx.ObjectCtx.GetObject(hObject); if (obj == null) { return(false); } if (obj.Type == CryptokiObjectType.Cert) { X509Certificate2 cert = obj.Data as X509Certificate2; bool fSave = false; bool fDelete = false; string group = "", fileName = ""; uint len = 0; byte[] data; for (int i = 0; i < (int)ulCount; i++) { CryptokiAttribute attrib = new CryptokiAttribute(); Marshal.PtrToStructure(pTemplate, attrib); switch ((CryptokiAttribType)attrib.type) { case CryptokiAttribType.Persist: fSave = Marshal.ReadInt32(attrib.pValue) == 1; fDelete = !fSave; break; case CryptokiAttribType.Label: len = attrib.ulValueLen; data = new byte[len]; Marshal.Copy(attrib.pValue, data, 0, (int)len); group = UTF8Encoding.UTF8.GetString(data); break; case CryptokiAttribType.ObjectID: len = attrib.ulValueLen; data = new byte[len]; Marshal.Copy(attrib.pValue, data, 0, (int)len); fileName = UTF8Encoding.UTF8.GetString(data); break; default: return(false); } pTemplate += Marshal.SizeOf(attrib); } if (fDelete) { ctx.ObjectCtx.DestroyObject(hObject); } else if (fSave) { // TODO: Store in persistant storage for emulator obj.Properties["FileName"] = fileName; obj.Properties["Group"] = group; } else { return(false); } } return(true); } catch { return(false); } }
public bool CreateObject(int session, IntPtr pTemplate, int ulCount, out int phObject) { phObject = -1; if (ulCount == 0) { return(false); } try { SessionData ctx = ((SessionDriver)this.Hal.Session).GetSessionCtx(session); CryptokiAttribute attrib = new CryptokiAttribute(); Marshal.PtrToStructure(pTemplate, attrib); if ((CryptokiAttribType)attrib.type == CryptokiAttribType.Class) { CryptokiClass type = (CryptokiClass)Marshal.ReadInt32(attrib.pValue); if (type == CryptokiClass.CERTIFICATE) { string password = ""; while (0 < --ulCount) { pTemplate += Marshal.SizeOf(attrib); Marshal.PtrToStructure(pTemplate, attrib); switch ((CryptokiAttribType)attrib.type) { case CryptokiAttribType.Password: { byte[] data = new byte[attrib.ulValueLen]; Marshal.Copy(attrib.pValue, data, 0, data.Length); password = UTF8Encoding.UTF8.GetString(data); } break; case CryptokiAttribType.Value: { byte[] data = new byte[attrib.ulValueLen]; Marshal.Copy(attrib.pValue, data, 0, data.Length); X509Certificate2 x509 = new X509Certificate2(data, password); phObject = ctx.ObjectCtx.AddObject(CryptokiObjectType.Cert, x509); return(true); } default: break; } } } } } catch { } return(false); }
public bool GetAttributeValue(int session, int hObject, IntPtr pTemplate, int ulCount) { try { SessionData ctx = ((SessionDriver)this.Hal.Session).GetSessionCtx(session); CryptokiObject obj = ctx.ObjectCtx.GetObject(hObject); if (obj == null) { return(false); } DSAParameters dsaParms = new DSAParameters(); bool hasDsaParms = false; int valueLen = 0; RSAParameters rsaParms = new RSAParameters(); while (ulCount-- > 0) { CryptokiAttribute attrib = new CryptokiAttribute(); Marshal.PtrToStructure(pTemplate, attrib); if (obj.Type == CryptokiObjectType.Cert) { X509Certificate2 cert = (X509Certificate2)obj.Data; switch ((CryptokiAttribType)attrib.type) { case CryptokiAttribType.Class: Marshal.WriteInt32(attrib.pValue, (int)CryptokiClass.CERTIFICATE); valueLen = 4; break; case CryptokiAttribType.Private: Marshal.WriteInt32(attrib.pValue, cert.HasPrivateKey ? 1 : 0); valueLen = 4; break; case CryptokiAttribType.KeyType: switch (cert.GetKeyAlgorithm()) { case "1.2.840.113549.1.1.1": Marshal.WriteInt32(attrib.pValue, (int)KeyType.RSA); break; default: Marshal.WriteInt32(attrib.pValue, -1); break; } valueLen = 4; break; case CryptokiAttribType.Issuer: { byte [] data = UTF8Encoding.UTF8.GetBytes(cert.Issuer); Marshal.Copy(data, 0, attrib.pValue, data.Length); valueLen = data.Length; } break; case CryptokiAttribType.Subject: { byte [] data = UTF8Encoding.UTF8.GetBytes(cert.Subject); Marshal.Copy(data, 0, attrib.pValue, data.Length); valueLen = data.Length; } break; case CryptokiAttribType.SerialNumber: { byte[] data = cert.GetSerialNumber(); Marshal.Copy(data, 0, attrib.pValue, data.Length); valueLen = data.Length; } break; case CryptokiAttribType.PublicExponent: { } break; case CryptokiAttribType.StartDate: case CryptokiAttribType.EndDate: { DATE_TIME_INFO dti = new DATE_TIME_INFO(); DateTime dt = (CryptokiAttribType)attrib.type == CryptokiAttribType.StartDate ? cert.NotBefore : cert.NotAfter; dti.year = dt.Year; dti.month = dt.Month; dti.day = dt.Day; dti.hour = dt.Hour; dti.minute = dt.Minute; dti.second = dt.Second; dti.msec = dt.Millisecond; dti.tzOffset = TimeZoneInfo.Local.BaseUtcOffset.Hours; Marshal.StructureToPtr(dti, attrib.pValue, true); valueLen = Marshal.SizeOf(dti); } break; case CryptokiAttribType.MechanismType: { switch (cert.SignatureAlgorithm.Value) { case "1.2.840.113549.1.1.5": Marshal.WriteInt32(attrib.pValue, (int)AlgorithmType.SHA1_RSA_PKCS); break; case "1.2.840.113549.1.1.4": Marshal.WriteInt32(attrib.pValue, (int)AlgorithmType.MD5_RSA_PKCS); break; case "1.2.840.113549.1.1.11": Marshal.WriteInt32(attrib.pValue, (int)AlgorithmType.SHA256_RSA_PKCS); break; case "1.2.840.113549.1.1.12": Marshal.WriteInt32(attrib.pValue, (int)AlgorithmType.SHA384_RSA_PKCS); break; case "1.2.840.113549.1.1.13": Marshal.WriteInt32(attrib.pValue, (int)AlgorithmType.SHA512_RSA_PKCS); break; } valueLen = 4; } break; case CryptokiAttribType.Value: valueLen = cert.RawData.Length; Marshal.Copy(cert.RawData, 0, attrib.pValue, valueLen); break; case CryptokiAttribType.ValueLen: Marshal.WriteInt32(attrib.pValue, valueLen); break; } } else if (obj.Type == CryptokiObjectType.Key) { object key = ((KeyData)obj.Data).KeyCsp; switch ((CryptokiAttribType)attrib.type) { case CryptokiAttribType.Class: Marshal.WriteInt32(attrib.pValue, (int)CryptokiClass.OTP_KEY); valueLen = 4; break; case CryptokiAttribType.PrivateExponent: if (key is DSACryptoServiceProvider) { if (!hasDsaParms) { dsaParms = ((DSACryptoServiceProvider)key).ExportParameters(true); } valueLen = Math.Min((int)attrib.ulValueLen, dsaParms.X.Length); Marshal.Copy(dsaParms.X, 0, attrib.pValue, valueLen); } else if (key is AesCryptoServiceProvider) { AesCryptoServiceProvider aes = (AesCryptoServiceProvider)key; valueLen = Math.Min((int)attrib.ulValueLen, aes.Key.Length); Marshal.Copy(aes.Key, 0, attrib.pValue, valueLen); } else if (key is KeyManagementDriver.SecretKey) { KeyManagementDriver.SecretKey sk = (KeyManagementDriver.SecretKey)key; valueLen = Math.Min((int)attrib.ulValueLen, sk.Data.Length); Marshal.Copy(sk.Data, 0, attrib.pValue, valueLen); } else if (key is RSACryptoServiceProvider) { RSACryptoServiceProvider rsa = (RSACryptoServiceProvider)key; if (rsaParms.Modulus == null) { rsaParms = rsa.ExportParameters(true); } valueLen = Math.Min((int)attrib.ulValueLen, rsaParms.D.Length); Marshal.Copy(rsaParms.D, 0, attrib.pValue, valueLen); } break; case CryptokiAttribType.PublicExponent: if (key is DSACryptoServiceProvider) { if (!hasDsaParms) { dsaParms = ((DSACryptoServiceProvider)key).ExportParameters(true); } valueLen = Math.Min((int)attrib.ulValueLen, dsaParms.Y.Length); Marshal.Copy(dsaParms.Y, 0, attrib.pValue, valueLen); } else if (key is ECDiffieHellman) { ECDiffieHellman ecdh = (ECDiffieHellman)key; byte[] pubKey = ecdh.PublicKey.ToByteArray(); valueLen = Math.Min((int)attrib.ulValueLen, pubKey.Length - 8); Marshal.Copy(pubKey, 8, attrib.pValue, valueLen); } else if (key is RSACryptoServiceProvider) { RSACryptoServiceProvider rsa = (RSACryptoServiceProvider)key; if (rsaParms.Modulus == null) { rsaParms = rsa.ExportParameters(true); } valueLen = Math.Min((int)attrib.ulValueLen, rsaParms.Exponent.Length); Marshal.Copy(rsaParms.Exponent, 0, attrib.pValue, valueLen); } break; case CryptokiAttribType.Prime: if (key is DSACryptoServiceProvider) { if (!hasDsaParms) { dsaParms = ((DSACryptoServiceProvider)key).ExportParameters(true); } valueLen = Math.Min((int)attrib.ulValueLen, dsaParms.P.Length); Marshal.Copy(dsaParms.P, 0, attrib.pValue, valueLen); } break; case CryptokiAttribType.Subprime: if (key is DSACryptoServiceProvider) { if (!hasDsaParms) { dsaParms = ((DSACryptoServiceProvider)key).ExportParameters(true); } valueLen = Math.Min((int)attrib.ulValueLen, dsaParms.Q.Length); Marshal.Copy(dsaParms.Q, 0, attrib.pValue, valueLen); } break; case CryptokiAttribType.Base: if (key is DSACryptoServiceProvider) { if (!hasDsaParms) { dsaParms = ((DSACryptoServiceProvider)key).ExportParameters(true); } valueLen = Math.Min((int)attrib.ulValueLen, dsaParms.G.Length); Marshal.Copy(dsaParms.G, 0, attrib.pValue, valueLen); } break; case CryptokiAttribType.ValueLen: Marshal.WriteInt32(attrib.pValue, valueLen); break; case CryptokiAttribType.KeyType: if (key is DSACryptoServiceProvider) { Marshal.WriteInt32(attrib.pValue, (int)KeyType.DSA); } else if (key is RSACryptoServiceProvider) { Marshal.WriteInt32(attrib.pValue, (int)KeyType.RSA); } else if (key is ECDsaCng) { Marshal.WriteInt32(attrib.pValue, (int)KeyType.ECDSA); } else if (key is ECDiffieHellman) { Marshal.WriteInt32(attrib.pValue, (int)KeyType.EC); } else if (key is AesCryptoServiceProvider) { Marshal.WriteInt32(attrib.pValue, (int)KeyType.AES); } else if (key is TripleDESCryptoServiceProvider) { Marshal.WriteInt32(attrib.pValue, (int)KeyType.DES3); } else if (key is KeyManagementDriver.SecretKey) { Marshal.WriteInt32(attrib.pValue, (int)KeyType.GENERIC_SECRET); } break; case CryptokiAttribType.ValueBits: if (key is DSACryptoServiceProvider) { Marshal.WriteInt32(attrib.pValue, ((DSACryptoServiceProvider)key).KeySize); } else if (key is RSACryptoServiceProvider) { Marshal.WriteInt32(attrib.pValue, ((RSACryptoServiceProvider)key).KeySize); } else if (key is ECDsaCng) { Marshal.WriteInt32(attrib.pValue, ((ECDsaCng)key).KeySize); } else if (key is ECDiffieHellman) { Marshal.WriteInt32(attrib.pValue, ((ECDiffieHellman)key).KeySize); } else if (key is AesCryptoServiceProvider) { Marshal.WriteInt32(attrib.pValue, ((AesCryptoServiceProvider)key).KeySize); } else if (key is TripleDESCryptoServiceProvider) { Marshal.WriteInt32(attrib.pValue, ((TripleDESCryptoServiceProvider)key).KeySize); } else if (key is KeyManagementDriver.SecretKey) { Marshal.WriteInt32(attrib.pValue, ((KeyManagementDriver.SecretKey)key).Size); } break; case CryptokiAttribType.Value: if (key is AesCryptoServiceProvider) { AesCryptoServiceProvider aes = (AesCryptoServiceProvider)key; valueLen = Math.Min((int)attrib.ulValueLen, aes.Key.Length); Marshal.Copy(aes.Key, 0, attrib.pValue, valueLen); } else if (key is KeyManagementDriver.SecretKey) { KeyManagementDriver.SecretKey sk = (KeyManagementDriver.SecretKey)key; valueLen = Math.Min((int)attrib.ulValueLen, sk.Data.Length); Marshal.Copy(sk.Data, 0, attrib.pValue, valueLen); } else if (key is ECDiffieHellman) { ECDiffieHellman ecdh = (ECDiffieHellman)key; byte[] pubKey = ecdh.PublicKey.ToByteArray(); valueLen = Math.Min((int)attrib.ulValueLen, pubKey.Length); Marshal.Copy(pubKey, 0, attrib.pValue, valueLen); } break; case CryptokiAttribType.Modulus: if (key is RSACryptoServiceProvider) { RSACryptoServiceProvider rsa = (RSACryptoServiceProvider)key; if (rsaParms.Modulus == null) { rsaParms = rsa.ExportParameters(true); } valueLen = Math.Min((int)attrib.ulValueLen, rsaParms.Modulus.Length); Marshal.Copy(rsaParms.Modulus, 0, attrib.pValue, valueLen); } break; case CryptokiAttribType.Prime1: if (key is RSACryptoServiceProvider) { RSACryptoServiceProvider rsa = (RSACryptoServiceProvider)key; if (rsaParms.Modulus == null) { rsaParms = rsa.ExportParameters(true); } valueLen = Math.Min((int)attrib.ulValueLen, rsaParms.P.Length); Marshal.Copy(rsaParms.P, 0, attrib.pValue, valueLen); } break; case CryptokiAttribType.Prime2: if (key is RSACryptoServiceProvider) { RSACryptoServiceProvider rsa = (RSACryptoServiceProvider)key; if (rsaParms.Modulus == null) { rsaParms = rsa.ExportParameters(true); } valueLen = Math.Min((int)attrib.ulValueLen, rsaParms.Q.Length); Marshal.Copy(rsaParms.Q, 0, attrib.pValue, valueLen); } break; case CryptokiAttribType.Exponent1: if (key is RSACryptoServiceProvider) { RSACryptoServiceProvider rsa = (RSACryptoServiceProvider)key; if (rsaParms.Modulus == null) { rsaParms = rsa.ExportParameters(true); } valueLen = Math.Min((int)attrib.ulValueLen, rsaParms.DP.Length); Marshal.Copy(rsaParms.DP, 0, attrib.pValue, valueLen); } break; case CryptokiAttribType.Exponent2: if (key is RSACryptoServiceProvider) { RSACryptoServiceProvider rsa = (RSACryptoServiceProvider)key; if (rsaParms.Modulus == null) { rsaParms = rsa.ExportParameters(true); } valueLen = Math.Min((int)attrib.ulValueLen, rsaParms.DQ.Length); Marshal.Copy(rsaParms.DQ, 0, attrib.pValue, valueLen); } break; case CryptokiAttribType.Coefficent: if (key is RSACryptoServiceProvider) { RSACryptoServiceProvider rsa = (RSACryptoServiceProvider)key; if (rsaParms.Modulus == null) { rsaParms = rsa.ExportParameters(true); } valueLen = Math.Min((int)attrib.ulValueLen, rsaParms.InverseQ.Length); Marshal.Copy(rsaParms.InverseQ, 0, attrib.pValue, valueLen); } break; } } pTemplate += Marshal.SizeOf(attrib); } } catch { return(false); } return(true); }
public bool FindObjectsInit(int session, IntPtr pTemplate, int ulCount) { try { SessionData ctx = ((SessionDriver)this.Hal.Session).GetSessionCtx(session); if (ctx.FindObjCtx != null) { return(false); } ctx.FindObjCtx = new FindObjectsContext(); string fileName = ""; string group = ""; CryptokiObjectType type = CryptokiObjectType.Data; byte[] data; while (ulCount-- > 0) { CryptokiAttribute attrib = new CryptokiAttribute(); Marshal.PtrToStructure(pTemplate, attrib); switch ((CryptokiAttribType)attrib.type) { case CryptokiAttribType.Class: switch ((CryptokiClass)Marshal.ReadInt32(attrib.pValue)) { case CryptokiClass.CERTIFICATE: type = CryptokiObjectType.Cert; break; case CryptokiClass.OTP_KEY: type = CryptokiObjectType.Key; break; } break; case CryptokiAttribType.ObjectID: data = new byte[attrib.ulValueLen]; Marshal.Copy(attrib.pValue, data, 0, data.Length); fileName = UTF8Encoding.UTF8.GetString(data); break; case CryptokiAttribType.Label: data = new byte[attrib.ulValueLen]; Marshal.Copy(attrib.pValue, data, 0, data.Length); group = UTF8Encoding.UTF8.GetString(data); break; } pTemplate += Marshal.SizeOf(attrib); } ctx.FindObjCtx.FileName = fileName; ctx.FindObjCtx.Group = group; ctx.FindObjCtx.Type = type; return(true); } catch { return(false); } }