2005-11-13 20:07:06 +01:00
|
|
|
/* qualified.c - Routines related to qualified signatures
|
2007-05-19 00:58:59 +02:00
|
|
|
* Copyright (C) 2005, 2007 Free Software Foundation, Inc.
|
2005-11-13 20:07:06 +01:00
|
|
|
*
|
|
|
|
* This file is part of GnuPG.
|
|
|
|
*
|
|
|
|
* GnuPG is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
2007-07-04 21:49:40 +02:00
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
2005-11-13 20:07:06 +01:00
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* GnuPG is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2016-11-05 12:02:19 +01:00
|
|
|
* along with this program; if not, see <https://www.gnu.org/licenses/>.
|
2005-11-13 20:07:06 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include "gpgsm.h"
|
2017-03-07 12:21:23 +01:00
|
|
|
#include "../common/i18n.h"
|
2005-11-13 20:07:06 +01:00
|
|
|
#include <ksba.h>
|
|
|
|
|
|
|
|
|
|
|
|
/* We open the file only once and keep the open file pointer as well
|
|
|
|
as the name of the file here. Note that, a listname not equal to
|
2017-04-28 03:06:33 +02:00
|
|
|
NULL indicates that this module has been initialized and if the
|
2005-11-13 20:07:06 +01:00
|
|
|
LISTFP is also NULL, no list of qualified signatures exists. */
|
|
|
|
static char *listname;
|
2020-10-20 11:52:16 +02:00
|
|
|
static estream_t listfp;
|
2005-11-13 20:07:06 +01:00
|
|
|
|
|
|
|
|
|
|
|
/* Read the trustlist and return entry by entry. KEY must point to a
|
|
|
|
buffer of at least 41 characters. COUNTRY shall be a buffer of at
|
|
|
|
least 3 characters to receive the country code of that qualified
|
|
|
|
signature (i.e. "de" for German and "be" for Belgium).
|
|
|
|
|
|
|
|
Reading a valid entry returns 0, EOF is indicated by GPG_ERR_EOF
|
|
|
|
and any other error condition is indicated by the appropriate error
|
|
|
|
code. */
|
|
|
|
static gpg_error_t
|
|
|
|
read_list (char *key, char *country, int *lnr)
|
|
|
|
{
|
|
|
|
gpg_error_t err;
|
|
|
|
int c, i, j;
|
|
|
|
char *p, line[256];
|
|
|
|
|
|
|
|
*key = 0;
|
|
|
|
*country = 0;
|
|
|
|
|
|
|
|
if (!listname)
|
|
|
|
{
|
2017-09-01 21:19:26 +02:00
|
|
|
listname = make_filename (gnupg_sysconfdir (), "qualified.txt", NULL);
|
2020-10-20 11:52:16 +02:00
|
|
|
listfp = es_fopen (listname, "r");
|
2005-11-13 20:07:06 +01:00
|
|
|
if (!listfp && errno != ENOENT)
|
|
|
|
{
|
2006-09-14 18:50:33 +02:00
|
|
|
err = gpg_error_from_syserror ();
|
2012-06-05 19:29:22 +02:00
|
|
|
log_error (_("can't open '%s': %s\n"), listname, gpg_strerror (err));
|
2005-11-13 20:07:06 +01:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!listfp)
|
|
|
|
return gpg_error (GPG_ERR_EOF);
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2020-10-20 11:52:16 +02:00
|
|
|
if (!es_fgets (line, DIM(line)-1, listfp) )
|
2005-11-13 20:07:06 +01:00
|
|
|
{
|
2020-10-20 11:52:16 +02:00
|
|
|
if (es_feof (listfp))
|
2005-11-13 20:07:06 +01:00
|
|
|
return gpg_error (GPG_ERR_EOF);
|
2006-09-14 18:50:33 +02:00
|
|
|
return gpg_error_from_syserror ();
|
2005-11-13 20:07:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!*line || line[strlen(line)-1] != '\n')
|
|
|
|
{
|
|
|
|
/* Eat until end of line. */
|
2020-10-20 11:52:16 +02:00
|
|
|
while ((c = es_getc (listfp)) != EOF && c != '\n')
|
2005-11-13 20:07:06 +01:00
|
|
|
;
|
|
|
|
return gpg_error (*line? GPG_ERR_LINE_TOO_LONG
|
|
|
|
: GPG_ERR_INCOMPLETE_LINE);
|
|
|
|
}
|
|
|
|
++*lnr;
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2005-11-13 20:07:06 +01:00
|
|
|
/* Allow for empty lines and spaces */
|
|
|
|
for (p=line; spacep (p); p++)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
while (!*p || *p == '\n' || *p == '#');
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2005-11-13 20:07:06 +01:00
|
|
|
for (i=j=0; (p[i] == ':' || hexdigitp (p+i)) && j < 40; i++)
|
|
|
|
if ( p[i] != ':' )
|
|
|
|
key[j++] = p[i] >= 'a'? (p[i] & 0xdf): p[i];
|
|
|
|
key[j] = 0;
|
|
|
|
if (j != 40 || !(spacep (p+i) || p[i] == '\n'))
|
|
|
|
{
|
2012-06-05 19:29:22 +02:00
|
|
|
log_error (_("invalid formatted fingerprint in '%s', line %d\n"),
|
2005-11-13 20:07:06 +01:00
|
|
|
listname, *lnr);
|
|
|
|
return gpg_error (GPG_ERR_BAD_DATA);
|
|
|
|
}
|
2020-07-08 14:40:34 +02:00
|
|
|
log_assert (p[i]);
|
2005-11-13 20:07:06 +01:00
|
|
|
i++;
|
|
|
|
while (spacep (p+i))
|
|
|
|
i++;
|
2011-02-04 12:57:53 +01:00
|
|
|
if ( p[i] >= 'a' && p[i] <= 'z'
|
|
|
|
&& p[i+1] >= 'a' && p[i+1] <= 'z'
|
2005-11-13 20:07:06 +01:00
|
|
|
&& (spacep (p+i+2) || p[i+2] == '\n'))
|
|
|
|
{
|
|
|
|
country[0] = p[i];
|
|
|
|
country[1] = p[i+1];
|
|
|
|
country[2] = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-06-05 19:29:22 +02:00
|
|
|
log_error (_("invalid country code in '%s', line %d\n"), listname, *lnr);
|
2005-11-13 20:07:06 +01:00
|
|
|
return gpg_error (GPG_ERR_BAD_DATA);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Check whether the certificate CERT is included in the list of
|
|
|
|
qualified certificates. This list is similar to the "trustlist.txt"
|
|
|
|
as maintained by gpg-agent and includes fingerprints of root
|
|
|
|
certificates to be used for qualified (legally binding like
|
|
|
|
handwritten) signatures. We keep this list system wide and not
|
2011-02-04 12:57:53 +01:00
|
|
|
per user because it is not a decision of the user.
|
2005-11-13 20:07:06 +01:00
|
|
|
|
|
|
|
Returns: 0 if the certificate is included. GPG_ERR_NOT_FOUND if it
|
2006-03-21 10:56:47 +01:00
|
|
|
is not in the list or any other error (e.g. if no list of
|
|
|
|
qualified signatures is available. If COUNTRY has not been passed
|
2018-10-24 21:56:18 +02:00
|
|
|
as NULL a string with a maximum length of 2 will be copied into it;
|
2006-03-21 10:56:47 +01:00
|
|
|
thus the caller needs to provide a buffer of length 3. */
|
2005-11-13 20:07:06 +01:00
|
|
|
gpg_error_t
|
2006-03-21 10:56:47 +01:00
|
|
|
gpgsm_is_in_qualified_list (ctrl_t ctrl, ksba_cert_t cert, char *country)
|
2005-11-13 20:07:06 +01:00
|
|
|
{
|
|
|
|
gpg_error_t err;
|
|
|
|
char *fpr;
|
|
|
|
char key[41];
|
2006-03-21 10:56:47 +01:00
|
|
|
char mycountry[3];
|
2005-11-13 20:07:06 +01:00
|
|
|
int lnr = 0;
|
|
|
|
|
2008-10-20 15:53:23 +02:00
|
|
|
(void)ctrl;
|
|
|
|
|
2006-03-21 10:56:47 +01:00
|
|
|
if (country)
|
|
|
|
*country = 0;
|
|
|
|
|
2005-11-13 20:07:06 +01:00
|
|
|
fpr = gpgsm_get_fingerprint_hexstring (cert, GCRY_MD_SHA1);
|
|
|
|
if (!fpr)
|
|
|
|
return gpg_error (GPG_ERR_GENERAL);
|
|
|
|
|
|
|
|
if (listfp)
|
2010-03-24 13:15:30 +01:00
|
|
|
{
|
|
|
|
/* W32ce has no rewind, thus we use the equivalent code. */
|
2020-10-20 11:52:16 +02:00
|
|
|
es_fseek (listfp, 0, SEEK_SET);
|
|
|
|
es_clearerr (listfp);
|
2010-03-24 13:15:30 +01:00
|
|
|
}
|
2006-03-21 10:56:47 +01:00
|
|
|
while (!(err = read_list (key, mycountry, &lnr)))
|
2005-11-13 20:07:06 +01:00
|
|
|
{
|
|
|
|
if (!strcmp (key, fpr))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (gpg_err_code (err) == GPG_ERR_EOF)
|
|
|
|
err = gpg_error (GPG_ERR_NOT_FOUND);
|
|
|
|
|
2006-03-21 10:56:47 +01:00
|
|
|
if (!err && country)
|
|
|
|
strcpy (country, mycountry);
|
|
|
|
|
2005-11-13 20:07:06 +01:00
|
|
|
xfree (fpr);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* We know that CERT is a qualified certificate. Ask the user for
|
|
|
|
consent to actually create a signature using this certificate.
|
2015-11-16 12:41:46 +01:00
|
|
|
Returns: 0 for yes, GPG_ERR_CANCEL for no or any other error
|
2005-11-13 20:07:06 +01:00
|
|
|
code. */
|
|
|
|
gpg_error_t
|
|
|
|
gpgsm_qualified_consent (ctrl_t ctrl, ksba_cert_t cert)
|
|
|
|
{
|
|
|
|
gpg_error_t err;
|
|
|
|
char *name, *subject, *buffer, *p;
|
|
|
|
const char *s;
|
|
|
|
char *orig_codeset = NULL;
|
|
|
|
|
|
|
|
name = ksba_cert_get_subject (cert, 0);
|
|
|
|
if (!name)
|
|
|
|
return gpg_error (GPG_ERR_GENERAL);
|
|
|
|
subject = gpgsm_format_name2 (name, 0);
|
|
|
|
ksba_free (name); name = NULL;
|
|
|
|
|
2007-10-19 17:58:38 +02:00
|
|
|
orig_codeset = i18n_switchto_utf8 ();
|
2005-11-13 20:07:06 +01:00
|
|
|
|
|
|
|
if (asprintf (&name,
|
|
|
|
_("You are about to create a signature using your "
|
|
|
|
"certificate:\n"
|
|
|
|
"\"%s\"\n"
|
|
|
|
"This will create a qualified signature by law "
|
2005-11-23 10:05:45 +01:00
|
|
|
"equated to a handwritten signature.\n\n%s%s"
|
2005-11-13 20:07:06 +01:00
|
|
|
"Are you really sure that you want to do this?"),
|
2005-11-23 10:05:45 +01:00
|
|
|
subject? subject:"?",
|
2011-02-04 12:57:53 +01:00
|
|
|
opt.qualsig_approval?
|
2005-11-23 10:05:45 +01:00
|
|
|
"":
|
2005-11-23 13:38:38 +01:00
|
|
|
_("Note, that this software is not officially approved "
|
|
|
|
"to create or verify such signatures.\n"),
|
2005-11-23 10:05:45 +01:00
|
|
|
opt.qualsig_approval? "":"\n"
|
2005-11-13 20:07:06 +01:00
|
|
|
) < 0 )
|
2006-09-14 18:50:33 +02:00
|
|
|
err = gpg_error_from_syserror ();
|
2005-11-13 20:07:06 +01:00
|
|
|
else
|
|
|
|
err = 0;
|
|
|
|
|
2007-10-19 17:58:38 +02:00
|
|
|
i18n_switchback (orig_codeset);
|
2005-11-13 20:07:06 +01:00
|
|
|
xfree (subject);
|
|
|
|
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
buffer = p = xtrymalloc (strlen (name) * 3 + 1);
|
|
|
|
if (!buffer)
|
|
|
|
{
|
2006-09-14 18:50:33 +02:00
|
|
|
err = gpg_error_from_syserror ();
|
2005-11-13 20:07:06 +01:00
|
|
|
free (name);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
for (s=name; *s; s++)
|
|
|
|
{
|
|
|
|
if (*s < ' ' || *s == '+')
|
|
|
|
{
|
|
|
|
sprintf (p, "%%%02X", *(unsigned char *)s);
|
|
|
|
p += 3;
|
|
|
|
}
|
|
|
|
else if (*s == ' ')
|
|
|
|
*p++ = '+';
|
|
|
|
else
|
|
|
|
*p++ = *s;
|
|
|
|
}
|
|
|
|
*p = 0;
|
2011-02-04 12:57:53 +01:00
|
|
|
free (name);
|
2005-11-13 20:07:06 +01:00
|
|
|
|
|
|
|
|
2005-11-23 10:05:45 +01:00
|
|
|
err = gpgsm_agent_get_confirmation (ctrl, buffer);
|
|
|
|
|
|
|
|
xfree (buffer);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Popup a prompt to inform the user that the signature created is not
|
2006-02-14 14:34:23 +01:00
|
|
|
a qualified one. This is of course only done if we know that we
|
2005-11-23 10:05:45 +01:00
|
|
|
have been approved. */
|
|
|
|
gpg_error_t
|
|
|
|
gpgsm_not_qualified_warning (ctrl_t ctrl, ksba_cert_t cert)
|
|
|
|
{
|
|
|
|
gpg_error_t err;
|
|
|
|
char *name, *subject, *buffer, *p;
|
|
|
|
const char *s;
|
2007-10-19 17:58:38 +02:00
|
|
|
char *orig_codeset;
|
2005-11-23 10:05:45 +01:00
|
|
|
|
|
|
|
if (!opt.qualsig_approval)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
name = ksba_cert_get_subject (cert, 0);
|
|
|
|
if (!name)
|
|
|
|
return gpg_error (GPG_ERR_GENERAL);
|
|
|
|
subject = gpgsm_format_name2 (name, 0);
|
|
|
|
ksba_free (name); name = NULL;
|
|
|
|
|
2007-10-19 17:58:38 +02:00
|
|
|
orig_codeset = i18n_switchto_utf8 ();
|
2005-11-23 10:05:45 +01:00
|
|
|
|
|
|
|
if (asprintf (&name,
|
|
|
|
_("You are about to create a signature using your "
|
|
|
|
"certificate:\n"
|
|
|
|
"\"%s\"\n"
|
|
|
|
"Note, that this certificate will NOT create a "
|
|
|
|
"qualified signature!"),
|
|
|
|
subject? subject:"?") < 0 )
|
2006-09-14 18:50:33 +02:00
|
|
|
err = gpg_error_from_syserror ();
|
2005-11-23 10:05:45 +01:00
|
|
|
else
|
|
|
|
err = 0;
|
|
|
|
|
2007-10-19 17:58:38 +02:00
|
|
|
i18n_switchback (orig_codeset);
|
2005-11-23 10:05:45 +01:00
|
|
|
xfree (subject);
|
|
|
|
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
buffer = p = xtrymalloc (strlen (name) * 3 + 1);
|
|
|
|
if (!buffer)
|
|
|
|
{
|
2006-09-14 18:50:33 +02:00
|
|
|
err = gpg_error_from_syserror ();
|
2005-11-23 10:05:45 +01:00
|
|
|
free (name);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
for (s=name; *s; s++)
|
|
|
|
{
|
|
|
|
if (*s < ' ' || *s == '+')
|
|
|
|
{
|
|
|
|
sprintf (p, "%%%02X", *(unsigned char *)s);
|
|
|
|
p += 3;
|
|
|
|
}
|
|
|
|
else if (*s == ' ')
|
|
|
|
*p++ = '+';
|
|
|
|
else
|
|
|
|
*p++ = *s;
|
|
|
|
}
|
|
|
|
*p = 0;
|
2011-02-04 12:57:53 +01:00
|
|
|
free (name);
|
2005-11-23 10:05:45 +01:00
|
|
|
|
|
|
|
|
2005-11-13 20:07:06 +01:00
|
|
|
err = gpgsm_agent_get_confirmation (ctrl, buffer);
|
|
|
|
|
|
|
|
xfree (buffer);
|
|
|
|
return err;
|
|
|
|
}
|