private SubjectPublicKeyInfo EncodePublicKey(Gost_R3410_KeyExchangeParams transportParameters) { var asnEncoder = new Asn1BerEncodeBuffer(); var publicKey = new Asn1OctetString(transportParameters.PublicKey); publicKey.Encode(asnEncoder); var publicKeyValue = asnEncoder.MsgCopy; var publicKeyInfo = new SubjectPublicKeyInfo { SubjectPublicKey = new Asn1BitString(publicKeyValue.Length * 8, publicKeyValue) }; var publicKeyParams = CreatePublicKeyParams(); publicKeyParams.PublicKeyParamSet = new Asn1ObjectIdentifier(OidValue.FromString(transportParameters.PublicKeyParamSet)); publicKeyParams.DigestParamSet = new Asn1ObjectIdentifier(OidValue.FromString(transportParameters.DigestParamSet)); publicKeyParams.EncryptionParamSet = Gost_28147_89_ParamSet.FromString(transportParameters.EncryptionParamSet); asnEncoder.Reset(); publicKeyParams.Encode(asnEncoder); var publicKeyAlgOid = new Asn1ObjectIdentifier(KeyAlgorithm); publicKeyInfo.Algorithm = new AlgorithmIdentifier(publicKeyAlgOid, new Asn1OpenType(asnEncoder.MsgCopy)); return(publicKeyInfo); }
/// <summary> /// Зашифровать информацию о ключе. /// </summary> public byte[] Encode() { var asnEncoder = new Asn1BerEncodeBuffer(); var keyTransport = new Gost_R3410_KeyTransport(); try { keyTransport.SessionEncryptedKey = new Gost_28147_89_EncryptedKey { EncryptedKey = new Gost_28147_89_Key(SessionEncryptedKey.EncryptedKey), MacKey = new Gost_28147_89_Mac(SessionEncryptedKey.Mac) }; keyTransport.TransportParams = new Gost_R3410_TransportParams { EncryptionParamSet = Gost_28147_89_ParamSet.FromString(SessionEncryptedKey.EncryptionParamSet), EphemeralPublicKey = EncodePublicKey(TransportParameters), Ukm = new Asn1OctetString(SessionEncryptedKey.Ukm) }; keyTransport.Encode(asnEncoder); } catch (Exception exception) { throw ExceptionUtility.CryptographicException(exception, Resources.Asn1EncodeError, nameof(Gost_R3410_KeyTransport)); } return(asnEncoder.MsgCopy); }
public override void Decode(Asn1BerDecodeBuffer buffer, bool explicitTagging, int implicitLength) { var elemLength = explicitTagging ? MatchTag(buffer, Asn1Tag.Sequence) : implicitLength; DigestParamSet = null; PublicKeyParamSet = null; EncryptionParamSet = 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); } PublicKeyParamSet = new Asn1ObjectIdentifier(); PublicKeyParamSet.Decode(buffer, true, parsedLen.Value); if (!context.MatchElemTag(0, 0, ObjectIdentifierTypeCode, parsedLen, false)) { throw ExceptionUtility.CryptographicException(Resources.Asn1MissingRequiredException, buffer.ByteCount); } DigestParamSet = new Asn1ObjectIdentifier(); DigestParamSet.Decode(buffer, true, parsedLen.Value); if (context.MatchElemTag(0, 0, ObjectIdentifierTypeCode, parsedLen, false)) { EncryptionParamSet = new Gost_28147_89_ParamSet(); EncryptionParamSet.Decode(buffer, true, parsedLen.Value); } }
/// <summary> /// Зашифровать параметры. /// </summary> public byte[] EncodeParameters() { byte[] data; try { var publicKeyParams = CreatePublicKeyParams(); publicKeyParams.DigestParamSet = new Asn1ObjectIdentifier(OidValue.FromString(DigestParamSet)); publicKeyParams.PublicKeyParamSet = new Asn1ObjectIdentifier(OidValue.FromString(PublicKeyParamSet)); publicKeyParams.EncryptionParamSet = Gost_28147_89_ParamSet.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 override void Decode(Asn1BerDecodeBuffer buffer, bool explicitTagging, int implicitLength) { var elemLength = explicitTagging ? MatchTag(buffer, Asn1Tag.Sequence) : implicitLength; EncryptionParamSet = null; EphemeralPublicKey = null; Ukm = 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); } EncryptionParamSet = new Gost_28147_89_ParamSet(); EncryptionParamSet.Decode(buffer, true, parsedLen.Value); if (context.MatchElemTag(0x80, 0x20, EocTypeCode, parsedLen, true)) { EphemeralPublicKey = new SubjectPublicKeyInfo(); EphemeralPublicKey.Decode(buffer, false, parsedLen.Value); } if (!context.MatchElemTag(0, 0, OctetStringTypeCode, parsedLen, false)) { throw ExceptionUtility.CryptographicException(Resources.Asn1MissingRequiredException, buffer.ByteCount); } Ukm = new Asn1OctetString(); Ukm.Decode(buffer, true, parsedLen.Value); if (Ukm.Length != 8) { throw ExceptionUtility.CryptographicException(Resources.Asn1ConsVioException, nameof(Ukm.Length), Ukm.Length); } }