/// <summary> /// Determines whether two ATR instances are equal. /// </summary> /// <param name="obj">The Object to compare with the current ATR.</param> /// <returns><b>true</b> if the specified Object is equal to the current ATR; otherwise, <b>false</b>. </returns> public override bool Equals(object obj) { Atr a = obj as Atr; if (a == null) { return(false); } if (a.m_Atr.Length != this.m_Atr.Length) { return(false); } // first ensure that the masks are equal for (int i = 0; i < this.m_Mask.Length; i++) { if (a.m_Mask[i] != this.m_Mask[i]) { return(false); } } // then make sure that the ATR, with the masks applied, are equal for (int i = 0; i < this.m_Atr.Length; i++) { if ((a.m_Atr[i] & a.m_Mask[i]) != (this.m_Atr[i] & this.m_Mask[i])) { return(false); } } return(true); }
/// <summary> /// Checks whether a given ATR matches with this ATR. /// </summary> /// <param name="atr">The ATR to check.</param> /// <returns><b>true</b> if the given ATR matches with this Atr instance, <b>false</b> otherwise.</returns> /// <exception cref="ArgumentNullException"><i>atr</i> is a null reference.</exception> public bool Match(Atr atr) { if (atr == null) { throw new ArgumentNullException("atr", ResourceController.GetString("Error_ParamNull")); } return(InternalMatch(atr.m_Atr)); }
/// <summary> /// Initializes a new instance of the SelectReaderForm. /// </summary> /// <param name="atrs">A list of ATRs that are considered valid.</param> /// <exception cref="ArgumentException"><i>atrs</i> is not a null reference, but it is empty or contains null references.</exception> public SelectReaderForm(Atr[] atrs) { if (atrs != null && atrs.Length == 0) throw new ArgumentException(ResourceController.GetString("Error_ParamInvalid"), "atrs"); if (atrs != null) { for (int i = 0; i < atrs.Length; i++) { if (atrs[i] == null) throw new ArgumentException(ResourceController.GetString("Error_ParamInvalid"), "atrs"); } } InitializeComponent(); m_Atrs = atrs; int ret = NativeMethods.SCardEstablishContext(NativeMethods.SCARD_SCOPE_USER, IntPtr.Zero, IntPtr.Zero, out m_Context); if (ret != NativeMethods.SCARD_S_SUCCESS) throw new SmartcardException(ResourceController.GetString("Error_SmartcardEstablishContext")); }
private void RefreshStatus() { IntPtr readerName; int nameLength = NativeMethods.SCARD_AUTOALLOCATE; byte[] atrBuffer = new byte[32]; int atrLength = atrBuffer.Length; int ret = NativeMethods.SCardStatus(m_Card, out readerName, ref nameLength, out m_State, out m_ActiveProtocol, atrBuffer, ref atrLength); try { if (ret != NativeMethods.SCARD_S_SUCCESS || atrLength < 0) { throw new SmartcardException(ResourceController.GetString("Error_SmartcardGetStatus"), ret); } } finally { NativeMethods.SCardFreeMemory(m_Context, readerName); } m_Atr = new Atr(atrBuffer, atrLength); }
/// <summary> /// Opens a SmartcardReader that has a card inserted with a specified ATR. /// </summary> /// <param name="atr">The ATR to search for.</param> /// <returns>An instance of the SmartcardReader class -or- a null reference if no smartcard reader was found.</returns> /// <exception cref="ArgumentNullException"><i>atr</i> is a null reference.</exception> /// <exception cref="SmartcardException">An error occurred while communication with the smartcard reader.</exception> public static SmartcardReader OpenReader(Atr atr) { if (atr == null) { throw new ArgumentNullException("atr", ResourceController.GetString("Error_ParamNull")); } string[] readers = SmartcardReader.GetReaders(); SmartcardReader sr = null; foreach (string reader in readers) { sr = new SmartcardReader(reader); sr.Connect(); if (atr.IsMatch(sr.Atr.GetValue())) { break; } sr.Dispose(); sr = null; } return(sr); }
/// <summary> /// Adds an ATR to the allowed list. /// </summary> /// <param name="atr">The ATR to add.</param> /// <exception cref="ArgumentNullException"><i>atr</i> is invalid.</exception> public void AddAllowedAtr(Atr atr) { if (atr == null) throw new ArgumentNullException("atr", ResourceController.GetString("Error_ParamNull")); AddAllowedAtrs(new Atr[] { atr }); }
private void Init(SmartcardConnectOption options, Atr[] atr) { m_AllowedAtrs = new LinkedList<Atr>(); if (atr != null) AddAllowedAtrs(atr); m_Options = options; }
internal SmartcardPermission(SmartcardConnectOption state, Atr[] atr) { Init(state, atr); }
/// <summary> /// Initializes a new SmartcardPermission instance. /// </summary> /// <param name="atr">An array of ATR objects that are allowed.</param> public SmartcardPermission(Atr[] atr) { Init(SmartcardConnectOption.AllowedAtrs, atr); }
private static SecurityElement AtrToXml(Atr a) { SecurityElement ret = new SecurityElement("ATR"); ret.AddAttribute("Value", Convert.ToBase64String(a.GetValue())); ret.AddAttribute("Mask", Convert.ToBase64String(a.GetMask())); return ret; }
/// <summary> /// Gets a list of the allowed ATRs. /// </summary> /// <returns>An array containing the allowed ATRs.</returns> public Atr[] GetAllowedAtrs() { Atr[] ret = new Atr[m_AllowedAtrs.Count]; int i = 0; foreach (Atr a in m_AllowedAtrs) { ret[i++] = (Atr)a.Clone(); } return ret; }
/// <summary> /// Initializes a new SmartcardPermissionAttribute instance. /// </summary> /// <param name="action">One of the SecurityAction values.</param> /// <param name="allowed">A list of ATRs to allow.</param> public SmartcardPermissionAttribute(SecurityAction action, Atr[] allowed) : base(action) { m_Options = SmartcardConnectOption.AllowedAtrs; m_AllowedAtrs = allowed; UpdateUnrestricted(); }
/// <summary> /// Checks whether a given ATR matches with this ATR. /// </summary> /// <param name="atr">The ATR to check.</param> /// <returns><b>true</b> if the given ATR matches with this Atr instance, <b>false</b> otherwise.</returns> /// <exception cref="ArgumentNullException"><i>atr</i> is a null reference.</exception> public bool Match(Atr atr) { if (atr == null) throw new ArgumentNullException("atr", ResourceController.GetString("Error_ParamNull")); return InternalMatch(atr.m_Atr); }
private void RefreshStatus() { IntPtr readerName; int nameLength = NativeMethods.SCARD_AUTOALLOCATE; byte[] atrBuffer = new byte[32]; int atrLength = atrBuffer.Length; int ret = NativeMethods.SCardStatus(m_Card, out readerName, ref nameLength ,out m_State , out m_ActiveProtocol , atrBuffer, ref atrLength); try { if (ret != NativeMethods.SCARD_S_SUCCESS || atrLength < 0) throw new SmartcardException(ResourceController.GetString("Error_SmartcardGetStatus"), ret); } finally { NativeMethods.SCardFreeMemory(m_Context, readerName); } m_Atr = new Atr(atrBuffer, atrLength); }
/// <summary> /// Adds an array of ATRs to the allowed list. /// </summary> /// <param name="atr">The array of ATRs to add.</param> /// <exception cref="ArgumentNullException"><i>atr</i> is invalid.</exception> public void AddAllowedAtrs(Atr[] atr) { if (atr == null) throw new ArgumentNullException("atr", ResourceController.GetString("Error_ParamNull")); foreach (Atr a in atr) { if (a != null && a.IsValid() && !m_AllowedAtrs.Contains(a)) { m_AllowedAtrs.AddLast((Atr)a.Clone()); } } if (m_Options == SmartcardConnectOption.None && m_AllowedAtrs.Count > 0) m_Options = SmartcardConnectOption.AllowedAtrs; }
/// <summary> /// Opens a SmartcardReader that has a card inserted with a specified ATR. /// </summary> /// <param name="atr">The ATR to search for.</param> /// <returns>An instance of the SmartcardReader class -or- a null reference if no smartcard reader was found.</returns> /// <exception cref="ArgumentNullException"><i>atr</i> is a null reference.</exception> /// <exception cref="SmartcardException">An error occurred while communication with the smartcard reader.</exception> public static SmartcardReader OpenReader(Atr atr) { if (atr == null) throw new ArgumentNullException("atr", ResourceController.GetString("Error_ParamNull")); string[] readers = SmartcardReader.GetReaders(); SmartcardReader sr = null; foreach(string reader in readers) { sr = new SmartcardReader(reader); sr.Connect(); if (atr.IsMatch(sr.Atr.GetValue())) break; sr.Dispose(); sr = null; } return sr; }
private void UpdateTimer_Tick(object sender, System.EventArgs e) { // check for updates if (NativeMethods.SCardGetStatusChange(m_Context, 0, m_States, m_States.Length) != NativeMethods.SCARD_S_SUCCESS) { return; } // update info bool update; bool ok = false; for(int i = 0; i < m_Readers.Length; i++) { update = (CardReaderList.FocusedItem == m_Readers[i].item); if ((m_States[i].dwEventState & NativeMethods.SCARD_STATE_PRESENT) != 0) { Atr insertedAtr = new Atr(m_States[i].rgbAtr, m_States[i].cbAtr); bool isValid = false; if (m_Atrs == null) { isValid = true; } else { for (int j = 0; j < m_Atrs.Length; j++) { if (m_Atrs[j].Match(insertedAtr)) { isValid = true; break; } } } if (isValid) { m_Readers[i].item.ImageIndex = 2; if (update) { CardTypeText.Text = ResourceController.GetString("SelectReaderForm_ValidCard"); CardStatusText.Text = ResourceController.GetString("SelectReaderForm_ValidCardDesc"); ok = true; } } else { m_Readers[i].item.ImageIndex = 1; if (update) { CardTypeText.Text = ResourceController.GetString("SelectReaderForm_UnknownCard"); CardStatusText.Text = ResourceController.GetString("SelectReaderForm_UnknownCardDesc"); } } } else if ((m_States[i].dwEventState & NativeMethods.SCARD_STATE_EMPTY) != 0) { m_Readers[i].item.ImageIndex = 0; if (update) { CardTypeText.Text = ResourceController.GetString("SelectReaderForm_NoCard"); CardStatusText.Text = ResourceController.GetString("SelectReaderForm_NoCardDesc"); } } else { m_Readers[i].item.ImageIndex = 3; if (update) { CardTypeText.Text = ResourceController.GetString("SelectReaderForm_UnknownStatus"); CardStatusText.Text = ResourceController.GetString("SelectReaderForm_UnknownStatusDesc"); } } } if (OkButton.Enabled != ok) OkButton.Enabled = ok; }