Skip to content
This repository was archived by the owner on Dec 5, 2019. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ namespace AeroGear.Mobile.Security
/// <summary>
/// Factory for Android security checks.
/// </summary>
internal class AndroidSecurityCheckFactory : ISecurityCheckFactory
internal class AndroidDeviceCheckFactory : IDeviceCheckFactory
{
public static readonly AndroidSecurityCheckFactory INSTANCE = new AndroidSecurityCheckFactory();
public static readonly AndroidDeviceCheckFactory INSTANCE = new AndroidDeviceCheckFactory();

private readonly Context context;

/// <summary>
/// Initializes a new instance of the <see cref="T:AeroGear.Mobile.Security.AndroidSecurityCheckFactory"/> class.
/// Initializes a new instance of the <see cref="T:AeroGear.Mobile.Security.AndroidDeviceCheckFactory"/> class.
/// </summary>
public AndroidSecurityCheckFactory(Context ctx = null)
public AndroidDeviceCheckFactory(Context ctx = null)
{
this.context = ctx != null ? ctx.ApplicationContext : Android.App.Application.Context;
}
Expand All @@ -25,17 +25,17 @@ public AndroidSecurityCheckFactory(Context ctx = null)
/// Returns an initialized instance of the check identified by the passed in pseudo enumeration.
/// </summary>
/// <returns>The initialized instance of the check.</returns>
/// <param name="type">Type of the check to be instantiated. This must be an instance of <see cref="T:AeroGear.Mobile.Security.SecurityChecks"/></param>
public ISecurityCheck create(ISecurityCheckType type)
/// <param name="type">Type of the check to be instantiated. This must be an instance of <see cref="T:AeroGear.Mobile.Security.DeviceChecks"/></param>
public IDeviceCheck create(IDeviceCheckType type)
{
SecurityChecks checkType = type as SecurityChecks;
DeviceChecks checkType = type as DeviceChecks;

if (checkType == null)
{
throw new Exception("Passed in security check type is not supported");
throw new Exception("Passed in device check type is not supported");
}

return Activator.CreateInstance(checkType.CheckType, this.context) as ISecurityCheck;
return Activator.CreateInstance(checkType.CheckType, this.context) as IDeviceCheck;
}

/// <summary>
Expand All @@ -44,12 +44,12 @@ public ISecurityCheck create(ISecurityCheckType type)
/// </summary>
/// <returns>The initialized instance of the check.</returns>
/// <param name="typeName">The name of the check to be instantiated.</param>
public ISecurityCheck create(string typeName)
public IDeviceCheck create(string typeName)
{
ISecurityCheckType securityCheckType = SecurityChecks.GetSecurityCheck(typeName);
IDeviceCheckType securityCheckType = DeviceChecks.GetDeviceCheck(typeName);
if (securityCheckType == null)
{
throw new Exception(String.Format("No security check with name {0} is known", typeName));
throw new Exception(String.Format("No device check with name {0} is known", typeName));
}

return create(securityCheckType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,22 @@ namespace AeroGear.Mobile.Security.Checks
/// <summary>
/// Check to determine whether the allowBackup flag is enabled for the application.
/// </summary>
public class BackupDisallowedCheck : AbstractSecurityCheck
public class BackupAllowedCheck : AbstractDeviceCheck
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this be renamed to BackupEnabledCheck?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

{
protected override string Name => "Backup Flag Check";

private readonly Context context;

public BackupDisallowedCheck(Context ctx)
public BackupAllowedCheck(Context ctx)
{
this.context = ctx;
}

public override SecurityCheckResult Check()
public override DeviceCheckResult Check()
{
PackageInfo packageInfo = context.PackageManager.GetPackageInfo(context.PackageName, 0);
bool disabled = (packageInfo.ApplicationInfo.Flags & ApplicationInfoFlags.AllowBackup) == 0;
return new SecurityCheckResult(this, disabled);
bool enabled = (packageInfo.ApplicationInfo.Flags & ApplicationInfoFlags.AllowBackup) != 0;
return new DeviceCheckResult(this, enabled);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@ namespace AeroGear.Mobile.Security.Checks
/// <summary>
/// A check for whether a debugger is attached to the current application.
/// </summary>
public class NoDebuggerCheck : AbstractSecurityCheck
public class DebuggerEnabledCheck : AbstractDeviceCheck
{
protected override string Name => "Debugger Check";

private readonly Context context;

public NoDebuggerCheck(Context ctx)
public DebuggerEnabledCheck(Context ctx)
{
this.context = ctx;
}

public override SecurityCheckResult Check()
public override DeviceCheckResult Check()
{
return new SecurityCheckResult(this, !(Debug.IsDebuggerConnected || System.Diagnostics.Debugger.IsAttached));
return new DeviceCheckResult(this, (Debug.IsDebuggerConnected || System.Diagnostics.Debugger.IsAttached));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,23 @@ namespace AeroGear.Mobile.Security.Checks
/// <summary>
/// Security check that detects if developer mode is enabled in the device.
/// </summary>
public class DeveloperModeDisabledCheck : AbstractSecurityCheck
public class DeveloperModeEnabledCheck : AbstractDeviceCheck
{
protected override string Name => "Developer Mode Check";

private readonly Context context;

public DeveloperModeDisabledCheck(Context ctx)
public DeveloperModeEnabledCheck(Context ctx)
{
this.context = ctx;
}

public override SecurityCheckResult Check()
public override DeviceCheckResult Check()
{
bool devModeEnabled = Settings.Global.GetInt(
context.ContentResolver,
Settings.Global.DevelopmentSettingsEnabled, 0) != 0;
return new SecurityCheckResult(this, !devModeEnabled);
return new DeviceCheckResult(this, devModeEnabled);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,26 @@ namespace AeroGear.Mobile.Security.Checks
/// <summary>
/// A check for whether the device the application is running on an emulator.
/// </summary>
public class NotInEmulatorCheck : AbstractSecurityCheck
public class EmulatorCheck : AbstractDeviceCheck
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

may be changed to IsEmulatorCheck?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

{
protected override string Name => "Emulator Check";

private readonly Context context;

public NotInEmulatorCheck(Context ctx)
public EmulatorCheck(Context ctx)
{
this.context = ctx;
}

public override SecurityCheckResult Check()
public override DeviceCheckResult Check()
{
if (Build.Fingerprint != null)
{
if (Build.Fingerprint.Contains("vbox") ||
Build.Fingerprint.Contains("generic"))
return new SecurityCheckResult(this, false);
return new DeviceCheckResult(this, true);
}
return new SecurityCheckResult(this, true);
return new DeviceCheckResult(this, false);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,24 @@ namespace AeroGear.Mobile.Security.Checks
/// <summary>
/// Detects whether a devices filesystem is encrypted.
/// </summary>
public class EncryptionCheck : AbstractSecurityCheck
public class EncryptionEnabledCheck : AbstractDeviceCheck
{
protected override string Name => "Encryption Check";

private readonly Context context;

public EncryptionCheck(Context ctx)
public EncryptionEnabledCheck(Context ctx)
{
this.context = ctx;
}

public override SecurityCheckResult Check()
public override DeviceCheckResult Check()
{
DevicePolicyManager policyManager = (DevicePolicyManager)context
.GetSystemService(Context.DevicePolicyService);
bool enabled = policyManager != null && policyManager
.StorageEncryptionStatus == EncryptionStatus.Active;
return new SecurityCheckResult(this, enabled);
return new DeviceCheckResult(this, enabled);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ namespace AeroGear.Mobile.Security.Checks
/// <summary>
/// A check for whether the device the application is running on is rooted.
/// </summary>
public class NonRootedCheck : AbstractSecurityCheck
public class RootEnabledCheck : AbstractDeviceCheck
{
protected override string Name => "Rooted Check";

private readonly Context context;

public NonRootedCheck(Context ctx)
public RootEnabledCheck(Context ctx)
{
this.context = ctx;
}
Expand All @@ -24,10 +24,10 @@ public NonRootedCheck(Context ctx)
/// Solution found at https://stackoverflow.com/a/8097801.
/// </summary>
/// <returns>The check.</returns>
public override SecurityCheckResult Check()
public override DeviceCheckResult Check()
{
bool rooted = CheckForTestKeys() || CheckForSuBinary() || CheckSuExists();
return new SecurityCheckResult(this, !rooted);
return new DeviceCheckResult(this, rooted);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@ namespace AeroGear.Mobile.Security.Checks
/// <summary>
/// A check for whether the device the application is running on has a screen lock.
/// </summary>
public class ScreenLockCheck : AbstractSecurityCheck
public class ScreenLockEnabledCheck : AbstractDeviceCheck
{
protected override string Name => "Screen Lock Check";

private readonly Context context;

public ScreenLockCheck(Context ctx)
public ScreenLockEnabledCheck(Context ctx)
{
this.context = ctx;
}

public override SecurityCheckResult Check()
public override DeviceCheckResult Check()
{
KeyguardManager manager = (KeyguardManager)context.GetSystemService(Context.KeyguardService);
return new SecurityCheckResult(this, manager.IsDeviceSecure);
return new DeviceCheckResult(this, manager.IsDeviceSecure);
}
}
}
77 changes: 77 additions & 0 deletions Security/Security.Platform.Android/DeviceChecks.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
using System;
using System.Collections.Generic;
using AeroGear.Mobile.Core.Utils;
using AeroGear.Mobile.Security.Checks;

namespace AeroGear.Mobile.Security
{
/// <summary>
/// This class enums all the provided security checks.
///
/// To get an instance of the check, use the following code:
/// <code>
/// <![CDATA[
/// var deviceChek = ServiceFinder.Resolve<IDeviceCheckFactory>().create(DeviceChecks.NOT_ROOTED);
/// ]]>
/// </code>
/// </summary>
public class DeviceChecks : IDeviceCheckType
{
private static Dictionary<string, DeviceChecks> typesByName = new Dictionary<string, DeviceChecks>();

public static readonly DeviceChecks ROOT_ENABLED = new DeviceChecks(typeof(RootEnabledCheck));
public static readonly DeviceChecks DEVELOPER_MODE_ENABLED = new DeviceChecks(typeof(DeveloperModeEnabledCheck));
public static readonly DeviceChecks IS_EMULATOR = new DeviceChecks(typeof(EmulatorCheck));
public static readonly DeviceChecks SCREEN_LOCK_ENABLED = new DeviceChecks(typeof(ScreenLockEnabledCheck));
public static readonly DeviceChecks BACKUP_ALLOWED = new DeviceChecks(typeof(BackupAllowedCheck));
public static readonly DeviceChecks ENCRYPTION_ENABLED = new DeviceChecks(typeof(EncryptionEnabledCheck));
public static readonly DeviceChecks DEBUGGER_ENABLED = new DeviceChecks(typeof(DebuggerEnabledCheck));

// add others checks here
// i.e.
// public static readonly DeviceChecks NO_DEBUGGER = new DeviceChecks(typeof(NoDebuggerCheck));
// this way the user will be able to do an enum like selection:
// DeviceChecks.NOT_JAILBROKEN

internal readonly Type CheckType;

/// <summary>
/// Initializes a new instance of the <see cref="T:AeroGear.Mobile.Security.DeviceChecks"/> class.
/// Private so that it can't be instantiated externally: useful to emulate an enum.
/// </summary>
/// <param name="checkType">The class type of the check represented by this instance.</param>
private DeviceChecks(Type checkType, string friendlyName = null)
{
if (!ServiceFinder.IsRegistered<IDeviceCheckFactory>())
{
ServiceFinder.RegisterInstance<IDeviceCheckFactory>(AndroidDeviceCheckFactory.INSTANCE);
}

this.CheckType = checkType;
typesByName[friendlyName ?? checkType.Name] = this;
}

/// <summary>
/// Returns an the DeviceChecks instance identified by the passed in name.
/// </summary>
/// <returns>The DeviceChecks instance identified by the passed in name or <code>null</code> if not found.</returns>
/// <param name="name">Name.</param>
public static DeviceChecks GetDeviceCheck(string name)
{
if (typesByName.ContainsKey(name))
{
return typesByName[name];
}
return null;
}

/// <summary>
/// Returns all the checks.
/// </summary>
/// <returns>All the checks.</returns>
public static ICollection<DeviceChecks> GetAllChecks()
{
return typesByName.Values;
}
}
}
Loading