diff options
author | NIIBE Yutaka <[email protected]> | 2023-07-25 01:04:18 +0000 |
---|---|---|
committer | NIIBE Yutaka <[email protected]> | 2023-07-25 01:04:18 +0000 |
commit | 95d97615097255a1db1b71eec4df18c171e88df1 (patch) | |
tree | ead969dbc9a8433619a0ea8026e96b13f81d5a70 /sm/import.c | |
parent | dirmngr: Silence compiler when it's without LDAP. (diff) | |
download | gnupg-95d97615097255a1db1b71eec4df18c171e88df1.tar.gz gnupg-95d97615097255a1db1b71eec4df18c171e88df1.zip |
sm: Use estream for I/O.
* 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 (struct server_local_s): Rename MESSAGE_FD to
MESSAGE_FP.
(close_message_fp): Rename from close_message_fd.
(reset_notify): Follow the change of close_message_fp.
(cmd_encrypt, cmd_decrypt, cmd_verify, cmd_sign): Follow the change of
close_message_fp. Use open_stream_nc to get estream.
(cmd_import): Likewise.
(cmd_export, cmd_delkeys, gpgsm_server): Follow the change of
close_message_fp.
(cmd_message): Setup MESSAGE_FP with open_stream_nc.
* sm/sign.c (hash_data): Use estream for the input.
(hash_and_copy_data): Likewise.
(gpgsm_sign): Likewise.
* sm/verify.c (hash_data): Use estream_t for FP.
(gpgsm_verify): Use estream_t for IN_FP and DATA_FP.
--
GnuPG-bug-id: 6592
Signed-off-by: NIIBE Yutaka <[email protected]>
Diffstat (limited to 'sm/import.c')
-rw-r--r-- | sm/import.c | 51 |
1 files changed, 20 insertions, 31 deletions
diff --git a/sm/import.c b/sm/import.c index 5a193ef52..cd28cfbff 100644 --- a/sm/import.c +++ b/sm/import.c @@ -37,6 +37,10 @@ #include "../common/membuf.h" #include "minip12.h" +#ifndef O_BINARY +#define O_BINARY 0 +#endif + /* The arbitrary limit of one PKCS#12 object. */ #define MAX_P12OBJ_SIZE 128 /*kb*/ @@ -269,25 +273,16 @@ check_and_store (ctrl_t ctrl, struct stats_s *stats, static int -import_one (ctrl_t ctrl, struct stats_s *stats, int in_fd) +import_one (ctrl_t ctrl, struct stats_s *stats, estream_t fp) { int rc; gnupg_ksba_io_t b64reader = NULL; ksba_reader_t reader; ksba_cert_t cert = NULL; ksba_cms_t cms = NULL; - estream_t fp = NULL; ksba_content_type_t ct; int any = 0; - fp = es_fdopen_nc (in_fd, "rb"); - if (!fp) - { - rc = gpg_error_from_syserror (); - log_error ("fdopen() failed: %s\n", strerror (errno)); - goto leave; - } - rc = gnupg_ksba_create_reader (&b64reader, ((ctrl->is_pem? GNUPG_KSBA_IO_PEM : 0) | (ctrl->is_base64? GNUPG_KSBA_IO_BASE64 : 0) @@ -388,7 +383,6 @@ import_one (ctrl_t ctrl, struct stats_s *stats, int in_fd) ksba_cms_release (cms); ksba_cert_release (cert); gnupg_ksba_destroy_reader (b64reader); - es_fclose (fp); return rc; } @@ -398,10 +392,9 @@ import_one (ctrl_t ctrl, struct stats_s *stats, int in_fd) fingerprints t re-import. The actual re-import is done by clearing the ephemeral flag. */ static int -reimport_one (ctrl_t ctrl, struct stats_s *stats, int in_fd) +reimport_one (ctrl_t ctrl, struct stats_s *stats, estream_t fp) { gpg_error_t err = 0; - estream_t fp = NULL; char line[100]; /* Sufficient for a fingerprint. */ KEYDB_HANDLE kh; KEYDB_SEARCH_DESC desc; @@ -417,14 +410,6 @@ reimport_one (ctrl_t ctrl, struct stats_s *stats, int in_fd) } keydb_set_ephemeral (kh, 1); - fp = es_fdopen_nc (in_fd, "r"); - if (!fp) - { - err = gpg_error_from_syserror (); - log_error ("es_fdopen(%d) failed: %s\n", in_fd, gpg_strerror (err)); - goto leave; - } - while (es_fgets (line, DIM(line)-1, fp) ) { if (*line && line[strlen(line)-1] != '\n') @@ -500,30 +485,29 @@ reimport_one (ctrl_t ctrl, struct stats_s *stats, int in_fd) if (es_ferror (fp)) { err = gpg_error_from_syserror (); - log_error ("error reading fd %d: %s\n", in_fd, gpg_strerror (err)); + log_error ("error reading fp %p: %s\n", fp, gpg_strerror (err)); goto leave; } leave: ksba_cert_release (cert); keydb_release (kh); - es_fclose (fp); return err; } int -gpgsm_import (ctrl_t ctrl, int in_fd, int reimport_mode) +gpgsm_import (ctrl_t ctrl, estream_t in_fp, int reimport_mode) { int rc; struct stats_s stats; memset (&stats, 0, sizeof stats); if (reimport_mode) - rc = reimport_one (ctrl, &stats, in_fd); + rc = reimport_one (ctrl, &stats, in_fp); else - rc = import_one (ctrl, &stats, in_fd); + rc = import_one (ctrl, &stats, in_fp); print_imported_summary (ctrl, &stats); /* If we never printed an error message do it now so that a command line invocation will return with an error (log_error keeps a @@ -536,7 +520,7 @@ gpgsm_import (ctrl_t ctrl, int in_fd, int reimport_mode) int gpgsm_import_files (ctrl_t ctrl, int nfiles, char **files, - int (*of)(const char *fname)) + estream_t (*of)(const char *fname, const char *mode)) { int rc = 0; struct stats_s stats; @@ -544,14 +528,19 @@ gpgsm_import_files (ctrl_t ctrl, int nfiles, char **files, memset (&stats, 0, sizeof stats); if (!nfiles) - rc = import_one (ctrl, &stats, 0); + { +#ifdef HAVE_DOSISH_SYSTEM + setmode (0, O_BINARY); +#endif + rc = import_one (ctrl, &stats, es_stdin); + } else { for (; nfiles && !rc ; nfiles--, files++) { - int fd = of (*files); - rc = import_one (ctrl, &stats, fd); - close (fd); + estream_t fp = of (*files, "rb"); + rc = import_one (ctrl, &stats, fp); + es_fclose (fp); if (rc == -1/* legacy*/ || gpg_err_code (rc) == GPG_ERR_NOT_FOUND) rc = 0; } |