private static GoogleAuthenticatorImport DeserializeGoogleAuthMigrationData(byte[] b) { GoogleAuthenticatorImport gi = new GoogleAuthenticatorImport(); using (var ms = new System.IO.MemoryStream()) { ms.Write(b, 0, b.Length); ms.Position = 0; gi = ProtoBuf.Serializer.Deserialize <GoogleAuthenticatorImport>(ms); } return(gi); }
internal static ProtectedString ParseGoogleAuthExport(string s, out int iOTPCount) { iOTPCount = 0; ProtectedString psResult = ProtectedString.Empty; try { var u = new Uri(s); var param = System.Web.HttpUtility.ParseQueryString(u.Query); var b = Convert.FromBase64String(param["data"]); GoogleAuthenticatorImport gi = DeserializeGoogleAuthMigrationData(b); GoogleAuthenticatorImport.OtpParameters gAuthData = null; iOTPCount = gi.otp_parameters.Count; if (iOTPCount != 1) { using (GoogleAuthenticatorImportSelection selForm = new GoogleAuthenticatorImportSelection()) { Tools.GlobalWindowManager(selForm); selForm.InitEx(gi.otp_parameters); if (selForm.ShowDialog(KeePass.UI.GlobalWindowManager.TopWindow) == DialogResult.OK) { gAuthData = selForm.SelectedEntry; if (gAuthData != null) { iOTPCount = 1; } } else { iOTPCount = -1; } } if (iOTPCount != 1) { throw new ArgumentException("Expected exactly one OTP object, found: " + iOTPCount.ToString()); } } else { gAuthData = gi.otp_parameters[0]; } KPOTP otp = new KPOTP(); switch (gAuthData.Algorithm) { case GoogleAuthenticatorImport.Algorithm.AlgorithmSha256: otp.Hash = KPOTPHash.SHA256; break; case GoogleAuthenticatorImport.Algorithm.AlgorithmSha512: otp.Hash = KPOTPHash.SHA512; break; default: otp.Hash = KPOTPHash.SHA1; break; } switch (gAuthData.Type) { case GoogleAuthenticatorImport.OtpType.OtpTypeHotp: otp.Type = KPOTPType.HOTP; otp.HOTPCounter = (int)gAuthData.Counter; break; default: otp.Type = KPOTPType.TOTP; break; } switch (gAuthData.Digits) { case GoogleAuthenticatorImport.DigitCount.DigitCountEight: otp.Length = 8; break; default: otp.Length = 6; break; } otp.Issuer = gAuthData.Issuer; otp.Label = string.IsNullOrEmpty(gAuthData.Issuer) ? gAuthData.Name : gAuthData.Name.Remove(0, gAuthData.Issuer.Length + 1); otp.Encoding = KPOTPEncoding.BASE32; byte[] bSeed = ConvertBase64ToBase32(gAuthData.Secret); otp.OTPSeed = new ProtectedString(true, bSeed); psResult = otp.OTPAuthString; } catch { } return(psResult); }