Home | History | Annotate | Line # | Download | only in pamu2fcfg
pamu2fcfg.c revision 1.1
      1  1.1  christos /*
      2  1.1  christos  * Copyright (C) 2014-2018 Yubico AB - See COPYING
      3  1.1  christos  */
      4  1.1  christos 
      5  1.1  christos #define BUFSIZE 1024
      6  1.1  christos #define PAM_PREFIX "pam://"
      7  1.1  christos #define TIMEOUT 15
      8  1.1  christos #define FREQUENCY 1
      9  1.1  christos 
     10  1.1  christos #include <fido.h>
     11  1.1  christos 
     12  1.1  christos #include <stdio.h>
     13  1.1  christos #include <stdlib.h>
     14  1.1  christos #include <string.h>
     15  1.1  christos #include <getopt.h>
     16  1.1  christos #include <unistd.h>
     17  1.1  christos #include <sys/types.h>
     18  1.1  christos #include <pwd.h>
     19  1.1  christos 
     20  1.1  christos #include "b64.h"
     21  1.1  christos #include "cmdline.h"
     22  1.1  christos #include "util.h"
     23  1.1  christos #ifndef HAVE_READPASSPHRASE
     24  1.1  christos #include "_readpassphrase.h"
     25  1.1  christos #else
     26  1.1  christos #include <readpassphrase.h>
     27  1.1  christos #endif
     28  1.1  christos 
     29  1.1  christos int main(int argc, char *argv[]) {
     30  1.1  christos   int exit_code = EXIT_FAILURE;
     31  1.1  christos   struct gengetopt_args_info args_info;
     32  1.1  christos   char buf[BUFSIZE];
     33  1.1  christos   char prompt[BUFSIZE];
     34  1.1  christos   char pin[BUFSIZE];
     35  1.1  christos   char *p;
     36  1.1  christos   char *response;
     37  1.1  christos   fido_cred_t *cred = NULL;
     38  1.1  christos   fido_dev_info_t *devlist = NULL;
     39  1.1  christos   fido_dev_t *dev = NULL;
     40  1.1  christos   const fido_dev_info_t *di = NULL;
     41  1.1  christos   size_t ndevs;
     42  1.1  christos   int cose_type;
     43  1.1  christos   int resident_key;
     44  1.1  christos   int user_presence;
     45  1.1  christos   int user_verification;
     46  1.1  christos   int pin_verification;
     47  1.1  christos   int r;
     48  1.1  christos   int n;
     49  1.1  christos   char *origin = NULL;
     50  1.1  christos   char *appid = NULL;
     51  1.1  christos   char *user = NULL;
     52  1.1  christos   char *b64_kh;
     53  1.1  christos   char *b64_pk;
     54  1.1  christos   struct passwd *passwd;
     55  1.1  christos   const unsigned char *kh = NULL;
     56  1.1  christos   size_t kh_len;
     57  1.1  christos   const unsigned char *pk = NULL;
     58  1.1  christos   size_t pk_len;
     59  1.1  christos   unsigned char userid[32];
     60  1.1  christos   unsigned char challenge[32];
     61  1.1  christos   unsigned i;
     62  1.1  christos   unsigned max_index = 0;
     63  1.1  christos 
     64  1.1  christos   if (cmdline_parser(argc, argv, &args_info) != 0)
     65  1.1  christos     exit(EXIT_FAILURE);
     66  1.1  christos 
     67  1.1  christos   if (args_info.help_given) {
     68  1.1  christos     cmdline_parser_print_help();
     69  1.1  christos     printf("\nReport bugs at <https://github.com/Yubico/pam-u2f>.\n");
     70  1.1  christos     exit(EXIT_SUCCESS);
     71  1.1  christos   }
     72  1.1  christos 
     73  1.1  christos   fido_init(args_info.debug_flag ? FIDO_DEBUG : 0);
     74  1.1  christos 
     75  1.1  christos   cred = fido_cred_new();
     76  1.1  christos   if (!cred) {
     77  1.1  christos     fprintf(stderr, "fido_cred_new failed\n");
     78  1.1  christos     exit(EXIT_FAILURE);
     79  1.1  christos   }
     80  1.1  christos 
     81  1.1  christos   if (!random_bytes(challenge, sizeof(challenge))) {
     82  1.1  christos     fprintf(stderr, "random_bytes failed\n");
     83  1.1  christos     exit(EXIT_FAILURE);
     84  1.1  christos   }
     85  1.1  christos 
     86  1.1  christos   if (args_info.type_given) {
     87  1.1  christos     if (!strcasecmp(args_info.type_arg, "es256"))
     88  1.1  christos       cose_type = COSE_ES256;
     89  1.1  christos     else if (!strcasecmp(args_info.type_arg, "rs256"))
     90  1.1  christos       cose_type = COSE_RS256;
     91  1.1  christos     else {
     92  1.1  christos       fprintf(stderr, "Unknown COSE type '%s'.\n", args_info.type_arg);
     93  1.1  christos       exit(EXIT_FAILURE);
     94  1.1  christos     }
     95  1.1  christos   } else
     96  1.1  christos     cose_type = COSE_ES256;
     97  1.1  christos 
     98  1.1  christos   r = fido_cred_set_type(cred, cose_type);
     99  1.1  christos   if (r != FIDO_OK) {
    100  1.1  christos     fprintf(stderr, "error: fido_cred_set_type (%d): %s\n", r, fido_strerr(r));
    101  1.1  christos     exit(EXIT_FAILURE);
    102  1.1  christos   }
    103  1.1  christos 
    104  1.1  christos   r = fido_cred_set_clientdata_hash(cred, challenge, sizeof(challenge));
    105  1.1  christos   if (r != FIDO_OK) {
    106  1.1  christos     fprintf(stderr, "error: fido_cred_set_clientdata_hash (%d): %s\n", r,
    107  1.1  christos             fido_strerr(r));
    108  1.1  christos     exit(EXIT_FAILURE);
    109  1.1  christos   }
    110  1.1  christos 
    111  1.1  christos   if (args_info.origin_given)
    112  1.1  christos     origin = args_info.origin_arg;
    113  1.1  christos   else {
    114  1.1  christos     if (!strcpy(buf, PAM_PREFIX)) {
    115  1.1  christos       fprintf(stderr, "strcpy failed\n");
    116  1.1  christos       exit(EXIT_FAILURE);
    117  1.1  christos     }
    118  1.1  christos     if (gethostname(buf + strlen(PAM_PREFIX), BUFSIZE - strlen(PAM_PREFIX)) ==
    119  1.1  christos         -1) {
    120  1.1  christos       perror("gethostname");
    121  1.1  christos       exit(EXIT_FAILURE);
    122  1.1  christos     }
    123  1.1  christos     origin = buf;
    124  1.1  christos   }
    125  1.1  christos 
    126  1.1  christos   if (args_info.verbose_given)
    127  1.1  christos     fprintf(stderr, "Setting origin to %s\n", origin);
    128  1.1  christos 
    129  1.1  christos   if (args_info.appid_given)
    130  1.1  christos     appid = args_info.appid_arg;
    131  1.1  christos   else {
    132  1.1  christos     appid = origin;
    133  1.1  christos   }
    134  1.1  christos 
    135  1.1  christos   if (args_info.verbose_given)
    136  1.1  christos     fprintf(stderr, "Setting appid to %s\n", appid);
    137  1.1  christos 
    138  1.1  christos   r = fido_cred_set_rp(cred, origin, appid);
    139  1.1  christos   if (r != FIDO_OK) {
    140  1.1  christos     fprintf(stderr, "error: fido_cred_set_rp (%d) %s\n", r, fido_strerr(r));
    141  1.1  christos     exit(EXIT_FAILURE);
    142  1.1  christos   }
    143  1.1  christos 
    144  1.1  christos   if (args_info.username_given)
    145  1.1  christos     user = args_info.username_arg;
    146  1.1  christos   else {
    147  1.1  christos     passwd = getpwuid(getuid());
    148  1.1  christos     if (passwd == NULL) {
    149  1.1  christos       perror("getpwuid");
    150  1.1  christos       exit(EXIT_FAILURE);
    151  1.1  christos     }
    152  1.1  christos     user = passwd->pw_name;
    153  1.1  christos   }
    154  1.1  christos 
    155  1.1  christos   if (!random_bytes(userid, sizeof(userid))) {
    156  1.1  christos     fprintf(stderr, "random_bytes failed\n");
    157  1.1  christos     exit(EXIT_FAILURE);
    158  1.1  christos   }
    159  1.1  christos 
    160  1.1  christos   if (args_info.verbose_given) {
    161  1.1  christos     fprintf(stderr, "Setting user to %s\n", user);
    162  1.1  christos     fprintf(stderr, "Setting user id to ");
    163  1.1  christos     for (size_t i = 0; i < sizeof(userid); i++)
    164  1.1  christos       fprintf(stderr, "%02x", userid[i]);
    165  1.1  christos     fprintf(stderr, "\n");
    166  1.1  christos   }
    167  1.1  christos 
    168  1.1  christos   r = fido_cred_set_user(cred, userid, sizeof(userid), user, NULL, NULL);
    169  1.1  christos   if (r != FIDO_OK) {
    170  1.1  christos     fprintf(stderr, "error: fido_cred_set_user (%d) %s\n", r, fido_strerr(r));
    171  1.1  christos     exit(EXIT_FAILURE);
    172  1.1  christos   }
    173  1.1  christos 
    174  1.1  christos   if (args_info.resident_given)
    175  1.1  christos     resident_key = 1;
    176  1.1  christos   else
    177  1.1  christos     resident_key = 0;
    178  1.1  christos 
    179  1.1  christos   if (args_info.no_user_presence_given)
    180  1.1  christos     user_presence = 0;
    181  1.1  christos   else
    182  1.1  christos     user_presence = 1;
    183  1.1  christos 
    184  1.1  christos   if (args_info.user_verification_given)
    185  1.1  christos     user_verification = 1;
    186  1.1  christos   else
    187  1.1  christos     user_verification = 0;
    188  1.1  christos 
    189  1.1  christos   if (args_info.pin_verification_given)
    190  1.1  christos     pin_verification = 1;
    191  1.1  christos   else
    192  1.1  christos     pin_verification = 0;
    193  1.1  christos 
    194  1.1  christos   r = fido_cred_set_rk(cred, resident_key);
    195  1.1  christos   if (r != FIDO_OK) {
    196  1.1  christos     fprintf(stderr, "error: fido_cred_set_rk (%d) %s\n", r, fido_strerr(r));
    197  1.1  christos     exit(EXIT_FAILURE);
    198  1.1  christos   }
    199  1.1  christos 
    200  1.1  christos   r = fido_cred_set_uv(cred, false);
    201  1.1  christos   if (r != FIDO_OK) {
    202  1.1  christos     fprintf(stderr, "error: fido_cred_set_uv (%d) %s\n", r, fido_strerr(r));
    203  1.1  christos     exit(EXIT_FAILURE);
    204  1.1  christos   }
    205  1.1  christos 
    206  1.1  christos   devlist = fido_dev_info_new(64);
    207  1.1  christos   if (!devlist) {
    208  1.1  christos     fprintf(stderr, "error: fido_dev_info_new failed\n");
    209  1.1  christos     exit(EXIT_FAILURE);
    210  1.1  christos   }
    211  1.1  christos 
    212  1.1  christos   r = fido_dev_info_manifest(devlist, 64, &ndevs);
    213  1.1  christos   if (r != FIDO_OK) {
    214  1.1  christos     fprintf(stderr, "Unable to discover device(s), %s (%d)\n", fido_strerr(r),
    215  1.1  christos             r);
    216  1.1  christos     exit(EXIT_FAILURE);
    217  1.1  christos   }
    218  1.1  christos 
    219  1.1  christos   if (ndevs == 0) {
    220  1.1  christos     for (i = 0; i < TIMEOUT; i += FREQUENCY) {
    221  1.1  christos       fprintf(stderr,
    222  1.1  christos               "\rNo U2F device available, please insert one now, you "
    223  1.1  christos               "have %2d seconds",
    224  1.1  christos               TIMEOUT - i);
    225  1.1  christos       fflush(stderr);
    226  1.1  christos       sleep(FREQUENCY);
    227  1.1  christos 
    228  1.1  christos       r = fido_dev_info_manifest(devlist, 64, &ndevs);
    229  1.1  christos       if (r != FIDO_OK) {
    230  1.1  christos         fprintf(stderr, "\nUnable to discover device(s), %s (%d)",
    231  1.1  christos                 fido_strerr(r), r);
    232  1.1  christos         exit(EXIT_FAILURE);
    233  1.1  christos       }
    234  1.1  christos 
    235  1.1  christos       if (ndevs != 0) {
    236  1.1  christos         fprintf(stderr, "\nDevice found!\n");
    237  1.1  christos         break;
    238  1.1  christos       }
    239  1.1  christos     }
    240  1.1  christos   }
    241  1.1  christos 
    242  1.1  christos   if (ndevs == 0) {
    243  1.1  christos     fprintf(stderr, "\rNo device found. Aborting.                              "
    244  1.1  christos                     "           \n");
    245  1.1  christos     exit(EXIT_FAILURE);
    246  1.1  christos   }
    247  1.1  christos 
    248  1.1  christos   /* XXX loop over every device? */
    249  1.1  christos   dev = fido_dev_new();
    250  1.1  christos   if (!dev) {
    251  1.1  christos     fprintf(stderr, "fido_dev_new failed\n");
    252  1.1  christos     exit(EXIT_FAILURE);
    253  1.1  christos   }
    254  1.1  christos 
    255  1.1  christos   di = fido_dev_info_ptr(devlist, 0);
    256  1.1  christos   if (!di) {
    257  1.1  christos     fprintf(stderr, "error: fido_dev_info_ptr returned NULL\n");
    258  1.1  christos     exit(EXIT_FAILURE);
    259  1.1  christos   }
    260  1.1  christos 
    261  1.1  christos   r = fido_dev_open(dev, fido_dev_info_path(di));
    262  1.1  christos   if (r != FIDO_OK) {
    263  1.1  christos     fprintf(stderr, "error: fido_dev_open (%d) %s\n", r, fido_strerr(r));
    264  1.1  christos     exit(EXIT_FAILURE);
    265  1.1  christos   }
    266  1.1  christos 
    267  1.1  christos   r = fido_dev_make_cred(dev, cred, NULL);
    268  1.1  christos   if (r == FIDO_ERR_PIN_REQUIRED) {
    269  1.1  christos     n = snprintf(prompt, sizeof(prompt),
    270  1.1  christos                  "Enter PIN for %s: ", fido_dev_info_path(di));
    271  1.1  christos     if (n < 0 || (size_t) n >= sizeof(prompt)) {
    272  1.1  christos       fprintf(stderr, "error: snprintf prompt");
    273  1.1  christos       exit(EXIT_FAILURE);
    274  1.1  christos     }
    275  1.1  christos     if (!readpassphrase(prompt, pin, sizeof(pin), RPP_ECHO_OFF)) {
    276  1.1  christos       fprintf(stderr, "error: failed to read pin");
    277  1.1  christos       exit(EXIT_FAILURE);
    278  1.1  christos     }
    279  1.1  christos     r = fido_dev_make_cred(dev, cred, pin);
    280  1.1  christos   }
    281  1.1  christos   explicit_bzero(pin, sizeof(pin));
    282  1.1  christos 
    283  1.1  christos   if (r != FIDO_OK) {
    284  1.1  christos     fprintf(stderr, "error: fido_dev_make_cred (%d) %s\n", r, fido_strerr(r));
    285  1.1  christos     exit(EXIT_FAILURE);
    286  1.1  christos   }
    287  1.1  christos 
    288  1.1  christos   r = fido_cred_verify(cred);
    289  1.1  christos   if (r != FIDO_OK) {
    290  1.1  christos     fprintf(stderr, "error: fido_cred_verify (%d) %s\n", r, fido_strerr(r));
    291  1.1  christos     exit(EXIT_FAILURE);
    292  1.1  christos   }
    293  1.1  christos 
    294  1.1  christos   kh = fido_cred_id_ptr(cred);
    295  1.1  christos   if (!kh) {
    296  1.1  christos     fprintf(stderr, "error: fido_cred_id_ptr returned NULL\n");
    297  1.1  christos     exit(EXIT_FAILURE);
    298  1.1  christos   }
    299  1.1  christos 
    300  1.1  christos   kh_len = fido_cred_id_len(cred);
    301  1.1  christos   if (kh_len == 0) {
    302  1.1  christos     fprintf(stderr, "error: fido_cred_id_len returned 0\n");
    303  1.1  christos     exit(EXIT_FAILURE);
    304  1.1  christos   }
    305  1.1  christos 
    306  1.1  christos   pk = (const unsigned char *) fido_cred_pubkey_ptr(cred);
    307  1.1  christos   if (!pk) {
    308  1.1  christos     fprintf(stderr, "error: fido_cred_pubkey_ptr returned NULL\n");
    309  1.1  christos     exit(EXIT_FAILURE);
    310  1.1  christos   }
    311  1.1  christos 
    312  1.1  christos   pk_len = fido_cred_pubkey_len(cred);
    313  1.1  christos   if (pk_len == 0) {
    314  1.1  christos     fprintf(stderr, "error: fido_cred_pubkey_len returned 0\n");
    315  1.1  christos     exit(EXIT_FAILURE);
    316  1.1  christos   }
    317  1.1  christos 
    318  1.1  christos   if (!b64_encode(kh, kh_len, &b64_kh)) {
    319  1.1  christos     fprintf(stderr, "error: failed to encode key handle\n");
    320  1.1  christos     exit(EXIT_FAILURE);
    321  1.1  christos   }
    322  1.1  christos 
    323  1.1  christos   if (!b64_encode(pk, pk_len, &b64_pk)) {
    324  1.1  christos     fprintf(stderr, "error: failed to encode public key\n");
    325  1.1  christos     exit(EXIT_FAILURE);
    326  1.1  christos   }
    327  1.1  christos 
    328  1.1  christos   if (!args_info.nouser_given)
    329  1.1  christos     printf("%s", user);
    330  1.1  christos 
    331  1.1  christos   printf(":%s,%s,%s,%s%s%s", resident_key ? "*" : b64_kh, b64_pk,
    332  1.1  christos          cose_type == COSE_ES256 ? "es256" : "rs256",
    333  1.1  christos          user_presence ? "+presence" : "",
    334  1.1  christos          user_verification ? "+verification" : "",
    335  1.1  christos          pin_verification ? "+pin" : "");
    336  1.1  christos 
    337  1.1  christos   exit_code = EXIT_SUCCESS;
    338  1.1  christos 
    339  1.1  christos   fido_dev_info_free(&devlist, ndevs);
    340  1.1  christos   fido_cred_free(&cred);
    341  1.1  christos   fido_dev_free(&dev);
    342  1.1  christos 
    343  1.1  christos   free(b64_kh);
    344  1.1  christos   free(b64_pk);
    345  1.1  christos 
    346  1.1  christos   exit(exit_code);
    347  1.1  christos }
    348