예제 #1
0
        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);
        }
예제 #2
0
        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();
        }
예제 #3
0
        /*
         * 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();
                }
            }
        }
예제 #4
0
        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;
        }
예제 #5
0
        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);
        }
예제 #6
0
        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();
            }
        }
예제 #7
0
        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();

            }
        }
예제 #8
0
파일: OtpInfo.cs 프로젝트: pythe/wristpass
        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();
        }
예제 #9
0
파일: OtpInfo.cs 프로젝트: pythe/wristpass
        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;
        }