Exemplo n.º 1
0
        public override int Encode(Asn1BerEncodeBuffer buffer, bool explicitTagging)
        {
            int _aal = 0, len;

            switch (ChoiceId)
            {
            // encode null_
            case _NULL_:
                NULLParams null_ = (NULLParams)GetElement();
                len   = null_.Encode(buffer, true);
                _aal += len;
                break;

            // encode params_
            case _PARAMS_:
                GostR3410_2012_PublicKeyParameters params_ = (GostR3410_2012_PublicKeyParameters)GetElement();
                len   = params_.Encode(buffer, true);
                _aal += len;
                break;

            default:
                throw new Exception("Asn1InvalidChoiceOptionException()");
            }

            return(_aal);
        }
Exemplo n.º 2
0
        public override int Encode(Asn1BerEncodeBuffer buffer, bool explicitTagging)
        {
            int _aal = 0, len;

            // encode macKey

            if (!(macKey.Length == 4))
            {
                throw new Exception("Asn1ConsVioException");
            }

            len   = macKey.Encode(buffer, true);
            _aal += len;

            // encode maskKey

            if (maskKey != null)
            {
                len   = maskKey.Encode(buffer, false);
                _aal += len;
                _aal += buffer.EncodeTagAndLength(Asn1Tag.CTXT, Asn1Tag.PRIM, 0, len);
            }

            // encode encryptedKey

            len   = encryptedKey.Encode(buffer, true);
            _aal += len;

            if (explicitTagging)
            {
                _aal += buffer.EncodeTagAndLength(Asn1Tag.Sequence, _aal);
            }

            return(_aal);
        }
Exemplo n.º 3
0
        public byte[] Encode()
        {
            var asnEncoder   = new Asn1BerEncodeBuffer();
            var keyTransport = new GostR3410KeyTransport();

            try
            {
                keyTransport.SessionEncryptedKey = new Gost2814789EncryptedKey
                {
                    EncryptedKey = new Gost2814789Key(SessionEncryptedKey.EncryptedKey),
                    MacKey       = new Gost2814789Mac(SessionEncryptedKey.Mac)
                };

                keyTransport.TransportParameters = new GostR3410TransportParameters
                {
                    Ukm = new Asn1OctetString(SessionEncryptedKey.Ukm),
                    EncryptionParamSet = CreateEncryptionParamSet(SessionEncryptedKey.EncryptionParamSet),
                    EphemeralPublicKey = EncodePublicKey(TransportParameters)
                };

                keyTransport.Encode(asnEncoder);
            }
            catch (Exception exception)
            {
                throw ExceptionUtility.CryptographicException(exception, Resources.Asn1EncodeError, typeof(GostR3410KeyTransport).FullName);
            }

            return(asnEncoder.MsgCopy);
        }
        public override int Encode(Asn1BerEncodeBuffer buffer, bool explicitTagging)
        {
            var len = 0;

            if (Ukm.Length != 8)
            {
                throw ExceptionUtility.CryptographicException(Resources.Asn1ConsVioException, "Ukm.Length", Ukm.Length);
            }

            len += Ukm.Encode(buffer, true);

            if (EphemeralPublicKey != null)
            {
                var epkLength = EphemeralPublicKey.Encode(buffer, false);

                len += epkLength;
                len += buffer.EncodeTagAndLength(0x80, 0x20, EocTypeCode, epkLength);
            }

            len += EncryptionParamSet.Encode(buffer, true);

            if (explicitTagging)
            {
                len += buffer.EncodeTagAndLength(Asn1Tag.Sequence, len);
            }

            return len;
        }
Exemplo n.º 5
0
        private static SubjectPublicKeyInfo EncodePublicKey(GostKeyExchangeParameters 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 = new GostR34102001PublicKeyParameters
            {
                PublicKeyParamSet  = Asn1ObjectIdentifier.FromOidString(transportParameters.PublicKeyParamSet),
                DigestParamSet     = Asn1ObjectIdentifier.FromOidString(transportParameters.DigestParamSet),
                EncryptionParamSet = CreateEncryptionParamSet(transportParameters.EncryptionParamSet)
            };

            asnEncoder.Reset();
            publicKeyParams.Encode(asnEncoder);

            var publicKeyAlgOid = new Asn1ObjectIdentifier(GostR34102001Constants.IdGostR34102001);

            publicKeyInfo.Algorithm = new AlgorithmIdentifier(publicKeyAlgOid, new Asn1OpenType(asnEncoder.MsgCopy));

            return(publicKeyInfo);
        }
