using GostCryptography.Asn1.Ber;
using GostCryptography.Properties;
using System;
namespace GostCryptography.Asn1.Gost.Gost_R3410
{
///
/// Параметры ключа цифровой подписи ГОСТ Р 34.10
///
public abstract class Gost_R3410_KeyExchangeParams
{
protected Gost_R3410_KeyExchangeParams()
{
}
protected Gost_R3410_KeyExchangeParams(Gost_R3410_KeyExchangeParams other)
{
DigestParamSet = other.DigestParamSet;
PublicKeyParamSet = other.PublicKeyParamSet;
EncryptionParamSet = other.EncryptionParamSet;
PublicKey = other.PublicKey;
PrivateKey = other.PrivateKey;
}
///
/// Идентификатор OID параметров хэширования
///
public string DigestParamSet { get; set; }
///
/// Идентификатор OID параметров открытого ключа
///
public string PublicKeyParamSet { get; set; }
///
/// Идентификатор OID параметров шифрования
///
public string EncryptionParamSet { get; set; }
///
/// Открытый ключ
///
public byte[] PublicKey { get; set; }
///
/// Закрытый ключ
///
public byte[] PrivateKey { get; set; }
public abstract Gost_R3410_KeyExchangeParams Clone();
protected abstract Gost_R3410_PublicKey CreatePublicKey();
protected abstract Gost_R3410_PublicKeyParams CreatePublicKeyParams();
///
/// Расшифровать параметры
///
public void DecodeParameters(byte[] data)
{
if (data == null)
{
throw ExceptionUtility.ArgumentNull(nameof(data));
}
try
{
var asnDecoder = new Asn1BerDecodeBuffer(data);
var publicKeyParams = CreatePublicKeyParams();
publicKeyParams.Decode(asnDecoder);
PublicKeyParamSet = publicKeyParams.PublicKeyParamSet.Oid.Value;
DigestParamSet = publicKeyParams.DigestParamSet?.Oid.Value;
EncryptionParamSet = publicKeyParams.EncryptionParamSet?.Oid.Value;
}
catch (Exception exception)
{
throw ExceptionUtility.CryptographicException(exception, Resources.Asn1DecodeError, nameof(Gost_R3410_PublicKeyParams));
}
}
///
/// Зашифровать параметры
///
public byte[] EncodeParameters()
{
byte[] data;
try
{
var publicKeyParams = CreatePublicKeyParams();
publicKeyParams.PublicKeyParamSet = Asn1ObjectIdentifier.FromString(PublicKeyParamSet);
publicKeyParams.DigestParamSet = Asn1ObjectIdentifier.FromString(DigestParamSet);
publicKeyParams.EncryptionParamSet = Asn1ObjectIdentifier.FromString(EncryptionParamSet);
var asnEncoder = new Asn1BerEncodeBuffer();
publicKeyParams.Encode(asnEncoder);
data = asnEncoder.MsgCopy;
}
catch (Exception exception)
{
throw ExceptionUtility.CryptographicException(exception, Resources.Asn1EncodeError, nameof(Gost_R3410_PublicKeyParams));
}
return data;
}
///
/// Расшифровать публичный ключ
///
public void DecodePublicKey(byte[] data)
{
if (data == null)
{
throw ExceptionUtility.ArgumentNull(nameof(data));
}
try
{
var asnDecoder = new Asn1BerDecodeBuffer(data);
var publicKey = CreatePublicKey();
publicKey.Decode(asnDecoder);
PublicKey = publicKey.Value;
}
catch (Exception exception)
{
throw ExceptionUtility.CryptographicException(exception, Resources.Asn1DecodeError, nameof(Gost_R3410_PublicKey));
}
}
///
/// Зашифровать публичный ключ
///
public byte[] EncodePublicKey()
{
byte[] data;
try
{
var publicKey = CreatePublicKey();
publicKey.Value = PublicKey;
var asnEncoder = new Asn1BerEncodeBuffer();
publicKey.Encode(asnEncoder);
data = asnEncoder.MsgCopy;
}
catch (Exception exception)
{
throw ExceptionUtility.CryptographicException(exception, Resources.Asn1EncodeError, nameof(Gost_R3410_PublicKeyParams));
}
return data;
}
}
}