1 /* sign.c - Sign a message
2 * Copyright (C) 2001, 2002, 2003, 2008,
3 * 2010 Free Software Foundation, Inc.
5 * This file is part of GnuPG.
7 * GnuPG is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
12 * GnuPG is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, see <https://www.gnu.org/licenses/>.
34 #include "../common/i18n.h"
37 /* Hash the data and return if something was hashed. Return -1 on error. */
39 hash_data (int fd, gcry_md_hd_t md)
46 fp = es_fdopen_nc (fd, "rb");
49 log_error ("fdopen(%d) failed: %s\n", fd, strerror (errno));
55 nread = es_fread (buffer, 1, DIM(buffer), fp);
56 gcry_md_write (md, buffer, nread);
61 log_error ("read error on fd %d: %s\n", fd, strerror (errno));
70 hash_and_copy_data (int fd, gcry_md_hd_t md, ksba_writer_t writer)
79 fp = es_fdopen_nc (fd, "rb");
82 gpg_error_t tmperr = gpg_error_from_syserror ();
83 log_error ("fdopen(%d) failed: %s\n", fd, strerror (errno));
89 nread = es_fread (buffer, 1, DIM(buffer), fp);
93 gcry_md_write (md, buffer, nread);
94 err = ksba_writer_write_octet_string (writer, buffer, nread, 0);
97 log_error ("write failed: %s\n", gpg_strerror (err));
102 while (nread && !rc);
105 rc = gpg_error_from_syserror ();
106 log_error ("read error on fd %d: %s\n", fd, strerror (errno));
111 /* We can't allow signing an empty message because it does not
112 make much sense and more seriously, ksba_cms_build has
113 already written the tag for data and now expects an octet
114 string and an octet string of size 0 is illegal. */
115 log_error ("cannot sign an empty message\n");
116 rc = gpg_error (GPG_ERR_NO_DATA);
120 err = ksba_writer_write_octet_string (writer, NULL, 0, 1);
123 log_error ("write failed: %s\n", gpg_strerror (err));
132 /* Get the default certificate which is defined as the first
133 certificate capable of signing returned by the keyDB and has a
134 secret key available. */
136 gpgsm_get_default_cert (ctrl_t ctrl, ksba_cert_t *r_cert)
139 ksba_cert_t cert = NULL;
143 hd = keydb_new (ctrl);
145 return gpg_error (GPG_ERR_GENERAL);
146 rc = keydb_search_first (ctrl, hd);
155 rc = keydb_get_cert (hd, &cert);
158 log_error ("keydb_get_cert failed: %s\n", gpg_strerror (rc));
163 if (!gpgsm_cert_use_sign_p (cert, 1))
165 p = gpgsm_get_keygrip_hexstring (cert);
168 if (!gpgsm_agent_havekey (ctrl, p))
173 return 0; /* got it */
179 ksba_cert_release (cert);
182 while (!(rc = keydb_search_next (ctrl, hd)));
184 log_error ("keydb_search_next failed: %s\n", gpg_strerror (rc));
186 ksba_cert_release (cert);
193 get_default_signer (ctrl_t ctrl)
195 KEYDB_SEARCH_DESC desc;
196 ksba_cert_t cert = NULL;
197 KEYDB_HANDLE kh = NULL;
202 rc = gpgsm_get_default_cert (ctrl, &cert);
206 log_debug ("failed to find default certificate: %s\n",
213 rc = classify_user_id (opt.local_user, &desc, 0);
216 log_error ("failed to find default signer: %s\n", gpg_strerror (rc));
220 kh = keydb_new (ctrl);
224 rc = keydb_search (ctrl, kh, &desc, 1);
227 log_debug ("failed to find default certificate: rc=%d\n", rc);
231 rc = keydb_get_cert (kh, &cert);
234 log_debug ("failed to get cert: rc=%d\n", rc);
242 /* Depending on the options in CTRL add the certificate CERT as well as
243 other certificate up in the chain to the Root-CA to the CMS
246 add_certificate_list (ctrl_t ctrl, ksba_cms_t cms, ksba_cert_t cert)
250 ksba_cert_t next = NULL;
254 ksba_cert_ref (cert);
256 n = ctrl->include_certs;
263 n = 50; /* We better apply an upper bound */
265 /* First add my own certificate unless we don't want any certificate
269 if (not_root && gpgsm_is_root_cert (cert))
272 err = ksba_cms_add_cert (cms, cert);
278 /* Walk the chain to include all other certificates. Note that a -1
279 used for N makes sure that there is no limit and all certs get
281 while ( n-- && !(rc = gpgsm_walk_cert_chain (ctrl, cert, &next)) )
283 if (not_root && gpgsm_is_root_cert (next))
286 err = ksba_cms_add_cert (cms, next);
287 ksba_cert_release (cert);
288 cert = next; next = NULL;
292 ksba_cert_release (cert);
294 return gpg_err_code (rc) == GPG_ERR_NOT_FOUND? 0 : rc;
297 ksba_cert_release (cert);
298 log_error ("ksba_cms_add_cert failed: %s\n", gpg_strerror (err));
303 #if KSBA_VERSION_NUMBER >= 0x010400 && 0 /* 1.4.0 */
305 add_signed_attribute (ksba_cms_t cms, const char *attrstr)
308 char **fields = NULL;
311 unsigned char *der = NULL;
314 fields = strtokenize (attrstr, ":");
317 err = gpg_error_from_syserror ();
318 log_error ("strtokenize failed: %s\n", gpg_strerror (err));
322 for (i=0; fields[i]; i++)
326 err = gpg_error (GPG_ERR_SYNTAX);
327 log_error ("invalid attribute specification '%s': %s\n",
328 attrstr, i < 3 ? "not enough fields":"too many fields");
331 if (!ascii_strcasecmp (fields[1], "u"))
334 goto leave; /* Skip unsigned attributes. */
336 if (ascii_strcasecmp (fields[1], "s"))
338 err = gpg_error (GPG_ERR_SYNTAX);
339 log_error ("invalid attribute specification '%s': %s\n",
340 attrstr, "type is not 's' or 'u'");
343 /* Check that the OID is valid. */
344 err = ksba_oid_from_str (fields[0], &der, &derlen);
347 log_error ("invalid attribute specification '%s': %s\n",
348 attrstr, gpg_strerror (err));
354 if (strchr (fields[2], '/'))
356 /* FIXME: read from file. */
358 else /* Directly given in hex. */
360 for (i=0, s = fields[2]; hexdigitp (s); s++, i++)
362 if (*s || !i || (i&1))
364 log_error ("invalid attribute specification '%s': %s\n",
365 attrstr, "invalid hex encoding of the data");
366 err = gpg_error (GPG_ERR_SYNTAX);
369 der = xtrystrdup (fields[2]);
372 err = gpg_error_from_syserror ();
373 log_error ("malloc failed: %s\n", gpg_strerror (err));
376 for (s=fields[2], derlen=0; s[0] && s[1]; s += 2)
377 der[derlen++] = xtoi_2 (s);
380 /* Store the data in the CMS object for all signers. */
381 err = ksba_cms_add_attribute (cms, -1, fields[0], 0, der, derlen);
384 log_error ("invalid attribute specification '%s': %s\n",
385 attrstr, gpg_strerror (err));
394 #endif /*ksba >= 1.4.0 */
398 /* Perform a sign operation.
400 Sign the data received on DATA-FD in embedded mode or in detached
401 mode when DETACHED is true. Write the signature to OUT_FP. The
402 keys used to sign are taken from SIGNERLIST or the default one will
403 be used if the value of this argument is NULL. */
405 gpgsm_sign (ctrl_t ctrl, certlist_t signerlist,
406 int data_fd, int detached, estream_t out_fp)
410 gnupg_ksba_io_t b64writer = NULL;
411 ksba_writer_t writer;
412 ksba_cms_t cms = NULL;
413 ksba_stop_reason_t stopreason;
414 KEYDB_HANDLE kh = NULL;
415 gcry_md_hd_t data_md = NULL;
419 ksba_isotime_t signed_at;
421 int release_signerlist = 0;
423 audit_set_type (ctrl->audit, AUDIT_TYPE_SIGN);
425 kh = keydb_new (ctrl);
428 log_error (_("failed to allocate keyDB handle\n"));
429 rc = gpg_error (GPG_ERR_GENERAL);
433 if (!gnupg_rng_is_compliant (opt.compliance))
435 rc = gpg_error (GPG_ERR_FORBIDDEN);
436 log_error (_("%s is not compliant with %s mode\n"),
438 gnupg_compliance_option_string (opt.compliance));
439 gpgsm_status_with_error (ctrl, STATUS_ERROR,
440 "random-compliance", rc);
444 ctrl->pem_name = "SIGNED MESSAGE";
445 rc = gnupg_ksba_create_writer
446 (&b64writer, ((ctrl->create_pem? GNUPG_KSBA_IO_PEM : 0)
447 | (ctrl->create_base64? GNUPG_KSBA_IO_BASE64 : 0)),
448 ctrl->pem_name, out_fp, &writer);
451 log_error ("can't create writer: %s\n", gpg_strerror (rc));
455 err = ksba_cms_new (&cms);
462 err = ksba_cms_set_reader_writer (cms, NULL, writer);
465 log_debug ("ksba_cms_set_reader_writer failed: %s\n",
471 /* We are going to create signed data with data as encap. content.
472 * In authenticode mode we use spcIndirectDataContext instead. */
473 err = ksba_cms_set_content_type (cms, 0, KSBA_CT_SIGNED_DATA);
475 err = ksba_cms_set_content_type
477 #if KSBA_VERSION_NUMBER >= 0x010400 && 0
478 opt.authenticode? KSBA_CT_SPC_IND_DATA_CTX :
484 log_debug ("ksba_cms_set_content_type failed: %s\n",
490 /* If no list of signers is given, use the default certificate. */
493 ksba_cert_t cert = get_default_signer (ctrl);
496 log_error ("no default signer found\n");
497 gpgsm_status2 (ctrl, STATUS_INV_SGNR,
498 get_inv_recpsgnr_code (GPG_ERR_NO_SECKEY), NULL);
499 rc = gpg_error (GPG_ERR_GENERAL);
503 /* Although we don't check for ambiguous specification we will
504 check that the signer's certificate is usable and valid. */
505 rc = gpgsm_cert_use_sign_p (cert, 0);
507 rc = gpgsm_validate_chain (ctrl, cert, "", NULL, 0, NULL, 0, NULL);
512 tmpfpr = gpgsm_get_fingerprint_hexstring (cert, 0);
513 gpgsm_status2 (ctrl, STATUS_INV_SGNR,
514 get_inv_recpsgnr_code (rc), tmpfpr, NULL);
519 /* That one is fine - create signerlist. */
520 signerlist = xtrycalloc (1, sizeof *signerlist);
524 ksba_cert_release (cert);
527 signerlist->cert = cert;
528 release_signerlist = 1;
532 /* Figure out the hash algorithm to use. We do not want to use the
533 one for the certificate but if possible an OID for the plain
535 if (opt.forced_digest_algo && opt.verbose)
536 log_info ("user requested hash algorithm %d\n", opt.forced_digest_algo);
537 for (i=0, cl=signerlist; cl; cl = cl->next, i++)
543 pk_algo = gpgsm_get_key_algo_info (cl->cert, &nbits);
544 cl->pk_algo = pk_algo;
546 if (opt.forced_digest_algo)
549 cl->hash_algo = opt.forced_digest_algo;
553 if (pk_algo == GCRY_PK_ECC)
555 /* Map the Curve to a corresponding hash algo. */
557 oid = "2.16.840.1.101.3.4.2.1"; /* sha256 */
558 else if (nbits <= 384)
559 oid = "2.16.840.1.101.3.4.2.2"; /* sha384 */
561 oid = "2.16.840.1.101.3.4.2.3"; /* sha512 */
565 /* For RSA we reuse the hash algo used by the certificate. */
566 oid = ksba_cert_get_digest_algo (cl->cert);
568 cl->hash_algo = oid ? gcry_md_map_name (oid) : 0;
570 switch (cl->hash_algo)
572 case GCRY_MD_SHA1: oid = "1.3.14.3.2.26"; break;
573 case GCRY_MD_RMD160: oid = "1.3.36.3.2.1"; break;
574 case GCRY_MD_SHA224: oid = "2.16.840.1.101.3.4.2.4"; break;
575 case GCRY_MD_SHA256: oid = "2.16.840.1.101.3.4.2.1"; break;
576 case GCRY_MD_SHA384: oid = "2.16.840.1.101.3.4.2.2"; break;
577 case GCRY_MD_SHA512: oid = "2.16.840.1.101.3.4.2.3"; break;
578 /* case GCRY_MD_WHIRLPOOL: oid = "No OID yet"; break; */
580 case GCRY_MD_MD5: /* We don't want to use MD5. */
581 case 0: /* No algorithm found in cert. */
582 default: /* Other algorithms. */
583 log_info (_("hash algorithm %d (%s) for signer %d not supported;"
585 cl->hash_algo, oid? oid: "?", i,
586 gcry_md_algo_name (GCRY_MD_SHA1));
587 cl->hash_algo = GCRY_MD_SHA1;
588 oid = "1.3.14.3.2.26";
591 cl->hash_algo_oid = oid;
593 /* Check compliance. */
594 if (! gnupg_digest_is_allowed (opt.compliance, 1, cl->hash_algo))
596 log_error (_("digest algorithm '%s' may not be used in %s mode\n"),
597 gcry_md_algo_name (cl->hash_algo),
598 gnupg_compliance_option_string (opt.compliance));
599 err = gpg_error (GPG_ERR_DIGEST_ALGO);
603 if (! gnupg_pk_is_allowed (opt.compliance, PK_USE_SIGNING, pk_algo, 0,
608 snprintf (kidstr, sizeof kidstr, "0x%08lX",
609 gpgsm_get_short_fingerprint (cl->cert, NULL));
610 log_error (_("key %s may not be used for signing in %s mode\n"),
612 gnupg_compliance_option_string (opt.compliance));
613 err = gpg_error (GPG_ERR_PUBKEY_ALGO);
619 if (opt.verbose > 1 || opt.debug)
621 for (i=0, cl=signerlist; cl; cl = cl->next, i++)
622 log_info (_("hash algorithm used for signer %d: %s (%s)\n"),
623 i, gcry_md_algo_name (cl->hash_algo), cl->hash_algo_oid);
627 /* Gather certificates of signers and store them in the CMS object. */
628 for (cl=signerlist; cl; cl = cl->next)
630 rc = gpgsm_cert_use_sign_p (cl->cert, 0);
634 err = ksba_cms_add_signer (cms, cl->cert);
637 log_error ("ksba_cms_add_signer failed: %s\n", gpg_strerror (err));
641 rc = add_certificate_list (ctrl, cms, cl->cert);
644 log_error ("failed to store list of certificates: %s\n",
648 /* Set the hash algorithm we are going to use */
649 err = ksba_cms_add_digest_algo (cms, cl->hash_algo_oid);
652 log_debug ("ksba_cms_add_digest_algo failed: %s\n",
660 /* Check whether one of the certificates is qualified. Note that we
661 already validated the certificate and thus the user data stored
662 flag must be available. */
663 if (!opt.no_chain_validation)
665 for (cl=signerlist; cl; cl = cl->next)
670 err = ksba_cert_get_user_data (cl->cert, "is_qualified",
671 &buffer, sizeof (buffer), &buflen);
674 log_error (_("checking for qualified certificate failed: %s\n"),
680 err = gpgsm_qualified_consent (ctrl, cl->cert);
682 err = gpgsm_not_qualified_warning (ctrl, cl->cert);
691 /* Prepare hashing (actually we are figuring out what we have set
693 rc = gcry_md_open (&data_md, 0, 0);
696 log_error ("md_open failed: %s\n", gpg_strerror (rc));
700 gcry_md_debug (data_md, "sign.data");
702 for (i=0; (algoid=ksba_cms_get_digest_algo_list (cms, i)); i++)
704 algo = gcry_md_map_name (algoid);
707 log_error ("unknown hash algorithm '%s'\n", algoid? algoid:"?");
708 rc = gpg_error (GPG_ERR_BUG);
711 gcry_md_enable (data_md, algo);
712 audit_log_i (ctrl->audit, AUDIT_DATA_HASH_ALGO, algo);
715 audit_log (ctrl->audit, AUDIT_SETUP_READY);
718 { /* We hash the data right now so that we can store the message
719 digest. ksba_cms_build() takes this as an flag that detached
721 unsigned char *digest;
724 if (!hash_data (data_fd, data_md))
725 audit_log (ctrl->audit, AUDIT_GOT_DATA);
726 for (cl=signerlist,signer=0; cl; cl = cl->next, signer++)
728 digest = gcry_md_read (data_md, cl->hash_algo);
729 digest_len = gcry_md_get_algo_dlen (cl->hash_algo);
730 if ( !digest || !digest_len )
732 log_error ("problem getting the hash of the data\n");
733 rc = gpg_error (GPG_ERR_BUG);
736 err = ksba_cms_set_message_digest (cms, signer, digest, digest_len);
739 log_error ("ksba_cms_set_message_digest failed: %s\n",
747 gnupg_get_isotime (signed_at);
748 for (cl=signerlist,signer=0; cl; cl = cl->next, signer++)
750 err = ksba_cms_set_signing_time (cms, signer, signed_at);
753 log_error ("ksba_cms_set_signing_time failed: %s\n",
760 /* We can add signed attributes only when build against libksba 1.4. */
761 #if KSBA_VERSION_NUMBER >= 0x010400 && 0 /* 1.4.0 */
765 for (sl = opt.attributes; sl; sl = sl->next)
766 if ((err = add_signed_attribute (cms, sl->d)))
771 log_info ("Note: option --attribute is ignored by this version\n");
772 #endif /*ksba >= 1.4.0 */
775 /* We need to write at least a minimal list of our capabilities to
776 try to convince some MUAs to use 3DES and not the crippled
782 err = ksba_cms_add_smime_capability (cms, "2.16.840.1.101.3.4.1.2", NULL, 0);
784 err = ksba_cms_add_smime_capability (cms, "1.2.840.113549.3.7", NULL, 0);
787 log_error ("ksba_cms_add_smime_capability failed: %s\n",
793 /* Main building loop. */
796 err = ksba_cms_build (cms, &stopreason);
799 log_debug ("ksba_cms_build failed: %s\n", gpg_strerror (err));
804 if (stopreason == KSBA_SR_BEGIN_DATA)
806 /* Hash the data and store the message digest. */
807 unsigned char *digest;
810 log_assert (!detached);
812 rc = hash_and_copy_data (data_fd, data_md, writer);
815 audit_log (ctrl->audit, AUDIT_GOT_DATA);
816 for (cl=signerlist,signer=0; cl; cl = cl->next, signer++)
818 digest = gcry_md_read (data_md, cl->hash_algo);
819 digest_len = gcry_md_get_algo_dlen (cl->hash_algo);
820 if ( !digest || !digest_len )
822 log_error ("problem getting the hash of the data\n");
823 rc = gpg_error (GPG_ERR_BUG);
826 err = ksba_cms_set_message_digest (cms, signer,
830 log_error ("ksba_cms_set_message_digest failed: %s\n",
837 else if (stopreason == KSBA_SR_NEED_SIG)
839 /* Compute the signature for all signers. */
842 rc = gcry_md_open (&md, 0, 0);
845 log_error ("md_open failed: %s\n", gpg_strerror (rc));
849 gcry_md_debug (md, "sign.attr");
850 ksba_cms_set_hash_function (cms, HASH_FNC, md);
851 for (cl=signerlist,signer=0; cl; cl = cl->next, signer++)
853 unsigned char *sigval = NULL;
856 audit_log_i (ctrl->audit, AUDIT_NEW_SIG, signer);
862 for (cl_tmp=signerlist; cl_tmp; cl_tmp = cl_tmp->next)
864 gcry_md_enable (md, cl_tmp->hash_algo);
865 audit_log_i (ctrl->audit, AUDIT_ATTR_HASH_ALGO,
870 rc = ksba_cms_hash_signed_attrs (cms, signer);
873 log_debug ("hashing signed attrs failed: %s\n",
879 rc = gpgsm_create_cms_signature (ctrl, cl->cert,
880 md, cl->hash_algo, &sigval);
883 audit_log_cert (ctrl->audit, AUDIT_SIGNED_BY, cl->cert, rc);
888 err = ksba_cms_set_sig_val (cms, signer, sigval);
892 audit_log_cert (ctrl->audit, AUDIT_SIGNED_BY, cl->cert, err);
893 log_error ("failed to store the signature: %s\n",
900 /* write a status message */
901 fpr = gpgsm_get_fingerprint_hexstring (cl->cert, GCRY_MD_SHA1);
904 rc = gpg_error (GPG_ERR_ENOMEM);
911 char *pkalgostr = gpgsm_pubkey_algo_string (cl->cert, NULL);
912 log_info (_("%s/%s signature using %s key %s\n"),
913 pubkey_algo_to_string (cl->pk_algo),
914 gcry_md_algo_name (cl->hash_algo),
918 buf = xtryasprintf ("%c %d %d 00 %s %s",
925 rc = gpg_error_from_syserror ();
932 gpgsm_status (ctrl, STATUS_SIG_CREATED, buf);
934 audit_log_cert (ctrl->audit, AUDIT_SIGNED_BY, cl->cert, 0);
939 while (stopreason != KSBA_SR_READY);
941 rc = gnupg_ksba_finish_writer (b64writer);
944 log_error ("write failed: %s\n", gpg_strerror (rc));
948 audit_log (ctrl->audit, AUDIT_SIGNING_DONE);
949 log_info ("signature created\n");
953 log_error ("error creating signature: %s <%s>\n",
954 gpg_strerror (rc), gpg_strsource (rc) );
955 if (release_signerlist)
956 gpgsm_release_certlist (signerlist);
957 ksba_cms_release (cms);
958 gnupg_ksba_destroy_writer (b64writer);
960 gcry_md_close (data_md);