2009-02-24 22:27:28 +01:00
|
|
|
#!/usr/bin/perl
|
|
|
|
|
|
|
|
# Written by Simon Josefsson <simon@josefsson.org>.
|
2012-01-23 20:47:28 +01:00
|
|
|
# Copyright (c) 2009-2012 Yubico AB
|
2009-02-24 22:27:28 +01:00
|
|
|
# All rights reserved.
|
|
|
|
#
|
|
|
|
# Redistribution and use in source and binary forms, with or without
|
|
|
|
# modification, are permitted provided that the following conditions are
|
|
|
|
# met:
|
|
|
|
#
|
|
|
|
# * Redistributions of source code must retain the above copyright
|
|
|
|
# notice, this list of conditions and the following disclaimer.
|
|
|
|
#
|
|
|
|
# * 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
|
|
|
|
# OWNER 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.
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
use POSIX qw(strftime);
|
2010-11-11 15:13:56 +01:00
|
|
|
use MIME::Base64;
|
|
|
|
|
|
|
|
my $device = "/dev/random";
|
2009-02-24 22:27:28 +01:00
|
|
|
|
|
|
|
sub usage {
|
2012-12-18 11:28:33 +01:00
|
|
|
print "Usage: ykksm-gen-keys [--verbose] [--help] [--urandom] [--progflags PROGFLAGS] [--pskc] START [END]\n";
|
2009-02-24 22:27:28 +01:00
|
|
|
print "\n";
|
2012-12-18 11:28:33 +01:00
|
|
|
print "Tool to generate keys on the YKKSM-KEYPROV format.\n";
|
2009-02-24 22:27:28 +01:00
|
|
|
print "\n";
|
2012-12-18 11:28:33 +01:00
|
|
|
print "START: Decimal start point.\n";
|
2009-02-24 22:27:28 +01:00
|
|
|
print "\n";
|
2012-12-18 11:28:33 +01:00
|
|
|
print "END: Decimal end point, if absent START is used as END.\n";
|
2009-02-24 22:27:28 +01:00
|
|
|
print "\n";
|
2009-02-27 16:06:18 +01:00
|
|
|
print " --urandom: Use /dev/urandom instead of /dev/random as entropy source.\n";
|
|
|
|
print "\n";
|
|
|
|
print " --progflags PROGFLAGS: Add a final personalization configuration string.\n";
|
|
|
|
print "\n";
|
2010-11-11 15:24:07 +01:00
|
|
|
print " --pskc: Output keys on the YubiKey PSKC format.\n";
|
|
|
|
print "\n";
|
2009-02-24 22:27:28 +01:00
|
|
|
print "Usage example:\n";
|
|
|
|
print "\n";
|
2012-12-18 11:28:33 +01:00
|
|
|
print " ./ykksm-gen-keys --urandom 1 10 |\n";
|
2009-02-25 14:12:51 +01:00
|
|
|
print " gpg -a --sign --encrypt -r 1D2F473E > keys.txt\n";
|
2009-02-24 22:27:28 +01:00
|
|
|
print "\n";
|
|
|
|
exit 1;
|
|
|
|
}
|
|
|
|
|
2010-11-11 15:13:56 +01:00
|
|
|
sub hex2modhex {
|
|
|
|
$_ = shift;
|
|
|
|
tr/0123456789abcdef/cbdefghijklnrtuv/;
|
|
|
|
return $_;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub getrand {
|
|
|
|
my $cnt = shift;
|
|
|
|
my $buf;
|
|
|
|
|
|
|
|
open (FH, $device) or die "Cannot open $device for reading";
|
|
|
|
read (FH, $buf, $cnt) or die "Cannot read from $device";
|
|
|
|
close FH;
|
|
|
|
|
|
|
|
return $buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub gethexrand {
|
|
|
|
my $cnt = shift;
|
|
|
|
my $buf = getrand ($cnt);
|
|
|
|
return lc(unpack("H*", $buf));
|
|
|
|
}
|
|
|
|
|
|
|
|
sub getb64rand {
|
|
|
|
my $cnt = shift;
|
|
|
|
my $buf = getrand ($cnt);
|
|
|
|
return encode_base64($buf, '');
|
|
|
|
}
|
|
|
|
|
|
|
|
# main
|
|
|
|
|
2009-02-24 22:27:28 +01:00
|
|
|
if ($#ARGV==-1) {
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
|
|
|
|
my $verbose = 0;
|
2010-11-11 15:24:07 +01:00
|
|
|
my $pskc = 0;
|
2009-02-27 16:06:18 +01:00
|
|
|
my $progflags;
|
2010-11-11 18:15:23 +01:00
|
|
|
my $start = "";
|
|
|
|
my $end = "";
|
|
|
|
while (defined($ARGV[0])) {
|
2009-02-24 22:27:28 +01:00
|
|
|
my $cmd = shift @ARGV;
|
|
|
|
if (($cmd eq "-v") || ($cmd eq "--verbose")) {
|
|
|
|
$verbose = 1;
|
|
|
|
} elsif (($cmd eq "-h") || ($cmd eq "--help")) {
|
|
|
|
usage();
|
|
|
|
} elsif ($cmd eq "--urandom") {
|
|
|
|
$device = "/dev/urandom";
|
2009-02-27 16:06:18 +01:00
|
|
|
} elsif ($cmd eq "--progflags") {
|
|
|
|
$progflags = "," . shift;
|
2010-11-11 15:24:07 +01:00
|
|
|
} elsif ($cmd eq "--pskc") {
|
|
|
|
$pskc = 1;
|
2010-11-11 15:13:56 +01:00
|
|
|
} elsif ($cmd =~ m/^[0-9]+/) {
|
2010-11-11 18:15:23 +01:00
|
|
|
if ($start eq "") {
|
2010-11-11 15:13:56 +01:00
|
|
|
$start = $cmd;
|
2010-11-11 18:15:23 +01:00
|
|
|
} elsif ($end eq "") {
|
2010-11-11 15:13:56 +01:00
|
|
|
$end = $cmd;
|
|
|
|
} else {
|
|
|
|
die "Invalid extra argument: $cmd";
|
|
|
|
}
|
2009-02-24 22:27:28 +01:00
|
|
|
}
|
|
|
|
}
|
2010-11-11 18:15:23 +01:00
|
|
|
die "Missing START parameter, try --help" if ($start eq "");
|
2010-11-11 15:13:56 +01:00
|
|
|
$end = $start if (!$end);
|
2009-02-24 22:27:28 +01:00
|
|
|
|
|
|
|
my $now = strftime "%Y-%m-%dT%H:%M:%S", localtime;
|
2010-11-11 15:13:56 +01:00
|
|
|
if ($pskc) {
|
|
|
|
$now .= 'Z';
|
|
|
|
}
|
2009-02-24 22:27:28 +01:00
|
|
|
my $ctr;
|
|
|
|
|
2010-11-11 15:13:56 +01:00
|
|
|
if ($pskc) {
|
|
|
|
print "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
|
|
|
|
print "<KeyContainer Version=\"1.0\"\n";
|
|
|
|
if ($start == $end) {
|
|
|
|
print " Id=\"yk-$start-pskc\"\n";
|
|
|
|
} else {
|
|
|
|
print " Id=\"yk-$start-to-$end-pskc\"\n";
|
|
|
|
}
|
|
|
|
print " xmlns=\"urn:ietf:params:xml:ns:keyprov:pskc\">\n";
|
|
|
|
} else {
|
|
|
|
print "# ykksm 1\n";
|
|
|
|
print "# start $start end $end device $device\n" if ($verbose);
|
|
|
|
print "# serialnr,identity,internaluid,aeskey,lockpw,created,accessed[,progflags]\n";
|
|
|
|
}
|
2009-02-24 22:27:28 +01:00
|
|
|
|
|
|
|
$ctr = $start;
|
|
|
|
while ($ctr <= $end) {
|
|
|
|
my $hexctr = sprintf "%012x", $ctr;
|
|
|
|
my $modhexctr = hex2modhex($hexctr);
|
|
|
|
my $internaluid = gethexrand(6);
|
2010-11-11 15:13:56 +01:00
|
|
|
my $aeskey = $pskc ? getb64rand(16) : gethexrand(16);
|
2009-02-24 22:27:28 +01:00
|
|
|
my $lockpw = gethexrand(6);
|
2010-11-11 15:13:56 +01:00
|
|
|
|
|
|
|
if ($pskc) {
|
|
|
|
print " <KeyPackage>\n";
|
|
|
|
print " <DeviceInfo>\n";
|
|
|
|
print " <Manufacturer>oath.UB</Manufacturer>\n";
|
|
|
|
print " <SerialNo>$ctr</SerialNo>\n";
|
|
|
|
print " <StartDate>$now</StartDate>\n";
|
|
|
|
print " </DeviceInfo>\n";
|
|
|
|
print " <CryptoModuleInfo>\n";
|
|
|
|
print " <Id>1</Id>\n";
|
|
|
|
print " </CryptoModuleInfo>\n";
|
|
|
|
print " <Key Id=\"yk-key-$ctr-slot-1\"\n";
|
|
|
|
print " Algorithm=\"http://www.yubico.com/#yubikey-aes\">\n";
|
|
|
|
print " <Issuer>Yubico</Issuer>\n";
|
|
|
|
print " <AlgorithmParameters>\n";
|
|
|
|
print " <ResponseFormat Encoding=\"ALPHANUMERIC\" Length=\"44\"/>\n";
|
|
|
|
print " </AlgorithmParameters>\n";
|
|
|
|
print " <Data>\n";
|
|
|
|
print " <Secret>\n";
|
|
|
|
print " <PlainValue>\n";
|
|
|
|
print " $aeskey\n";
|
|
|
|
print " </PlainValue>\n";
|
|
|
|
print " </Secret>\n";
|
|
|
|
print " </Data>\n";
|
|
|
|
print " <UserId>CN=$modhexctr, UID=$internaluid</UserId>\n";
|
|
|
|
print " </Key>\n";
|
|
|
|
print " </KeyPackage>\n";
|
|
|
|
} else {
|
|
|
|
print "# hexctr $hexctr modhexctr $modhexctr\n" if ($verbose);
|
|
|
|
printf "$ctr,$modhexctr,$internaluid,$aeskey,$lockpw,$now,$progflags\n";
|
|
|
|
}
|
|
|
|
|
2009-02-24 22:27:28 +01:00
|
|
|
$ctr++;
|
|
|
|
}
|
|
|
|
|
2010-11-11 15:13:56 +01:00
|
|
|
if ($pskc) {
|
|
|
|
print "</KeyContainer>\n";
|
|
|
|
} else {
|
|
|
|
print "# the end\n";
|
|
|
|
}
|
|
|
|
|
2009-02-24 22:27:28 +01:00
|
|
|
exit 0;
|