1 /* certreqgen.c - Generate a key and a certification request
2 * Copyright (C) 2002, 2003, 2005, 2007 Free Software Foundation, Inc.
4 * This file is part of GnuPG.
6 * GnuPG is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * GnuPG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
21 The format of the native parameter file is follows:
22 o Text only, line length is limited to about 1000 chars.
23 o You must use UTF-8 encoding to specify non-ascii characters.
24 o Empty lines are ignored.
25 o Leading and trailing spaces are ignored.
26 o A hash sign as the first non white space character is a comment line.
27 o Control statements are indicated by a leading percent sign, the
28 arguments are separated by white space from the keyword.
29 o Parameters are specified by a keyword, followed by a colon. Arguments
30 are separated by white space.
31 o The first parameter must be "Key-Type", control statements
32 may be placed anywhere.
33 o Key generation takes place when either the end of the parameter file
34 is reached, the next "Key-Type" parameter is encountered or at the
35 controlstatement "%commit"
40 Suppress actual key generation (useful for syntax checking).
42 Perform the key generation. Note that an implicit commit is done
43 at the next "Key-Type" parameter.
45 Do not write the certificate to the keyDB but to <filename>.
46 This must be given before the first
47 commit to take place, duplicate specification of the same filename
48 is ignored, the last filename before a commit is used.
49 The filename is used until a new filename is used (at commit points)
50 and all keys are written to that file. If a new filename is given,
51 this file is created (and overwrites an existing one).
52 Both control statements must be given.
53 o The order of the parameters does not matter except for "Key-Type"
54 which must be the first parameter. The parameters are only for the
55 generated keyblock and parameters from previous key generations are not
56 used. Some syntactically checks may be performed.
57 The currently defined parameters are:
59 Starts a new parameter block by giving the type of the
60 primary key. The algorithm must be capable of signing.
61 This is a required parameter. For now the only supported
63 Key-Length: <length-in-bits>
64 Length of the key in bits. Default is 1024.
66 This is optional and used to generate a request for an already
67 existing key. Key-Length will be ignored when given,
68 Key-Usage: <usage-list>
69 Space or comma delimited list of key usage, allowed values are
70 "encrypt" and "sign". This is used to generate the KeyUsage extension.
71 Please make sure that the algorithm is capable of this usage. Default
72 is to allow encrypt and sign.
74 This is the DN name of the subject in rfc2253 format.
76 The is an email address for the altSubjectName
78 The is an DNS name for the altSubjectName
80 The is an URI for the altSubjectName
84 %echo Generating a standard key
87 Name-DN: CN=test cert 1,OU=Aegypten Project,O=g10 Code GmbH,L=Düsseldorf,C=DE
88 Name-Email: joe@foo.bar
89 # Do a commit here, so that we can later print "done" :-)
125 struct para_data_s
*next
;
134 struct reqgen_ctrl_s
{
137 ksba_writer_t writer
;
141 static const char oidstr_keyUsage
[] = "2.5.29.15";
144 static int proc_parameters (ctrl_t ctrl
,
145 struct para_data_s
*para
,
146 struct reqgen_ctrl_s
*outctrl
);
147 static int create_request (ctrl_t ctrl
,
148 struct para_data_s
*para
,
149 const char *carddirect
,
150 ksba_const_sexp_t
public,
151 struct reqgen_ctrl_s
*outctrl
);
156 release_parameter_list (struct para_data_s
*r
)
158 struct para_data_s
*r2
;
167 static struct para_data_s
*
168 get_parameter (struct para_data_s
*para
, enum para_name key
, int seq
)
170 struct para_data_s
*r
;
172 for (r
= para
; r
; r
= r
->next
)
173 if ( r
->key
== key
&& !seq
--)
179 get_parameter_value (struct para_data_s
*para
, enum para_name key
, int seq
)
181 struct para_data_s
*r
= get_parameter (para
, key
, seq
);
182 return (r
&& *r
->u
.value
)? r
->u
.value
: NULL
;
186 get_parameter_algo (struct para_data_s
*para
, enum para_name key
)
188 struct para_data_s
*r
= get_parameter (para
, key
, 0);
191 if (digitp (r
->u
.value
))
192 return atoi( r
->u
.value
);
193 return gcry_pk_map_name (r
->u
.value
);
196 /* Parse the usage parameter. Returns 0 on success. Note that we
197 only care about sign and encrypt and don't (yet) allow all the
198 other X.509 usage to be specified; instead we will use a fixed
199 mapping to the X.509 usage flags. */
201 parse_parameter_usage (struct para_data_s
*para
, enum para_name key
)
203 struct para_data_s
*r
= get_parameter (para
, key
, 0);
208 return 0; /* none (this is an optional parameter)*/
212 while ( (p
= strsep (&pn
, " \t,")) )
216 else if ( !ascii_strcasecmp (p
, "sign") )
217 use
|= GCRY_PK_USAGE_SIGN
;
218 else if ( !ascii_strcasecmp (p
, "encrypt") )
219 use
|= GCRY_PK_USAGE_ENCR
;
222 log_error ("line %d: invalid usage list\n", r
->lnr
);
223 return -1; /* error */
232 get_parameter_uint (struct para_data_s
*para
, enum para_name key
)
234 struct para_data_s
*r
= get_parameter (para
, key
, 0);
239 if (r
->key
== pKEYUSAGE
)
242 return (unsigned int)strtoul (r
->u
.value
, NULL
, 10);
247 /* Read the certificate generation parameters from FP and generate
248 (all) certificate requests. */
250 read_parameters (ctrl_t ctrl
, estream_t fp
, ksba_writer_t writer
)
257 { "Key-Type", pKEYTYPE
},
258 { "Key-Length", pKEYLENGTH
},
259 { "Key-Grip", pKEYGRIP
},
260 { "Key-Usage", pKEYUSAGE
},
261 { "Name-DN", pNAMEDN
},
262 { "Name-Email", pNAMEEMAIL
, 1 },
263 { "Name-DNS", pNAMEDNS
, 1 },
264 { "Name-URI", pNAMEURI
, 1 },
268 const char *err
= NULL
;
269 struct para_data_s
*para
, *r
;
270 int i
, rc
= 0, any
= 0;
271 struct reqgen_ctrl_s outctrl
;
273 memset (&outctrl
, 0, sizeof (outctrl
));
274 outctrl
.writer
= writer
;
278 while (es_fgets (line
, DIM(line
)-1, fp
) )
280 char *keyword
, *value
;
283 if (*line
&& line
[strlen(line
)-1] != '\n')
285 err
= "line too long";
288 for (p
=line
; spacep (p
); p
++)
290 if (!*p
|| *p
== '#')
296 for (; *p
&& !spacep (p
); p
++)
300 for (; spacep (p
); p
++)
303 trim_trailing_spaces (value
);
305 if (!ascii_strcasecmp (keyword
, "%echo"))
306 log_info ("%s\n", value
);
307 else if (!ascii_strcasecmp (keyword
, "%dry-run"))
309 else if (!ascii_strcasecmp( keyword
, "%commit"))
311 rc
= proc_parameters (ctrl
, para
, &outctrl
);
315 release_parameter_list (para
);
319 log_info ("skipping control `%s' (%s)\n", keyword
, value
);
325 if (!(p
= strchr (p
, ':')) || p
== keyword
)
327 err
= "missing colon";
332 for (; spacep (p
); p
++)
336 err
= "missing argument";
340 trim_trailing_spaces (value
);
342 for (i
=0; (keywords
[i
].name
343 && ascii_strcasecmp (keywords
[i
].name
, keyword
)); i
++)
345 if (!keywords
[i
].name
)
347 err
= "unknown keyword";
350 if (keywords
[i
].key
!= pKEYTYPE
&& !para
)
352 err
= "parameter block does not start with \"Key-Type\"";
356 if (keywords
[i
].key
== pKEYTYPE
&& para
)
358 rc
= proc_parameters (ctrl
, para
, &outctrl
);
362 release_parameter_list (para
);
365 else if (!keywords
[i
].allow_dups
)
367 for (r
= para
; r
&& r
->key
!= keywords
[i
].key
; r
= r
->next
)
371 err
= "duplicate keyword";
376 r
= xtrycalloc (1, sizeof *r
+ strlen( value
));
382 r
->lnr
= outctrl
.lnr
;
383 r
->key
= keywords
[i
].key
;
384 strcpy (r
->u
.value
, value
);
391 log_error ("line %d: %s\n", outctrl
.lnr
, err
);
392 rc
= gpg_error (GPG_ERR_GENERAL
);
394 else if (es_ferror(fp
))
396 log_error ("line %d: read error: %s\n", outctrl
.lnr
, strerror(errno
) );
397 rc
= gpg_error (GPG_ERR_GENERAL
);
401 rc
= proc_parameters (ctrl
, para
, &outctrl
);
408 rc
= gpg_error (GPG_ERR_NO_DATA
);
411 release_parameter_list (para
);
415 /* check whether there are invalid characters in the email address S */
417 has_invalid_email_chars (const char *s
)
420 static char valid_chars
[] = "01234567890_-."
421 "abcdefghijklmnopqrstuvwxyz"
422 "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
429 else if (!at_seen
&& !( !!strchr (valid_chars
, *s
) || *s
== '+'))
431 else if (at_seen
&& !strchr (valid_chars
, *s
))
438 /* Check that all required parameters are given and perform the action */
440 proc_parameters (ctrl_t ctrl
,
441 struct para_data_s
*para
, struct reqgen_ctrl_s
*outctrl
)
444 struct para_data_s
*r
;
449 unsigned char keyparms
[100];
453 size_t erroff
, errlen
;
454 char *cardkeyid
= NULL
;
456 /* Check that we have all required parameters; */
457 assert (get_parameter (para
, pKEYTYPE
, 0));
459 /* We can only use RSA for now. There is a problem with pkcs-10 on
460 how to use ElGamal because it is expected that a PK algorithm can
461 always be used for signing. Another problem is that on-card
462 generated encryption keys may not be used for signing. */
463 i
= get_parameter_algo (para
, pKEYTYPE
);
464 if (!i
&& (s
= get_parameter_value (para
, pKEYTYPE
, 0)) && *s
)
466 /* Hack to allow creation of certificates directly from a smart
467 card. For example: "Key-Type: card:OPENPGP.3". */
468 if (!strncmp (s
, "card:", 5) && s
[5])
469 cardkeyid
= xtrystrdup (s
+5);
471 if ( (i
< 1 || i
!= GCRY_PK_RSA
) && !cardkeyid
)
473 r
= get_parameter (para
, pKEYTYPE
, 0);
474 log_error (_("line %d: invalid algorithm\n"), r
->lnr
);
475 return gpg_error (GPG_ERR_INV_PARAMETER
);
478 /* Check the keylength. */
479 if (!get_parameter (para
, pKEYLENGTH
, 0))
482 nbits
= get_parameter_uint (para
, pKEYLENGTH
);
483 if ((nbits
< 1024 || nbits
> 4096) && !cardkeyid
)
485 /* The BSI specs dated 2002-11-25 don't allow lengths below 1024. */
486 r
= get_parameter (para
, pKEYLENGTH
, 0);
487 log_error (_("line %d: invalid key length %u (valid are %d to %d)\n"),
488 r
->lnr
, nbits
, 1024, 4096);
490 return gpg_error (GPG_ERR_INV_PARAMETER
);
493 /* Check the usage. */
494 if (parse_parameter_usage (para
, pKEYUSAGE
))
497 return gpg_error (GPG_ERR_INV_PARAMETER
);
500 /* Check that there is a subject name and that this DN fits our
502 if (!(s
=get_parameter_value (para
, pNAMEDN
, 0)))
504 r
= get_parameter (para
, pNAMEDN
, 0);
505 log_error (_("line %d: no subject name given\n"), r
->lnr
);
507 return gpg_error (GPG_ERR_INV_PARAMETER
);
509 err
= ksba_dn_teststr (s
, 0, &erroff
, &errlen
);
512 r
= get_parameter (para
, pNAMEDN
, 0);
513 if (gpg_err_code (err
) == GPG_ERR_UNKNOWN_NAME
)
514 log_error (_("line %d: invalid subject name label `%.*s'\n"),
515 r
->lnr
, (int)errlen
, s
+erroff
);
517 log_error (_("line %d: invalid subject name `%s' at pos %d\n"),
518 r
->lnr
, s
, (int)erroff
);
521 return gpg_error (GPG_ERR_INV_PARAMETER
);
524 /* Check that the optional email address is okay. */
525 for (seq
=0; (s
=get_parameter_value (para
, pNAMEEMAIL
, seq
)); seq
++)
527 if (has_invalid_email_chars (s
)
529 || s
[strlen(s
)-1] == '@'
530 || s
[strlen(s
)-1] == '.'
533 r
= get_parameter (para
, pNAMEEMAIL
, seq
);
534 log_error (_("line %d: not a valid email address\n"), r
->lnr
);
536 return gpg_error (GPG_ERR_INV_PARAMETER
);
540 if (cardkeyid
) /* Take the key from the current smart card. */
542 rc
= gpgsm_agent_readkey (ctrl
, 1, cardkeyid
, &public);
545 r
= get_parameter (para
, pKEYTYPE
, 0);
546 log_error (_("line %d: error reading key `%s' from card: %s\n"),
547 r
->lnr
, cardkeyid
, gpg_strerror (rc
));
552 else if ((s
=get_parameter_value (para
, pKEYGRIP
, 0))) /* Use existing key.*/
554 rc
= gpgsm_agent_readkey (ctrl
, 0, s
, &public);
557 r
= get_parameter (para
, pKEYTYPE
, 0);
558 log_error (_("line %d: error getting key by keygrip `%s': %s\n"),
559 r
->lnr
, s
, gpg_strerror (rc
));
564 else /* Generate new key. */
566 sprintf (numbuf
, "%u", nbits
);
567 snprintf ((char*)keyparms
, DIM (keyparms
)-1,
568 "(6:genkey(3:rsa(5:nbits%d:%s)))",
569 (int)strlen (numbuf
), numbuf
);
570 rc
= gpgsm_agent_genkey (ctrl
, keyparms
, &public);
573 r
= get_parameter (para
, pKEYTYPE
, 0);
574 log_error (_("line %d: key generation failed: %s <%s>\n"),
575 r
->lnr
, gpg_strerror (rc
), gpg_strsource (rc
));
581 rc
= create_request (ctrl
, para
, cardkeyid
, public, outctrl
);
589 /* Parameters are checked, the key pair has been created. Now
590 generate the request and write it out */
592 create_request (ctrl_t ctrl
,
593 struct para_data_s
*para
,
594 const char *carddirect
,
595 ksba_const_sexp_t
public,
596 struct reqgen_ctrl_s
*outctrl
)
601 ksba_stop_reason_t stopreason
;
610 err
= ksba_certreq_new (&cr
);
614 rc
= gcry_md_open (&md
, GCRY_MD_SHA1
, 0);
617 log_error ("md_open failed: %s\n", gpg_strerror (rc
));
621 gcry_md_start_debug (md
, "cr.cri");
623 ksba_certreq_set_hash_function (cr
, HASH_FNC
, md
);
624 ksba_certreq_set_writer (cr
, outctrl
->writer
);
626 err
= ksba_certreq_add_subject (cr
, get_parameter_value (para
, pNAMEDN
, 0));
629 log_error ("error setting the subject's name: %s\n",
635 for (seq
=0; (s
= get_parameter_value (para
, pNAMEEMAIL
, seq
)); seq
++)
637 buf
= xtrymalloc (strlen (s
) + 3);
646 err
= ksba_certreq_add_subject (cr
, buf
);
650 log_error ("error setting the subject's alternate name: %s\n",
657 for (seq
=0; (s
= get_parameter_value (para
, pNAMEDNS
, seq
)); seq
++)
661 snprintf (numbuf
, DIM(numbuf
), "%u:", (unsigned int)len
);
662 buf
= p
= xtrymalloc (11 + strlen (numbuf
) + len
+ 3);
668 p
= stpcpy (p
, "(8:dns-name");
669 p
= stpcpy (p
, numbuf
);
673 err
= ksba_certreq_add_subject (cr
, buf
);
677 log_error ("error setting the subject's alternate name: %s\n",
684 for (seq
=0; (s
= get_parameter_value (para
, pNAMEURI
, seq
)); seq
++)
688 snprintf (numbuf
, DIM(numbuf
), "%u:", (unsigned int)len
);
689 buf
= p
= xtrymalloc (6 + strlen (numbuf
) + len
+ 3);
695 p
= stpcpy (p
, "(3:uri");
696 p
= stpcpy (p
, numbuf
);
700 err
= ksba_certreq_add_subject (cr
, buf
);
704 log_error ("error setting the subject's alternate name: %s\n",
712 err
= ksba_certreq_set_public_key (cr
, public);
715 log_error ("error setting the public key: %s\n",
722 use
= get_parameter_uint (para
, pKEYUSAGE
);
723 if (use
== GCRY_PK_USAGE_SIGN
)
725 /* For signing only we encode the bits:
726 KSBA_KEYUSAGE_DIGITAL_SIGNATURE
727 KSBA_KEYUSAGE_NON_REPUDIATION */
728 err
= ksba_certreq_add_extension (cr
, oidstr_keyUsage
, 1,
729 "\x03\x02\x06\xC0", 4);
731 else if (use
== GCRY_PK_USAGE_ENCR
)
733 /* For encrypt only we encode the bits:
734 KSBA_KEYUSAGE_KEY_ENCIPHERMENT
735 KSBA_KEYUSAGE_DATA_ENCIPHERMENT */
736 err
= ksba_certreq_add_extension (cr
, oidstr_keyUsage
, 1,
737 "\x03\x02\x04\x30", 4);
740 err
= 0; /* Both or none given: don't request one. */
743 log_error ("error setting the key usage: %s\n",
752 err
= ksba_certreq_build (cr
, &stopreason
);
755 log_error ("ksba_certreq_build failed: %s\n", gpg_strerror (err
));
759 if (stopreason
== KSBA_SR_NEED_SIG
)
763 unsigned char grip
[20];
765 unsigned char *sigval
;
768 n
= gcry_sexp_canon_len (public, 0, NULL
, NULL
);
771 log_error ("libksba did not return a proper S-Exp\n");
772 rc
= gpg_error (GPG_ERR_BUG
);
775 rc
= gcry_sexp_sscan (&s_pkey
, NULL
, (const char*)public, n
);
778 log_error ("gcry_sexp_scan failed: %s\n", gpg_strerror (rc
));
781 if ( !gcry_pk_get_keygrip (s_pkey
, grip
) )
783 rc
= gpg_error (GPG_ERR_GENERAL
);
784 log_error ("can't figure out the keygrip\n");
785 gcry_sexp_release (s_pkey
);
788 gcry_sexp_release (s_pkey
);
789 bin2hex (grip
, 20, hexgrip
);
791 log_info ("about to sign CSR for key: &%s\n", hexgrip
);
794 rc
= gpgsm_scd_pksign (ctrl
, carddirect
, NULL
,
795 gcry_md_read(md
, GCRY_MD_SHA1
),
796 gcry_md_get_algo_dlen (GCRY_MD_SHA1
),
804 orig_codeset
= i18n_switchto_utf8 ();
805 desc
= percent_plus_escape
806 (_("To complete this certificate request please enter"
807 " the passphrase for the key you just created once"
809 i18n_switchback (orig_codeset
);
810 rc
= gpgsm_agent_pksign (ctrl
, hexgrip
, desc
,
811 gcry_md_read(md
, GCRY_MD_SHA1
),
812 gcry_md_get_algo_dlen (GCRY_MD_SHA1
),
819 log_error ("signing failed: %s\n", gpg_strerror (rc
));
823 err
= ksba_certreq_set_sig_val (cr
, sigval
);
827 log_error ("failed to store the sig_val: %s\n",
834 while (stopreason
!= KSBA_SR_READY
);
839 ksba_certreq_release (cr
);
845 /* Create a new key by reading the parameters from IN_FP. Multiple
846 keys may be created */
848 gpgsm_genkey (ctrl_t ctrl
, estream_t in_stream
, FILE *out_fp
)
851 Base64Context b64writer
= NULL
;
852 ksba_writer_t writer
;
854 ctrl
->pem_name
= "CERTIFICATE REQUEST";
855 rc
= gpgsm_create_writer (&b64writer
, ctrl
, out_fp
, NULL
, &writer
);
858 log_error ("can't create writer: %s\n", gpg_strerror (rc
));
862 rc
= read_parameters (ctrl
, in_stream
, writer
);
865 log_error ("error creating certificate request: %s <%s>\n",
866 gpg_strerror (rc
), gpg_strsource (rc
));
870 rc
= gpgsm_finish_writer (b64writer
);
873 log_error ("write failed: %s\n", gpg_strerror (rc
));
877 gpgsm_status (ctrl
, STATUS_KEY_CREATED
, "P");
878 log_info ("certificate request created\n");
881 gpgsm_destroy_writer (b64writer
);