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 2 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, write to the Free Software
20 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
29 INPUT FD=$ASSUAN_FP_IN\n\
30 OUTPUT FD=$ASSUAN_FP_OUT --armor\n\
53 for choice
in "$@"; do
55 echo " [$i] $choice" >&2
60 echo -n "Your selection: " >&2
63 while [ $j -lt $i -o $j -eq $i ]; do
64 if [ "$idx" = $j ]; then
69 if [ $j -lt $i -o $j -eq $i ]; then
75 for choice
in "$@"; do
77 if [ $i -eq $idx ]; then
84 echo "You selected: $ANSWER" >&2
90 while [ -z "$KEY_TYPE" ]; do
91 query_user_menu
"Key type" "RSA" "Existing key" "Direct from card"
95 query_user_menu
"Key length" "1024" "2048"
100 # User requested to use an existing key; need to set some dummy defaults
101 query_user
"Keygrip "
102 if [ -n "$ANSWER" ]; then
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
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"
134 query_user_menu
"Key usage" "sign, encrypt" "sign" "encrypt"
137 query_user
"Name (DN)"
143 query_user
"E-Mail addresses (end with an empty line)"
144 [ -z "$ANSWER" ] && break
145 EMAIL_ADDRESSES
="${EMAIL_ADDRESSES}${LF}Name-Email: $ANSWER"
153 query_user
"DNS Names (optional; end with an empty line)"
154 [ -z "$ANSWER" ] && break
155 DNS_ADDRESSES
="${DNS_ADDRESSES}${LF}Name-DNS: $ANSWER"
163 query_user
"URIs (optional; end with an empty line)"
164 [ -z "$ANSWER" ] && break
165 URI_ADDRESSES
="${URI_ADDRESSES}${LF}Name-URI: $ANSWER"
170 file_parameter
=$
(mktemp
"/tmp/gpgsm.XXXXXX")
171 outfile
=$
(mktemp
"/tmp/gpgsm.XXXXXX")
177 Key-Length: $KEY_LENGTH
178 Key-Usage: $KEY_USAGE
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
192 query_user_menu
"Really create such a CSR?" "yes" "no"
193 [ "$ANSWER" != "yes" ] && exit 1
196 echo -e "$ASSUAN_COMMANDS" | \
197 gpgsm
--no-log-file --debug-level none
--debug-none \
198 --server 4< "$file_parameter" 5>"$outfile" >/dev
/null
202 rm "$file_parameter" "$outfile"