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/>.
27 INPUT FD=$ASSUAN_FP_IN\n\
28 OUTPUT FD=$ASSUAN_FP_OUT --armor\n\
51 for choice
in "$@"; do
53 echo " [$i] $choice" >&2
58 echo -n "Your selection: " >&2
61 while [ $j -lt $i -o $j -eq $i ]; do
62 if [ "$idx" = $j ]; then
67 if [ $j -lt $i -o $j -eq $i ]; then
73 for choice
in "$@"; do
75 if [ $i -eq $idx ]; then
82 echo "You selected: $ANSWER" >&2
88 while [ -z "$KEY_TYPE" ]; do
89 query_user_menu
"Key type" "RSA" "Existing key" "Direct from card"
93 query_user_menu
"Key length" "1024" "2048"
98 # User requested to use an existing key; need to set some dummy defaults
100 if [ -n "$ANSWER" ]; then
107 tmp
=$
(echo 'SCD SERIALNO' | gpg-connect-agent | \
108 awk '$2 == "SERIALNO" {print $3}')
109 if [ -z "$tmp" ]; then
110 echo "No card found" >&2
112 echo "Card with S/N $tmp found" >&2
113 tmp
=$
(echo 'SCD LEARN --force' | gpg-connect-agent | \
114 awk '$2 == "KEYPAIRINFO" {printf " %s", $4}')
115 sshid
=$
(echo 'SCD GETATTR $AUTHKEYID' | gpg-connect-agent | \
116 awk '$2 == "$AUTHKEYID" {print $3}')
117 [ -n "$sshid" ] && echo "gpg-agent uses $sshid as ssh key" >&2
118 query_user_menu
"Select key " $tmp "back"
119 if [ "$ANSWER" != "back" ]; then
120 KEY_TYPE
="card:$ANSWER"
132 query_user_menu
"Key usage" "sign, encrypt" "sign" "encrypt"
135 query_user
"Name (DN)"
141 query_user
"E-Mail addresses (end with an empty line)"
142 [ -z "$ANSWER" ] && break
143 EMAIL_ADDRESSES
="${EMAIL_ADDRESSES}${LF}Name-Email: $ANSWER"
151 query_user
"DNS Names (optional; end with an empty line)"
152 [ -z "$ANSWER" ] && break
153 DNS_ADDRESSES
="${DNS_ADDRESSES}${LF}Name-DNS: $ANSWER"
161 query_user
"URIs (optional; end with an empty line)"
162 [ -z "$ANSWER" ] && break
163 URI_ADDRESSES
="${URI_ADDRESSES}${LF}Name-URI: $ANSWER"
168 file_parameter
=$
(mktemp
"/tmp/gpgsm.XXXXXX")
169 outfile
=$
(mktemp
"/tmp/gpgsm.XXXXXX")
175 Key-Length: $KEY_LENGTH
176 Key-Usage: $KEY_USAGE
179 [ -n "$KEY_GRIP" ] && echo "Key-Grip: $KEY_GRIP"
180 [ -n "$EMAIL_ADDRESSES" ] && echo "$EMAIL_ADDRESSES"
181 [ -n "$DNS_ADDRESSES" ] && echo "$DNS_ADDRESSES"
182 [ -n "$URI_ADDRESSES" ] && echo "$URI_ADDRESSES"
183 ) > "$file_parameter"
186 echo 'Parameters for certificate request to create:' >&2
187 cat -n "$file_parameter" >&2
190 query_user_menu
"Really create such a CSR?" "yes" "no"
191 [ "$ANSWER" != "yes" ] && exit 1
194 printf "$ASSUAN_COMMANDS" | \
195 gpgsm
--no-log-file --debug-level none
--debug-none \
196 --server 4< "$file_parameter" 5>"$outfile" >/dev
/null
200 rm "$file_parameter" "$outfile"