Exemplo n.º 6
0
        public override int Encode(Asn1BerEncodeBuffer buffer, bool explicitTagging)
        {
            int _aal = 0, len;

            // encode transportParameters

            if (transportParameters != null)
            {
                len   = transportParameters.Encode(buffer, false);
                _aal += len;
                _aal += buffer.EncodeTagAndLength(Asn1Tag.CTXT, Asn1Tag.CONS, 0, len);
            }

            // encode sessionEncryptedKey

            len   = sessionEncryptedKey.Encode(buffer, true);
            _aal += len;

            if (explicitTagging)
            {
                _aal += buffer.EncodeTagAndLength(Asn1Tag.Sequence, _aal);
            }

            return(_aal);
        }
Exemplo n.º 7
0
        public byte[] Encode()
        {
            var asnEncoder = new Asn1BerEncodeBuffer();
            var keyTransport = new GostR3410KeyTransport();

            try
            {
                keyTransport.SessionEncryptedKey = new Gost2814789EncryptedKey
                                                   {
                                                       EncryptedKey = new Gost2814789Key(SessionEncryptedKey.EncryptedKey),
                                                       MacKey = new Gost2814789Mac(SessionEncryptedKey.Mac)
                                                   };

                keyTransport.TransportParameters = new GostR3410TransportParameters
                                                   {
                                                       Ukm = new Asn1OctetString(SessionEncryptedKey.Ukm),
                                                       EncryptionParamSet = CreateEncryptionParamSet(SessionEncryptedKey.EncryptionParamSet),
                                                       EphemeralPublicKey = EncodePublicKey(TransportParameters)
                                                   };

                keyTransport.Encode(asnEncoder);
            }
            catch (Exception exception)
            {
                throw ExceptionUtility.CryptographicException(exception, Resources.Asn1EncodeError, typeof(GostR3410KeyTransport).FullName);
            }

            return asnEncoder.MsgCopy;
        }
Exemplo n.º 8
0
        /// <summary>
        /// Упаковка открытого ключа ГОСТ 34.10-2012 512 и его параметров в Asn1c структуру.
        /// </summary>
        ///
        /// <param name="pub">Открытый ключ.</param>
        ///
        /// <returns>Asn1c структура <c>SubjectPublicKeyInfo</c> открытого
        /// ключа.</returns>
        private static SubjectPublicKeyInfo PackPublicKeyInfo2012_512(
            Gost3410CspObject pub)
        {
            SubjectPublicKeyInfo spki      = new SubjectPublicKeyInfo();
            Asn1BerEncodeBuffer  buffer    = new Asn1BerEncodeBuffer();
            Asn1OctetString      publicKey = new Asn1OctetString(pub._publicKey);

            publicKey.Encode(buffer);
            byte[] octetString = buffer.MsgCopy;
            spki.subjectPublicKey = new Asn1BitString(
                octetString.Length * 8, octetString);
            GostR3410_2012_PublicKeyParameters par =
                new GostR3410_2012_PublicKeyParameters();

            par.publicKeyParamSet  = fromString(pub._publicKeyParamSet);
            par.digestParamSet     = fromString(pub._digestParamSet);
            par.encryptionParamSet = CreateGost28147_89_ParamSet(
                pub._encryptionParamSet);
            buffer.Reset();
            par.Encode(buffer);
            spki.algorithm = new AlgorithmIdentifier(
                fromString(GostConstants.OID_CP_GOST_R3410_12_512),
                new Asn1OpenType(buffer.MsgCopy));
            return(spki);
        }
        /// <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);
        }
Exemplo n.º 10
0
        public override int Encode(Asn1BerEncodeBuffer buffer, bool explicitTagging)
        {
            int _aal = 0, len;

            // encode ukm

            if (!(ukm.Length == 8))
            {
                throw new Exception("Asn1ConsVioException (ukm.Length, ukm.Length)");
            }

            len   = ukm.Encode(buffer, true);
            _aal += len;

            // encode ephemeralPublicKey

            if (ephemeralPublicKey != null)
            {
                len   = ephemeralPublicKey.Encode(buffer, false);
                _aal += len;
                _aal += buffer.EncodeTagAndLength(Asn1Tag.CTXT, Asn1Tag.CONS, 0, len);
            }

            // encode encryptionParamSet

            len   = encryptionParamSet.Encode(buffer, true);
            _aal += len;

            if (explicitTagging)
            {
                _aal += buffer.EncodeTagAndLength(Asn1Tag.Sequence, _aal);
            }

            return(_aal);
        }
