Example #1
0
        public void Establish(SCardScope scope)
        {
            if (hasContext)
                if (IsValid())
                    Release();

            SCardError rc;
            IntPtr hContext = IntPtr.Zero;

            rc = SCardAPI.Lib.EstablishContext(scope,
                IntPtr.Zero,
                IntPtr.Zero,
                ref hContext);

            if (rc == SCardError.Success)
            {
                contextPtr = hContext;
                lastScope = scope;
                hasContext = true;
            }
            else
            {
                if (rc == SCardError.InvalidValue)
                    throw new InvalidScopeTypeException(rc, "Invalid scope type passed");
                else
                    throw new PCSCException(rc, SCardHelper.StringifyError(rc));
            }

        }
Example #2
0
        /// <summary>
        /// Create and establish a new smart card context.
        /// </summary>
        /// <param name="scope">Scope of the establishment. This can either be a local or remote connection.</param>
        /// <returns>A new established smart card context</returns>
        public ISCardContext Establish(SCardScope scope)
        {
            var context = new SCardContext();

            context.Establish(scope);
            return(context);
        }
Example #3
0
        /// <summary>Creates an Application Context to the PC/SC Resource Manager.</summary>
        /// <param name="scope">Scope of the establishment. This can either be a local or remote connection.</param>
        /// <remarks>
        ///     <para>Creates an Application Context for a client. This must be the first WinSCard function called in a PC/SC application. Each thread of an application shall use its own <see cref="T:PCSC.SCardContext" />.</para>
        ///     <para>This method calls the API function SCardEstablishContext().</para>
        ///     <example>
        ///         <code lang="C#">
        /// var context = new SCardContext();
        /// context.Establish(SCardScope.System);
        ///   </code>
        ///     </example>
        /// </remarks>
        public void Establish(SCardScope scope)
        {
            if (_hasContext && IsValid())
            {
                Release();
            }

            IntPtr hContext;

            var rc = Platform.Lib.EstablishContext(
                scope,
                IntPtr.Zero,
                IntPtr.Zero,
                out hContext);

            switch (rc)
            {
            case SCardError.Success:
                _contextPtr = hContext;
                _lastScope  = scope;
                _hasContext = true;
                break;

            case SCardError.InvalidValue:
                throw new InvalidScopeTypeException(rc, "Invalid scope type passed");

            default:
                throw new PCSCException(rc, SCardHelper.StringifyError(rc));
            }
        }
        public void Establish(SCardScope scope)
        {
            if (hasContext)
                if (IsValid())
                    Release();

            SCardError rc;
            IntPtr hContext = IntPtr.Zero;

            rc = SCardAPI.Lib.EstablishContext(scope,
                IntPtr.Zero,
                IntPtr.Zero,
                ref hContext);

            if (rc == SCardError.Success)
            {
                contextPtr = hContext;
                lastScope = scope;
                hasContext = true;
            }
            else
            {
                if (rc == SCardError.InvalidValue)
                    throw new InvalidScopeTypeException(rc, "Invalid scope type passed");
                else
                    throw new PCSCException(rc, SCardHelper.StringifyError(rc));
            }

        }
Example #5
0
        /// <inheritdoc />
        public void Establish(SCardScope scope)
        {
            if (_contextPtr != IntPtr.Zero && IsValid())
            {
                Release();
            }

            var rc = _api.EstablishContext(
                scope,
                IntPtr.Zero,
                IntPtr.Zero,
                out var hContext);

            switch (rc)
            {
            case SCardError.Success:
                _contextPtr = hContext;
                _lastScope  = scope;
                break;

            case SCardError.InvalidValue:
                throw new InvalidScopeTypeException(rc, "Invalid scope type passed");

            default:
                rc.Throw();
                break;
            }
        }
Example #6
0
        public void Connect(SCardScope scope)
        {
            // If we are already connected, ignore
            if (myContext != IntPtr.Zero)
            {
                return;
            }

            // Establish a resource manager context
            PCSCResult result = NativeMethods.SCardEstablishContext(scope, IntPtr.Zero, IntPtr.Zero, out myContext);

            if (result != PCSCResult.None)
            {
                throw new PCSCException("PCSCManager.Connect(): Unable to establish resource manager context");
            }

            // Create our reader list
            myReaders = new Dictionary <string, PCSCReader>();

            string[] readers = ListReaders();

            // Check for our initial list of readers readers
            foreach (string r in readers)
            {
                lock (myReaders)
                {
                    myReaders.Add(r, new PCSCReader(r));
                }
                SpawnOnReaderInsertedEvent(r);
            }
        }
