Better reset the PIN verification stati after changing the key attributes.
[gnupg.git] / sm / sign.c
blob0569052ed19c04a2492bcd06b5bea1c94d34abe7
1 /* sign.c - Sign a message
2 * Copyright (C) 2001, 2002, 2003, 2008 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/>.
20 #include <config.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <errno.h>
25 #include <unistd.h>
26 #include <time.h>
27 #include <assert.h>
29 #include "gpgsm.h"
30 #include <gcrypt.h>
31 #include <ksba.h>
33 #include "keydb.h"
34 #include "i18n.h"
37 static void
38 hash_data (int fd, gcry_md_hd_t md)
40 FILE *fp;
41 char buffer[4096];
42 int nread;
44 fp = fdopen ( dup (fd), "rb");
45 if (!fp)
47 log_error ("fdopen(%d) failed: %s\n", fd, strerror (errno));
48 return;
51 do
53 nread = fread (buffer, 1, DIM(buffer), fp);
54 gcry_md_write (md, buffer, nread);
56 while (nread);
57 if (ferror (fp))
58 log_error ("read error on fd %d: %s\n", fd, strerror (errno));
59 fclose (fp);
62 static int
63 hash_and_copy_data (int fd, gcry_md_hd_t md, ksba_writer_t writer)
65 gpg_error_t err;
66 FILE *fp;
67 char buffer[4096];
68 int nread;
69 int rc = 0;
70 int any = 0;
72 fp = fdopen ( dup (fd), "rb");
73 if (!fp)
75 gpg_error_t tmperr = gpg_error (gpg_err_code_from_errno (errno));
76 log_error ("fdopen(%d) failed: %s\n", fd, strerror (errno));
77 return tmperr;
80 do
82 nread = fread (buffer, 1, DIM(buffer), fp);
83 if (nread)
85 any = 1;
86 gcry_md_write (md, buffer, nread);
87 err = ksba_writer_write_octet_string (writer, buffer, nread, 0);
88 if (err)
90 log_error ("write failed: %s\n", gpg_strerror (err));
91 rc = err;
95 while (nread && !rc);
96 if (ferror (fp))
98 rc = gpg_error (gpg_err_code_from_errno (errno));
99 log_error ("read error on fd %d: %s\n", fd, strerror (errno));
101 fclose (fp);
102 if (!any)
104 /* We can't allow to sign an empty message because it does not
105 make much sense and more seriously, ksba-cms_build has
106 already written the tag for data and now expects an octet
107 string but an octet string of zeize 0 is illegal. */
108 log_error ("cannot sign an empty message\n");
109 rc = gpg_error (GPG_ERR_NO_DATA);
111 if (!rc)
113 err = ksba_writer_write_octet_string (writer, NULL, 0, 1);
114 if (err)
116 log_error ("write failed: %s\n", gpg_strerror (err));
117 rc = err;
121 return rc;
125 /* Get the default certificate which is defined as the first
126 certificate capable of signing returned by the keyDB and has a
127 secret key available. */
129 gpgsm_get_default_cert (ctrl_t ctrl, ksba_cert_t *r_cert)
131 KEYDB_HANDLE hd;
132 ksba_cert_t cert = NULL;
133 int rc;
134 char *p;
136 hd = keydb_new (0);
137 if (!hd)
138 return gpg_error (GPG_ERR_GENERAL);
139 rc = keydb_search_first (hd);
140 if (rc)
142 keydb_release (hd);
143 return rc;
148 rc = keydb_get_cert (hd, &cert);
149 if (rc)
151 log_error ("keydb_get_cert failed: %s\n", gpg_strerror (rc));
152 keydb_release (hd);
153 return rc;
156 if (!gpgsm_cert_use_sign_p (cert))
158 p = gpgsm_get_keygrip_hexstring (cert);
159 if (p)
161 if (!gpgsm_agent_havekey (ctrl, p))
163 xfree (p);
164 keydb_release (hd);
165 *r_cert = cert;
166 return 0; /* got it */
168 xfree (p);
172 ksba_cert_release (cert);
173 cert = NULL;
175 while (!(rc = keydb_search_next (hd)));
176 if (rc && rc != -1)
177 log_error ("keydb_search_next failed: %s\n", gpg_strerror (rc));
179 ksba_cert_release (cert);
180 keydb_release (hd);
181 return rc;
185 static ksba_cert_t
186 get_default_signer (ctrl_t ctrl)
188 KEYDB_SEARCH_DESC desc;
189 ksba_cert_t cert = NULL;
190 KEYDB_HANDLE kh = NULL;
191 int rc;
193 if (!opt.local_user)
195 rc = gpgsm_get_default_cert (ctrl, &cert);
196 if (rc)
198 if (rc != -1)
199 log_debug ("failed to find default certificate: %s\n",
200 gpg_strerror (rc));
201 return NULL;
203 return cert;
206 rc = keydb_classify_name (opt.local_user, &desc);
207 if (rc)
209 log_error ("failed to find default signer: %s\n", gpg_strerror (rc));
210 return NULL;
213 kh = keydb_new (0);
214 if (!kh)
215 return NULL;
217 rc = keydb_search (kh, &desc, 1);
218 if (rc)
220 log_debug ("failed to find default certificate: rc=%d\n", rc);
222 else
224 rc = keydb_get_cert (kh, &cert);
225 if (rc)
227 log_debug ("failed to get cert: rc=%d\n", rc);
231 keydb_release (kh);
232 return cert;
235 /* Depending on the options in CTRL add the certificate CERT as well as
236 other certificate up in the chain to the Root-CA to the CMS
237 object. */
238 static int
239 add_certificate_list (ctrl_t ctrl, ksba_cms_t cms, ksba_cert_t cert)
241 gpg_error_t err;
242 int rc = 0;
243 ksba_cert_t next = NULL;
244 int n;
245 int not_root = 0;
247 ksba_cert_ref (cert);
249 n = ctrl->include_certs;
250 log_debug ("adding certificates at level %d\n", n);
251 if (n == -2)
253 not_root = 1;
254 n = -1;
256 if (n < 0 || n > 50)
257 n = 50; /* We better apply an upper bound */
259 /* First add my own certificate unless we don't want any certificate
260 included at all. */
261 if (n)
263 if (not_root && gpgsm_is_root_cert (cert))
264 err = 0;
265 else
266 err = ksba_cms_add_cert (cms, cert);
267 if (err)
268 goto ksba_failure;
269 if (n>0)
270 n--;
272 /* Walk the chain to include all other certificates. Note that a -1
273 used for N makes sure that there is no limit and all certs get
274 included. */
275 while ( n-- && !(rc = gpgsm_walk_cert_chain (ctrl, cert, &next)) )
277 if (not_root && gpgsm_is_root_cert (next))
278 err = 0;
279 else
280 err = ksba_cms_add_cert (cms, next);
281 ksba_cert_release (cert);
282 cert = next; next = NULL;
283 if (err)
284 goto ksba_failure;
286 ksba_cert_release (cert);
288 return rc == -1? 0: rc;
290 ksba_failure:
291 ksba_cert_release (cert);
292 log_error ("ksba_cms_add_cert failed: %s\n", gpg_strerror (err));
293 return err;
299 /* Perform a sign operation.
301 Sign the data received on DATA-FD in embedded mode or in detached
302 mode when DETACHED is true. Write the signature to OUT_FP. The
303 keys used to sign are taken from SIGNERLIST or the default one will
304 be used if the value of this argument is NULL. */
306 gpgsm_sign (ctrl_t ctrl, certlist_t signerlist,
307 int data_fd, int detached, FILE *out_fp)
309 int i, rc;
310 gpg_error_t err;
311 Base64Context b64writer = NULL;
312 ksba_writer_t writer;
313 ksba_cms_t cms = NULL;
314 ksba_stop_reason_t stopreason;
315 KEYDB_HANDLE kh = NULL;
316 gcry_md_hd_t data_md = NULL;
317 int signer;
318 const char *algoid;
319 int algo;
320 ksba_isotime_t signed_at;
321 certlist_t cl;
322 int release_signerlist = 0;
324 kh = keydb_new (0);
325 if (!kh)
327 log_error (_("failed to allocated keyDB handle\n"));
328 rc = gpg_error (GPG_ERR_GENERAL);
329 goto leave;
332 ctrl->pem_name = "SIGNED MESSAGE";
333 rc = gpgsm_create_writer (&b64writer, ctrl, out_fp, NULL, &writer);
334 if (rc)
336 log_error ("can't create writer: %s\n", gpg_strerror (rc));
337 goto leave;
340 err = ksba_cms_new (&cms);
341 if (err)
343 rc = err;
344 goto leave;
347 err = ksba_cms_set_reader_writer (cms, NULL, writer);
348 if (err)
350 log_debug ("ksba_cms_set_reader_writer failed: %s\n",
351 gpg_strerror (err));
352 rc = err;
353 goto leave;
356 /* We are going to create signed data with data as encap. content */
357 err = ksba_cms_set_content_type (cms, 0, KSBA_CT_SIGNED_DATA);
358 if (!err)
359 err = ksba_cms_set_content_type (cms, 1, KSBA_CT_DATA);
360 if (err)
362 log_debug ("ksba_cms_set_content_type failed: %s\n",
363 gpg_strerror (err));
364 rc = err;
365 goto leave;
368 /* If no list of signers is given, use the default certificate. */
369 if (!signerlist)
371 ksba_cert_t cert = get_default_signer (ctrl);
372 if (!cert)
374 log_error ("no default signer found\n");
375 rc = gpg_error (GPG_ERR_GENERAL);
376 goto leave;
379 /* Although we don't check for ambigious specification we will
380 check that the signer's certificate is usable and valid. */
381 rc = gpgsm_cert_use_sign_p (cert);
382 if (!rc)
383 rc = gpgsm_validate_chain (ctrl, cert, "", NULL, 0, NULL, 0, NULL);
384 if (rc)
385 goto leave;
387 /* That one is fine - create signerlist. */
388 signerlist = xtrycalloc (1, sizeof *signerlist);
389 if (!signerlist)
391 rc = out_of_core ();
392 ksba_cert_release (cert);
393 goto leave;
395 signerlist->cert = cert;
396 release_signerlist = 1;
399 /* Figure out the hash algorithm to use. We do not want to use the
400 one for the certificate but if possible an OID for the plain
401 algorithm. */
402 if (opt.forced_digest_algo && opt.verbose)
403 log_info ("user requested hash algorithm %d\n", opt.forced_digest_algo);
404 for (i=0, cl=signerlist; cl; cl = cl->next, i++)
406 const char *oid;
408 if (opt.forced_digest_algo)
410 oid = NULL;
411 cl->hash_algo = opt.forced_digest_algo;
413 else
415 oid = ksba_cert_get_digest_algo (cl->cert);
416 cl->hash_algo = oid ? gcry_md_map_name (oid) : 0;
418 switch (cl->hash_algo)
420 case GCRY_MD_SHA1: oid = "1.3.14.3.2.26"; break;
421 case GCRY_MD_RMD160: oid = "1.3.36.3.2.1"; break;
422 case GCRY_MD_SHA224: oid = "2.16.840.1.101.3.4.2.4"; break;
423 case GCRY_MD_SHA256: oid = "2.16.840.1.101.3.4.2.1"; break;
424 case GCRY_MD_SHA384: oid = "2.16.840.1.101.3.4.2.2"; break;
425 case GCRY_MD_SHA512: oid = "2.16.840.1.101.3.4.2.3"; break;
426 /* case GCRY_MD_WHIRLPOOL: oid = "No OID yet"; break; */
428 case GCRY_MD_MD5: /* We don't want to use MD5. */
429 case 0: /* No algorithm found in cert. */
430 default: /* Other algorithms. */
431 log_info (_("hash algorithm %d (%s) for signer %d not supported;"
432 " using %s\n"),
433 cl->hash_algo, oid? oid: "?", i,
434 gcry_md_algo_name (GCRY_MD_SHA1));
435 cl->hash_algo = GCRY_MD_SHA1;
436 oid = "1.3.14.3.2.26";
437 break;
439 cl->hash_algo_oid = oid;
442 if (opt.verbose)
444 for (i=0, cl=signerlist; cl; cl = cl->next, i++)
445 log_info (_("hash algorithm used for signer %d: %s (%s)\n"),
446 i, gcry_md_algo_name (cl->hash_algo), cl->hash_algo_oid);
450 /* Gather certificates of signers and store them in the CMS object. */
451 for (cl=signerlist; cl; cl = cl->next)
453 rc = gpgsm_cert_use_sign_p (cl->cert);
454 if (rc)
455 goto leave;
457 err = ksba_cms_add_signer (cms, cl->cert);
458 if (err)
460 log_error ("ksba_cms_add_signer failed: %s\n", gpg_strerror (err));
461 rc = err;
462 goto leave;
464 rc = add_certificate_list (ctrl, cms, cl->cert);
465 if (rc)
467 log_error ("failed to store list of certificates: %s\n",
468 gpg_strerror(rc));
469 goto leave;
471 /* Set the hash algorithm we are going to use */
472 err = ksba_cms_add_digest_algo (cms, cl->hash_algo_oid);
473 if (err)
475 log_debug ("ksba_cms_add_digest_algo failed: %s\n",
476 gpg_strerror (err));
477 rc = err;
478 goto leave;
483 /* Check whether one of the certificates is qualified. Note that we
484 already validated the certificate and thus the user data stored
485 flag must be available. */
486 for (cl=signerlist; cl; cl = cl->next)
488 size_t buflen;
489 char buffer[1];
491 err = ksba_cert_get_user_data (cl->cert, "is_qualified",
492 &buffer, sizeof (buffer), &buflen);
493 if (err || !buflen)
495 log_error (_("checking for qualified certificate failed: %s\n"),
496 gpg_strerror (err));
497 rc = err;
498 goto leave;
500 if (*buffer)
501 err = gpgsm_qualified_consent (ctrl, cl->cert);
502 else
503 err = gpgsm_not_qualified_warning (ctrl, cl->cert);
504 if (err)
506 rc = err;
507 goto leave;
511 /* Prepare hashing (actually we are figuring out what we have set
512 above). */
513 rc = gcry_md_open (&data_md, 0, 0);
514 if (rc)
516 log_error ("md_open failed: %s\n", gpg_strerror (rc));
517 goto leave;
519 if (DBG_HASHING)
520 gcry_md_start_debug (data_md, "sign.data");
522 for (i=0; (algoid=ksba_cms_get_digest_algo_list (cms, i)); i++)
524 algo = gcry_md_map_name (algoid);
525 if (!algo)
527 log_error ("unknown hash algorithm `%s'\n", algoid? algoid:"?");
528 rc = gpg_error (GPG_ERR_BUG);
529 goto leave;
531 gcry_md_enable (data_md, algo);
534 if (detached)
535 { /* We hash the data right now so that we can store the message
536 digest. ksba_cms_build() takes this as an flag that detached
537 data is expected. */
538 unsigned char *digest;
539 size_t digest_len;
541 hash_data (data_fd, data_md);
542 for (cl=signerlist,signer=0; cl; cl = cl->next, signer++)
544 digest = gcry_md_read (data_md, cl->hash_algo);
545 digest_len = gcry_md_get_algo_dlen (cl->hash_algo);
546 if ( !digest || !digest_len )
548 log_error ("problem getting the hash of the data\n");
549 rc = gpg_error (GPG_ERR_BUG);
550 goto leave;
552 err = ksba_cms_set_message_digest (cms, signer, digest, digest_len);
553 if (err)
555 log_error ("ksba_cms_set_message_digest failed: %s\n",
556 gpg_strerror (err));
557 rc = err;
558 goto leave;
563 gnupg_get_isotime (signed_at);
564 for (cl=signerlist,signer=0; cl; cl = cl->next, signer++)
566 err = ksba_cms_set_signing_time (cms, signer, signed_at);
567 if (err)
569 log_error ("ksba_cms_set_signing_time failed: %s\n",
570 gpg_strerror (err));
571 rc = err;
572 goto leave;
576 /* We need to write at least a minimal list of our capabilities to
577 try to convince some MUAs to use 3DES and not the crippled
578 RC2. Our list is:
580 aes128-CBC
581 des-EDE3-CBC
583 err = ksba_cms_add_smime_capability (cms, "2.16.840.1.101.3.4.1.2", NULL, 0);
584 if (!err)
585 err = ksba_cms_add_smime_capability (cms, "1.2.840.113549.3.7", NULL, 0);
586 if (err)
588 log_error ("ksba_cms_add_smime_capability failed: %s\n",
589 gpg_strerror (err));
590 goto leave;
594 /* Main building loop. */
597 err = ksba_cms_build (cms, &stopreason);
598 if (err)
600 log_debug ("ksba_cms_build failed: %s\n", gpg_strerror (err));
601 rc = err;
602 goto leave;
605 if (stopreason == KSBA_SR_BEGIN_DATA)
607 /* Hash the data and store the message digest. */
608 unsigned char *digest;
609 size_t digest_len;
611 assert (!detached);
613 rc = hash_and_copy_data (data_fd, data_md, writer);
614 if (rc)
615 goto leave;
616 for (cl=signerlist,signer=0; cl; cl = cl->next, signer++)
618 digest = gcry_md_read (data_md, cl->hash_algo);
619 digest_len = gcry_md_get_algo_dlen (cl->hash_algo);
620 if ( !digest || !digest_len )
622 log_error ("problem getting the hash of the data\n");
623 rc = gpg_error (GPG_ERR_BUG);
624 goto leave;
626 err = ksba_cms_set_message_digest (cms, signer,
627 digest, digest_len);
628 if (err)
630 log_error ("ksba_cms_set_message_digest failed: %s\n",
631 gpg_strerror (err));
632 rc = err;
633 goto leave;
637 else if (stopreason == KSBA_SR_NEED_SIG)
639 /* Compute the signature for all signers. */
640 gcry_md_hd_t md;
642 rc = gcry_md_open (&md, 0, 0);
643 if (rc)
645 log_error ("md_open failed: %s\n", gpg_strerror (rc));
646 goto leave;
648 if (DBG_HASHING)
649 gcry_md_start_debug (md, "sign.attr");
650 ksba_cms_set_hash_function (cms, HASH_FNC, md);
651 for (cl=signerlist,signer=0; cl; cl = cl->next, signer++)
653 unsigned char *sigval = NULL;
654 char *buf, *fpr;
656 if (signer)
657 gcry_md_reset (md);
659 certlist_t cl_tmp;
661 for (cl_tmp=signerlist; cl_tmp; cl_tmp = cl_tmp->next)
662 gcry_md_enable (md, cl_tmp->hash_algo);
665 rc = ksba_cms_hash_signed_attrs (cms, signer);
666 if (rc)
668 log_debug ("hashing signed attrs failed: %s\n",
669 gpg_strerror (rc));
670 gcry_md_close (md);
671 goto leave;
674 rc = gpgsm_create_cms_signature (ctrl, cl->cert,
675 md, cl->hash_algo, &sigval);
676 if (rc)
678 gcry_md_close (md);
679 goto leave;
682 err = ksba_cms_set_sig_val (cms, signer, sigval);
683 xfree (sigval);
684 if (err)
686 log_error ("failed to store the signature: %s\n",
687 gpg_strerror (err));
688 rc = err;
689 gcry_md_close (md);
690 goto leave;
693 /* write a status message */
694 fpr = gpgsm_get_fingerprint_hexstring (cl->cert, GCRY_MD_SHA1);
695 if (!fpr)
697 rc = gpg_error (GPG_ERR_ENOMEM);
698 gcry_md_close (md);
699 goto leave;
702 int pkalgo = gpgsm_get_key_algo_info (cl->cert, NULL);
703 rc = asprintf (&buf, "%c %d %d 00 %s %s",
704 detached? 'D':'S',
705 pkalgo,
706 cl->hash_algo,
707 signed_at,
708 fpr);
710 xfree (fpr);
711 if (rc < 0)
713 rc = gpg_error (GPG_ERR_ENOMEM);
714 gcry_md_close (md);
715 goto leave;
717 rc = 0;
718 gpgsm_status (ctrl, STATUS_SIG_CREATED, buf);
719 free (buf); /* yes, we must use the regular free() here */
721 gcry_md_close (md);
725 while (stopreason != KSBA_SR_READY);
727 rc = gpgsm_finish_writer (b64writer);
728 if (rc)
730 log_error ("write failed: %s\n", gpg_strerror (rc));
731 goto leave;
734 log_info ("signature created\n");
737 leave:
738 if (rc)
739 log_error ("error creating signature: %s <%s>\n",
740 gpg_strerror (rc), gpg_strsource (rc) );
741 if (release_signerlist)
742 gpgsm_release_certlist (signerlist);
743 ksba_cms_release (cms);
744 gpgsm_destroy_writer (b64writer);
745 keydb_release (kh);
746 gcry_md_close (data_md);
747 return rc;