Ejemplo n.º 1
0
        public override void Initialize()
        {
            if (!Directory.Exists(path.Path))
            {
                Directory.CreateDirectory(path.Path);
            }
            //string indexFileName = path.CreatePath ("index").Path;
            string blockFileName = path.CreatePath(name).Path;

            Console.WriteLine("Name: {0}", name);
            //Console.WriteLine ("Index: {0}", indexFileName);
            Console.WriteLine("Block: {0}", blockFileName);
            //bool indexExists = File.Exists (indexFileName);
            bool blockExists = File.Exists(blockFileName);

            options = new BPlusTree <byte[], long> .OptionsV2(BytesSerializer.RawBytes, PrimitiveSerializer.Int64);

            options.KeyComparer = ByteSequenceComparer.Shared;
            options.StorageType = StorageType.Memory;
            index = new BPlusTree <byte[], long> (options);
            index.DebugSetOutput(Console.Out);
            Console.WriteLine("Count: {0}", index.Count);
            Console.WriteLine();
            fs = File.Open(blockFileName, FileMode.OpenOrCreate);
            if ((capacity = fs.Length) == 0)
            {
                fs.SetLength(capacity = 0x110000);
            }
            fs.Close();
            mmfBlock = MemoryMappedFile.CreateFromFile(blockFileName, FileMode.Open);
            maMeta   = mmfBlock.CreateViewAccessor(0, reservedBytes, MemoryMappedFileAccess.ReadWrite);
            if (!blockExists)
            {
                mh.MAGIC       = 0x88DFB78311EFF07A;
                mh.WriteOffset = 0;
                mh.GrowRate    = 0x100000;
                maMeta.Write <MetaHeader> (0, ref mh);
            }
            else
            {
                maMeta.Read <MetaHeader> (0, out mh);
                if (mh.MAGIC != 0x88DFB78311EFF07A)
                {
                    throw new InvalidDataException("file magic doesn't match.");
                }
            }
            CreateIndex();
        }
Ejemplo n.º 2
0
        public KeyValueStorage <byte[]> GetStore(string name)
        {
            if (stores.ContainsKey(name))
            {
                return(stores [name]);
            }
            KeyValueStorage <byte[]> store = new LevelDBKeyValueStorage(path.CreatePath(name));

            System.IO.MemoryStream ms = new System.IO.MemoryStream(repositoryConfigurations.Get
                                                                       (System.Text.Encoding.Unicode.GetBytes(name)));
            System.Xml.Serialization.XmlSerializer xmls = new System.Xml.Serialization.XmlSerializer(typeof(GenericUserRepositoryConfiguration));
            GenericUserRepositoryConfiguration     gurc = (GenericUserRepositoryConfiguration)xmls.Deserialize(ms);

            switch (gurc.EncryptionMehtod)
            {
            case  EncryptionMethod.AES:
                store = new AESEncryptingKeyValueStorage(store, symmetricKeys.Get(gurc.AESKeyID));
                stores.Add(name, store);
                return(store);

            case EncryptionMethod.RSA:
                store = new RSAEncryptingKeyValueStorage(store, rsa.Value);
                stores.Add(name, store);
                return(store);

            case EncryptionMethod.None:
                stores.Add(name, store);
                return(store);

            default:
                throw new NotSupportedException("Specified encryption method is not supported.");
            }
        }
Ejemplo n.º 3
0
        public bool Login(byte[] userID, string password, string pepper)
        {
            if (userStorage.LoggedInUsers.Contains(userID))
            {
                throw new InvalidOperationException(string.Format("User [0x{0}] is already logged in.", userID.ToHexadecimal()));
            }
            bool v = userStorage.Login(userID, password, pepper);

            if (v)
            {
                foreach (var ur in userStorage.GetUserRepositories(userID))
                {
                    byte[] repoInfo = userStorage.GetRawRepositoryInfo(ur);
                    System.Xml.Serialization.XmlSerializer xmls = new System.Xml.Serialization.XmlSerializer(typeof(ChunkRepositoryConfiguration));
                    ChunkRepositoryConfiguration           cri  = (ChunkRepositoryConfiguration)xmls.Deserialize(new MemoryStream(repoInfo, false));
                    DatabasePath dbp = GetRepositoryPath(cri.ID);
                    if (dataStorage.ContainsKey(ur))
                    {
                        foreach (var sk in userStorage.GetUserRepository(ur).SymmetricKeys)
                        {
                            if (!dataStorage [ur].LencryptedData.ContainsKey(sk.Key))
                            {
                                dataStorage [ur].LencryptedData.Add(sk.Key, new AESEncryptingKeyValueStorage(
                                                                        new LevelDBKeyValueStorage(dbp.CreatePath("Encrypted").CreatePath(sk.Key.ToHexadecimal()))
                                                                        , sk.Value));
                            }
                        }
                    }
                    else
                    {
                        SortedDictionary <byte[], KeyValueStorage <byte[]> > EncryptedData = new SortedDictionary <byte[], KeyValueStorage <byte[]> > ();
                        foreach (var sk in userStorage.GetUserRepository(ur).SymmetricKeys)
                        {
                            KeyValueStorageConfiguration ESC = new KeyValueStorageConfiguration();
                            ESC.Type = cri.Data.Type;
                            ESC.Path = sk.Key.ToHexadecimal();
                            EncryptedData.Add(sk.Key, new AESEncryptingKeyValueStorage(
                                                  ESC.OpenStorage <byte[]> (dbp.CreatePath("Encrypted")), sk.Value));
                        }
                        ChunkRepository cr = new ChunkRepository(
                            cri.Data.OpenStorage <byte[]> (dbp),
                            cri.TopLevels.OpenStorage <byte[][]> (dbp),
                            cri.Dependencies.OpenStorage <byte[][]> (dbp),
                            cri.Meta.OpenStorage <byte[]> (dbp),
                            cri.MetaTopLevels.OpenStorage <byte[][]> (dbp),
                            cri.MetaDependencies.OpenStorage <byte[][]> (dbp),
                            cri.Signatures.OpenStorage <byte[][]> (dbp),
                            cri.ChunkSymmetricKeys.OpenStorage <byte[][]> (dbp),
                            cri.Index.OpenStorage <byte[]> (dbp),
                            EncryptedData);
                        dataStorage.Add(cr.ID, cr);
                    }
                    //encryptedStorageManager.Add (ur, new EncryptedStorageManager (cr, userStorage));
                }
            }
            return(v);
        }