Example #7
0
		/// <summary>Creates an Application Context to the PC/SC Resource Manager.</summary>
		/// <param name="scope">Scope of the establishment. This can either be a local or remote connection.</param>
		/// <remarks>
		///     <para>Creates an Application Context for a client. This must be the first WinSCard function called in a PC/SC application. Each thread of an application shall use its own <see cref="T:PCSC.SCardContext" />.</para>
		///     <para>This method calls the API function SCardEstablishContext().</para>
		///     <example>
		///         <code lang="C#">
		/// var context = new SCardContext();
		/// context.Establish(SCardScope.System);
		///   </code>
		///     </example>
		/// </remarks>
		/// <exception cref="InvalidScopeTypeException">If an invalid scope type has been passed</exception>
		/// <exception cref="NoServiceException">If the smart card service is not running</exception>
		public void Establish(SCardScope scope) {
            if (_hasContext && IsValid()) {
                Release();
            }

            IntPtr hContext;

            var rc = Platform.Lib.EstablishContext(
                scope,
                IntPtr.Zero,
                IntPtr.Zero,
                out hContext);

            switch (rc) {
                case SCardError.Success:
                    _contextPtr = hContext;
                    _lastScope = scope;
                    _hasContext = true;
                    break;
                case SCardError.InvalidValue:
                    throw new InvalidScopeTypeException(rc, "Invalid scope type passed");
				default:
		            rc.Throw();
		            break;
            }
        }
 public ISCardMonitor Start(SCardScope scope, string readerName)
 {
     if (readerName == null)
     {
         throw new ArgumentNullException(nameof(readerName));
     }
     return(Start(scope, new[] { readerName }));
 }
Example #9
0
 public SCardError EstablishContext(SCardScope dwScope, IntPtr pvReserved1, IntPtr pvReserved2, out IntPtr phContext) {
     var ctx = IntPtr.Zero;
     var rc = SCardHelper.ToSCardError(SCardEstablishContext(
         (IntPtr) dwScope,
         pvReserved1,
         pvReserved2,
         ref ctx));
     phContext = ctx;
     return rc;
 }
Example #10
0
        /// <summary>Creates a new SCardMonitor object that is able to listen for certain smart card / reader changes.</summary>
        /// <param name="contextFactory">A smartcard context factory</param>
        /// <param name="scope">Scope of the establishment. This can either be a local or remote connection.</param>
        public SCardMonitor(IContextFactory contextFactory, SCardScope scope)
        {
            if (contextFactory == null)
            {
                throw new ArgumentNullException(nameof(contextFactory));
            }

            _context = contextFactory.Establish(scope);
            _releaseContextOnDispose = true;
        }
Example #11
0
 public SCardMonitor(ISCardContext context, SCardScope scope, bool releaseContextOnDispose = true)
 {
     if (context == null)
     {
         throw new ArgumentNullException(nameof(context));
     }
     _context = context;
     _context.Establish(scope);
     _releaseContextOnDispose = releaseContextOnDispose;
 }
        internal PcscSmartCardReader(string readerName, SCardScope scardScope = SCardScope.System)
        {
            if (string.IsNullOrEmpty("readerName"))
                throw new ArgumentNullException("readerName");

            name = readerName;
            scope = scardScope;

            CreateContextAndReader();
        }
 /// <summary>
 /// Get the reader context
 /// </summary>
 /// <param name="scope"></param>
 /// <returns>Scope of the reader </returns>
 public SCardContext getReaderContext(SCardScope scope)
 {
     HContext = new SCardContext();
     if (scope != null)
     {
         HContext.Establish(scope);
         return(HContext);
     }
     return(null);
 }