Exemplo n.º 11
0
        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);
        }
Exemplo n.º 12
0
        public override int Encode(Asn1BerEncodeBuffer buffer, bool explicitTagging)
        {
            var len = 0;

            if (MacKey.Length != 4)
            {
                throw ExceptionUtility.CryptographicException(Resources.Asn1ConsVioException, "MacKey.Length", MacKey.Length);
            }

            var num2 = MacKey.Encode(buffer, true);

            len += num2;

            if (_maskKey != null)
            {
                num2 = _maskKey.Encode(buffer, false);
                len += num2;
                len += buffer.EncodeTagAndLength(0x80, 0, EocTypeCode, num2);
            }

            num2 = EncryptedKey.Encode(buffer, true);
            len += num2;

            if (explicitTagging)
            {
                len += buffer.EncodeTagAndLength(Asn1Tag.Sequence, len);
            }

            return(len);
        }
        public override int Encode(Asn1BerEncodeBuffer buffer, bool explicitTagging)
        {
            int _aal = 0, len;

            // encode encryptionParamSet

            if (encryptionParamSet != null)
            {
                len   = encryptionParamSet.Encode(buffer, true);
                _aal += len;
            }

            // encode digestParamSet

            len   = digestParamSet.Encode(buffer, true);
            _aal += len;

            // encode publicKeyParamSet

            len   = publicKeyParamSet.Encode(buffer, true);
            _aal += len;

            if (explicitTagging)
            {
                _aal += buffer.EncodeTagAndLength(Asn1Tag.Sequence, _aal);
            }

            return(_aal);
        }
Exemplo n.º 14
0
        public override int Encode(Asn1BerEncodeBuffer buffer, bool explicitTagging)
        {
            var len = 0;

            if (Ukm.Length != 8)
            {
                throw ExceptionUtility.CryptographicException(Resources.Asn1ConsVioException, "Ukm.Length", Ukm.Length);
            }

            len += Ukm.Encode(buffer, true);

            if (EphemeralPublicKey != null)
            {
                var epkLength = EphemeralPublicKey.Encode(buffer, false);

                len += epkLength;
                len += buffer.EncodeTagAndLength(0x80, 0x20, EocTypeCode, epkLength);
            }

            len += EncryptionParamSet.Encode(buffer, true);

            if (explicitTagging)
            {
                len += buffer.EncodeTagAndLength(Asn1Tag.Sequence, len);
            }

            return(len);
        }
Exemplo n.º 15
0
        public static byte[] EncodeEncryptionParamSet(string encryptionParamSet)
        {
            if (encryptionParamSet == null)
            {
                throw ExceptionUtility.ArgumentNull("encryptionParamSet");
            }

            byte[] data;

            try
            {
                var asnEncoder = new Asn1BerEncodeBuffer();
                var parameters = new Gost2814789BlobParameters {
                    EncryptionParamSet = CreateEncryptionParamSet(encryptionParamSet)
                };
                parameters.Encode(asnEncoder);

                data = asnEncoder.MsgCopy;
            }
            catch (Exception exception)
            {
                throw ExceptionUtility.CryptographicException(exception, Resources.Asn1EncodeError, typeof(Gost2814789BlobParameters).FullName);
            }

            return(data);
        }
Exemplo n.º 16
0
        public override int Encode(Asn1BerEncodeBuffer buffer, bool explicitTagging)
        {
            int _aal = 0, len;

            checkTC(false);
            // encode parameters

            if (parameters != null)
            {
                len   = parameters.Encode(buffer, true);
                _aal += len;
            }

            // encode algorithm

            len   = algorithm.Encode(buffer, true);
            _aal += len;

            if (explicitTagging)
            {
                _aal += buffer.EncodeTagAndLength(Asn1Tag.Sequence, _aal);
            }

            return(_aal);
        }
        /// <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);
        }
Exemplo n.º 18
0
        /// <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);
        }
