summaryrefslogtreecommitdiffstats
path: root/contrib/pkcs11-keygen/writekey.sh
blob: 9b235d090a4996a907fac3b24b171959520c0850 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/bin/bash --debug

usage="Usage: $0 -x ext -p pin -f keyrootname"
tmp_file=/tmp/cur_pem.$$
while getopts ":x:p:f:" opt; do
  case $opt in
    x  ) ext=$OPTARG ;;
    p  ) pin=$OPTARG ;;
    f  ) root=$OPTARG ;;
    \? ) echo $usage 
	 exit 1 ;;
   esac
done
shift $(($OPTIND -1))

if [ ! "$ext" -o ! "$pin" -o ! "$root" ] ; then
  echo $usage
  exit 1
fi

keyfile=${root}.key
privfile=${root}.private
file=`basename $root | sed 's/^K//'`
zone=`echo $file | awk -F+ '{ print $1 }' | sed 's/\.$//'`
algo=`echo $file | awk -F+ '{ print $2 }'`
tag=`echo $file | awk -F+ '{ print $3 }'`

# debug
echo 'zone: ' $zone
echo 'algo: ' $algo
echo 'tag: ' $tag

if [ ! -r "$keyfile" ] ; then
  echo "can't read " $keyfile
  exit 1
fi
if [ ! -r "$privfile" ] ; then
  echo "can't read " $privfile
  exit 1
fi

if [ "$algo" != "005" ] ; then
  echo 'algorithm must be 005'
  exit 1
fi

# for testing
mypath=.

echo 'Reading key files'
flag=`$mypath/keydump.pl -k $keyfile -p $privfile -o $tmp_file`

if [ "$flag" = "256" ] ; then
  label=$zone,zsk,$ext
elif [ "$flag" = "257" ] ; then
  label=$zone,ksk,$ext
else
  echo 'flag must be 256 or 257'
  rm $tmp_file
  exit 1
fi

echo "Label will be '"$label"'"
$mypath/writekey -p $pin -l $label -i $tag -f $tmp_file

rm $tmp_file

echo 'Now you can add at the end of ' $privfile
/usr/bin/perl <<EOF
use MIME::Base64;
print "Engine: ", encode_base64("pkcs11\0",""), "\n";
print "Label: ", encode_base64("pkcs11:"."$label"."\0",""), "\n";
EOF