diff options
author | root <root@new-selene.erebei.org> | 2015-12-01 10:51:56 +0000 |
---|---|---|
committer | root <root@new-selene.erebei.org> | 2015-12-01 10:51:56 +0000 |
commit | bc832d6d342922a828aebb997d1d9c6626898487 (patch) | |
tree | 655249961aa9abdc64e9a0227ddb0d114d4b7fd5 /host/cryptopad.c | |
parent | 15f6f34fe239b0b71cb2ef4fd16f278a23b52506 (diff) | |
download | candlestick-bc832d6d342922a828aebb997d1d9c6626898487.tar.gz candlestick-bc832d6d342922a828aebb997d1d9c6626898487.tar.bz2 candlestick-bc832d6d342922a828aebb997d1d9c6626898487.zip |
fish
Diffstat (limited to 'host/cryptopad.c')
-rw-r--r-- | host/cryptopad.c | 122 |
1 files changed, 122 insertions, 0 deletions
diff --git a/host/cryptopad.c b/host/cryptopad.c new file mode 100644 index 0000000..42b1d36 --- /dev/null +++ b/host/cryptopad.c @@ -0,0 +1,122 @@ +#include "project.h" +#include "../common/vendor_req.h" + +#define TIMEOUT 4000 + +struct libusb_device * find_device(libusb_context *ctx) +{ + libusb_device **list; + ssize_t num_devs; + ssize_t i; + + num_devs = libusb_get_device_list(ctx, &list); + for (i = 0; i < num_devs; ++i) { + struct libusb_device_descriptor desc; + struct libusb_device *dev = list[i]; + + if (libusb_get_device_descriptor(dev, &desc)) + continue; + + + if (desc.idVendor!=0x1d6b) continue; + if (desc.idProduct!=0x1932) continue; + + return dev; + + } + libusb_free_device_list(list, 0); + + return NULL; +} + + +static void usage(char *n) +{ +fprintf(stderr,"Usage:\n" + "%s [-g [-n secs]] [-s [-p pwd]] [-h]\n" + " -h display this message\n" + " -g ask cryptopad to type password\n" + " -n secs wait secs before typing password\n" + " -s set password in cryptopad\n" + " -p pwd password to set, prompts otherwise\n" + " -w wipe password from cryptopad\n", + n); + +exit(1); +} + + + + +int main(int argc,char *argv[]) +{ + libusb_context *ctx; + libusb_device *dev; + libusb_device_handle *devh; + int wflag=0,gflag=0,sflag=0; + char *pass=NULL; + int c,ret; + int delay=0; + + + while((c=getopt(argc,argv,"hwgsp:n:"))!=-1) { + switch(c) { + case 'n': + if (optarg) delay=atoi(optarg); + break; + case 'g': + gflag++; + break; + case 's': + sflag++; + break; + case 'w': + wflag++; + break; + case 'p': + pass=optarg; + break; + default: + usage(argv[0]); + } + } + + if ((ret=libusb_init(&ctx))) + errx(EX_IOERR, "unable to initialize libusb: %i", ret); + + dev=find_device(ctx); + + if (!dev) err(1,"no cryptopad found"); + + if ((ret=libusb_open(dev, &devh))) err(1,"unable to open usb device: %i",ret); + + + if (sflag) { + char *pwd; + if (!pass) pass=getpass("Enter password:"); + + pwd=malloc(strlen(pass)+2); + strcpy(pwd,pass); + strcat(pwd,"\n"); + + libusb_control_transfer( devh, LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE, VENDOR_REQ_SET_KEY, 0, 0, pwd, strlen(pwd), TIMEOUT ); + } + + + if (gflag) + libusb_control_transfer( devh, LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE, VENDOR_REQ_SEND_KEY, delay, 0, NULL, 0, TIMEOUT ); + + if (wflag) + libusb_control_transfer( devh, LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE, VENDOR_REQ_CLEAR_KEY, 0, 0, NULL, 0, TIMEOUT ); + + + libusb_close(devh); + + return 0; +} + + + + + + |