Example #14
0
        public SCardError EstablishContext(SCardScope dwScope, IntPtr pvReserved1, IntPtr pvReserved2, out IntPtr phContext)
        {
            var ctx = IntPtr.Zero;
            var rc  = SCardHelper.ToSCardError(SCardEstablishContext(
                                                   (IntPtr)dwScope,
                                                   pvReserved1,
                                                   pvReserved2,
                                                   ref ctx));

            phContext = ctx;
            return(rc);
        }
Example #15
0
        internal PcscSmartCardReader(string readerName, SCardScope scardScope = SCardScope.System)
        {
            if (string.IsNullOrEmpty("readerName"))
            {
                throw new ArgumentNullException("readerName");
            }

            name  = readerName;
            scope = scardScope;

            CreateContextAndReader();
        }
 public SmartcardFactory(SCardScope scope)
 {
     try
     {
         _scope = scope;
         //Estabilish context
         cardFactory = contextFactory.Establish(_scope);
     }
     catch
     {
         throw;
     }
 }
        public SCardError EstablishContext(SCardScope dwScope, IntPtr pvReserved1, IntPtr pvReserved2,
                                           out IntPtr phContext)
        {
            var ctx = 0;
            var rc  = SCardHelper.ToSCardError(MacOsxNativeMethods.SCardEstablishContext(
                                                   (int)dwScope,
                                                   pvReserved1,
                                                   pvReserved2,
                                                   ref ctx));

            phContext = (IntPtr)ctx;
            return(rc);
        }
Example #18
0
        public SCardError EstablishContext(
            SCardScope dwScope,
            IntPtr pvReserved1,
            IntPtr pvReserved2,
            ref IntPtr phContext)
        {
            IntPtr     ctx = IntPtr.Zero;
            SCardError rc  = SCardHelper.ToSCardError(SCardEstablishContext(
                                                          (IntPtr)dwScope,
                                                          (IntPtr)pvReserved1,
                                                          (IntPtr)pvReserved2,
                                                          ref ctx));

            phContext = (IntPtr)ctx;
            return(rc);
        }
Example #19
0
        public unsafe PcscContext Establish(SCardScope scope, PcscExceptionHandler onException = null)
        {
            if (IsDisposed)
            {
                throw new ObjectDisposedException(nameof(PcscContext), nameof(Establish));
            }
            if (IsEstablished)
            {
                throw new InvalidOperationException("Context has been established.");
            }
            SCardContext handle;

            Provider.SCardEstablishContext(scope, null, null, &handle).ThrowIfNotSuccess(onException);
            Handle = handle;
            return(this);
        }
        /// <summary>
        /// Creates an observable for smartcard reader device events.
        /// </summary>
        /// <param name="factory">Factory to use for <see cref="IDeviceMonitor"/> creation.</param>
        /// <param name="scope">Scope of the establishment. This can either be a local or remote connection.</param>
        /// <param name="scheduler">The scheduler to run the add and remove event handler logic on.</param>
        /// <returns></returns>
        public static IObservable <DeviceMonitorEvent> CreateObservable(this IDeviceMonitorFactory factory,
                                                                        SCardScope scope, IScheduler scheduler = null)
        {
            if (factory == null)
            {
                throw new ArgumentNullException(nameof(factory));
            }

            return(Observable.Create <DeviceMonitorEvent>(obs => {
                var monitor = factory.Create(scope);
                var useScheduler = scheduler ?? Scheduler.ForCurrentContext();

                var subscription = monitor
                                   .ObserveEvents(useScheduler)
                                   .Subscribe(obs);

                monitor.Start();

                return new CompositeDisposable(subscription, monitor);
            }));
        }
        public ISCardMonitor Start(SCardScope scope, IEnumerable <string> readerNames, Action <ISCardMonitor> preStartAction)
        {
            if (readerNames == null)
            {
                throw new ArgumentNullException(nameof(readerNames));
            }

            var readers = readerNames
                          .Where(name => !string.IsNullOrWhiteSpace(name))
                          .ToArray();

            var monitor = Create(scope);

            try {
                preStartAction?.Invoke(monitor);
                monitor.Start(readers);
                return(monitor);
            } catch {
                monitor.Dispose();
                throw;
            }
        }