Exemplo n.º 19
0
        public byte[] Encode()
        {
            byte[] data;

            var keyWrap = new GostR3410KeyWrap();

            try
            {
                keyWrap.EncryptedKey = new Gost2814789EncryptedKey
                {
                    EncryptedKey = new Gost2814789Key(EncryptedKey),
                    MacKey       = new Gost2814789Mac(Mac)
                };

                keyWrap.EncryptedParameters = new Gost2814789KeyWrapParameters
                {
                    EncryptionParamSet = CreateEncryptionParamSet(EncryptionParamSet),
                    Ukm = new Asn1OctetString(Ukm)
                };

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

            return(data);
        }
Exemplo n.º 20
0
        public override int Encode(Asn1BerEncodeBuffer buffer, bool explicitTagging)
        {
            int _aal = 0, len;

            // encode ukm

            if (ukm != null)
            {
                if (!(ukm.Length == 8))
                {
                    throw new Exception("Asn1ConsVioException");
                }

                len   = ukm.Encode(buffer, true);
                _aal += len;
            }

            // encode encryptionParamSet

            len   = encryptionParamSet.Encode(buffer, true);
            _aal += len;

            if (explicitTagging)
            {
                _aal += buffer.EncodeTagAndLength(Asn1Tag.Sequence, _aal);
            }

            return(_aal);
        }
Exemplo n.º 21
0
        public override int Encode(Asn1BerEncodeBuffer buffer, bool explicitTagging)
        {
            if (Value == null)
            {
                return _length;
            }

            return base.Encode(buffer, false);
        }
Exemplo n.º 22
0
        public override int Encode(Asn1BerEncodeBuffer buffer, bool explicitTagging)
        {
            var len = buffer.EncodeIntValue(Value);

            if (explicitTagging)
            {
                len += buffer.EncodeTagAndLength(Tag, len);
            }

            return(len);
        }
Exemplo n.º 23
0
        /// <summary>
        /// ASN.1 кодирование структуры транспорта.
        /// </summary>
        ///
        /// <param name="transport">ASN.1 транспорт.</param>
        ///
        /// <returns>Закодированная структура.</returns>
        ///
        /// <argnull name="transport" />
        /// <exception cref="CryptographicException">При ошибках
        /// кодирования исходной структуры.</exception>
        public static byte[] EncodeGostKeyTransport(
            GostKeyTransportObject transport)
        {
            if (transport == null)
            {
                throw new ArgumentNullException("transport");
            }

            GostR3410_KeyTransport asnTransport =
                new GostR3410_KeyTransport();
            Asn1BerEncodeBuffer buffer = new Asn1BerEncodeBuffer();

            try
            {
                asnTransport.sessionEncryptedKey =
                    new Gost28147_89_EncryptedKey();
                asnTransport.sessionEncryptedKey.encryptedKey =
                    new Gost28147_89_Key(
                        transport.sessionEncryptedKey_._encryptedKey);
                asnTransport.sessionEncryptedKey.macKey =
                    new Gost28147_89_MAC(transport.sessionEncryptedKey_._mac);
                asnTransport.transportParameters =
                    new GostR3410_TransportParameters();
                asnTransport.transportParameters.ukm =
                    new Asn1OctetString(transport.sessionEncryptedKey_._ukm);
                asnTransport.transportParameters.encryptionParamSet =
                    CreateGost28147_89_ParamSet(
                        transport.sessionEncryptedKey_._encryptionParamSet);

                if (transport.Transport.TransportParameters.DigestParamSet == "1.2.643.2.2.30.1")
                {
                    asnTransport.transportParameters.ephemeralPublicKey =
                        PackPublicKeyInfo2001(transport.transportParameters_);
                }
                else if (transport.Transport.TransportParameters.DigestParamSet == "1.2.643.7.1.1.2.2")
                {
                    asnTransport.transportParameters.ephemeralPublicKey =
                        PackPublicKeyInfo2012_256(transport.transportParameters_);
                }
                else if (transport.Transport.TransportParameters.DigestParamSet == "1.2.643.7.1.1.2.3")
                {
                    asnTransport.transportParameters.ephemeralPublicKey =
                        PackPublicKeyInfo2012_512(transport.transportParameters_);
                }
                asnTransport.Encode(buffer);
            }
            catch (Exception e)
            {
                throw new CryptographicException(
                          "Resources.Cryptography_ASN1_EncodeWithException " +
                          "GostR3410_KeyTransport", e);
            }
            return(buffer.MsgCopy);
        }
Exemplo n.º 24
0
        public override int Encode(Asn1BerEncodeBuffer buffer, bool explicitTagging)
        {
            var len = buffer.EncodeIntValue(Value);

            if (explicitTagging)
            {
                len += buffer.EncodeTagAndLength(Tag, len);
            }

            return len;
        }
        public override int Encode(Asn1BerEncodeBuffer buffer, bool explicitTagging)
        {
            switch (base.ChoiceId)
            {
                case Null:
                    return GetElement().Encode(buffer, true);
                case Params:
                    return GetElement().Encode(buffer, true);
            }

            throw ExceptionUtility.CryptographicException(Resources.Asn1InvalidChoiceOptionException);
        }
Exemplo n.º 26
0
        public override int Encode(Asn1BerEncodeBuffer buffer, bool explicitTagging)
        {
            var num = 0;

            for (var i = Value.Count - 1; i >= 0; i--)
            {
                var type = (Asn1OpenType)Value[i];
                num += type.Encode(buffer, false);
            }

            return num;
        }
Exemplo n.º 27
0
        public override int Encode(Asn1BerEncodeBuffer buffer, bool explicitTagging)
        {
            var len = 0;
            len += EncryptedParameters.Encode(buffer, true);
            len += EncryptedKey.Encode(buffer, true);

            if (explicitTagging)
            {
                len += buffer.EncodeTagAndLength(Asn1Tag.Sequence, len);
            }

            return len;
        }
Exemplo n.º 28
0
        public override void Encode(Asn1BerOutputStream outs, bool explicitTagging)
        {
            if (explicitTagging)
            {
                outs.EncodeTag(Tag);
            }

            var buffer = new Asn1BerEncodeBuffer();
            var len    = EncodeValue(buffer, _value, true);

            outs.EncodeLength(len);
            outs.Write(buffer.MsgCopy);
        }
Exemplo n.º 29
0
        public override int Encode(Asn1BerEncodeBuffer buffer, bool explicitTagging)
        {
            var len = 1;

            buffer.Copy(Value ? byte.MaxValue : ((byte)0));

            if (explicitTagging)
            {
                len += buffer.EncodeTagAndLength(Tag, len);
            }

            return(len);
        }
Exemplo n.º 30
0
        public override int Encode(Asn1BerEncodeBuffer buffer, bool explicitTagging)
        {
            var len = 1;

            buffer.Copy(Value ? byte.MaxValue : ((byte)0));

            if (explicitTagging)
            {
                len += buffer.EncodeTagAndLength(Tag, len);
            }

            return len;
        }
        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;
        }
        public override int Encode(Asn1BerEncodeBuffer buffer, bool explicitTagging)
        {
            switch (base.ChoiceId)
            {
            case Null:
                return(GetElement().Encode(buffer, true));

            case Params:
                return(GetElement().Encode(buffer, true));
            }

            throw ExceptionUtility.CryptographicException(Resources.Asn1InvalidChoiceOptionException);
        }
        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);
        }
