Update estream.
[gnupg.git] / tools / gpgsm-gencert.sh
blobb209c8e5a96b34171820a49390fb29d32c960423
1 #!/bin/sh
2 # -*- sh -*-
3 # gpgsm-gencert.c - Generate X.509 certificates through GPGSM.
4 # Copyright (C) 2004, 2005 Free Software Foundation, Inc.
6 # This file is part of GnuPG.
8 # GnuPG is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
13 # GnuPG is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, see <http://www.gnu.org/licenses/>.
21 set -e
23 ASSUAN_FP_IN=4
24 ASSUAN_FP_OUT=5
26 ASSUAN_COMMANDS="\
27 INPUT FD=$ASSUAN_FP_IN\n\
28 OUTPUT FD=$ASSUAN_FP_OUT --armor\n\
29 GENKEY\n\
30 BYE\n"
32 ANSWER=""
34 query_user()
36 message=$1; shift
38 echo "$message" >&2
39 echo -n "> " >&2
40 read answer
42 ANSWER=$answer;
45 query_user_menu()
47 message=$1; shift
48 i=0
50 echo "$message" >&2
51 for choice in "$@"; do
52 i=$(expr $i + 1)
53 echo " [$i] $choice" >&2
54 done
56 while true; do
57 j=1
58 echo -n "Your selection: " >&2
59 read idx
61 while [ $j -lt $i -o $j -eq $i ]; do
62 if [ "$idx" = $j ]; then
63 break
65 j=$(expr $j + 1)
66 done
67 if [ $j -lt $i -o $j -eq $i ]; then
68 break
70 done
72 i=0
73 for choice in "$@"; do
74 i=$(expr $i + 1)
75 if [ $i -eq $idx ]; then
76 ANSWER=$1
77 break;
79 shift
80 done
82 echo "You selected: $ANSWER" >&2
86 echo "WARNING: This script is deprecated; please use" >&2
87 echo " gpgsm --gen-key" >&2
88 echo " instead." >&2
89 KEY_TYPE=""
90 while [ -z "$KEY_TYPE" ]; do
91 query_user_menu "Key type" "RSA" "Existing key" "Direct from card"
92 case "$ANSWER" in
93 RSA)
94 KEY_TYPE=$ANSWER
95 query_user_menu "Key length" "1024" "2048"
96 KEY_LENGTH=$ANSWER
97 KEY_GRIP=
99 Existing*)
100 # User requested to use an existing key; need to set some dummy defaults
101 query_user "Keygrip "
102 if [ -n "$ANSWER" ]; then
103 KEY_TYPE=RSA
104 KEY_LENGTH=1024
105 KEY_GRIP=$ANSWER
108 Direct*)
109 tmp=$(echo 'SCD SERIALNO' | gpg-connect-agent | \
110 awk '$2 == "SERIALNO" {print $3}')
111 if [ -z "$tmp" ]; then
112 echo "No card found" >&2
113 else
114 echo "Card with S/N $tmp found" >&2
115 tmp=$(echo 'SCD LEARN --force' | gpg-connect-agent | \
116 awk '$2 == "KEYPAIRINFO" {printf " %s", $4}')
117 sshid=$(echo 'SCD GETATTR $AUTHKEYID' | gpg-connect-agent | \
118 awk '$2 == "$AUTHKEYID" {print $3}')
119 [ -n "$sshid" ] && echo "gpg-agent uses $sshid as ssh key" >&2
120 query_user_menu "Select key " $tmp "back"
121 if [ "$ANSWER" != "back" ]; then
122 KEY_TYPE="card:$ANSWER"
123 KEY_LENGTH=
124 KEY_GRIP=
129 exit 1
131 esac
132 done
134 query_user_menu "Key usage" "sign, encrypt" "sign" "encrypt"
135 KEY_USAGE=$ANSWER
137 query_user "Name (DN)"
138 NAME=$ANSWER
140 EMAIL_ADDRESSES=
142 while : ; do
143 query_user "E-Mail addresses (end with an empty line)"
144 [ -z "$ANSWER" ] && break
145 EMAIL_ADDRESSES="${EMAIL_ADDRESSES}${LF}Name-Email: $ANSWER"
146 LF='
148 done
150 DNS_ADDRESSES=
152 while : ; do
153 query_user "DNS Names (optional; end with an empty line)"
154 [ -z "$ANSWER" ] && break
155 DNS_ADDRESSES="${DNS_ADDRESSES}${LF}Name-DNS: $ANSWER"
156 LF='
158 done
160 URI_ADDRESSES=
162 while : ; do
163 query_user "URIs (optional; end with an empty line)"
164 [ -z "$ANSWER" ] && break
165 URI_ADDRESSES="${URI_ADDRESSES}${LF}Name-URI: $ANSWER"
166 LF='
168 done
170 file_parameter=$(mktemp "/tmp/gpgsm.XXXXXX")
171 outfile=$(mktemp "/tmp/gpgsm.XXXXXX")
175 cat <<EOF
176 Key-Type: $KEY_TYPE
177 Key-Length: $KEY_LENGTH
178 Key-Usage: $KEY_USAGE
179 Name-DN: $NAME
181 [ -n "$KEY_GRIP" ] && echo "Key-Grip: $KEY_GRIP"
182 [ -n "$EMAIL_ADDRESSES" ] && echo "$EMAIL_ADDRESSES"
183 [ -n "$DNS_ADDRESSES" ] && echo "$DNS_ADDRESSES"
184 [ -n "$URI_ADDRESSES" ] && echo "$URI_ADDRESSES"
185 ) > "$file_parameter"
188 echo 'Parameters for certificate request to create:' >&2
189 cat -n "$file_parameter" >&2
190 echo >&2
192 query_user_menu "Really create such a CSR?" "yes" "no"
193 [ "$ANSWER" != "yes" ] && exit 1
196 printf "$ASSUAN_COMMANDS" | \
197 gpgsm --no-log-file --debug-level none --debug-none \
198 --server 4< "$file_parameter" 5>"$outfile" >/dev/null
200 cat "$outfile"
202 rm "$file_parameter" "$outfile"
203 exit 0