/// <summary> /// Exports the security configuration for an application identified by a file or url. /// </summary> /// <param name="filePath">The file path.</param> /// <returns>The security configuration.</returns> public SecuredApplication ReadConfiguration(string filePath) { if (filePath == null) { throw new ArgumentNullException(nameof(filePath)); } string configFilePath = filePath; string exeFilePath = null; // check for valid file. if (!File.Exists(filePath)) { throw ServiceResultException.Create( StatusCodes.BadNotReadable, "Cannot find the executable or configuration file: {0}", filePath); } // find the configuration file for the executable. if (filePath.EndsWith(".exe", StringComparison.OrdinalIgnoreCase)) { exeFilePath = filePath; try { FileInfo file = new FileInfo(filePath); string sectionName = file.Name; sectionName = sectionName.Substring(0, sectionName.Length - file.Extension.Length); configFilePath = ApplicationConfiguration.GetFilePathFromAppConfig(sectionName); if (configFilePath == null) { configFilePath = filePath + ".config"; } } catch (Exception e) { throw ServiceResultException.Create( StatusCodes.BadNotReadable, e, "Cannot find the configuration file for the executable: {0}", filePath); } if (!File.Exists(configFilePath)) { throw ServiceResultException.Create( StatusCodes.BadNotReadable, "Cannot find the configuration file: {0}", configFilePath); } } SecuredApplication application = null; ApplicationConfiguration applicationConfiguration = null; try { FileStream reader = File.Open(configFilePath, FileMode.Open, FileAccess.Read, FileShare.Read); try { byte[] data = new byte[reader.Length]; reader.Read(data, 0, (int)reader.Length); // find the SecuredApplication element in the file. if (data.ToString().Contains("SecuredApplication")) { DataContractSerializer serializer = new DataContractSerializer(typeof(SecuredApplication)); application = serializer.ReadObject(reader) as SecuredApplication; application.ConfigurationFile = configFilePath; application.ExecutableFile = exeFilePath; } // load the application configuration. else { reader.Dispose(); reader = File.Open(configFilePath, FileMode.Open, FileAccess.Read, FileShare.Read); DataContractSerializer serializer = new DataContractSerializer(typeof(ApplicationConfiguration)); applicationConfiguration = serializer.ReadObject(reader) as ApplicationConfiguration; } } finally { reader.Dispose(); } } catch (Exception e) { throw ServiceResultException.Create( StatusCodes.BadNotReadable, e, "Cannot load the configuration file: {0}", filePath); } // check if security info store on disk. if (application != null) { return(application); } application = new SecuredApplication(); // copy application info. application.ApplicationName = applicationConfiguration.ApplicationName; application.ApplicationUri = applicationConfiguration.ApplicationUri; application.ProductName = applicationConfiguration.ProductUri; application.ApplicationType = (ApplicationType)(int)applicationConfiguration.ApplicationType; application.ConfigurationFile = configFilePath; application.ExecutableFile = exeFilePath; application.ConfigurationMode = "http://opcfoundation.org/UASDK/ConfigurationTool"; application.LastExportTime = DateTime.UtcNow; // copy the security settings. if (applicationConfiguration.SecurityConfiguration != null) { application.ApplicationCertificate = SecuredApplication.ToCertificateIdentifier(applicationConfiguration.SecurityConfiguration.ApplicationCertificate); if (applicationConfiguration.SecurityConfiguration.TrustedIssuerCertificates != null) { application.IssuerCertificateStore = SecuredApplication.ToCertificateStoreIdentifier(applicationConfiguration.SecurityConfiguration.TrustedIssuerCertificates); if (applicationConfiguration.SecurityConfiguration.TrustedIssuerCertificates.TrustedCertificates != null) { application.IssuerCertificates = SecuredApplication.ToCertificateList(applicationConfiguration.SecurityConfiguration.TrustedIssuerCertificates.TrustedCertificates); } } if (applicationConfiguration.SecurityConfiguration.TrustedPeerCertificates != null) { application.TrustedCertificateStore = SecuredApplication.ToCertificateStoreIdentifier(applicationConfiguration.SecurityConfiguration.TrustedPeerCertificates); if (applicationConfiguration.SecurityConfiguration.TrustedPeerCertificates.TrustedCertificates != null) { application.TrustedCertificates = SecuredApplication.ToCertificateList(applicationConfiguration.SecurityConfiguration.TrustedPeerCertificates.TrustedCertificates); } } if (applicationConfiguration.SecurityConfiguration.RejectedCertificateStore != null) { application.RejectedCertificatesStore = SecuredApplication.ToCertificateStoreIdentifier(applicationConfiguration.SecurityConfiguration.RejectedCertificateStore); } } ServerBaseConfiguration serverConfiguration = null; if (applicationConfiguration.ServerConfiguration != null) { serverConfiguration = applicationConfiguration.ServerConfiguration; } else if (applicationConfiguration.DiscoveryServerConfiguration != null) { serverConfiguration = applicationConfiguration.DiscoveryServerConfiguration; } if (serverConfiguration != null) { application.BaseAddresses = SecuredApplication.ToListOfBaseAddresses(serverConfiguration); application.SecurityProfiles = SecuredApplication.ToListOfSecurityProfiles(serverConfiguration.SecurityPolicies); } // return exported setttings. return(application); }