Пример #1
0
        /// <summary>
        /// Упаковка открытого ключа ГОСТ 34.10-2001 и его параметров в Asn1c структуру.
        /// </summary>
        ///
        /// <param name="pub">Открытый ключ.</param>
        ///
        /// <returns>Asn1c структура <c>SubjectPublicKeyInfo</c> открытого
        /// ключа.</returns>
        ///
        private static SubjectPublicKeyInfo PackPublicKeyInfo2001(
            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_2001_PublicKeyParameters par =
                new GostR3410_2001_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_R3410EL),
                new Asn1OpenType(buffer.MsgCopy));
            return(spki);
        }
Пример #2
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_2001_PublicKeyParameters params_ = (GostR3410_2001_PublicKeyParameters)GetElement();
                len   = params_.Encode(buffer, true);
                _aal += len;
                break;

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

            return(_aal);
        }
Пример #3
0
        public override void Decode
            (Asn1BerDecodeBuffer buffer, bool explicitTagging, int implicitLength)
        {
            int llen = implicitLength;

            // decode CHOICE

            Asn1Tag tag = new Asn1Tag();

            buffer.Mark();
            int len = buffer.DecodeTagAndLength(tag);

            if (tag.Equals(Asn1Tag.Universal, Asn1Tag.PRIM, 5))
            {
                buffer.Reset();
                NULLParams null_ = new NULLParams();
                SetElement(_NULL_, null_);
                Element.Decode(buffer, true, len);
            }
            else if (tag.Equals(Asn1Tag.Universal, Asn1Tag.CONS, 16))
            {
                buffer.Reset();
                GostR3410_2001_PublicKeyParameters params_ = new GostR3410_2001_PublicKeyParameters();
                SetElement(_PARAMS_, params_);
                Element.Decode(buffer, true, len);
            }
            else
            {
                throw new Exception("Asn1InvalidChoiceOptionException (buffer, tag)");
            }
        }
Пример #4
0
        public void DecodeParameters(byte[] data)
        {
            if (data == null)
            {
                throw new ArgumentNullException(nameof(data));
            }

            var asnDecoder          = new Asn1BerDecodeBuffer(data);
            var publicKeyParameters = new GostR3410_2001_PublicKeyParameters();

            publicKeyParameters.Decode(asnDecoder);

            DigestParamSet     = Asn1ObjectIdentifier.ToOidString(publicKeyParameters.DigestParamSet);
            PublicKeyParamSet  = Asn1ObjectIdentifier.ToOidString(publicKeyParameters.PublicKeyParamSet);
            EncryptionParamSet = Asn1ObjectIdentifier.ToOidString(publicKeyParameters.EncryptionParamSet);
        }
Пример #5
0
        public byte[] EncodeParameters()
        {
            byte[] data;

            var publicKeyParameters = new GostR3410_2001_PublicKeyParameters();

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

            var asnEncoder = new Asn1BerEncodeBuffer();

            publicKeyParameters.Encode(asnEncoder);
            data = asnEncoder.MsgCopy;

            return(data);
        }
Пример #6
0
        /// <summary>
        /// Разбор декодированной ASN1c структуры ГОСТ 34.10-2001 <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 UnpackPublicKeyInfo2001(
            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_2001_PublicKeyParameters publicKeyParameters =
                choice.GetElement() as GostR3410_2001_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);
        }
Пример #7
0
 public void Set_params_(GostR3410_2001_PublicKeyParameters value)
 {
     SetElement(_PARAMS_, value);
 }