Exemplo n.º 34
0
        public override int Encode(Asn1BerEncodeBuffer buffer, bool explicitTagging)
        {
            var len = 0;

            len += EncryptionParamSet.Encode(buffer, true);
            len += Iv.Encode(buffer, true);

            if (explicitTagging)
            {
                len += buffer.EncodeTagAndLength(Asn1Tag.Sequence, len);
            }

            return(len);
        }
Exemplo n.º 35
0
        /// <summary>
        /// Кодирование ASN.1 структуры зашифрованного ключа.
        /// </summary>
        ///
        /// <param name="wk">Зашифрованный ключ.</param>
        ///
        /// <returns>Закодированная структура.</returns>
        ///
        /// <argnull name="wk" />
        /// <exception cref="CryptographicException">При ошибках
        /// кодирования исходной структуры.</exception>
        public static byte[] EncodeXmlGostR3410WrappedKey(GostWrappedKeyObject wk)
        {
            GostR3410_KeyWrap   wrapped = new GostR3410_KeyWrap();
            Asn1BerEncodeBuffer buffer  = new Asn1BerEncodeBuffer();

            wrapped.encryptedKey              = new Gost28147_89_EncryptedKey();
            wrapped.encryptedKey.macKey       = new Gost28147_89_MAC(wk._mac);
            wrapped.encryptedKey.encryptedKey = new Gost28147_89_Key(wk._encryptedKey);
            wrapped.encryptedParameters       = new Gost28147_89_KeyWrapParameters();
            wrapped.encryptedParameters.ukm   = new Asn1OctetString(wk._ukm);
            wrapped.encryptedParameters.encryptionParamSet =
                CreateGost28147_89_ParamSet(wk._encryptionParamSet);
            wrapped.Encode(buffer);
            return(buffer.MsgCopy);
        }
Exemplo n.º 36
0
        public override int Encode(Asn1BerEncodeBuffer buffer, bool explicitTagging)
        {
            if (Length != 0x40)
            {
                throw new Exception("Asn1ConsVioException");
            }
            var len = base.Encode(buffer, false);

            if (explicitTagging)
            {
                len += buffer.EncodeTagAndLength(Tag, len);
            }

            return(len);
        }
