static public byte[] GetIVAsn1(byte[] data)
        {
            var asn = BERelement.DecodePacket(data);

            byte[] iv = asn.Items[1].Value;
            return(iv);
        }
        static public byte[] GetCertAsn1(byte[] data)
        {
            var asn = BERelement.DecodePacket(data);

            byte[] cert = asn.Items[0].Value;
            return(cert);
        }
        static public byte[] GetSymAsn1(byte[] data)
        {
            var asn = BERelement.DecodePacket(data);

            byte[] symKey = asn.Items[0].Value;
            return(symKey);
        }
Ejemplo n.º 4
0
 private void Cipher_CheckedChanged(object sender, EventArgs e)
 {
     if (Cipher.Checked)
     {
         var asn1Cmd = Asn1Formatter.SetCommandAsn1((int)Cmd.certs);
         handler.Send(asn1Cmd);
         data = handler.Recieve();
         BERelement certsNames = BERelement.DecodePacket(data);
         CerificatesBox.DataSource = null;
         CerificatesBox.Items.Clear();
         foreach (var cert in certsNames.Items)
         {
             CerificatesBox.Items.Add(Encoding.ASCII.GetString(cert.Value));
         }
         CerificatesBox.SelectedIndex = 0;
     }
 }
Ejemplo n.º 5
0
        private void GetCertsFromServer(object sender, EventArgs e)
        {
            var asn1Cmd = Asn1Formatter.SetCommandAsn1((int)Cmd.certs);

            int.TryParse(textBox3.Text, out int port);
            handler = new ClientSocket(textBox2.Text, port);
            handler.Init();
            handler.Send(asn1Cmd);
            data = handler.Recieve();
            BERelement certsNames = BERelement.DecodePacket(data);

            CerificatesBox.DataSource = null;
            CerificatesBox.Items.Clear();
            foreach (var cert in certsNames.Items)
            {
                CerificatesBox.Items.Add(Encoding.ASCII.GetString(cert.Value));
            }
            CerificatesBox.SelectedIndex = 0;
        }
