public static byte[] GetKeyParameter(SafeKeyHandleImpl keyHandle, uint keyParamId) { uint dataLength = 0; if (!CryptoApi.CryptGetKeyParam(keyHandle, keyParamId, null, ref dataLength, 0)) { throw CreateWin32Error(); } var dataBytes = new byte[dataLength]; if (!CryptoApi.CryptGetKeyParam(keyHandle, keyParamId, dataBytes, ref dataLength, 0)) { throw CreateWin32Error(); } return(dataBytes); }
public static int GetKeyParameterInt32(SafeKeyHandleImpl keyHandle, uint keyParamId) { const int doubleWordSize = 4; uint dwDataLength = doubleWordSize; var dwDataBytes = new byte[doubleWordSize]; if (!CryptoApi.CryptGetKeyParam(keyHandle, keyParamId, dwDataBytes, ref dwDataLength, 0)) { throw CreateWin32Error(); } if (dwDataLength != doubleWordSize) { throw ExceptionUtility.CryptographicException(Constants.NTE_BAD_DATA); } return(BitConverter.ToInt32(dwDataBytes, 0)); }
/// <summary> /// Возвращает сертификат X.509 для указанного ключа. /// </summary> /// <param name="keyHandle">Дескриптор ключа сертификата.</param> public static X509Certificate2 GetKeyCertificate(SafeKeyHandleImpl keyHandle) { uint certDataLength = 0; if (!CryptoApi.CryptGetKeyParam(keyHandle, Constants.KP_CERTIFICATE, null, ref certDataLength, 0)) { if (Marshal.GetLastWin32Error() != Constants.ERROR_NO_SUCH_CERTIFICATE) { throw CreateWin32Error(); } return(null); } var certData = new byte[certDataLength]; if (!CryptoApi.CryptGetKeyParam(keyHandle, Constants.KP_CERTIFICATE, certData, ref certDataLength, 0)) { throw CreateWin32Error(); } return(new X509Certificate2(certData)); }