private string JoinIVToEncryptedMessage(byte[] encryptedMessage, byte[] AESIV) { var message = SecurityWrapper.BytesToString(encryptedMessage); var iv = SecurityWrapper.BytesToString(AESIV); return(iv + message); }
private string GetReadyToSendMessage(string serializedBaseMessage, byte[] AESKey) { var AESIV = SecurityWrapper.GetRandomBytes(); var encryptedMessage = EncryptMessage(serializedBaseMessage, AESKey, AESIV); return(JoinIVToEncryptedMessage(encryptedMessage, AESIV)); }
private Stream GetWrappedStream() { if (_wrappedStream == null) { var stream = GetStream(); _wrappedStream = SecurityWrapper == null ? stream : SecurityWrapper.Wrap(stream); } return(_wrappedStream); }
public void SendMessage(string serializedMessage) { var AESIV = SecurityWrapper.GetRandomBytes(); var encryptedMessage = AESWrapper.EncryptStringToBytes(serializedMessage, AESKey, AESIV); var message = JoinIVToEncryptedMessage(encryptedMessage, AESIV); Writer.WriteLine(message); Writer.Flush(); }
private byte[] GetEncryptedMessageFromInput(string input) { if (input.Length >= 24) { string encryptedMessage = input.Substring(24); return(SecurityWrapper.StringToBytes(encryptedMessage)); } else { throw new InvalidOperationException(); } }
private byte[] GetIVFromInput(string input) { if (input.Length >= 24) { string base64IV = input.Substring(0, 24); return(SecurityWrapper.StringToBytes(base64IV)); } else { throw new InvalidOperationException(); } }
/// <summary> /// for testing purposes. this is of course useless when compiled as .dll /// left in here for quick experiments/tests /// </summary> /// <param name="args"></param> public static void Main(String[] args) { SecurityWrapper.CopyAndEncrypt(@"Z:\out.txt", @"Z:\out_enc.txt", Util.GetBytes("1234567890123456")); Console.ReadKey(); }
private static PolicyHelper FetchPolicy(string URL) { MessageProcessor m = MessageProcessor.Instance; if (config.OperatingMode == ConfigLoader.OperationMode.OFFLINE) return GetDefaultPolicy(URL); PolicyHelper myPolicy = null; //first lets check our policy cache, if we don't have one cached or it's out of date, update it. lock (policyCache) { myPolicy = (PolicyHelper)policyCache[URL]; if (myPolicy == null) { switch (config.PCSalgo) { case ConfigLoader.Algorithm.FAILOVER: for (int i = 0; i < config.pcsurl.Count; i++) { if (polservice != null) { try { ((IClientChannel)polservice).Close(); ((IClientChannel)polservice).Dispose(); } catch (Exception ex) { //log.TraceEvent(TraceEventType.Warning, 0, "Uh oh" + ex.Message + ex.StackTrace); } } polservice = config.GetPCSProxy(config.pcsurl[i]); for (int k = 0; k < config.PCSretrycount; k++) { try { ServicePolicyRequestMsg requestr = new ServicePolicyRequestMsg(); //requestr = new ServicePolicyRequestMsg(); requestr.URI = URL; //remember, .NET/IIS is not case sensitive // ServicePolicyRequestMsg r = new ServicePolicyRequestMsg(); requestr.classification = currentlevel; ServicePolicyResponseMsg responsep1 = polservice.GetServicePolicy(requestr); PolicyHelper temp = new PolicyHelper(); temp.lastUpdate = DateTime.Now; if (responsep1.policy.GetType() != typeof(TransactionalWebServicePolicy)) { Logger.info("FGSMS.MessageProcessor" + "Successfully retrieved a policy from the policy configuration service, however it was of an unexpected type " + URL + " type info " + responsep1.policy.GetType().FullName); throw new ArgumentOutOfRangeException(); } temp.policy = (TransactionalWebServicePolicy)responsep1.policy; MAXCAP = temp.policy.RecordedMessageCap; Enabled = temp.policy.AgentsEnabled; try { policyCache.Add(URL, temp); } catch (Exception e) { Logger.debug(e, "error caught adding " + URL + " to the policy cache"); } currentlevel = responsep1.classification; Logger.info("FGSMS.MessageProcessor" + "Successfully Refreshed policy from the policy configuration service for " + URL); myPolicy = policyCache[URL] as PolicyHelper; return myPolicy; } //{"There was no endpoint listening at http://localhost:8080/FGSMSServices/PCS that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details."} catch (EndpointNotFoundException ex) { _lasterror = ex.GetType().FullName + " " + ex.Message; Logger.info(ex, "FGSMS.MessageProcessor" + "unable to transmit to " + config.pcsurl[i]); //this means that the current PCS url is not functioning, try moving to the next one //polservice = config.GetPCSProxy(); } catch (Exception ex) { Logger.info(ex, "FGSMS.MessageProcessor" + "unable to transmit to " + config.pcsurl[i] + " Policy Updater - failed to policy from the policy configuration service for " + URL + " check that the URL and credentials are correct"); } } } break; case ConfigLoader.Algorithm.ROUNDROBIN: for (int k = 0; k < config.PCSretrycount; k++) { for (int i = 0; i < config.pcsurl.Count; i++) { try { if (polservice != null) { try { ((IClientChannel)polservice).Close(); ((IClientChannel)polservice).Dispose(); } catch (Exception ex) { // log.TraceEvent(TraceEventType.Warning, 0, "Uh oh" + ex.Message + ex.StackTrace); } } polservice = config.GetPCSProxy(config.pcsurl[i]); ServicePolicyRequestMsg requestr = new ServicePolicyRequestMsg(); //requestr = new ServicePolicyRequestMsg(); requestr.URI = URL; //remember, .NET/IIS is not case sensitive // GetServicePolicyRequest r = new GetServicePolicyRequest(); // r.request = requestr; requestr.classification = currentlevel; ServicePolicyResponseMsg responsep1 = polservice.GetServicePolicy(requestr); PolicyHelper temp = new PolicyHelper(); temp.lastUpdate = DateTime.Now; if (responsep1.policy.GetType() != typeof(TransactionalWebServicePolicy)) { Logger.info("FGSMS.MessageProcessor" + "Successfully retrieved a policy from the policy configuration service, however it was of an unexpected type " + URL + " type info " + responsep1.policy.GetType().FullName); throw new ArgumentOutOfRangeException(); } temp.policy = (TransactionalWebServicePolicy)responsep1.policy; MAXCAP = temp.policy.RecordedMessageCap; Enabled = temp.policy.AgentsEnabled; try { policyCache.Add(URL, temp); } catch (Exception e) { Logger.debug(e, "error caught adding " + URL + " to the policy cache"); //its possible that there is another instance of this class running, even through there isn't supposed to be. } currentlevel = responsep1.classification; Logger.info("FGSMS.MessageProcessor" + "Successfully Refreshed policy from the policy configuration service for " + URL); myPolicy = policyCache[URL] as PolicyHelper; return myPolicy; } //{"There was no endpoint listening at http://localhost:8080/FGSMSServices/PCS that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details."} catch (EndpointNotFoundException ex) { _lasterror = ex.GetType().FullName + " " + ex.Message; //this means that the current PCS url is not function, try moving to the next one //polservice = config.GetPCSProxy(); Logger.info(ex, "FGSMS.MessageProcessor" + "unable to transmit to " + config.pcsurl[i]); } catch (Exception ex) { Logger.info(ex, "FGSMS.MessageProcessor" + "unable to transmit to " + config.pcsurl[i] + " Policy Updater - failed to policy from the policy configuration service for " + URL + " check that the URL and credentials are correct"); } } } break; } if (myPolicy == null) { Logger.error("FGSMS.MessageProcessor" + "Cannot contact the policy config service. Reverting to default policy. "); return GetDefaultPolicy(URL); } } } TimeSpan times = XmlConvert.ToTimeSpan(myPolicy.policy.PolicyRefreshRate); if ((DateTime.Now.Ticks - myPolicy.lastUpdate.Ticks) > times.Ticks) { //TODO load balance this //if the policy is out of date, update it. try { //policyConfigurationServiceClient service = new policyConfigurationServiceClient(); ServicePolicyRequestMsg requestr = new ServicePolicyRequestMsg(); requestr.URI = URL; //ServicePolicyRequest r = new GetServicePolicyRequest(); //r.request = requestr; requestr.classification = currentlevel; ServicePolicyResponseMsg responsep = polservice.GetServicePolicy(requestr); policyCache.Remove(URL); PolicyHelper temp = new PolicyHelper(); if (responsep.policy.GetType() != typeof(TransactionalWebServicePolicy)) { Logger.info("FGSMS.MessageProcessor" + "Successfully retrieved a policy from the policy configuration service, however it was of an unexpected type " + URL + " type info " + responsep.policy.GetType().FullName); throw new ArgumentOutOfRangeException(); } temp.policy = (TransactionalWebServicePolicy)responsep.policy; temp.lastUpdate = DateTime.Now; currentlevel = responsep.classification; try { policyCache.Add(URL, temp); } catch (Exception e) { Logger.debug(e, "error caught adding " + URL + " to the policy cache"); } Logger.info("Policy Updater- Successfully Refreshed policy from the policy configuration service for " + URL.ToString()); return temp; } catch (Exception ex) { Logger.warn(ex, "Policy update failure, unable to update policy for " + URL + ". Using old policy instead. Error:"); } } return myPolicy; }
// private Boolean errorState = false; // private static Hashtable dependencyCache; private MessageProcessor() { if (AppDomain.CurrentDomain != null) AppDomain.CurrentDomain.ProcessExit += new EventHandler(CurrentDomain_ProcessExit); //Shutdown = true; currentlevel = new SecurityWrapper(); currentlevel.caveats = ""; currentlevel.classification = ClassificationType.U; Hashtable t2 = new Hashtable(); threadmap = Hashtable.Synchronized(t2); //log = new TraceSource(name); // dependencyCache = new Hashtable(); // dependency_queue = new Queue<DependencyContainer>(); Hashtable t = new Hashtable(); policyCache = Hashtable.Synchronized(t); // publishingThread = new Thread(new ThreadStart(SendPerformanceData)); //publishingThread.Start(); publishingThread = new List<Thread>(); // dependencyThread = new Thread(new ThreadStart(SendDependencyData)); // dependencyThread.Start(); Queue q = new Queue(); the_queue = Queue.Synchronized(q); try { config = new ConfigLoader(); // polservice = config.GetPCSProxy(); // dcsservice = config.GetDCSProxy(); } catch { ErrorState = true; } }