Ejemplo n.º 6
0
        static void Main(string[] args)
        {
            try
            {
                socket.Init();
                while (true)
                {
                    byte[]     data = socket.Recieve();
                    BERelement asn  = BERelement.DecodePacket(data);
                    int        operation;
                    if (asn.Items[0].Value.Length > 1)
                    {
                        operation = BitConverter.ToInt32(asn.Items[0].Value, 0);
                    }
                    else
                    {
                        operation = asn.Items[0].Value[0];
                    }

                    switch (operation)
                    {
                    case (int)Cmd.certs:
                        SendCertificatesList();
                        break;

                    case (int)Cmd.cipher:
                        SendCertificatesList();
                        data = socket.Recieve();
                        asn  = BERelement.DecodePacket(data);
                        string certName = Encoding.ASCII.GetString(asn.Items[0].Value);
                        foreach (var cert in crypter.Certificates)
                        {
                            if (cert.FriendlyName == certName)
                            {
                                crypter.currentCertificate = cert;
                                SendPublicKey(cert);
                                break;
                            }
                        }

                        data = socket.Recieve();
                        crypter.SetSymmetrKey(Asn1Formatter.GetSymAsn1(data));
                        crypter.IV = Asn1Formatter.GetIVAsn1(data);
                        byte[] asn1Established = Asn1Formatter.SetCertASN1(Encoding.ASCII.GetBytes("ESTABLISHED"));
                        socket.Send(asn1Established);
                        data = socket.Recieve();
                        String text = Encoding.ASCII.GetString(crypter.Decrypt(data));
                        Console.WriteLine("Recieved data from client: " + text);
                        break;

                    case (int)Cmd.sign:
                        asn1Established = Asn1Formatter.SetCertASN1(Encoding.ASCII.GetBytes("ESTABLISHED"));
                        socket.Send(asn1Established);
                        data = socket.Recieve();
                        asn  = BERelement.DecodePacket(data);
                        try
                        {
                            if (asn.Items[0].Value.Length > 1)
                            {
                                operation = BitConverter.ToInt32(asn.Items[0].Value, 0);
                            }
                            else
                            {
                                operation = asn.Items[0].Value[0];
                            }
                            if (operation == (int)Cmd.error)
                            {
                                Console.WriteLine("Error!");
                                continue;
                            }
                        }
                        catch (NotSupportedException)
                        {
                            Console.WriteLine("Signed message came!");
                        }

                        BERelement mSeq = BERelement.DecodePacket(data);
                        BERelement sSeq = mSeq.Items[0];
                        BERelement fSeq = mSeq.Items[1];

                        var signature = sSeq.Items[0].Value;
                        var certS     = new X509Certificate2(sSeq.Items[1].Value);
                        var time      = DateTime.FromBinary(BitConverter.ToInt64(sSeq.Items[2].Value, 0));
                        var sData     = fSeq.Items[0].Value;

                        Gost3410_2012_256CryptoServiceProvider sGost = (Gost3410_2012_256CryptoServiceProvider)certS.PublicKey.Key;
                        Gost3411CryptoServiceProvider          hGost = new Gost3411CryptoServiceProvider();
                        bool correct = sGost.VerifySignature(hGost.ComputeHash(sData), signature);

                        Console.WriteLine($"Friendly name: {certS.FriendlyName}");
                        Console.WriteLine(certS + "\n");
                        Console.WriteLine("Signature time...");
                        Console.WriteLine(time + "\n");
                        Console.WriteLine("Correct signature?...");
                        Console.WriteLine(correct + "\n");
                        Console.WriteLine("Data:");
                        Console.WriteLine(Encoding.ASCII.GetString(sData) + "\n");
                        break;

                    default:
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
Ejemplo n.º 7
0
        private void check_response()
        {
            TcpClient client = null;

            try
            {
                TcpListener listener = new TcpListener(IPAddress.Parse("127.0.0.1"), 9595);
                listener.Start();

                Byte[] bytes = new Byte[256];
                String data  = null;

                while (true)
                {
                    client = listener.AcceptTcpClient();
                    NetworkStream ns         = client.GetStream();
                    Byte[]        mode_bytes = new Byte[1];
                    int           mode       = ns.Read(mode_bytes, 0, mode_bytes.Length);

                    if (mode_bytes[0] == 0x01)
                    {
                        richTextBox1.Text += "Received encrypted msg. Try to decrypt: \n";
                        int    bytes_read = ns.Read(buffer, 0, buffer.Length);
                        byte[] message    = new byte[bytes_read];
                        Array.Copy(buffer, message, bytes_read);
                        BERelement mSeq = BERelement.DecodePacket(message);
                        BERelement sSeq = null;

                        sSeq = mSeq.Items[0];
                        var cert_name         = sSeq.Items[0].Value;
                        var wrapped_key2      = sSeq.Items[1].Value;
                        var iv2               = sSeq.Items[2].Value;
                        var public_key_bytes  = sSeq.Items[3].Value;
                        var cipher_text_bytes = sSeq.Items[4].Value;

                        MemoryStream ms = new MemoryStream(public_key_bytes);

                        BinaryFormatter           bf         = new BinaryFormatter();
                        Gost3410Parameters        key_params = (Gost3410Parameters)bf.Deserialize(ms);
                        GostSharedSecretAlgorithm agree_key  = csp.CreateAgree(key_params);
                        SymmetricAlgorithm        gost       = agree_key.Unwrap(wrapped_key2, GostKeyWrapMethod.CryptoProKeyWrap);
                        gost.IV = iv2;
                        MemoryStream memoryStream = new MemoryStream();
                        CryptoStream cryptoStream = new CryptoStream(memoryStream,
                                                                     gost.CreateDecryptor(), CryptoStreamMode.Write);
                        int bytesRead = cipher_text_bytes.Length;
                        cryptoStream.Write(cipher_text_bytes, 0, bytesRead);
                        cryptoStream.FlushFinalBlock();
                        byte[] plain_text_bytes = memoryStream.ToArray();
                        richTextBox1.Text += Encoding.ASCII.GetString(plain_text_bytes, 0, plain_text_bytes.Length) + "\n";
                    }
                    if (mode_bytes[0] == 0x02)
                    {
                        richTextBox1.Text += "Received sign. Try to check it: \n";
                        Gost3411CryptoServiceProvider hash = new Gost3411CryptoServiceProvider();

                        int    bytes_read = ns.Read(buffer, 0, buffer.Length);
                        byte[] message    = new byte[bytes_read];
                        Array.Copy(buffer, message, bytes_read);

                        BERelement mSeq = BERelement.DecodePacket(message);
                        BERelement sSeq = null;

                        sSeq = mSeq.Items[0];
                        var cert_name = sSeq.Items[0].Value;
                        var signed2   = sSeq.Items[1].Value;
                        var msg       = sSeq.Items[2].Value;

                        int len = signed2.Length;

                        bool test = csp.VerifyData(msg, hash, signed2);
                        if (test)
                        {
                            richTextBox1.Text += "Подпись корректна.\r\n";
                        }
                        else
                        {
                            richTextBox1.Text += "Подпись некорректна.\r\n";
                        }

                        /*
                         * bool test = csp.VerifyData(message, hash, signed);
                         * if (test) richTextBox1.Text += "Подпись корректна.\r\n";
                         * else richTextBox1.Text += "Подпись некорректна.\r\n";
                         *
                         */
                    }
                    if (mode_bytes[0] == 0x03)
                    {
                        richTextBox1.Text += "Received sign and encryption. Making magic: \n";

                        int    bytes_read = ns.Read(buffer, 0, buffer.Length);
                        byte[] message    = new byte[bytes_read];
                        Array.Copy(buffer, message, bytes_read);
                        BERelement mSeq = BERelement.DecodePacket(message);
                        BERelement sSeq = null;

                        sSeq = mSeq.Items[0];
                        var cert_name         = sSeq.Items[0].Value;
                        var wrapped_key2      = sSeq.Items[1].Value;
                        var iv2               = sSeq.Items[2].Value;
                        var public_key_bytes  = sSeq.Items[3].Value;
                        var cipher_text_bytes = sSeq.Items[4].Value;

                        MemoryStream ms = new MemoryStream(public_key_bytes);

                        BinaryFormatter           bf         = new BinaryFormatter();
                        Gost3410Parameters        key_params = (Gost3410Parameters)bf.Deserialize(ms);
                        GostSharedSecretAlgorithm agree_key  = csp.CreateAgree(key_params);
                        SymmetricAlgorithm        gost       = agree_key.Unwrap(wrapped_key2, GostKeyWrapMethod.CryptoProKeyWrap);
                        gost.IV = iv2;
                        MemoryStream memoryStream = new MemoryStream();
                        CryptoStream cryptoStream = new CryptoStream(memoryStream,
                                                                     gost.CreateDecryptor(), CryptoStreamMode.Write);
                        int bytesRead = cipher_text_bytes.Length;
                        cryptoStream.Write(cipher_text_bytes, 0, bytesRead);
                        cryptoStream.FlushFinalBlock();
                        byte[] plain_text_bytes = memoryStream.ToArray();

                        //check signature
                        Gost3411CryptoServiceProvider hash = new Gost3411CryptoServiceProvider();


                        //Array.Copy(buffer, message, bytes_read);

                        BERelement mSeq2 = BERelement.DecodePacket(plain_text_bytes);
                        BERelement sSeq2 = null;

                        sSeq2 = mSeq2.Items[0];
                        var cert_name2 = sSeq2.Items[0].Value;
                        var signed3    = sSeq2.Items[1].Value;
                        var msg2       = sSeq2.Items[2].Value;

                        int len2 = signed3.Length;

                        bool test = csp.VerifyData(msg2, hash, signed3);
                        richTextBox1.Text += "Полученное сообщение: "
                                             + Encoding.ASCII.GetString(msg2, 0, msg2.Length) + "\r\n";
                        if (test)
                        {
                            richTextBox1.Text += "Подпись корректна.\r\n";
                        }
                        else
                        {
                            richTextBox1.Text += "Подпись некорректна.\r\n";
                        }
                    }

                    int i;
                    while ((i = ns.Read(bytes, 0, bytes.Length)) != 0)
                    {
                        data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
                        richTextBox1.Text  = DateTime.Now.ToString() + "\n";
                        richTextBox1.Text += data;
                    }
                    client.Close();
                }
            }
            catch (SocketException exception)
            {
                MessageBox.Show("SocketException: " + exception);
            }
        }