blob: 8acb66a4dadab574536c654b8cdefb623039bbdc [file] [log] [blame]
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01001#!/usr/bin/php
2<?php
3
4 /* Copyright (c) 2015 Yubico AB
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * * Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer in the documentation and/or other materials provided
17 * with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32/**
33 * This is a basic example of a u2f-server command line that can be used
34 * with the u2f-host binary to perform regitrations and authentications.
35 */
36
37require_once('../../src/u2flib_server/U2F.php');
38
39$options = getopt("rao:R:");
40$mode;
41$challenge;
42$response;
43$result;
44$regs;
45
46if(array_key_exists('r', $options)) {
47 $mode = "register";
48} elseif(array_key_exists('a', $options)) {
49 if(!array_key_exists('R', $options)) {
50 print "a registration must be supplied with -R";
51 exit(1);
52 }
53 $regs = json_decode('[' . $options['R'] . ']');
54 $mode = "authenticate";
55} else {
56 print "-r or -a must be used\n";
57 exit(1);
58}
59if(!array_key_exists('o', $options)) {
60 print "origin must be supplied with -o\n";
61 exit(1);
62}
63
64$u2f = new u2flib_server\U2F($options['o']);
65
66if($mode === "register") {
67 $challenge = $u2f->getRegisterData();
68} elseif($mode === "authenticate") {
69 $challenge = $u2f->getAuthenticateData($regs);
70}
71
72print json_encode($challenge[0]) . "\n";
73$response = fgets(STDIN);
74
75if($mode === "register") {
76 $result = $u2f->doRegister($challenge[0], json_decode($response));
77} elseif($mode === "authenticate") {
78 $result = $u2f->doAuthenticate($challenge, $regs, json_decode($response));
79}
80
81print json_encode($result) . "\n";
82
83?>