Last active
July 28, 2018 08:10
-
-
Save Sjlver/dca13002d48a35301b0fbedde12f06bd to your computer and use it in GitHub Desktop.
Script to generate an U2F private key and certificate for OnlyKey
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# onlykey-u2f-setup.sh: Helps setting up U2F for OnlyKey. | |
# Copyright (c) 2016, Jonas Wagner <ltlygwayh@gmail.com> | |
# All rights reserved. | |
# | |
# Redistribution and use in source and binary forms, with or without | |
# modification, are permitted provided that the following conditions are met: | |
# | |
# 1. Redistributions of source code must retain the above copyright notice, this | |
# list of conditions and the following disclaimer. | |
# | |
# 2. Redistributions in binary form must reproduce the above copyright notice, | |
# this list of conditions and the following disclaimer in the documentation | |
# and/or other materials provided with the distribution. | |
# | |
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | |
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | |
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | |
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | |
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
set -e | |
set -o pipefail | |
# Check prerequisites | |
for command in openssl sed grep tr xxd; do | |
if ! which "$command" >/dev/null 2>&1; then | |
echo "Could not find command: $command" >&2 | |
echo "Please install the corresponding package." >&2 | |
exit 1 | |
fi | |
done | |
for openssl_subcommand in ecparam req x509; do | |
if ! openssl list-standard-commands | grep -q "$openssl_subcommand"; then | |
echo "OpenSSL does not support the \"$openssl_subcommand\" command." >&2 | |
echo "Please compile a full-featured version of OpenSSL." >&2 | |
exit 1 | |
fi | |
done | |
# Create a temporary work directory | |
workdir="$(mktemp -d)" | |
trap "rm -rf $workdir" EXIT | |
cd "$workdir" | |
# Generate CA key and certificate | |
openssl ecparam -genkey -name prime256v1 -out ca.key | |
openssl req -x509 -new -batch -SHA256 -nodes -key ca.key -days 3650 -out ca.crt | |
# Generate server key | |
openssl ecparam -genkey -name prime256v1 -out server.key | |
# Sign the server key with the certificate | |
openssl req -new -batch -SHA256 -key server.key -nodes -out server.csr | |
openssl x509 -req -SHA256 -days 3650 -in server.csr -CA ca.crt -CAkey ca.key -CAcreateserial -outform DER -out server.der 2>/dev/null | |
# Print private key. Extract with openssl, strip initial '00' if needed, and | |
# verify length. | |
privkey="$( \ | |
openssl ec -in server.key -noout -text 2>/dev/null | \ | |
sed -n '/priv:/,/pub:/p' | grep -o '[0-9a-f]\{2\}' | \ | |
tr -d ' \n' )" | |
privkey="${privkey#00}" | |
if ! echo "$privkey" | grep -q '^[0-9a-f]\{64\}$'; then | |
echo "Something went wrong with generating the private key." >&2 | |
echo "Got this: \"$privkey\"" >&2 | |
echo "Expected to see a 64-character hex string instead..." >&2 | |
exit 1 | |
fi | |
echo "Private Identity:" | |
echo "$privkey" | |
echo | |
# Print certificate | |
echo "U2F Certificate:" | |
xxd -p server.der | |
echo | |
# Give users some help | |
echo "Please enter the values above into the OnlyKey Configuration Wizard." | |
echo "Also, set up one of the 12 slots to use U2F." | |
echo | |
echo "To verify whether this worked, you can perform the following steps:" | |
echo | |
echo "- visit https://demo.yubico.com/u2f?tab=register" | |
echo "- find the \"Register a U2F device\" form" | |
echo "- enter an arbitrary username and password, and click \"Next\"" | |
echo "- touch your U2F slot" | |
echo "- the site should say \"Registration completed\"" | |
echo "- click on \"Technical data\"" | |
echo "- check that the attestation certificate shows something similar to:" | |
echo | |
openssl x509 -inform DER -in server.der -noout -text |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment