Add migrated to .NET 8.0 variant of Hcs.Client
This commit is contained in:
81
Hcs.ClientNet/Client/Internal/CertificateHelper.cs
Normal file
81
Hcs.ClientNet/Client/Internal/CertificateHelper.cs
Normal file
@ -0,0 +1,81 @@
|
||||
using GostCryptography.Base;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Security.Cryptography.X509Certificates;
|
||||
|
||||
namespace Hcs.ClientNet.Internal
|
||||
{
|
||||
internal static class CertificateHelper
|
||||
{
|
||||
internal static bool IsGostPrivateKey(this X509Certificate2 certificate)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (certificate.HasPrivateKey)
|
||||
{
|
||||
var cspInfo = certificate.GetPrivateKeyInfo();
|
||||
if (cspInfo.ProviderType == (int)ProviderType.CryptoPro ||
|
||||
cspInfo.ProviderType == (int)ProviderType.VipNet ||
|
||||
cspInfo.ProviderType == (int)ProviderType.CryptoPro_2012_512 ||
|
||||
cspInfo.ProviderType == (int)ProviderType.CryptoPro_2012_1024)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
internal static GostXades.CryptoProviderTypeEnum GetProviderType(this X509Certificate2 certificate)
|
||||
{
|
||||
return (GostXades.CryptoProviderTypeEnum)GetProviderInfo(certificate).Item1;
|
||||
}
|
||||
|
||||
internal static Tuple<int, string> GetProviderInfo(this X509Certificate2 certificate)
|
||||
{
|
||||
if (certificate.HasPrivateKey)
|
||||
{
|
||||
var cspInfo = certificate.GetPrivateKeyInfo();
|
||||
return new Tuple<int, string>(cspInfo.ProviderType, cspInfo.ProviderName);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception("Certificate has no private key");
|
||||
}
|
||||
}
|
||||
|
||||
internal static X509Certificate2 FindCertificate(Func<X509Certificate2, bool> predicate)
|
||||
{
|
||||
if (predicate == null)
|
||||
{
|
||||
throw new ArgumentException("Null subject predicate");
|
||||
}
|
||||
|
||||
var store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
|
||||
try
|
||||
{
|
||||
store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
|
||||
|
||||
var collection = store.Certificates
|
||||
.OfType<X509Certificate2>()
|
||||
.Where(x => x.HasPrivateKey && x.IsGostPrivateKey());
|
||||
|
||||
var now = DateTime.Now;
|
||||
return collection.First(
|
||||
x => now >= x.NotBefore && now <= x.NotAfter && predicate(x));
|
||||
}
|
||||
finally
|
||||
{
|
||||
store.Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
25
Hcs.ClientNet/Client/Internal/Constants.cs
Normal file
25
Hcs.ClientNet/Client/Internal/Constants.cs
Normal file
@ -0,0 +1,25 @@
|
||||
namespace Hcs.ClientNet.Internal
|
||||
{
|
||||
internal static class Constants
|
||||
{
|
||||
/// <summary>
|
||||
/// Имя XML-элемента в сообщении, которое будет подписываться в фильтре
|
||||
/// отправки подписывающем XML
|
||||
/// </summary>
|
||||
internal const string SIGNED_XML_ELEMENT_ID = "signed-data-container";
|
||||
|
||||
/// <summary>
|
||||
/// Если PIN сертификата не указан пользователем, применяется это значение
|
||||
/// по умолчанию для сертификатов RuToken
|
||||
/// </summary>
|
||||
internal const string DEFAULT_CERTIFICATE_PIN = "12345678";
|
||||
|
||||
internal const string URI_PPAK = "api.dom.gosuslugi.ru";
|
||||
internal const string URI_SIT_01 = "sit01.dom.test.gosuslugi.ru:10081";
|
||||
internal const string URI_SIT_02 = "sit02.dom.test.gosuslugi.ru:10081";
|
||||
internal const string URI_TUNNEL = "127.0.0.1:8080";
|
||||
|
||||
internal const string NAME_SIT = "sit";
|
||||
internal const string PASSWORD_SIT = "xw{p&&Ee3b9r8?amJv*]";
|
||||
}
|
||||
}
|
||||
63
Hcs.ClientNet/Client/Internal/Util.cs
Normal file
63
Hcs.ClientNet/Client/Internal/Util.cs
Normal file
@ -0,0 +1,63 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Hcs.ClientNet.Internal
|
||||
{
|
||||
internal static class Util
|
||||
{
|
||||
/// <summary>
|
||||
/// Возвращает список все вложенных исключений для данного исключения
|
||||
/// </summary>
|
||||
internal static List<Exception> EnumerateInnerExceptions(Exception e)
|
||||
{
|
||||
var list = new List<Exception>();
|
||||
WalkInnerExceptionsRecurse(e, list);
|
||||
return list;
|
||||
}
|
||||
|
||||
private static void WalkInnerExceptionsRecurse(Exception e, List<Exception> list)
|
||||
{
|
||||
if (e == null || list.Contains(e))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
list.Add(e);
|
||||
|
||||
WalkInnerExceptionsRecurse(e.InnerException, list);
|
||||
|
||||
if (e is AggregateException)
|
||||
{
|
||||
var aggregate = e as AggregateException;
|
||||
foreach (var inner in aggregate.InnerExceptions)
|
||||
{
|
||||
WalkInnerExceptionsRecurse(inner, list);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal static string FormatDate(DateTime date)
|
||||
{
|
||||
return date.ToString("yyyyMMdd");
|
||||
}
|
||||
|
||||
internal static string FormatDate(DateTime? date)
|
||||
{
|
||||
return (date == null) ? string.Empty : FormatDate((DateTime)date);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Преобразует массиб байтов в строку в формате binhex
|
||||
/// </summary>
|
||||
internal static string ConvertToHexString(byte[] ba)
|
||||
{
|
||||
var buf = new StringBuilder(ba.Length * 2);
|
||||
foreach (byte b in ba)
|
||||
{
|
||||
buf.AppendFormat("{0:x2}", b);
|
||||
}
|
||||
return buf.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user