2018-02-06 15:12:11 +01:00
|
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018 ETH Zürich, Educational Development and Technology (LET)
|
|
|
|
|
*
|
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
using System;
|
2018-11-08 09:39:52 +01:00
|
|
|
|
using System.Collections.Generic;
|
2018-02-06 15:12:11 +01:00
|
|
|
|
using System.IO;
|
2018-11-08 09:39:52 +01:00
|
|
|
|
using System.Linq;
|
2018-12-14 09:50:10 +01:00
|
|
|
|
using System.Security.Cryptography.X509Certificates;
|
2018-12-11 16:06:10 +01:00
|
|
|
|
using SafeExamBrowser.Configuration.DataFormats;
|
2018-02-06 15:12:11 +01:00
|
|
|
|
using SafeExamBrowser.Contracts.Configuration;
|
2018-12-14 09:50:10 +01:00
|
|
|
|
using SafeExamBrowser.Contracts.Configuration.Cryptography;
|
|
|
|
|
using SafeExamBrowser.Contracts.Configuration.DataFormats;
|
|
|
|
|
using SafeExamBrowser.Contracts.Configuration.DataResources;
|
2018-02-06 15:12:11 +01:00
|
|
|
|
using SafeExamBrowser.Contracts.Configuration.Settings;
|
2018-08-31 07:49:41 +02:00
|
|
|
|
using SafeExamBrowser.Contracts.Logging;
|
2018-02-06 15:12:11 +01:00
|
|
|
|
|
|
|
|
|
namespace SafeExamBrowser.Configuration
|
|
|
|
|
{
|
|
|
|
|
public class ConfigurationRepository : IConfigurationRepository
|
|
|
|
|
{
|
2018-03-14 11:04:28 +01:00
|
|
|
|
private const string BASE_ADDRESS = "net.pipe://localhost/safeexambrowser";
|
|
|
|
|
|
2018-09-04 10:58:56 +02:00
|
|
|
|
private readonly string executablePath;
|
|
|
|
|
private readonly string programCopyright;
|
|
|
|
|
private readonly string programTitle;
|
|
|
|
|
private readonly string programVersion;
|
|
|
|
|
|
2018-06-29 09:50:20 +02:00
|
|
|
|
private AppConfig appConfig;
|
2018-12-11 16:06:10 +01:00
|
|
|
|
private IHashAlgorithm hashAlgorithm;
|
2018-11-08 09:39:52 +01:00
|
|
|
|
private IList<IDataFormat> dataFormats;
|
2018-12-14 09:50:10 +01:00
|
|
|
|
private IList<IDataResource> dataResources;
|
2018-12-11 16:06:10 +01:00
|
|
|
|
private ILogger logger;
|
2018-02-06 15:12:11 +01:00
|
|
|
|
|
2018-12-11 16:06:10 +01:00
|
|
|
|
public ConfigurationRepository(
|
|
|
|
|
IHashAlgorithm hashAlgorithm,
|
|
|
|
|
ILogger logger,
|
|
|
|
|
string executablePath,
|
|
|
|
|
string programCopyright,
|
|
|
|
|
string programTitle,
|
|
|
|
|
string programVersion)
|
2018-09-04 10:58:56 +02:00
|
|
|
|
{
|
2018-11-08 09:39:52 +01:00
|
|
|
|
dataFormats = new List<IDataFormat>();
|
2018-12-14 09:50:10 +01:00
|
|
|
|
dataResources = new List<IDataResource>();
|
2018-11-08 09:39:52 +01:00
|
|
|
|
|
2018-12-11 16:06:10 +01:00
|
|
|
|
this.hashAlgorithm = hashAlgorithm;
|
2018-11-08 09:39:52 +01:00
|
|
|
|
this.logger = logger;
|
2018-10-12 11:16:59 +02:00
|
|
|
|
this.executablePath = executablePath ?? string.Empty;
|
|
|
|
|
this.programCopyright = programCopyright ?? string.Empty;
|
|
|
|
|
this.programTitle = programTitle ?? string.Empty;
|
|
|
|
|
this.programVersion = programVersion ?? string.Empty;
|
2018-02-06 15:12:11 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-14 09:50:10 +01:00
|
|
|
|
public void ConfigureClientWith(Uri resource, EncryptionParameters encryption = null)
|
|
|
|
|
{
|
|
|
|
|
logger.Info($"Attempting to configure local client settings from '{resource}'...");
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
TryLoadData(resource, out Stream stream);
|
|
|
|
|
|
|
|
|
|
using (stream)
|
|
|
|
|
{
|
|
|
|
|
// TODO:
|
|
|
|
|
//TryParseData(stream, encryption, out _, out _, out var data);
|
|
|
|
|
//HandleIdentityCertificates(data);
|
|
|
|
|
|
|
|
|
|
// Save configuration data as local client config under %APPDATA%!
|
|
|
|
|
// -> New key will determine whether to use default password or current settings password!
|
|
|
|
|
// -> "clientConfigEncryptUsingSettingsPassword"
|
|
|
|
|
// -> Default settings password for local client configuration appears to be string.Empty -> passwords.SettingsPassword
|
|
|
|
|
// -> Otherwise, the local client configuration must again be encrypted in the same way as the original file!!
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
logger.Info($"Successfully configured local client settings with '{resource}'.");
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
logger.Error($"Unexpected error while trying to configure local client settings '{resource}'!", e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-12 11:16:59 +02:00
|
|
|
|
public AppConfig InitializeAppConfig()
|
2018-02-06 15:12:11 +01:00
|
|
|
|
{
|
|
|
|
|
var appDataFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), nameof(SafeExamBrowser));
|
|
|
|
|
var startTime = DateTime.Now;
|
|
|
|
|
var logFolder = Path.Combine(appDataFolder, "Logs");
|
|
|
|
|
var logFilePrefix = startTime.ToString("yyyy-MM-dd\\_HH\\hmm\\mss\\s");
|
|
|
|
|
|
2018-06-29 09:50:20 +02:00
|
|
|
|
appConfig = new AppConfig();
|
|
|
|
|
appConfig.ApplicationStartTime = startTime;
|
|
|
|
|
appConfig.AppDataFolder = appDataFolder;
|
|
|
|
|
appConfig.BrowserCachePath = Path.Combine(appDataFolder, "Cache");
|
2018-08-16 11:23:37 +02:00
|
|
|
|
appConfig.BrowserLogFile = Path.Combine(logFolder, $"{logFilePrefix}_Browser.log");
|
2018-06-29 09:50:20 +02:00
|
|
|
|
appConfig.ClientId = Guid.NewGuid();
|
|
|
|
|
appConfig.ClientAddress = $"{BASE_ADDRESS}/client/{Guid.NewGuid()}";
|
2018-09-04 10:58:56 +02:00
|
|
|
|
appConfig.ClientExecutablePath = Path.Combine(Path.GetDirectoryName(executablePath), $"{nameof(SafeExamBrowser)}.Client.exe");
|
2018-08-16 11:23:37 +02:00
|
|
|
|
appConfig.ClientLogFile = Path.Combine(logFolder, $"{logFilePrefix}_Client.log");
|
2018-06-29 09:50:20 +02:00
|
|
|
|
appConfig.ConfigurationFileExtension = ".seb";
|
|
|
|
|
appConfig.DefaultSettingsFileName = "SebClientSettings.seb";
|
|
|
|
|
appConfig.DownloadDirectory = Path.Combine(appDataFolder, "Downloads");
|
2018-08-31 07:49:41 +02:00
|
|
|
|
appConfig.LogLevel = LogLevel.Debug;
|
2018-09-04 10:58:56 +02:00
|
|
|
|
appConfig.ProgramCopyright = programCopyright;
|
2018-06-29 09:50:20 +02:00
|
|
|
|
appConfig.ProgramDataFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), nameof(SafeExamBrowser));
|
2018-09-04 10:58:56 +02:00
|
|
|
|
appConfig.ProgramTitle = programTitle;
|
|
|
|
|
appConfig.ProgramVersion = programVersion;
|
2018-06-29 09:50:20 +02:00
|
|
|
|
appConfig.RuntimeId = Guid.NewGuid();
|
|
|
|
|
appConfig.RuntimeAddress = $"{BASE_ADDRESS}/runtime/{Guid.NewGuid()}";
|
2018-08-16 11:23:37 +02:00
|
|
|
|
appConfig.RuntimeLogFile = Path.Combine(logFolder, $"{logFilePrefix}_Runtime.log");
|
2018-08-14 09:06:35 +02:00
|
|
|
|
appConfig.SebUriScheme = "seb";
|
|
|
|
|
appConfig.SebUriSchemeSecure = "sebs";
|
2018-06-29 09:50:20 +02:00
|
|
|
|
appConfig.ServiceAddress = $"{BASE_ADDRESS}/service";
|
2018-10-12 11:16:59 +02:00
|
|
|
|
|
|
|
|
|
return appConfig;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ISessionConfiguration InitializeSessionConfiguration()
|
|
|
|
|
{
|
|
|
|
|
var configuration = new SessionConfiguration();
|
|
|
|
|
|
|
|
|
|
UpdateAppConfig();
|
|
|
|
|
|
2018-11-08 09:39:52 +01:00
|
|
|
|
configuration.AppConfig = appConfig.Clone();
|
2018-10-12 11:16:59 +02:00
|
|
|
|
configuration.Id = Guid.NewGuid();
|
|
|
|
|
configuration.StartupToken = Guid.NewGuid();
|
|
|
|
|
|
|
|
|
|
return configuration;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Settings LoadDefaultSettings()
|
|
|
|
|
{
|
|
|
|
|
var settings = new Settings();
|
|
|
|
|
|
2018-11-22 14:36:20 +01:00
|
|
|
|
// TODO: Specify default settings
|
|
|
|
|
|
2018-11-08 09:39:52 +01:00
|
|
|
|
settings.KioskMode = KioskMode.None;
|
2018-10-12 11:16:59 +02:00
|
|
|
|
settings.ServicePolicy = ServicePolicy.Optional;
|
|
|
|
|
|
|
|
|
|
settings.Browser.StartUrl = "https://www.safeexambrowser.org/testing";
|
|
|
|
|
settings.Browser.AllowAddressBar = true;
|
|
|
|
|
settings.Browser.AllowBackwardNavigation = true;
|
2018-11-08 09:39:52 +01:00
|
|
|
|
settings.Browser.AllowConfigurationDownloads = true;
|
2018-10-12 11:16:59 +02:00
|
|
|
|
settings.Browser.AllowDeveloperConsole = true;
|
2018-11-08 09:39:52 +01:00
|
|
|
|
settings.Browser.AllowDownloads = true;
|
2018-10-12 11:16:59 +02:00
|
|
|
|
settings.Browser.AllowForwardNavigation = true;
|
|
|
|
|
settings.Browser.AllowReloading = true;
|
|
|
|
|
|
|
|
|
|
settings.Taskbar.AllowApplicationLog = true;
|
|
|
|
|
settings.Taskbar.AllowKeyboardLayout = true;
|
|
|
|
|
settings.Taskbar.AllowWirelessNetwork = true;
|
|
|
|
|
|
|
|
|
|
return settings;
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-08 09:39:52 +01:00
|
|
|
|
public void Register(IDataFormat dataFormat)
|
|
|
|
|
{
|
|
|
|
|
dataFormats.Add(dataFormat);
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-14 09:50:10 +01:00
|
|
|
|
public void Register(IDataResource dataResource)
|
2018-11-08 09:39:52 +01:00
|
|
|
|
{
|
2018-12-14 09:50:10 +01:00
|
|
|
|
dataResources.Add(dataResource);
|
2018-11-08 09:39:52 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-14 09:50:10 +01:00
|
|
|
|
public LoadStatus TryLoadSettings(Uri resource, PasswordParameters password, out EncryptionParameters encryption, out Format format, out Settings settings)
|
2018-10-12 11:16:59 +02:00
|
|
|
|
{
|
2018-11-08 09:39:52 +01:00
|
|
|
|
logger.Info($"Attempting to load '{resource}'...");
|
|
|
|
|
|
2018-12-14 09:50:10 +01:00
|
|
|
|
encryption = default(EncryptionParameters);
|
|
|
|
|
format = default(Format);
|
2018-11-30 14:50:28 +01:00
|
|
|
|
settings = LoadDefaultSettings();
|
|
|
|
|
|
2018-11-08 09:39:52 +01:00
|
|
|
|
try
|
|
|
|
|
{
|
2018-12-14 09:50:10 +01:00
|
|
|
|
var status = TryLoadData(resource, out Stream stream);
|
2018-11-08 09:39:52 +01:00
|
|
|
|
|
2018-12-14 09:50:10 +01:00
|
|
|
|
using (stream)
|
2018-11-08 09:39:52 +01:00
|
|
|
|
{
|
2018-12-14 09:50:10 +01:00
|
|
|
|
if (status != LoadStatus.Success)
|
2018-12-11 16:06:10 +01:00
|
|
|
|
{
|
2018-12-14 09:50:10 +01:00
|
|
|
|
return status;
|
2018-12-11 16:06:10 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-14 09:50:10 +01:00
|
|
|
|
status = TryParseData(stream, password, out encryption, out format, out var data);
|
|
|
|
|
|
|
|
|
|
if (status == LoadStatus.Success)
|
2018-11-15 08:45:17 +01:00
|
|
|
|
{
|
2018-12-14 09:50:10 +01:00
|
|
|
|
data.MapTo(settings);
|
2018-11-15 08:45:17 +01:00
|
|
|
|
}
|
2018-11-08 09:39:52 +01:00
|
|
|
|
|
2018-12-14 09:50:10 +01:00
|
|
|
|
return status;
|
2018-11-30 14:50:28 +01:00
|
|
|
|
}
|
2018-11-08 09:39:52 +01:00
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
logger.Error($"Unexpected error while trying to load '{resource}'!", e);
|
|
|
|
|
|
|
|
|
|
return LoadStatus.UnexpectedError;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-14 09:50:10 +01:00
|
|
|
|
public SaveStatus TrySaveSettings(Uri resource, Format format, Settings settings, EncryptionParameters encryption = null)
|
2018-12-11 16:06:10 +01:00
|
|
|
|
{
|
2018-12-14 09:50:10 +01:00
|
|
|
|
throw new NotImplementedException();
|
2018-12-11 16:06:10 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-14 09:50:10 +01:00
|
|
|
|
private void HandleIdentityCertificates(IDictionary<string, object> data)
|
2018-12-11 16:06:10 +01:00
|
|
|
|
{
|
2018-12-14 09:50:10 +01:00
|
|
|
|
const int IDENTITY_CERTIFICATE = 1;
|
|
|
|
|
var hasCertificates = data.TryGetValue("embeddedCertificates", out object value);
|
|
|
|
|
|
|
|
|
|
if (hasCertificates && value is IList<IDictionary<string, object>> certificates)
|
|
|
|
|
{
|
|
|
|
|
var toRemove = new List<IDictionary<string, object>>();
|
|
|
|
|
|
|
|
|
|
foreach (var certificate in certificates)
|
|
|
|
|
{
|
|
|
|
|
var isIdentity = certificate.TryGetValue("type", out object t) && t is int type && type == IDENTITY_CERTIFICATE;
|
|
|
|
|
var hasData = certificate.TryGetValue("certificateData", out value);
|
2018-12-11 16:06:10 +01:00
|
|
|
|
|
2018-12-14 09:50:10 +01:00
|
|
|
|
if (isIdentity && hasData && value is byte[] certificateData)
|
|
|
|
|
{
|
|
|
|
|
ImportIdentityCertificate(certificateData, new X509Store(StoreLocation.CurrentUser));
|
|
|
|
|
ImportIdentityCertificate(certificateData, new X509Store(StoreName.TrustedPeople, StoreLocation.LocalMachine));
|
|
|
|
|
|
|
|
|
|
toRemove.Add(certificate);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
toRemove.ForEach(c => certificates.Remove(c));
|
|
|
|
|
}
|
2018-12-11 16:06:10 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-14 09:50:10 +01:00
|
|
|
|
private void ImportIdentityCertificate(byte[] certificateData, X509Store store)
|
2018-12-11 16:06:10 +01:00
|
|
|
|
{
|
2018-12-14 09:50:10 +01:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var certificate = new X509Certificate2();
|
|
|
|
|
|
|
|
|
|
certificate.Import(certificateData, "Di𝈭l𝈖Ch𝈒aht𝈁aHai1972", X509KeyStorageFlags.UserKeySet | X509KeyStorageFlags.PersistKeySet);
|
2018-12-11 16:06:10 +01:00
|
|
|
|
|
2018-12-14 09:50:10 +01:00
|
|
|
|
store.Open(OpenFlags.ReadWrite);
|
|
|
|
|
store.Add(certificate);
|
2018-12-11 16:06:10 +01:00
|
|
|
|
|
2018-12-14 09:50:10 +01:00
|
|
|
|
logger.Info($"Successfully imported identity certificate into {store.Location}.{store.Name}.");
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
2018-12-11 16:06:10 +01:00
|
|
|
|
{
|
2018-12-14 09:50:10 +01:00
|
|
|
|
logger.Error($"Failed to import identity certificate into {store.Location}.{store.Name}!", e);
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
store.Close();
|
2018-12-11 16:06:10 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-09 14:15:56 +01:00
|
|
|
|
private LoadStatus TryLoadData(Uri resource, out Stream data)
|
2018-11-08 09:39:52 +01:00
|
|
|
|
{
|
2018-11-09 14:15:56 +01:00
|
|
|
|
var status = LoadStatus.NotSupported;
|
2018-12-14 09:50:10 +01:00
|
|
|
|
var resourceLoader = dataResources.FirstOrDefault(l => l.CanLoad(resource));
|
2018-11-09 14:15:56 +01:00
|
|
|
|
|
|
|
|
|
data = default(Stream);
|
|
|
|
|
|
|
|
|
|
if (resourceLoader != null)
|
|
|
|
|
{
|
|
|
|
|
status = resourceLoader.TryLoad(resource, out data);
|
|
|
|
|
logger.Info($"Tried to load data from '{resource}' using {resourceLoader.GetType().Name} -> Result: {status}.");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
logger.Warn($"No resource loader found for '{resource}'!");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return status;
|
|
|
|
|
}
|
2018-11-08 09:39:52 +01:00
|
|
|
|
|
2018-12-14 09:50:10 +01:00
|
|
|
|
private LoadStatus TryParseData(Stream data, PasswordParameters password, out EncryptionParameters encryption, out Format format, out IDictionary<string, object> rawData)
|
2018-11-09 14:15:56 +01:00
|
|
|
|
{
|
|
|
|
|
var dataFormat = dataFormats.FirstOrDefault(f => f.CanParse(data));
|
2018-12-11 16:06:10 +01:00
|
|
|
|
var status = LoadStatus.NotSupported;
|
2018-11-09 14:15:56 +01:00
|
|
|
|
|
2018-12-14 09:50:10 +01:00
|
|
|
|
encryption = default(EncryptionParameters);
|
|
|
|
|
format = default(Format);
|
|
|
|
|
rawData = default(Dictionary<string, object>);
|
|
|
|
|
|
2018-11-09 14:15:56 +01:00
|
|
|
|
if (dataFormat != null)
|
|
|
|
|
{
|
2018-12-14 09:50:10 +01:00
|
|
|
|
var result = dataFormat.TryParse(data, password);
|
2018-12-11 16:06:10 +01:00
|
|
|
|
|
2018-12-14 09:50:10 +01:00
|
|
|
|
encryption = result.Encryption;
|
|
|
|
|
format = result.Format;
|
|
|
|
|
rawData = result.RawData;
|
2018-12-11 16:06:10 +01:00
|
|
|
|
status = result.Status;
|
2018-12-14 09:50:10 +01:00
|
|
|
|
|
|
|
|
|
logger.Info($"Tried to parse data from '{data}' using {dataFormat.GetType().Name} -> Result: {status}.");
|
2018-11-09 14:15:56 +01:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
logger.Warn($"No data format found for '{data}'!");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return status;
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-29 09:50:20 +02:00
|
|
|
|
private void UpdateAppConfig()
|
2018-03-14 11:04:28 +01:00
|
|
|
|
{
|
2018-10-12 11:16:59 +02:00
|
|
|
|
appConfig.ClientId = Guid.NewGuid();
|
|
|
|
|
appConfig.ClientAddress = $"{BASE_ADDRESS}/client/{Guid.NewGuid()}";
|
2018-03-14 11:04:28 +01:00
|
|
|
|
}
|
2018-02-06 15:12:11 +01:00
|
|
|
|
}
|
|
|
|
|
}
|