public bool GenerateCertFromStore() { try { X509Store store = new X509Store(StoreName.Root, StoreLocation.CurrentUser); store.Open(OpenFlags.ReadOnly); foreach (var c in store.Certificates) { this.certificate = c; foreach (var c2 in store.Certificates) { if (!c.Equals(c2)) { if (c.NotAfter <= c2.NotAfter) { this.certificate = c2; // } } } } store.Close(); if (IsCertLoaded) { // Check if certificate is not valid/has expired if (DateTime.Compare(this.certificate.NotAfter, DateTime.Today) < 0) { throw new CertificateInstalledExpiredException(); } } return(true); } catch (Exception e) { if (Errors.IsOwnException(e)) { throw e; } else { throw new CertificateInstalledNotValidException(); } } }
public bool StoreCertificate(X509Certificate2 cert) { bool bFound = false; try { if (cert != null) { X509Store store = new X509Store(StoreName.Root, StoreLocation.CurrentUser); store.Open(OpenFlags.ReadWrite); foreach (var c in store.Certificates) { if (c.Equals(cert)) { bFound = true; break; } else if (c.NotAfter <= cert.NotAfter) { store.Remove(c); // only one certificate } else { bFound = true; // if date is previous not save in store } } if (!bFound) { store.Add(cert); } store.Close(); } return(true); } catch (Exception e) { if (Errors.IsOwnException(e)) { throw e; } else { throw new CertificateInstalledNotValidException(); } } }
/* * public void LoadCertFromKeychain () * { * // https://docs.microsoft.com/es-es/dotnet/api/system.security.cryptography.x509certificates.storename?view=netframework-4.8 * * GetCol ( StoreName.AddressBook ); * GetCol ( StoreName.AuthRoot ); * GetCol ( StoreName.CertificateAuthority ); * GetCol ( StoreName.Disallowed ); * GetCol ( StoreName.My ); * GetCol ( StoreName.Root ); * GetCol ( StoreName.TrustedPeople ); * GetCol ( StoreName.TrustedPublisher ); * } * * private void GetCol ( StoreName name ) * { * X509Store store = new X509Store ( name ); * store.Open ( OpenFlags.ReadWrite ); * //X509Certificate2 certificate = new X509Certificate2 (); * * X509Certificate2Collection storecollection = (X509Certificate2Collection)store.Certificates; * * Console.WriteLine("Store name: {0}", store.Name); * Console.WriteLine("Store location: {0} , count: {1}", store.Location, storecollection.Count ); * foreach (X509Certificate2 x509 in storecollection) * Console.WriteLine("certificate name: {0}", x509.Subject); * * store.Close (); * store.Dispose (); * } */ public X509Certificate2 CreateCertificate(string sCert = null, string sFileCer = null) { string content; byte[] contentBytes; X509Certificate2 certNew = null; try { if (!String.IsNullOrEmpty(sCert)) { content = sCert; if (!content.StartsWith(CER_INIT)) { contentBytes = Convert.FromBase64String(content); } else { contentBytes = Encoding.ASCII.GetBytes(CER_HEADER + content + CER_FOOTER); } certNew = new X509Certificate2(contentBytes); } else if (!String.IsNullOrEmpty(sFileCer)) { sFileCer = Path.Combine(ConfigPath, sFileCer); certNew = new X509Certificate2(sFileCer); File.Delete(sFileCer); } return(certNew); } catch (Exception e) { if (Errors.IsOwnException(e)) { throw e; } else { throw new CertificateInstalledNotValidException(); } } }
public void ParseScriptAndRun(ISerial serial_device, String script_stream, int stream_size) { XmlSerializer s = null; try { // Script file is empty if (string.IsNullOrEmpty(script_stream.Trim())) { throw new ScriptEmptyException(); } Script script = new Script(); s = new XmlSerializer(typeof(Script)); // Register unknown elements ( not present in Script class ) as additional parameters s.UnknownElement += this.UnknownElementEvent; using (StringReader reader = new StringReader(script_stream.Substring(0, stream_size))) { script = ( Script )s.Deserialize(reader); } BuildScriptActions(serial_device, script); } catch (Exception e) { if (!Errors.IsOwnException(e)) { Errors.LogErrorNowAndContinue(new ScriptWrongStructureException()); // Script file has invalid format or structure } else { Errors.LogErrorNowAndContinue(e); // ScriptLogfileInvalidException, ScriptActionTypeInvalidException } this.OnError(); return; } finally { s.UnknownElement -= this.UnknownElementEvent; } this.Run(); }
public void ParseScriptAndRun(ISerial serial_device, String script_stream, int stream_size) { // Script file is empty if (string.IsNullOrEmpty(script_stream.Trim())) { Errors.LogErrorNowAndKill(new ScriptEmptyException()); //this.OnError (); return; } Script script = new Script(); XmlSerializer s = new XmlSerializer(typeof(Script)); try { using (StringReader reader = new StringReader(script_stream.Substring(0, stream_size))) { script = (Script)s.Deserialize(reader); } buildScriptActions(serial_device, script); } catch (Exception e) { if (!Errors.IsOwnException(e)) { Errors.LogErrorNowAndKill(new ScriptWrongStructureException()); // Script file has invalid format or structure } else { Errors.LogErrorNowAndKill(e); // ScriptLogfileInvalidException, ScriptActionTypeInvalidException } //this.OnError (); return; } this.Run(); }
public void GenerateCert(string sCertificate = null) { string content; try { string path = Path.Combine(Mobile.ConfigPath, "certificate.txt"); if (File.Exists(path)) { // NOTE: Full certificate file should be converted to base64 and not only the data that appear when // open the file with some text editor. The resulting string will be without header and footer strings // and seems that always starting with "MII..." and finish with "==" // https://www.base64encode.org // e.g. Aclara certificate in base64 // base64cert = "MIICxDCCAaygAwIBAgIQV5fB/SvFm4VDwxNIjmx3LzANBgkqhkiG9w0BAQUFADAeMRwwGgYDVQQDExNOZXctVGVzdC1EZXYtQWNsYXJhMB4XDTE1MDQxNTA0MDAwMFoXDTI1MDQyMjA0MDAwMFowHjEcMBoGA1UEAxMTTmV3LVRlc3QtRGV2LUFjbGFyYTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBANOISmTy1kRTeOPqajIm+y27q676LFKodBpgrm0M3imYpwnVd+aTnVdk7+NT5vSA1c9dB5PSojh/UfGg2kWDe5gNj2ZA+KaemXFqvl8YI/D6XjoNz3JqoqocjF4/hJnrUdwqOoUL6WPtbWEhCnzin/cVkKx5qxMrOh9qAzp+qYAqyJ26Aocr+nlM7oHRtBUmYRKZbpkNAnpiIV/Q6quSR5Qzsf4XrhvkPDkf2ZX8DvcJmAbXEAaBVa2ORsY9qA86jIphui5kwI9JPcw9hTZy1QxvNcZAijtPyC6AKDuRyEv0Awa1gcSBBRsf0HbeCSD91U/O51+alP3hLhA9tcxddx0CAwEAATANBgkqhkiG9w0BAQUFAAOCAQEAGuTqwTvEgaTl/E2jdG9RUD3zN9MhRCijJIpjv9NdkkH13LK5Sn9up1+DraaccA5h2El9kiXDHYWPA/qRMq1auhNcmTFVYjeQSNW0tyuTqbQiG/8fwZiAZrGn6UmOU/vzzhkyv05x5KzVAEwp94fU/J+kOIJVH0ff5jnMeYHARc1sY6JgXgJKoJbdS4Q4wG2RHj5yFAixv/zwS1XBy2GWtsz03aucNQzBIbk1uTIv2eyYqFMhSGT36vkfJFidRcR3H4FWnvInWoWmxlGcs0MS3bNOAv5ij55h0rREGJ9WdJmI/gw84aA4itFwwUuG6kKdF9AF/rljtVCFVH6T9PFI2Q=="; // /Library/Frameworks/Xamarin.iOS.framework/Versions/12.2.1.13/src/Xamarin.iOS/mcs/class/Mono.Security/Mono.Security.X509/X509Certificate.cs // NOTE: Method PEM needs to find the header and footer strings previous to start with certificate // parsing/generation, and these both const should be concatenated with the cert in base64 // e.g. -----BEGIN CERTIFICATE----- + base64cert + -----END CERTIFICATE----- // Each part converted to byte array content = File.ReadAllText(path); } else if (!String.IsNullOrEmpty(sCertificate)) //intune { content = sCertificate; } else { return; } byte[] contentBytes; // If the cer file used was already in base64 format, was saved in the // txt file doubly converted in base64 and directly with header and footer if (!content.StartsWith(CER_INIT)) { contentBytes = Convert.FromBase64String(content); } else { contentBytes = Encoding.ASCII.GetBytes(CER_HEADER + content + CER_FOOTER); } this.certificate = new X509Certificate2(contentBytes); // Check if certificate is not valid/has expired if (DateTime.Compare(this.certificate.NotAfter, DateTime.Today) < 0) { throw new CertificateInstalledExpiredException(); } } catch (Exception e) { if (Errors.IsOwnException(e)) { throw e; } else { throw new CertificateInstalledNotValidException(); } } }
public void GenerateCert(string sCertificate = null) { string content; try { string path = Path.Combine(Mobile.ConfigPath, "certificate.txt"); if (File.Exists(path)) { // NOTE: Full certificate file should be converted to base64 and not only the data that appear when // open the file with some text editor. The resulting string will be without header and footer strings // and seems that always starting with "MII..." and finish with "==" // https://www.base64encode.org // e.g. Aclara certificate in base64 // base64cert = "MIICxDCCAaygAwIBAgIQV5fB/SvFm4VD...uG6kKdF9AF/rljtVCFVH6T9PFI2Q=="; // /Library/Frameworks/Xamarin.iOS.framework/Versions/12.2.1.13/src/Xamarin.iOS/mcs/class/Mono.Security/Mono.Security.X509/X509Certificate.cs // NOTE: Method PEM needs to find the header and footer strings previous to start with certificate // parsing/generation, and these both const should be concatenated with the cert in base64 // e.g. -----BEGIN CERTIFICATE----- + base64cert + -----END CERTIFICATE----- // Each part converted to byte array content = File.ReadAllText(path); } else if (!String.IsNullOrEmpty(sCertificate)) //intune { content = sCertificate; } else { return; } byte[] contentBytes; // If the cer file used was already in base64 format, was saved in the // txt file doubly converted in base64 and directly with header and footer if (!content.StartsWith(CER_INIT)) { contentBytes = Convert.FromBase64String(content); } else { contentBytes = Encoding.ASCII.GetBytes(CER_HEADER + content + CER_FOOTER); } this.certificate = new X509Certificate2(contentBytes); // Check if certificate is not valid/has expired if (DateTime.Compare(this.certificate.NotAfter, DateTime.Today) < 0) { throw new CertificateInstalledExpiredException(); } } catch (Exception e) { if (Errors.IsOwnException(e)) { throw e; } else { throw new CertificateInstalledNotValidException(); } } }
private Configuration(string path = "", bool avoidXmlError = false) { string configPath = Mobile.ConfigPath; device = "PC"; try { // Load configuration files ( xml's ) mtuTypes = Utils.DeserializeXml <MtuTypes> (Path.Combine(configPath, XML_MTUS)); meterTypes = Utils.DeserializeXml <MeterTypes> (Path.Combine(configPath, XML_METERS)); Global = Utils.DeserializeXml <Global> (Path.Combine(configPath, XML_GLOBAL)); alarms = Utils.DeserializeXml <AlarmList> (Path.Combine(configPath, XML_ALARMS)); demands = Utils.DeserializeXml <DemandConf> (Path.Combine(configPath, XML_DEMANDS)); users = Utils.DeserializeXml <UserList> (Path.Combine(configPath, XML_USERS)).List; interfaces = Utils.DeserializeXml <InterfaceConfig> (XML_INTERFACE, true); // From resources // Preload port types, because some ports use a letter but other a list of Meter IDs // Done here because Xml project has no reference to MTUComm ( cross references ) List <string> portTypes; foreach (Mtu mtu in mtuTypes.Mtus) { foreach (Port port in mtu.Ports) { bool isNumeric = MeterAux.GetPortTypes(port.Type, out portTypes); if (!isNumeric) { port.TypeString = portTypes[0]; } else { port.TypeString = meterTypes.FindByMterId(int.Parse(portTypes[0])).Type; } Utils.Print("MTU " + mtu.Id + ": Type " + port.TypeString); } } // Regenerate certificate from base64 string Mobile.configData.GenerateCert(); //Mobile.configData.LoadCertFromKeychain (); // Check global min date allowed if (!string.IsNullOrEmpty(Global.MinDate) && DateTime.Compare(DateTime.ParseExact(Global.MinDate, "MM/dd/yyyy", null), DateTime.Today) < 0) { throw new DeviceMinDateAllowedException(); } } catch (Exception e) { if (!avoidXmlError) { if (Errors.IsOwnException(e)) { throw e; } else if (e is FileNotFoundException) { throw new ConfigurationFilesNotFoundException(); } else { throw new ConfigurationFilesCorruptedException(); } } } }