Ejemplo n.º 1
0
        public override void Decode(Asn1BerDecodeBuffer buffer, bool explicitTagging, int implicitLength)
        {
            var elemLength = explicitTagging ? MatchTag(buffer, Asn1Tag.Sequence) : implicitLength;

            EncryptionParamSet = 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(0, 0, OctetStringTypeCode, parsedLen, false))
            {
                Ukm = new Asn1OctetString();
                Ukm.Decode(buffer, true, parsedLen.Value);

                if (Ukm.Length != 8)
                {
                    throw ExceptionUtility.CryptographicException(Resources.Asn1ConsVioException, nameof(Ukm.Length), Ukm.Length);
                }
            }
        }
        /// <summary>
        /// Расшифровать информацию о ключе.
        /// </summary>
        public byte[] Encode()
        {
            byte[] data;

            var keyWrap = new Gost_28147_89_KeyWrap();

            try
            {
                keyWrap.EncryptedKey = new Gost_28147_89_EncryptedKey
                {
                    EncryptedKey = new Gost_28147_89_Key(EncryptedKey),
                    MacKey       = new Gost_28147_89_Mac(Mac)
                };

                keyWrap.EncryptedParams = new Gost_28147_89_KeyWrapParams
                {
                    EncryptionParamSet = Gost_28147_89_ParamSet.FromString(EncryptionParamSet),
                    Ukm = new Asn1OctetString(Ukm)
                };

                var asnEncoder = new Asn1BerEncodeBuffer();
                keyWrap.Encode(asnEncoder);
                data = asnEncoder.MsgCopy;
            }
            catch (Exception exception)
            {
                throw ExceptionUtility.CryptographicException(exception, Resources.Asn1DecodeError, nameof(Gost_28147_89_KeyWrap));
            }

            return(data);
        }
        /// <summary>
        /// Зашифровать идентификатор OID параметров шифрования.
        /// </summary>
        public static byte[] EncodeEncryptionParamSet(string encryptionParamSet)
        {
            if (encryptionParamSet == null)
            {
                throw ExceptionUtility.ArgumentNull(nameof(encryptionParamSet));
            }

            byte[] data;

            try
            {
                var parameters = new Gost_28147_89_BlobParams {
                    EncryptionParamSet = Gost_28147_89_ParamSet.FromString(encryptionParamSet)
                };

                var asnEncoder = new Asn1BerEncodeBuffer();
                parameters.Encode(asnEncoder);
                data = asnEncoder.MsgCopy;
            }
            catch (Exception exception)
            {
                throw ExceptionUtility.CryptographicException(exception, Resources.Asn1EncodeError, nameof(Gost_28147_89_BlobParams));
            }

            return(data);
        }
        public override void Decode(Asn1BerDecodeBuffer buffer, bool explicitTagging, int implicitLength)
        {
            var elemLength = explicitTagging ? MatchTag(buffer, Asn1Tag.Sequence) : implicitLength;

            EncryptionParamSet = null;
            ExtElement         = 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.Expired())
            {
                if (buffer.PeekTag().Equals(0, 0, ObjectIdentifierTypeCode))
                {
                    throw ExceptionUtility.CryptographicException(Resources.Asn1SeqOrderException);
                }

                ExtElement = new Asn1OpenExt();

                while (!context.Expired())
                {
                    ExtElement.DecodeComponent(buffer);
                }
            }
            else
            {
                ExtElement = null;
            }
        }