예제 #1
0
        public Profile UploadProfile(
            int cost,
            ExchangePublicKey exchangePublicKey,
            IEnumerable <string> trustSignatures,
            IEnumerable <string> deleteSignatures,
            IEnumerable <Wiki> wikis,
            IEnumerable <Chat> chats,

            DigitalSignature digitalSignature)
        {
            lock (this.ThisLock)
            {
                var uploadItem = new UploadItem();
                uploadItem.Type             = "Profile";
                uploadItem.Profile          = new Profile(DateTime.UtcNow, cost, exchangePublicKey, trustSignatures, deleteSignatures, wikis, chats, digitalSignature);
                uploadItem.DigitalSignature = digitalSignature;

                _settings.UploadItems.RemoveAll((target) =>
                {
                    return(target.Type == uploadItem.Type &&
                           target.DigitalSignature == digitalSignature);
                });

                _settings.UploadItems.Add(uploadItem);

                return(uploadItem.Profile);
            }
        }
예제 #2
0
        internal Profile(DateTime creationTime, int cost, ExchangePublicKey exchangePublicKey, IEnumerable <string> trustSignatures, IEnumerable <string> deleteSignatures, IEnumerable <Wiki> wikis, IEnumerable <Chat> chats, DigitalSignature digitalSignature)
        {
            this.CreationTime = creationTime;

            this.Cost = cost;
            this.ExchangePublicKey = exchangePublicKey;
            if (trustSignatures != null)
            {
                this.ProtectedTrustSignatures.AddRange(trustSignatures);
            }
            if (deleteSignatures != null)
            {
                this.ProtectedDeleteSignatures.AddRange(deleteSignatures);
            }
            if (wikis != null)
            {
                this.ProtectedWikis.AddRange(wikis);
            }
            if (chats != null)
            {
                this.ProtectedChats.AddRange(chats);
            }

            this.CreateCertificate(digitalSignature);
        }
예제 #3
0
        public SignatureMessage UploadSignatureMessage(string signature,
                                                       string comment,

                                                       ExchangePublicKey exchangePublicKey,
                                                       int miningLimit,
                                                       TimeSpan miningTime,
                                                       DigitalSignature digitalSignature)
        {
            if (_disposed)
            {
                throw new ObjectDisposedException(this.GetType().FullName);
            }

            lock (this.ThisLock)
            {
                return(_uploadManager.UploadSignatureMessage(signature, comment, exchangePublicKey, miningLimit, miningTime, digitalSignature));
            }
        }
예제 #4
0
 public Profile(string comment, ExchangePublicKey exchangePublicKey,
                IEnumerable <Signature> trustSignatures, IEnumerable <Signature> deleteSignatures,
                IEnumerable <Tag> tags)
 {
     this.Comment           = comment;
     this.ExchangePublicKey = exchangePublicKey;
     if (trustSignatures != null)
     {
         this.ProtectedTrustSignatures.AddRange(trustSignatures);
     }
     if (deleteSignatures != null)
     {
         this.ProtectedDeleteSignatures.AddRange(deleteSignatures);
     }
     if (tags != null)
     {
         this.ProtectedTags.AddRange(tags);
     }
 }
예제 #5
0
        public Profile UploadProfile(
            int cost,
            ExchangePublicKey exchangePublicKey,
            IEnumerable <string> trustSignatures,
            IEnumerable <string> deleteSignatures,
            IEnumerable <Wiki> wikis,
            IEnumerable <Chat> chats,

            DigitalSignature digitalSignature)
        {
            if (_disposed)
            {
                throw new ObjectDisposedException(this.GetType().FullName);
            }

            lock (this.ThisLock)
            {
                return(_uploadManager.UploadProfile(cost, exchangePublicKey, trustSignatures, deleteSignatures, wikis, chats, digitalSignature));
            }
        }
예제 #6
0
            public static Stream ToCryptoStream <T>(T message, int paddingSize, ExchangePublicKey publicKey)
                where T : ItemBase <T>
            {
                if (message == null)
                {
                    throw new ArgumentNullException(nameof(message));
                }
                if (publicKey == null)
                {
                    throw new ArgumentNullException(nameof(publicKey));
                }

                try
                {
                    return(AddVersion(AddHash(Encrypt(AddPadding(Compress(message.Export(_bufferManager)), paddingSize), publicKey)), 0));
                }
                catch (Exception)
                {
                    return(null);
                }
            }
