1
0
Fork 0
mirror of git://git.gnupg.org/gnupg.git synced 2025-07-03 22:56:33 +02:00

sm: Use estream_t for operations.

* sm/decrypt.c (gpgsm_decrypt): Use estream for the input.
* sm/encrypt.c (gpgsm_encrypt): Likewise.
* sm/gpgsm.c (open_read): Remove.
(main): Use open_es_fread for gpgsm_import_files.  Fix call of
gpgsm_encrypt, gpgsm_sign, gpgsm_verify and gpgsm_decrypt.
(open_es_fread): Use gnupg_check_special_filename and open_stream_nc.
* sm/gpgsm.h: Fix function declarations.
* sm/import.c (import_one): Use estream for the input.
(reimport_one, gpgsm_import, gpgsm_import_files): Likewise.
* sm/server.c (cmd_encrypt): Use open_stream_nc to get estream and
call gpgsm_encrypt with it.
(cmd_decrypt): Likewise for the call of gpgsm_decrypt.
(cmd_verify): Likewise for the call of gpgsm_verify.
(cmd_sign): Likewise for the call of gpgsm_sign.
(cmd_import): Likewise for the call of gpgsm_import.
* sm/sign.c (hash_data): Use estream for the input.
(hash_and_copy_data): Likewise.
(gpgsm_sign): Likewise.
* sm/verify.c (gpgsm_verify): Use estream for the input.

--

GnuPG-bug-id: 6592
Signed-off-by: NIIBE Yutaka <gniibe@fsij.org>
This commit is contained in:
NIIBE Yutaka 2023-07-13 13:18:05 +09:00
parent a43d0f7d46
commit 3e1357789f
No known key found for this signature in database
GPG key ID: 640114AF89DE6054
8 changed files with 141 additions and 154 deletions

View file

