public HomeController(ILogger <HomeController> logger, IWebHostEnvironment env, PersistentStores stores, IDataProtectionProvider provider) { _logger = logger; contentRootPath = env.ContentRootPath; stores.SetContentRootPath(env.ContentRootPath); _conf = stores.Configuration; secureBallot = stores.SecureBallot; egSecureBallot = stores.EGSecureBallot; dataProtector = provider; var confAPI = new VotingSystemConfiguration(); lock (_conf) { var v = _conf.Get(APIConfigurationKey); if (v != null) { confAPI = VotingSystemConfiguration.FromJson(v); } } if (confAPI.GuardianAPI != null && confAPI.MediatorAPI != null) { GuardianApi = new ElectionGuard.GuardianClient(confAPI.GuardianAPI); MediatorApi = new ElectionGuard.MediatorClient(confAPI.MediatorAPI); } }
public IActionResult Index() { if (!HttpContext.Session.Keys.Contains(IsAuthenticated)) { return(RedirectToAction("Signin")); } var electionDataAvailable = false; var confAPI = new VotingSystemConfiguration(); lock (_conf) { electionDataAvailable = _conf.Get(ESElectionConfigurationKey) != null; var v = _conf.Get(APIConfigurationKey); if (v != null) { confAPI = VotingSystemConfiguration.FromJson(v); } } ViewData["ESElectionConfigured"] = electionDataAvailable; ViewData["ESApiEndPointConfigured"] = confAPI.ElectionSystemAPI != null; return(View(confAPI)); }
public IActionResult LoadESElectionConfiguration() { if (!HttpContext.Session.Keys.Contains(IsAuthenticated)) { return(RedirectToAction("Signin")); } var confAPI = new VotingSystemConfiguration(); lock (_conf) { if (_conf.Get(ESElectionConfigurationKey) != null) { return(new ForbidResult()); } var v = _conf.Get(APIConfigurationKey); if (v != null) { confAPI = VotingSystemConfiguration.FromJson(v); } } var urlBuilder = new System.Text.StringBuilder(); urlBuilder.Append(confAPI.ElectionSystemAPI.TrimEnd('/')).Append("/RunningElections"); var req = WebRequest.Create(urlBuilder.ToString()); var resp = req.GetResponse(); var pdata = (new StreamReader(resp.GetResponseStream())).ReadToEnd(); var dp = dataProtector.CreateProtector("EligereMetadataExchange"); var data = dp.Unprotect(pdata); var fn = Path.Combine(contentRootPath, $"wwwroot/temp/log.txt"); System.IO.File.WriteAllText(fn, data); var eldesc = JsonSerializer.Deserialize <ElectionGuard.ElectionDescription>(data); lock (_conf) { _conf.Put(ESElectionConfigurationKey, JsonSerializer.Serialize <ElectionGuard.ElectionDescription>(eldesc)); } return(View("ShowESElectionConfiguration", eldesc)); }
public IActionResult CastBallot(string tickets, string election, string ballotType, string preferences) { var encTickets = Convert.FromBase64String(tickets); var protector = dataprotection.CreateProtector("EVSKeyExchange"); var plainSerTickets = Encoding.UTF8.GetString(protector.Unprotect(encTickets)); var ticketList = JsonSerializer.Deserialize <List <VoteTicket> >(plainSerTickets); string d; lock (secureBallot) { if (secureBallot.Get(HomeController.VotingForTallyClosedKey) != null) { return(NotFound("Election Voting System is closed after tally")); } } lock (configuration) { d = configuration.Get(HomeController.ESElectionConfigurationKey); } var confAPI = new VotingSystemConfiguration(); lock (configuration) { var v = configuration.Get(HomeController.APIConfigurationKey); if (v != null) { confAPI = VotingSystemConfiguration.FromJson(v); } } var electionDescription = JsonSerializer.Deserialize <ElectionGuard.ElectionDescription>(d); var contests = electionDescription.contests.ToDictionary(v => v.object_id); if (ticketList.Where(t => t.ElectionId == election).Count() > 1) { return(Json(new CastBallotResult() { Status = 500, Message = "Internal error: more tickets for a single election" })); } var ticket = ticketList.Where(t => t.ElectionId == election).FirstOrDefault(); if (ticket == null) { return(Json(new CastBallotResult() { Status = 403, Message = "No ticket for election" })); } lock (ticketsDb) { if (ticketsDb.Get(ticket.HashId) != null) { return(Json(new CastBallotResult() { Status = 403, Message = "Ticket already used" })); } } if (contests.ContainsKey(election)) { var el = contests[election]; var candidates = electionDescription.candidates.ToDictionary(v => v.object_id); switch (ballotType) { case "emptyBallot": CastVote(ticket, "$blank$"); break; case "spoiledBallot": CastVote(ticket, "$spoil$"); break; default: var prefs = JsonSerializer.Deserialize <string[]>(preferences); var partyel = (el.extensions["CandidatesType"] == "Party"); if (partyel) { var partycount = 0; foreach (var pref in prefs) { var cand = el.ballot_selections.Where(c => candidates[c.candidate_id].ballot_name.text[0].value == pref).FirstOrDefault(); if (cand.object_id[0] != '*') { partycount++; } } // Check can be improved if (partycount > 1) { return(Json(new CastBallotResult() { Status = 403, Message = "Too many parties selected (only 1 is allowed)" })); } } var extracount = partyel ? 1 : 0; // If party election one is for the party if (prefs.Length > el.votes_allowed + extracount) { return(Json(new CastBallotResult() { Status = 403, Message = $"Too many votes expressed ({prefs.Length}) with respect to the maximum ({el.votes_allowed})" })); } foreach (var pref in prefs) { var cand = el.ballot_selections.Where(c => candidates[c.candidate_id].ballot_name.text[0].value == pref).FirstOrDefault(); if (cand == null) { return(Json(new CastBallotResult() { Status = 403, Message = "Invalid candidate" })); } } foreach (var pref in prefs) { CastVote(ticket, pref); } break; } } else { return(Json(new CastBallotResult() { Status = 500, Message = "Invalid election id" })); } var dp = dataprotection.CreateProtector("EligereMetadataExchange"); var secretHash = dp.Protect(ticket.HashId); var urlBuilder = new System.Text.StringBuilder(); urlBuilder.Append(confAPI.ElectionSystemAPI.TrimEnd('/')).Append("/TicketUsed/" + secretHash); var req = WebRequest.Create(urlBuilder.ToString()); var resp = req.GetResponse(); var text = new StreamReader(resp.GetResponseStream()).ReadToEnd(); resp.Close(); var ret = new CastBallotResult() { Status = 200, Message = "Vote casted" }; return(Json(ret)); }