示例#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);
                }
            }
        }
        public override void Decode
            (Asn1BerDecodeBuffer buffer, bool explicitTagging, int implicitLength)
        {
            int llen = (explicitTagging) ?
                       MatchTag(buffer, Asn1Tag.Sequence) : implicitLength;

            Init();

            // decode SEQUENCE

            Asn1BerDecodeContext context =
                new Asn1BerDecodeContext(buffer, llen);

            IntHolder elemLen = new IntHolder();

            // decode encryptionParamSet

            if (context.MatchElemTag(Asn1Tag.Universal, Asn1Tag.PRIM, 6, elemLen, false))
            {
                encryptionParamSet = new Gost28147_89_ParamSet();
                encryptionParamSet.Decode(buffer, true, elemLen.Value);
            }
            else
            {
                throw new Exception("Asn1MissingRequiredException (buffer)");
            }

            // decode ephemeralPublicKey

            if (context.MatchElemTag(Asn1Tag.CTXT, Asn1Tag.CONS, 0, elemLen, true))
            {
                ephemeralPublicKey = new SubjectPublicKeyInfo();
                ephemeralPublicKey.Decode(buffer, false, elemLen.Value);
            }

            // decode ukm

            if (context.MatchElemTag(Asn1Tag.Universal, Asn1Tag.PRIM, 4, elemLen, false))
            {
                ukm = new Asn1OctetString();
                ukm.Decode(buffer, true, elemLen.Value);
                if (!(ukm.Length == 8))
                {
                    throw new Exception("Asn1ConsVioException (ukm.Length, ukm.Length)");
                }
            }
            else
            {
                throw new Exception("Asn1MissingRequiredException (buffer)");
            }
        }
示例#3
0
        /// <summary>
        /// Разбор декодированной ASN1c структуры ГОСТ 34.10-2012 <c>SubjectPublicKeyInfo</c>.
        /// </summary>
        ///
        /// <param name="spki">ASN1c структура <c>SubjectPublicKeyInfo</c>.
        /// </param>
        ///
        /// <returns>Параметры открытого ключа.</returns>
        /// <argnull name="spki" />
        /// <exception cref="ArgumentException">Если вложенная структура
        /// не приводится к <c>GostR3410_2001_PublicKeyParameters</c>
        /// </exception>
        private static Gost3410CspObject UnpackPublicKeyInfo2012(
            SubjectPublicKeyInfo spki)
        {
            if (spki == null)
            {
                throw new ArgumentNullException("spki");
            }
            Asn1Choice choice = spki.algorithm.parameters as Asn1Choice;

            if (choice == null)
            {
                throw new ArgumentException(
                          "spki.algorithm.parameters");
            }
            GostR3410_2012_PublicKeyParameters publicKeyParameters =
                choice.GetElement() as GostR3410_2012_PublicKeyParameters;

            if (publicKeyParameters == null)
            {
                throw new ArgumentException(
                          "spki.algorithm.parameters.element");
            }
            byte[] bitString              = spki.subjectPublicKey.Value;
            Asn1BerDecodeBuffer buffer    = new Asn1BerDecodeBuffer(bitString);
            Asn1OctetString     publicKey = new Asn1OctetString();

            publicKey.Decode(buffer);
            Gost3410CspObject ret = new Gost3410CspObject();

            ret._publicKeyParamSet = toString(
                publicKeyParameters.publicKeyParamSet);
            ret._digestParamSet = toString(
                publicKeyParameters.digestParamSet);
            ret._encryptionParamSet = toString(
                publicKeyParameters.encryptionParamSet);
            ret._publicKey  = publicKey.Value;
            ret._privateKey = null;
            return(ret);
        }
        private static GostKeyExchangeParameters DecodePublicKey(GostR3410KeyTransport keyTransport)
        {
            var publicKeyInfo   = keyTransport.TransportParameters.EphemeralPublicKey;
            var publicKeyAlgOid = Asn1ObjectIdentifier.ToOidString(publicKeyInfo.Algorithm.Algorithm);

            if (!publicKeyAlgOid.Equals(GostR34102001Constants.IdGostR34102001String))
            {
                throw ExceptionUtility.CryptographicException(Resources.Asn1DecodeAlg, publicKeyAlgOid);
            }

            var choice = publicKeyInfo.Algorithm.Parameters as Asn1Choice;

            if (choice == null)
            {
                throw ExceptionUtility.CryptographicException(Resources.Asn1DecodeAlgorithmParameters);
            }

            var publicKeyParams = choice.GetElement() as GostR34102001PublicKeyParameters;

            if (publicKeyParams == null)
            {
                throw ExceptionUtility.CryptographicException(Resources.Asn1DecodeAlgorithmParameters);
            }

            var asnDecoder = new Asn1BerDecodeBuffer(publicKeyInfo.SubjectPublicKey.Value);
            var publicKey  = new Asn1OctetString();

            publicKey.Decode(asnDecoder);

            return(new GostKeyExchangeParameters
            {
                DigestParamSet = Asn1ObjectIdentifier.ToOidString(publicKeyParams.DigestParamSet),
                PublicKeyParamSet = Asn1ObjectIdentifier.ToOidString(publicKeyParams.PublicKeyParamSet),
                EncryptionParamSet = Asn1ObjectIdentifier.ToOidString(publicKeyParams.EncryptionParamSet),
                PublicKey = publicKey.Value,
                PrivateKey = null
            });
        }
示例#5
0
        public override void Decode(Asn1BerDecodeBuffer buffer, bool explicitTagging, int implicitLength)
        {
            var elemLength = explicitTagging ? MatchTag(buffer, Asn1Tag.Sequence) : implicitLength;

            Init();

            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 Gost2814789ParamSet();
            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, "Ukm.Length", Ukm.Length);
            }
        }
示例#6
0
        private void DecodePublicKey(Gost_R3410_KeyTransport keyTransport)
        {
            var publicKeyInfo   = keyTransport.TransportParams.EphemeralPublicKey;
            var publicKeyAlgOid = publicKeyInfo.Algorithm.Algorithm.Oid.Value;

            if (!publicKeyAlgOid.Equals(KeyAlgorithm.Value))
            {
                throw ExceptionUtility.CryptographicException(Resources.Asn1DecodeAlg, publicKeyAlgOid);
            }

            var choice = publicKeyInfo.Algorithm.Parameters as Asn1Choice;

            if (choice == null)
            {
                throw ExceptionUtility.CryptographicException(Resources.Asn1DecodeAlgorithmParameters);
            }

            var publicKeyParams = choice.GetElement() as Gost_R3410_PublicKeyParams;

            if (publicKeyParams == null)
            {
                throw ExceptionUtility.CryptographicException(Resources.Asn1DecodeAlgorithmParameters);
            }

            var asnDecoder = new Asn1BerDecodeBuffer(publicKeyInfo.SubjectPublicKey.Value);
            var publicKey  = new Asn1OctetString();

            publicKey.Decode(asnDecoder);

            TransportParameters = CreateKeyExchangeParams();
            TransportParameters.DigestParamSet     = publicKeyParams.DigestParamSet.Oid.Value;
            TransportParameters.PublicKeyParamSet  = publicKeyParams.PublicKeyParamSet.Oid.Value;
            TransportParameters.EncryptionParamSet = publicKeyParams.EncryptionParamSet?.Oid.Value;
            TransportParameters.PublicKey          = publicKey.Value;
            TransportParameters.PrivateKey         = null;
        }