Esempio n. 1
0
        internal YubikeyPivDevice(string name)
        {
            _deviceHandle = new YubikeyPivDeviceHandle();

            YubicoPivReturnCode code = YubikeyPivNative.YkPivConnect(_deviceHandle.State, name);

            if (code != YubicoPivReturnCode.YKPIV_OK)
            {
                throw new Exception("Unable to connect to PIV: " + code);
            }
        }
Esempio n. 2
0
        public IEnumerable <string> ListDevices(bool filter = true)
        {
            byte[] data;
            using (YubikeyPivDeviceHandle deviceHandle = new YubikeyPivDeviceHandle())
            {
                IntPtr ptr = IntPtr.Zero;
                try
                {
                    int len = 2048; // A typical reader name is 32 chars long. This gives space for 64 readers.
                    ptr = Marshal.AllocHGlobal(len);

                    IntPtr dev = deviceHandle.State;
                    YubicoPivReturnCode res = YubikeyPivNative.YkPivListReaders(dev, ptr, ref len);
                    if (res != YubicoPivReturnCode.YKPIV_OK)
                    {
                        return(Enumerable.Empty <string>());
                    }

                    data = new byte[len];
                    Marshal.Copy(ptr, data, 0, len);
                }
                finally
                {
                    if (ptr != IntPtr.Zero)
                    {
                        Marshal.FreeHGlobal(ptr);
                    }
                }
            }

            if (filter)
            {
                return(StringUtils.ParseStrings(data).Where(IsValidDevice));
            }

            return(StringUtils.ParseStrings(data));
        }