Пример #1
0
        private void SetSeed(ProtectedString value)
        {
            ProtectedString work = SanitizeSeed(value, Encoding);

            m_seed = work;
            try
            {
                if (Encoding == KPOTPEncoding.BASE32)
                {
                    while ((work.Length % 8) != 0)
                    {
                        work += new ProtectedString(true, "=");
                    }
                    m_key = PSConvert.ToBASE32(work);
                }
                else if (Encoding == KPOTPEncoding.BASE64)
                {
                    while ((work.Length % 8) != 0)
                    {
                        work += new ProtectedString(true, "=");
                    }
                    m_key = Convert.FromBase64CharArray(work.ReadChars(), 0, work.Length);
                }
                else if (Encoding == KPOTPEncoding.HEX)
                {
                    m_key = PSConvert.HexStringToByteArray(value);
                }
                else if (Encoding == KPOTPEncoding.UTF8)
                {
                    m_key = work.ReadUtf8();
                }
                else
                {
                    if (m_key != null)
                    {
                        m_key.Initialize();
                    }
                    m_seed = ProtectedString.EmptyEx;
                }
            }
            catch
            {
                if (m_key != null)
                {
                    m_key.Initialize();
                }
                m_seed = ProtectedString.EmptyEx;
            }
        }
Пример #2
0
 private ProtectedString CheckAndConvertGoogleAuthFormat(ProtectedString otp)
 {
     if (otp.ReadString().ToLowerInvariant().StartsWith("otpauth-migration://offline?data="))
     {
         int iOTPCount = 0;
         try { otp = PSConvert.ParseGoogleAuthExport(otp.ReadString(), out iOTPCount); }
         catch { }
         if (iOTPCount > 1)
         {
             Tools.ShowError(string.Format(PluginTranslate.ErrorGoogleAuthImportCount, iOTPCount.ToString()));
         }
         else if (iOTPCount == 0)
         {
             Tools.ShowError(PluginTranslate.ErrorGoogleAuthImport);
         }
     }
     return(otp);
 }
