1 /* sign.c - Sign a message
2 * Copyright (C) 2001, 2002, 2003, 2008,
3 * 2010 Free Software Foundation, Inc.
4 * Copyright (C) 2003-2012, 2016-2017, 2019,
5 * 2020, 2022-2023 g10 Code GmbH
7 * This file is part of GnuPG.
9 * GnuPG is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
14 * GnuPG is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, see <https://www.gnu.org/licenses/>.
21 * SPDX-License-Identifier: GPL-3.0-or-later
37 #include "../common/i18n.h"
38 #include "../common/tlv.h"
41 /* Hash the data and return if something was hashed. Return -1 on error. */
43 hash_data (int fd, gcry_md_hd_t md)
50 fp = es_fdopen_nc (fd, "rb");
53 log_error ("fdopen(%d) failed: %s\n", fd, strerror (errno));
59 nread = es_fread (buffer, 1, DIM(buffer), fp);
60 gcry_md_write (md, buffer, nread);
65 log_error ("read error on fd %d: %s\n", fd, strerror (errno));
74 hash_and_copy_data (int fd, gcry_md_hd_t md, ksba_writer_t writer)
83 fp = es_fdopen_nc (fd, "rb");
86 gpg_error_t tmperr = gpg_error_from_syserror ();
87 log_error ("fdopen(%d) failed: %s\n", fd, strerror (errno));
93 nread = es_fread (buffer, 1, DIM(buffer), fp);
97 gcry_md_write (md, buffer, nread);
98 err = ksba_writer_write_octet_string (writer, buffer, nread, 0);
101 log_error ("write failed: %s\n", gpg_strerror (err));
106 while (nread && !rc);
109 rc = gpg_error_from_syserror ();
110 log_error ("read error on fd %d: %s\n", fd, strerror (errno));
115 /* We can't allow signing an empty message because it does not
116 make much sense and more seriously, ksba_cms_build has
117 already written the tag for data and now expects an octet
118 string and an octet string of size 0 is illegal. */
119 log_error ("cannot sign an empty message\n");
120 rc = gpg_error (GPG_ERR_NO_DATA);
124 err = ksba_writer_write_octet_string (writer, NULL, 0, 1);
127 log_error ("write failed: %s\n", gpg_strerror (err));
136 /* Get the default certificate which is defined as the first
137 certificate capable of signing returned by the keyDB and has a
138 secret key available. */
140 gpgsm_get_default_cert (ctrl_t ctrl, ksba_cert_t *r_cert)
143 ksba_cert_t cert = NULL;
147 hd = keydb_new (ctrl);
149 return gpg_error (GPG_ERR_GENERAL);
150 rc = keydb_search_first (ctrl, hd);
159 rc = keydb_get_cert (hd, &cert);
162 log_error ("keydb_get_cert failed: %s\n", gpg_strerror (rc));
167 if (!gpgsm_cert_use_sign_p (cert, 1))
169 p = gpgsm_get_keygrip_hexstring (cert);
172 if (!gpgsm_agent_havekey (ctrl, p))
177 return 0; /* got it */
183 ksba_cert_release (cert);
186 while (!(rc = keydb_search_next (ctrl, hd)));
188 log_error ("keydb_search_next failed: %s\n", gpg_strerror (rc));
190 ksba_cert_release (cert);
197 get_default_signer (ctrl_t ctrl)
199 KEYDB_SEARCH_DESC desc;
200 ksba_cert_t cert = NULL;
201 KEYDB_HANDLE kh = NULL;
206 rc = gpgsm_get_default_cert (ctrl, &cert);
210 log_debug ("failed to find default certificate: %s\n",
217 rc = classify_user_id (opt.local_user, &desc, 0);
220 log_error ("failed to find default signer: %s\n", gpg_strerror (rc));
224 kh = keydb_new (ctrl);
228 rc = keydb_search (ctrl, kh, &desc, 1);
231 log_debug ("failed to find default certificate: rc=%d\n", rc);
235 rc = keydb_get_cert (kh, &cert);
238 log_debug ("failed to get cert: rc=%d\n", rc);
246 /* Depending on the options in CTRL add the certificate CERT as well as
247 other certificate up in the chain to the Root-CA to the CMS
250 add_certificate_list (ctrl_t ctrl, ksba_cms_t cms, ksba_cert_t cert)
254 ksba_cert_t next = NULL;
258 ksba_cert_ref (cert);
260 n = ctrl->include_certs;
267 n = 50; /* We better apply an upper bound */
269 /* First add my own certificate unless we don't want any certificate
273 if (not_root && gpgsm_is_root_cert (cert))
276 err = ksba_cms_add_cert (cms, cert);
282 /* Walk the chain to include all other certificates. Note that a -1
283 used for N makes sure that there is no limit and all certs get
285 while ( n-- && !(rc = gpgsm_walk_cert_chain (ctrl, cert, &next)) )
287 if (not_root && gpgsm_is_root_cert (next))
290 err = ksba_cms_add_cert (cms, next);
291 ksba_cert_release (cert);
292 cert = next; next = NULL;
296 ksba_cert_release (cert);
298 return gpg_err_code (rc) == GPG_ERR_NOT_FOUND? 0 : rc;
301 ksba_cert_release (cert);
302 log_error ("ksba_cms_add_cert failed: %s\n", gpg_strerror (err));
308 add_signed_attribute (ksba_cms_t cms, const char *attrstr)
311 char **fields = NULL;
314 unsigned char *der = NULL;
317 fields = strtokenize (attrstr, ":");
320 err = gpg_error_from_syserror ();
321 log_error ("strtokenize failed: %s\n", gpg_strerror (err));
325 for (i=0; fields[i]; i++)
329 err = gpg_error (GPG_ERR_SYNTAX);
330 log_error ("invalid attribute specification '%s': %s\n",
331 attrstr, i < 3 ? "not enough fields":"too many fields");
334 if (!ascii_strcasecmp (fields[1], "u"))
337 goto leave; /* Skip unsigned attributes. */
339 if (ascii_strcasecmp (fields[1], "s"))
341 err = gpg_error (GPG_ERR_SYNTAX);
342 log_error ("invalid attribute specification '%s': %s\n",
343 attrstr, "type is not 's' or 'u'");
346 /* Check that the OID is valid. */
347 err = ksba_oid_from_str (fields[0], &der, &derlen);
350 log_error ("invalid attribute specification '%s': %s\n",
351 attrstr, gpg_strerror (err));
357 if (strchr (fields[2], '/'))
359 /* FIXME: read from file. */
361 else /* Directly given in hex. */
363 for (i=0, s = fields[2]; hexdigitp (s); s++, i++)
365 if (*s || !i || (i&1))
367 log_error ("invalid attribute specification '%s': %s\n",
368 attrstr, "invalid hex encoding of the data");
369 err = gpg_error (GPG_ERR_SYNTAX);
372 der = xtrystrdup (fields[2]);
375 err = gpg_error_from_syserror ();
376 log_error ("malloc failed: %s\n", gpg_strerror (err));
379 for (s=fields[2], derlen=0; s[0] && s[1]; s += 2)
380 der[derlen++] = xtoi_2 (s);
383 /* Store the data in the CMS object for all signers. */
385 err = ksba_cms_add_attribute (cms, -1, fields[0], 0, der, derlen);
388 err = gpg_error (GPG_ERR_NOT_IMPLEMENTED);
392 log_error ("invalid attribute specification '%s': %s\n",
393 attrstr, gpg_strerror (err));
405 /* This function takes a binary detached signature in (BLOB,BLOBLEN)
406 * and writes it to OUT_FP. The core of the function is to replace
407 * NDEF length sequences in the input to those with fixed inputs.
408 * This helps certain other implementations to properly verify
409 * detached signature. Moreover, it allows our own trailing zero
410 * stripping code - which we need for PDF signatures - to work
413 * Example start of a detached signature as created by us:
414 * 0 NDEF: SEQUENCE { -- 1st sequence
415 * 2 9: OBJECT IDENTIFIER signedData (1 2 840 113549 1 7 2)
416 * 13 NDEF: [0] { -- 2nd sequence
417 * 15 NDEF: SEQUENCE { -- 3rd sequence
418 * 17 1: INTEGER 1 -- version
419 * 20 15: SET { -- set of algorithms
421 * 24 9: OBJECT IDENTIFIER sha-256 (2 16 840 1 101 3 4 2 1)
425 * 37 NDEF: SEQUENCE { -- 4th pretty short sequence
426 * 39 9: OBJECT IDENTIFIER data (1 2 840 113549 1 7 1)
429 * Our goal is to replace the NDEF by fixed length tags.
432 write_detached_signature (ctrl_t ctrl, const void *blob, size_t bloblen,
436 const unsigned char *p;
437 size_t n, objlen, hdrlen;
438 int class, tag, cons, ndef;
439 const unsigned char *p_ctoid, *p_version, *p_algoset, *p_dataoid;
440 size_t n_ctoid, n_version, n_algoset, n_dataoid;
441 const unsigned char *p_certset, *p_signerinfos;
442 size_t n_certset, n_signerinfos;
445 unsigned char *finalder = NULL;
452 if ((err=parse_ber_header (&p,&n,&class,&tag,&cons,&ndef,&objlen,&hdrlen)))
454 if (!(class == CLASS_UNIVERSAL && tag == TAG_SEQUENCE && cons))
455 return gpg_error (GPG_ERR_INV_CMS_OBJ); /* No 1st sequence. */
457 if ((err=parse_ber_header (&p,&n,&class,&tag,&cons,&ndef,&objlen,&hdrlen)))
459 if (!(class == CLASS_UNIVERSAL && tag == TAG_OBJECT_ID && !cons))
460 return gpg_error (GPG_ERR_INV_CMS_OBJ); /* No signedData OID. */
462 return gpg_error (GPG_ERR_BAD_BER); /* Object larger than data. */
468 if ((err=parse_ber_header (&p,&n,&class,&tag,&cons,&ndef,&objlen,&hdrlen)))
470 if (!(class == CLASS_CONTEXT && tag == 0 && cons))
471 return gpg_error (GPG_ERR_INV_CMS_OBJ); /* No 2nd sequence. */
473 if ((err=parse_ber_header (&p,&n,&class,&tag,&cons,&ndef,&objlen,&hdrlen)))
475 if (!(class == CLASS_UNIVERSAL && tag == TAG_SEQUENCE && cons))
476 return gpg_error (GPG_ERR_INV_CMS_OBJ); /* No 3rd sequence. */
478 if ((err=parse_ber_header (&p,&n,&class,&tag,&cons,&ndef,&objlen,&hdrlen)))
480 if (!(class == CLASS_UNIVERSAL && tag == TAG_INTEGER))
481 return gpg_error (GPG_ERR_INV_CMS_OBJ); /* No version. */
483 return gpg_error (GPG_ERR_BAD_BER); /* Object larger than data. */
490 if ((err=parse_ber_header (&p,&n,&class,&tag,&cons,&ndef,&objlen,&hdrlen)))
492 if (!(class == CLASS_UNIVERSAL && tag == TAG_SET && cons && !ndef))
493 return gpg_error (GPG_ERR_INV_CMS_OBJ); /* No set of algorithms. */
495 return gpg_error (GPG_ERR_BAD_BER); /* Object larger than data. */
496 n_algoset = hdrlen + objlen;
500 if ((err=parse_ber_header (&p,&n,&class,&tag,&cons,&ndef,&objlen,&hdrlen)))
502 if (!(class == CLASS_UNIVERSAL && tag == TAG_SEQUENCE && cons))
503 return gpg_error (GPG_ERR_INV_CMS_OBJ); /* No 4th sequence. */
505 if ((err=parse_ber_header (&p,&n,&class,&tag,&cons,&ndef,&objlen,&hdrlen)))
507 if (!(class == CLASS_UNIVERSAL && tag == TAG_OBJECT_ID && !cons))
508 return gpg_error (GPG_ERR_INV_CMS_OBJ); /* No data OID. */
510 return gpg_error (GPG_ERR_BAD_BER); /* Object larger than data. */
516 if ((err=parse_ber_header (&p,&n,&class,&tag,&cons,&ndef,&objlen,&hdrlen)))
518 if (!(class == CLASS_UNIVERSAL && tag == TAG_NONE && !cons && !objlen))
519 return gpg_error (GPG_ERR_INV_CMS_OBJ); /* No End tag. */
521 /* certificates [0] IMPLICIT CertificateSet OPTIONAL,
522 * Note: We ignore the following
523 * crls [1] IMPLICIT CertificateRevocationLists OPTIONAL
524 * because gpgsm does not create them. */
525 if ((err=parse_ber_header (&p,&n,&class,&tag,&cons,&ndef,&objlen,&hdrlen)))
527 if (class == CLASS_CONTEXT && tag == 0 && cons)
530 return gpg_error (GPG_ERR_BAD_BER); /* Object larger than data. */
535 if ((err=parse_ber_header (&p,&n,&class,&tag,&cons,&ndef,
545 /* SignerInfos ::= SET OF SignerInfo */
546 if (!(class == CLASS_UNIVERSAL && tag == TAG_SET && cons && !ndef))
547 return gpg_error (GPG_ERR_INV_CMS_OBJ); /* No set of signerInfos. */
549 return gpg_error (GPG_ERR_BAD_BER); /* Object larger than data. */
551 n_signerinfos = objlen;
555 /* For the fun of it check the 3 end tags. */
556 for (i=0; i < 3; i++)
558 if ((err=parse_ber_header (&p,&n,&class,&tag,&cons,&ndef,
561 if (!(class == CLASS_UNIVERSAL && tag == TAG_NONE && !cons && !objlen))
562 return gpg_error (GPG_ERR_INV_CMS_OBJ); /* No End tag. */
565 return gpg_error (GPG_ERR_INV_CMS_OBJ); /* Garbage */
567 /*---- From here on we jump to leave on error. ----*/
569 /* Now create a new object from the collected data. */
570 dbld = ksba_der_builder_new (16); /* (pre-allocate 16 items) */
573 err = gpg_error_from_syserror ();
576 ksba_der_add_tag (dbld, 0, KSBA_TYPE_SEQUENCE);
577 ksba_der_add_val ( dbld, 0, KSBA_TYPE_OBJECT_ID, p_ctoid, n_ctoid);
578 ksba_der_add_tag ( dbld, KSBA_CLASS_CONTEXT, 0);
579 ksba_der_add_tag ( dbld, 0, KSBA_TYPE_SEQUENCE);
580 ksba_der_add_val ( dbld, 0, KSBA_TYPE_INTEGER, p_version, n_version);
581 ksba_der_add_der ( dbld, p_algoset, n_algoset);
582 ksba_der_add_tag ( dbld, 0, KSBA_TYPE_SEQUENCE);
583 ksba_der_add_val ( dbld, 0, KSBA_TYPE_OBJECT_ID, p_dataoid, n_dataoid);
584 ksba_der_add_end ( dbld);
587 ksba_der_add_tag ( dbld, KSBA_CLASS_CONTEXT, 0);
588 ksba_der_add_der ( dbld, p_certset, n_certset);
589 ksba_der_add_end ( dbld);
591 ksba_der_add_tag ( dbld, 0, KSBA_TYPE_SET);
592 ksba_der_add_der ( dbld, p_signerinfos, n_signerinfos);
593 ksba_der_add_end ( dbld);
594 ksba_der_add_end ( dbld);
595 ksba_der_add_end ( dbld);
596 ksba_der_add_end (dbld);
598 err = ksba_der_builder_get (dbld, &finalder, &finalderlen);
602 if (es_fwrite (finalder, finalderlen, 1, out_fp) != 1)
604 err = gpg_error_from_syserror ();
610 ksba_der_release (dbld);
611 ksba_free (finalder);
617 /* Perform a sign operation.
619 Sign the data received on DATA-FD in embedded mode or in detached
620 mode when DETACHED is true. Write the signature to OUT_FP. The
621 keys used to sign are taken from SIGNERLIST or the default one will
622 be used if the value of this argument is NULL. */
624 gpgsm_sign (ctrl_t ctrl, certlist_t signerlist,
625 int data_fd, int detached, estream_t out_fp)
629 gnupg_ksba_io_t b64writer = NULL;
630 ksba_writer_t writer;
631 estream_t sig_fp = NULL; /* Used for detached signatures. */
632 ksba_cms_t cms = NULL;
633 ksba_stop_reason_t stopreason;
634 KEYDB_HANDLE kh = NULL;
635 gcry_md_hd_t data_md = NULL;
639 ksba_isotime_t signed_at;
641 int release_signerlist = 0;
642 int binary_detached = detached && !ctrl->create_pem && !ctrl->create_base64;
644 audit_set_type (ctrl->audit, AUDIT_TYPE_SIGN);
646 kh = keydb_new (ctrl);
649 log_error (_("failed to allocate keyDB handle\n"));
650 rc = gpg_error (GPG_ERR_GENERAL);
654 if (!gnupg_rng_is_compliant (opt.compliance))
656 rc = gpg_error (GPG_ERR_FORBIDDEN);
657 log_error (_("%s is not compliant with %s mode\n"),
659 gnupg_compliance_option_string (opt.compliance));
660 gpgsm_status_with_error (ctrl, STATUS_ERROR,
661 "random-compliance", rc);
665 /* Note that in detached mode the b64 write is actually a binary
666 * writer because we need to fixup the created signature later.
667 * Note that we do this only for binary output because we have no
668 * PEM writer interface outside of the ksba create writer code. */
669 ctrl->pem_name = "SIGNED MESSAGE";
672 sig_fp = es_fopenmem (0, "w+");
673 rc = sig_fp? 0 : gpg_error_from_syserror ();
675 rc = gnupg_ksba_create_writer (&b64writer, 0, NULL, sig_fp, &writer);
679 rc = gnupg_ksba_create_writer
680 (&b64writer, ((ctrl->create_pem? GNUPG_KSBA_IO_PEM : 0)
681 | (ctrl->create_base64? GNUPG_KSBA_IO_BASE64 : 0)),
682 ctrl->pem_name, out_fp, &writer);
686 log_error ("can't create writer: %s\n", gpg_strerror (rc));
690 gnupg_ksba_set_progress_cb (b64writer, gpgsm_progress_cb, ctrl);
691 if (ctrl->input_size_hint)
692 gnupg_ksba_set_total (b64writer, ctrl->input_size_hint);
694 err = ksba_cms_new (&cms);
701 err = ksba_cms_set_reader_writer (cms, NULL, writer);
704 log_debug ("ksba_cms_set_reader_writer failed: %s\n",
710 /* We are going to create signed data with data as encap. content.
711 * In authenticode mode we use spcIndirectDataContext instead. */
712 err = ksba_cms_set_content_type (cms, 0, KSBA_CT_SIGNED_DATA);
714 err = ksba_cms_set_content_type
716 opt.authenticode? KSBA_CT_SPC_IND_DATA_CTX :
721 log_debug ("ksba_cms_set_content_type failed: %s\n",
727 /* If no list of signers is given, use the default certificate. */
730 ksba_cert_t cert = get_default_signer (ctrl);
733 log_error ("no default signer found\n");
734 gpgsm_status2 (ctrl, STATUS_INV_SGNR,
735 get_inv_recpsgnr_code (GPG_ERR_NO_SECKEY), NULL);
736 rc = gpg_error (GPG_ERR_GENERAL);
740 /* Although we don't check for ambiguous specification we will
741 check that the signer's certificate is usable and valid. */
742 rc = gpgsm_cert_use_sign_p (cert, 0);
744 rc = gpgsm_validate_chain (ctrl, cert,
745 GNUPG_ISOTIME_NONE, NULL, 0, NULL, 0, NULL);
750 tmpfpr = gpgsm_get_fingerprint_hexstring (cert, 0);
751 gpgsm_status2 (ctrl, STATUS_INV_SGNR,
752 get_inv_recpsgnr_code (rc), tmpfpr, NULL);
757 /* That one is fine - create signerlist. */
758 signerlist = xtrycalloc (1, sizeof *signerlist);
762 ksba_cert_release (cert);
765 signerlist->cert = cert;
766 release_signerlist = 1;
770 /* Figure out the hash algorithm to use. We do not want to use the
771 one for the certificate but if possible an OID for the plain
773 if (opt.forced_digest_algo && opt.verbose)
774 log_info ("user requested hash algorithm %d\n", opt.forced_digest_algo);
775 for (i=0, cl=signerlist; cl; cl = cl->next, i++)
781 pk_algo = gpgsm_get_key_algo_info (cl->cert, &nbits);
782 cl->pk_algo = pk_algo;
784 if (opt.forced_digest_algo)
787 cl->hash_algo = opt.forced_digest_algo;
791 if (pk_algo == GCRY_PK_ECC)
793 /* Map the Curve to a corresponding hash algo. */
795 oid = "2.16.840.1.101.3.4.2.1"; /* sha256 */
796 else if (nbits <= 384)
797 oid = "2.16.840.1.101.3.4.2.2"; /* sha384 */
799 oid = "2.16.840.1.101.3.4.2.3"; /* sha512 */
803 /* For RSA we reuse the hash algo used by the certificate. */
804 oid = ksba_cert_get_digest_algo (cl->cert);
806 cl->hash_algo = oid ? gcry_md_map_name (oid) : 0;
808 switch (cl->hash_algo)
810 case GCRY_MD_SHA1: oid = "1.3.14.3.2.26"; break;
811 case GCRY_MD_RMD160: oid = "1.3.36.3.2.1"; break;
812 case GCRY_MD_SHA224: oid = "2.16.840.1.101.3.4.2.4"; break;
813 case GCRY_MD_SHA256: oid = "2.16.840.1.101.3.4.2.1"; break;
814 case GCRY_MD_SHA384: oid = "2.16.840.1.101.3.4.2.2"; break;
815 case GCRY_MD_SHA512: oid = "2.16.840.1.101.3.4.2.3"; break;
816 /* case GCRY_MD_WHIRLPOOL: oid = "No OID yet"; break; */
818 case GCRY_MD_MD5: /* We don't want to use MD5. */
819 case 0: /* No algorithm found in cert. */
820 default: /* Other algorithms. */
821 log_info (_("hash algorithm %d (%s) for signer %d not supported;"
823 cl->hash_algo, oid? oid: "?", i,
824 gcry_md_algo_name (GCRY_MD_SHA1));
825 cl->hash_algo = GCRY_MD_SHA1;
826 oid = "1.3.14.3.2.26";
829 cl->hash_algo_oid = oid;
831 /* Check compliance. */
832 if (! gnupg_digest_is_allowed (opt.compliance, 1, cl->hash_algo))
834 log_error (_("digest algorithm '%s' may not be used in %s mode\n"),
835 gcry_md_algo_name (cl->hash_algo),
836 gnupg_compliance_option_string (opt.compliance));
837 err = gpg_error (GPG_ERR_DIGEST_ALGO);
841 if (! gnupg_pk_is_allowed (opt.compliance, PK_USE_SIGNING, pk_algo, 0,
846 snprintf (kidstr, sizeof kidstr, "0x%08lX",
847 gpgsm_get_short_fingerprint (cl->cert, NULL));
848 log_error (_("key %s may not be used for signing in %s mode\n"),
850 gnupg_compliance_option_string (opt.compliance));
851 err = gpg_error (GPG_ERR_PUBKEY_ALGO);
857 if (opt.verbose > 1 || opt.debug)
859 for (i=0, cl=signerlist; cl; cl = cl->next, i++)
860 log_info (_("hash algorithm used for signer %d: %s (%s)\n"),
861 i, gcry_md_algo_name (cl->hash_algo), cl->hash_algo_oid);
865 /* Gather certificates of signers and store them in the CMS object. */
866 for (cl=signerlist; cl; cl = cl->next)
868 rc = gpgsm_cert_use_sign_p (cl->cert, 0);
872 err = ksba_cms_add_signer (cms, cl->cert);
875 log_error ("ksba_cms_add_signer failed: %s\n", gpg_strerror (err));
879 rc = add_certificate_list (ctrl, cms, cl->cert);
882 log_error ("failed to store list of certificates: %s\n",
886 /* Set the hash algorithm we are going to use */
887 err = ksba_cms_add_digest_algo (cms, cl->hash_algo_oid);
890 log_debug ("ksba_cms_add_digest_algo failed: %s\n",
898 /* Check whether one of the certificates is qualified. Note that we
899 already validated the certificate and thus the user data stored
900 flag must be available. */
901 if (!opt.no_chain_validation)
903 for (cl=signerlist; cl; cl = cl->next)
908 err = ksba_cert_get_user_data (cl->cert, "is_qualified",
909 &buffer, sizeof (buffer), &buflen);
912 log_error (_("checking for qualified certificate failed: %s\n"),
918 err = gpgsm_qualified_consent (ctrl, cl->cert);
920 err = gpgsm_not_qualified_warning (ctrl, cl->cert);
929 /* Prepare hashing (actually we are figuring out what we have set
931 rc = gcry_md_open (&data_md, 0, 0);
934 log_error ("md_open failed: %s\n", gpg_strerror (rc));
938 gcry_md_debug (data_md, "sign.data");
940 for (i=0; (algoid=ksba_cms_get_digest_algo_list (cms, i)); i++)
942 algo = gcry_md_map_name (algoid);
945 log_error ("unknown hash algorithm '%s'\n", algoid? algoid:"?");
946 rc = gpg_error (GPG_ERR_BUG);
949 gcry_md_enable (data_md, algo);
950 audit_log_i (ctrl->audit, AUDIT_DATA_HASH_ALGO, algo);
953 audit_log (ctrl->audit, AUDIT_SETUP_READY);
956 { /* We hash the data right now so that we can store the message
957 digest. ksba_cms_build() takes this as an flag that detached
959 unsigned char *digest;
962 if (!hash_data (data_fd, data_md))
963 audit_log (ctrl->audit, AUDIT_GOT_DATA);
964 for (cl=signerlist,signer=0; cl; cl = cl->next, signer++)
966 digest = gcry_md_read (data_md, cl->hash_algo);
967 digest_len = gcry_md_get_algo_dlen (cl->hash_algo);
968 if ( !digest || !digest_len )
970 log_error ("problem getting the hash of the data\n");
971 rc = gpg_error (GPG_ERR_BUG);
974 err = ksba_cms_set_message_digest (cms, signer, digest, digest_len);
977 log_error ("ksba_cms_set_message_digest failed: %s\n",
985 gnupg_get_isotime (signed_at);
986 for (cl=signerlist,signer=0; cl; cl = cl->next, signer++)
988 err = ksba_cms_set_signing_time (cms, signer, signed_at);
991 log_error ("ksba_cms_set_signing_time failed: %s\n",
1001 for (sl = opt.attributes; sl; sl = sl->next)
1002 if ((err = add_signed_attribute (cms, sl->d)))
1007 /* We need to write at least a minimal list of our capabilities to
1008 * try to convince some MUAs to use 3DES and not the crippled
1015 err = ksba_cms_add_smime_capability (cms, "2.16.840.1.101.3.4.1.42", NULL,0);
1017 err = ksba_cms_add_smime_capability (cms, "2.16.840.1.101.3.4.1.2", NULL,0);
1019 err = ksba_cms_add_smime_capability (cms, "1.2.840.113549.3.7", NULL, 0);
1022 log_error ("ksba_cms_add_smime_capability failed: %s\n",
1023 gpg_strerror (err));
1028 /* Main building loop. */
1031 err = ksba_cms_build (cms, &stopreason);
1034 log_error ("creating CMS object failed: %s\n", gpg_strerror (err));
1039 if (stopreason == KSBA_SR_BEGIN_DATA)
1041 /* Hash the data and store the message digest. */
1042 unsigned char *digest;
1045 log_assert (!detached);
1047 rc = hash_and_copy_data (data_fd, data_md, writer);
1050 audit_log (ctrl->audit, AUDIT_GOT_DATA);
1051 for (cl=signerlist,signer=0; cl; cl = cl->next, signer++)
1053 digest = gcry_md_read (data_md, cl->hash_algo);
1054 digest_len = gcry_md_get_algo_dlen (cl->hash_algo);
1055 if ( !digest || !digest_len )
1057 log_error ("problem getting the hash of the data\n");
1058 rc = gpg_error (GPG_ERR_BUG);
1061 err = ksba_cms_set_message_digest (cms, signer,
1062 digest, digest_len);
1065 log_error ("ksba_cms_set_message_digest failed: %s\n",
1066 gpg_strerror (err));
1072 else if (stopreason == KSBA_SR_NEED_SIG)
1074 /* Compute the signature for all signers. */
1077 rc = gcry_md_open (&md, 0, 0);
1080 log_error ("md_open failed: %s\n", gpg_strerror (rc));
1084 gcry_md_debug (md, "sign.attr");
1085 ksba_cms_set_hash_function (cms, HASH_FNC, md);
1086 for (cl=signerlist,signer=0; cl; cl = cl->next, signer++)
1088 unsigned char *sigval = NULL;
1091 audit_log_i (ctrl->audit, AUDIT_NEW_SIG, signer);
1097 for (cl_tmp=signerlist; cl_tmp; cl_tmp = cl_tmp->next)
1099 gcry_md_enable (md, cl_tmp->hash_algo);
1100 audit_log_i (ctrl->audit, AUDIT_ATTR_HASH_ALGO,
1105 rc = ksba_cms_hash_signed_attrs (cms, signer);
1108 log_debug ("hashing signed attrs failed: %s\n",
1114 rc = gpgsm_create_cms_signature (ctrl, cl->cert,
1115 md, cl->hash_algo, &sigval);
1118 audit_log_cert (ctrl->audit, AUDIT_SIGNED_BY, cl->cert, rc);
1123 err = ksba_cms_set_sig_val (cms, signer, sigval);
1127 audit_log_cert (ctrl->audit, AUDIT_SIGNED_BY, cl->cert, err);
1128 log_error ("failed to store the signature: %s\n",
1129 gpg_strerror (err));
1135 /* write a status message */
1136 fpr = gpgsm_get_fingerprint_hexstring (cl->cert, GCRY_MD_SHA1);
1139 rc = gpg_error (GPG_ERR_ENOMEM);
1146 char *pkalgostr = gpgsm_pubkey_algo_string (cl->cert, NULL);
1147 log_info (_("%s/%s signature using %s key %s\n"),
1148 pubkey_algo_to_string (cl->pk_algo),
1149 gcry_md_algo_name (cl->hash_algo),
1153 buf = xtryasprintf ("%c %d %d 00 %s %s",
1160 rc = gpg_error_from_syserror ();
1167 gpgsm_status (ctrl, STATUS_SIG_CREATED, buf);
1169 audit_log_cert (ctrl->audit, AUDIT_SIGNED_BY, cl->cert, 0);
1174 while (stopreason != KSBA_SR_READY);
1176 rc = gnupg_ksba_finish_writer (b64writer);
1179 log_error ("write failed: %s\n", gpg_strerror (rc));
1183 if (binary_detached)
1188 rc = es_fclose_snatch (sig_fp, &blob, &bloblen);
1192 rc = write_detached_signature (ctrl, blob, bloblen, out_fp);
1199 audit_log (ctrl->audit, AUDIT_SIGNING_DONE);
1200 log_info ("signature created\n");
1204 log_error ("error creating signature: %s <%s>\n",
1205 gpg_strerror (rc), gpg_strsource (rc) );
1206 if (release_signerlist)
1207 gpgsm_release_certlist (signerlist);
1208 ksba_cms_release (cms);
1209 gnupg_ksba_destroy_writer (b64writer);
1211 gcry_md_close (data_md);