예제 #7
0
        public Task Upload(Signature targetSignature, MailMessage mailMessage, ExchangePublicKey exchangePublicKey, DigitalSignature digitalSignature, CancellationToken token)
        {
            if (targetSignature == null)
            {
                throw new ArgumentNullException(nameof(targetSignature));
            }
            if (mailMessage == null)
            {
                throw new ArgumentNullException(nameof(mailMessage));
            }
            if (digitalSignature == null)
            {
                throw new ArgumentNullException(nameof(digitalSignature));
            }

            return(_coreManager.VolatileSetStream(ContentConverter.ToCryptoStream(mailMessage, 1024 * 256, exchangePublicKey), TimeSpan.FromDays(360), token)
                   .ContinueWith(task =>
            {
                _coreManager.UploadMetadata(new UnicastMetadata("MailMessage", targetSignature, DateTime.UtcNow, task.Result, digitalSignature));
            }));
        }
예제 #8
0
        protected override void ProtectedImport(Stream stream, BufferManager bufferManager, int depth)
        {
            using (var reader = new ItemStreamReader(stream, bufferManager))
            {
                while (reader.Available > 0)
                {
                    int id = (int)reader.GetUInt32();

                    if (id == (int)SerializeId.Comment)
                    {
                        this.Comment = reader.GetString();
                    }
                    else if (id == (int)SerializeId.ExchangePublicKey)
                    {
                        this.ExchangePublicKey = ExchangePublicKey.Import(reader.GetStream(), bufferManager);
                    }
                    else if (id == (int)SerializeId.TrustSignatures)
                    {
                        for (int i = (int)reader.GetUInt32() - 1; i >= 0; i--)
                        {
                            this.ProtectedTrustSignatures.Add(Signature.Import(reader.GetStream(), bufferManager));
                        }
                    }
                    else if (id == (int)SerializeId.DeleteSignatures)
                    {
                        for (int i = (int)reader.GetUInt32() - 1; i >= 0; i--)
                        {
                            this.ProtectedDeleteSignatures.Add(Signature.Import(reader.GetStream(), bufferManager));
                        }
                    }
                    else if (id == (int)SerializeId.Tags)
                    {
                        for (int i = (int)reader.GetUInt32() - 1; i >= 0; i--)
                        {
                            this.ProtectedTags.Add(Tag.Import(reader.GetStream(), bufferManager));
                        }
                    }
                }
            }
        }
예제 #9
0
        public SignatureMessage UploadSignatureMessage(string signature,
                                                       string comment,

                                                       ExchangePublicKey exchangePublicKey,
                                                       int miningLimit,
                                                       TimeSpan miningTime,
                                                       DigitalSignature digitalSignature)
        {
            lock (this.ThisLock)
            {
                var uploadItem = new UploadItem();
                uploadItem.Type              = "SignatureMessage";
                uploadItem.SignatureMessage  = new SignatureMessage(signature, DateTime.UtcNow, comment, digitalSignature);
                uploadItem.ExchangePublicKey = exchangePublicKey;
                uploadItem.MiningLimit       = miningLimit;
                uploadItem.MiningTime        = miningTime;
                uploadItem.DigitalSignature  = digitalSignature;

                _settings.UploadItems.Add(uploadItem);

                return(uploadItem.SignatureMessage);
            }
        }
예제 #10
0
        public void UnicastUpload(string signature,
            Message message,

            ExchangePublicKey exchangePublicKey,
            DigitalSignature digitalSignature)
        {
            this.Check();

            lock (this.ThisLock)
            {
                _backgroundUploadManager.UnicastUpload(signature, message, exchangePublicKey, digitalSignature);
            }
        }