Пример #3
0
        private void UpdatePreview()
        {
            if (m_NoUpdate)
            {
                return;
            }
            if (StrUtil.IsDataUri(tbOTPSeed.Text))
            {
                try
                {
                    OTP.OTPAuthString = ParseFromImageByteArray(StrUtil.DataUriToData(tbOTPSeed.Text));
                    InitSettings(true);
                }
                catch { tbOTPSeed.Text = string.Empty; }
            }
            if (tbOTPSeed.Text.ToLowerInvariant().StartsWith("otpauth://"))
            {
                OTP.OTPAuthString = new ProtectedString(true, tbOTPSeed.Text);
                InitSettings(true);
            }
            else if (tbOTPSeed.Text.ToLowerInvariant().StartsWith("otpauth-migration://"))
            {
                m_NoUpdate = true;
                try
                {
                    int             iCount;
                    ProtectedString psGoogleAuth = PSConvert.ParseGoogleAuthExport(tbOTPSeed.Text, out iCount);
                    if ((iCount == 1) && (psGoogleAuth.Length > 0))
                    {
                        //tbOTPSeed.Text = psGoogleAuth.ReadString();
                        OTP.OTPAuthString = psGoogleAuth;
                        InitSettings(true);
                        return;
                    }
                    tbOTPSeed.Text = string.Empty;

                    if (iCount > 1)
                    {
                        Tools.ShowError(string.Format(PluginTranslate.ErrorGoogleAuthImportCount, iCount.ToString()));
                    }
                    else if (iCount == 0)
                    {
                        Tools.ShowError(PluginTranslate.ErrorGoogleAuthImport);
                    }
                    return;
                }
                finally { m_NoUpdate = false; }
            }

            else
            {
                OTP.OTPSeed = new ProtectedString(true, tbOTPSeed.Text);
            }

            if (cbOTPFormat.SelectedIndex == 0)
            {
                OTP.Encoding = KPOTPEncoding.BASE32;
            }
            if (cbOTPFormat.SelectedIndex == 1)
            {
                OTP.Encoding = KPOTPEncoding.BASE64;
            }
            if (cbOTPFormat.SelectedIndex == 2)
            {
                OTP.Encoding = KPOTPEncoding.HEX;
            }
            if (cbOTPFormat.SelectedIndex == 3)
            {
                OTP.Encoding = KPOTPEncoding.UTF8;
            }

            OTP.Length = cbOTPLength.SelectedIndex + 6;

            int dummy = -1;

            if (int.TryParse(tbTOTPTimestep.Text, out dummy))
            {
                OTP.TOTPTimestep = dummy;
            }
            if (int.TryParse(tbHOTPCounter.Text, out dummy))
            {
                OTP.HOTPCounter = dummy;
            }

            if (cbOTPHashFunc.SelectedIndex == 0)
            {
                OTP.Hash = KPOTPHash.SHA1;
            }
            if (cbOTPHashFunc.SelectedIndex == 1)
            {
                OTP.Hash = KPOTPHash.SHA256;
            }
            if (cbOTPHashFunc.SelectedIndex == 2)
            {
                OTP.Hash = KPOTPHash.SHA512;
            }

            if (cbOTPType.SelectedIndex == 0)
            {
                OTP.Type = KPOTPType.HOTP;
            }
            else if (cbOTPType.SelectedIndex == 1)
            {
                OTP.Type = KPOTPType.TOTP;
            }
            else if (cbOTPType.SelectedIndex == 2)
            {
                OTP.Type = KPOTPType.STEAM;
            }

            pbTOTPLifetime.Maximum = OTP.TOTPTimestep;
            pbTOTPLifetime.Value   = OTP.RemainingSeconds;
            gTime.Enabled          = tbTOTPTimestep.Visible = lTimestep.Visible = OTP.Type == KPOTPType.TOTP;
            pbTOTPLifetime.Visible = OTP.Type != KPOTPType.HOTP;
            tbHOTPCounter.Visible  = lCounter.Visible = OTP.Type == KPOTPType.HOTP;

            cbOTPLength.Enabled = OTP.Type != KPOTPType.STEAM;
            if (OTP.Type == KPOTPType.STEAM)
            {
                if (!cbOTPLength.Items.Contains("5"))
                {
                    cbOTPLength.Items.Add("5");
                }
                cbOTPLength.SelectedIndex = cbOTPLength.Items.Count - 1;
            }
            else if (cbOTPLength.Items.Contains("5"))
            {
                cbOTPLength.Items.RemoveAt(cbOTPLength.Items.Count - 1);
                cbOTPLength.SelectedIndex = 0;
            }

            if (!tbTOTPTimeCorrectionURL.Focused)
            {
                if (totpTimeCorrectionType.SelectedIndex == 0)
                {
                    if (!tbTOTPTimeCorrectionURL.ReadOnly)
                    {
                        m_BackupURL = tbTOTPTimeCorrectionURL.Text;
                    }
                    tbTOTPTimeCorrectionURL.Text     = string.Empty;
                    tbTOTPTimeCorrectionURL.ReadOnly = true;
                    OTP.TimeCorrectionUrlOwn         = false;
                    OTP.TimeCorrectionUrl            = string.Empty;
                }
                else if (totpTimeCorrectionType.SelectedIndex == 1)
                {
                    if (!tbTOTPTimeCorrectionURL.ReadOnly)
                    {
                        m_BackupURL = tbTOTPTimeCorrectionURL.Text;
                    }
                    tbTOTPTimeCorrectionURL.Text     = EntryUrl;
                    tbTOTPTimeCorrectionURL.ReadOnly = true;
                    OTP.TimeCorrectionUrlOwn         = true;
                    OTP.TimeCorrectionUrl            = EntryUrl;
                }
                else
                {
                    if (tbTOTPTimeCorrectionURL.ReadOnly)
                    {
                        tbTOTPTimeCorrectionURL.Text = string.IsNullOrEmpty(m_BackupURL) ? EntryUrl : m_BackupURL;
                    }
                    tbTOTPTimeCorrectionURL.ReadOnly = false;
                    OTP.TimeCorrectionUrlOwn         = false;
                    OTP.TimeCorrectionUrl            = tbTOTPTimeCorrectionURL.Text;
                }
            }
            totpTimeCorrectionValue.Text = OTP.OTPTimeCorrection.ToString();

            string otpValue = OTP.Valid ? OTP.ReadableOTP(OTP.GetOTP(false, true)) : PluginTranslate.Error;

            otpPreview.Text = "OTP: " + (string.IsNullOrEmpty(otpValue) ? "N/A" : otpValue);
            if ((OTP.Type != KPOTPType.HOTP) && OTP.RemainingSeconds <= Config.TOTPSoonExpiring)
            {
                otpPreview.ForeColor     = System.Drawing.Color.Red;
                pbTOTPLifetime.ForeColor = System.Drawing.Color.Red;
            }
            else
            {
                otpPreview.ForeColor     = System.Drawing.SystemColors.ControlText;
                pbTOTPLifetime.ForeColor = System.Drawing.SystemColors.Highlight;
            }

            otpValue            = OTP.Valid ? OTP.ReadableOTP(OTP.GetOTP(true, true)) : PluginTranslate.Error;
            otpPreviewNext.Text = "Next: " + (string.IsNullOrEmpty(otpValue) ? "N/A" : otpValue);
        }