Example #22
0
 /// <summary>Creates a new SCardMonitor object that is able to listen for certain smart card / reader changes.</summary>
 /// <param name="context">A new Application Context to the PC/SC Resource Manager.</param>
 /// <param name="scope">Scope of the establishment. This can either be a local or remote connection.</param>
 /// <param name="releaseContextOnDispose">If <see langword="true" /> the supplied <paramref name="context" /> will be released (using <see cref="ISCardContext.Release()" />) on <see cref="Dispose()" /></param>
 /// <remarks>The monitor object should use its own application context to the PC/SC Resource Manager. It will create a (new) backgroud thread that will listen for status changes.
 ///     <para>Warning: You MUST dispose the monitor instance otherwise the background thread will run forever!</para>
 /// </remarks>
 public SCardMonitor(ISCardContext context, SCardScope scope, bool releaseContextOnDispose = true)
     : this(context, releaseContextOnDispose)
 {
     _context.Establish(scope);
 }
Example #23
0
 public SCardError EstablishContext(
     SCardScope dwScope,
     IntPtr pvReserved1,
     IntPtr pvReserved2,
     ref IntPtr phContext)
 {
     IntPtr ctx = IntPtr.Zero;
     SCardError rc = SCardHelper.ToSCardError(SCardEstablishContext(
         (IntPtr)dwScope,
         (IntPtr)pvReserved1,
         (IntPtr)pvReserved2,
         ref ctx));
     phContext = (IntPtr)ctx;
     return rc;
 }
Example #24
0
 public SCardMonitor(SCardContext hContext, SCardScope scope)
     : this(hContext)
 {
     hContext.Establish(scope);
 }
Example #25
0
 public static PcscContext EstablishContext(SCardScope scope, PcscExceptionHandler onException = null)
 {
     return(Instance.EstablishContext(scope, onException));
 }
        /// <summary>
        /// Creates an observable for smart card events.
        /// </summary>
        /// <param name="factory">Factory to use for <see cref="ISCardMonitor"/> creation.</param>
        /// <param name="scope">Scope of the establishment. This can either be a local or remote connection.</param>
        /// <param name="readerNames">Name of the smart card reader that shall be monitored.</param>
        /// <param name="scheduler">The scheduler to run the add and remove event handler logic on.</param>
        /// <returns></returns>
        public static IObservable <MonitorEvent> CreateObservable(this IMonitorFactory factory, SCardScope scope, IEnumerable <string> readerNames, IScheduler scheduler = null)
        {
            if (factory == null)
            {
                throw new ArgumentNullException(nameof(factory));
            }
            if (readerNames == null)
            {
                throw new ArgumentNullException(nameof(readerNames));
            }

            var useScheduler = scheduler ?? Scheduler.ForCurrentContext();

            IObservable <MonitorEvent> events = null;
            var monitor = factory.Start(
                scope,
                readerNames,
                preStartMonitor => events = preStartMonitor.ObserveEvents(useScheduler)
                );

            return(Observable.Using(() => monitor, _ => events));
        }
 /// <summary>
 /// Creates an observable for smart card events.
 /// </summary>
 /// <param name="factory">Factory to use for <see cref="ISCardMonitor"/> creation.</param>
 /// <param name="scope">Scope of the establishment. This can either be a local or remote connection.</param>
 /// <param name="readerName">Name of the smart card reader that shall be monitored.</param>
 /// <param name="scheduler">The scheduler to run the add and remove event handler logic on.</param>
 /// <returns></returns>
 public static IObservable <MonitorEvent> CreateObservable(this IMonitorFactory factory, SCardScope scope, string readerName, IScheduler scheduler = null)
 {
     if (factory == null)
     {
         throw new ArgumentNullException(nameof(factory));
     }
     if (readerName == null)
     {
         throw new ArgumentNullException(nameof(readerName));
     }
     return(factory.CreateObservable(scope, new[] { readerName }, scheduler));
 }
Example #28
0
 public SCardMonitor(SCardContext hContext, SCardScope scope)
     : this(hContext)
 {
     hContext.Establish(scope);
 }
