private void IncrementSession() { if (cboKeys.SelectedIndex < 0) { return; } System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); YubikeysSection keysSection = (YubikeysSection)config.GetSection("tokens"); YubikeySettings key = keysSection.Keys[((YubikeySettings)cboKeys.SelectedItem).Name]; ++key.SessionCounter; key.UseCounter = 0; key.StartTime = DateTime.Now; byte[] buffer = new byte[3]; RNGCryptoServiceProvider.Create().GetBytes(buffer); key.TimeStamp = (((int)buffer[0]) << 16) | (((int)buffer[1]) << 8) | (int)buffer[2]; config.Save(ConfigurationSaveMode.Modified); ConfigurationManager.RefreshSection("tokens"); int index = cboKeys.SelectedIndex; YubikeysCollection keys = ((YubikeysCollection)cboKeys.DataSource); keys[index] = key; cboKeys.DataSource = keys; }
private void btnCreateOTP_Click(object sender, EventArgs e) { if (cboKeys.SelectedIndex < 0) { return; } YubikeySettings key = ((YubikeysCollection)cboKeys.DataSource)[cboKeys.SelectedIndex]; string otp = OTPCreator.CreateOTP(key, this); txtOTP.Text = otp; Clipboard.SetData(DataFormats.StringFormat, otp); }
void _enterOTPHandler_OnHotKeyEvent(object sender, EventArgs e) { if (cboKeys.SelectedIndex < 0) { return; } YubikeySettings key = ((YubikeysCollection)cboKeys.DataSource)[cboKeys.SelectedIndex]; string otp = OTPCreator.CreateOTP(key, this); txtOTP.Text = otp; SendKeys.SendWait(otp); if (key.PressEnter) { SendKeys.Flush(); Thread.Sleep(_enterKeyDelay); SendKeys.Send("{ENTER}"); } }
public static string CreateOTP(YubikeySettings key, Form1 form) { string tokenID = ModHex.Encode(key.TokenID); // Assemble key unencrypted data byte[] keyBytes = new byte[16]; for (int i = 0; i < key.PrivateID.Length; ++i) { keyBytes[i] = key.PrivateID[i]; } keyBytes[6] = (byte)(key.SessionCounter & 0xff); keyBytes[7] = (byte)((key.SessionCounter >> 8) & 0xff); form.SessionCounter = key.SessionCounter.ToString(); TimeSpan diff = DateTime.Now - key.StartTime; int timer = (int)((((uint)(diff.TotalSeconds / TS_SEC) & 0x00FFFFFF) + key.TimeStamp) & 0x00FFFFFF); form.Timestamp = timer.ToString(); keyBytes[8] = (byte)(timer & 0xff); keyBytes[9] = (byte)((timer >> 8) & 0xff); keyBytes[10] = (byte)((timer >> 16) & 0xff); keyBytes[11] = key.UseCounter++; form.UseCounter = keyBytes[11].ToString(); byte[] buffer = new byte[2]; RNGCryptoServiceProvider.Create().GetBytes(buffer); form.Random = (((int)buffer[1] << 8) + (int)buffer[0]).ToString(); keyBytes[12] = buffer[0]; keyBytes[13] = buffer[1]; CRC(keyBytes); using (Rijndael aes = Rijndael.Create()) { aes.Padding = PaddingMode.None; aes.Mode = CipherMode.ECB; using (ICryptoTransform xform = aes.CreateEncryptor(key.Secret, new byte[16])) { byte[] plainBytes = new byte[16]; xform.TransformBlock(keyBytes, 0, keyBytes.Length, plainBytes, 0); string otp = tokenID + ModHex.Encode(plainBytes); return(otp); } } }
bool AddKey(string name, byte[] secret, byte[] tokenID, byte[] privateID, int sessionCounter, bool pressEnter) { YubikeySettings key = new YubikeySettings(); key.Name = name; key.Secret = secret; key.TokenID = tokenID; key.PrivateID = privateID; key.SessionCounter = sessionCounter; key.PressEnter = pressEnter; YubikeysCollection keys = (YubikeysCollection)dataGridView1.DataSource; if (keys.Contains(key)) { return(false); } keys.Add(key); dataGridView1.DataSource = null; dataGridView1.Invalidate(); dataGridView1.DataSource = keys; return(true); }
private void PopulateKeyList() { System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); YubikeysSection keysSection = (YubikeysSection)config.GetSection("tokens"); lock (cboKeys) { this.cboKeys.SelectedIndexChanged -= _indexChangedHandler; this.cmcboKeys.SelectedIndexChanged -= _contextMenuIndexChangedHandler; YubikeySettings selectedItem = (YubikeySettings)this.cboKeys.SelectedItem; this.cboKeys.DataSource = null; foreach (YubikeySettings key in keysSection.Keys) { key.StartTime = DateTime.Now; } this.cmcboKeys.Items.AddRange(keysSection.Keys.GetAll()); this.cboKeys.DataSource = keysSection.Keys; this.cboKeys.ValueMember = "Name"; this.cboKeys.DisplayMember = "Name"; if (selectedItem == null) { this.cboKeys.SelectedIndex = -1; this.cmcboKeys.SelectedIndex = -1; } else { int selectedIndex = keysSection.Keys.IndexOf(selectedItem); this.cboKeys.SelectedIndex = selectedIndex; this.cmcboKeys.SelectedIndex = selectedIndex; } this.cboKeys.SelectedIndexChanged += _indexChangedHandler; this.cmcboKeys.SelectedIndexChanged += _contextMenuIndexChangedHandler; } IncrementSession(); }