Fix #534 by setting default language to english before creating keyge… (#536)

* Fix #534 by setting default language to english before creating keygenerator.

* Fix build!
This commit is contained in:
James Montemagno 2018-10-05 10:35:41 -07:00 коммит произвёл Jonathan Dick
Родитель d9d42b02e5
Коммит 8db259fd33
3 изменённых файлов: 62 добавлений и 20 удалений

Просмотреть файл

@ -7,13 +7,13 @@ namespace DeviceTests
public class SecureStorage_Tests
{
[Theory]
[InlineData("test.txt", "data", true)]
[InlineData("noextension", "data2", true)]
[InlineData("funny*&$%@!._/\\chars", "data3", true)]
[InlineData("test.txt2", "data2", false)]
[InlineData("noextension2", "data22", false)]
[InlineData("funny*&$%@!._/\\chars2", "data32", false)]
public async Task Saves_And_Loads(string key, string data, bool emulatePreApi23)
[InlineData("test.txt", "data", true, true)]
[InlineData("noextension", "data2", true, false)]
[InlineData("funny*&$%@!._/\\chars", "data3", true, false)]
[InlineData("test.txt2", "data2", false, true)]
[InlineData("noextension2", "data22", false, false)]
[InlineData("funny*&$%@!._/\\chars2", "data32", false, false)]
public async Task Saves_And_Loads(string key, string data, bool emulatePreApi23, bool emulateNonEnglishLocale)
{
#if __IOS__
// Try the new platform specific api
@ -26,6 +26,11 @@ namespace DeviceTests
#if __ANDROID__
SecureStorage.AlwaysUseAsymmetricKeyStorage = emulatePreApi23;
if (emulateNonEnglishLocale)
{
Platform.SetLocale(new Java.Util.Locale("ar"));
}
#endif
await SecureStorage.SetAsync(key, data);

Просмотреть файл

@ -87,6 +87,31 @@ namespace Xamarin.Essentials
internal static PowerManager PowerManager =>
AppContext.GetSystemService(Context.PowerService) as PowerManager;
internal static Java.Util.Locale GetLocale()
{
var resources = AppContext.Resources;
var config = resources.Configuration;
if (HasApiLevel(BuildVersionCodes.N))
return config.Locales.Get(0);
return config.Locale;
}
internal static void SetLocale(Java.Util.Locale locale)
{
Java.Util.Locale.Default = locale;
var resources = AppContext.Resources;
var config = resources.Configuration;
if (HasApiLevel(BuildVersionCodes.N))
config.SetLocale(locale);
else
config.Locale = locale;
#pragma warning disable CS0618 // Type or member is obsolete
resources.UpdateConfiguration(config, resources.DisplayMetrics);
#pragma warning restore CS0618 // Type or member is obsolete
}
}
class ActivityLifecycleContextListener : Java.Lang.Object, Application.IActivityLifecycleCallbacks

Просмотреть файл

@ -172,27 +172,39 @@ namespace Xamarin.Essentials
if (privateKey != null && publicKey != null)
return new KeyPair(publicKey, privateKey);
// Otherwise we create a new key
var generator = KeyPairGenerator.GetInstance(KeyProperties.KeyAlgorithmRsa, androidKeyStore);
var originalLocale = Platform.GetLocale();
try
{
// Force to english for known bug in date parsing:
// https://issuetracker.google.com/issues/37095309
Platform.SetLocale(Java.Util.Locale.English);
var end = DateTime.UtcNow.AddYears(20);
var startDate = new Java.Util.Date();
// Otherwise we create a new key
var generator = KeyPairGenerator.GetInstance(KeyProperties.KeyAlgorithmRsa, androidKeyStore);
var end = DateTime.UtcNow.AddYears(20);
var startDate = new Java.Util.Date();
#pragma warning disable CS0618 // Type or member is obsolete
var endDate = new Java.Util.Date(end.Year, end.Month, end.Day);
var endDate = new Java.Util.Date(end.Year, end.Month, end.Day);
#pragma warning restore CS0618 // Type or member is obsolete
#pragma warning disable CS0618
var builder = new KeyPairGeneratorSpec.Builder(Platform.AppContext)
.SetAlias(asymmetricAlias)
.SetSerialNumber(Java.Math.BigInteger.One)
.SetSubject(new Javax.Security.Auth.X500.X500Principal($"CN={asymmetricAlias} CA Certificate"))
.SetStartDate(startDate)
.SetEndDate(endDate);
var builder = new KeyPairGeneratorSpec.Builder(Platform.AppContext)
.SetAlias(asymmetricAlias)
.SetSerialNumber(Java.Math.BigInteger.One)
.SetSubject(new Javax.Security.Auth.X500.X500Principal($"CN={asymmetricAlias} CA Certificate"))
.SetStartDate(startDate)
.SetEndDate(endDate);
generator.Initialize(builder.Build());
generator.Initialize(builder.Build());
#pragma warning restore CS0618
return generator.GenerateKeyPair();
return generator.GenerateKeyPair();
}
finally
{
Platform.SetLocale(originalLocale);
}
}
byte[] WrapKey(IKey keyToWrap, IKey withKey)