1
0
mirror of git://git.gnupg.org/gnupg.git synced 2024-12-22 10:19:57 +01:00

Use gpgrt_b64* API of libgpg-error.

* common/Makefile.am (common_sources): Remove b64enc.c and b64dec.c.
(module_maint_tests): Remove t-b64.
(t_b64_LDADD): Remove.
* common/util.h: Remove the internal API.
* common/ssh-utils.c (get_fingerprint): Use the gpgrt_b64 API.
(ssh_public_key_in_base64): Likewise.
* dirmngr/crlfetch.c (my_es_read, crl_close_reader): Likewise.
* dirmngr/dirmngr-client.c (data_cb, do_lookup): Likewise.
* dirmngr/misc.c (armor_data): Likewise.
* g10/export.c (export_one_ssh_key, export_secret_ssh_key): Likewise.
* tools/gpg-card.c (cmd_writecert): Likewise.
* tools/mime-parser.c (parse_message_cb, mime_parser_release)
(process_part_data): Likewise.
* tools/wks-util.c (wks_armor_key): Likewise.

--

GnuPG-bug-id: 6734
Signed-off-by: NIIBE Yutaka <gniibe@fsij.org>
This commit is contained in:
NIIBE Yutaka 2023-09-26 13:34:50 +09:00
parent 34f812475e
commit 26939ea222
No known key found for this signature in database
GPG Key ID: 640114AF89DE6054
10 changed files with 75 additions and 99 deletions

View File

@ -65,7 +65,7 @@ common_sources = \
homedir.c \ homedir.c \
gettime.c gettime.h \ gettime.c gettime.h \
yesno.c \ yesno.c \
b64enc.c b64dec.c zb32.c zb32.h \ zb32.c zb32.h \
convert.c \ convert.c \
percent.c \ percent.c \
mbox-util.c mbox-util.h \ mbox-util.c mbox-util.h \
@ -169,7 +169,7 @@ module_tests += t-exectool
endif endif
if MAINTAINER_MODE if MAINTAINER_MODE
module_maint_tests = t-helpfile t-b64 module_maint_tests = t-helpfile
else else
module_maint_tests = module_maint_tests =
endif endif
@ -196,7 +196,6 @@ t_gettime_LDADD = $(t_common_ldadd)
t_sysutils_LDADD = $(t_common_ldadd) t_sysutils_LDADD = $(t_common_ldadd)
t_helpfile_LDADD = $(t_common_ldadd) t_helpfile_LDADD = $(t_common_ldadd)
t_sexputil_LDADD = $(t_common_ldadd) t_sexputil_LDADD = $(t_common_ldadd)
t_b64_LDADD = $(t_common_ldadd)
t_exechelp_LDADD = $(t_common_ldadd) t_exechelp_LDADD = $(t_common_ldadd)
t_exectool_LDADD = $(t_common_ldadd) t_exectool_LDADD = $(t_common_ldadd)
t_session_env_LDADD = $(t_common_ldadd) t_session_env_LDADD = $(t_common_ldadd)

View File

