Expand a ~ in the ca-cert-file argument.
[gnupg.git] / sm / sign.c
blob776a5a571bb2c786f9310ce8ca983c93181bc000
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 gpgsm_status2 (ctrl, STATUS_INV_SGNR,
376 get_inv_recpsgnr_code (GPG_ERR_NO_SECKEY), NULL);
377 rc = gpg_error (GPG_ERR_GENERAL);
378 goto leave;
381 /* Although we don't check for ambigious specification we will
382 check that the signer's certificate is usable and valid. */
383 rc = gpgsm_cert_use_sign_p (cert);
384 if (!rc)
385 rc = gpgsm_validate_chain (ctrl, cert, "", NULL, 0, NULL, 0, NULL);
386 if (rc)
388 char *tmpfpr;
390 tmpfpr = gpgsm_get_fingerprint_hexstring (cert, 0);
391 gpgsm_status2 (ctrl, STATUS_INV_SGNR,
392 get_inv_recpsgnr_code (rc), tmpfpr, NULL);
393 xfree (tmpfpr);
394 goto leave;
397 /* That one is fine - create signerlist. */
398 signerlist = xtrycalloc (1, sizeof *signerlist);
399 if (!signerlist)
401 rc = out_of_core ();
402 ksba_cert_release (cert);
403 goto leave;
405 signerlist->cert = cert;
406 release_signerlist = 1;
409 /* Figure out the hash algorithm to use. We do not want to use the
410 one for the certificate but if possible an OID for the plain
411 algorithm. */
412 if (opt.forced_digest_algo && opt.verbose)
413 log_info ("user requested hash algorithm %d\n", opt.forced_digest_algo);
414 for (i=0, cl=signerlist; cl; cl = cl->next, i++)
416 const char *oid;
418 if (opt.forced_digest_algo)
420 oid = NULL;
421 cl->hash_algo = opt.forced_digest_algo;
423 else
425 oid = ksba_cert_get_digest_algo (cl->cert);
426 cl->hash_algo = oid ? gcry_md_map_name (oid) : 0;
428 switch (cl->hash_algo)
430 case GCRY_MD_SHA1: oid = "1.3.14.3.2.26"; break;
431 case GCRY_MD_RMD160: oid = "1.3.36.3.2.1"; break;
432 case GCRY_MD_SHA224: oid = "2.16.840.1.101.3.4.2.4"; break;
433 case GCRY_MD_SHA256: oid = "2.16.840.1.101.3.4.2.1"; break;
434 case GCRY_MD_SHA384: oid = "2.16.840.1.101.3.4.2.2"; break;
435 case GCRY_MD_SHA512: oid = "2.16.840.1.101.3.4.2.3"; break;
436 /* case GCRY_MD_WHIRLPOOL: oid = "No OID yet"; break; */
438 case GCRY_MD_MD5: /* We don't want to use MD5. */
439 case 0: /* No algorithm found in cert. */
440 default: /* Other algorithms. */
441 log_info (_("hash algorithm %d (%s) for signer %d not supported;"
442 " using %s\n"),
443 cl->hash_algo, oid? oid: "?", i,
444 gcry_md_algo_name (GCRY_MD_SHA1));
445 cl->hash_algo = GCRY_MD_SHA1;
446 oid = "1.3.14.3.2.26";
447 break;
449 cl->hash_algo_oid = oid;
452 if (opt.verbose)
454 for (i=0, cl=signerlist; cl; cl = cl->next, i++)
455 log_info (_("hash algorithm used for signer %d: %s (%s)\n"),
456 i, gcry_md_algo_name (cl->hash_algo), cl->hash_algo_oid);
460 /* Gather certificates of signers and store them in the CMS object. */
461 for (cl=signerlist; cl; cl = cl->next)
463 rc = gpgsm_cert_use_sign_p (cl->cert);
464 if (rc)
465 goto leave;
467 err = ksba_cms_add_signer (cms, cl->cert);
468 if (err)
470 log_error ("ksba_cms_add_signer failed: %s\n", gpg_strerror (err));
471 rc = err;
472 goto leave;
474 rc = add_certificate_list (ctrl, cms, cl->cert);
475 if (rc)
477 log_error ("failed to store list of certificates: %s\n",
478 gpg_strerror(rc));
479 goto leave;
481 /* Set the hash algorithm we are going to use */
482 err = ksba_cms_add_digest_algo (cms, cl->hash_algo_oid);
483 if (err)
485 log_debug ("ksba_cms_add_digest_algo failed: %s\n",
486 gpg_strerror (err));
487 rc = err;
488 goto leave;
493 /* Check whether one of the certificates is qualified. Note that we
494 already validated the certificate and thus the user data stored
495 flag must be available. */
496 for (cl=signerlist; cl; cl = cl->next)
498 size_t buflen;
499 char buffer[1];
501 err = ksba_cert_get_user_data (cl->cert, "is_qualified",
502 &buffer, sizeof (buffer), &buflen);
503 if (err || !buflen)
505 log_error (_("checking for qualified certificate failed: %s\n"),
506 gpg_strerror (err));
507 rc = err;
508 goto leave;
510 if (*buffer)
511 err = gpgsm_qualified_consent (ctrl, cl->cert);
512 else
513 err = gpgsm_not_qualified_warning (ctrl, cl->cert);
514 if (err)
516 rc = err;
517 goto leave;
521 /* Prepare hashing (actually we are figuring out what we have set
522 above). */
523 rc = gcry_md_open (&data_md, 0, 0);
524 if (rc)
526 log_error ("md_open failed: %s\n", gpg_strerror (rc));
527 goto leave;
529 if (DBG_HASHING)
530 gcry_md_start_debug (data_md, "sign.data");
532 for (i=0; (algoid=ksba_cms_get_digest_algo_list (cms, i)); i++)
534 algo = gcry_md_map_name (algoid);
535 if (!algo)
537 log_error ("unknown hash algorithm `%s'\n", algoid? algoid:"?");
538 rc = gpg_error (GPG_ERR_BUG);
539 goto leave;
541 gcry_md_enable (data_md, algo);
544 if (detached)
545 { /* We hash the data right now so that we can store the message
546 digest. ksba_cms_build() takes this as an flag that detached
547 data is expected. */
548 unsigned char *digest;
549 size_t digest_len;
551 hash_data (data_fd, data_md);
552 for (cl=signerlist,signer=0; cl; cl = cl->next, signer++)
554 digest = gcry_md_read (data_md, cl->hash_algo);
555 digest_len = gcry_md_get_algo_dlen (cl->hash_algo);
556 if ( !digest || !digest_len )
558 log_error ("problem getting the hash of the data\n");
559 rc = gpg_error (GPG_ERR_BUG);
560 goto leave;
562 err = ksba_cms_set_message_digest (cms, signer, digest, digest_len);
563 if (err)
565 log_error ("ksba_cms_set_message_digest failed: %s\n",
566 gpg_strerror (err));
567 rc = err;
568 goto leave;
573 gnupg_get_isotime (signed_at);
574 for (cl=signerlist,signer=0; cl; cl = cl->next, signer++)
576 err = ksba_cms_set_signing_time (cms, signer, signed_at);
577 if (err)
579 log_error ("ksba_cms_set_signing_time failed: %s\n",
580 gpg_strerror (err));
581 rc = err;
582 goto leave;
586 /* We need to write at least a minimal list of our capabilities to
587 try to convince some MUAs to use 3DES and not the crippled
588 RC2. Our list is:
590 aes128-CBC
591 des-EDE3-CBC
593 err = ksba_cms_add_smime_capability (cms, "2.16.840.1.101.3.4.1.2", NULL, 0);
594 if (!err)
595 err = ksba_cms_add_smime_capability (cms, "1.2.840.113549.3.7", NULL, 0);
596 if (err)
598 log_error ("ksba_cms_add_smime_capability failed: %s\n",
599 gpg_strerror (err));
600 goto leave;
604 /* Main building loop. */
607 err = ksba_cms_build (cms, &stopreason);
608 if (err)
610 log_debug ("ksba_cms_build failed: %s\n", gpg_strerror (err));
611 rc = err;
612 goto leave;
615 if (stopreason == KSBA_SR_BEGIN_DATA)
617 /* Hash the data and store the message digest. */
618 unsigned char *digest;
619 size_t digest_len;
621 assert (!detached);
623 rc = hash_and_copy_data (data_fd, data_md, writer);
624 if (rc)
625 goto leave;
626 for (cl=signerlist,signer=0; cl; cl = cl->next, signer++)
628 digest = gcry_md_read (data_md, cl->hash_algo);
629 digest_len = gcry_md_get_algo_dlen (cl->hash_algo);
630 if ( !digest || !digest_len )
632 log_error ("problem getting the hash of the data\n");
633 rc = gpg_error (GPG_ERR_BUG);
634 goto leave;
636 err = ksba_cms_set_message_digest (cms, signer,
637 digest, digest_len);
638 if (err)
640 log_error ("ksba_cms_set_message_digest failed: %s\n",
641 gpg_strerror (err));
642 rc = err;
643 goto leave;
647 else if (stopreason == KSBA_SR_NEED_SIG)
649 /* Compute the signature for all signers. */
650 gcry_md_hd_t md;
652 rc = gcry_md_open (&md, 0, 0);
653 if (rc)
655 log_error ("md_open failed: %s\n", gpg_strerror (rc));
656 goto leave;
658 if (DBG_HASHING)
659 gcry_md_start_debug (md, "sign.attr");
660 ksba_cms_set_hash_function (cms, HASH_FNC, md);
661 for (cl=signerlist,signer=0; cl; cl = cl->next, signer++)
663 unsigned char *sigval = NULL;
664 char *buf, *fpr;
666 if (signer)
667 gcry_md_reset (md);
669 certlist_t cl_tmp;
671 for (cl_tmp=signerlist; cl_tmp; cl_tmp = cl_tmp->next)
672 gcry_md_enable (md, cl_tmp->hash_algo);
675 rc = ksba_cms_hash_signed_attrs (cms, signer);
676 if (rc)
678 log_debug ("hashing signed attrs failed: %s\n",
679 gpg_strerror (rc));
680 gcry_md_close (md);
681 goto leave;
684 rc = gpgsm_create_cms_signature (ctrl, cl->cert,
685 md, cl->hash_algo, &sigval);
686 if (rc)
688 gcry_md_close (md);
689 goto leave;
692 err = ksba_cms_set_sig_val (cms, signer, sigval);
693 xfree (sigval);
694 if (err)
696 log_error ("failed to store the signature: %s\n",
697 gpg_strerror (err));
698 rc = err;
699 gcry_md_close (md);
700 goto leave;
703 /* write a status message */
704 fpr = gpgsm_get_fingerprint_hexstring (cl->cert, GCRY_MD_SHA1);
705 if (!fpr)
707 rc = gpg_error (GPG_ERR_ENOMEM);
708 gcry_md_close (md);
709 goto leave;
712 int pkalgo = gpgsm_get_key_algo_info (cl->cert, NULL);
713 rc = asprintf (&buf, "%c %d %d 00 %s %s",
714 detached? 'D':'S',
715 pkalgo,
716 cl->hash_algo,
717 signed_at,
718 fpr);
720 xfree (fpr);
721 if (rc < 0)
723 rc = gpg_error (GPG_ERR_ENOMEM);
724 gcry_md_close (md);
725 goto leave;
727 rc = 0;
728 gpgsm_status (ctrl, STATUS_SIG_CREATED, buf);
729 free (buf); /* yes, we must use the regular free() here */
731 gcry_md_close (md);
735 while (stopreason != KSBA_SR_READY);
737 rc = gpgsm_finish_writer (b64writer);
738 if (rc)
740 log_error ("write failed: %s\n", gpg_strerror (rc));
741 goto leave;
744 log_info ("signature created\n");
747 leave:
748 if (rc)
749 log_error ("error creating signature: %s <%s>\n",
750 gpg_strerror (rc), gpg_strsource (rc) );
751 if (release_signerlist)
752 gpgsm_release_certlist (signerlist);
753 ksba_cms_release (cms);
754 gpgsm_destroy_writer (b64writer);
755 keydb_release (kh);
756 gcry_md_close (data_md);
757 return rc;