예제 #11
0
            private static Stream Encrypt(Stream stream, ExchangePublicKey publicKey)
            {
                if (stream == null)
                {
                    throw new ArgumentNullException(nameof(stream));
                }
                if (publicKey == null)
                {
                    throw new ArgumentNullException(nameof(publicKey));
                }

                try
                {
                    RecyclableMemoryStream outStream = null;

                    try
                    {
                        outStream = new RecyclableMemoryStream(_bufferManager);
                        Varint.SetUInt64(outStream, (uint)ConvertCryptoAlgorithm.Aes256);

                        var cryptoKey = new byte[32];
                        var iv        = new byte[32];

                        using (var random = RandomNumberGenerator.Create())
                        {
                            random.GetBytes(cryptoKey);
                            random.GetBytes(iv);
                        }

                        {
                            var encryptedBuffer = Exchange.Encrypt(publicKey, cryptoKey);
                            Varint.SetUInt64(outStream, (uint)encryptedBuffer.Length);
                            outStream.Write(encryptedBuffer, 0, encryptedBuffer.Length);
                        }

                        outStream.Write(iv, 0, iv.Length);

                        using (var aes = Aes.Create())
                        {
                            aes.KeySize = 256;
                            aes.Mode    = CipherMode.CBC;
                            aes.Padding = PaddingMode.PKCS7;

                            using (var inStream = new WrapperStream(stream))
                                using (var cs = new CryptoStream(inStream, aes.CreateEncryptor(cryptoKey, iv), CryptoStreamMode.Read))
                                    using (var safeBuffer = _bufferManager.CreateSafeBuffer(1024 * 4))
                                    {
                                        int length;

                                        while ((length = cs.Read(safeBuffer.Value, 0, safeBuffer.Value.Length)) > 0)
                                        {
                                            outStream.Write(safeBuffer.Value, 0, length);
                                        }
                                    }
                        }

                        outStream.Seek(0, SeekOrigin.Begin);
                    }
                    catch (Exception)
                    {
                        if (outStream != null)
                        {
                            outStream.Dispose();
                        }

                        throw;
                    }

                    return(outStream);
                }
                catch (Exception e)
                {
                    throw new ArgumentException(e.Message, e);
                }
                finally
                {
                    if (stream != null)
                    {
                        stream.Dispose();
                    }
                }
            }
예제 #12
0
        public Profile UploadProfile(
            int cost,
            ExchangePublicKey exchangePublicKey,
            IEnumerable<string> trustSignatures,
            IEnumerable<string> deleteSignatures,
            IEnumerable<Wiki> wikis,
            IEnumerable<Chat> chats,

            DigitalSignature digitalSignature)
        {
            if (_disposed) throw new ObjectDisposedException(this.GetType().FullName);

            lock (this.ThisLock)
            {
                return _uploadManager.UploadProfile(cost, exchangePublicKey, trustSignatures, deleteSignatures, wikis, chats, digitalSignature);
            }
        }
예제 #13
0
        public SignatureMessage UploadSignatureMessage(string signature,
            string comment,

            ExchangePublicKey exchangePublicKey,
            int miningLimit,
            TimeSpan miningTime,
            DigitalSignature digitalSignature)
        {
            if (_disposed) throw new ObjectDisposedException(this.GetType().FullName);

            lock (this.ThisLock)
            {
                return _uploadManager.UploadSignatureMessage(signature, comment, exchangePublicKey, miningLimit, miningTime, digitalSignature);
            }
        }
예제 #14
0
        public Task SetMailMessage(Signature targetSignature, MailMessage mailMessage, ExchangePublicKey exchangePublicKey, DigitalSignature digitalSignature, CancellationToken token)
        {
            this.Check();

            lock (_lockObject)
            {
                return(_messageManager.Upload(targetSignature, mailMessage, exchangePublicKey, digitalSignature, token));
            }
        }
예제 #15
0
        public Profile UploadProfile(
            int cost,
            ExchangePublicKey exchangePublicKey,
            IEnumerable<string> trustSignatures,
            IEnumerable<string> deleteSignatures,
            IEnumerable<Wiki> wikis,
            IEnumerable<Chat> chats,

            DigitalSignature digitalSignature)
        {
            lock (this.ThisLock)
            {
                var uploadItem = new UploadItem();
                uploadItem.Type = "Profile";
                uploadItem.Profile = new Profile(DateTime.UtcNow, cost, exchangePublicKey, trustSignatures, deleteSignatures, wikis, chats, digitalSignature);
                uploadItem.DigitalSignature = digitalSignature;

                _settings.UploadItems.RemoveAll((target) =>
                {
                    return target.Type == uploadItem.Type
                        && target.DigitalSignature == digitalSignature;
                });

                _settings.UploadItems.Add(uploadItem);

                return uploadItem.Profile;
            }
        }
