try to generate key with and without date

This commit is contained in:
Kyle Spearrin 2018-06-29 10:39:29 -04:00
parent db7f2622c8
commit 61c480618c
1 changed files with 26 additions and 12 deletions

View File

@ -44,7 +44,15 @@ namespace Bit.Android.Services
_keyStore = KeyStore.GetInstance(AndroidKeyStore); _keyStore = KeyStore.GetInstance(AndroidKeyStore);
_keyStore.Load(null); _keyStore.Load(null);
GenerateStoreKey(); try
{
GenerateStoreKey(true);
}
catch
{
GenerateStoreKey(false);
}
GenerateAesKey(); GenerateAesKey();
} }
@ -128,7 +136,7 @@ namespace Bit.Android.Services
} }
} }
private void GenerateStoreKey() private void GenerateStoreKey(bool withDate)
{ {
if(_keyStore.ContainsAlias(KeyAlias)) if(_keyStore.ContainsAlias(KeyAlias))
{ {
@ -144,27 +152,33 @@ namespace Bit.Android.Services
{ {
var subject = new X500Principal($"CN={KeyAlias}"); var subject = new X500Principal($"CN={KeyAlias}");
var spec = new KeyPairGeneratorSpec.Builder(Application.Context) var builder = new KeyPairGeneratorSpec.Builder(Application.Context)
.SetAlias(KeyAlias) .SetAlias(KeyAlias)
.SetSubject(subject) .SetSubject(subject)
.SetSerialNumber(BigInteger.Ten) .SetSerialNumber(BigInteger.Ten);
//.SetStartDate(new Date(0))
//.SetEndDate(end.Time)
.Build();
if(withDate)
{
builder.SetStartDate(new Date(0)).SetEndDate(end.Time);
}
var spec = builder.Build();
var gen = KeyPairGenerator.GetInstance(KeyProperties.KeyAlgorithmRsa, AndroidKeyStore); var gen = KeyPairGenerator.GetInstance(KeyProperties.KeyAlgorithmRsa, AndroidKeyStore);
gen.Initialize(spec); gen.Initialize(spec);
gen.GenerateKeyPair(); gen.GenerateKeyPair();
} }
else else
{ {
var spec = new KeyGenParameterSpec.Builder(KeyAlias, KeyStorePurpose.Decrypt | KeyStorePurpose.Encrypt) var builder = new KeyGenParameterSpec.Builder(KeyAlias, KeyStorePurpose.Decrypt | KeyStorePurpose.Encrypt)
.SetBlockModes(KeyProperties.BlockModeGcm) .SetBlockModes(KeyProperties.BlockModeGcm)
.SetEncryptionPaddings(KeyProperties.EncryptionPaddingNone) .SetEncryptionPaddings(KeyProperties.EncryptionPaddingNone);
//.SetKeyValidityStart(new Date(0))
//.SetKeyValidityEnd(end.Time)
.Build();
if(withDate)
{
builder.SetKeyValidityStart(new Date(0)).SetKeyValidityEnd(end.Time);
}
var spec = builder.Build();
var gen = KeyGenerator.GetInstance(KeyProperties.KeyAlgorithmAes, AndroidKeyStore); var gen = KeyGenerator.GetInstance(KeyProperties.KeyAlgorithmAes, AndroidKeyStore);
gen.Init(spec); gen.Init(spec);
gen.GenerateKey(); gen.GenerateKey();