Exemplo n.º 1
0
        public static int startWizard(string pw, string pemfn, int pemOption, string sockproxy, int proxyport, string server_addr, int server_port, byte[] server_pubkey, DB db)
        {
            string orgpw = pw, temppw = pw;
            byte[] serverPub;
            System.Security.Cryptography.RSAParameters load_pem_rsa = new RSAParameters();
            bool useRsa;
            while (true)
            {
                var r = pw == null ? InputBox("RSA-PM",
            @"How would you like your protection?
            1) Password
            2) PEM file (it may be password protected)
            3) Password and PEM (only one required to unlock).") : InputBox("RSA-PM",
            @"How would you like your protection?
            0) Leave Password
            1) New Password
            2) PEM file (it may be password protected)
            3) Password and PEM (only one required to unlock).");
                int i;
                if (r == null)
                {
                    return -1;
                }
                var b = int.TryParse(r, out i);
                if (b == false)
                {
                    if (MessageBox.Show("Error that isn't a number. Please select one of the options") == DialogResult.OK) { continue; }
                    else { return -1; }
                }
                serverPub = null;
                switch (i)
                {
                    case 0:
                        temppw = pw;
                        if (pw == null)
                            goto default;
                        break;
                    case 1:
                        temppw = PasswordConfig(pw);
                        if (temppw == null)
                            continue;
                        break;
                    case 2:
                        temppw = null;
                        pemfn = PemConfig(out pemOption, out load_pem_rsa, true);
                        if (pemOption == -1)
                            continue;
                        break;
                    case 3:
                        temppw = PasswordConfig(pw);
                        if (temppw == null)
                            continue;
                        pemfn = PemConfig(out pemOption, out load_pem_rsa, true);
                        if (pemOption == -1)
                            continue;
                        break;
                    default:
                        if (MessageBox.Show("Invalid Option") == DialogResult.OK) { continue; }
                        else { return -1; }
                }
                var restart = false;
                while (true)
                {
                    var r2 = MessageBox.Show("Would you like to use a SOCK proxy? (Http and other proxies are not supported)", "", MessageBoxButtons.YesNoCancel);
                    if (r2 != DialogResult.Yes)
                    {
                        restart = r2 == DialogResult.Cancel;
                        sockproxy = null;
                        break;
                    }
                //i am lazy
                addrpart:
                    var addr = sockproxy;
                    if (InputBox("What is the address?", null, ref addr) != DialogResult.OK)
                        continue;
                    if (Regex.IsMatch(addr, @"^\d+$") || !Regex.IsMatch(addr, @"^(\w+\:)?[\w\-_]+\.[\w\-_\.]+$")) { MessageBox.Show("Invalid Address"); goto addrpart; }
                portpart:
                    string szport = proxyport.ToString();
                    if (InputBox("What is the port?", null, ref szport) != DialogResult.OK)
                        continue;
                    Int16 iport;
                    if (Int16.TryParse(szport, out iport) == false) { MessageBox.Show("Invalid Port"); goto portpart; }
                    sockproxy = addr;
                    proxyport = iport;
                    break;
                }
                if (restart)
                    continue;
                while (true)
                {
                    useRsa = false;
                    if (false)
                    {
                        MessageBox.Show("What is the server public key? (pem file)");
                        var dia = new OpenFileDialog() { Filter = "PEM files|*.pem" };
                        if (dia.ShowDialog() != DialogResult.OK)
                        {
                            restart = true;
                            break;
                        }
                        if (Shared.LoadKey(dia.FileName, null, out load_pem_rsa))
                        {
                            MessageBox.Show("This file has a private key. This is either incorrect or the server is not secure as it gave away its private key. Pick another pem file (or server)");
                            continue;
                        }
                        using (var f = File.OpenText(dia.FileName))
                        {
                            serverPub = Utils.ExtractPublicKey2(f.ReadToEnd());
                        }
                    }
                    else
                    {
                        string res = "";
                        if (server_pubkey != null)
                        {
                            res = Shared.pubToPem(server_pubkey);
                        }
                        if (InputBox("What is the server public key?", null, ref res) != DialogResult.OK)
                        {
                            restart = true;
                            break;
                        }
                        try
                        {
                            RSAParameters rsap;
                            //The lib requires line returns so lets ->byte->pem this
                            res = Shared.pubToPem(Utils.ExtractPublicKey2(res));
                            if (Shared.LoadKey2(res, null, out rsap))
                            {
                                MessageBox.Show("This is a private key. This is either incorrect or the server is not secure as it gave away its private key. Pick paste another public key or choose another server");
                                continue;
                            }
                            serverPub = Utils.ExtractPublicKey2(res);
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show("I don't like this public key");
                            continue;
                        }
                        useRsa = true;
                    }

                addrpart:
                    var addr = server_addr ?? "";
                    if (InputBox("What is the address?", null, ref addr) != DialogResult.OK)
                        continue;
                    if (!(
                        addr == "localhost" ||
                        Regex.IsMatch(addr, @"^(\d{1,3}\.){3}\d+$") ||
                        Regex.IsMatch(addr, @"^[\w\-_]+\.[\w\-_\.]+$"))
                        ) { MessageBox.Show("Invalid Address"); goto addrpart; }
                portpart:
                    string szport = server_port.ToString();
                    if (InputBox("What is the port?", null, ref szport) != DialogResult.OK)
                        continue;
                    Int16 iport;
                    if (Int16.TryParse(szport, out iport) == false) { MessageBox.Show("Invalid Port"); goto portpart; }
                    server_addr = addr;
                    server_port = iport;
                    break;
                }
                if (restart)
                    continue;
                break;
            }
            db.Setup(temppw, pemfn, pemOption, sockproxy, proxyport, server_addr, server_port, serverPub, load_pem_rsa, useRsa);
            return 0;
        }
