示例#1
0
        public async Task <ActionResult <MainResponse> > CreateChat(CreateChatRequest request)
        {
            User user = HttpContext.GetUser();

            if (user == null || user.KeySession == null)
            {
                return(Unauthorized());
            }

            int[] keyIds = request.EncryptedPayload.Select(p => p.KeyId).ToArray();

            Chat chat = new Chat()
            {
                Id = RandomUtilities.GetCryptoRandomString(20),
                EncryptionChatDatas = new List <EncryptionChatData>(),
                Image           = "",
                LastMessageDate = DateTime.Now,
                LastMessageId   = null,
                Messages        = new List <Message>(),
            };

            _context.Chats.Add(chat);
            await _context.SaveChangesAsync();

            User[] users = await _context.Users.Where(p => keyIds.Contains(p.RSAKeyPair.Id)).ToArrayAsync();

            EncryptionChatData[] encryptionChatDatas = new EncryptionChatData[users.Length + 1];

            CreateChatEncryptedPayload encryptedPayload;

            for (int i = 0; i < users.Length; i++)
            {
                encryptedPayload       = request.EncryptedPayload.First(p => p.KeyId == users[i].RSAKeyPair.Id);
                encryptionChatDatas[i] = new EncryptionChatData()
                {
                    AESEncryptedKey    = new AESEncryptedData(),
                    Chat               = chat,
                    EncryptedTitle     = new AESEncryptedData(),
                    KeyTransferPayload = encryptedPayload.PayLoad,
                    RSAEncryptedKey    = encryptedPayload.EncryptedChatKey,
                    RSAKeyPair         = users[i].RSAKeyPair
                };
            }

            encryptionChatDatas[^ 1] = new EncryptionChatData()
示例#2
0
        public async Task <ActionResult <MainResponse> > ConfirmChat(ConfirmChatRequest request)
        {
            User user = HttpContext.GetUser();

            if (user == null || user.KeySession == null)
            {
                return(Unauthorized());
            }

            Chat chat = await _context.Chats.FirstOrDefaultAsync(p => p.Id == request.ChatId);

            if (chat == null)
            {
                return(MainResponse.GetError(Enums.RequestError.ChatNotFound));
            }

            EncryptionChatData encryptionChatData = chat.EncryptionChatDatas.FirstOrDefault(p => p.RSAKeyPair == user.RSAKeyPair);

            if (encryptionChatData == null)
            {
                return(MainResponse.GetError(Enums.RequestError.ChatKeyNotFound));
            }

            encryptionChatData.KeySession         = user.KeySession;
            encryptionChatData.KeyTransferPayload = null;
            encryptionChatData.RSAEncryptedKey    = null;
            encryptionChatData.SessionKeySign     = request.SessionChatKeySign;
            encryptionChatData.UserKeySign        = request.UserChatKeySign;
            encryptionChatData.AESEncryptedKey    = request.EncryptedChatKey;
            encryptionChatData.EncryptedTitle     = request.EncryptedTitle;

            _context.Entry(encryptionChatData).Reference(a => a.AESEncryptedKey).TargetEntry.State = EntityState.Modified;
            _context.Entry(encryptionChatData).Reference(a => a.EncryptedTitle).TargetEntry.State  = EntityState.Modified;

            UserEvent userEvent = await _context.UserEvents.FirstOrDefaultAsync(p => p.Params.FirstOrDefault(p => p.Alias == "chatId").Value == chat.Id);

            _context.UserEvents.Remove(userEvent);

            await _context.SaveChangesAsync().ConfigureAwait(false);

            return(MainResponse.GetSuccess());
        }
示例#3
0
        public async Task <ActionResult <MainResponse> > UnconfirmedInfo(GetUnconfirmedChatInfoRequest request)
        {
            User user = HttpContext.GetUser();

            if (user == null)
            {
                return(Unauthorized());
            }

            Chat chat = await _context.Chats.FirstOrDefaultAsync(p => p.Id == request.ChatId);

            if (chat == null)
            {
                return(MainResponse.GetError(Enums.RequestError.ChatNotFound));
            }

            EncryptionChatData chatData = chat.EncryptionChatDatas.FirstOrDefault(p => p?.RSAKeyPair?.Id == user.RSAKeyPair.Id);

            if (chatData == null)
            {
                return(MainResponse.GetError(Enums.RequestError.ChatKeyNotFound));
            }

            if (chatData.AESEncryptedKey != null)
            {
                return(MainResponse.GetError(Enums.RequestError.ChatAlreadyConfirmed));
            }

            GetUnconfirmedChatInfoResponse response = new GetUnconfirmedChatInfoResponse()
            {
                Payload             = chatData.KeyTransferPayload,
                RSAEncryptedChatKey = chatData.RSAEncryptedKey
            };

            return(MainResponse.GetSuccess(response));
        }