2007-08-28 19:48:13 +02:00
|
|
|
|
/* genkey.c - Generate a keypair
|
2010-06-17 17:44:44 +02:00
|
|
|
|
* Copyright (C) 2002, 2003, 2004, 2007, 2010 Free Software Foundation, Inc.
|
2015-06-18 05:10:47 +02:00
|
|
|
|
* Copyright (C) 2015 g10 Code GmbH.
|
2003-08-05 19:11:04 +02: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
|
2003-08-05 19:11:04 +02: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/>.
|
2003-08-05 19:11:04 +02:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <ctype.h>
|
|
|
|
|
|
|
|
|
|
#include "agent.h"
|
agent: Resolve conflict of util.h.
* agent/Makefile.am (AM_CPPFLAGS): Remove -I$(top_srcdir)/common.
* agent/call-pinentry.c, agent/call-scd.c: Follow the change.
* agent/command-ssh.c, agent/command.c, agent/cvt-openpgp.c: Ditto.
* agent/divert-scd.c, agent/findkey.c, agent/genkey.c: Ditto.
* agent/gpg-agent.c, agent/pksign.c, agent/preset-passphrase.c: Ditto.
* agent/protect-tool.c, agent/protect.c, agent/trustlist.c: Ditto.
* agent/w32main.c: Ditto.
--
For openpty function, we need to include util.h on some OS.
We also have util.h in common/, so this change is needed.
Signed-off-by: NIIBE Yutaka <gniibe@fsij.org>
2017-03-07 11:22:48 +01:00
|
|
|
|
#include "../common/i18n.h"
|
|
|
|
|
#include "../common/sysutils.h"
|
2003-08-05 19:11:04 +02:00
|
|
|
|
|
2024-01-22 13:22:44 +01:00
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
clear_ephemeral_keys (ctrl_t ctrl)
|
|
|
|
|
{
|
|
|
|
|
while (ctrl->ephemeral_keys)
|
|
|
|
|
{
|
|
|
|
|
ephemeral_private_key_t next = ctrl->ephemeral_keys->next;
|
|
|
|
|
if (ctrl->ephemeral_keys->keybuf)
|
|
|
|
|
{
|
|
|
|
|
wipememory (ctrl->ephemeral_keys->keybuf,
|
|
|
|
|
ctrl->ephemeral_keys->keybuflen);
|
|
|
|
|
xfree (ctrl->ephemeral_keys->keybuf);
|
|
|
|
|
}
|
|
|
|
|
xfree (ctrl->ephemeral_keys);
|
|
|
|
|
ctrl->ephemeral_keys = next;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Store the key either to a file, or in ctrl->ephemeral_mode in the
|
|
|
|
|
* session data. */
|
|
|
|
|
static gpg_error_t
|
|
|
|
|
store_key (ctrl_t ctrl, gcry_sexp_t private,
|
|
|
|
|
const char *passphrase, int force,
|
2020-08-17 14:21:00 +02:00
|
|
|
|
unsigned long s2k_count, time_t timestamp)
|
2003-08-05 19:11:04 +02:00
|
|
|
|
{
|
2024-01-22 13:22:44 +01:00
|
|
|
|
gpg_error_t err;
|
2005-06-16 10:12:03 +02:00
|
|
|
|
unsigned char *buf;
|
2003-08-05 19:11:04 +02:00
|
|
|
|
size_t len;
|
2024-01-22 13:22:44 +01:00
|
|
|
|
unsigned char grip[KEYGRIP_LEN];
|
2011-02-04 12:57:53 +01:00
|
|
|
|
|
2003-08-05 19:11:04 +02:00
|
|
|
|
if ( !gcry_pk_get_keygrip (private, grip) )
|
|
|
|
|
{
|
|
|
|
|
log_error ("can't calculate keygrip\n");
|
|
|
|
|
return gpg_error (GPG_ERR_GENERAL);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
len = gcry_sexp_sprint (private, GCRYSEXP_FMT_CANON, NULL, 0);
|
2019-05-14 10:31:46 +02:00
|
|
|
|
log_assert (len);
|
2003-08-05 19:11:04 +02:00
|
|
|
|
buf = gcry_malloc_secure (len);
|
|
|
|
|
if (!buf)
|
2024-01-22 13:22:44 +01:00
|
|
|
|
{
|
|
|
|
|
err = gpg_error_from_syserror ();
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
2003-08-05 19:11:04 +02:00
|
|
|
|
len = gcry_sexp_sprint (private, GCRYSEXP_FMT_CANON, buf, len);
|
2019-05-14 10:31:46 +02:00
|
|
|
|
log_assert (len);
|
2003-08-05 19:11:04 +02:00
|
|
|
|
|
|
|
|
|
if (passphrase)
|
|
|
|
|
{
|
|
|
|
|
unsigned char *p;
|
|
|
|
|
|
2024-01-22 13:22:44 +01:00
|
|
|
|
err = agent_protect (buf, passphrase, &p, &len, s2k_count);
|
|
|
|
|
if (err)
|
|
|
|
|
goto leave;
|
2003-08-05 19:11:04 +02:00
|
|
|
|
xfree (buf);
|
|
|
|
|
buf = p;
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-22 13:22:44 +01:00
|
|
|
|
if (ctrl->ephemeral_mode)
|
|
|
|
|
{
|
|
|
|
|
ephemeral_private_key_t ek;
|
|
|
|
|
|
|
|
|
|
for (ek = ctrl->ephemeral_keys; ek; ek = ek->next)
|
|
|
|
|
if (!memcmp (ek->grip, grip, KEYGRIP_LEN))
|
|
|
|
|
break;
|
|
|
|
|
if (!ek)
|
|
|
|
|
{
|
|
|
|
|
ek = xtrycalloc (1, sizeof *ek);
|
|
|
|
|
if (!ek)
|
|
|
|
|
{
|
|
|
|
|
err = gpg_error_from_syserror ();
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
memcpy (ek->grip, grip, KEYGRIP_LEN);
|
|
|
|
|
ek->next = ctrl->ephemeral_keys;
|
|
|
|
|
ctrl->ephemeral_keys = ek;
|
|
|
|
|
}
|
|
|
|
|
if (ek->keybuf)
|
|
|
|
|
{
|
|
|
|
|
wipememory (ek->keybuf, ek->keybuflen);
|
|
|
|
|
xfree (ek->keybuf);
|
|
|
|
|
}
|
|
|
|
|
ek->keybuf = buf;
|
|
|
|
|
buf = NULL;
|
|
|
|
|
ek->keybuflen = len;
|
2024-05-28 16:37:25 +02:00
|
|
|
|
err = 0;
|
2024-01-22 13:22:44 +01:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
err = agent_write_private_key (ctrl, grip, buf, len, force,
|
|
|
|
|
NULL, NULL, NULL, timestamp);
|
|
|
|
|
|
|
|
|
|
if (!err)
|
|
|
|
|
{
|
|
|
|
|
char hexgrip[2*KEYGRIP_LEN+1];
|
|
|
|
|
|
|
|
|
|
bin2hex (grip, KEYGRIP_LEN, hexgrip);
|
|
|
|
|
agent_write_status (ctrl, "KEYGRIP", hexgrip, NULL);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
leave:
|
2003-08-05 19:11:04 +02:00
|
|
|
|
xfree (buf);
|
2024-01-22 13:22:44 +01:00
|
|
|
|
return err;
|
2003-08-05 19:11:04 +02:00
|
|
|
|
}
|
|
|
|
|
|
2007-01-25 09:30:47 +01:00
|
|
|
|
|
2007-08-27 20:10:27 +02:00
|
|
|
|
/* Count the number of non-alpha characters in S. Control characters
|
|
|
|
|
and non-ascii characters are not considered. */
|
|
|
|
|
static size_t
|
|
|
|
|
nonalpha_count (const char *s)
|
|
|
|
|
{
|
|
|
|
|
size_t n;
|
|
|
|
|
|
|
|
|
|
for (n=0; *s; s++)
|
|
|
|
|
if (isascii (*s) && ( isdigit (*s) || ispunct (*s) ))
|
|
|
|
|
n++;
|
|
|
|
|
|
|
|
|
|
return n;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Check PW against a list of pattern. Return 0 if PW does not match
|
2021-08-13 13:42:31 +02:00
|
|
|
|
these pattern. If CHECK_CONSTRAINTS_NEW_SYMKEY is set in flags and
|
|
|
|
|
--check-sym-passphrase-pattern has been configured, use the pattern
|
|
|
|
|
file from that option. */
|
2007-08-27 20:10:27 +02:00
|
|
|
|
static int
|
2021-08-13 13:42:31 +02:00
|
|
|
|
do_check_passphrase_pattern (ctrl_t ctrl, const char *pw, unsigned int flags)
|
2007-08-27 20:10:27 +02:00
|
|
|
|
{
|
|
|
|
|
gpg_error_t err = 0;
|
|
|
|
|
const char *pgmname = gnupg_module_name (GNUPG_MODULE_NAME_CHECK_PATTERN);
|
2022-04-22 06:33:45 +02:00
|
|
|
|
estream_t stream_to_check_pattern = NULL;
|
2007-08-27 20:10:27 +02:00
|
|
|
|
const char *argv[10];
|
Use gpgrt_process_spawn API from libgpg-error.
* agent/genkey.c (do_check_passphrase_pattern): Use the gpgrt API.
* common/asshelp.c (start_new_service): Ditto.
* common/exechelp.h: Remove gnupg_process_spawn API.
* common/exechelp-posix.c: Remove gnupg_process_spawn implementation.
* common/exechelp-w32.c: Likewise.
* common/exectool.c (gnupg_exec_tool_stream): Use the gpgrt API.
* common/t-exechelp.c (test_pipe_stream): Remove.
* dirmngr/ldap-wrapper.c (destroy_wrapper, ldap_reaper_thread): Use
the gpgrt API.
(ldap_wrapper_connection_cleanup, ldap_wrapper): Ditto.
* dirmngr/ldap.c, g10/call-keyboxd.c: No need to include exechelp.h.
* g10/photoid.c (run_with_pipe, show_photo): Use the gpgrt API.
* g13/be-encfs.c (run_umount_helper, run_encfs_tool): Ditto.
* g13/g13.c, g13/mount.c, g13/runner.c: No need to include exechelp.h.
* scd/apdu.c: No need to include exechelp.h.
* scd/app.c (report_change): Use the gpgrt API.
* sm/export.c, sm/import.c: No need to include exechelp.h.
* tests/gpgscm/ffi.c (proc_object_finalize, proc_wrap)
(do_process_spawn_io, do_process_spawn_fd, do_process_wait): Use the
gpgrt API.
* tools/gpg-auth.c: No need to include exechelp.h.
* tools/gpg-card.c (cmd_gpg): Use the gpgrt API.
* tools/gpg-connect-agent.c: No need to include exechelp.h.
* tools/gpg-mail-tube.c (mail_tube_encrypt, prepare_for_appimage)
(start_gpg_encrypt): Use the gpgrt API.
* tools/gpgconf-comp.c (gpg_agent_runtime_change)
(scdaemon_runtime_change, tpm2daemon_runtime_change)
(dirmngr_runtime_change, keyboxd_runtime_change)
(gc_component_launch, gc_component_check_options)
(retrieve_options_from_program): Ditto.
* tools/gpgconf.c (show_versions_via_dirmngr): Ditto.
* tools/gpgtar-create.c (gpgtar_create): Ditto.
* tools/gpgtar-extract.c (gpgtar_extract): Ditto.
* tools/gpgtar-list.c (gpgtar_list): Ditto.
--
GnuPG-bug-id: 7192
Signed-off-by: NIIBE Yutaka <gniibe@fsij.org>
2024-07-09 02:39:15 +02:00
|
|
|
|
gpgrt_process_t proc;
|
2007-08-27 20:10:27 +02:00
|
|
|
|
int result, i;
|
2021-08-13 13:42:31 +02:00
|
|
|
|
const char *pattern;
|
2021-08-18 19:21:22 +02:00
|
|
|
|
char *patternfname;
|
2007-08-27 20:10:27 +02:00
|
|
|
|
|
2008-10-20 15:53:23 +02:00
|
|
|
|
(void)ctrl;
|
|
|
|
|
|
2021-08-13 13:42:31 +02:00
|
|
|
|
pattern = opt.check_passphrase_pattern;
|
|
|
|
|
if ((flags & CHECK_CONSTRAINTS_NEW_SYMKEY)
|
|
|
|
|
&& opt.check_sym_passphrase_pattern)
|
|
|
|
|
pattern = opt.check_sym_passphrase_pattern;
|
|
|
|
|
if (!pattern)
|
|
|
|
|
return 1; /* Oops - Assume password should not be used */
|
|
|
|
|
|
2021-08-18 19:21:22 +02:00
|
|
|
|
if (strchr (pattern, '/') || strchr (pattern, '\\')
|
|
|
|
|
|| (*pattern == '~' && pattern[1] == '/'))
|
|
|
|
|
patternfname = make_absfilename_try (pattern, NULL);
|
|
|
|
|
else
|
|
|
|
|
patternfname = make_filename_try (gnupg_sysconfdir (), pattern, NULL);
|
|
|
|
|
if (!patternfname)
|
|
|
|
|
{
|
|
|
|
|
log_error ("error making filename from '%s': %s\n",
|
|
|
|
|
pattern, gpg_strerror (gpg_error_from_syserror ()));
|
|
|
|
|
return 1; /* Do not pass the check. */
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Make debugging a broken config easier by printing a useful error
|
|
|
|
|
* message. */
|
|
|
|
|
if (gnupg_access (patternfname, F_OK))
|
|
|
|
|
{
|
|
|
|
|
log_error ("error accessing '%s': %s\n",
|
|
|
|
|
patternfname, gpg_strerror (gpg_error_from_syserror ()));
|
|
|
|
|
xfree (patternfname);
|
|
|
|
|
return 1; /* Do not pass the check. */
|
|
|
|
|
}
|
|
|
|
|
|
2007-08-27 20:10:27 +02:00
|
|
|
|
i = 0;
|
|
|
|
|
argv[i++] = "--null";
|
|
|
|
|
argv[i++] = "--",
|
2021-08-18 19:21:22 +02:00
|
|
|
|
argv[i++] = patternfname,
|
2007-08-27 20:10:27 +02:00
|
|
|
|
argv[i] = NULL;
|
2019-05-14 10:31:46 +02:00
|
|
|
|
log_assert (i < sizeof argv);
|
2007-08-27 20:10:27 +02:00
|
|
|
|
|
Use gpgrt_process_spawn API from libgpg-error.
* agent/genkey.c (do_check_passphrase_pattern): Use the gpgrt API.
* common/asshelp.c (start_new_service): Ditto.
* common/exechelp.h: Remove gnupg_process_spawn API.
* common/exechelp-posix.c: Remove gnupg_process_spawn implementation.
* common/exechelp-w32.c: Likewise.
* common/exectool.c (gnupg_exec_tool_stream): Use the gpgrt API.
* common/t-exechelp.c (test_pipe_stream): Remove.
* dirmngr/ldap-wrapper.c (destroy_wrapper, ldap_reaper_thread): Use
the gpgrt API.
(ldap_wrapper_connection_cleanup, ldap_wrapper): Ditto.
* dirmngr/ldap.c, g10/call-keyboxd.c: No need to include exechelp.h.
* g10/photoid.c (run_with_pipe, show_photo): Use the gpgrt API.
* g13/be-encfs.c (run_umount_helper, run_encfs_tool): Ditto.
* g13/g13.c, g13/mount.c, g13/runner.c: No need to include exechelp.h.
* scd/apdu.c: No need to include exechelp.h.
* scd/app.c (report_change): Use the gpgrt API.
* sm/export.c, sm/import.c: No need to include exechelp.h.
* tests/gpgscm/ffi.c (proc_object_finalize, proc_wrap)
(do_process_spawn_io, do_process_spawn_fd, do_process_wait): Use the
gpgrt API.
* tools/gpg-auth.c: No need to include exechelp.h.
* tools/gpg-card.c (cmd_gpg): Use the gpgrt API.
* tools/gpg-connect-agent.c: No need to include exechelp.h.
* tools/gpg-mail-tube.c (mail_tube_encrypt, prepare_for_appimage)
(start_gpg_encrypt): Use the gpgrt API.
* tools/gpgconf-comp.c (gpg_agent_runtime_change)
(scdaemon_runtime_change, tpm2daemon_runtime_change)
(dirmngr_runtime_change, keyboxd_runtime_change)
(gc_component_launch, gc_component_check_options)
(retrieve_options_from_program): Ditto.
* tools/gpgconf.c (show_versions_via_dirmngr): Ditto.
* tools/gpgtar-create.c (gpgtar_create): Ditto.
* tools/gpgtar-extract.c (gpgtar_extract): Ditto.
* tools/gpgtar-list.c (gpgtar_list): Ditto.
--
GnuPG-bug-id: 7192
Signed-off-by: NIIBE Yutaka <gniibe@fsij.org>
2024-07-09 02:39:15 +02:00
|
|
|
|
if (gpgrt_process_spawn (pgmname, argv,
|
|
|
|
|
GPGRT_PROCESS_STDIN_PIPE,
|
2024-05-31 08:36:39 +02:00
|
|
|
|
NULL, &proc))
|
2007-08-27 20:10:27 +02:00
|
|
|
|
result = 1; /* Execute error - assume password should no be used. */
|
|
|
|
|
else
|
2022-04-22 06:33:45 +02:00
|
|
|
|
{
|
common,agent,gpg,dirmngr,g13,scd,tests,tools: New spawn function.
* common/exechelp-posix.c (do_exec, gnupg_spawn_process): Remove.
(check_syscall_func, pre_syscall, post_syscall) : New.
(do_create_socketpair, posix_open_null, call_spawn_cb): New.
(my_exec, spawn_detached, gnupg_spawn_helper): New.
(gnupg_process_spawn, process_kill, gnupg_process_terminate): New.
(gnupg_process_get_fds, gnupg_process_get_streams): New.
(process_vctl, gnupg_process_ctl): New.
(gnupg_process_wait, gnupg_process_release): New.
(gnupg_process_wait_list): New.
* common/exechelp-w32.c: Add definition of _WIN32_WINNT as 0x600.
(check_syscall_func, pre_syscall, post_syscall): New.
(gnupg_spawn_process): Remove.
(check_windows_version): New.
(spawn_detached, gnupg_spawn_helper, gnupg_process_spawn): New.
(gnupg_process_get_fds, gnupg_process_get_streams): New.
(process_kill, process_vctl, gnupg_process_ctl): New.
(gnupg_process_wait, gnupg_process_terminate): New.
(gnupg_process_release, gnupg_process_wait_list): New.
* common/exechelp.h: Re-write for new API.
* common/exectool.c (gnupg_exec_tool_stream): Follow the change.
* common/asshelp.c (start_new_service): Likewise.
* agent/genkey.c (do_check_passphrase_pattern): Likewise.
* dirmngr/ldap-wrapper.c (struct wrapper_context_s): Use PROC.
(destroy_wrapper): Follow the change of API.
(read_log_data): Follow the change of API, use printable_pid.
(ldap_reaper_thread, ldap_wrapper_release_context): Likewise.
(ldap_wrapper_connection_cleanup, ldap_wrapper): Likewise.
* g10/photoid.c (run_with_pipe): Follow the change of API.
(show_photo): Likewise.
* g13/be-encfs.c (run_umount_helper): Likewise.
(run_encfs_tool): Likewise.
* g13/g13.c: Add including ./common/exechelp.h.
* g13/mount.c: Likewise.
* g13/runner.c: Follow the change of API.
* g13/runner.h: Follow the change of API.
* scd/app.c (setup_env): New.
(report_change): Follow the change of API.
* tests/gpgscm/ffi.c (proc_object_finalize): New.
(proc_object_to_string): New.
(proc_wrap, proc_unwrap): New.
(do_spawn_process): Remove.
(do_process_spawn): New.
(setup_std_fds): New.
(do_spawn_process_fd): Remove.
(do_process_spawn_fd): New.
(do_wait_process): Remove.
(do_process_wait): New.
(do_wait_processes): Remove.
* tests/gpgscm/t-child.scm: Follow the change of API.
* tests/gpgscm/tests.scm: Likewise.
* tests/openpgp/defs.scm: Likewise.
* tests/tpm2dtests/defs.scm: Likewise.
* tools/gpg-card.c: Likewise.
* tools/gpgconf-comp.c: Likewise.
* tools/gpgconf.c: Likewise.
* tools/gpgtar-create.c: Likewise.
* tools/gpgtar-extract.c: Likewise.
* tools/gpgtar-list.c: Likewise.
--
GnuPG-bug-id: 6275
Signed-off-by: NIIBE Yutaka <gniibe@fsij.org>
2023-05-11 12:18:21 +02:00
|
|
|
|
int status;
|
|
|
|
|
|
Use gpgrt_process_spawn API from libgpg-error.
* agent/genkey.c (do_check_passphrase_pattern): Use the gpgrt API.
* common/asshelp.c (start_new_service): Ditto.
* common/exechelp.h: Remove gnupg_process_spawn API.
* common/exechelp-posix.c: Remove gnupg_process_spawn implementation.
* common/exechelp-w32.c: Likewise.
* common/exectool.c (gnupg_exec_tool_stream): Use the gpgrt API.
* common/t-exechelp.c (test_pipe_stream): Remove.
* dirmngr/ldap-wrapper.c (destroy_wrapper, ldap_reaper_thread): Use
the gpgrt API.
(ldap_wrapper_connection_cleanup, ldap_wrapper): Ditto.
* dirmngr/ldap.c, g10/call-keyboxd.c: No need to include exechelp.h.
* g10/photoid.c (run_with_pipe, show_photo): Use the gpgrt API.
* g13/be-encfs.c (run_umount_helper, run_encfs_tool): Ditto.
* g13/g13.c, g13/mount.c, g13/runner.c: No need to include exechelp.h.
* scd/apdu.c: No need to include exechelp.h.
* scd/app.c (report_change): Use the gpgrt API.
* sm/export.c, sm/import.c: No need to include exechelp.h.
* tests/gpgscm/ffi.c (proc_object_finalize, proc_wrap)
(do_process_spawn_io, do_process_spawn_fd, do_process_wait): Use the
gpgrt API.
* tools/gpg-auth.c: No need to include exechelp.h.
* tools/gpg-card.c (cmd_gpg): Use the gpgrt API.
* tools/gpg-connect-agent.c: No need to include exechelp.h.
* tools/gpg-mail-tube.c (mail_tube_encrypt, prepare_for_appimage)
(start_gpg_encrypt): Use the gpgrt API.
* tools/gpgconf-comp.c (gpg_agent_runtime_change)
(scdaemon_runtime_change, tpm2daemon_runtime_change)
(dirmngr_runtime_change, keyboxd_runtime_change)
(gc_component_launch, gc_component_check_options)
(retrieve_options_from_program): Ditto.
* tools/gpgconf.c (show_versions_via_dirmngr): Ditto.
* tools/gpgtar-create.c (gpgtar_create): Ditto.
* tools/gpgtar-extract.c (gpgtar_extract): Ditto.
* tools/gpgtar-list.c (gpgtar_list): Ditto.
--
GnuPG-bug-id: 7192
Signed-off-by: NIIBE Yutaka <gniibe@fsij.org>
2024-07-09 02:39:15 +02:00
|
|
|
|
gpgrt_process_get_streams (proc, 0, &stream_to_check_pattern,
|
common,agent,gpg,dirmngr,g13,scd,tests,tools: New spawn function.
* common/exechelp-posix.c (do_exec, gnupg_spawn_process): Remove.
(check_syscall_func, pre_syscall, post_syscall) : New.
(do_create_socketpair, posix_open_null, call_spawn_cb): New.
(my_exec, spawn_detached, gnupg_spawn_helper): New.
(gnupg_process_spawn, process_kill, gnupg_process_terminate): New.
(gnupg_process_get_fds, gnupg_process_get_streams): New.
(process_vctl, gnupg_process_ctl): New.
(gnupg_process_wait, gnupg_process_release): New.
(gnupg_process_wait_list): New.
* common/exechelp-w32.c: Add definition of _WIN32_WINNT as 0x600.
(check_syscall_func, pre_syscall, post_syscall): New.
(gnupg_spawn_process): Remove.
(check_windows_version): New.
(spawn_detached, gnupg_spawn_helper, gnupg_process_spawn): New.
(gnupg_process_get_fds, gnupg_process_get_streams): New.
(process_kill, process_vctl, gnupg_process_ctl): New.
(gnupg_process_wait, gnupg_process_terminate): New.
(gnupg_process_release, gnupg_process_wait_list): New.
* common/exechelp.h: Re-write for new API.
* common/exectool.c (gnupg_exec_tool_stream): Follow the change.
* common/asshelp.c (start_new_service): Likewise.
* agent/genkey.c (do_check_passphrase_pattern): Likewise.
* dirmngr/ldap-wrapper.c (struct wrapper_context_s): Use PROC.
(destroy_wrapper): Follow the change of API.
(read_log_data): Follow the change of API, use printable_pid.
(ldap_reaper_thread, ldap_wrapper_release_context): Likewise.
(ldap_wrapper_connection_cleanup, ldap_wrapper): Likewise.
* g10/photoid.c (run_with_pipe): Follow the change of API.
(show_photo): Likewise.
* g13/be-encfs.c (run_umount_helper): Likewise.
(run_encfs_tool): Likewise.
* g13/g13.c: Add including ./common/exechelp.h.
* g13/mount.c: Likewise.
* g13/runner.c: Follow the change of API.
* g13/runner.h: Follow the change of API.
* scd/app.c (setup_env): New.
(report_change): Follow the change of API.
* tests/gpgscm/ffi.c (proc_object_finalize): New.
(proc_object_to_string): New.
(proc_wrap, proc_unwrap): New.
(do_spawn_process): Remove.
(do_process_spawn): New.
(setup_std_fds): New.
(do_spawn_process_fd): Remove.
(do_process_spawn_fd): New.
(do_wait_process): Remove.
(do_process_wait): New.
(do_wait_processes): Remove.
* tests/gpgscm/t-child.scm: Follow the change of API.
* tests/gpgscm/tests.scm: Likewise.
* tests/openpgp/defs.scm: Likewise.
* tests/tpm2dtests/defs.scm: Likewise.
* tools/gpg-card.c: Likewise.
* tools/gpgconf-comp.c: Likewise.
* tools/gpgconf.c: Likewise.
* tools/gpgtar-create.c: Likewise.
* tools/gpgtar-extract.c: Likewise.
* tools/gpgtar-list.c: Likewise.
--
GnuPG-bug-id: 6275
Signed-off-by: NIIBE Yutaka <gniibe@fsij.org>
2023-05-11 12:18:21 +02:00
|
|
|
|
NULL, NULL);
|
|
|
|
|
|
2022-04-22 06:33:45 +02:00
|
|
|
|
es_set_binary (stream_to_check_pattern);
|
|
|
|
|
if (es_fwrite (pw, strlen (pw), 1, stream_to_check_pattern) != 1)
|
|
|
|
|
{
|
|
|
|
|
err = gpg_error_from_syserror ();
|
|
|
|
|
log_error (_("error writing to pipe: %s\n"), gpg_strerror (err));
|
|
|
|
|
result = 1; /* Error - assume password should not be used. */
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
es_fflush (stream_to_check_pattern);
|
|
|
|
|
es_fclose (stream_to_check_pattern);
|
Use gpgrt_process_spawn API from libgpg-error.
* agent/genkey.c (do_check_passphrase_pattern): Use the gpgrt API.
* common/asshelp.c (start_new_service): Ditto.
* common/exechelp.h: Remove gnupg_process_spawn API.
* common/exechelp-posix.c: Remove gnupg_process_spawn implementation.
* common/exechelp-w32.c: Likewise.
* common/exectool.c (gnupg_exec_tool_stream): Use the gpgrt API.
* common/t-exechelp.c (test_pipe_stream): Remove.
* dirmngr/ldap-wrapper.c (destroy_wrapper, ldap_reaper_thread): Use
the gpgrt API.
(ldap_wrapper_connection_cleanup, ldap_wrapper): Ditto.
* dirmngr/ldap.c, g10/call-keyboxd.c: No need to include exechelp.h.
* g10/photoid.c (run_with_pipe, show_photo): Use the gpgrt API.
* g13/be-encfs.c (run_umount_helper, run_encfs_tool): Ditto.
* g13/g13.c, g13/mount.c, g13/runner.c: No need to include exechelp.h.
* scd/apdu.c: No need to include exechelp.h.
* scd/app.c (report_change): Use the gpgrt API.
* sm/export.c, sm/import.c: No need to include exechelp.h.
* tests/gpgscm/ffi.c (proc_object_finalize, proc_wrap)
(do_process_spawn_io, do_process_spawn_fd, do_process_wait): Use the
gpgrt API.
* tools/gpg-auth.c: No need to include exechelp.h.
* tools/gpg-card.c (cmd_gpg): Use the gpgrt API.
* tools/gpg-connect-agent.c: No need to include exechelp.h.
* tools/gpg-mail-tube.c (mail_tube_encrypt, prepare_for_appimage)
(start_gpg_encrypt): Use the gpgrt API.
* tools/gpgconf-comp.c (gpg_agent_runtime_change)
(scdaemon_runtime_change, tpm2daemon_runtime_change)
(dirmngr_runtime_change, keyboxd_runtime_change)
(gc_component_launch, gc_component_check_options)
(retrieve_options_from_program): Ditto.
* tools/gpgconf.c (show_versions_via_dirmngr): Ditto.
* tools/gpgtar-create.c (gpgtar_create): Ditto.
* tools/gpgtar-extract.c (gpgtar_extract): Ditto.
* tools/gpgtar-list.c (gpgtar_list): Ditto.
--
GnuPG-bug-id: 7192
Signed-off-by: NIIBE Yutaka <gniibe@fsij.org>
2024-07-09 02:39:15 +02:00
|
|
|
|
gpgrt_process_wait (proc, 1);
|
|
|
|
|
gpgrt_process_ctl (proc, GPGRT_PROCESS_GET_EXIT_ID, &status);
|
common,agent,gpg,dirmngr,g13,scd,tests,tools: New spawn function.
* common/exechelp-posix.c (do_exec, gnupg_spawn_process): Remove.
(check_syscall_func, pre_syscall, post_syscall) : New.
(do_create_socketpair, posix_open_null, call_spawn_cb): New.
(my_exec, spawn_detached, gnupg_spawn_helper): New.
(gnupg_process_spawn, process_kill, gnupg_process_terminate): New.
(gnupg_process_get_fds, gnupg_process_get_streams): New.
(process_vctl, gnupg_process_ctl): New.
(gnupg_process_wait, gnupg_process_release): New.
(gnupg_process_wait_list): New.
* common/exechelp-w32.c: Add definition of _WIN32_WINNT as 0x600.
(check_syscall_func, pre_syscall, post_syscall): New.
(gnupg_spawn_process): Remove.
(check_windows_version): New.
(spawn_detached, gnupg_spawn_helper, gnupg_process_spawn): New.
(gnupg_process_get_fds, gnupg_process_get_streams): New.
(process_kill, process_vctl, gnupg_process_ctl): New.
(gnupg_process_wait, gnupg_process_terminate): New.
(gnupg_process_release, gnupg_process_wait_list): New.
* common/exechelp.h: Re-write for new API.
* common/exectool.c (gnupg_exec_tool_stream): Follow the change.
* common/asshelp.c (start_new_service): Likewise.
* agent/genkey.c (do_check_passphrase_pattern): Likewise.
* dirmngr/ldap-wrapper.c (struct wrapper_context_s): Use PROC.
(destroy_wrapper): Follow the change of API.
(read_log_data): Follow the change of API, use printable_pid.
(ldap_reaper_thread, ldap_wrapper_release_context): Likewise.
(ldap_wrapper_connection_cleanup, ldap_wrapper): Likewise.
* g10/photoid.c (run_with_pipe): Follow the change of API.
(show_photo): Likewise.
* g13/be-encfs.c (run_umount_helper): Likewise.
(run_encfs_tool): Likewise.
* g13/g13.c: Add including ./common/exechelp.h.
* g13/mount.c: Likewise.
* g13/runner.c: Follow the change of API.
* g13/runner.h: Follow the change of API.
* scd/app.c (setup_env): New.
(report_change): Follow the change of API.
* tests/gpgscm/ffi.c (proc_object_finalize): New.
(proc_object_to_string): New.
(proc_wrap, proc_unwrap): New.
(do_spawn_process): Remove.
(do_process_spawn): New.
(setup_std_fds): New.
(do_spawn_process_fd): Remove.
(do_process_spawn_fd): New.
(do_wait_process): Remove.
(do_process_wait): New.
(do_wait_processes): Remove.
* tests/gpgscm/t-child.scm: Follow the change of API.
* tests/gpgscm/tests.scm: Likewise.
* tests/openpgp/defs.scm: Likewise.
* tests/tpm2dtests/defs.scm: Likewise.
* tools/gpg-card.c: Likewise.
* tools/gpgconf-comp.c: Likewise.
* tools/gpgconf.c: Likewise.
* tools/gpgtar-create.c: Likewise.
* tools/gpgtar-extract.c: Likewise.
* tools/gpgtar-list.c: Likewise.
--
GnuPG-bug-id: 6275
Signed-off-by: NIIBE Yutaka <gniibe@fsij.org>
2023-05-11 12:18:21 +02:00
|
|
|
|
if (status)
|
2022-04-22 06:33:45 +02:00
|
|
|
|
result = 1; /* Helper returned an error - probably a match. */
|
|
|
|
|
else
|
|
|
|
|
result = 0; /* Success; i.e. no match. */
|
Use gpgrt_process_spawn API from libgpg-error.
* agent/genkey.c (do_check_passphrase_pattern): Use the gpgrt API.
* common/asshelp.c (start_new_service): Ditto.
* common/exechelp.h: Remove gnupg_process_spawn API.
* common/exechelp-posix.c: Remove gnupg_process_spawn implementation.
* common/exechelp-w32.c: Likewise.
* common/exectool.c (gnupg_exec_tool_stream): Use the gpgrt API.
* common/t-exechelp.c (test_pipe_stream): Remove.
* dirmngr/ldap-wrapper.c (destroy_wrapper, ldap_reaper_thread): Use
the gpgrt API.
(ldap_wrapper_connection_cleanup, ldap_wrapper): Ditto.
* dirmngr/ldap.c, g10/call-keyboxd.c: No need to include exechelp.h.
* g10/photoid.c (run_with_pipe, show_photo): Use the gpgrt API.
* g13/be-encfs.c (run_umount_helper, run_encfs_tool): Ditto.
* g13/g13.c, g13/mount.c, g13/runner.c: No need to include exechelp.h.
* scd/apdu.c: No need to include exechelp.h.
* scd/app.c (report_change): Use the gpgrt API.
* sm/export.c, sm/import.c: No need to include exechelp.h.
* tests/gpgscm/ffi.c (proc_object_finalize, proc_wrap)
(do_process_spawn_io, do_process_spawn_fd, do_process_wait): Use the
gpgrt API.
* tools/gpg-auth.c: No need to include exechelp.h.
* tools/gpg-card.c (cmd_gpg): Use the gpgrt API.
* tools/gpg-connect-agent.c: No need to include exechelp.h.
* tools/gpg-mail-tube.c (mail_tube_encrypt, prepare_for_appimage)
(start_gpg_encrypt): Use the gpgrt API.
* tools/gpgconf-comp.c (gpg_agent_runtime_change)
(scdaemon_runtime_change, tpm2daemon_runtime_change)
(dirmngr_runtime_change, keyboxd_runtime_change)
(gc_component_launch, gc_component_check_options)
(retrieve_options_from_program): Ditto.
* tools/gpgconf.c (show_versions_via_dirmngr): Ditto.
* tools/gpgtar-create.c (gpgtar_create): Ditto.
* tools/gpgtar-extract.c (gpgtar_extract): Ditto.
* tools/gpgtar-list.c (gpgtar_list): Ditto.
--
GnuPG-bug-id: 7192
Signed-off-by: NIIBE Yutaka <gniibe@fsij.org>
2024-07-09 02:39:15 +02:00
|
|
|
|
gpgrt_process_release (proc);
|
2022-04-22 06:33:45 +02:00
|
|
|
|
}
|
2021-08-18 19:21:22 +02:00
|
|
|
|
|
|
|
|
|
xfree (patternfname);
|
2007-08-27 20:10:27 +02:00
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2011-02-04 12:57:53 +01:00
|
|
|
|
static int
|
2019-05-28 04:29:25 +02:00
|
|
|
|
take_this_one_anyway (ctrl_t ctrl, const char *desc, const char *anyway_btn)
|
2007-08-27 20:10:27 +02:00
|
|
|
|
{
|
2019-05-28 04:29:25 +02:00
|
|
|
|
return agent_get_confirmation (ctrl, desc,
|
|
|
|
|
anyway_btn, L_("Enter new passphrase"), 0);
|
2007-08-27 20:10:27 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2007-01-25 09:30:47 +01:00
|
|
|
|
/* Check whether the passphrase PW is suitable. Returns 0 if the
|
2021-08-13 13:42:31 +02:00
|
|
|
|
* passphrase is suitable and true if it is not and the user should be
|
|
|
|
|
* asked to provide a different one. If FAILED_CONSTRAINT is set, a
|
|
|
|
|
* message describing the problem is returned at FAILED_CONSTRAINT.
|
|
|
|
|
* The FLAGS are:
|
|
|
|
|
* CHECK_CONSTRAINTS_NOT_EMPTY
|
|
|
|
|
* Do not allow an empty passphrase
|
|
|
|
|
* CHECK_CONSTRAINTS_NEW_SYMKEY
|
|
|
|
|
* Hint that the passphrase is used for a new symmetric key.
|
|
|
|
|
*/
|
2007-01-25 09:30:47 +01:00
|
|
|
|
int
|
2021-08-13 13:42:31 +02:00
|
|
|
|
check_passphrase_constraints (ctrl_t ctrl, const char *pw, unsigned int flags,
|
2015-06-18 05:10:47 +02:00
|
|
|
|
char **failed_constraint)
|
2007-01-25 09:30:47 +01:00
|
|
|
|
{
|
2014-07-23 19:51:52 +02:00
|
|
|
|
gpg_error_t err = 0;
|
2007-01-25 09:30:47 +01:00
|
|
|
|
unsigned int minlen = opt.min_passphrase_len;
|
2007-08-27 20:10:27 +02:00
|
|
|
|
unsigned int minnonalpha = opt.min_passphrase_nonalpha;
|
2014-07-23 19:51:52 +02:00
|
|
|
|
char *msg1 = NULL;
|
|
|
|
|
char *msg2 = NULL;
|
|
|
|
|
char *msg3 = NULL;
|
2021-08-13 13:42:31 +02:00
|
|
|
|
int no_empty = !!(flags & CHECK_CONSTRAINTS_NOT_EMPTY);
|
2007-08-27 20:10:27 +02:00
|
|
|
|
|
2015-08-16 19:46:59 +02:00
|
|
|
|
if (ctrl && ctrl->pinentry_mode == PINENTRY_MODE_LOOPBACK)
|
|
|
|
|
return 0;
|
|
|
|
|
|
2007-01-25 09:30:47 +01:00
|
|
|
|
if (!pw)
|
|
|
|
|
pw = "";
|
|
|
|
|
|
2014-07-23 19:16:51 +02:00
|
|
|
|
/* The first check is to warn about an empty passphrase. */
|
|
|
|
|
if (!*pw)
|
|
|
|
|
{
|
2020-07-08 14:20:01 +02:00
|
|
|
|
const char *desc = (opt.enforce_passphrase_constraints || no_empty?
|
2015-06-30 21:58:02 +02:00
|
|
|
|
L_("You have not entered a passphrase!%0A"
|
|
|
|
|
"An empty passphrase is not allowed.") :
|
|
|
|
|
L_("You have not entered a passphrase - "
|
|
|
|
|
"this is in general a bad idea!%0A"
|
|
|
|
|
"Please confirm that you do not want to "
|
|
|
|
|
"have any protection on your key."));
|
2014-07-23 19:16:51 +02:00
|
|
|
|
|
2015-06-18 05:10:47 +02:00
|
|
|
|
err = 1;
|
|
|
|
|
if (failed_constraint)
|
|
|
|
|
{
|
2020-07-08 14:20:01 +02:00
|
|
|
|
if (opt.enforce_passphrase_constraints || no_empty)
|
2015-06-18 05:10:47 +02:00
|
|
|
|
*failed_constraint = xstrdup (desc);
|
|
|
|
|
else
|
2019-05-28 04:29:25 +02:00
|
|
|
|
err = take_this_one_anyway (ctrl, desc,
|
|
|
|
|
L_("Yes, protection is not needed"));
|
2015-06-18 05:10:47 +02:00
|
|
|
|
}
|
2014-07-23 19:16:51 +02:00
|
|
|
|
|
2014-07-23 19:51:52 +02:00
|
|
|
|
goto leave;
|
2014-07-23 19:16:51 +02:00
|
|
|
|
}
|
|
|
|
|
|
2014-07-23 19:51:52 +02:00
|
|
|
|
/* Now check the constraints and collect the error messages unless
|
2017-02-20 22:19:50 +01:00
|
|
|
|
in silent mode which returns immediately. */
|
2015-11-23 22:13:56 +01:00
|
|
|
|
if (utf8_charcount (pw, -1) < minlen )
|
2007-01-25 09:30:47 +01:00
|
|
|
|
{
|
2015-06-18 05:10:47 +02:00
|
|
|
|
if (!failed_constraint)
|
2014-07-23 19:51:52 +02:00
|
|
|
|
{
|
|
|
|
|
err = gpg_error (GPG_ERR_INV_PASSPHRASE);
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
2007-09-18 13:40:09 +02:00
|
|
|
|
|
2014-07-23 19:51:52 +02:00
|
|
|
|
msg1 = xtryasprintf
|
|
|
|
|
( ngettext ("A passphrase should be at least %u character long.",
|
2011-02-04 12:57:53 +01:00
|
|
|
|
"A passphrase should be at least %u characters long.",
|
2007-10-24 10:06:16 +02:00
|
|
|
|
minlen), minlen );
|
2014-07-23 19:51:52 +02:00
|
|
|
|
if (!msg1)
|
|
|
|
|
{
|
|
|
|
|
err = gpg_error_from_syserror ();
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
2007-01-25 09:30:47 +01:00
|
|
|
|
}
|
|
|
|
|
|
2011-02-04 12:57:53 +01:00
|
|
|
|
if (nonalpha_count (pw) < minnonalpha )
|
2007-08-27 20:10:27 +02:00
|
|
|
|
{
|
2015-06-18 05:10:47 +02:00
|
|
|
|
if (!failed_constraint)
|
2014-07-23 19:51:52 +02:00
|
|
|
|
{
|
|
|
|
|
err = gpg_error (GPG_ERR_INV_PASSPHRASE);
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
2007-09-18 13:40:09 +02:00
|
|
|
|
|
2014-07-23 19:51:52 +02:00
|
|
|
|
msg2 = xtryasprintf
|
|
|
|
|
( ngettext ("A passphrase should contain at least %u digit or%%0A"
|
2011-02-04 12:57:53 +01:00
|
|
|
|
"special character.",
|
2007-10-24 10:06:16 +02:00
|
|
|
|
"A passphrase should contain at least %u digits or%%0A"
|
|
|
|
|
"special characters.",
|
2007-08-27 20:10:27 +02:00
|
|
|
|
minnonalpha), minnonalpha );
|
2014-07-23 19:51:52 +02:00
|
|
|
|
if (!msg2)
|
|
|
|
|
{
|
|
|
|
|
err = gpg_error_from_syserror ();
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
2007-08-27 20:10:27 +02:00
|
|
|
|
}
|
|
|
|
|
|
2014-07-23 19:16:51 +02:00
|
|
|
|
/* If configured check the passphrase against a list of known words
|
2007-08-27 20:10:27 +02:00
|
|
|
|
and pattern. The actual test is done by an external program.
|
|
|
|
|
The warning message is generic to give the user no hint on how to
|
|
|
|
|
circumvent this list. */
|
2021-08-13 13:42:31 +02:00
|
|
|
|
if (*pw
|
|
|
|
|
&& (opt.check_passphrase_pattern || opt.check_sym_passphrase_pattern)
|
|
|
|
|
&& do_check_passphrase_pattern (ctrl, pw, flags))
|
2007-08-27 20:10:27 +02:00
|
|
|
|
{
|
2015-06-18 05:10:47 +02:00
|
|
|
|
if (!failed_constraint)
|
2014-07-23 19:51:52 +02:00
|
|
|
|
{
|
|
|
|
|
err = gpg_error (GPG_ERR_INV_PASSPHRASE);
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
2007-09-18 13:40:09 +02:00
|
|
|
|
|
2015-06-30 22:28:41 +02:00
|
|
|
|
msg3 = xtryasprintf
|
|
|
|
|
(L_("A passphrase may not be a known term or match%%0A"
|
|
|
|
|
"certain pattern."));
|
2014-07-23 19:51:52 +02:00
|
|
|
|
if (!msg3)
|
|
|
|
|
{
|
|
|
|
|
err = gpg_error_from_syserror ();
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
2007-08-27 20:10:27 +02:00
|
|
|
|
}
|
|
|
|
|
|
2015-06-18 05:10:47 +02:00
|
|
|
|
if (failed_constraint && (msg1 || msg2 || msg3))
|
2014-07-23 19:51:52 +02:00
|
|
|
|
{
|
|
|
|
|
char *msg;
|
|
|
|
|
size_t n;
|
|
|
|
|
|
|
|
|
|
msg = strconcat
|
2015-06-30 21:58:02 +02:00
|
|
|
|
(L_("Warning: You have entered an insecure passphrase."),
|
2014-07-23 19:51:52 +02:00
|
|
|
|
"%0A%0A",
|
|
|
|
|
msg1? msg1 : "", msg1? "%0A" : "",
|
|
|
|
|
msg2? msg2 : "", msg2? "%0A" : "",
|
|
|
|
|
msg3? msg3 : "", msg3? "%0A" : "",
|
|
|
|
|
NULL);
|
|
|
|
|
if (!msg)
|
|
|
|
|
{
|
|
|
|
|
err = gpg_error_from_syserror ();
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
/* Strip a trailing "%0A". */
|
|
|
|
|
n = strlen (msg);
|
|
|
|
|
if (n > 3 && !strcmp (msg + n - 3, "%0A"))
|
|
|
|
|
msg[n-3] = 0;
|
|
|
|
|
|
2015-06-18 05:10:47 +02:00
|
|
|
|
err = 1;
|
|
|
|
|
if (opt.enforce_passphrase_constraints)
|
|
|
|
|
*failed_constraint = msg;
|
|
|
|
|
else
|
|
|
|
|
{
|
2019-05-28 04:29:25 +02:00
|
|
|
|
err = take_this_one_anyway (ctrl, msg, L_("Take this one anyway"));
|
2015-06-18 05:10:47 +02:00
|
|
|
|
xfree (msg);
|
|
|
|
|
}
|
2014-07-23 19:51:52 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
leave:
|
|
|
|
|
xfree (msg1);
|
|
|
|
|
xfree (msg2);
|
|
|
|
|
xfree (msg3);
|
|
|
|
|
return err;
|
2007-01-25 09:30:47 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2003-08-05 19:11:04 +02:00
|
|
|
|
/* Callback function to compare the first entered PIN with the one
|
|
|
|
|
currently being entered. */
|
2015-10-09 04:33:13 +02:00
|
|
|
|
static gpg_error_t
|
2003-08-05 19:11:04 +02:00
|
|
|
|
reenter_compare_cb (struct pin_entry_info_s *pi)
|
|
|
|
|
{
|
|
|
|
|
const char *pin1 = pi->check_cb_arg;
|
|
|
|
|
|
|
|
|
|
if (!strcmp (pin1, pi->pin))
|
|
|
|
|
return 0; /* okay */
|
2015-10-09 04:33:13 +02:00
|
|
|
|
return gpg_error (GPG_ERR_BAD_PASSPHRASE);
|
2003-08-05 19:11:04 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2010-06-17 17:44:44 +02:00
|
|
|
|
/* Ask the user for a new passphrase using PROMPT. On success the
|
|
|
|
|
function returns 0 and store the passphrase at R_PASSPHRASE; if the
|
|
|
|
|
user opted not to use a passphrase NULL will be stored there. The
|
|
|
|
|
user needs to free the returned string. In case of an error and
|
2010-10-13 17:57:08 +02:00
|
|
|
|
error code is returned and NULL stored at R_PASSPHRASE. */
|
2010-06-17 17:44:44 +02:00
|
|
|
|
gpg_error_t
|
|
|
|
|
agent_ask_new_passphrase (ctrl_t ctrl, const char *prompt,
|
2010-10-13 17:57:08 +02:00
|
|
|
|
char **r_passphrase)
|
2010-06-17 17:44:44 +02:00
|
|
|
|
{
|
|
|
|
|
gpg_error_t err;
|
|
|
|
|
const char *text1 = prompt;
|
2015-06-30 21:58:02 +02:00
|
|
|
|
const char *text2 = L_("Please re-enter this passphrase");
|
2015-06-18 05:10:47 +02:00
|
|
|
|
char *initial_errtext = NULL;
|
2010-06-17 17:44:44 +02:00
|
|
|
|
struct pin_entry_info_s *pi, *pi2;
|
2011-02-04 12:57:53 +01:00
|
|
|
|
|
2010-06-17 17:44:44 +02:00
|
|
|
|
*r_passphrase = NULL;
|
|
|
|
|
|
2011-09-11 22:55:34 +02:00
|
|
|
|
if (ctrl->pinentry_mode == PINENTRY_MODE_LOOPBACK)
|
|
|
|
|
{
|
|
|
|
|
size_t size;
|
|
|
|
|
unsigned char *buffer;
|
|
|
|
|
|
2017-07-05 11:54:45 +02:00
|
|
|
|
err = pinentry_loopback (ctrl, "NEW_PASSPHRASE", &buffer, &size,
|
|
|
|
|
MAX_PASSPHRASE_LEN);
|
2011-09-13 02:13:18 +02:00
|
|
|
|
if (!err)
|
2011-09-11 22:55:34 +02:00
|
|
|
|
{
|
2011-09-13 02:13:19 +02:00
|
|
|
|
if (size)
|
|
|
|
|
{
|
|
|
|
|
buffer[size] = 0;
|
|
|
|
|
*r_passphrase = buffer;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
*r_passphrase = NULL;
|
2011-09-11 22:55:34 +02:00
|
|
|
|
}
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-01 13:21:25 +02:00
|
|
|
|
pi = gcry_calloc_secure (1, sizeof (*pi) + MAX_PASSPHRASE_LEN + 1);
|
|
|
|
|
if (!pi)
|
|
|
|
|
return gpg_error_from_syserror ();
|
|
|
|
|
pi2 = gcry_calloc_secure (1, sizeof (*pi2) + MAX_PASSPHRASE_LEN + 1);
|
|
|
|
|
if (!pi2)
|
|
|
|
|
{
|
|
|
|
|
err = gpg_error_from_syserror ();
|
2021-05-20 10:13:51 +02:00
|
|
|
|
xfree (pi);
|
2015-10-01 13:21:25 +02:00
|
|
|
|
return err;
|
|
|
|
|
}
|
2015-08-24 16:14:09 +02:00
|
|
|
|
pi->max_length = MAX_PASSPHRASE_LEN + 1;
|
2010-06-17 17:44:44 +02:00
|
|
|
|
pi->max_tries = 3;
|
2020-07-08 14:54:10 +02:00
|
|
|
|
pi->with_qualitybar = 0;
|
2014-10-24 16:20:20 +02:00
|
|
|
|
pi->with_repeat = 1;
|
2015-08-24 16:14:09 +02:00
|
|
|
|
pi2->max_length = MAX_PASSPHRASE_LEN + 1;
|
2010-06-17 17:44:44 +02:00
|
|
|
|
pi2->max_tries = 3;
|
|
|
|
|
pi2->check_cb = reenter_compare_cb;
|
|
|
|
|
pi2->check_cb_arg = pi->pin;
|
|
|
|
|
|
|
|
|
|
next_try:
|
2015-04-14 18:41:05 +02:00
|
|
|
|
err = agent_askpin (ctrl, text1, NULL, initial_errtext, pi, NULL, 0);
|
2015-06-30 21:58:02 +02:00
|
|
|
|
xfree (initial_errtext);
|
2010-06-17 17:44:44 +02:00
|
|
|
|
initial_errtext = NULL;
|
|
|
|
|
if (!err)
|
|
|
|
|
{
|
2020-07-08 14:20:01 +02:00
|
|
|
|
if (check_passphrase_constraints (ctrl, pi->pin, 0, &initial_errtext))
|
2010-06-17 17:44:44 +02:00
|
|
|
|
{
|
|
|
|
|
pi->failed_tries = 0;
|
|
|
|
|
pi2->failed_tries = 0;
|
|
|
|
|
goto next_try;
|
|
|
|
|
}
|
2014-10-24 16:20:20 +02:00
|
|
|
|
/* Unless the passphrase is empty or the pinentry told us that
|
|
|
|
|
it already did the repetition check, ask to confirm it. */
|
2015-03-15 12:57:13 +01:00
|
|
|
|
if (*pi->pin && !pi->repeat_okay)
|
2010-06-17 17:44:44 +02:00
|
|
|
|
{
|
2015-04-14 18:41:05 +02:00
|
|
|
|
err = agent_askpin (ctrl, text2, NULL, NULL, pi2, NULL, 0);
|
2015-10-09 04:33:13 +02:00
|
|
|
|
if (gpg_err_code (err) == GPG_ERR_BAD_PASSPHRASE)
|
2010-06-17 17:44:44 +02:00
|
|
|
|
{ /* The re-entered one did not match and the user did not
|
|
|
|
|
hit cancel. */
|
2015-06-30 21:58:02 +02:00
|
|
|
|
initial_errtext = xtrystrdup (L_("does not match - try again"));
|
|
|
|
|
if (initial_errtext)
|
|
|
|
|
goto next_try;
|
|
|
|
|
err = gpg_error_from_syserror ();
|
2010-06-17 17:44:44 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-02-04 12:57:53 +01:00
|
|
|
|
|
2010-06-17 17:44:44 +02:00
|
|
|
|
if (!err && *pi->pin)
|
|
|
|
|
{
|
|
|
|
|
/* User wants a passphrase. */
|
|
|
|
|
*r_passphrase = xtrystrdup (pi->pin);
|
|
|
|
|
if (!*r_passphrase)
|
|
|
|
|
err = gpg_error_from_syserror ();
|
|
|
|
|
}
|
2015-06-30 21:58:02 +02:00
|
|
|
|
|
|
|
|
|
xfree (initial_errtext);
|
2015-10-01 13:21:25 +02:00
|
|
|
|
xfree (pi2);
|
2010-06-17 17:44:44 +02:00
|
|
|
|
xfree (pi);
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2003-08-05 19:11:04 +02:00
|
|
|
|
|
|
|
|
|
/* Generate a new keypair according to the parameters given in
|
2024-01-22 13:22:44 +01:00
|
|
|
|
* KEYPARAM. If CACHE_NONCE is given first try to lookup a passphrase
|
|
|
|
|
* using the cache nonce. If NO_PROTECTION is true the key will not
|
|
|
|
|
* be protected by a passphrase. If OVERRIDE_PASSPHRASE is true that
|
|
|
|
|
* passphrase will be used for the new key. If TIMESTAMP is not zero
|
|
|
|
|
* it will be recorded as creation date of the key (unless extended
|
|
|
|
|
* format is disabled). In ctrl_ephemeral_mode the key is stored in
|
|
|
|
|
* the session data and an identifier is returned using a status
|
|
|
|
|
* line. */
|
2003-08-05 19:11:04 +02:00
|
|
|
|
int
|
2024-01-22 13:22:44 +01:00
|
|
|
|
agent_genkey (ctrl_t ctrl, unsigned int flags,
|
|
|
|
|
const char *cache_nonce, time_t timestamp,
|
|
|
|
|
const char *keyparam, size_t keyparamlen,
|
|
|
|
|
const char *override_passphrase, membuf_t *outbuf)
|
2003-08-05 19:11:04 +02:00
|
|
|
|
{
|
|
|
|
|
gcry_sexp_t s_keyparam, s_key, s_private, s_public;
|
2015-01-21 11:31:20 +01:00
|
|
|
|
char *passphrase_buffer = NULL;
|
|
|
|
|
const char *passphrase;
|
2003-08-05 19:11:04 +02:00
|
|
|
|
int rc;
|
|
|
|
|
size_t len;
|
|
|
|
|
char *buf;
|
|
|
|
|
|
|
|
|
|
rc = gcry_sexp_sscan (&s_keyparam, NULL, keyparam, keyparamlen);
|
|
|
|
|
if (rc)
|
|
|
|
|
{
|
|
|
|
|
log_error ("failed to convert keyparam: %s\n", gpg_strerror (rc));
|
|
|
|
|
return gpg_error (GPG_ERR_INV_DATA);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Get the passphrase now, cause key generation may take a while. */
|
2015-01-21 11:31:20 +01:00
|
|
|
|
if (override_passphrase)
|
|
|
|
|
passphrase = override_passphrase;
|
2024-01-22 13:22:44 +01:00
|
|
|
|
else if ((flags & GENKEY_FLAG_NO_PROTECTION) || !cache_nonce)
|
2010-10-14 18:34:31 +02:00
|
|
|
|
passphrase = NULL;
|
2011-02-04 12:57:53 +01:00
|
|
|
|
else
|
2015-01-21 11:31:20 +01:00
|
|
|
|
{
|
2018-03-27 08:40:58 +02:00
|
|
|
|
passphrase_buffer = agent_get_cache (ctrl, cache_nonce, CACHE_MODE_NONCE);
|
2015-01-21 11:31:20 +01:00
|
|
|
|
passphrase = passphrase_buffer;
|
|
|
|
|
}
|
2010-10-14 18:34:31 +02:00
|
|
|
|
|
2024-01-22 13:22:44 +01:00
|
|
|
|
if (passphrase || (flags & GENKEY_FLAG_NO_PROTECTION))
|
|
|
|
|
; /* No need to ask for a passphrase. */
|
2010-09-01 11:48:35 +02:00
|
|
|
|
else
|
2015-01-21 11:31:20 +01:00
|
|
|
|
{
|
|
|
|
|
rc = agent_ask_new_passphrase (ctrl,
|
2015-06-30 21:58:02 +02:00
|
|
|
|
L_("Please enter the passphrase to%0A"
|
|
|
|
|
"protect your new key"),
|
2015-01-21 11:31:20 +01:00
|
|
|
|
&passphrase_buffer);
|
|
|
|
|
if (rc)
|
2021-05-20 10:13:51 +02:00
|
|
|
|
{
|
|
|
|
|
gcry_sexp_release (s_keyparam);
|
|
|
|
|
return rc;
|
|
|
|
|
}
|
2015-01-21 11:31:20 +01:00
|
|
|
|
passphrase = passphrase_buffer;
|
|
|
|
|
}
|
2003-08-05 19:11:04 +02:00
|
|
|
|
|
|
|
|
|
rc = gcry_pk_genkey (&s_key, s_keyparam );
|
|
|
|
|
gcry_sexp_release (s_keyparam);
|
|
|
|
|
if (rc)
|
|
|
|
|
{
|
|
|
|
|
log_error ("key generation failed: %s\n", gpg_strerror (rc));
|
2015-01-21 11:31:20 +01:00
|
|
|
|
xfree (passphrase_buffer);
|
2003-11-12 16:17:44 +01:00
|
|
|
|
return rc;
|
2003-08-05 19:11:04 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* break out the parts */
|
|
|
|
|
s_private = gcry_sexp_find_token (s_key, "private-key", 0);
|
|
|
|
|
if (!s_private)
|
|
|
|
|
{
|
|
|
|
|
log_error ("key generation failed: invalid return value\n");
|
|
|
|
|
gcry_sexp_release (s_key);
|
2015-01-21 11:31:20 +01:00
|
|
|
|
xfree (passphrase_buffer);
|
2003-08-05 19:11:04 +02:00
|
|
|
|
return gpg_error (GPG_ERR_INV_DATA);
|
|
|
|
|
}
|
|
|
|
|
s_public = gcry_sexp_find_token (s_key, "public-key", 0);
|
|
|
|
|
if (!s_public)
|
|
|
|
|
{
|
|
|
|
|
log_error ("key generation failed: invalid return value\n");
|
|
|
|
|
gcry_sexp_release (s_private);
|
|
|
|
|
gcry_sexp_release (s_key);
|
2015-01-21 11:31:20 +01:00
|
|
|
|
xfree (passphrase_buffer);
|
2003-08-05 19:11:04 +02:00
|
|
|
|
return gpg_error (GPG_ERR_INV_DATA);
|
|
|
|
|
}
|
|
|
|
|
gcry_sexp_release (s_key); s_key = NULL;
|
2011-02-04 12:57:53 +01:00
|
|
|
|
|
2003-08-05 19:11:04 +02:00
|
|
|
|
/* store the secret key */
|
2024-01-22 13:22:44 +01:00
|
|
|
|
if (opt.verbose)
|
|
|
|
|
log_info ("storing %sprivate key\n",
|
|
|
|
|
ctrl->ephemeral_mode?"ephemeral ":"");
|
|
|
|
|
rc = store_key (ctrl, s_private, passphrase, 0, ctrl->s2k_count, timestamp);
|
|
|
|
|
if (!rc && !ctrl->ephemeral_mode)
|
2010-09-01 11:48:35 +02:00
|
|
|
|
{
|
2024-01-22 13:22:44 +01:00
|
|
|
|
/* FIXME: or does it make sense to also cache passphrases in
|
|
|
|
|
* ephemeral mode using a dedicated cache? */
|
2010-09-01 11:48:35 +02:00
|
|
|
|
if (!cache_nonce)
|
|
|
|
|
{
|
|
|
|
|
char tmpbuf[12];
|
|
|
|
|
gcry_create_nonce (tmpbuf, 12);
|
|
|
|
|
cache_nonce = bin2hex (tmpbuf, 12, NULL);
|
|
|
|
|
}
|
2010-10-14 18:34:31 +02:00
|
|
|
|
if (cache_nonce
|
2024-01-22 13:22:44 +01:00
|
|
|
|
&& !(flags & GENKEY_FLAG_NO_PROTECTION)
|
2018-03-27 08:40:58 +02:00
|
|
|
|
&& !agent_put_cache (ctrl, cache_nonce, CACHE_MODE_NONCE,
|
2011-04-21 15:40:48 +02:00
|
|
|
|
passphrase, ctrl->cache_ttl_opt_preset))
|
2010-09-01 11:48:35 +02:00
|
|
|
|
agent_write_status (ctrl, "CACHE_NONCE", cache_nonce, NULL);
|
2024-01-22 13:22:44 +01:00
|
|
|
|
if ((flags & GENKEY_FLAG_PRESET)
|
|
|
|
|
&& !(flags & GENKEY_FLAG_NO_PROTECTION))
|
|
|
|
|
{
|
|
|
|
|
unsigned char grip[20];
|
|
|
|
|
char hexgrip[40+1];
|
|
|
|
|
if (gcry_pk_get_keygrip (s_private, grip))
|
|
|
|
|
{
|
|
|
|
|
bin2hex(grip, 20, hexgrip);
|
|
|
|
|
rc = agent_put_cache (ctrl, hexgrip,
|
|
|
|
|
CACHE_MODE_ANY, passphrase,
|
2011-04-21 15:40:48 +02:00
|
|
|
|
ctrl->cache_ttl_opt_preset);
|
2024-01-22 13:22:44 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2010-09-01 11:48:35 +02:00
|
|
|
|
}
|
2015-01-21 11:31:20 +01:00
|
|
|
|
xfree (passphrase_buffer);
|
|
|
|
|
passphrase_buffer = NULL;
|
2010-06-17 17:44:44 +02:00
|
|
|
|
passphrase = NULL;
|
2003-08-05 19:11:04 +02:00
|
|
|
|
gcry_sexp_release (s_private);
|
|
|
|
|
if (rc)
|
|
|
|
|
{
|
|
|
|
|
gcry_sexp_release (s_public);
|
|
|
|
|
return rc;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* return the public key */
|
2004-12-20 09:32:56 +01:00
|
|
|
|
if (DBG_CRYPTO)
|
|
|
|
|
log_debug ("returning public key\n");
|
2003-08-05 19:11:04 +02:00
|
|
|
|
len = gcry_sexp_sprint (s_public, GCRYSEXP_FMT_CANON, NULL, 0);
|
2019-05-14 10:31:46 +02:00
|
|
|
|
log_assert (len);
|
2003-08-05 19:11:04 +02:00
|
|
|
|
buf = xtrymalloc (len);
|
|
|
|
|
if (!buf)
|
|
|
|
|
{
|
|
|
|
|
gpg_error_t tmperr = out_of_core ();
|
|
|
|
|
gcry_sexp_release (s_private);
|
|
|
|
|
gcry_sexp_release (s_public);
|
|
|
|
|
return tmperr;
|
|
|
|
|
}
|
|
|
|
|
len = gcry_sexp_sprint (s_public, GCRYSEXP_FMT_CANON, buf, len);
|
2019-05-14 10:31:46 +02:00
|
|
|
|
log_assert (len);
|
2004-12-20 09:32:56 +01:00
|
|
|
|
put_membuf (outbuf, buf, len);
|
2003-08-05 19:11:04 +02:00
|
|
|
|
gcry_sexp_release (s_public);
|
|
|
|
|
xfree (buf);
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-10-26 11:10:29 +02:00
|
|
|
|
/* Apply a new passphrase to the key S_SKEY and store it. If
|
|
|
|
|
PASSPHRASE_ADDR and *PASSPHRASE_ADDR are not NULL, use that
|
|
|
|
|
passphrase. If PASSPHRASE_ADDR is not NULL store a newly entered
|
|
|
|
|
passphrase at that address. */
|
|
|
|
|
gpg_error_t
|
|
|
|
|
agent_protect_and_store (ctrl_t ctrl, gcry_sexp_t s_skey,
|
2011-02-04 12:57:53 +01:00
|
|
|
|
char **passphrase_addr)
|
2003-08-05 19:11:04 +02:00
|
|
|
|
{
|
2010-10-26 11:10:29 +02:00
|
|
|
|
gpg_error_t err;
|
2003-08-05 19:11:04 +02:00
|
|
|
|
|
2010-10-26 11:10:29 +02:00
|
|
|
|
if (passphrase_addr && *passphrase_addr)
|
2010-06-17 17:44:44 +02:00
|
|
|
|
{
|
2010-10-26 11:10:29 +02:00
|
|
|
|
/* Take an empty string as request not to protect the key. */
|
2024-01-22 13:22:44 +01:00
|
|
|
|
err = store_key (ctrl, s_skey,
|
|
|
|
|
**passphrase_addr? *passphrase_addr:NULL, 1,
|
2020-08-17 14:21:00 +02:00
|
|
|
|
ctrl->s2k_count, 0);
|
2010-06-17 17:44:44 +02:00
|
|
|
|
}
|
2010-10-26 11:10:29 +02:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
char *pass = NULL;
|
|
|
|
|
|
|
|
|
|
if (passphrase_addr)
|
|
|
|
|
{
|
|
|
|
|
xfree (*passphrase_addr);
|
|
|
|
|
*passphrase_addr = NULL;
|
|
|
|
|
}
|
2011-02-04 12:57:53 +01:00
|
|
|
|
err = agent_ask_new_passphrase (ctrl,
|
2015-06-30 21:58:02 +02:00
|
|
|
|
L_("Please enter the new passphrase"),
|
2010-10-26 11:10:29 +02:00
|
|
|
|
&pass);
|
|
|
|
|
if (!err)
|
2024-01-22 13:22:44 +01:00
|
|
|
|
err = store_key (ctrl, s_skey, pass, 1, ctrl->s2k_count, 0);
|
2010-10-26 11:10:29 +02:00
|
|
|
|
if (!err && passphrase_addr)
|
|
|
|
|
*passphrase_addr = pass;
|
|
|
|
|
else
|
|
|
|
|
xfree (pass);
|
|
|
|
|
}
|
2011-02-04 12:57:53 +01:00
|
|
|
|
|
2010-10-26 11:10:29 +02:00
|
|
|
|
return err;
|
2003-08-05 19:11:04 +02:00
|
|
|
|
}
|