예제 #16
0
        public SignatureMessage UploadSignatureMessage(string signature,
            string comment,

            ExchangePublicKey exchangePublicKey,
            int miningLimit,
            TimeSpan miningTime,
            DigitalSignature digitalSignature)
        {
            lock (this.ThisLock)
            {
                var uploadItem = new UploadItem();
                uploadItem.Type = "SignatureMessage";
                uploadItem.SignatureMessage = new SignatureMessage(signature, DateTime.UtcNow, comment, digitalSignature);
                uploadItem.ExchangePublicKey = exchangePublicKey;
                uploadItem.MiningLimit = miningLimit;
                uploadItem.MiningTime = miningTime;
                uploadItem.DigitalSignature = digitalSignature;

                _settings.UploadItems.Add(uploadItem);

                return uploadItem.SignatureMessage;
            }
        }
예제 #17
0
        protected override void ProtectedImport(Stream stream, BufferManager bufferManager, int count)
        {
            for (; ;)
            {
                byte id;
                {
                    byte[] idBuffer = new byte[1];
                    if (stream.Read(idBuffer, 0, idBuffer.Length) != idBuffer.Length)
                    {
                        return;
                    }
                    id = idBuffer[0];
                }

                int length;
                {
                    byte[] lengthBuffer = new byte[4];
                    if (stream.Read(lengthBuffer, 0, lengthBuffer.Length) != lengthBuffer.Length)
                    {
                        return;
                    }
                    length = NetworkConverter.ToInt32(lengthBuffer);
                }

                using (RangeStream rangeStream = new RangeStream(stream, stream.Position, length, true))
                {
                    if (id == (byte)SerializeId.CreationTime)
                    {
                        this.CreationTime = DateTime.ParseExact(ItemUtilities.GetString(rangeStream), "yyyy-MM-ddTHH:mm:ssZ", System.Globalization.DateTimeFormatInfo.InvariantInfo).ToUniversalTime();
                    }

                    else if (id == (byte)SerializeId.Cost)
                    {
                        this.Cost = ItemUtilities.GetInt(rangeStream);
                    }
                    else if (id == (byte)SerializeId.ExchangePublicKey)
                    {
                        this.ExchangePublicKey = ExchangePublicKey.Import(rangeStream, bufferManager);
                    }
                    else if (id == (byte)SerializeId.TrustSignature)
                    {
                        this.ProtectedTrustSignatures.Add(ItemUtilities.GetString(rangeStream));
                    }
                    else if (id == (byte)SerializeId.DeleteSignature)
                    {
                        this.ProtectedDeleteSignatures.Add(ItemUtilities.GetString(rangeStream));
                    }
                    else if (id == (byte)SerializeId.Wiki)
                    {
                        this.ProtectedWikis.Add(Wiki.Import(rangeStream, bufferManager));
                    }
                    else if (id == (byte)SerializeId.Chat)
                    {
                        this.ProtectedChats.Add(Chat.Import(rangeStream, bufferManager));
                    }

                    else if (id == (byte)SerializeId.Certificate)
                    {
                        this.Certificate = Certificate.Import(rangeStream, bufferManager);
                    }
                }
            }
        }
예제 #18
0
        public Task SetMailMessage(Signature targetSignature, MailMessage mailMessage, ExchangePublicKey exchangePublicKey, DigitalSignature digitalSignature, CancellationToken token)
        {
            this.Check();

            lock (_lockObject)
            {
                return(Task.Run(() =>
                {
                    this.Action(AmoebaRequestType.SetMailMessage, (targetSignature, mailMessage, exchangePublicKey, digitalSignature), token);
                }));
            }
        }
        public void UnicastUpload(string signature,
            Message message,

            ExchangePublicKey exchangePublicKey,
            DigitalSignature digitalSignature)
        {
            if (signature == null) throw new ArgumentNullException(nameof(signature));
            if (message == null) throw new ArgumentNullException(nameof(message));
            if (exchangePublicKey == null) throw new ArgumentNullException(nameof(exchangePublicKey));
            if (digitalSignature == null) throw new ArgumentNullException(nameof(digitalSignature));

            lock (_thisLock)
            {
                var item = new BackgroundUploadItem();

                item.State = BackgroundUploadState.Encoding;
                item.Signature = signature;
                item.Message = message;
                item.Scheme = "Unicast";
                item.Type = "Message";
                item.CreationTime = DateTime.UtcNow;
                item.Depth = 1;
                item.BlockLength = 1024 * 1024 * 1;
                item.CorrectionAlgorithm = CorrectionAlgorithm.ReedSolomon8;
                item.HashAlgorithm = HashAlgorithm.Sha256;
                item.ExchangePublicKey = exchangePublicKey;
                item.DigitalSignature = digitalSignature;

                _settings.UploadItems.Add(item);
            }
        }