Add project
Basic formatting applied. Unnecessary comments have been removed. Suspicious code is covered by TODO.
This commit is contained in:
@ -0,0 +1,17 @@
|
||||
using GostCryptography.Asn1.Ber;
|
||||
|
||||
namespace GostCryptography.Asn1.Gost.PublicKey
|
||||
{
|
||||
public sealed class AlgorithmId
|
||||
{
|
||||
public AlgorithmId(Asn1ObjectIdentifier id, Asn1Type type)
|
||||
{
|
||||
Id = id;
|
||||
Type = type;
|
||||
}
|
||||
|
||||
public Asn1ObjectIdentifier Id { get; }
|
||||
|
||||
public Asn1Type Type { get; }
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,100 @@
|
||||
using GostCryptography.Asn1.Ber;
|
||||
using GostCryptography.Properties;
|
||||
using System;
|
||||
|
||||
namespace GostCryptography.Asn1.Gost.PublicKey
|
||||
{
|
||||
public sealed class AlgorithmIdentifier : Asn1Type
|
||||
{
|
||||
public AlgorithmIdentifier()
|
||||
{
|
||||
}
|
||||
|
||||
public AlgorithmIdentifier(Asn1ObjectIdentifier algorithm, Asn1OpenType parameters)
|
||||
{
|
||||
Algorithm = algorithm;
|
||||
Parameters = parameters;
|
||||
}
|
||||
|
||||
public Asn1ObjectIdentifier Algorithm { get; private set; }
|
||||
|
||||
public Asn1Type Parameters { get; private set; }
|
||||
|
||||
public override void Decode(Asn1BerDecodeBuffer buffer, bool explicitTagging, int implicitLength)
|
||||
{
|
||||
var elemLength = explicitTagging ? MatchTag(buffer, Asn1Tag.Sequence) : implicitLength;
|
||||
|
||||
Algorithm = null;
|
||||
Parameters = null;
|
||||
|
||||
var context = new Asn1BerDecodeContext(buffer, elemLength);
|
||||
var parsedLen = new IntHolder();
|
||||
|
||||
if (!context.MatchElemTag(0, 0, ObjectIdentifierTypeCode, parsedLen, false))
|
||||
{
|
||||
throw ExceptionUtility.CryptographicException(Resources.Asn1MissingRequiredException, buffer.ByteCount);
|
||||
}
|
||||
|
||||
Algorithm = new Asn1ObjectIdentifier();
|
||||
Algorithm.Decode(buffer, true, parsedLen.Value);
|
||||
|
||||
if (!context.Expired())
|
||||
{
|
||||
Parameters = new Asn1OpenType();
|
||||
Parameters.Decode(buffer, true, 0);
|
||||
}
|
||||
|
||||
CheckAlg(true);
|
||||
}
|
||||
|
||||
public override int Encode(Asn1BerEncodeBuffer buffer, bool explicitTagging)
|
||||
{
|
||||
var len = 0;
|
||||
CheckAlg(false);
|
||||
|
||||
if (Parameters != null)
|
||||
{
|
||||
len += Parameters.Encode(buffer, true);
|
||||
}
|
||||
|
||||
len += Algorithm.Encode(buffer, true);
|
||||
|
||||
if (explicitTagging)
|
||||
{
|
||||
len += buffer.EncodeTagAndLength(Asn1Tag.Sequence, len);
|
||||
}
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
private void CheckAlg(bool decode)
|
||||
{
|
||||
AlgorithmId algorithmId = null;
|
||||
|
||||
foreach (var alg in PkiConstants.SupportedAlgorithms)
|
||||
{
|
||||
if (alg.Id.Equals(Algorithm))
|
||||
{
|
||||
algorithmId = alg;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if ((algorithmId != null) && ((decode && (Parameters != null)) && (algorithmId.Type != null)))
|
||||
{
|
||||
try
|
||||
{
|
||||
var buffer = new Asn1BerDecodeBuffer(((Asn1OpenType)Parameters).Value);
|
||||
Parameters = (Asn1Type)Activator.CreateInstance(algorithmId.Type.GetType());
|
||||
Parameters.Decode(buffer, true, 0);
|
||||
buffer.InvokeEndElement("parameters", -1);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
Asn1Util.WriteStackTrace(exception, Console.Error);
|
||||
throw ExceptionUtility.CryptographicException(Resources.Asn1TableConstraint);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
116
Hcs.Client/GostCryptography/Asn1/Gost/PublicKey/PkiConstants.cs
Normal file
116
Hcs.Client/GostCryptography/Asn1/Gost/PublicKey/PkiConstants.cs
Normal file
@ -0,0 +1,116 @@
|
||||
using GostCryptography.Asn1.Ber;
|
||||
using GostCryptography.Asn1.Gost.Gost_28147_89;
|
||||
using GostCryptography.Asn1.Gost.Gost_R3410_2001;
|
||||
using GostCryptography.Asn1.Gost.Gost_R3410_2012_256;
|
||||
using GostCryptography.Asn1.Gost.Gost_R3410_2012_512;
|
||||
using GostCryptography.Asn1.Gost.Gost_R3410_94;
|
||||
|
||||
namespace GostCryptography.Asn1.Gost.PublicKey
|
||||
{
|
||||
static class PkiConstants
|
||||
{
|
||||
// ГОСТ 28147-89
|
||||
|
||||
private static readonly AlgorithmId Gost_28147_89_EncryptAlgorithm = new AlgorithmId(
|
||||
new Asn1ObjectIdentifier(Gost_28147_89_Constants.EncryptAlgorithm),
|
||||
new Gost_28147_89_Params());
|
||||
|
||||
// ГОСТ Р 34.10-94
|
||||
|
||||
private static readonly AlgorithmId Gost_R3410_94_KeyAlgorithm = new AlgorithmId(
|
||||
new Asn1ObjectIdentifier(Gost_R3410_94_Constants.KeyAlgorithm),
|
||||
new Gost_R3410_94_PublicKeyType());
|
||||
|
||||
private static readonly AlgorithmId Gost_R3410_94_DhAlgorithm = new AlgorithmId(
|
||||
new Asn1ObjectIdentifier(Gost_R3410_94_Constants.DhAlgorithm),
|
||||
new Gost_R3410_94_DhPublicKeyType());
|
||||
|
||||
private static readonly AlgorithmId Gost_R3410_94_SignatureAlgorithm = new AlgorithmId(
|
||||
new Asn1ObjectIdentifier(Gost_R3410_94_Constants.SignatureAlgorithm),
|
||||
new NullParams());
|
||||
|
||||
private static readonly AlgorithmId Gost_R3411_94_HashAlgorithm = new AlgorithmId(
|
||||
new Asn1ObjectIdentifier(Gost_R3410_94_Constants.HashAlgorithm),
|
||||
new Gost_R3411_94_DigestParamsType());
|
||||
|
||||
// ГОСТ Р 34.10-2001
|
||||
|
||||
private static readonly AlgorithmId Gost_R3410_2001_KeyAlgorithm = new AlgorithmId(
|
||||
new Asn1ObjectIdentifier(Gost_R3410_2001_Constants.KeyAlgorithm),
|
||||
new Gost_R3410_2001_PublicKeyType());
|
||||
|
||||
private static readonly AlgorithmId Gost_R3410_2001_DhAlgorithm = new AlgorithmId(
|
||||
new Asn1ObjectIdentifier(Gost_R3410_2001_Constants.DhAlgorithm),
|
||||
new Gost_R3410_2001_DhPublicKeyType());
|
||||
|
||||
private static readonly AlgorithmId Gost_R3410_2001_SignatureAlgorithm = new AlgorithmId(
|
||||
new Asn1ObjectIdentifier(Gost_R3410_2001_Constants.SignatureAlgorithm),
|
||||
new NullParams());
|
||||
|
||||
private static readonly AlgorithmId Gost_R3411_2001_HashAlgorithm = new AlgorithmId(
|
||||
new Asn1ObjectIdentifier(Gost_R3410_2001_Constants.HashAlgorithm),
|
||||
new Gost_R3411_2001_DigestParamsType());
|
||||
|
||||
// ГОСТ Р 34.10-2012/256
|
||||
|
||||
private static readonly AlgorithmId Gost_R3410_2012_256_KeyAlgorithm = new AlgorithmId(
|
||||
new Asn1ObjectIdentifier(Gost_R3410_2012_256_Constants.KeyAlgorithm),
|
||||
new Gost_R3410_2012_256_PublicKeyType());
|
||||
|
||||
private static readonly AlgorithmId Gost_R3410_2012_256_DhAlgorithm = new AlgorithmId(
|
||||
new Asn1ObjectIdentifier(Gost_R3410_2012_256_Constants.DhAlgorithm),
|
||||
new Gost_R3410_2012_256_DhPublicKeyType());
|
||||
|
||||
private static readonly AlgorithmId Gost_R3410_2012_256_SignatureAlgorithm = new AlgorithmId(
|
||||
new Asn1ObjectIdentifier(Gost_R3410_2012_256_Constants.SignatureAlgorithm),
|
||||
new NullParams());
|
||||
|
||||
private static readonly AlgorithmId Gost_R3411_2012_256_HashAlgorithm = new AlgorithmId(
|
||||
new Asn1ObjectIdentifier(Gost_R3410_2012_256_Constants.HashAlgorithm),
|
||||
new Gost_R3411_2012_256_DigestParamsType());
|
||||
|
||||
// ГОСТ Р 34.10-2012/512
|
||||
|
||||
private static readonly AlgorithmId Gost_R3410_2012_512_KeyAlgorithm = new AlgorithmId(
|
||||
new Asn1ObjectIdentifier(Gost_R3410_2012_512_Constants.KeyAlgorithm),
|
||||
new Gost_R3410_2012_512_PublicKeyType());
|
||||
|
||||
private static readonly AlgorithmId Gost_R3410_2012_512_DhAlgorithm = new AlgorithmId(
|
||||
new Asn1ObjectIdentifier(Gost_R3410_2012_512_Constants.DhAlgorithm),
|
||||
new Gost_R3410_2012_512_DhPublicKeyType());
|
||||
|
||||
private static readonly AlgorithmId Gost_R3410_2012_512_SignatureAlgorithm = new AlgorithmId(
|
||||
new Asn1ObjectIdentifier(Gost_R3410_2012_512_Constants.SignatureAlgorithm),
|
||||
new NullParams());
|
||||
|
||||
private static readonly AlgorithmId Gost_R3411_2012_512_HashAlgorithm = new AlgorithmId(
|
||||
new Asn1ObjectIdentifier(Gost_R3410_2012_512_Constants.HashAlgorithm),
|
||||
new Gost_R3411_2012_512_DigestParamsType());
|
||||
|
||||
|
||||
public static readonly AlgorithmId[] SupportedAlgorithms =
|
||||
{
|
||||
Gost_28147_89_EncryptAlgorithm,
|
||||
|
||||
Gost_R3410_94_KeyAlgorithm,
|
||||
Gost_R3410_94_DhAlgorithm,
|
||||
Gost_R3410_94_SignatureAlgorithm,
|
||||
Gost_R3411_94_HashAlgorithm,
|
||||
|
||||
Gost_R3410_2001_KeyAlgorithm,
|
||||
Gost_R3410_2001_DhAlgorithm,
|
||||
Gost_R3410_2001_SignatureAlgorithm,
|
||||
Gost_R3411_2001_HashAlgorithm,
|
||||
|
||||
Gost_R3410_2012_256_KeyAlgorithm,
|
||||
Gost_R3410_2012_256_DhAlgorithm,
|
||||
Gost_R3410_2012_256_SignatureAlgorithm,
|
||||
Gost_R3411_2012_256_HashAlgorithm,
|
||||
|
||||
Gost_R3410_2012_512_KeyAlgorithm,
|
||||
Gost_R3410_2012_512_DhAlgorithm,
|
||||
Gost_R3410_2012_512_SignatureAlgorithm,
|
||||
Gost_R3411_2012_512_HashAlgorithm
|
||||
};
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,53 @@
|
||||
using GostCryptography.Asn1.Ber;
|
||||
using GostCryptography.Properties;
|
||||
|
||||
namespace GostCryptography.Asn1.Gost.PublicKey
|
||||
{
|
||||
public sealed class SubjectPublicKeyInfo : Asn1Type
|
||||
{
|
||||
public AlgorithmIdentifier Algorithm { get; set; }
|
||||
|
||||
public Asn1BitString SubjectPublicKey { get; set; }
|
||||
|
||||
public override void Decode(Asn1BerDecodeBuffer buffer, bool explicitTagging, int implicitLength)
|
||||
{
|
||||
var elemLength = explicitTagging ? MatchTag(buffer, Asn1Tag.Sequence) : implicitLength;
|
||||
|
||||
Algorithm = null;
|
||||
SubjectPublicKey = null;
|
||||
|
||||
var context = new Asn1BerDecodeContext(buffer, elemLength);
|
||||
var parsedLen = new IntHolder();
|
||||
|
||||
if (!context.MatchElemTag(0, 0x20, 0x10, parsedLen, false))
|
||||
{
|
||||
throw ExceptionUtility.CryptographicException(Resources.Asn1MissingRequiredException, buffer.ByteCount);
|
||||
}
|
||||
|
||||
Algorithm = new AlgorithmIdentifier();
|
||||
Algorithm.Decode(buffer, true, parsedLen.Value);
|
||||
|
||||
if (!context.MatchElemTag(0, 0, 3, parsedLen, false))
|
||||
{
|
||||
throw ExceptionUtility.CryptographicException(Resources.Asn1MissingRequiredException, buffer.ByteCount);
|
||||
}
|
||||
|
||||
SubjectPublicKey = new Asn1BitString();
|
||||
SubjectPublicKey.Decode(buffer, true, parsedLen.Value);
|
||||
}
|
||||
|
||||
public override int Encode(Asn1BerEncodeBuffer buffer, bool explicitTagging)
|
||||
{
|
||||
var len = 0;
|
||||
len += SubjectPublicKey.Encode(buffer, true);
|
||||
len += Algorithm.Encode(buffer, true);
|
||||
|
||||
if (explicitTagging)
|
||||
{
|
||||
len += buffer.EncodeTagAndLength(Asn1Tag.Sequence, len);
|
||||
}
|
||||
|
||||
return len;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user