private void postNext(String questionsFromServer) { Message questions = new Message(questionsFromServer); if (!questions.Verify(serverKey)) { Error("Message from server is tampered with."); currentPanel.Enabled = true; return; } Question.Text = questions.getCleartext(); switchPanel(questionsPanel); SetStatus(""); }
private String FetchPublicKey(String userID) { String publicKey; while (userID == null || userID == "") { Console.Write("Sender: "); userID = Console.ReadLine(); userID.Trim(); } try { Connect(); } catch (Exception e) { Console.Error.WriteLine("Error: {0}", e.Message); return(null); } try { publicKey = server.KeyObt(userID); } catch (Exception e) { Console.Error.WriteLine("Error: {0}", e.Message); return(null); } Message message = new Message(publicKey); if (message.Verify(serverKey)) { return(Encoding.UTF8.GetString(message.Cleartext)); } else { Console.Error.WriteLine("WARNING: Message from server is tampered with."); return(null); } }
public bool KeyUpdate(String userID, String email, Message signedMessage) { ActionLog_Write(email + ": KeyUpdate"); Console.WriteLine(beginProtocol); Console.WriteLine(email + ": KeyUpdate"); DatabaseConnection connection = new DatabaseConnection(); String publicKey = connection.getPublicKey(email); if (signedMessage.Verify(publicKey)) { if (DateTime.Compare(signedMessage.Time, DateTime.Now.AddHours(-1)) >= 0) { bool result = connection.updatePublicKey(userID, email, signedMessage.getCleartext()); connection.close(); return(result); } } connection.close(); throw new Exception("Incorrect message"); }
private bool Verify(Message message, String sender, bool includeComments) { String publicKey = FetchPublicKey(sender); if (publicKey == null) { return(false); } bool result = message.Verify(publicKey, includeComments); if (result) { Console.Error.WriteLine("Message verification succeeded."); } else { Console.Error.WriteLine("Message verification failed."); } return(result); }
public String KeyObt(String email, DateTime date) //get public key of a user ( complete ) { ActionLog_Write(email + ": KeyObt"); Console.WriteLine(beginProtocol); Console.WriteLine(email + ": KeyObt"); int index = email.IndexOf('@'); String domainName = email.Substring(index, email.Length - index); String publicKey = null; Core core = new Core(Server.passphrase); if (core.GetXmlNodeInnerText("domain") == domainName) { DatabaseConnection connection = new DatabaseConnection(); publicKey = connection.getPublicKey(email, date); connection.close(); } else { byte[] rawCertData = Certificate.SearchCertificate(domainName); if (rawCertData == null) { if (ConnectRootServer(core.GetXmlNodeInnerText("root_server"))) { if (GetCertificate(domainName)) { rawCertData = Certificate.SearchCertificate(domainName); } } } byte[] foreignServerCertPK = Certificate.GetPublicKey(rawCertData); String foreignServerXmlPK = Crypto.CertToXMLKey(foreignServerCertPK); Console.WriteLine("Public key of " + domainName + ":\n" + foreignServerXmlPK); String foreignServerHost = Certificate.GetHostName(rawCertData); ActionLog_Write("Connecting to foreign PractiSES server (" + foreignServerHost + ")..."); Console.WriteLine("Connecting to foreign PractiSES server ({0})...", foreignServerHost); IServer foreignServer = (IServer)Activator.GetObject(typeof(IServer), "http://" + foreignServerHost + "/PractiSES"); String signedPublicKey = foreignServer.KeyObt(email, date); if (signedPublicKey != null) { Message foreignmessage = new Message(signedPublicKey); //**************** if (foreignmessage.Verify(foreignServerXmlPK)) { publicKey = foreignmessage.getCleartext(); } //**************** } } if (publicKey == null) { ActionLog_Write("Error - " + email + ": Email does not exist!"); Console.WriteLine("Error - " + email + ": Email does not exist!"); throw new Exception("Invalid user"); } Message message = new Message(publicKey); message.AddComment("Email", email); message.Sign(core.PrivateKey); String result = message.ToString(); return(result); }
private bool Verify(Message message, String sender, bool includeComments) { String publicKey = FetchPublicKey(sender); if (publicKey == null) return false; bool result = message.Verify(publicKey, includeComments); if (result) Console.Error.WriteLine("Message verification succeeded."); else Console.Error.WriteLine("Message verification failed."); return result; }
private void Initialize(String passphrase) { if (File.Exists(core.KeyFile)) { Console.Write("Are you ABSOLUTELY sure that you want to delete your existing keys FOREVER? (y/N): "); String response = Console.ReadLine(); response.Trim(); if (response == "y") File.Delete(core.KeyFile); else return; } try { core.InitializeKeys(passphrase); } catch { Console.Error.WriteLine("Invalid passphrase"); return; } Console.Write("Username: "******"Email: "); String email = Console.ReadLine(); StreamWriter sw = new StreamWriter(Path.Combine(core.ApplicationDataFolder, "identity")); sw.WriteLine(username); sw.WriteLine(email); sw.Close(); Connect(); String questionsFromServer; try { questionsFromServer = server.InitKeySet_AskQuestions(username, email); } catch (Exception e) { Console.Error.WriteLine(e.Message); return; } Message questions = new Message(questionsFromServer); if (!questions.Verify(serverKey)) { Console.Error.WriteLine("WARNING: Message from server is tampered with."); Console.Error.WriteLine(questionsFromServer); return; } Console.WriteLine("Questions:"); Console.WriteLine(questions.getCleartext()); Console.Write("Answers: "); String answers = Console.ReadLine(); byte[] message = Encoding.UTF8.GetBytes(answers); Rijndael aes = Rijndael.Create(); String encrypted = Crypto.Encrypt(message, serverKey, aes); ArrayList key = new ArrayList(); key.AddRange(aes.Key); key.AddRange(aes.IV); File.WriteAllBytes(Path.Combine(core.ApplicationDataFolder, "answers.key"), (byte[]) key.ToArray(Type.GetType("System.Byte"))); try { server.InitKeySet_EnvelopeAnswers(username, email, encrypted); } catch (Exception e) { Console.Error.WriteLine(e.Message); return; } Console.Error.WriteLine("Answers sent. Please check your email to finalize PractiSES initialization."); }
private String FetchPublicKey(String userID) { String publicKey; while (userID == null || userID == "") { Console.Write("Sender: "); userID = Console.ReadLine(); userID.Trim(); } try { Connect(); } catch (Exception e) { Console.Error.WriteLine("Error: {0}", e.Message); return null; } try { publicKey = server.KeyObt(userID); } catch (Exception e) { Console.Error.WriteLine("Error: {0}", e.Message); return null; } Message message = new Message(publicKey); if (message.Verify(serverKey)) { return Encoding.UTF8.GetString(message.Cleartext); } else { Console.Error.WriteLine("WARNING: Message from server is tampered with."); return null; } }
//get public key of a user ( complete ) public String KeyObt(String email, DateTime date) { ActionLog_Write(email + ": KeyObt"); Console.WriteLine(beginProtocol); Console.WriteLine(email + ": KeyObt"); int index = email.IndexOf('@'); String domainName = email.Substring(index, email.Length - index); String publicKey = null; Core core = new Core(Server.passphrase); if (core.GetXmlNodeInnerText("domain") == domainName) { DatabaseConnection connection = new DatabaseConnection(); publicKey = connection.getPublicKey(email, date); connection.close(); } else { byte[] rawCertData = Certificate.SearchCertificate(domainName); if (rawCertData == null) { if (ConnectRootServer(core.GetXmlNodeInnerText("root_server"))) { if (GetCertificate(domainName)) { rawCertData = Certificate.SearchCertificate(domainName); } } } byte[] foreignServerCertPK = Certificate.GetPublicKey(rawCertData); String foreignServerXmlPK = Crypto.CertToXMLKey(foreignServerCertPK); Console.WriteLine("Public key of " + domainName + ":\n" + foreignServerXmlPK); String foreignServerHost = Certificate.GetHostName(rawCertData); ActionLog_Write("Connecting to foreign PractiSES server (" + foreignServerHost + ")..."); Console.WriteLine("Connecting to foreign PractiSES server ({0})...", foreignServerHost); IServer foreignServer = (IServer)Activator.GetObject(typeof(IServer), "http://" + foreignServerHost + "/PractiSES"); String signedPublicKey = foreignServer.KeyObt(email, date); if (signedPublicKey != null) { Message foreignmessage = new Message(signedPublicKey); //**************** if (foreignmessage.Verify(foreignServerXmlPK)) { publicKey = foreignmessage.getCleartext(); } //**************** } } if (publicKey == null) { ActionLog_Write("Error - " + email + ": Email does not exist!"); Console.WriteLine("Error - " + email + ": Email does not exist!"); throw new Exception("Invalid user"); } Message message = new Message(publicKey); message.AddComment("Email",email); message.Sign(core.PrivateKey); String result = message.ToString(); return result; }
public bool KeyUpdate(String userID, String email, Message signedMessage) { ActionLog_Write(email + ": KeyUpdate"); Console.WriteLine(beginProtocol); Console.WriteLine(email + ": KeyUpdate"); DatabaseConnection connection = new DatabaseConnection(); String publicKey = connection.getPublicKey(email); if (signedMessage.Verify(publicKey)) { if (DateTime.Compare(signedMessage.Time, DateTime.Now.AddHours(-1)) >= 0) { bool result = connection.updatePublicKey(userID, email, signedMessage.getCleartext()); connection.close(); return result; } } connection.close(); throw new Exception("Incorrect message"); }
private void Initialize(String passphrase) { if (File.Exists(core.KeyFile)) { Console.Write("Are you ABSOLUTELY sure that you want to delete your existing keys FOREVER? (y/N): "); String response = Console.ReadLine(); response.Trim(); if (response == "y") { File.Delete(core.KeyFile); } else { return; } } try { core.InitializeKeys(passphrase); } catch { Console.Error.WriteLine("Invalid passphrase"); return; } Console.Write("Username: "******"Email: "); String email = Console.ReadLine(); StreamWriter sw = new StreamWriter(Path.Combine(core.ApplicationDataFolder, "identity")); sw.WriteLine(username); sw.WriteLine(email); sw.Close(); Connect(); String questionsFromServer; try { questionsFromServer = server.InitKeySet_AskQuestions(username, email); } catch (Exception e) { Console.Error.WriteLine(e.Message); return; } Message questions = new Message(questionsFromServer); if (!questions.Verify(serverKey)) { Console.Error.WriteLine("WARNING: Message from server is tampered with."); Console.Error.WriteLine(questionsFromServer); return; } Console.WriteLine("Questions:"); Console.WriteLine(questions.getCleartext()); Console.Write("Answers: "); String answers = Console.ReadLine(); byte[] message = Encoding.UTF8.GetBytes(answers); Rijndael aes = Rijndael.Create(); String encrypted = Crypto.Encrypt(message, serverKey, aes); ArrayList key = new ArrayList(); key.AddRange(aes.Key); key.AddRange(aes.IV); File.WriteAllBytes(Path.Combine(core.ApplicationDataFolder, "answers.key"), (byte[])key.ToArray(Type.GetType("System.Byte"))); try { server.InitKeySet_EnvelopeAnswers(username, email, encrypted); } catch (Exception e) { Console.Error.WriteLine(e.Message); return; } Console.Error.WriteLine("Answers sent. Please check your email to finalize PractiSES initialization."); }