Exemplo n.º 37
0
		public override int Encode(Asn1BerEncodeBuffer buffer, bool explicitTagging)
		{
			if (Length != 32)
			{
				throw ExceptionUtility.CryptographicException(Resources.Asn1ConsVioException, "Length", Length);
			}

			var len = base.Encode(buffer, false);

			if (explicitTagging)
			{
				len += buffer.EncodeTagAndLength(Tag, len);
			}

			return len;
		}
Exemplo n.º 38
0
        /// <summary>
        /// ASN.1 кодирование структуры параметров шифрования.
        /// </summary>
        ///
        /// <param name="parameters">OID параметров шифрования.</param>
        ///
        /// <returns>Закодированная ASN.1 структура.</returns>
        ///
        /// <argnull name="parameters" />
        /// <exception cref="CryptographicException">При ошибках
        /// кодирования структуры.</exception>
        public static byte[] EncodeGost28147_89_BlobParameters(
            string parameters)
        {
            if (parameters == null)
            {
                throw new ArgumentNullException("parameters");
            }
            Asn1BerEncodeBuffer         buffer    = new Asn1BerEncodeBuffer();
            Gost28147_89_BlobParameters asnParams =
                new Gost28147_89_BlobParameters();

            asnParams.encryptionParamSet =
                CreateGost28147_89_ParamSet(parameters);
            asnParams.Encode(buffer);
            return(buffer.MsgCopy);
        }
Exemplo n.º 39
0
        public override int Encode(Asn1BerEncodeBuffer buffer, bool explicitTagging)
        {
            if ((Length < 1) || (Length > 4))
            {
                throw ExceptionUtility.CryptographicException(Resources.Asn1ConsVioException, nameof(Length), Length);
            }

            var len = base.Encode(buffer, false);

            if (explicitTagging)
            {
                len += buffer.EncodeTagAndLength(Tag, len);
            }

            return(len);
        }
Exemplo n.º 40
0
        public override int Encode(Asn1BerEncodeBuffer buffer, bool explicitTagging)
        {
            if (!(Length == 8))
            {
                throw new Exception("Asn1ConsVioException (Length, Length)");
            }

            int _aal = base.Encode(buffer, false);

            if (explicitTagging)
            {
                _aal += buffer.EncodeTagAndLength(Tag, _aal);
            }

            return(_aal);
        }
        public override int Encode(Asn1BerEncodeBuffer buffer, bool explicitTagging)
        {
            var len = 0;

            var num2 = _encryptionParamSet.Encode(buffer, true);
            len += num2;

            num2 = _iv.Encode(buffer, true);
            len += num2;

            if (explicitTagging)
            {
                len += buffer.EncodeTagAndLength(Asn1Tag.Sequence, len);
            }

            return len;
        }
        public override int Encode(Asn1BerEncodeBuffer buffer, bool explicitTagging)
        {
            var len = 0;

            if (EncryptionParamSet != null)
            {
                len += EncryptionParamSet.Encode(buffer, true);
            }

            len += DigestParamSet.Encode(buffer, true);
            len += PublicKeyParamSet.Encode(buffer, true);

            if (explicitTagging)
            {
                len += buffer.EncodeTagAndLength(Asn1Tag.Sequence, len);
            }

            return len;
        }
        public override int Encode(Asn1BerEncodeBuffer buffer, bool explicitTagging)
        {
            var len = 0;
            CheckTc(false);

            if (Parameters != null)
            {
                len += Parameters.Encode(buffer, true);
            }

            len += Algorithm.Encode(buffer, true);

            if (explicitTagging)
            {
                len += buffer.EncodeTagAndLength(Asn1Tag.Sequence, len);
            }

            return len;
        }
Exemplo n.º 44
0
        public override int Encode(Asn1BerEncodeBuffer buffer, bool explicitTagging)
        {
            if (Value.Length < 1)
            {
                throw ExceptionUtility.CryptographicException(Resources.Asn1InvalidObjectIdException);
            }

            var len = 0;

            for (var i = Value.Length - 1; i >= 0; i--)
            {
                len += buffer.EncodeIdentifier(Value[i]);
            }

            if (explicitTagging)
            {
                len += buffer.EncodeTagAndLength(Tag, len);
            }

            return len;
        }
        public override int Encode(Asn1BerEncodeBuffer buffer, bool explicitTagging)
        {
            int num2;
            var len = 0;

            if (_extElem1 != null)
            {
                num2 = _extElem1.Encode(buffer, false);
                len += num2;
            }

            num2 = EncryptionParamSet.Encode(buffer, true);
            len += num2;

            if (explicitTagging)
            {
                len += buffer.EncodeTagAndLength(Asn1Tag.Sequence, len);
            }

            return len;
        }
        public override int Encode(Asn1BerEncodeBuffer buffer, bool explicitTagging)
        {
            var len = 0;

            if (TransportParameters != null)
            {
                var tpLength = TransportParameters.Encode(buffer, false);

                len += tpLength;
                len += buffer.EncodeTagAndLength(0x80, 0x20, EocTypeCode, tpLength);
            }

            len += SessionEncryptedKey.Encode(buffer, true);

            if (explicitTagging)
            {
                len += buffer.EncodeTagAndLength(Asn1Tag.Sequence, len);
            }

            return len;
        }
