/// <summary> /// Loads all Cryptoki certificates for the specified certificate store (if supported). /// </summary> /// <param name="session">The Cryptoki session context.</param> /// <param name="storeName">The certificate store moniker.</param> /// <returns>The loaded cryptoki certificate object array</returns> public static CryptokiCertificate[] LoadCertificates(Session session, string storeName) { CryptokiCertificate[] certs; CryptokiAttribute[] attribs = new CryptokiAttribute[] { new CryptokiAttribute(CryptokiAttribute.CryptokiType.Class, Utility.ConvertToBytes((int)CryptokiClass.CERTIFICATE)), new CryptokiAttribute(CryptokiAttribute.CryptokiType.Label, UTF8Encoding.UTF8.GetBytes(storeName)), }; FindObjectEnum objEnum = new FindObjectEnum(session, attribs); if (objEnum.Count == 0) { certs = new CryptokiCertificate[0]; } else { Array ar = objEnum.GetNext(objEnum.Count); certs = ar as CryptokiCertificate[]; } objEnum.Close(); return(certs); }
/// <summary> /// Loads all Cryptoki certificates for the specified certificate store (if supported). /// </summary> /// <param name="session">The Cryptoki session context.</param> /// <param name="storeName">The certificate store moniker.</param> /// <returns>The loaded cryptoki certificate object array</returns> public static CryptokiCertificate[] LoadCertificates(Session session, string storeName) { CryptokiCertificate[] certs; CryptokiAttribute[] attribs = new CryptokiAttribute[] { new CryptokiAttribute(CryptokiAttribute.CryptokiType.Class, Utility.ConvertToBytes((int)CryptokiClass.CERTIFICATE)), new CryptokiAttribute(CryptokiAttribute.CryptokiType.Label, UTF8Encoding.UTF8.GetBytes(storeName)), }; FindObjectEnum objEnum = new FindObjectEnum(session, attribs); if (objEnum.Count == 0) { certs = new CryptokiCertificate[0]; } else { Array ar = objEnum.GetNext(objEnum.Count); certs = ar as CryptokiCertificate[]; } objEnum.Close(); return certs; }
/// <summary> /// Opens a CryptoKey with the specified key name from the underlying key store. /// </summary> /// <param name="session">The Cryptoki session context.</param> /// <param name="keyName">The name of the key to be opened.</param> /// <returns>The CryptoKey for the specifed key name.</returns> public static CryptoKey OpenKey(Session session, string keyName, string keyStore="") { CryptokiAttribute[] template = new CryptokiAttribute[] { new CryptokiAttribute(CryptokiAttribute.CryptokiType.Class, Utility.ConvertToBytes((int)CryptokiClass.OTP_KEY)), new CryptokiAttribute(CryptokiAttribute.CryptokiType.Label, System.Text.UTF8Encoding.UTF8.GetBytes(keyStore)), new CryptokiAttribute(CryptokiAttribute.CryptokiType.ObjectID, System.Text.UTF8Encoding.UTF8.GetBytes(keyName)) }; using (FindObjectEnum objects = new FindObjectEnum(session, template)) { CryptokiObject[] objs = objects.GetNext(1); if (objs != null && objs.Length == 1 && objs[0] is CryptoKey) { return (CryptoKey)objs[0]; } } return null; }