Esempio n. 1
0
File: OpCore.cs Progetto: swax/DeOps
        public string GenerateInvite(string pubLink, out string name)
        {
            IdentityLink ident = IdentityLink.Decode(pubLink);

            // dont check opID, because invites come from different ops

            name = ident.Name;

            // deops://firesoft/invite/person@GlobalIM/originalopID~invitedata {op key web caches ips}

            string link = string.Format("deops://{0}/invite/{1}@{2}/",
                            HttpUtility.UrlEncode(User.Settings.Operation),
                            HttpUtility.UrlEncode(ident.Name),
                            HttpUtility.UrlEncode(ident.OpName));

            // encode invite info in user's public key
            byte[] data = new byte[4096];
            MemoryStream mem = new MemoryStream(data);
            PacketStream stream = new PacketStream(mem, GuiProtocol, FileAccess.Write);

            // write invite
            OneWayInvite invite = new OneWayInvite();
            invite.UserName = ident.Name;
            invite.OpName = User.Settings.Operation;
            invite.OpAccess = User.Settings.OpAccess;
            invite.OpID = User.Settings.OpKey;

            stream.WritePacket(invite);

            // write some contacts
            foreach (DhtContact contact in Network.Routing.GetCacheArea())
            {
                byte[] bytes = contact.Encode(GuiProtocol, InvitePacket.Contact);
                mem.Write(bytes, 0, bytes.Length);
            }

            // write web caches
            foreach (WebCache cache in Network.Cache.GetLastSeen(3))
                stream.WritePacket(new WebCache(cache, InvitePacket.WebCache));

            mem.WriteByte(0); // end packets

            byte[] packets = Utilities.ExtractBytes(data, 0, (int)mem.Position);
            byte[] encrypted = Utilities.KeytoRsa(ident.PublicKey).Encrypt(packets,false);

            // ensure that this link is opened from the original operation remote's public key came from
            byte[] final = Utilities.CombineArrays(ident.PublicOpID, encrypted);

            return link + Utilities.BytestoHex(final);
        }
Esempio n. 2
0
File: OpCore.cs Progetto: swax/DeOps
 // ensure that key/name associations persist between runs, done so remote people dont change their name and try to play with
 // us, once we make an association with a key, we change that name on our terms, also prevents key spoofing with dupe
 // user ids
 public void SaveKeyIndex(PacketStream stream)
 {
     NameMap.LockReading(() =>
     {
         foreach (ulong user in KeyMap.Keys)
             if (NameMap.ContainsKey(user))
             {
                 Debug.Assert(NameMap[user] != null);
                 if (NameMap[user] != null)
                     stream.WritePacket(new UserInfo() { Name = NameMap[user], Key = KeyMap[user] });
             }
     });
 }
Esempio n. 3
0
        public void SaveLocal()
        {
            if (Core.InvokeRequired)
            {
                Core.RunInCoreAsync(() => SaveLocal());
                return;
            }

            try
            {
                // create new link file in temp dir
                string tempPath = Core.GetTempPath();
                byte[] key = Utilities.GenerateKey(Core.StrongRndGen, 256);
                using (IVCryptoStream stream = IVCryptoStream.Save(tempPath, key))
                {
                    // project packets
                    foreach (OpLink link in LocalTrust.Links.Values)
                        if (link.Active)
                        {
                            ProjectData project = new ProjectData();
                            project.ID = link.Project;
                            project.Name = GetProjectName(link.Project);

                            if (link.Project == 0)
                                project.UserName = Core.User.Settings.UserName;

                            byte[] packet = SignedData.Encode(Network.Protocol, Core.User.Settings.KeyPair, project);
                            stream.Write(packet, 0, packet.Length);

                            // uplinks
                            if (link.Uplink != null)
                            {
                                LinkData data = new LinkData(link.Project, link.Uplink.Trust.File.Key);
                                packet = SignedData.Encode(Network.Protocol, Core.User.Settings.KeyPair, data);
                                stream.Write(packet, 0, packet.Length);
                            }

                            // downlinks
                            foreach (OpLink downlink in link.Downlinks)
                                if (link.Confirmed.Contains(downlink.UserID))
                                {
                                    string title;
                                    link.Titles.TryGetValue(downlink.UserID, out title);

                                    LinkData data = new LinkData(link.Project, downlink.Trust.File.Key, title);
                                    packet = SignedData.Encode(Network.Protocol, Core.User.Settings.KeyPair, data);
                                    stream.Write(packet, 0, packet.Length);
                                }
                        }

                    PacketStream streamEx = new PacketStream(stream, Network.Protocol, FileAccess.Write);

                    // save top 5 web caches
                    foreach (WebCache cache in Network.Cache.GetLastSeen(5))
                        streamEx.WritePacket(new WebCache(cache, TrustPacket.WebCache));

                    // save inheritable settings only if they can be inherited
                    if (IsInheritNode(Core.UserID))
                    {
                        if (Core.User.OpIcon != null)
                            streamEx.WritePacket(new IconPacket(TrustPacket.Icon, Core.User.OpIcon));

                        if (Core.User.OpSplash != null)
                        {
                            MemoryStream splash = new MemoryStream();
                            Core.User.OpSplash.Save(splash, ImageFormat.Jpeg);
                            LargeDataPacket.Write(streamEx, TrustPacket.Splash, splash.ToArray());
                        }
                    }

                    stream.WriteByte(0); // signal last packet
                    stream.FlushFinalBlock();
                }

                OpVersionedFile file = Cache.UpdateLocal(tempPath, key, null);

                Store.PublishDirect(GetLocsAbove(), Core.UserID, ServiceID, DataTypeFile, file.SignedHeader);

                SaveUplinkReqs();
            }
            catch (Exception ex)
            {
                Network.UpdateLog("LinkControl", "Error updating local " + ex.Message);
            }
        }
