Home | History | Annotate | Line # | Download | only in tools
pin.c revision 1.1.1.2.2.1
      1          1.1  christos /*
      2          1.1  christos  * Copyright (c) 2018 Yubico AB. All rights reserved.
      3          1.1  christos  * Use of this source code is governed by a BSD-style
      4          1.1  christos  * license that can be found in the LICENSE file.
      5  1.1.1.2.2.1    martin  * SPDX-License-Identifier: BSD-2-Clause
      6          1.1  christos  */
      7          1.1  christos 
      8          1.1  christos #include <fido.h>
      9          1.1  christos #include <stdbool.h>
     10          1.1  christos #include <stdio.h>
     11          1.1  christos #include <stdlib.h>
     12          1.1  christos #include <string.h>
     13          1.1  christos #ifdef HAVE_UNISTD_H
     14          1.1  christos #include <unistd.h>
     15          1.1  christos #endif
     16          1.1  christos 
     17          1.1  christos #include "../openbsd-compat/openbsd-compat.h"
     18          1.1  christos #include "extern.h"
     19          1.1  christos 
     20          1.1  christos int
     21          1.1  christos pin_set(char *path)
     22          1.1  christos {
     23          1.1  christos 	fido_dev_t *dev = NULL;
     24          1.1  christos 	char prompt[1024];
     25  1.1.1.2.2.1    martin 	char pin1[128];
     26  1.1.1.2.2.1    martin 	char pin2[128];
     27          1.1  christos 	int r;
     28          1.1  christos 	int status = 1;
     29          1.1  christos 
     30          1.1  christos 	dev = open_dev(path);
     31          1.1  christos 
     32          1.1  christos 	r = snprintf(prompt, sizeof(prompt), "Enter new PIN for %s: ", path);
     33          1.1  christos 	if (r < 0 || (size_t)r >= sizeof(prompt)) {
     34          1.1  christos 		warnx("snprintf");
     35          1.1  christos 		goto out;
     36          1.1  christos 	}
     37          1.1  christos 
     38          1.1  christos 	if (!readpassphrase(prompt, pin1, sizeof(pin1), RPP_ECHO_OFF)) {
     39          1.1  christos 		warnx("readpassphrase");
     40          1.1  christos 		goto out;
     41          1.1  christos 	}
     42          1.1  christos 
     43          1.1  christos 	r = snprintf(prompt, sizeof(prompt), "Enter the same PIN again: ");
     44          1.1  christos 	if (r < 0 || (size_t)r >= sizeof(prompt)) {
     45          1.1  christos 		warnx("snprintf");
     46          1.1  christos 		goto out;
     47          1.1  christos 	}
     48          1.1  christos 
     49          1.1  christos 	if (!readpassphrase(prompt, pin2, sizeof(pin2), RPP_ECHO_OFF)) {
     50          1.1  christos 		warnx("readpassphrase");
     51          1.1  christos 		goto out;
     52          1.1  christos 	}
     53          1.1  christos 
     54          1.1  christos 	if (strcmp(pin1, pin2) != 0) {
     55          1.1  christos 		fprintf(stderr, "PINs do not match. Try again.\n");
     56          1.1  christos 		goto out;
     57          1.1  christos 	}
     58          1.1  christos 
     59  1.1.1.2.2.1    martin 	if (strlen(pin1) < 4 || strlen(pin1) > 63) {
     60  1.1.1.2.2.1    martin 		fprintf(stderr, "invalid PIN length\n");
     61  1.1.1.2.2.1    martin 		goto out;
     62  1.1.1.2.2.1    martin 	}
     63  1.1.1.2.2.1    martin 
     64          1.1  christos 	if ((r = fido_dev_set_pin(dev, pin1, NULL)) != FIDO_OK) {
     65          1.1  christos 		warnx("fido_dev_set_pin: %s", fido_strerr(r));
     66          1.1  christos 		goto out;
     67          1.1  christos 	}
     68          1.1  christos 
     69          1.1  christos 	fido_dev_close(dev);
     70          1.1  christos 	fido_dev_free(&dev);
     71          1.1  christos 
     72          1.1  christos 	status = 0;
     73          1.1  christos out:
     74          1.1  christos 	explicit_bzero(pin1, sizeof(pin1));
     75          1.1  christos 	explicit_bzero(pin2, sizeof(pin2));
     76          1.1  christos 
     77          1.1  christos 	exit(status);
     78          1.1  christos }
     79          1.1  christos 
     80          1.1  christos int
     81          1.1  christos pin_change(char *path)
     82          1.1  christos {
     83          1.1  christos 	fido_dev_t *dev = NULL;
     84          1.1  christos 	char prompt[1024];
     85  1.1.1.2.2.1    martin 	char pin0[128];
     86  1.1.1.2.2.1    martin 	char pin1[128];
     87  1.1.1.2.2.1    martin 	char pin2[128];
     88          1.1  christos 	int r;
     89          1.1  christos 	int status = 1;
     90          1.1  christos 
     91          1.1  christos 	if (path == NULL)
     92          1.1  christos 		usage();
     93          1.1  christos 
     94          1.1  christos 	dev = open_dev(path);
     95          1.1  christos 
     96          1.1  christos 	r = snprintf(prompt, sizeof(prompt), "Enter current PIN for %s: ", path);
     97          1.1  christos 	if (r < 0 || (size_t)r >= sizeof(prompt)) {
     98          1.1  christos 		warnx("snprintf");
     99          1.1  christos 		goto out;
    100          1.1  christos 	}
    101          1.1  christos 
    102          1.1  christos 	if (!readpassphrase(prompt, pin0, sizeof(pin0), RPP_ECHO_OFF)) {
    103          1.1  christos 		warnx("readpassphrase");
    104          1.1  christos 		goto out;
    105          1.1  christos 	}
    106          1.1  christos 
    107  1.1.1.2.2.1    martin 	if (strlen(pin0) < 4 || strlen(pin0) > 63) {
    108  1.1.1.2.2.1    martin 		warnx("invalid PIN length");
    109  1.1.1.2.2.1    martin 		goto out;
    110  1.1.1.2.2.1    martin 	}
    111  1.1.1.2.2.1    martin 
    112          1.1  christos 	r = snprintf(prompt, sizeof(prompt), "Enter new PIN for %s: ", path);
    113          1.1  christos 	if (r < 0 || (size_t)r >= sizeof(prompt)) {
    114          1.1  christos 		warnx("snprintf");
    115          1.1  christos 		goto out;
    116          1.1  christos 	}
    117          1.1  christos 
    118          1.1  christos 	if (!readpassphrase(prompt, pin1, sizeof(pin1), RPP_ECHO_OFF)) {
    119          1.1  christos 		warnx("readpassphrase");
    120          1.1  christos 		goto out;
    121          1.1  christos 	}
    122          1.1  christos 
    123          1.1  christos 	r = snprintf(prompt, sizeof(prompt), "Enter the same PIN again: ");
    124          1.1  christos 	if (r < 0 || (size_t)r >= sizeof(prompt)) {
    125          1.1  christos 		warnx("snprintf");
    126          1.1  christos 		goto out;
    127          1.1  christos 	}
    128          1.1  christos 
    129          1.1  christos 	if (!readpassphrase(prompt, pin2, sizeof(pin2), RPP_ECHO_OFF)) {
    130          1.1  christos 		warnx("readpassphrase");
    131          1.1  christos 		goto out;
    132          1.1  christos 	}
    133          1.1  christos 
    134          1.1  christos 	if (strcmp(pin1, pin2) != 0) {
    135          1.1  christos 		fprintf(stderr, "PINs do not match. Try again.\n");
    136          1.1  christos 		goto out;
    137          1.1  christos 	}
    138          1.1  christos 
    139  1.1.1.2.2.1    martin 	if (strlen(pin1) < 4 || strlen(pin1) > 63) {
    140  1.1.1.2.2.1    martin 		fprintf(stderr, "invalid PIN length\n");
    141  1.1.1.2.2.1    martin 		goto out;
    142  1.1.1.2.2.1    martin 	}
    143  1.1.1.2.2.1    martin 
    144          1.1  christos 	if ((r = fido_dev_set_pin(dev, pin1, pin0)) != FIDO_OK) {
    145          1.1  christos 		warnx("fido_dev_set_pin: %s", fido_strerr(r));
    146          1.1  christos 		goto out;
    147          1.1  christos 	}
    148          1.1  christos 
    149          1.1  christos 	fido_dev_close(dev);
    150          1.1  christos 	fido_dev_free(&dev);
    151          1.1  christos 
    152          1.1  christos 	status = 0;
    153          1.1  christos out:
    154          1.1  christos 	explicit_bzero(pin0, sizeof(pin0));
    155          1.1  christos 	explicit_bzero(pin1, sizeof(pin1));
    156          1.1  christos 	explicit_bzero(pin2, sizeof(pin2));
    157          1.1  christos 
    158          1.1  christos 	exit(status);
    159          1.1  christos }
    160