@ -259,7 +259,7 @@ get_fingerprint (gcry_sexp_t key, int algo,
} }
else else
{ {
struct b64state b64s; gpgrt_b64state_t b64s;
estream_t stream; estream_t stream;
char *p; char *p;
long int len; long int len;
@ -273,15 +273,15 @@ get_fingerprint (gcry_sexp_t key, int algo,
goto leave; goto leave;
} }
err = b64enc_start_es (&b64s, stream, ""); b64s = gpgrt_b64enc_start (stream, "");
if (err) if (!b64s)
{ {
es_fclose (stream); es_fclose (stream);
goto leave; goto leave;
} }
err = b64enc_write (&b64s, err = gpgrt_b64enc_write (b64s, gcry_md_read (md, algo),
gcry_md_read (md, algo), gcry_md_get_algo_dlen (algo)); gcry_md_get_algo_dlen (algo));
if (err) if (err)
{ {
es_fclose (stream); es_fclose (stream);
@ -289,7 +289,7 @@ get_fingerprint (gcry_sexp_t key, int algo,
} }
/* Finish, get the length, and close the stream. */ /* Finish, get the length, and close the stream. */
err = b64enc_finish (&b64s); err = gpgrt_b64enc_finish (b64s);
len = es_ftell (stream); len = es_ftell (stream);
es_fclose (stream); es_fclose (stream);
if (err) if (err)
@ -566,7 +566,7 @@ ssh_public_key_in_base64 (gcry_sexp_t key, estream_t stream,
const char *identifier = NULL; const char *identifier = NULL;
void *blob = NULL; void *blob = NULL;
size_t bloblen; size_t bloblen;
struct b64state b64_state; gpgrt_b64state_t b64_state;
algo = get_pk_algo_from_key (key); algo = get_pk_algo_from_key (key);
if (algo == 0) if (algo == 0)
@ -624,15 +624,15 @@ ssh_public_key_in_base64 (gcry_sexp_t key, estream_t stream,
es_fprintf (stream, "%s ", identifier); es_fprintf (stream, "%s ", identifier);
err = b64enc_start_es (&b64_state, stream, ""); b64_state = gpgrt_b64enc_start (stream, "");
if (err) if (!b64_state)
{ {
es_free (blob); es_free (blob);
return err; return gpg_error_from_syserror ();
} }
err = b64enc_write (&b64_state, blob, bloblen); err = gpgrt_b64enc_write (b64_state, blob, bloblen);
b64enc_finish (&b64_state); gpgrt_b64enc_finish (b64_state);
es_free (blob); es_free (blob);
if (err) if (err)
return err; return err;

View File

@ -143,35 +143,6 @@ ssize_t read_line (FILE *fp,
char **addr_of_buffer, size_t *length_of_buffer, char **addr_of_buffer, size_t *length_of_buffer,
size_t *max_length); size_t *max_length);
/*-- b64enc.c and b64dec.c --*/
struct b64state
{
unsigned int flags;
int idx;
int quad_count;
FILE *fp;
estream_t stream;
char *title;
unsigned char radbuf[4];
u32 crc;
int stop_seen:1;
int invalid_encoding:1;
gpg_error_t lasterr;
};
gpg_error_t b64enc_start (struct b64state *state, FILE *fp, const char *title);
gpg_error_t b64enc_start_es (struct b64state *state, estream_t fp,
const char *title);
gpg_error_t b64enc_write (struct b64state *state,
const void *buffer, size_t nbytes);
gpg_error_t b64enc_finish (struct b64state *state);
gpg_error_t b64dec_start (struct b64state *state, const char *title);
gpg_error_t b64dec_proc (struct b64state *state, void *buffer, size_t length,
size_t *r_nbytes);
gpg_error_t b64dec_finish (struct b64state *state);
/*-- sexputil.c */ /*-- sexputil.c */
char *canon_sexp_to_string (const unsigned char *canon, size_t canonlen); char *canon_sexp_to_string (const unsigned char *canon, size_t canonlen);
void log_printcanon (const char *text, void log_printcanon (const char *text,

View File

@ -40,9 +40,9 @@
struct reader_cb_context_s struct reader_cb_context_s
{ {
estream_t fp; /* The stream used with the ksba reader. */ estream_t fp; /* The stream used with the ksba reader. */
int checked:1; /* PEM/binary detection ahs been done. */ unsigned int checked:1; /* PEM/binary detection ahs been done. */
int is_pem:1; /* The file stream is PEM encoded. */ unsigned int is_pem:1; /* The file stream is PEM encoded. */
struct b64state b64state; /* The state used for Base64 decoding. */ gpgrt_b64state_t b64state; /* The state used for Base64 decoding. */
}; };
@ -126,14 +126,16 @@ my_es_read (void *opaque, char *buffer, size_t nbytes, size_t *nread)
else else
{ {
cb_ctx->is_pem = 1; cb_ctx->is_pem = 1;
b64dec_start (&cb_ctx->b64state, ""); cb_ctx->b64state = gpgrt_b64dec_start ("");
if (!cb_ctx->b64state)
return gpg_error_from_syserror ();
} }
} }
if (cb_ctx->is_pem && *nread) if (cb_ctx->is_pem && *nread)
{ {
size_t nread2; size_t nread2;
if (b64dec_proc (&cb_ctx->b64state, buffer, *nread, &nread2)) if (gpgrt_b64dec_proc (cb_ctx->b64state, buffer, *nread, &nread2))
{ {
/* EOF from decoder. */ /* EOF from decoder. */
*nread = 0; *nread = 0;
@ -581,7 +583,7 @@ crl_close_reader (ksba_reader_t reader)
es_fclose (cb_ctx->fp); es_fclose (cb_ctx->fp);
/* Release the base64 decoder state. */ /* Release the base64 decoder state. */
if (cb_ctx->is_pem) if (cb_ctx->is_pem)
b64dec_finish (&cb_ctx->b64state); gpgrt_b64dec_finish (cb_ctx->b64state);
/* Release the callback context. */ /* Release the callback context. */
xfree (cb_ctx); xfree (cb_ctx);
} }

View File

@ -441,11 +441,11 @@ static gpg_error_t
data_cb (void *opaque, const void *buffer, size_t length) data_cb (void *opaque, const void *buffer, size_t length)
{ {
gpg_error_t err; gpg_error_t err;
struct b64state *state = opaque; gpgrt_b64state_t state = opaque;
if (buffer) if (buffer)
{ {
err = b64enc_write (state, buffer, length); err = gpgrt_b64enc_write (state, buffer, length);
if (err) if (err)
log_error (_("error writing base64 encoding: %s\n"), log_error (_("error writing base64 encoding: %s\n"),
gpg_strerror (err)); gpg_strerror (err));
@ -853,14 +853,14 @@ do_lookup (assuan_context_t ctx, const char *pattern)
gpg_error_t err; gpg_error_t err;
const unsigned char *s; const unsigned char *s;
char *line, *p; char *line, *p;
struct b64state state; gpgrt_b64state_t state;
if (opt.verbose) if (opt.verbose)
log_info (_("looking up '%s'\n"), pattern); log_info (_("looking up '%s'\n"), pattern);
err = b64enc_start (&state, stdout, NULL); state = gpgrt_b64enc_start (es_stdout, NULL);
if (err) if (!state)
return err; return gpg_error_from_syserror ();
line = xmalloc (10 + 6 + 13 + strlen (pattern)*3 + 1); line = xmalloc (10 + 6 + 13 + strlen (pattern)*3 + 1);
@ -885,13 +885,13 @@ do_lookup (assuan_context_t ctx, const char *pattern)
err = assuan_transact (ctx, line, err = assuan_transact (ctx, line,
data_cb, &state, data_cb, state,
NULL, NULL, NULL, NULL,
status_cb, NULL); status_cb, NULL);
if (opt.verbose > 1) if (opt.verbose > 1)
log_info ("response of dirmngr: %s\n", err? gpg_strerror (err): "okay"); log_info ("response of dirmngr: %s\n", err? gpg_strerror (err): "okay");
err = b64enc_finish (&state); err = gpgrt_b64enc_finish (state);
xfree (line); xfree (line);
return err; return err;

View File

@ -583,7 +583,7 @@ gpg_error_t
armor_data (char **r_string, const void *data, size_t datalen) armor_data (char **r_string, const void *data, size_t datalen)
{ {
gpg_error_t err; gpg_error_t err;
struct b64state b64state; gpgrt_b64state_t b64state;
estream_t fp; estream_t fp;
long length; long length;
char *buffer; char *buffer;
@ -595,9 +595,15 @@ armor_data (char **r_string, const void *data, size_t datalen)
if (!fp) if (!fp)
return gpg_error_from_syserror (); return gpg_error_from_syserror ();
if ((err=b64enc_start_es (&b64state, fp, "PGP PUBLIC KEY BLOCK")) b64state = gpgrt_b64enc_start (fp, "PGP PUBLIC KEY BLOCK");
|| (err=b64enc_write (&b64state, data, datalen)) if (!b64state)
|| (err = b64enc_finish (&b64state))) {
es_fclose (fp);
return gpg_error_from_syserror ();
}
if ((err = gpgrt_b64enc_write (b64state, data, datalen))
|| (err = gpgrt_b64enc_finish (b64state)))
{ {
es_fclose (fp); es_fclose (fp);
return err; return err;

View File

@ -2706,18 +2706,18 @@ export_one_ssh_key (estream_t fp, PKT_public_key *pk)
blob = get_membuf (&mb, &bloblen); blob = get_membuf (&mb, &bloblen);
if (blob) if (blob)
{ {
struct b64state b64_state; gpgrt_b64state_t b64_state;
es_fprintf (fp, "%s ", identifier); es_fprintf (fp, "%s ", identifier);
err = b64enc_start_es (&b64_state, fp, ""); b64_state = gpgrt_b64enc_start (fp, "");
if (err) if (!b64_state)
{ {
xfree (blob); xfree (blob);
goto leave; goto leave;
} }
err = b64enc_write (&b64_state, blob, bloblen); err = gpgrt_b64enc_write (b64_state, blob, bloblen);
b64enc_finish (&b64_state); gpgrt_b64enc_finish (b64_state);
es_fprintf (fp, " openpgp:0x%08lX\n", (ulong)keyid_from_pk (pk, NULL)); es_fprintf (fp, " openpgp:0x%08lX\n", (ulong)keyid_from_pk (pk, NULL));
xfree (blob); xfree (blob);
@ -2961,7 +2961,7 @@ export_secret_ssh_key (ctrl_t ctrl, const char *userid)
int pkalgo; int pkalgo;
int i; int i;
gcry_mpi_t keyparam[10] = { NULL }; gcry_mpi_t keyparam[10] = { NULL };
struct b64state b64_state; gpgrt_b64state_t b64_state;
init_membuf_secure (&mb, 1024); init_membuf_secure (&mb, 1024);
init_membuf_secure (&mb2, 1024); init_membuf_secure (&mb2, 1024);
@ -3139,11 +3139,11 @@ export_secret_ssh_key (ctrl_t ctrl, const char *userid)
goto leave; goto leave;
} }
err = b64enc_start_es (&b64_state, fp, "OPENSSH PRIVATE_KEY"); b64_state = gpgrt_b64enc_start (fp, "OPENSSH PRIVATE_KEY");
if (err) if (!b64_state)
goto leave; goto leave;
err = b64enc_write (&b64_state, blob, bloblen); err = gpgrt_b64enc_write (b64_state, blob, bloblen);
b64enc_finish (&b64_state); gpgrt_b64enc_finish (b64_state);
if (err) if (err)
goto leave; goto leave;

View File

@ -2080,13 +2080,15 @@ cmd_writecert (card_info_t info, char *argstr)
&& ascii_memistr (data, datalen, "-----END CERTIFICATE-----") && ascii_memistr (data, datalen, "-----END CERTIFICATE-----")
&& !memchr (data, 0, datalen) && !memchr (data, 1, datalen)) && !memchr (data, 0, datalen) && !memchr (data, 1, datalen))
{ {
struct b64state b64; gpgrt_b64state_t b64;
err = b64dec_start (&b64, ""); b64 = gpgrt_b64dec_start ("");
if (!b64)
err = gpg_error_from_syserror ();
else
err = gpgrt_b64dec_proc (b64, data, datalen, &datalen);
if (!err) if (!err)
err = b64dec_proc (&b64, data, datalen, &datalen); err = gpgrt_b64dec_finish (b64);
if (!err)
err = b64dec_finish (&b64);
if (err) if (err)
goto leave; goto leave;
} }

View File

@ -93,7 +93,7 @@ struct mime_parser_context_s
unsigned int boundary : 1; unsigned int boundary : 1;
} show; } show;
struct b64state *b64state; /* NULL or malloced Base64 decoder state. */ gpgrt_b64state_t b64state; /* NULL or malloced Base64 decoder state. */
/* A buffer for reading a mail line, */ /* A buffer for reading a mail line, */
char line[5000]; char line[5000];
@ -410,16 +410,12 @@ parse_message_cb (void *opaque, rfc822parse_event_t event, rfc822parse_t msg)
{ {
ctx->decode_part = 2; ctx->decode_part = 2;
if (ctx->b64state) if (ctx->b64state)
b64dec_finish (ctx->b64state); /* Reuse state. */ gpgrt_b64dec_finish (ctx->b64state); /* Release. */
else
{ ctx->b64state = gpgrt_b64dec_start (NULL);
ctx->b64state = xtrymalloc (sizeof *ctx->b64state);
if (!ctx->b64state) if (!ctx->b64state)
rc = gpg_error_from_syserror (); rc = gpg_error_from_syserror ();
} }
if (!rc)
rc = b64dec_start (ctx->b64state, NULL);
}
free (value); /* Right, we need a plain free. */ free (value); /* Right, we need a plain free. */
} }
} }
@ -528,10 +524,7 @@ mime_parser_release (mime_parser_t ctx)
return; return;
if (ctx->b64state) if (ctx->b64state)
{ gpgrt_b64dec_finish (ctx->b64state);
b64dec_finish (ctx->b64state);
xfree (ctx->b64state);
}
xfree (ctx); xfree (ctx);
} }
@ -661,7 +654,7 @@ process_part_data (mime_parser_t ctx, char *line, size_t *length)
else if (ctx->decode_part == 2) else if (ctx->decode_part == 2)
{ {
log_assert (ctx->b64state); log_assert (ctx->b64state);
err = b64dec_proc (ctx->b64state, line, *length, &nbytes); err = gpgrt_b64dec_proc (ctx->b64state, line, *length, &nbytes);
if (err) if (err)
return err; return err;
*length = nbytes; *length = nbytes;

View File

@ -598,7 +598,7 @@ wks_armor_key (estream_t *r_newkey, estream_t key, const char *prefix)
{ {
gpg_error_t err; gpg_error_t err;
estream_t newkey; estream_t newkey;
struct b64state b64state; gpgrt_b64state_t b64state;
char buffer[4096]; char buffer[4096];
size_t nread; size_t nread;
@ -614,16 +614,19 @@ wks_armor_key (estream_t *r_newkey, estream_t key, const char *prefix)
if (prefix) if (prefix)
es_fputs (prefix, newkey); es_fputs (prefix, newkey);
err = b64enc_start_es (&b64state, newkey, "PGP PUBLIC KEY BLOCK"); b64state = gpgrt_b64enc_start (newkey, "PGP PUBLIC KEY BLOCK");
if (err) if (!b64state)
{
err = gpg_error_from_syserror ();
goto leave; goto leave;
}
do do
{ {
nread = es_fread (buffer, 1, sizeof buffer, key); nread = es_fread (buffer, 1, sizeof buffer, key);
if (!nread) if (!nread)
break; break;
err = b64enc_write (&b64state, buffer, nread); err = gpgrt_b64enc_write (b64state, buffer, nread);
if (err) if (err)
goto leave; goto leave;
} }
@ -634,7 +637,7 @@ wks_armor_key (estream_t *r_newkey, estream_t key, const char *prefix)
goto leave; goto leave;
} }
err = b64enc_finish (&b64state); err = gpgrt_b64enc_finish (b64state);
if (err) if (err)
goto leave; goto leave;