Esempio n. 4
0
        public void SaveLocal()
        {
            // create temp, write buddy list
            string tempPath = Core.GetTempPath();
            byte[] key = Utilities.GenerateKey(Core.StrongRndGen, 256);
            using (IVCryptoStream crypto = IVCryptoStream.Save(tempPath, key))
            {
                PacketStream stream = new PacketStream(crypto, Network.Protocol, FileAccess.Write);

                BuddyList.LockReading(() =>
                {
                    foreach (OpBuddy buddy in BuddyList.Values)
                        stream.WritePacket(buddy);
                });

                IgnoreList.LockReading(() => // we need to ensure we have name/key for each ignore
                {
                    foreach (OpBuddy ignore in IgnoreList.Values)
                        stream.WritePacket(ignore);
                });
            }

            byte[] publicEncryptedKey = Core.User.Settings.KeyPair.Encrypt(key, false); //private key required to decrypt buddy list

            Cache.UpdateLocal(tempPath, publicEncryptedKey, null);
        }
Esempio n. 5
0
File: OpUser.cs Progetto: swax/DeOps
        public void Save()
        {
            if (Core != null && Core.InvokeRequired)
            {
                Debug.Assert(false);
                Core.RunInCoreAsync(() => Save());
                return;
            }

            string backupPath = ProfilePath.Replace(".dop", ".bak");

            if( !File.Exists(backupPath) && File.Exists(ProfilePath))
                File.Copy(ProfilePath, backupPath, true);

            RijndaelManaged Password = new RijndaelManaged();
            Password.Key = PasswordKey;

            try
            {
                // Attach to crypto stream and write file
                string tempPath = TempPath + Path.DirectorySeparatorChar + "firstsave";
                if (Core != null)
                    tempPath = Core.GetTempPath();

                using (FileStream file = new FileStream(tempPath, FileMode.Create))
                {
                    // write encrypted part of file
                    Password.GenerateIV();
                    file.Write(Password.IV, 0, Password.IV.Length);
                    file.Write(PasswordSalt, 0, PasswordSalt.Length);

                    using (CryptoStream crypto = new CryptoStream(file, Password.CreateEncryptor(), CryptoStreamMode.Write))
                    {
                        PacketStream stream = new PacketStream(crypto, Protocol, FileAccess.Write);

                        stream.WritePacket(Settings);

                        if (Core != null)
                        {
                            if (Core.Context.Lookup != null)
                            {
                                Core.Context.Lookup.Network.Cache.SaveIPs(stream);
                                Core.Context.Lookup.Network.Cache.SaveWeb(stream);
                            }

                            Core.Network.Cache.SaveIPs(stream);
                            Core.Network.Cache.SaveWeb(stream);

                            Core.SaveKeyIndex(stream);
                        }

                        if (OpIcon != null)
                            stream.WritePacket(new IconPacket(IdentityPacket.Icon, OpIcon));
                    }
                }

                // write unencrypted splash
                using (FileStream file = new FileStream(tempPath, FileMode.Open))
                {
                    file.Seek(0, SeekOrigin.End);

                    long startpos = file.Position;

                    PacketStream stream = new PacketStream(file, Protocol, FileAccess.Write);

                    // get right splash image (only used for startup logo, main setting is in link file)
                    if (OpSplash != null)
                    {
                        MemoryStream mem = new MemoryStream();
                        OpSplash.Save(mem, ImageFormat.Jpeg);
                        LargeDataPacket.Write(stream, IdentityPacket.Splash, mem.ToArray());
                    }
                    else
                        LargeDataPacket.Write(stream, IdentityPacket.Splash, null);

                    file.WriteByte(0); // end packet stream

                    byte[] last = BitConverter.GetBytes(startpos);
                    file.Write(last, 0, last.Length);
                }

                File.Copy(tempPath, ProfilePath, true);
                File.Delete(tempPath);
            }

            catch (Exception ex)
            {
                if (Core != null)
                    Core.ConsoleLog("Exception Identity::Save() " + ex.Message);
                else
                    Core.UserMessage("Profile Save Error:\n" + ex.Message + "\nBackup Restored");

                // restore backup
                if (File.Exists(backupPath))
                    File.Copy(backupPath, ProfilePath, true);
            }

            File.Delete(backupPath);
        }