Exemplo n.º 2
0
 static void Main()
 {
     Application.EnableVisualStyles();
     Application.SetCompatibleTextRenderingDefault(false);
     //new ClientTest().Run(); return;
     if (false)
     {
         var aes = new System.Security.Cryptography.RijndaelManaged();
         for (int i = 0; i < 20; ++i)
         {
             var f = true;
             foreach (var v in aes.IV)
             {
                 if (f) { Console.Write(@"new byte[16] { "); f = false; } else { Console.Write(", "); }
                 Console.Write(@"{0}", v);
             }
             Console.WriteLine("}");
             aes.GenerateIV();
         }
     }
     DB db;
     DB.filename = ConfigurationSettings.AppSettings["dbfile"] ?? "RSA-PM.sqlite3";
     var fi = new FileInfo(DB.filename);
     if (fi.Exists && fi.Length > 0)
     {
         db = new DB();
         var d = db.LoadInfo();
         var hasPw = d.ContainsKey("pw");
         while (hasPw)
         {
             password = InputBox("What is the password?", null, true);
             if (password == null)
                 break;
             try
             {
                 db.Load(password);
                 break;
             }
             catch (System.Security.Cryptography.CryptographicException ex)
             {
                 MessageBox.Show("Wrong Password");
             }
         }
         var hasPem = d.ContainsKey("pem_data");
         while (!db.IsLoaded && hasPem)
         {
             RSAParameters rsap = new RSAParameters();
             var pemfn = d.ContainsKey("pempath") ? Encoding.UTF8.GetString(d["pempath"]) : "";
             if (File.Exists(pemfn))
             {
                 LoadPemFile(pemfn, out rsap);
             }
             else
             {
                 int optdummy = 0;
                 if (PemConfig(out optdummy, out rsap, false) == null)
                     break;
             }
             try
             {
                 if (db.Load(rsap, d["pem_data"]))
                     break;
             }
             catch (System.Security.Cryptography.CryptographicException ex)
             {
                 if (ex.Message.StartsWith(@"Bad Data."))
                 {
                     MessageBox.Show("Wrong PEM file?");
                     continue;
                 }
                 return;
             }
             catch
             {
                 return;
             }
         }
         if (db.IsLoaded == false)
             return;
     }
     else
     {
         db = new DB();
         if (startWizard(null, null, 0, null, 0, null, 10101, null, db) != 0) { MessageBox.Show("Configuration failed. Existing application"); return; }
     }
     Application.Run(new Form1(db));
 }
Exemplo n.º 3
0
Arquivo: db.cs Projeto: RSA-PM/RSA-PM
 public DB()
 {
     conn = MakeConn(); db = this;
 }