/// <summary> /// Метод получения алгоритма хэширования /// </summary> /// <param name="signatureAlgOid"></param> /// <returns></returns> internal static CRYPT_OID_INFO GetHashAlg(string signatureAlgOid) { IntPtr sigId = CApiExtWin.CryptFindOIDInfo(OidKeyType.Oid, signatureAlgOid, OidGroup.SignatureAlgorithm); CRYPT_OID_INFO CertInfo = Marshal.PtrToStructure <CRYPT_OID_INFO>(sigId); uint alg = CertInfo.Algid; IntPtr int_addr = Marshal.AllocHGlobal(Marshal.SizeOf(alg)); Marshal.WriteInt32(int_addr, (int)alg); IntPtr sigs = CApiExtWin.CryptFindOIDInfo(OidKeyType.AlgorithmID, int_addr, OidGroup.SignatureAlgorithm); CRYPT_OID_INFO sigsInfo = Marshal.PtrToStructure <CRYPT_OID_INFO>(sigs); if (sigs == IntPtr.Zero) { throw new CryptographicException(Marshal.GetLastWin32Error()); } IntPtr hass = CApiExtWin.CryptFindOIDInfo(OidKeyType.AlgorithmID, int_addr, OidGroup.HashAlgorithm); CRYPT_OID_INFO hassInfo = Marshal.PtrToStructure <CRYPT_OID_INFO>(hass); if (hass == IntPtr.Zero) { throw new CryptographicException(Marshal.GetLastWin32Error()); } return(hassInfo); }
internal static void UnregisterOid(CRYPT_OID_INFO oid) { if (!UnsafeNativeMethods.CryptUnregisterOIDInfo(ref oid)) { Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error()); } }
internal static int GetAlgId(IntPtr certHandle) { try { if (certHandle == IntPtr.Zero) { throw new Exception("Ошибка при попытке получить Handle сертификата."); } var certContext = Marshal.PtrToStructure <CERT_CONTEXT>(certHandle); var certInfo = Marshal.PtrToStructure <CERT_INFO>(certContext.pCertInfo); var publicKeyAlg = certInfo.SubjectPublicKeyInfo.Algorithm.pszObjId; string signatureAlgOid = GetSignatureAlg(publicKeyAlg); CRYPT_OID_INFO oidInfo = new CRYPT_OID_INFO(); if (IsUnix) { oidInfo = SignServiceUnix.GetHashAlg(signatureAlgOid); } else { oidInfo = SignServiceWin.GetHashAlg(signatureAlgOid); } return((int)oidInfo.Algid); } catch (Exception ex) { throw new CryptographicException($"Ошибка при получении хэш алгоритма ГОСТ. {ex.Message}."); } }
internal static bool GetExtendedKeyUsagesCallback(IntPtr pInfo, ref OidCollection pvParam) { CRYPT_OID_INFO oInfo = (CRYPT_OID_INFO)Marshal.PtrToStructure(pInfo, typeof(CRYPT_OID_INFO)); OidCollection ExtendedKeyUsages = (OidCollection)pvParam; ExtendedKeyUsages.Add(new Oid(oInfo.pszOID, oInfo.pwszName)); return(true); }
private static bool OidEnumerationCallback(ref CRYPT_OID_INFO oid, IntPtr oidInformationPointer) { Debug.Assert(oidInformationPointer != IntPtr.Zero, "oidInformationPointer != IntPtr.Zero"); GCHandle oidInformationHandle = GCHandle.FromIntPtr(oidInformationPointer); List<CRYPT_OID_INFO> oidInformation = oidInformationHandle.Target as List<CRYPT_OID_INFO>; Debug.Assert(oidInformation != null, "Unexpected type in oidInformationPointer GC Handle"); oidInformation.Add(oid); return true; }
/// <summary> /// Get the HashAlgorithmName from the given ALG_ID /// </summary> internal static HashAlgorithmName?GetHashAlgorithmName(Interop.BCrypt.ECC_CURVE_ALG_ID_ENUM hashId) { CRYPT_OID_INFO oid = Interop.Crypt32.FindAlgIdOidInfo(hashId); if (oid.AlgId == -1) { // The original hash algorithm may not be found and is optional return(null); } return(new HashAlgorithmName(oid.Name)); }
/// <summary> /// Get the ALG_ID from the given HashAlgorithmName /// </summary> internal static Interop.BCrypt.ECC_CURVE_ALG_ID_ENUM GetHashAlgorithmId(HashAlgorithmName?name) { if (name.HasValue == false || string.IsNullOrEmpty(name.Value.Name)) { return(Interop.BCrypt.ECC_CURVE_ALG_ID_ENUM.BCRYPT_NO_CURVE_GENERATION_ALG_ID); } CRYPT_OID_INFO oid = Interop.Crypt32.FindOidInfo( CryptOidInfoKeyType.CRYPT_OID_INFO_NAME_KEY, name.Value.Name, OidGroup.HashAlgorithm, false); if (oid.AlgId == -1) { throw new CryptographicException(SR.Cryptography_UnknownHashAlgorithm, name.Value.Name); } return((Interop.BCrypt.ECC_CURVE_ALG_ID_ENUM)oid.AlgId); }
internal static CRYPT_OID_INFO CryptFindOIDInfo( [In] uint dwKeyType, [In] IntPtr pvKey, [In] OidGroup dwGroupId) { if (pvKey == IntPtr.Zero) { throw new ArgumentNullException("pvKey"); } CRYPT_OID_INFO pOIDInfo = new CRYPT_OID_INFO(Marshal.SizeOf(typeof(CRYPT_OID_INFO))); IntPtr pv = UnsafeNativeMethods.CryptFindOIDInfo(dwKeyType, pvKey, dwGroupId); if (pv != IntPtr.Zero) { pOIDInfo = (CRYPT_OID_INFO)Marshal.PtrToStructure(pv, typeof(CRYPT_OID_INFO)); } return(pOIDInfo); }
internal static CRYPT_OID_INFO CryptFindOIDInfo( [In] uint dwKeyType, [In] SafeLocalAllocHandle pvKey, [In] OidGroup dwGroupId) { if (pvKey == null) { throw new ArgumentNullException("pvKey"); } if (pvKey.IsInvalid) { throw new CryptographicException("SR.GetString(SR.Cryptography_InvalidHandle)", "pvKey"); } CRYPT_OID_INFO pOIDInfo = new CRYPT_OID_INFO(Marshal.SizeOf(typeof(CRYPT_OID_INFO))); IntPtr pv = UnsafeNativeMethods.CryptFindOIDInfo(dwKeyType, pvKey, dwGroupId); if (pv != IntPtr.Zero) { pOIDInfo = (CRYPT_OID_INFO)Marshal.PtrToStructure(pv, typeof(CRYPT_OID_INFO)); } return(pOIDInfo); }
/// <summary> /// Returns (AlgId)(-1) if oid is unknown. /// </summary> public static AlgId ToAlgId(this string oidValue) { CRYPT_OID_INFO info = Interop.Crypt32.FindOidInfo(CryptOidInfoKeyType.CRYPT_OID_INFO_OID_KEY, oidValue, OidGroup.All, false); return((AlgId)(info.AlgId)); }
private static string NativeOidToFriendlyName(string oid, OidGroup oidGroup, bool fallBackToAllGroups) { CRYPT_OID_INFO oidInfo = OidInfo.FindOidInfo(CryptOidInfoKeyType.CRYPT_OID_INFO_OID_KEY, oid, oidGroup, fallBackToAllGroups); return(oidInfo.Name); }
internal static bool TryFindOidInfo(string key, OidGroup group, OidKeyType keyType, bool lookupInActiveDirectory, out CRYPT_OID_INFO oidInfo) { Debug.Assert(!String.IsNullOrEmpty(key), "!String.IsNullOrEmpty(key)"); IntPtr keyPointer = IntPtr.Zero; RuntimeHelpers.PrepareConstrainedRegions(); try { // Setting the CRYPT_OID_DISABLE_SEARCH_DS_FLAG (0x80000000) in the group type suppresses // the network lookup of the OID information. We or the bit in directly, since it's not a // real group type, and we don't want to mix it into the enumeration itself if (!lookupInActiveDirectory) { group = (OidGroup)((uint)group | 0x80000000); } // Convert the key into a native representation based upon search type if (keyType == OidKeyType.Oid) { keyPointer = Marshal.StringToCoTaskMemAnsi(key); } else if (keyType == OidKeyType.Name || keyType == OidKeyType.CngAlgorithmId) { keyPointer = Marshal.StringToCoTaskMemUni(key); } else { Debug.Assert(false, "Unsupported key type"); } IntPtr oid = UnsafeNativeMethods.CryptFindOIDInfo(keyType, keyPointer, group); // Do the search, and if we succeeded, marshal the data back to the caller. The // CRYPT_OID_INFO being pointed to by the result of the search should not be freed by us // because it is owned by CAPI. if (oid != IntPtr.Zero) { if (!UseWin2k3OidStructures) { oidInfo = (CRYPT_OID_INFO)Marshal.PtrToStructure(oid, typeof(CRYPT_OID_INFO)); } else { oidInfo = UpgradeOidInfo((CRYPT_OID_INFO_WIN2K3)Marshal.PtrToStructure(oid, typeof(CRYPT_OID_INFO_WIN2K3))); } return true; } else { // Did not find the OID oidInfo = new CRYPT_OID_INFO(); return false; } } finally { if (keyPointer != IntPtr.Zero) { Marshal.FreeCoTaskMem(keyPointer); } } }
private static string NativeFriendlyNameToOid(string friendlyName, OidGroup oidGroup, bool fallBackToAllGroups) { CRYPT_OID_INFO oidInfo = OidInfo.FindOidInfo(CryptOidInfoKeyType.CRYPT_OID_INFO_NAME_KEY, friendlyName, oidGroup, fallBackToAllGroups); return(oidInfo.OID); }
internal static extern bool CryptRegisterOIDInfo([In] ref CRYPT_OID_INFO pInfo, OidRegistrationOptions dwFlags);
internal static CRYPT_OID_INFO CryptFindOIDInfo([In] uint dwKeyType, [In] System.Security.Cryptography.SafeLocalAllocHandle pvKey, [In] uint dwGroupId) { if (pvKey == null) { throw new ArgumentNullException("pvKey"); } if (pvKey.IsInvalid) { throw new CryptographicException(SecurityResources.GetResourceString("Cryptography_InvalidHandle"), "pvKey"); } CRYPT_OID_INFO crypt_oid_info = new CRYPT_OID_INFO(Marshal.SizeOf(typeof(CRYPT_OID_INFO))); IntPtr ptr = CAPISafe.CryptFindOIDInfo(dwKeyType, pvKey, dwGroupId); if (ptr != IntPtr.Zero) { crypt_oid_info = (CRYPT_OID_INFO) Marshal.PtrToStructure(ptr, typeof(CRYPT_OID_INFO)); } return crypt_oid_info; }
internal static CRYPT_OID_INFO CryptFindOIDInfo([In] uint dwKeyType, [In] IntPtr pvKey, [In] uint dwGroupId) { if (pvKey == IntPtr.Zero) { throw new ArgumentNullException("pvKey"); } CRYPT_OID_INFO crypt_oid_info = new CRYPT_OID_INFO(Marshal.SizeOf(typeof(CRYPT_OID_INFO))); IntPtr ptr = CAPISafe.CryptFindOIDInfo(dwKeyType, pvKey, dwGroupId); if (ptr != IntPtr.Zero) { crypt_oid_info = (CRYPT_OID_INFO) Marshal.PtrToStructure(ptr, typeof(CRYPT_OID_INFO)); } return crypt_oid_info; }
private static bool OidEnumerationCallbackWin2k3(ref CRYPT_OID_INFO_WIN2K3 oid, IntPtr oidInformationPointer) { CRYPT_OID_INFO fullOid = UpgradeOidInfo(oid); return OidEnumerationCallback(ref fullOid, oidInformationPointer); }
internal static extern bool CryptUnregisterOIDInfo([In] ref CRYPT_OID_INFO pInfo);