Ejemplo n.º 4
0
        //readonly DatabasePath path;

        public UserRepository(DatabasePath path, UserRepositoryConfiguration config)
        {
            if (path == null)
            {
                throw new ArgumentNullException("path");
            }
            if (config == null)
            {
                throw new ArgumentNullException("config");
            }

            Console.WriteLine("Reading KeyValueStores");
            userNames        = config.Users.OpenStorage <string> (path);
            userParents      = config.UserParents.OpenStorage <byte[]> (path);
            userCerts        = config.UserCerts.OpenStorage <byte[]> (path);
            userKeys         = config.UserKeys.OpenStorage <byte[]> (path);
            userSigningCerts = config.UserSigningCerts.OpenStorage <byte[]> (path);
            userSigningKeys  = config.UserSigningKeys.OpenStorage <byte[]> (path);
            userRepositories = config.UserRepositores.OpenStorage <byte[][]> (path);
            repositories     = config.Repositores.OpenStorage <byte[]> (path);
            meta             = config.Meta.OpenStorage <byte[]> (path);
            //permissions = new LevelDBKeyValueStorage<byte[]> (path.CreatePath (config.PermissionsPath));
            genericRepositories = new SortedDictionary <byte[], GenericUserRepositoryCollection> (ByteSequenceComparer.Shared);
            loggedInUsers       = new SortedDictionary <byte[], RSAParameters> (ByteSequenceComparer.Shared);
            Console.WriteLine("Done");
            var e = userNames.GetEnumerator();

            while (e.MoveNext())
            {
                var user = e.Current;
                Console.WriteLine("user: {0}:{1}", user.Key, user.Value);
                genericRepositories.Add(user.Key,
                                        new GenericUserRepositoryCollection(path.CreatePath(user.Key.ToHexadecimal()),
                                                                            DeserializeKey(new MemoryStream(userCerts.Get(user.Key), false)))
                                        );
            }
        }
Ejemplo n.º 5
0
 public LocalUserStorage(DatabasePath path, UserStorageConfiguration config)
 {
     if (path == null)
         throw new ArgumentNullException ("path");
     if (config == null)
         throw new ArgumentNullException ("config");
     this.path = path;
     Log.WriteLine ("Reading KeyValueStores");
     userNames = config.Users.OpenStorage<string> (path);
     userParents = config.UserParents.OpenStorage<byte[]> (path);
     userCerts = config.UserCerts.OpenStorage<byte[]> (path);
     userKeys = config.UserKeys.OpenStorage<byte[]> (path);
     userSigningCerts = config.UserSigningCerts.OpenStorage<byte[]> (path);
     userSigningKeys = config.UserSigningKeys.OpenStorage<byte[]> (path);
     userRepositories = config.UserRepositores.OpenStorage<byte[][]> (path);
     repositories = config.Repositores.OpenStorage<byte[]> (path);
     meta = config.Meta.OpenStorage<byte[]> (path);
     //permissions = new LevelDBKeyValueStorage<byte[]> (path.CreatePath (config.PermissionsPath));
     genericRepositories = new SortedDictionary<byte[], GenericUserStorageCollection> (ByteSequenceComparer.Shared);
     loggedInUsers = new SortedDictionary<byte[], RSAParameters> (ByteSequenceComparer.Shared);
     Log.WriteLine ("Done");
     var e = userNames.GetEnumerator ();
     while (e.MoveNext ()) {
         var user = e.Current;
         Log.WriteLine ("user: {0}:{1}", user.Key, user.Value);
         genericRepositories.Add (user.Key,
             new GenericUserStorageCollection (path.CreatePath (user.Key.ToHexadecimal ()), null));
     }
 }