public static OtpAuthData uriToOtpAuthData(Uri uri) { if (uri.Scheme == "otpauth") { OtpAuthData data = new OtpAuthData(); data.Type = (OtpType)Enum.Parse(typeof(OtpType), uri.Host, true); string query = uri.Query; if (query.StartsWith("?")) { query = query.TrimStart('?'); } NameValueCollection parameters = ParseQueryString(query); if (parameters[uriSecretKey] != null) { if (data.Type == OtpType.Totp && parameters[KeeOtp1EncoderParameter] != null) { data.Type = (OtpType)Enum.Parse(typeof(OtpType), parameters[KeeOtp1EncoderParameter], true); } data.Encoding = OtpSecretEncoding.Base32; string secret = correctPlainSecret(parameters[uriSecretKey], data.Encoding); // Validate secret (catch) OtpAuthUtils.validatePlainSecret(secret, data.Encoding); data.SetPlainSecret(secret); if (parameters[uriAlgorithmKey] != null) { data.Algorithm = (OtpHashMode)Enum.Parse(typeof(OtpHashMode), parameters[uriAlgorithmKey], true); } if (data.Type == OtpType.Totp) { data.Period = GetIntOrDefault(parameters, uriPeriodKey, 30); } else if (data.Type == OtpType.Hotp) { data.Counter = GetIntOrDefault(parameters, uriCounterKey, 0); } data.Digits = GetIntOrDefault(parameters, uriDigitsKey, 6); return(data); } else { throw new InvalidUriFormat("The Uri does not contain a secret. A secret is required!"); } } else { throw new InvalidUriFormat("Given Uri does not start with 'otpauth://'!"); } }
private OtpAuthData readData(bool skipType = false) { if (OtpAuthUtils.checkUriString(textBoxKey.Text)) { OtpAuthData data = OtpAuthUtils.uriToOtpAuthData(new Uri(textBoxKey.Text)); if (this.data != null) { data.loadedFields = this.data.loadedFields; } return(data); } else { OtpAuthData data = new OtpAuthData(); if (this.data != null) { data = (OtpAuthData)this.data.Clone(); } string secret = textBoxKey.Text.Replace(" ", string.Empty).Replace("-", string.Empty); if (string.IsNullOrEmpty(this.textBoxKey.Text)) { throw new InvalidOtpConfiguration(KeeOtp2Statics.InvalidOtpConfigurationMissingSecret); } if (checkBoxCustomSettings.Checked) { if (this.radioButtonBase32.Checked) { data.Encoding = OtpSecretEncoding.Base32; } else if (this.radioButtonBase64.Checked) { data.Encoding = OtpSecretEncoding.Base64; } else if (this.radioButtonHex.Checked) { data.Encoding = OtpSecretEncoding.Hex; } else if (this.radioButtonUtf8.Checked) { data.Encoding = OtpSecretEncoding.UTF8; } } // Secret validation, will throw an error if invalid secret = OtpAuthUtils.correctPlainSecret(secret, data.Encoding); OtpAuthUtils.validatePlainSecret(secret, data.Encoding); if (checkBoxCustomSettings.Checked) { if (!skipType) { data.Type = comboBoxTypeIndexValue[comboBoxType.SelectedIndex]; } if (data.Type == OtpType.Totp || data.Type == OtpType.Steam) { int period = 30; if (int.TryParse(this.textBoxPeriodCounter.Text, out period)) { if (period <= 0) { throw new InvalidOtpConfiguration(KeeOtp2Statics.InvalidOtpConfigurationInvalidInteger); } } else { throw new InvalidOtpConfiguration(KeeOtp2Statics.InvalidOtpConfigurationInvalidInteger); } data.Period = period; } else if (data.Type == OtpType.Hotp) { int counter = 0; if (int.TryParse(this.textBoxPeriodCounter.Text, out counter)) { if (counter < 0) { throw new InvalidOtpConfiguration(KeeOtp2Statics.InvalidOtpConfigurationInvalidInteger); } } else { throw new InvalidOtpConfiguration(KeeOtp2Statics.InvalidOtpConfigurationInvalidInteger); } data.Counter = counter; } data.Digits = comboBoxLengthIndexValue[comboBoxLength.SelectedIndex]; if (this.radioButtonSha1.Checked) { data.Algorithm = OtpHashMode.Sha1; } else if (this.radioButtonSha256.Checked) { data.Algorithm = OtpHashMode.Sha256; } else if (this.radioButtonSha512.Checked) { data.Algorithm = OtpHashMode.Sha512; } data.KeeOtp1Mode = checkboxOldKeeOtp.Checked; } data.SetPlainSecret(secret); return(data); } }
public static OtpAuthData loadDataFromBuiltInOtp(PwEntry entry) { OtpAuthData data = new OtpAuthData(); data.Type = checkBuiltInType(entry); data.loadedFields = new List <string>(); string currentOtpPrefix = builtInTotpPrefix; if (data.Type == OtpType.Hotp) { currentOtpPrefix = builtInHotpPrefix; } string secretBase32Key = currentOtpPrefix + builtInBase32Suffix; string secretBase64Key = currentOtpPrefix + builtInBase64Suffix; string secretHexKey = currentOtpPrefix + builtInHexSuffix; string secretUTF8Key = currentOtpPrefix + builtInUtf8Suffix; if (entry.Strings.Exists(secretBase32Key)) { data.Encoding = OtpSecretEncoding.Base32; data.SetPlainSecret(correctPlainSecret(entry.Strings.Get(secretBase32Key).ReadString(), data.Encoding)); data.loadedFields.Add(secretBase32Key); } else if (entry.Strings.Exists(secretBase64Key)) { data.Encoding = OtpSecretEncoding.Base64; data.SetPlainSecret(correctPlainSecret(entry.Strings.Get(secretBase64Key).ReadString(), data.Encoding)); data.loadedFields.Add(secretBase64Key); } else if (entry.Strings.Exists(secretHexKey)) { data.Encoding = OtpSecretEncoding.Hex; data.SetPlainSecret(entry.Strings.Get(secretHexKey).ReadString()); data.loadedFields.Add(secretHexKey); } else if (entry.Strings.Exists(secretUTF8Key)) { data.Encoding = OtpSecretEncoding.UTF8; data.SetPlainSecret(entry.Strings.Get(secretUTF8Key).ReadString()); data.loadedFields.Add(secretUTF8Key); } else { return(null); } string lengthKey = currentOtpPrefix + builtInLengthSuffix; if (entry.Strings.Exists(lengthKey)) { int size; if (int.TryParse(entry.Strings.Get(lengthKey).ReadString(), out size)) { data.Digits = size; data.loadedFields.Add(lengthKey); if (data.Type == OtpType.Hotp && data.Digits != 6) { data.Proprietary = false; } } } string hashModeKey = currentOtpPrefix + builtInAlgorithmSuffix; if (entry.Strings.Exists(hashModeKey)) { string hashMode = entry.Strings.Get(hashModeKey).ReadString(); if (hashMode == builtInOtpHashModeSha1) { data.Algorithm = OtpHashMode.Sha1; } else if (hashMode == builtInOtpHashModeSha256) { data.Algorithm = OtpHashMode.Sha256; } else if (hashMode == builtInOtpHashModeSha512) { data.Algorithm = OtpHashMode.Sha512; } data.loadedFields.Add(hashModeKey); if (data.Type == OtpType.Hotp && data.Algorithm != OtpHashMode.Sha1) { data.Proprietary = false; } } if (data.Type == OtpType.Totp) { string periodKey = currentOtpPrefix + builtInPeriodSuffix; if (entry.Strings.Exists(periodKey)) { int period; if (int.TryParse(entry.Strings.Get(periodKey).ReadString(), out period)) { data.Period = period; data.loadedFields.Add(periodKey); } } } else if (data.Type == OtpType.Hotp) { string counterKey = currentOtpPrefix + builtInCounterSuffix; if (entry.Strings.Exists(counterKey)) { int counter; if (int.TryParse(entry.Strings.Get(counterKey).ReadString(), out counter)) { data.Counter = counter; data.loadedFields.Add(counterKey); } } } return(data); }
public static OtpAuthData loadDataFromKeeOtp1String(PwEntry entry) { if (checkUriString(entry.Strings.Get(StringDictionaryKey).ReadString())) { OtpAuthData data = uriToOtpAuthData(new Uri(entry.Strings.Get(StringDictionaryKey).ReadString())); data.loadedFields = new List <string>() { StringDictionaryKey }; data.Proprietary = false; return(data); } else { NameValueCollection parameters = ParseQueryString(entry.Strings.Get(StringDictionaryKey).ReadString()); if (parameters[KeeOtp1KeyParameter] == null) { throw new ArgumentException("Must have a key in the data"); } OtpAuthData data = new OtpAuthData(); data.loadedFields = new List <string>() { StringDictionaryKey }; if (parameters[KeeOtp1TypeParameter] != null) { data.Type = (OtpType)Enum.Parse(typeof(OtpType), parameters[KeeOtp1TypeParameter], true); } if (data.Type == OtpType.Totp && parameters[KeeOtp1EncoderParameter] != null) { data.Type = (OtpType)Enum.Parse(typeof(OtpType), parameters[KeeOtp1EncoderParameter], true); } if (data.Type == OtpType.Steam) { data.Proprietary = false; } if (parameters[KeeOtp1EncodingParameter] != null) { data.Encoding = (OtpSecretEncoding)Enum.Parse(typeof(OtpSecretEncoding), parameters[KeeOtp1EncodingParameter], true); } data.SetPlainSecret(correctPlainSecret(parameters[KeeOtp1KeyParameter].Replace("%3d", "="), data.Encoding)); if (parameters[KeeOtp1OtpHashModeParameter] != null) { data.Algorithm = (OtpHashMode)Enum.Parse(typeof(OtpHashMode), parameters[KeeOtp1OtpHashModeParameter], true); } if (data.Type == OtpType.Hotp && data.Algorithm != OtpHashMode.Sha1) { data.Proprietary = false; } if (data.Type == OtpType.Totp) { data.Period = GetIntOrDefault(parameters, KeeOtp1StepParameter, 30); } else if (data.Type == OtpType.Hotp) { data.Counter = GetIntOrDefault(parameters, KeeOtp1CounterParameter, 0); } data.Digits = GetIntOrDefault(parameters, KeeOtp1SizeParameter, 6); if (data.Type == OtpType.Hotp && data.Digits != 6) { data.Proprietary = false; } return(data); } }