summaryrefslogtreecommitdiffstats
path: root/common/ksba-io-support.c
diff options
context:
space:
mode:
Diffstat (limited to 'common/ksba-io-support.c')
-rw-r--r--common/ksba-io-support.c106
1 files changed, 71 insertions, 35 deletions
diff --git a/common/ksba-io-support.c b/common/ksba-io-support.c
index da7b4d6c7..48a7ac1cc 100644
--- a/common/ksba-io-support.c
+++ b/common/ksba-io-support.c
@@ -36,13 +36,12 @@
#include <unistd.h>
#include <time.h>
#include <assert.h>
-
-#include "../sm/gpgsm.h"
-
-
#include <ksba.h>
+#include "util.h"
#include "i18n.h"
+#include "ksba-io-support.h"
+
#ifdef HAVE_DOSISH_SYSTEM
#define LF "\r\n"
@@ -50,6 +49,7 @@
#define LF "\n"
#endif
+
/* Data used by the reader callbacks. */
struct reader_cb_parm_s
{
@@ -87,7 +87,7 @@ struct writer_cb_parm_s
{
estream_t stream; /* Output stream. */
- const char *pem_name;
+ char *pem_name; /* Malloced. */
int wrote_begin;
int did_finish;
@@ -550,18 +550,30 @@ base64_finish_write (struct writer_cb_parm_s *parm)
-/* Create a reader for the given file descriptor. Depending on the
- control information an input decoding is automagically chosen.
- The function returns a Base64Context object which must be passed to
- the gpgme_destroy_reader function. The created KsbaReader object
- is also returned, but the caller must not call the
- ksba_reader_release function on. If ALLOW_MULTI_PEM is true, the
- reader expects that the caller uses ksba_reader_clear after EOF
- until no more objects were found. */
-int
-gpgsm_create_reader (Base64Context *ctx,
- ctrl_t ctrl, estream_t fp, int allow_multi_pem,
- ksba_reader_t *r_reader)
+/* Create a reader for the stream FP. FLAGS can be used to specify
+ * the expected input encoding.
+ *
+ * The function returns a Base64Context object which must be passed to
+ * the gpgme_destroy_reader function. The created ksba_reader_t
+ * object is stored at R_READER - the caller must not call the
+ * ksba_reader_release function on.
+ *
+ * The supported flags are:
+ *
+ * GNUPG_KSBA_IO_PEM - Assume the input is PEM encoded
+ * GNUPG_KSBA_IO_BASE64 - Assume the input is Base64 encoded.
+ * GNUPG_KSBA_IO_AUTODETECT - The reader tries to detect the encoding.
+ * GNUPG_KSBA_IO_MULTIPEM - The reader expects that the caller uses
+ * ksba_reader_clear after EOF until no more
+ * objects were found.
+ *
+ * Note that the PEM flag has a higher priority than the BASE64 flag
+ * which in turn has a gight priority than the AUTODETECT flag.
+ */
+gpg_error_t
+gnupg_ksba_create_reader (Base64Context *ctx,
+ unsigned int flags, estream_t fp,
+ ksba_reader_t *r_reader)
{
int rc;
ksba_reader_t r;
@@ -570,7 +582,7 @@ gpgsm_create_reader (Base64Context *ctx,
*ctx = xtrycalloc (1, sizeof **ctx);
if (!*ctx)
return out_of_core ();
- (*ctx)->u.rparm.allow_multi_pem = allow_multi_pem;
+ (*ctx)->u.rparm.allow_multi_pem = !!(flags & GNUPG_KSBA_IO_MULTIPEM);
rc = ksba_reader_new (&r);
if (rc)
@@ -580,18 +592,18 @@ gpgsm_create_reader (Base64Context *ctx,
}
(*ctx)->u.rparm.fp = fp;
- if (ctrl->is_pem)
+ if ((flags & GNUPG_KSBA_IO_PEM))
{
(*ctx)->u.rparm.assume_pem = 1;
(*ctx)->u.rparm.assume_base64 = 1;
rc = ksba_reader_set_cb (r, base64_reader_cb, &(*ctx)->u.rparm);
}
- else if (ctrl->is_base64)
+ else if ((flags & GNUPG_KSBA_IO_BASE64))
{
(*ctx)->u.rparm.assume_base64 = 1;
rc = ksba_reader_set_cb (r, base64_reader_cb, &(*ctx)->u.rparm);
}
- else if (ctrl->autodetect_encoding)
+ else if ((flags & GNUPG_KSBA_IO_AUTODETECT))
{
(*ctx)->u.rparm.autodetect = 1;
rc = ksba_reader_set_cb (r, base64_reader_cb, &(*ctx)->u.rparm);
@@ -630,15 +642,27 @@ gpgsm_destroy_reader (Base64Context ctx)
-/* Create a writer for the given STREAM. Depending on
- the control information an output encoding is automagically
- chosen. The function returns a Base64Context object which must be
- passed to the gpgme_destroy_writer function. The created
- KsbaWriter object is also returned, but the caller must not call
- the ksba_reader_release function on it. */
-int
-gpgsm_create_writer (Base64Context *ctx, ctrl_t ctrl, estream_t stream,
- ksba_writer_t *r_writer)
+/* Create a writer for the given STREAM. Depending on FLAGS an output
+ * encoding is chosen. In PEM mode PEM_NAME is used for the header
+ * and footer lines; if PEM_NAME is NULL the string "CMS OBJECT" is
+ * used.
+ *
+ * The function returns a Base64Context object which must be passed to
+ * the gpgme_destroy_writer function. The created ksba_writer_t
+ * object is stored at R_WRITER - the caller must not call the
+ * ksba_reader_release function on it.
+ *
+ * The supported flags are:
+ *
+ * GNUPG_KSBA_IO_PEM - Write output as PEM
+ * GNUPG_KSBA_IO_BASE64 - Write output as plain Base64; note that the PEM
+ * flag overrides this flag.
+ *
+ */
+gpg_error_t
+gnupg_ksba_create_writer (Base64Context *ctx, unsigned int flags,
+ const char *pem_name, estream_t stream,
+ ksba_writer_t *r_writer)
{
int rc;
ksba_writer_t w;
@@ -646,7 +670,7 @@ gpgsm_create_writer (Base64Context *ctx, ctrl_t ctrl, estream_t stream,
*r_writer = NULL;
*ctx = xtrycalloc (1, sizeof **ctx);
if (!*ctx)
- return out_of_core ();
+ return gpg_error_from_syserror ();
rc = ksba_writer_new (&w);
if (rc)
@@ -655,12 +679,22 @@ gpgsm_create_writer (Base64Context *ctx, ctrl_t ctrl, estream_t stream,
return rc;
}
- if (ctrl->create_pem || ctrl->create_base64)
+ if ((flags & GNUPG_KSBA_IO_PEM) || (flags & GNUPG_KSBA_IO_BASE64))
{
(*ctx)->u.wparm.stream = stream;
- if (ctrl->create_pem)
- (*ctx)->u.wparm.pem_name = ctrl->pem_name? ctrl->pem_name
- : "CMS OBJECT";
+ if ((flags & GNUPG_KSBA_IO_PEM))
+ {
+ (*ctx)->u.wparm.pem_name = xtrystrdup (pem_name
+ ? pem_name
+ : "CMS OBJECT");
+ if (!(*ctx)->u.wparm.pem_name)
+ {
+ rc = gpg_error_from_syserror ();
+ ksba_writer_release (w);
+ xfree (*ctx); *ctx = NULL;
+ return rc;
+ }
+ }
rc = ksba_writer_set_cb (w, base64_writer_cb, &(*ctx)->u.wparm);
}
else if (stream)
@@ -700,6 +734,7 @@ gpgsm_finish_writer (Base64Context ctx)
return base64_finish_write (parm);
}
+
void
gpgsm_destroy_writer (Base64Context ctx)
{
@@ -707,5 +742,6 @@ gpgsm_destroy_writer (Base64Context ctx)
return;
ksba_writer_release (ctx->u2.writer);
+ xfree (ctx->u.wparm.pem_name);
xfree (ctx);
}