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/>.
38 hash_data (int fd
, gcry_md_hd_t md
)
44 fp
= fdopen ( dup (fd
), "rb");
47 log_error ("fdopen(%d) failed: %s\n", fd
, strerror (errno
));
53 nread
= fread (buffer
, 1, DIM(buffer
), fp
);
54 gcry_md_write (md
, buffer
, nread
);
58 log_error ("read error on fd %d: %s\n", fd
, strerror (errno
));
63 hash_and_copy_data (int fd
, gcry_md_hd_t md
, ksba_writer_t writer
)
72 fp
= fdopen ( dup (fd
), "rb");
75 gpg_error_t tmperr
= gpg_error (gpg_err_code_from_errno (errno
));
76 log_error ("fdopen(%d) failed: %s\n", fd
, strerror (errno
));
82 nread
= fread (buffer
, 1, DIM(buffer
), fp
);
86 gcry_md_write (md
, buffer
, nread
);
87 err
= ksba_writer_write_octet_string (writer
, buffer
, nread
, 0);
90 log_error ("write failed: %s\n", gpg_strerror (err
));
98 rc
= gpg_error (gpg_err_code_from_errno (errno
));
99 log_error ("read error on fd %d: %s\n", fd
, strerror (errno
));
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
);
113 err
= ksba_writer_write_octet_string (writer
, NULL
, 0, 1);
116 log_error ("write failed: %s\n", gpg_strerror (err
));
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
)
132 ksba_cert_t cert
= NULL
;
138 return gpg_error (GPG_ERR_GENERAL
);
139 rc
= keydb_search_first (hd
);
148 rc
= keydb_get_cert (hd
, &cert
);
151 log_error ("keydb_get_cert failed: %s\n", gpg_strerror (rc
));
156 if (!gpgsm_cert_use_sign_p (cert
))
158 p
= gpgsm_get_keygrip_hexstring (cert
);
161 if (!gpgsm_agent_havekey (ctrl
, p
))
166 return 0; /* got it */
172 ksba_cert_release (cert
);
175 while (!(rc
= keydb_search_next (hd
)));
177 log_error ("keydb_search_next failed: %s\n", gpg_strerror (rc
));
179 ksba_cert_release (cert
);
186 get_default_signer (ctrl_t ctrl
)
188 KEYDB_SEARCH_DESC desc
;
189 ksba_cert_t cert
= NULL
;
190 KEYDB_HANDLE kh
= NULL
;
195 rc
= gpgsm_get_default_cert (ctrl
, &cert
);
199 log_debug ("failed to find default certificate: %s\n",
206 rc
= keydb_classify_name (opt
.local_user
, &desc
);
209 log_error ("failed to find default signer: %s\n", gpg_strerror (rc
));
217 rc
= keydb_search (kh
, &desc
, 1);
220 log_debug ("failed to find default certificate: rc=%d\n", rc
);
224 rc
= keydb_get_cert (kh
, &cert
);
227 log_debug ("failed to get cert: rc=%d\n", rc
);
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
239 add_certificate_list (ctrl_t ctrl
, ksba_cms_t cms
, ksba_cert_t cert
)
243 ksba_cert_t next
= NULL
;
247 ksba_cert_ref (cert
);
249 n
= ctrl
->include_certs
;
250 log_debug ("adding certificates at level %d\n", n
);
257 n
= 50; /* We better apply an upper bound */
259 /* First add my own certificate unless we don't want any certificate
263 if (not_root
&& gpgsm_is_root_cert (cert
))
266 err
= ksba_cms_add_cert (cms
, cert
);
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
275 while ( n
-- && !(rc
= gpgsm_walk_cert_chain (ctrl
, cert
, &next
)) )
277 if (not_root
&& gpgsm_is_root_cert (next
))
280 err
= ksba_cms_add_cert (cms
, next
);
281 ksba_cert_release (cert
);
282 cert
= next
; next
= NULL
;
286 ksba_cert_release (cert
);
288 return rc
== -1? 0: rc
;
291 ksba_cert_release (cert
);
292 log_error ("ksba_cms_add_cert failed: %s\n", gpg_strerror (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
)
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
;
320 ksba_isotime_t signed_at
;
322 int release_signerlist
= 0;
327 log_error (_("failed to allocated keyDB handle\n"));
328 rc
= gpg_error (GPG_ERR_GENERAL
);
332 ctrl
->pem_name
= "SIGNED MESSAGE";
333 rc
= gpgsm_create_writer (&b64writer
, ctrl
, out_fp
, NULL
, &writer
);
336 log_error ("can't create writer: %s\n", gpg_strerror (rc
));
340 err
= ksba_cms_new (&cms
);
347 err
= ksba_cms_set_reader_writer (cms
, NULL
, writer
);
350 log_debug ("ksba_cms_set_reader_writer failed: %s\n",
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
);
359 err
= ksba_cms_set_content_type (cms
, 1, KSBA_CT_DATA
);
362 log_debug ("ksba_cms_set_content_type failed: %s\n",
368 /* If no list of signers is given, use the default certificate. */
371 ksba_cert_t cert
= get_default_signer (ctrl
);
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
);
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
);
385 rc
= gpgsm_validate_chain (ctrl
, cert
, "", NULL
, 0, NULL
, 0, NULL
);
390 tmpfpr
= gpgsm_get_fingerprint_hexstring (cert
, 0);
391 gpgsm_status2 (ctrl
, STATUS_INV_SGNR
,
392 get_inv_recpsgnr_code (rc
), tmpfpr
, NULL
);
397 /* That one is fine - create signerlist. */
398 signerlist
= xtrycalloc (1, sizeof *signerlist
);
402 ksba_cert_release (cert
);
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
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
++)
418 if (opt
.forced_digest_algo
)
421 cl
->hash_algo
= opt
.forced_digest_algo
;
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;"
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";
449 cl
->hash_algo_oid
= oid
;
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
);
467 err
= ksba_cms_add_signer (cms
, cl
->cert
);
470 log_error ("ksba_cms_add_signer failed: %s\n", gpg_strerror (err
));
474 rc
= add_certificate_list (ctrl
, cms
, cl
->cert
);
477 log_error ("failed to store list of certificates: %s\n",
481 /* Set the hash algorithm we are going to use */
482 err
= ksba_cms_add_digest_algo (cms
, cl
->hash_algo_oid
);
485 log_debug ("ksba_cms_add_digest_algo failed: %s\n",
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
)
501 err
= ksba_cert_get_user_data (cl
->cert
, "is_qualified",
502 &buffer
, sizeof (buffer
), &buflen
);
505 log_error (_("checking for qualified certificate failed: %s\n"),
511 err
= gpgsm_qualified_consent (ctrl
, cl
->cert
);
513 err
= gpgsm_not_qualified_warning (ctrl
, cl
->cert
);
521 /* Prepare hashing (actually we are figuring out what we have set
523 rc
= gcry_md_open (&data_md
, 0, 0);
526 log_error ("md_open failed: %s\n", gpg_strerror (rc
));
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
);
537 log_error ("unknown hash algorithm `%s'\n", algoid
? algoid
:"?");
538 rc
= gpg_error (GPG_ERR_BUG
);
541 gcry_md_enable (data_md
, algo
);
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
548 unsigned char *digest
;
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
);
562 err
= ksba_cms_set_message_digest (cms
, signer
, digest
, digest_len
);
565 log_error ("ksba_cms_set_message_digest failed: %s\n",
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
);
579 log_error ("ksba_cms_set_signing_time failed: %s\n",
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
593 err
= ksba_cms_add_smime_capability (cms
, "2.16.840.1.101.3.4.1.2", NULL
, 0);
595 err
= ksba_cms_add_smime_capability (cms
, "1.2.840.113549.3.7", NULL
, 0);
598 log_error ("ksba_cms_add_smime_capability failed: %s\n",
604 /* Main building loop. */
607 err
= ksba_cms_build (cms
, &stopreason
);
610 log_debug ("ksba_cms_build failed: %s\n", gpg_strerror (err
));
615 if (stopreason
== KSBA_SR_BEGIN_DATA
)
617 /* Hash the data and store the message digest. */
618 unsigned char *digest
;
623 rc
= hash_and_copy_data (data_fd
, data_md
, writer
);
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
);
636 err
= ksba_cms_set_message_digest (cms
, signer
,
640 log_error ("ksba_cms_set_message_digest failed: %s\n",
647 else if (stopreason
== KSBA_SR_NEED_SIG
)
649 /* Compute the signature for all signers. */
652 rc
= gcry_md_open (&md
, 0, 0);
655 log_error ("md_open failed: %s\n", gpg_strerror (rc
));
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
;
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
);
678 log_debug ("hashing signed attrs failed: %s\n",
684 rc
= gpgsm_create_cms_signature (ctrl
, cl
->cert
,
685 md
, cl
->hash_algo
, &sigval
);
692 err
= ksba_cms_set_sig_val (cms
, signer
, sigval
);
696 log_error ("failed to store the signature: %s\n",
703 /* write a status message */
704 fpr
= gpgsm_get_fingerprint_hexstring (cl
->cert
, GCRY_MD_SHA1
);
707 rc
= gpg_error (GPG_ERR_ENOMEM
);
712 int pkalgo
= gpgsm_get_key_algo_info (cl
->cert
, NULL
);
713 rc
= asprintf (&buf
, "%c %d %d 00 %s %s",
723 rc
= gpg_error (GPG_ERR_ENOMEM
);
728 gpgsm_status (ctrl
, STATUS_SIG_CREATED
, buf
);
729 free (buf
); /* yes, we must use the regular free() here */
735 while (stopreason
!= KSBA_SR_READY
);
737 rc
= gpgsm_finish_writer (b64writer
);
740 log_error ("write failed: %s\n", gpg_strerror (rc
));
744 log_info ("signature created\n");
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
);
756 gcry_md_close (data_md
);