public async Task <IActionResult> Edit(int id, [Bind("Id,Name")] CertificationResult certificationResult)
        {
            if (id != certificationResult.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(certificationResult);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!CertificationResultExists(certificationResult.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction("Index"));
            }
            return(View(certificationResult));
        }
        public async Task <IActionResult> Create([Bind("Id,Name")] CertificationResult certificationResult)
        {
            if (ModelState.IsValid)
            {
                _context.Add(certificationResult);
                await _context.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }
            return(View(certificationResult));
        }
Beispiel #3
0
        private async Task ClientLoop(CancellationToken token)
        {
            try
            {
                while (true)
                {
                    Message message;
                    try
                    {
                        var t = new Task <Message>(() => mes.WaitForMessage(), token);
                        t.Start();

                        message = await t;
                    }
                    catch (GracefulShutdownException)
                    {
                        log.LogInformation("Client disconnected");

                        Dispose();
                        return;
                    }
                    catch (Exception e)
                    {
                        log.LogError(e, "Error during receive, shutting down");
                        try
                        {
                            mes.SendMessage(new Message(Message.MessageType.None, true, e.ToString()));
                        }
                        catch { }

                        Dispose();
                        return;
                    }



                    if (message.IsError)
                    {
                        log.LogInformation("Encountered a remote error:\n" + message);
                        stream.Close();
                    }

                    #region user is not authed
                    if (!IsAuthenticated)
                    {
                        switch (message.Type)
                        {
                        case Message.MessageType.CertRequest:

                            if (DoTwoFaCheckIfNeeded(message[0]))
                            {
                                if (db.IsValid(message[0], message[1]))
                                {
                                    CertificationResult result = certificateHandler.Certify(new[] { message[2] as byte[], message[3] as byte[] });
                                    db.SetSerial(message[0], result.SerialNumber, result.ValidUntil, message[4]);

                                    mes.SendMessage(new Message(Message.MessageType.CertSuccess, false, result.Rawdata));
                                    log.LogDebug("Authenticated with a Public Key");
                                    break;
                                }
                            }
                            mes.SendMessage(new Message(Message.MessageType.CertSuccess, true, "Login data not correct"));
                            log.LogInformation("Failed to authenticate a Public Key");

                            break;

                        default:
                            mes.SendMessage(new Message(Message.MessageType.Sequence, true, "Not authenticated"));
                            break;
                        }
                    }
                    #endregion
                    else
                    {
                        switch (message.Type)
                        {
                        case Message.MessageType.CertRevokeRequest:
                            db.Deauthenticate();
                            IsAuthenticated = false;
                            mes.SendMessage(new Message(Message.MessageType.CertRevokeSuccess));

                            log.LogInformation("Deauthenticated");

                            break;


                        case Message.MessageType.CertRenewRequest:
                            CertificationResult result = certificateHandler.Certify(new[] { message[0] as byte[], message[1] as byte[] });
                            db.SetSerial(db.Name, result.SerialNumber, result.ValidUntil, message[2]);
                            db.RemoveSerial(db.SerialNumber);

                            mes.SendMessage(new Message(Message.MessageType.CertRenewSuccess, false, result.Rawdata));

                            log.LogInformation("Certificate renewed");

                            break;

                        case Message.MessageType.FileUploadRequest:

                            (bool, string)returned = await FileHandler.ReceiveFile(message, mes, db);

                            if (returned.Item1)
                            {
                                log.LogInformation("Uploaded a file: ", returned.Item2.Substring(0, 10));
                            }
                            else
                            {
                                log.LogInformation("Failed to upload a file");
                            }

                            break;

                        case Message.MessageType.FileListRequest:

                            List <File> fileList = db.GetFiles();

                            var Fields = new dynamic[1 + fileList.Count];
                            Fields[0] = fileList.Count;

                            for (int i = 0; i < fileList.Count; i++)
                            {
                                Fields[i + 1] = new Message(Message.MessageType.FileInfo, false, fileList[i].Filename ?? "", fileList[i].Extension ?? "", fileList[i].Identifier, fileList[i].Size, fileList[i].Path ?? "");
                            }

                            mes.SendMessage(new Message(Message.MessageType.FileList, false, Fields));

                            log.LogDebug("Listed files");

                            break;

                        case Message.MessageType.ShareListRequest:

                            List <Share> shareList = db.GetShares(message[0]);

                            Fields    = new dynamic[1 + shareList.Count];
                            Fields[0] = shareList.Count;

                            for (int i = 0; i < shareList.Count; i++)
                            {
                                Fields[i + 1] = new Message(Message.MessageType.FileInfo, false, shareList[i].Identifier, shareList[i].File.Identifier, shareList[i].FirstView, shareList[i].Public, shareList[i].PublicRegistered, shareList[i].Whitelisted, shareList[i].WhitelistText);
                            }

                            mes.SendMessage(new Message(Message.MessageType.FileList, false, Fields));

                            log.LogDebug("Listed shares for file: " + message[0] as string);

                            break;

                        case Message.MessageType.ShareRequest:

                            string id = db.SetFileShare(
                                message[0],
                                message[1] == 1,
                                message[2] == 1,
                                message[3] == 1,
                                message[4] == 1,
                                (message[5] as string).Split(';', StringSplitOptions.RemoveEmptyEntries)
                                );

                            mes.SendMessage(new Message(Message.MessageType.ShareResponse, false, id));

                            log.LogInformation("Created a share: " + id);

                            break;

                        case Message.MessageType.DeleteFile:
                            string file_identifier = message.Fields[0];
                            try
                            {
                                //delete the file from the fs
                                FileHandler.DeleteFile(file_identifier);
                                //delete all database records of it
                                db.DeleteFile(file_identifier);
                                mes.SendMessage(new Message(Message.MessageType.DeleteFile, false, ""));
                                log.LogInformation("Deleted file: " + file_identifier.Substring(0, 10));
                            }
                            catch (Exception e)
                            {
                                if (e.GetType() != typeof(UnauthorizedAccessException) && e.GetType() != typeof(ArgumentException))
                                {
                                    log.LogError(e, "Error while deleting file");
                                }
                                log.LogWarning("Failed to delete file: " + file_identifier.Substring(0, 10));
                                mes.SendMessage(new Message(Message.MessageType.DeleteFile, true, "Error deleting"));
                            }
                            break;

                            #region TwoFa
                        case Message.MessageType.TwoFactorAdd:
                            if (!db.HasTwoFa())
                            {
                                string secret = TwoFactorHandler.CreateSecret();
                                mes.SendMessage(new Message(Message.MessageType.TwoFactorAdd, false, TwoFactorHandler.GenerateQrCode(db.Name, secret)));
                                db.SetTwoFactorSecret(secret);
                                log.LogInformation("Added Two fa");
                            }
                            else
                            {
                                mes.SendMessage(new Message(Message.MessageType.TwoFactorAdd, true));
                                log.LogInformation("Two Fa already exists");
                            }

                            break;

                        case Message.MessageType.TwoFactorRemove:
                            try
                            {
                                if (DoTwoFaCheckIfNeeded())
                                {
                                    db.RemoveTwoFactorSecret();
                                    mes.SendMessage(new Message(Message.MessageType.TwoFactorRemove, false));
                                }
                            }
                            catch (UnauthorizedAccessException e)
                            {
                                log.LogError(e, "Error while removing Two Fa");
                                Dispose();
                            }

                            break;

                        case Message.MessageType.HasTwoFactor:
                            try
                            {
                                if (db.HasTwoFa())
                                {
                                    //has secret
                                    mes.SendMessage(new Message(Message.MessageType.HasTwoFactor, false, 1));
                                }
                                else
                                {
                                    //no secret
                                    mes.SendMessage(new Message(Message.MessageType.HasTwoFactor, false, 0));
                                }
                            }
                            catch (Exception e)
                            {
                                log.LogError(e, "Error while testing if user has TwoFa");
                                mes.SendMessage(new Message(Message.MessageType.HasTwoFactor, true));
                            }
                            break;

                            #endregion
                        default:
                            mes.SendMessage(new Message(Message.MessageType.Sequence, true, "Not expected"));
                            break;
                        }
                    }
                }
            }
            catch (SequenceBreakException e)
            {
                log.LogError(e, "Sequence break, Shutting down client");
                try
                {
                    mes.SendMessage(new Message(Message.MessageType.Sequence, true, e.ToString()));
                }
                catch { }
                Dispose();
            }
            catch (Exception e)
            {
                log.LogError(e, "Shutting down client");
                try
                {
                    mes.SendMessage(new Message(Message.MessageType.None, true, e.ToString()));
                }
                catch { }

                Dispose();
            }
        }