Exemplo n.º 47
0
        public override int Encode(Asn1BerEncodeBuffer buffer, bool explicitTagging)
        {
            var length = Value.Length;

            for (var i = length - 1; i >= 0; --i)
            {
                var num3 = Value[i];
                var num = num3 % 0x100;
                var num2 = num3 / 0x100;

                buffer.Copy((byte)num);
                buffer.Copy((byte)num2);
            }

            length *= 2;

            if (explicitTagging)
            {
                length += buffer.EncodeTagAndLength(Tag, length);
            }

            return length;
        }
        public override int Encode(Asn1BerEncodeBuffer buffer, bool explicitTagging)
        {
            var len = 0;

            if (Ukm != null)
            {
                if (Ukm.Length != 8)
                {
                    throw ExceptionUtility.CryptographicException(Resources.Asn1ConsVioException, "Ukm.Length", Ukm.Length);
                }

                len += Ukm.Encode(buffer, true);
            }

            len += EncryptionParamSet.Encode(buffer, true);

            if (explicitTagging)
            {
                len += buffer.EncodeTagAndLength(Asn1Tag.Sequence, len);
            }

            return len;
        }
Exemplo n.º 49
0
        public override int Encode(Asn1BerEncodeBuffer buffer, bool explicitTagging)
        {
            var len = 0;

            try
            {
                var bytes = Encoding.UTF8.GetBytes(Value);
                len = bytes.Length;
                buffer.Copy(bytes);
            }
            catch (IOException exception)
            {
                Console.Out.WriteLine("This JVM does not support UTF-8 encoding");
                Asn1Util.WriteStackTrace(exception, Console.Error);
            }

            if (explicitTagging)
            {
                len += buffer.EncodeTagAndLength(Tag, len);
            }

            return len;
        }
        public static byte[] EncodeEncryptionParamSet(string encryptionParamSet)
        {
            if (encryptionParamSet == null)
            {
                throw ExceptionUtility.ArgumentNull("encryptionParamSet");
            }

            byte[] data;

            try
            {
                var asnEncoder = new Asn1BerEncodeBuffer();
                var parameters = new Gost2814789BlobParameters { EncryptionParamSet = CreateEncryptionParamSet(encryptionParamSet) };
                parameters.Encode(asnEncoder);

                data = asnEncoder.MsgCopy;
            }
            catch (Exception exception)
            {
                throw ExceptionUtility.CryptographicException(exception, Resources.Asn1EncodeError, typeof(Gost2814789BlobParameters).FullName);
            }

            return data;
        }
Exemplo n.º 51
0
		public override int Encode(Asn1BerEncodeBuffer buffer, bool explicitTagging)
		{
			return Encode(buffer, explicitTagging, Tag);
		}
Exemplo n.º 52
0
 public virtual int Encode(Asn1BerEncodeBuffer buffer)
 {
     return Encode(buffer, true);
 }
Exemplo n.º 53
0
 public virtual int Encode(Asn1BerEncodeBuffer buffer, bool explicitTagging)
 {
     return 0;
 }
        public override int Encode(Asn1BerEncodeBuffer buffer, bool explicitTagging)
        {
            var length = _value.Length;

            for (var i = length - 1; i >= 0; i--)
            {
                var num3 = _value[i];

                for (var j = 0; j < 4; j++)
                {
                    var num = num3 % 0x100;
                    num3 /= 0x100;
                    buffer.Copy((byte)num);
                }
            }

            length *= 4;

            if (explicitTagging)
            {
                length += buffer.EncodeTagAndLength(Tag, length);
            }

            return length;
        }
        public byte[] EncodeParameters()
        {
            byte[] data;

            var publicKeyParameters = new GostR34102001PublicKeyParameters();

            try
            {
                publicKeyParameters.DigestParamSet = Asn1ObjectIdentifier.FromOidString(DigestParamSet);
                publicKeyParameters.PublicKeyParamSet = Asn1ObjectIdentifier.FromOidString(PublicKeyParamSet);
                publicKeyParameters.EncryptionParamSet = CreateEncryptionParamSet(EncryptionParamSet);

                var asnEncoder = new Asn1BerEncodeBuffer();
                publicKeyParameters.Encode(asnEncoder);
                data = asnEncoder.MsgCopy;
            }
            catch (Exception exception)
            {
                throw ExceptionUtility.CryptographicException(exception, Resources.Asn1EncodeError, typeof(GostR34102001PublicKeyParameters).FullName);
            }

            return data;
        }
