public static void Main(string[] args) { Error ec = Error.Success; using (UsbContext context = new UsbContext()) { try { // Find and open the usb device. MyUsbDevice = context.Find(MyUsbFinder); // If the device is open and ready if (MyUsbDevice == null) { throw new Exception("Device Not Found."); } // If this is a "whole" usb device (libusb-win32, linux libusb) // it will have an IUsbDevice interface. If not (WinUSB) the // variable will be null indicating this is an interface of a // device. IUsbDevice wholeUsbDevice = MyUsbDevice as IUsbDevice; if (!ReferenceEquals(wholeUsbDevice, null)) { // This is a "whole" USB device. Before it can be used, // the desired configuration and interface must be selected. // Select config #1 wholeUsbDevice.SetConfiguration(1); // Claim interface #0. wholeUsbDevice.ClaimInterface(0); } // open read endpoint 1. var reader = MyUsbDevice.OpenEndpointReader(ReadEndpointID.Ep01); // open write endpoint 1. var writer = MyUsbDevice.OpenEndpointWriter(WriteEndpointID.Ep01); // the write test data. string testWriteString = "ABCDEFGH"; Error ecWrite; Error ecRead; int transferredOut; int transferredIn; UsbTransfer usbWriteTransfer; UsbTransfer usbReadTransfer; byte[] bytesToSend = Encoding.Default.GetBytes(testWriteString); byte[] readBuffer = new byte[1024]; int testCount = 0; do { // Create and submit transfer ecRead = reader.SubmitAsyncTransfer(readBuffer, 0, readBuffer.Length, 100, out usbReadTransfer); if (ecRead != Error.Success) { throw new Exception("Submit Async Read Failed."); } ecWrite = writer.SubmitAsyncTransfer(bytesToSend, 0, bytesToSend.Length, 100, out usbWriteTransfer); if (ecWrite != Error.Success) { usbReadTransfer.Dispose(); throw new Exception("Submit Async Write Failed."); } WaitHandle.WaitAll(new WaitHandle[] { usbWriteTransfer.AsyncWaitHandle, usbReadTransfer.AsyncWaitHandle }, 200, false); if (!usbWriteTransfer.IsCompleted) { usbWriteTransfer.Cancel(); } if (!usbReadTransfer.IsCompleted) { usbReadTransfer.Cancel(); } ecWrite = usbWriteTransfer.Wait(out transferredOut); ecRead = usbReadTransfer.Wait(out transferredIn); usbWriteTransfer.Dispose(); usbReadTransfer.Dispose(); Console.WriteLine("Read :{0} Error:{1}", transferredIn, ecRead); Console.WriteLine("Write :{0} Error:{1}", transferredOut, ecWrite); Console.WriteLine("Data :" + Encoding.Default.GetString(readBuffer, 0, transferredIn)); testCount++; } while (testCount < 5); Console.WriteLine("\r\nDone!\r\n"); } catch (Exception ex) { Console.WriteLine(); Console.WriteLine((ec != Error.Success ? ec + ":" : String.Empty) + ex.Message); } finally { if (MyUsbDevice != null) { if (MyUsbDevice.IsOpen) { // If this is a "whole" usb device (libusb-win32, linux libusb-1.0) // it exposes an IUsbDevice interface. If not (WinUSB) the // 'wholeUsbDevice' variable will be null indicating this is // an interface of a device; it does not require or support // configuration and interface selection. IUsbDevice wholeUsbDevice = MyUsbDevice as IUsbDevice; if (!ReferenceEquals(wholeUsbDevice, null)) { // Release interface #0. wholeUsbDevice.ReleaseInterface(0); } MyUsbDevice.Close(); } MyUsbDevice = null; } // Wait for user input.. Console.ReadKey(); } } }
static void Main(string[] args) { try { using var context = new UsbContext(); var devices = context.List(); Console.WriteLine(); var selectedDeviceNum = 0; while (true) { Console.WriteLine(); Console.WriteLine("Select a device (or X to exit):"); ListDevices(devices); var input = Console.ReadLine(); if (input.ToLower().Trim() == "x") { return; } if (int.TryParse(input, out selectedDeviceNum)) { break; } Console.WriteLine($"Invalid number: select a device from 0 to {devices.Count}"); } var selectedDevice = devices[selectedDeviceNum]; selectedDevice.Open(); var writeEndpoint = selectedDevice.OpenEndpointWriter(WriteEndpointID.Ep01); var readEnpoint = selectedDevice.OpenEndpointReader(ReadEndpointID.Ep01); while (true) { Console.WriteLine(); Console.WriteLine("Enter the data to send (or a blank line to exit):"); var input = Console.ReadLine(); if (string.IsNullOrEmpty(input)) { break; } var buffer = new byte[1]; var readBuffer = new byte[64]; buffer[0] = 0x2B; writeEndpoint.Write(buffer, timeout, out var bytesWritten); readEnpoint.Read(readBuffer, timeout, out var readBytes); if (readBytes == 0) { Console.WriteLine("null"); } Console.WriteLine(string.Join(" ", readBuffer.Take(readBytes).Select(b => b.ToString()))); } } catch (Exception error) { Console.WriteLine($"An {error.GetType()} occurred:"); while (error != null) { Console.WriteLine(error.Message); error = error.InnerException; } } }
static void Run(string[] args) { //var z = new NSRecord("DFFFFFFFFFFFFFFFFF7F8188818180bb5c424c1b3121cf630cbcbaf60fa91e53786d1ab9e8b6e5855acb9afbec944555481d88fcd8e32947f7696d80a8f4df55be51dcb967fc5ef3d213a971a11fee54917cbe10d4b6ba69a71ee1434ce6b6cadb46ceff0bbf2ba832cb5516af35a1debf182e0a57544a64bfe2d0f711cf94dffb44dda9d1d4a9abdf1460e783b6f18203010001"); /* * var x = new PCSC(); * var prc = x.establish_context(PCSC.SCARD_SCOPE_SYSTEM, IntPtr.Zero, IntPtr.Zero, out var pctx); * var buf = new byte[512]; * int cb = 512; * prc = x.list_readers(pctx, null, buf, ref cb); * var s = Encoding.UTF8.GetString(buf[0..cb]); * Console.WriteLine(s); * prc = x.release_context(pctx); */ var scp03_context = new Scp03Context("password"); using (var usb_ctx = new UsbContext()) { foreach (var device in usb_ctx.GetDeviceList()) { Console.WriteLine($"Id {device.Id} Vendor 0x{device.Vendor:x} Product 0x{device.Product:x}"); if (device.IsYubiHsm) { //var config = usb_ctx.GetConfigDescriptor(device, 0); //var config_id = config.bConfigurationValue; using (var usb_device = usb_ctx.Open(device, 1)) { Console.WriteLine($"Manufacturer '{usb_device.Manufacturer}' Product '{usb_device.Product}' Serial '{usb_device.SerialNumber}'"); using (var usb_session = usb_device.Claim(0)) { //usb_session.SendCmd(HsmCommand.Bsl); //usb_session.SendCmd(new SetSerialReq { serial = 12345 }); //usb_session.SendCmd(new SetDemoModeReq { demo = 0xffff }); var resp = usb_session.SendCmd(HsmCommand.Echo, new byte[] { 1, 2, 3, 4, 5 }); using (var scp03_session = scp03_context.CreateSession(usb_session, 1)) { //scp03_session.SendCmd(HsmCommand.Reset); /* * var opts = scp03_session.SendCmd(new GetAlgorithmToggleReq { }); * for(int i = 1; i < opts.Length; i += 2) * { * opts[i] = 1; * } * var res = scp03_session.SendCmd(new PutAlgorithmToggleReq { data = opts.ToArray() }); */ scp03_context.PutAesKey(scp03_session, 4, new byte[16]); var encrypted = scp03_session.SendCmd(new EncryptEcbReq { key_id = 4, data = new byte[16 * 125] }); var decrypted = scp03_session.EcbCrypt(false, new byte[16], encrypted.ToArray()); var decrypted2 = scp03_session.SendCmd(new DecryptEcbReq { key_id = 4, data = encrypted.ToArray() }); encrypted = scp03_session.SendCmd(new EncryptCbcReq { key_id = 4, iv = new byte[16], data = new byte[16 * 125] }); decrypted = scp03_session.CbcCrypt(false, new byte[16], new byte[16], encrypted.ToArray()); decrypted2 = scp03_session.SendCmd(new DecryptCbcReq { key_id = 4, iv = new byte[16], data = encrypted.ToArray() }); var id = scp03_context.PutEcdhKey(scp03_session, 4); scp03_context.PutWrapKey(scp03_session, 2, new byte[32]); scp03_context.ExportWrapped(scp03_session, 2, ObjectType.AsymmetricKey, 4); scp03_context.ExportWrapped(scp03_session, 2, ObjectType.SymmetricKey, 4); scp03_context.ExportWrapped(scp03_session, 2, ObjectType.WrapKey, 2); var info = scp03_session.SendCmd(HsmCommand.GetDeviceInfo); Console.WriteLine("DeviceInfo over scp03_session"); foreach (var b in info) { Console.Write($"{b:x2}"); } Console.WriteLine(); var rand1 = scp03_session.SendCmd(new GetPseudoRandomReq { length = 64 }); Console.WriteLine("GetPseudoRandom over scp03_session"); foreach (var b in rand1) { Console.Write($"{b:x2}"); } Console.WriteLine(); var context = new Scp11Context(usb_session); context.PutAuthKey(scp03_session, 3); // Device pubkey in 3 var sk_oce = context.GenerateKeyPair(); //usb_session.SendCmd(new SetAttestKeyReq { algorithm = Algorithm.EC_P256, key = sk_oce.D.ToByteArrayFixed() }); //usb_session.SendCmd(new SetAttestCertReq { cert = context.GenerateCertificate(sk_oce).GetEncoded() }); //context.SetDefaultKey(usb_session); context.PutAuthKey(scp03_session, 2); using (var scp11_session = context.CreateSession(usb_session, 2)) { context.GenerateKeyPair("password"); context.ChangeAuthKey(scp11_session, 2); context.DeleteObject(scp11_session, 2, ObjectType.AuthenticationKey); var info2 = scp11_session.SendCmd(HsmCommand.GetDeviceInfo); Console.WriteLine("DeviceInfo over first scp11_session"); foreach (var b in info2) { Console.Write($"{b:x2}"); } Console.WriteLine(); var rand2 = scp11_session.SendCmd(new GetPseudoRandomReq { length = 64 }); Console.WriteLine("GetPseudoRandom over first scp11_session"); foreach (var b in rand2) { Console.Write($"{b:x2}"); } Console.WriteLine(); context.PutAuthKey(scp03_session, 2); //context.PutAuthKey(scp11_session, 2); //context.ChangeAuthKey(scp11_session, 2); } using (var scp11_session = context.CreateSession(usb_session, 2)) { var info2 = scp11_session.SendCmd(HsmCommand.GetDeviceInfo); Console.WriteLine("DeviceInfo over second scp11_session"); foreach (var b in info2) { Console.Write($"{b:x2}"); } Console.WriteLine(); var rand2 = scp11_session.SendCmd(new GetPseudoRandomReq { length = 64 }); Console.WriteLine("GetPseudoRandom over second scp11_session"); foreach (var b in rand2) { Console.Write($"{b:x2}"); } Console.WriteLine(); var attestation = scp11_session.SendCmd(new AttestAsymmetricReq { key_id = 0, attest_id = 0 }); foreach (var b in attestation) { Console.Write($"{b:x2}"); } Console.WriteLine(); File.WriteAllBytes("attestation.cer", attestation.ToArray()); } /* * using (var sess = new Scp03Session(usb_session, 1, scp03_session, 1)) * { * sess.SendCmd(new GetPseudoRandomReq { length = 64 }); * } * context.SetClientPubKey(scp03_session); * context.PutAuthKey(scp03_session, 4); // Client pubkey in 4 * using(var sess = new Scp11Session(usb_session, 4, scp03_session, 3)) * { * sess.SendCmd(new GetPseudoRandomReq { length = 64 }); * } */ } } } } } var devices = usb_ctx.OpenDevices(d => d.IsYubiHsm, 1).ToList(); var sessions = devices.Select(d => d.Claim(0)).ToList(); var scp03_sessions = sessions.Select(s => scp03_context.CreateSession(s, 1)).ToList(); if (sessions.Count == 2) { using (var sess = new Scp03Session(sessions[0], 1, scp03_sessions[1], 1)) { sess.SendCmd(new GetPseudoRandomReq { length = 64 }); } using (var sess = new Scp03Session(sessions[1], 1, scp03_sessions[0], 1)) { sess.SendCmd(new GetPseudoRandomReq { length = 64 }); } var ctx0 = new Scp11Context(sessions[0]); ctx0.PutAuthKey(scp03_sessions[1], 5); // Device 0 pubkey in device 1 key 5 ctx0.SetClientPubKey(scp03_sessions[0]); ctx0.PutAuthKey(scp03_sessions[1], 6); // Client 0 pubkey in device 1 key 6 var ctx1 = new Scp11Context(sessions[1]); ctx1.PutAuthKey(scp03_sessions[0], 5); // Device 1 pubkey in device 0 key 5 ctx1.SetClientPubKey(scp03_sessions[1]); ctx1.PutAuthKey(scp03_sessions[0], 6); // CLient 1 pubkey in device 0 key 6 using (var sess = new Scp11Session(sessions[0], 6, scp03_sessions[1], 5)) { sess.SendCmd(new GetPseudoRandomReq { length = 64 }); } using (var sess = new Scp11Session(sessions[1], 6, scp03_sessions[0], 5)) { sess.SendCmd(new GetPseudoRandomReq { length = 64 }); } } scp03_sessions.ForEach(s => s.Dispose()); sessions.ForEach(s => s.Dispose()); devices.ForEach(s => s.Dispose()); } }
public static void Main(string[] args) { Error ec = Error.Success; using (UsbContext context = new UsbContext()) { try { // Find and open the usb device. MyUsbDevice = context.Find(MyUsbFinder); // If the device is open and ready if (MyUsbDevice == null) { throw new Exception("Device Not Found."); } // If this is a "whole" usb device (libusb-win32, linux libusb-1.0) // it exposes an IUsbDevice interface. If not (WinUSB) the // 'wholeUsbDevice' variable will be null indicating this is // an interface of a device; it does not require or support // configuration and interface selection. IUsbDevice wholeUsbDevice = MyUsbDevice as IUsbDevice; if (!ReferenceEquals(wholeUsbDevice, null)) { // This is a "whole" USB device. Before it can be used, // the desired configuration and interface must be selected. // Select config #1 wholeUsbDevice.SetConfiguration(1); // Claim interface #0. wholeUsbDevice.ClaimInterface(0); } // open read endpoint 1. var reader = MyUsbDevice.OpenEndpointReader(ReadEndpointID.Ep01); byte[] readBuffer = new byte[1024]; while (ec == Error.Success) { int bytesRead; // If the device hasn't sent data in the last 5 seconds, // a timeout error (ec = IoTimedOut) will occur. ec = reader.Read(readBuffer, 5000, out bytesRead); if (bytesRead == 0) { throw new Exception(string.Format("{0}:No more bytes!", ec)); } Console.WriteLine("{0} bytes read", bytesRead); // Write that output to the console. Console.Write(Encoding.Default.GetString(readBuffer, 0, bytesRead)); } Console.WriteLine("\r\nDone!\r\n"); } catch (Exception ex) { Console.WriteLine(); Console.WriteLine((ec != Error.Success ? ec + ":" : String.Empty) + ex.Message); } finally { if (MyUsbDevice != null) { if (MyUsbDevice.IsOpen) { // If this is a "whole" usb device (libusb-win32, linux libusb-1.0) // it exposes an IUsbDevice interface. If not (WinUSB) the // 'wholeUsbDevice' variable will be null indicating this is // an interface of a device; it does not require or support // configuration and interface selection. IUsbDevice wholeUsbDevice = MyUsbDevice as IUsbDevice; if (!ReferenceEquals(wholeUsbDevice, null)) { // Release interface #0. wholeUsbDevice.ReleaseInterface(0); } MyUsbDevice.Close(); } MyUsbDevice = null; } // Wait for user input.. Console.ReadKey(); } } }
public static void Main(string[] args) { var ec = Error.Success; using (var context = new UsbContext()) { try { // Find and open the usb device. MyUsbDevice = context.Find(MyUsbFinder); // If the device is open and ready if (MyUsbDevice == null) { throw new Exception("Device Not Found."); } // If this is a "whole" usb device (libusb-win32, linux libusb) // it will have an IUsbDevice interface. If not (WinUSB) the // variable will be null indicating this is an interface of a // device. var wholeUsbDevice = MyUsbDevice as IUsbDevice; if (!ReferenceEquals(wholeUsbDevice, null)) { // This is a "whole" USB device. Before it can be used, // the desired configuration and interface must be selected. // Select config #1 wholeUsbDevice.SetConfiguration(1); // Claim interface #0. wholeUsbDevice.ClaimInterface(0); } // open read endpoint 1. var reader = MyUsbDevice.OpenEndpointReader(ReadEndpointID.Ep01); // open write endpoint 1. var writer = MyUsbDevice.OpenEndpointWriter(WriteEndpointID.Ep01); // Remove the exepath/startup filename text from the begining of the CommandLine. var cmdLine = Regex.Replace( Environment.CommandLine, "^\".+?\"^.*? |^.*? ", "", RegexOptions.Singleline); if (!string.IsNullOrEmpty(cmdLine)) { ec = writer.Write(Encoding.Default.GetBytes(cmdLine), 2000, out var bytesWritten); if (ec != Error.Success) { throw new Exception($"The command line {cmdLine} failed with an error of {ec}."); } var readBuffer = new byte[1024]; while (ec == Error.Success) { // If the device hasn't sent data in the last 100 milliseconds, // a timeout error (ec = IoTimedOut) will occur. ec = reader.Read(readBuffer, 100, out var bytesRead); if (bytesRead == 0) { throw new Exception("No more bytes!"); } // Write that output to the console. Console.Write(Encoding.Default.GetString(readBuffer, 0, bytesRead)); } Console.WriteLine("\r\nDone!\r\n"); } else { throw new Exception("Nothing to do."); } } catch (Exception ex) { Console.WriteLine(); Console.WriteLine((ec != Error.Success ? ec + ":" : string.Empty) + ex.Message); } finally { if (MyUsbDevice != null) { if (MyUsbDevice.IsOpen) { // If this is a "whole" usb device (libusb-win32, linux libusb-1.0) // it exposes an IUsbDevice interface. If not (WinUSB) the // 'wholeUsbDevice' variable will be null indicating this is // an interface of a device; it does not require or support // configuration and interface selection. var wholeUsbDevice = MyUsbDevice as IUsbDevice; if (!ReferenceEquals(wholeUsbDevice, null)) { // Release interface #0. wholeUsbDevice.ReleaseInterface(0); } MyUsbDevice.Close(); } MyUsbDevice = null; } // Wait for user input.. Console.ReadKey(); } } }
public UsbHolder(ILogger <UsbHolder> logger, UsbContext context) { this.logger = logger; this.context = context; }