Skip to content

Commit

Permalink
Add reset command for nitrokey 3
Browse files Browse the repository at this point in the history
  • Loading branch information
sosthene-nitrokey committed Nov 25, 2024
1 parent e9050e0 commit de355ed
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#include "ccid.h"
#include "operations.h"
#include "operations_ccid.h"
#include "return_codes.h"
#include "utils.h"
#include "version.h"
Expand Down Expand Up @@ -134,8 +135,13 @@ int parse_cmd_and_run(int argc, char *const *argv) {
}
break;
case 'r':
if (argc != 3) break;
res = regenerate_AES_key(&dev, argv[2]);
if (strncmp(argv[1], "reset", 15) == 0) {
if (argc != 2) break;
res = nk3_reset(&dev);
} else if (strncmp(argv[1], "regenerate", 15) == 0) {
if (argc != 3) break;
res = regenerate_AES_key(&dev, argv[2]);
}
break;
default:
break;
Expand Down
41 changes: 41 additions & 0 deletions src/operations_ccid.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,47 @@
#include <string.h>



int nk3_reset(struct Device *dev) {
libusb_device *usb_dev;
struct libusb_device_descriptor usb_desc;
usb_dev = libusb_get_device(dev->mp_devhandle_ccid);

int r = libusb_get_device_descriptor(usb_dev, &usb_desc);

if (r < 0) {
return r;
}


if (usb_desc.idVendor != NITROKEY_USB_VID || usb_desc.idProduct != NITROKEY_3_USB_PID) {
return 0;
}


uint8_t buf[10];
// encode
uint32_t icc_actual_length = iso7816_compose(buf, sizeof buf, Ins_Reset, 0xDE, 0xAD, 0, 0, NULL, 0);

// encode ccid wrapper
icc_actual_length = icc_compose(dev->ccid_buffer_out, sizeof dev->ccid_buffer_out,
0x6F, icc_actual_length,
0, 0, 0, buf);
// send
IccResult iccResult;
r = ccid_process_single(dev->mp_devhandle_ccid, dev->ccid_buffer_in, sizeof dev->ccid_buffer_in,
dev->ccid_buffer_out, icc_actual_length, &iccResult);
if (r != 0) {
return r;
}
// check status code
if (iccResult.data_status_code != 0x9000) {
return 1;
}

return RET_NO_ERROR;
}

int set_pin_ccid(struct Device *dev, const char *admin_PIN) {
TLV tlvs[] = {
{
Expand Down
1 change: 1 addition & 0 deletions src/operations_ccid.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ int authenticate_or_set_ccid(struct Device *dev, const char *admin_PIN);
int set_secret_on_device_ccid(struct Device *dev, const char *admin_PIN, const char *OTP_secret_base32, const uint64_t hotp_counter);
int verify_code_ccid(struct Device *dev, const uint32_t code_to_verify);
int status_ccid(libusb_device_handle *handle, int *attempt_counter, uint16_t *firmware_version, uint32_t *serial_number);
int nk3_reset(struct Device *dev);


#endif//NITROKEY_HOTP_VERIFICATION_OPERATIONS_CCID_H

0 comments on commit de355ed

Please sign in to comment.