Exemplo n.º 56
0
        public override int Encode(Asn1BerEncodeBuffer buffer, bool explicitTagging)
        {
            int _aal = 0, len;

             // encode vals

             len = vals.Encode (buffer, true);
             _aal += len;

             // encode type

             len = type.Encode (buffer, true);
             _aal += len;

             if (explicitTagging) {
            _aal += buffer.EncodeTagAndLength (Asn1Tag.SEQUENCE, _aal);
             }

             return (_aal);
        }
Exemplo n.º 57
0
        public override int Encode(Asn1BerEncodeBuffer buffer, bool explicitTagging)
        {
            var len = 0;

            if (double.IsNegativeInfinity(Value))
            {
                len = buffer.EncodeIntValue(MinusInfinity);
            }
            else if (double.IsPositiveInfinity(Value))
            {
                len = buffer.EncodeIntValue(PlusInfinity);
            }

            else if (Value != 0.0)
            {
                var num2 = BitConverter.DoubleToInt64Bits(Value);
                var num3 = ((num2 >> RealIso6093Mask) == 0L) ? 1 : -1;
                var num4 = ((int)((num2 >> 0x34) & 0x7ffL)) - 0x433;
                var w = (num4 == 0) ? ((num2 & 0xfffffffffffffL) << 1) : ((num2 & 0xfffffffffffffL) | 0x10000000000000L);

                if (w != 0L)
                {
                    var bits = TrailingZerosCnt(w);
                    w = Asn1Util.UrShift(w, bits);
                    num4 += bits;
                }

                len += buffer.EncodeIntValue(w);

                var num7 = buffer.EncodeIntValue(num4);
                len += num7;

                var num8 = RealBinary;

                if (num3 == -1)
                {
                    num8 |= PlusInfinity;
                }

                switch (num7)
                {
                    case RealExplen2:
                        break;

                    case RealExplen3:
                        num8 |= 1;
                        break;

                    case RealExplenLong:
                        num8 |= 2;
                        break;

                    default:
                        num8 |= 3;
                        len += buffer.EncodeIntValue(num7);
                        break;
                }

                buffer.Copy((byte)num8);
                len++;
            }

            if (explicitTagging)
            {
                len += buffer.EncodeTagAndLength(Tag, len);
            }

            return len;
        }
Exemplo n.º 58
0
        protected override int Encode(Asn1BerEncodeBuffer buffer, bool explicitTagging, Asn1Tag tag)
        {
            SafeParseString();

            var flag = buffer is Asn1DerEncodeBuffer;

            if (DerRules != flag)
            {
                DerRules = flag;

                if (!CompileString())
                {
                    throw ExceptionUtility.CryptographicException(Resources.Asn1TimeStringCouldNotBeGenerated);
                }
            }

            return base.Encode(buffer, explicitTagging, tag);
        }
Exemplo n.º 59
0
        protected virtual int Encode(Asn1BerEncodeBuffer buffer, bool explicitTagging, Asn1Tag tag)
        {
            var length = Value.Length;
            buffer.Copy(Value);

            if (explicitTagging)
            {
                length += buffer.EncodeTagAndLength(tag, length);
            }

            return length;
        }
        public byte[] Encode()
        {
            byte[] data;

            var keyWrap = new GostR3410KeyWrap();

            try
            {
                keyWrap.EncryptedKey = new Gost2814789EncryptedKey
                                       {
                                           EncryptedKey = new Gost2814789Key(EncryptedKey),
                                           MacKey = new Gost2814789Mac(Mac)
                                       };

                keyWrap.EncryptedParameters = new Gost2814789KeyWrapParameters
                                              {
                                                  EncryptionParamSet = CreateEncryptionParamSet(EncryptionParamSet),
                                                  Ukm = new Asn1OctetString(Ukm)
                                              };

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

            return data;
        }