Skip to content

Commit

Permalink
Merge pull request #48 from Nitrokey/format
Browse files Browse the repository at this point in the history
Run make format
  • Loading branch information
sosthene-nitrokey authored Dec 12, 2024
2 parents 73bd653 + a6a2e55 commit eebf78e
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 49 deletions.
7 changes: 3 additions & 4 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
#include "operations_ccid.h"
#include "return_codes.h"
#include "utils.h"
#include "operations_ccid.h"
#include "version.h"
#include <stdio.h>
#include <string.h>
Expand Down Expand Up @@ -100,7 +99,7 @@ int parse_cmd_and_run(int argc, char *const *argv) {
break;
case 'i': {// id | info
struct FullResponseStatus status;
memset(&status, 0, sizeof (struct FullResponseStatus));
memset(&status, 0, sizeof(struct FullResponseStatus));

res = device_get_status(&dev, &status);
check_ret((res != RET_NO_ERROR) && (res != RET_NO_PIN_ATTEMPTS), res);
Expand All @@ -113,7 +112,7 @@ int parse_cmd_and_run(int argc, char *const *argv) {
printf("\tCard serial: ");
print_card_serial(&status.response_status);
if (status.device_type == Nk3) {
printf("\tFirmware Nitrokey 3: v%d.%d.%d\n",
printf("\tFirmware Nitrokey 3: v%d.%d.%d\n",
(status.nk3_extra_info.firmware_version >> 22) & 0b1111111111,
(status.nk3_extra_info.firmware_version >> 6) & 0xFFFF,
status.nk3_extra_info.firmware_version & 0b111111);
Expand Down Expand Up @@ -167,7 +166,7 @@ int parse_cmd_and_run(int argc, char *const *argv) {
case 'r':
if (strncmp(argv[1], "reset", 15) == 0) {
if (argc != 2 && argc != 3) break;
res = nk3_reset(&dev, argc == 3 ? argv[2]: NULL);
res = nk3_reset(&dev, argc == 3 ? argv[2] : NULL);
} else if (strncmp(argv[1], "regenerate", 15) == 0) {
if (argc != 3) break;
res = regenerate_AES_key(&dev, argv[2]);
Expand Down
68 changes: 33 additions & 35 deletions src/operations_ccid.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@
#include <string.h>



int nk3_reset(struct Device *dev, const char * new_pin) {
int nk3_reset(struct Device *dev, const char *new_pin) {
libusb_device *usb_dev;
struct libusb_device_descriptor usb_desc;

Expand All @@ -42,7 +41,7 @@ int nk3_reset(struct Device *dev, const char * new_pin) {
printf("No Nitrokey 3 found. No operation performed\n");
return RET_NO_ERROR;
}

usb_dev = libusb_get_device(dev->mp_devhandle_ccid);

int r = libusb_get_device_descriptor(usb_dev, &usb_desc);
Expand All @@ -64,12 +63,12 @@ int nk3_reset(struct Device *dev, const char * new_pin) {

// 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);
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);
dev->ccid_buffer_out, icc_actual_length, &iccResult);
if (r != 0) {
return r;
}
Expand Down Expand Up @@ -116,13 +115,13 @@ int set_pin_ccid(struct Device *dev, const char *admin_PIN) {
return 0;
}

int nk3_change_pin(struct Device *dev, const char *old_pin, const char*new_pin) {
int nk3_change_pin(struct Device *dev, const char *old_pin, const char *new_pin) {
libusb_device *usb_dev;
struct libusb_device_descriptor usb_desc;

if (!dev->mp_devhandle_ccid) {
printf("No Nitrokey 3 found. No operation performed\n");
return RET_NO_ERROR;
return RET_NO_ERROR;
}

usb_dev = libusb_get_device(dev->mp_devhandle_ccid);
Expand All @@ -136,30 +135,30 @@ int nk3_change_pin(struct Device *dev, const char *old_pin, const char*new_pin)

if (usb_desc.idVendor != NITROKEY_USB_VID || usb_desc.idProduct != NITROKEY_3_USB_PID) {
printf("No Nitrokey 3 found. No operation performed\n");
return RET_NO_ERROR;
return RET_NO_ERROR;
}

TLV tlvs[] = {
{
.tag = Tag_Password,
.length = strnlen(old_pin, MAX_PIN_SIZE_CCID),
.type = 'S',
.v_str = old_pin,
},
{
.tag = Tag_NewPassword,
.length = strnlen(new_pin, MAX_PIN_SIZE_CCID),
.type = 'S',
.v_str = new_pin,
},
{
.tag = Tag_Password,
.length = strnlen(old_pin, MAX_PIN_SIZE_CCID),
.type = 'S',
.v_str = old_pin,
},
{
.tag = Tag_NewPassword,
.length = strnlen(new_pin, MAX_PIN_SIZE_CCID),
.type = 'S',
.v_str = new_pin,
},
};
// encode
uint32_t icc_actual_length = icc_pack_tlvs_for_sending(dev->ccid_buffer_out, sizeof dev->ccid_buffer_out,
tlvs, ARR_LEN(tlvs), Ins_ChangePIN);
// 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);
dev->ccid_buffer_out, icc_actual_length, &iccResult);
if (r != 0) {
return r;
}
Expand Down Expand Up @@ -223,15 +222,14 @@ int authenticate_or_set_ccid(struct Device *dev, const char *admin_PIN) {
}


int delete_secret_on_device_ccid(struct Device *dev) {
int delete_secret_on_device_ccid(struct Device *dev) {
TLV tlvs[] = {
{
.tag = Tag_CredentialId,
.length = SLOT_NAME_LEN,
.type = 'S',
.v_str = SLOT_NAME,
}
};
{
.tag = Tag_CredentialId,
.length = SLOT_NAME_LEN,
.type = 'S',
.v_str = SLOT_NAME,
}};

clean_buffers(dev);
// encode
Expand Down Expand Up @@ -275,11 +273,11 @@ int set_secret_on_device_ccid(struct Device *dev, const char *admin_PIN, const c
}

#ifdef CCID_SECRETS_AUTHENTICATE_OR_CREATE_PIN
if (strnlen(admin_PIN, 30) > 0) {
if (authenticate_or_set_ccid(dev, admin_PIN) != RET_NO_ERROR) {
return RET_SECURITY_STATUS_NOT_SATISFIED;
}
if (strnlen(admin_PIN, 30) > 0) {
if (authenticate_or_set_ccid(dev, admin_PIN) != RET_NO_ERROR) {
return RET_SECURITY_STATUS_NOT_SATISFIED;
}
}
#endif
TLV tlvs[] = {
{
Expand Down Expand Up @@ -317,7 +315,7 @@ int set_secret_on_device_ccid(struct Device *dev, const char *admin_PIN, const c
// 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);
dev->ccid_buffer_out, icc_actual_length, &iccResult);


if (r != 0) {
Expand Down
4 changes: 2 additions & 2 deletions src/operations_ccid.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ 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, struct FullResponseStatus *full_response);
int nk3_change_pin(struct Device *dev, const char *old_pin, const char*new_pin);
int nk3_change_pin(struct Device *dev, const char *old_pin, const char *new_pin);
// new_pin can be `null`
//
// If it is, no new pin will be set
int nk3_reset(struct Device *dev, const char * new_pin);
int nk3_reset(struct Device *dev, const char *new_pin);


#endif//NITROKEY_HOTP_VERIFICATION_OPERATIONS_CCID_H
2 changes: 1 addition & 1 deletion src/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
// #define FEATURE_CCID_ASK_FOR_PIN_ON_ERROR

// Use the provided PIN for authentication over CCID
// #define CCID_AUTHENTICATE
// #define CCID_AUTHENTICATE

// Attempt to authenticate before setting the PIN, if no pin is present, create the PIN
#define CCID_SECRETS_AUTHENTICATE_OR_CREATE_PIN
Expand Down
14 changes: 7 additions & 7 deletions src/structs.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,20 +117,20 @@ struct ResponseStatus {
};

enum DeviceType {
Unknown = 0,
Nk3,
NkPro2,
NkStorage,
LibremKey,
Unknown = 0,
Nk3,
NkPro2,
NkStorage,
LibremKey,
};

struct FullResponseStatus {
enum DeviceType device_type;
struct ResponseStatus response_status;
struct {
// Only valid if device_type is NK3
uint8_t pgp_admin_pin_retries;
uint8_t pgp_user_pin_retries;
uint8_t pgp_admin_pin_retries;
uint8_t pgp_user_pin_retries;
uint32_t firmware_version;
} nk3_extra_info;
};
Expand Down

0 comments on commit eebf78e

Please sign in to comment.