Esempio n. 6
0
File: OpUser.cs Progetto: swax/DeOps
        public void Save(OpCore core)
        {
            Debug.Assert(core.Network.IsLookup);

            try
            {
                var serializer = new XmlSerializer(typeof(PortsConfig));

                using (var writer = new StreamWriter(PortsConfigPath))
                    serializer.Serialize(writer, Ports);
            }
            catch { }

            if (core.Sim != null)
                return;

            try
            {
                // Attach to crypto stream and write file
                using (IVCryptoStream crypto = IVCryptoStream.Save(BootstrapPath, BootstrapKey))
                {
                    PacketStream stream = new PacketStream(crypto, core.Network.Protocol, FileAccess.Write);

                    if(core.Context.SignedUpdate != null)
                        stream.WritePacket(core.Context.SignedUpdate);

                    core.Network.Cache.SaveIPs(stream);
                    core.Network.Cache.SaveWeb(stream);
                }
            }

            catch (Exception ex)
            {
                core.Network.UpdateLog("Exception", "LookupSettings::Save " + ex.Message);
            }
        }
Esempio n. 7
0
        public void SaveWeb(PacketStream stream)
        {
            // randomize
            List<WebCache> source = new List<WebCache>(WebCaches);
            List<WebCache> randomized = new List<WebCache>();

            while (source.Count > 0)
            {
                WebCache pick = source[Core.RndGen.Next(source.Count)];
                randomized.Add(pick);
                source.Remove(pick);
            }

            foreach (WebCache cache in randomized)
                stream.WritePacket(cache);
        }
Esempio n. 8
0
        public void SaveIPs(PacketStream stream)
        {
            byte type = Network.IsLookup ? IdentityPacket.LookupCachedIP : IdentityPacket.OpCachedIP;

            foreach (var contact in BootstrapContacts.Values)
                stream.WritePacket(new CachedIP(type, contact, true));

            lock (IPs)
                foreach (var contact in IPs)
                    if (contact.TunnelClient == null)
                        stream.WritePacket(new CachedIP(type, contact, false));
        }
Esempio n. 9
0
        public void SaveHeaders()
        {
            // save public shared lists
            try
            {
                Local.Key = Utilities.GenerateKey(Core.StrongRndGen, 256);

                string tempPath = Core.GetTempPath();
                using (IVCryptoStream crypto = IVCryptoStream.Save(tempPath, Local.Key))
                {
                    PacketStream stream = new PacketStream(crypto, Network.Protocol, FileAccess.Write);

                    Local.Files.LockReading(delegate()
                    {
                        foreach (SharedFile file in Local.Files)
                            if (file.Hash != null &&
                                file.ClientID == Core.Network.Local.ClientID &&
                                file.Public)
                            {
                                file.SaveLocal = false;
                                stream.WritePacket(file);
                            }
                    });

                    crypto.FlushFinalBlock();
                }

                Utilities.HashTagFile(tempPath, Core.Network.Protocol, ref Local.Hash, ref Local.Size);

                string finalPath = GetPublicPath(Local);
                File.Copy(tempPath, finalPath, true);
                File.Delete(tempPath);
            }
            catch (Exception ex)
            {
                Network.UpdateLog("Share", "Error saving public: " + ex.Message);
            }

            // save private/public shared - this is also whats loaded on startup
            try
            {
                string tempPath = Core.GetTempPath();
                using (IVCryptoStream crypto = IVCryptoStream.Save(tempPath, Core.User.Settings.FileKey))
                {
                    PacketStream stream = new PacketStream(crypto, Network.Protocol, FileAccess.Write);

                    // save all files
                    Local.Files.LockReading(delegate()
                    {
                        foreach (SharedFile file in Local.Files)
                            if (file.Hash != null &&
                                file.ClientID == Core.Network.Local.ClientID)
                            {
                                file.SaveLocal = true;
                                stream.WritePacket(file);
                            }
                    });

                    // save our public header
                    if(Local.Hash != null)
                        stream.WritePacket(Local);

                    crypto.FlushFinalBlock();
                }

                File.Copy(tempPath, HeaderPath, true);
                File.Delete(tempPath);
            }
            catch (Exception ex)
            {
                Network.UpdateLog("Share", "Error saving headers: " + ex.Message);
            }
        }