public static bool Save(IOConnectionInfo ioc, OtpInfo otpInfo) { Stream sOut = null; try { using (var trans = App.Kp2a.GetOtpAuxFileStorage(ioc) .OpenWriteTransaction(ioc, App.Kp2a.GetBooleanPreference(PreferenceKey.UseFileTransactions))) { var stream = trans.OpenFile(); WriteToStream(otpInfo, stream); trans.CommitWrite(); } return(true); } catch (Exception) { Debug.Assert(false); } finally { if (sOut != null) { sOut.Close(); } } return(false); }
public static void WriteToStream(OtpInfo otpInfo, Stream stream) { var xws = XmlWriterSettings(); XmlWriter xw = XmlWriter.Create(stream, xws); XmlSerializer xs = new XmlSerializer(typeof(OtpInfo)); xs.Serialize(xw, otpInfo); xw.Close(); }
/* * private static byte[] Open(KeyProviderQueryContext ctx, OtpInfo otpInfo) * { * if(otpInfo.Type != ProvType) * { * MessageService.ShowWarning("Unknown OTP generator type!"); * return null; * } * * OtpKeyPromptForm dlg = new OtpKeyPromptForm(); * dlg.InitEx(otpInfo, ctx); * if(UIUtil.ShowDialogAndDestroy(dlg) != DialogResult.OK) * return null; * * if(!CreateAuxFile(otpInfo, ctx)) return null; * return otpInfo.Secret; * } * */ /// <summary> /// Sets the "Secret" field in otpInfo based on the list of entered OTPs (lOtps) or the entered secret itself which is in format fmt /// </summary> /// based on the code in OtpKeyPromptForm.cs public void SetSecret(OtpInfo otpInfo, List <string> lOtps, string secret, OtpDataFmt?fmt) { byte[] pbSecret = EncodingUtil.ParseKey(secret, (fmt.HasValue ? fmt.Value : OtpDataFmt.Hex)); if (pbSecret != null) { otpInfo.Secret = pbSecret; return; } if (!string.IsNullOrEmpty(otpInfo.EncryptedSecret)) // < v2.0 { byte[] pbKey32 = OtpUtil.KeyFromOtps(lOtps.ToArray(), 0, lOtps.Count, Convert.FromBase64String( otpInfo.TransformationKey), otpInfo.TransformationRounds); if (pbKey32 == null) { throw new InvalidOperationException(); } pbSecret = OtpUtil.DecryptData(otpInfo.EncryptedSecret, pbKey32, Convert.FromBase64String(otpInfo.EncryptionIV)); if (pbSecret == null) { throw new InvalidOperationException(); } otpInfo.Secret = pbSecret; otpInfo.Counter += (ulong)otpInfo.OtpsRequired; } else // >= v2.0, supporting look-ahead { bool bSuccess = false; for (int i = 0; i < otpInfo.EncryptedSecrets.Count; ++i) { OtpEncryptedData d = otpInfo.EncryptedSecrets[i]; pbSecret = OtpUtil.DecryptSecret(d, lOtps.ToArray(), 0, lOtps.Count); if (pbSecret != null) { otpInfo.Secret = pbSecret; otpInfo.Counter += ((ulong)otpInfo.OtpsRequired + (ulong)i); bSuccess = true; break; } } if (!bSuccess) { throw new InvalidOperationException(); } } }
public static bool CreateAuxFile(OtpInfo otpInfo, KeyProviderQueryContext ctx, IOConnectionInfo auxFileIoc) { otpInfo.Type = ProvType; otpInfo.Version = ProvVersion; otpInfo.Generator = ProductName; otpInfo.EncryptSecret(); if(!OtpInfo.Save(auxFileIoc, otpInfo)) { MessageService.ShowWarning("Failed to save auxiliary OTP info file:", auxFileIoc.GetDisplayName()); return false; } return true; }
public static bool CreateAuxFile(OtpInfo otpInfo, KeyProviderQueryContext ctx, IOConnectionInfo auxFileIoc) { otpInfo.Type = ProvType; otpInfo.Version = ProvVersion; otpInfo.Generator = ProductName; otpInfo.EncryptSecret(); if (!OtpInfo.Save(auxFileIoc, otpInfo)) { MessageService.ShowWarning("Failed to save auxiliary OTP info file:", auxFileIoc.GetDisplayName()); return(false); } return(true); }
public static void CreateOtpSecret(List<string> lOtps, OtpInfo otpInfo) { byte[] pbSecret; if (!string.IsNullOrEmpty(otpInfo.EncryptedSecret)) // < v2.0 { byte[] pbKey32 = OtpUtil.KeyFromOtps(lOtps.ToArray(), 0, lOtps.Count, Convert.FromBase64String( otpInfo.TransformationKey), otpInfo.TransformationRounds); if (pbKey32 == null) throw new InvalidOperationException(); pbSecret = OtpUtil.DecryptData(otpInfo.EncryptedSecret, pbKey32, Convert.FromBase64String(otpInfo.EncryptionIV)); if (pbSecret == null) throw new InvalidOperationException(); otpInfo.Secret = pbSecret; otpInfo.Counter += otpInfo.OtpsRequired; } else // >= v2.0, supporting look-ahead { bool bSuccess = false; for (int i = 0; i < otpInfo.EncryptedSecrets.Count; ++i) { OtpEncryptedData d = otpInfo.EncryptedSecrets[i]; pbSecret = OtpUtil.DecryptSecret(d, lOtps.ToArray(), 0, lOtps.Count); if (pbSecret != null) { otpInfo.Secret = pbSecret; otpInfo.Counter += ((ulong) otpInfo.OtpsRequired + (ulong) i); bSuccess = true; break; } } if (!bSuccess) throw new InvalidOperationException(); } }
private void RestoreState(Bundle savedInstanceState) { if (savedInstanceState != null) { _showPassword = savedInstanceState.GetBoolean(ShowpasswordKey, false); MakePasswordMaskedOrVisible(); _keyFileOrProvider = savedInstanceState.GetString(KeyFileOrProviderKey); _password = FindViewById<EditText>(Resource.Id.password).Text = savedInstanceState.GetString(PasswordKey); _pendingOtps = new List<string>(savedInstanceState.GetStringArrayList(PendingOtpsKey)); string otpInfoString = savedInstanceState.GetString(OtpInfoKey); if (otpInfoString != null) { XmlSerializer xs = new XmlSerializer(typeof(OtpInfo)); _otpInfo = (OtpInfo)xs.Deserialize(new StringReader(otpInfoString)); var enteredOtps = savedInstanceState.GetStringArrayList(EnteredOtpsKey); ShowOtpEntry(enteredOtps); } UpdateKeyProviderUiState(); } }
public static void WriteToStream(OtpInfo otpInfo, Stream stream) { var xws = XmlWriterSettings(); XmlWriter xw = XmlWriter.Create(stream, xws); XmlSerializer xs = new XmlSerializer(typeof (OtpInfo)); xs.Serialize(xw, otpInfo); xw.Close(); }
public static bool Save(IOConnectionInfo ioc, OtpInfo otpInfo) { Stream sOut = null; try { using (var trans = App.Kp2a.GetOtpAuxFileStorage(ioc) .OpenWriteTransaction(ioc, App.Kp2a.GetBooleanPreference(PreferenceKey.UseFileTransactions))) { var stream = trans.OpenFile(); WriteToStream(otpInfo, stream); trans.CommitWrite(); } return true; } catch(Exception) { Debug.Assert(false); } finally { if(sOut != null) sOut.Close(); } return false; }