Example #29
0
 unsafe SCardError IPcscProvider.SCardEstablishContext(SCardScope dwScope, void *pvReserved1, void *pvReserved2, SCardContext *phContext)
 {
     return(SCardEstablishContext(dwScope, pvReserved1, pvReserved2, phContext));
 }
Example #30
0
 public unsafe static extern SCardError SCardEstablishContext(SCardScope dwScope, void *pvReserved1, void *pvReserved2, SCardContext *phContext);
Example #31
0
 public static extern PCSCResult SCardEstablishContext(SCardScope dwScope, IntPtr pvReserved1, IntPtr pvReserved2, out IntPtr phContext);
Example #32
0
        /// <summary>
        /// Creates an observable for smart card events.
        /// </summary>
        /// <param name="factory">Factory to use for <see cref="ISCardMonitor"/> creation.</param>
        /// <param name="scope">Scope of the establishment. This can either be a local or remote connection.</param>
        /// <param name="readerNames">Name of the smart card reader that shall be monitored.</param>
        /// <param name="scheduler">The scheduler to run the add and remove event handler logic on.</param>
        /// <returns></returns>
        public static IObservable <MonitorEvent> CreateObservable(this IMonitorFactory factory, SCardScope scope,
                                                                  IEnumerable <string> readerNames, IScheduler scheduler = null)
        {
            if (factory == null)
            {
                throw new ArgumentNullException(nameof(factory));
            }

            if (readerNames == null)
            {
                throw new ArgumentNullException(nameof(readerNames));
            }

            return(Observable.Create <MonitorEvent>(obs => {
                var monitor = factory.Create(scope);
                var useScheduler = scheduler ?? Scheduler.ForCurrentContext();

                var readers = readerNames
                              .Where(name => !string.IsNullOrWhiteSpace(name))
                              .ToArray();

                var subscription = monitor
                                   .ObserveEvents(useScheduler)
                                   .Subscribe(obs);

                monitor.Start(readers);

                return new CompositeDisposable(subscription, monitor);
            }));
        }
 /// <summary>
 /// Creates a device monitor
 /// </summary>
 /// <param name="scope">Scope of the establishment. This can either be a local or remote connection.</param>
 /// <returns>A <see cref="IDeviceMonitor"/></returns>
 public IDeviceMonitor Create(SCardScope scope)
 {
     return(new DeviceMonitor(_contextFactory, scope));
 }
 public ISCardMonitor Start(SCardScope scope, IEnumerable <string> readerNames)
 {
     return(Start(scope, readerNames, null));
 }
Example #35
0
 /// <summary>
 /// Creates a new instance
 /// </summary>
 /// <param name="contextFactory">Context factory used for this monitor</param>
 /// <param name="scope">Scope of the establishment. This can either be a local or remote connection.</param>
 public DeviceMonitor(IContextFactory contextFactory, SCardScope scope)
 {
     _instanceName   = GetInstanceName();
     _contextFactory = contextFactory;
     _scope          = scope;
 }
 /// <summary>
 ///  Creates a smart card event monitor
 /// </summary>
 /// <param name="scope">Scope of the establishment. This can either be a local or remote connection.</param>
 /// <returns>A <see cref="ISCardMonitor"/></returns>
 public ISCardMonitor Create(SCardScope scope)
 {
     return(new SCardMonitor(_contextFactory, scope));
 }
Example #37
0
 /// <summary>Creates a new SCardMonitor object that is able to listen for certain smart card / reader changes.</summary>
 /// <param name="context">A new Application Context to the PC/SC Resource Manager.</param>
 /// <param name="scope">Scope of the establishment. This can either be a local or remote connection.</param>
 /// <param name="releaseContextOnDispose">If <see langword="true" /> the supplied <paramref name="context" /> will be released (using <see cref="ISCardContext.Release()" />) on <see cref="Dispose()" /></param>
 /// <remarks>The monitor object should use its own application context to the PC/SC Resource Manager. It will create a (new) backgroud thread that will listen for status changes.
 ///     <para>Warning: You MUST dispose the monitor instance otherwise the background thread will run forever!</para>
 /// </remarks>
 public SCardMonitor(ISCardContext context, SCardScope scope, bool releaseContextOnDispose = true)
     : this(context, releaseContextOnDispose)
 {
     _context.Establish(scope);
 }