@ -535,7 +535,6 @@ static void set_cmd (enum cmd_and_opt_values *ret_cmd,
enum cmd_and_opt_values new_cmd );
static void emergency_cleanup (void);
static int open_read (const char *filename);
static estream_t open_es_fread (const char *filename, const char *mode);
static estream_t open_es_fwrite (const char *filename);
static void run_protect_tool (int argc, char **argv);
@ -1778,7 +1777,7 @@ main ( int argc, char **argv)
{
log_info (_("importing common certificates '%s'\n"),
filelist[0]);
gpgsm_import_files (&ctrl, 1, filelist, open_read);
gpgsm_import_files (&ctrl, 1, filelist, open_es_fread);
}
xfree (filelist[0]);
}
@ -1916,9 +1915,20 @@ main ( int argc, char **argv)
set_binary (stdin);
if (!argc) /* Source is stdin. */
err = gpgsm_encrypt (&ctrl, recplist, 0, fp);
err = gpgsm_encrypt (&ctrl, recplist, es_stdin, fp);
else if (argc == 1) /* Source is the given file. */
err = gpgsm_encrypt (&ctrl, recplist, open_read (*argv), fp);
{
estream_t data_fp = es_fopen (*argv, "rb");
if (!data_fp)
{
log_error (_("can't open '%s': %s\n"), *argv,
strerror (errno));
gpgsm_exit (2);
}
err = gpgsm_encrypt (&ctrl, recplist, data_fp, fp);
es_fclose (data_fp);
}
else
wrong_args ("--encrypt [datafile]");
@ -1939,8 +1949,18 @@ main ( int argc, char **argv)
if (!argc) /* Create from stdin. */
err = gpgsm_sign (&ctrl, signerlist, 0, detached_sig, fp);
else if (argc == 1) /* From file. */
err = gpgsm_sign (&ctrl, signerlist,
open_read (*argv), detached_sig, fp);
{
estream_t data_fp = es_fopen (*argv, "rb");
if (!data_fp)
{
log_error (_("can't open '%s': %s\n"), *argv,
strerror (errno));
gpgsm_exit (2);
}
err = gpgsm_sign (&ctrl, signerlist, data_fp, detached_sig, fp);
es_fclose (data_fp);
}
else
wrong_args ("--sign [datafile]");
@ -1983,11 +2003,29 @@ main ( int argc, char **argv)
if (!argc)
gpgsm_verify (&ctrl, 0, NULL, fp); /* normal signature from stdin */
else if (argc == 1)
gpgsm_verify (&ctrl, open_read (*argv), NULL, fp); /* std signature */
{
estream_t in_fp = es_fopen (*argv, "rb");
if (!in_fp)
{
log_error (_("can't open '%s': %s\n"), *argv,
strerror (errno));
gpgsm_exit (2);
}
gpgsm_verify (&ctrl, in_fp, NULL, fp); /* std signature */
es_fclose (in_fp);
}
else if (argc == 2) /* detached signature (sig, detached) */
{
estream_t in_fp = es_fopen (*argv, "rb");
estream_t data_fp = es_fopen (argv[1], "rb");
if (!in_fp)
{
log_error (_("can't open '%s': %s\n"), *argv,
strerror (errno));
gpgsm_exit (2);
}
if (!data_fp)
{
log_error (_("can't open '%s': %s\n"), argv[1],
@ -1995,7 +2033,8 @@ main ( int argc, char **argv)
gpgsm_exit (2);
}
gpgsm_verify (&ctrl, open_read (*argv), data_fp, NULL);
gpgsm_verify (&ctrl, in_fp, data_fp, NULL);
es_fclose (in_fp);
es_fclose (data_fp);
}
else
@ -2013,7 +2052,17 @@ main ( int argc, char **argv)
if (!argc)
err = gpgsm_decrypt (&ctrl, 0, fp); /* from stdin */
else if (argc == 1)
err = gpgsm_decrypt (&ctrl, open_read (*argv), fp); /* from file */
{
estream_t data_fp = es_fopen (*argv, "rb");
if (!data_fp)
{
log_error (_("can't open '%s': %s\n"), *argv,
strerror (errno));
gpgsm_exit (2);
}
err = gpgsm_decrypt (&ctrl, data_fp, fp); /* from file */
es_fclose (data_fp);
}
else
wrong_args ("--decrypt [filename]");
@ -2104,7 +2153,7 @@ main ( int argc, char **argv)
case aImport:
gpgsm_import_files (&ctrl, argc, argv, open_read);
gpgsm_import_files (&ctrl, argc, argv, open_es_fread);
break;
case aExport:
@ -2305,46 +2354,20 @@ gpgsm_parse_validation_model (const char *model)
}
/* Open the FILENAME for read and return the file descriptor. Stop
with an error message in case of problems. "-" denotes stdin and
if special filenames are allowed the given fd is opened instead. */
static int
open_read (const char *filename)
{
int fd;
if (filename[0] == '-' && !filename[1])
{
set_binary (stdin);
return 0; /* stdin */
}
fd = check_special_filename (filename, 0, 0);
if (fd != -1)
return fd;
fd = gnupg_open (filename, O_RDONLY | O_BINARY, 0);
if (fd == -1)
{
log_error (_("can't open '%s': %s\n"), filename, strerror (errno));
gpgsm_exit (2);
}
return fd;
}
/* Same as open_read but return an estream_t. */
static estream_t
open_es_fread (const char *filename, const char *mode)
{
int fd;
gnupg_fd_t fd;
estream_t fp;
if (filename[0] == '-' && !filename[1])
fd = fileno (stdin);
else
fd = check_special_filename (filename, 0, 0);
if (fd != -1)
fd = gnupg_check_special_filename (filename);
if (fd != GNUPG_INVALID_FD)
{
fp = es_fdopen_nc (fd, mode);
fp = open_stream_nc (fd, mode);
if (!fp)
{
log_error ("es_fdopen(%d) failed: %s\n", fd, strerror (errno));