diff --git a/common/Makefile.am b/common/Makefile.am index 094ebe6e2..31924317e 100644 --- a/common/Makefile.am +++ b/common/Makefile.am @@ -1,228 +1,228 @@ # Makefile for common gnupg modules # Copyright (C) 2001, 2003, 2007, 2010 Free Software Foundation, Inc. # # 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 # the Free Software Foundation; either version 3 of the License, or # (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 # along with this program; if not, see . ## Process this file with automake to produce Makefile.in EXTRA_DIST = mkstrtable.awk exaudit.awk exstatus.awk ChangeLog-2011 \ audit-events.h status-codes.h ChangeLog.jnlib \ ChangeLog-2011.include w32info-rc.h.in gnupg.ico \ all-tests.scm noinst_LIBRARIES = libcommon.a libcommonpth.a libgpgrl.a if !HAVE_W32CE_SYSTEM noinst_LIBRARIES += libsimple-pwquery.a endif noinst_PROGRAMS = $(module_tests) $(module_maint_tests) TESTS = $(module_tests) BUILT_SOURCES = audit-events.h status-codes.h MAINTAINERCLEANFILES = audit-events.h status-codes.h AM_CPPFLAGS = AM_CFLAGS = $(LIBGCRYPT_CFLAGS) $(LIBASSUAN_CFLAGS) $(KSBA_CFLAGS) include $(top_srcdir)/am/cmacros.am common_sources = \ common-defs.h \ util.h utilproto.h fwddecl.h i18n.c i18n.h \ types.h host2net.h dynload.h w32help.h \ mapstrings.c stringhelp.c stringhelp.h \ strlist.c strlist.h \ utf8conv.c utf8conv.h \ logging.h \ dotlock.c dotlock.h \ mischelp.c mischelp.h \ status.c status.h\ shareddefs.h \ openpgpdefs.h \ gc-opt-flags.h \ keyserver.h \ sexp-parse.h \ - tlv.c tlv.h \ + tlv.c tlv.h tlv-builder.c \ init.c init.h \ sexputil.c \ sysutils.c sysutils.h \ homedir.c \ gettime.c gettime.h \ yesno.c \ b64enc.c b64dec.c zb32.c zb32.h \ convert.c \ percent.c \ mbox-util.c mbox-util.h \ miscellaneous.c \ xasprintf.c \ xreadline.c \ membuf.c membuf.h \ ccparray.c ccparray.h \ iobuf.c iobuf.h \ ttyio.c ttyio.h \ asshelp.c asshelp2.c asshelp.h \ exechelp.h \ signal.c \ audit.c audit.h \ localename.c \ session-env.c session-env.h \ userids.c userids.h \ openpgp-oid.c openpgp-s2k.c \ ssh-utils.c ssh-utils.h \ agent-opt.c \ helpfile.c \ mkdir_p.c mkdir_p.h \ strlist.c strlist.h \ exectool.c exectool.h \ server-help.c server-help.h \ name-value.c name-value.h \ recsel.c recsel.h \ ksba-io-support.c ksba-io-support.h \ compliance.c compliance.h \ pkscreening.c pkscreening.h if HAVE_W32_SYSTEM common_sources += w32-reg.c endif # To make the code easier to read we have split home some code into # separate source files. if HAVE_W32_SYSTEM if HAVE_W32CE_SYSTEM common_sources += exechelp-w32ce.c else common_sources += exechelp-w32.c endif else common_sources += exechelp-posix.c endif # Sources only useful without NPTH. without_npth_sources = \ get-passphrase.c get-passphrase.h # Sources only useful with NPTH. with_npth_sources = \ call-gpg.c call-gpg.h libcommon_a_SOURCES = $(common_sources) $(without_npth_sources) libcommon_a_CFLAGS = $(AM_CFLAGS) $(LIBASSUAN_CFLAGS) -DWITHOUT_NPTH=1 libcommonpth_a_SOURCES = $(common_sources) $(with_npth_sources) libcommonpth_a_CFLAGS = $(AM_CFLAGS) $(LIBASSUAN_CFLAGS) $(NPTH_CFLAGS) if !HAVE_W32CE_SYSTEM libsimple_pwquery_a_SOURCES = \ simple-pwquery.c simple-pwquery.h asshelp.c asshelp.h libsimple_pwquery_a_CFLAGS = $(AM_CFLAGS) $(LIBASSUAN_CFLAGS) endif libgpgrl_a_SOURCES = \ gpgrlhelp.c if MAINTAINER_MODE # Note: Due to the dependency on Makefile, the file will always be # rebuilt, so we allow this only in maintainer mode. # Create the audit-events.h include file from audit.h # Note: We create the target file in the source directory because it # is a distributed built source. If we would not do that we may end # up with two files and then it is not clear which version of the # files will be picked up. audit-events.h: Makefile.am mkstrtable.awk exaudit.awk audit.h $(AWK) -f $(srcdir)/exaudit.awk $(srcdir)/audit.h \ | $(AWK) -f $(srcdir)/mkstrtable.awk -v textidx=3 -v nogettext=1 \ -v pkg_namespace=eventstr_ > $(srcdir)/audit-events.h # Create the status-codes.h include file from status.h status-codes.h: Makefile.am mkstrtable.awk exstatus.awk status.h $(AWK) -f $(srcdir)/exstatus.awk $(srcdir)/status.h \ | $(AWK) -f $(srcdir)/mkstrtable.awk -v textidx=3 -v nogettext=1 \ -v pkg_namespace=statusstr_ > $(srcdir)/status-codes.h endif # # Module tests # module_tests = t-stringhelp t-timestuff \ t-convert t-percent t-gettime t-sysutils t-sexputil \ t-session-env t-openpgp-oid t-ssh-utils \ t-mapstrings t-zb32 t-mbox-util t-iobuf t-strlist \ t-name-value t-ccparray t-recsel if !HAVE_W32CE_SYSTEM module_tests += t-exechelp t-exectool endif if HAVE_W32_SYSTEM module_tests += t-w32-reg endif if MAINTAINER_MODE module_maint_tests = t-helpfile t-b64 else module_maint_tests = endif t_extra_src = t-support.h t_common_cflags = $(KSBA_CFLAGS) $(LIBGCRYPT_CFLAGS) \ $(LIBASSUAN_CFLAGS) $(GPG_ERROR_CFLAGS) $(INCICONV) t_common_ldadd = libcommon.a \ $(LIBGCRYPT_LIBS) $(LIBASSUAN_LIBS) $(GPG_ERROR_LIBS) \ $(LIBINTL) $(LIBICONV) # Common tests t_stringhelp_SOURCES = t-stringhelp.c $(t_extra_src) t_stringhelp_LDADD = $(t_common_ldadd) t_timestuff_SOURCES = t-timestuff.c $(t_extra_src) t_timestuff_LDADD = $(t_common_ldadd) t_convert_LDADD = $(t_common_ldadd) t_percent_LDADD = $(t_common_ldadd) t_gettime_LDADD = $(t_common_ldadd) t_sysutils_LDADD = $(t_common_ldadd) t_helpfile_LDADD = $(t_common_ldadd) t_sexputil_LDADD = $(t_common_ldadd) t_b64_LDADD = $(t_common_ldadd) t_exechelp_LDADD = $(t_common_ldadd) t_exectool_LDADD = $(t_common_ldadd) t_session_env_LDADD = $(t_common_ldadd) t_openpgp_oid_LDADD = $(t_common_ldadd) t_ssh_utils_LDADD = $(t_common_ldadd) t_mapstrings_LDADD = $(t_common_ldadd) t_zb32_SOURCES = t-zb32.c $(t_extra_src) t_zb32_LDADD = $(t_common_ldadd) t_mbox_util_LDADD = $(t_common_ldadd) t_iobuf_LDADD = $(t_common_ldadd) t_strlist_LDADD = $(t_common_ldadd) t_name_value_LDADD = $(t_common_ldadd) t_ccparray_LDADD = $(t_common_ldadd) t_recsel_LDADD = $(t_common_ldadd) # System specific test if HAVE_W32_SYSTEM t_w32_reg_SOURCES = t-w32-reg.c $(t_extra_src) t_w32_reg_LDADD = $(t_common_ldadd) endif # All programs should depend on the created libs. $(PROGRAMS) : libcommon.a libcommonpth.a diff --git a/common/tlv-builder.c b/common/tlv-builder.c new file mode 100644 index 000000000..3b644ca24 --- /dev/null +++ b/common/tlv-builder.c @@ -0,0 +1,387 @@ +/* tlv-builder.c - Build DER encoded objects + * Copyright (C) 2020 g10 Code GmbH + * + * This file is part of GnuPG. + * + * This file is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This file 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 Lesser General Public License + * along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later + */ + +#include + +#include +#include +#include +#include + +#include "util.h" +#include "tlv.h" + + +struct item_s +{ + int class; + int tag; + unsigned int is_constructed:1; /* This is a constructed element. */ + unsigned int is_stop:1; /* This is a STOP item. */ + const void *value; + size_t valuelen; + char *buffer; /* Malloced space or NULL. */ +}; + + +struct tlv_builder_s +{ + gpg_error_t error; /* Last error. */ + int use_secure; /* Use secure memory for the result. */ + size_t nallocateditems; /* Number of allocated items. */ + size_t nitems; /* Number of used items. */ + struct item_s *items; /* Array of items. */ + int laststop; /* Used as return value of compute_length. */ +}; + + +/* Allocate a new TLV Builder instance. Returns NULL on error. If + * SECURE is set the final object is stored in secure memory. */ +tlv_builder_t +tlv_builder_new (int secure) +{ + tlv_builder_t tb; + + tb = xtrycalloc (1, sizeof *tb); + if (tb && secure) + tb->use_secure = 1; + return tb; +} + + +/* Make sure the array of items is large enough for one new item. + * Records any error in TB and returns true in that case. */ +static int +ensure_space (tlv_builder_t tb) +{ + struct item_s *newitems; + + if (!tb || tb->error) + return 1; + + if (tb->nitems == tb->nallocateditems) + { + tb->nallocateditems += 32; + newitems = gpgrt_reallocarray (tb->items, tb->nitems, + tb->nallocateditems, sizeof *newitems); + if (!newitems) + tb->error = gpg_error_from_syserror (); + else + tb->items = newitems; + } + return !!tb->error; +} + + + +/* Add a new primitive element to the builder instance TB. The + * element is described by CLASS, TAG, VALUE, and VALUEEN. CLASS and + * TAG must describe a primitive element and (VALUE,VALUELEN) specify + * its value. The value is a pointer and its object must not be + * changed as long as the instance TB exists. For a TAG_NULL no vlaue + * is expected. Errors are not returned but recorded for later + * retrieval. */ +void +tlv_builder_add_ptr (tlv_builder_t tb, int class, int tag, + void *value, size_t valuelen) +{ + if (ensure_space (tb)) + return; + tb->items[tb->nitems].class = class; + tb->items[tb->nitems].tag = tag; + tb->items[tb->nitems].value = value; + tb->items[tb->nitems].valuelen = valuelen; + tb->nitems++; +} + + +/* This is the same as tlv_builder_add_ptr but it takes a copy of the + * value and thus the caller does not need to care about it. */ +void +tlv_builder_add_val (tlv_builder_t tb, int class, int tag, + const void *value, size_t valuelen) +{ + void *p; + + if (ensure_space (tb)) + return; + if (!value || !valuelen) + { + tb->error = gpg_error (GPG_ERR_INV_VALUE); + return; + } + p = tb->use_secure? xtrymalloc_secure (valuelen) : xtrymalloc (valuelen); + if (!p) + { + tb->error = gpg_error_from_syserror (); + return; + } + memcpy (p, value, valuelen); + tb->items[tb->nitems].buffer = p; + tb->items[tb->nitems].class = class; + tb->items[tb->nitems].tag = tag; + tb->items[tb->nitems].value = p; + tb->items[tb->nitems].valuelen = valuelen; + tb->nitems++; +} + + +/* Add a new constructed object to the builder instance TB. The + * object is described by CLASS and TAG which must describe a + * constructed object. The elements of the constructed objects are + * added with more call to the add functions. To close a constructed + * element a call to tlv_builer_add_end is required. Errors are not + * returned but recorded for later retrieval. */ +void +tlv_builder_add_tag (tlv_builder_t tb, int class, int tag) +{ + if (ensure_space (tb)) + return; + tb->items[tb->nitems].class = class; + tb->items[tb->nitems].tag = tag; + tb->items[tb->nitems].is_constructed = 1; + tb->nitems++; +} + + +/* A call to this function closes a constructed element. This must be + * called even for an empty constructed element. */ +void +tlv_builder_add_end (tlv_builder_t tb) +{ + if (ensure_space (tb)) + return; + tb->items[tb->nitems].is_stop = 1; + tb->nitems++; +} + + +/* Compute and set the length of all constructed elements in the item + * array of TB starting at IDX up to the corresponding stop item. On + * error tb->error is set. */ +static size_t +compute_lengths (tlv_builder_t tb, int idx) +{ + size_t total = 0; + + if (tb->error) + return 0; + + for (; idx < tb->nitems; idx++) + { + if (tb->items[idx].is_stop) + { + tb->laststop = idx; + break; + } + if (tb->items[idx].is_constructed) + { + tb->items[idx].valuelen = compute_lengths (tb, idx+1); + if (tb->error) + return 0; + /* Note: The last processed IDX is stored at tb->LASTSTOP. */ + } + total += get_tlv_length (tb->items[idx].class, tb->items[idx].tag, + tb->items[idx].is_constructed, + tb->items[idx].valuelen); + if (tb->items[idx].is_constructed) + idx = tb->laststop; + } + return total; +} + + +/* Return the constructed DER encoding and release this instance. On + * success the object is stored at R_OBJ and its length at R_OBJLEN. + * The caller needs to release that memory. On error NULL is stored + * at R_OBJ and an error code is returned. Note than an error may + * stem from any of the previous call made to this object or from + * constructing the the DER object. */ +gpg_error_t +tlv_builder_finalize (tlv_builder_t tb, void **r_obj, size_t *r_objlen) +{ + gpg_error_t err; + membuf_t mb; + int mb_initialized = 0; + int idx; + + *r_obj = NULL; + *r_objlen = 0; + + if (!tb) + return gpg_error (GPG_ERR_INTERNAL); + if (tb->error) + { + err = tb->error; + goto leave; + } + if (!tb->nitems || !tb->items[tb->nitems-1].is_stop) + { + err = gpg_error (GPG_ERR_NO_OBJ); + goto leave; + } + + compute_lengths (tb, 0); + err = tb->error; + if (err) + goto leave; + + /* for (idx=0; idx < tb->nitems; idx++) */ + /* log_debug ("TLVB[%2d]: c=%d t=%2d %s p=%p l=%zu\n", */ + /* idx, */ + /* tb->items[idx].class, */ + /* tb->items[idx].tag, */ + /* tb->items[idx].is_stop? "stop": */ + /* tb->items[idx].is_constructed? "cons":"prim", */ + /* tb->items[idx].value, */ + /* tb->items[idx].valuelen); */ + + if (tb->use_secure) + init_membuf_secure (&mb, 512); + else + init_membuf (&mb, 512); + mb_initialized = 1; + + for (idx=0; idx < tb->nitems; idx++) + { + if (tb->items[idx].is_stop) + continue; + put_tlv_to_membuf (&mb, tb->items[idx].class, tb->items[idx].tag, + tb->items[idx].is_constructed, + tb->items[idx].valuelen); + if (tb->items[idx].value) + put_membuf (&mb, tb->items[idx].value, tb->items[idx].valuelen); + } + + *r_obj = get_membuf (&mb, r_objlen); + if (!*r_obj) + err = gpg_error_from_syserror (); + mb_initialized = 0; + + leave: + if (mb_initialized) + xfree (get_membuf (&mb, NULL)); + for (idx=0; idx < tb->nitems; idx++) + xfree (tb->items[idx].buffer); + xfree (tb->items); + xfree (tb); + return err; +} + + +/* Write TAG of CLASS to MEMBUF. CONSTRUCTED is a flag telling + * whether the value is constructed. LENGTH gives the length of the + * value, if it is 0 undefinite length is assumed. LENGTH is ignored + * for the NULL tag. TAG must be less that 0x1f. */ +void +put_tlv_to_membuf (membuf_t *membuf, int class, int tag, + int constructed, size_t length) +{ + unsigned char buf[20]; + int buflen = 0; + int i; + + if (tag < 0x1f) + { + *buf = (class << 6) | tag; + if (constructed) + *buf |= 0x20; + buflen++; + } + else + BUG (); + + if (!tag && !class) + buf[buflen++] = 0; /* end tag */ + else if (tag == TAG_NULL && !class) + buf[buflen++] = 0; /* NULL tag */ + else if (!length) + buf[buflen++] = 0x80; /* indefinite length */ + else if (length < 128) + buf[buflen++] = length; + else + { + /* If we know the sizeof a size_t we could support larger + * objects - however this is pretty ridiculous */ + i = (length <= 0xff ? 1: + length <= 0xffff ? 2: + length <= 0xffffff ? 3: 4); + + buf[buflen++] = (0x80 | i); + if (i > 3) + buf[buflen++] = length >> 24; + if (i > 2) + buf[buflen++] = length >> 16; + if (i > 1) + buf[buflen++] = length >> 8; + buf[buflen++] = length; + } + + put_membuf (membuf, buf, buflen); +} + + +/* Return the length of the to be constructed TLV. CONSTRUCTED is a + * flag telling whether the value is constructed. LENGTH gives the + * length of the value, if it is 0 undefinite length is assumed. + * LENGTH is ignored for the NULL tag. TAG must be less that 0x1f. */ +size_t +get_tlv_length (int class, int tag, int constructed, size_t length) +{ + size_t buflen = 0; + int i; + + (void)constructed; /* Not used, but passed for uniformity of such calls. */ + + if (tag < 0x1f) + { + buflen++; + } + else + { + buflen++; /* assume one and let the actual write function bail out */ + } + + if (!tag && !class) + buflen++; /* end tag */ + else if (tag == TAG_NULL && !class) + buflen++; /* NULL tag */ + else if (!length) + buflen++; /* indefinite length */ + else if (length < 128) + buflen++; + else + { + i = (length <= 0xff ? 1: + length <= 0xffff ? 2: + length <= 0xffffff ? 3: 4); + + buflen++; + if (i > 3) + buflen++; + if (i > 2) + buflen++; + if (i > 1) + buflen++; + buflen++; + } + + return buflen + length; +} diff --git a/common/tlv.c b/common/tlv.c index 86e954a19..947464bf7 100644 --- a/common/tlv.c +++ b/common/tlv.c @@ -1,413 +1,304 @@ /* tlv.c - Tag-Length-Value Utilities * Copyright (C) 2003, 2004, 2005 Free Software Foundation, Inc. * * This file is part of GnuPG. * * This file is free software; you can redistribute it and/or modify * it under the terms of either * * - the GNU Lesser General Public License as published by the Free * Software Foundation; either version 3 of the License, or (at * your option) any later version. * * or * * - the GNU General Public License as published by the Free * Software Foundation; either version 2 of the License, or (at * your option) any later version. * * or both in parallel, as here. * * This file 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 * along with this program; if not, see . */ #include #include #include #include -#include - -#if GNUPG_MAJOR_VERSION == 1 -#define GPG_ERR_EOF (-1) -#define GPG_ERR_BAD_BER (1) /*G10ERR_GENERAL*/ -#define GPG_ERR_INV_SEXP (45) /*G10ERR_INV_ARG*/ -typedef int gpg_error_t; -#define gpg_make_err(x,n) (n) -#else #include -#endif + #include "util.h" #include "tlv.h" + static const unsigned char * do_find_tlv (const unsigned char *buffer, size_t length, int tag, size_t *nbytes, int nestlevel) { const unsigned char *s = buffer; size_t n = length; size_t len; int this_tag; int composite; for (;;) { if (n < 2) return NULL; /* Buffer definitely too short for tag and length. */ if (!*s || *s == 0xff) { /* Skip optional filler between TLV objects. */ s++; n--; continue; } composite = !!(*s & 0x20); if ((*s & 0x1f) == 0x1f) { /* more tag bytes to follow */ s++; n--; if (n < 2) return NULL; /* buffer definitely too short for tag and length. */ if ((*s & 0x1f) == 0x1f) return NULL; /* We support only up to 2 bytes. */ this_tag = (s[-1] << 8) | (s[0] & 0x7f); } else this_tag = s[0]; len = s[1]; s += 2; n -= 2; if (len < 0x80) ; else if (len == 0x81) { /* One byte length follows. */ if (!n) return NULL; /* we expected 1 more bytes with the length. */ len = s[0]; s++; n--; } else if (len == 0x82) { /* Two byte length follows. */ if (n < 2) return NULL; /* We expected 2 more bytes with the length. */ len = ((size_t)s[0] << 8) | s[1]; s += 2; n -= 2; } else return NULL; /* APDU limit is 65535, thus it does not make sense to assume longer length fields. */ if (composite && nestlevel < 100) { /* Dive into this composite DO after checking for a too deep nesting. */ const unsigned char *tmp_s; size_t tmp_len; tmp_s = do_find_tlv (s, len, tag, &tmp_len, nestlevel+1); if (tmp_s) { *nbytes = tmp_len; return tmp_s; } } if (this_tag == tag) { *nbytes = len; return s; } if (len > n) return NULL; /* Buffer too short to skip to the next tag. */ s += len; n -= len; } } /* Locate a TLV encoded data object in BUFFER of LENGTH and return a pointer to value as well as its length in NBYTES. Return NULL if it was not found or if the object does not fit into the buffer. */ const unsigned char * find_tlv (const unsigned char *buffer, size_t length, int tag, size_t *nbytes) { const unsigned char *p; p = do_find_tlv (buffer, length, tag, nbytes, 0); if (p && *nbytes > (length - (p-buffer))) p = NULL; /* Object longer than buffer. */ return p; } /* Locate a TLV encoded data object in BUFFER of LENGTH and return a pointer to value as well as its length in NBYTES. Return NULL if it was not found. Note, that the function does not check whether the value fits into the provided buffer. */ const unsigned char * find_tlv_unchecked (const unsigned char *buffer, size_t length, int tag, size_t *nbytes) { return do_find_tlv (buffer, length, tag, nbytes, 0); } -/* Write TAG of CLASS to MEMBUF. CONSTRUCTED is a flag telling - * whether the value is constructed. LENGTH gives the length of the - * value, if it is 0 undefinite length is assumed. LENGTH is ignored - * for the NULL tag. TAG must be less that 0x1f. */ -void -put_tlv_to_membuf (membuf_t *membuf, int class, int tag, - int constructed, size_t length) -{ - unsigned char buf[20]; - int buflen = 0; - int i; - - if (tag < 0x1f) - { - *buf = (class << 6) | tag; - if (constructed) - *buf |= 0x20; - buflen++; - } - else - BUG (); - - if (!tag && !class) - buf[buflen++] = 0; /* end tag */ - else if (tag == TAG_NULL && !class) - buf[buflen++] = 0; /* NULL tag */ - else if (!length) - buf[buflen++] = 0x80; /* indefinite length */ - else if (length < 128) - buf[buflen++] = length; - else - { - /* If we know the sizeof a size_t we could support larger - * objects - however this is pretty ridiculous */ - i = (length <= 0xff ? 1: - length <= 0xffff ? 2: - length <= 0xffffff ? 3: 4); - - buf[buflen++] = (0x80 | i); - if (i > 3) - buf[buflen++] = length >> 24; - if (i > 2) - buf[buflen++] = length >> 16; - if (i > 1) - buf[buflen++] = length >> 8; - buf[buflen++] = length; - } - - put_membuf (membuf, buf, buflen); -} - - -/* Return the length of the to be constructed TLV. CONSTRUCTED is a - * flag telling whether the value is constructed. LENGTH gives the - * length of the value, if it is 0 undefinite length is assumed. - * LENGTH is ignored for the NULL tag. TAG must be less that 0x1f. */ -size_t -get_tlv_length (int class, int tag, int constructed, size_t length) -{ - size_t buflen = 0; - int i; - - (void)constructed; /* Not used, but passed for uniformity of such calls. */ - - if (tag < 0x1f) - { - buflen++; - } - else - { - buflen++; /* assume one and let the actual write function bail out */ - } - - if (!tag && !class) - buflen++; /* end tag */ - else if (tag == TAG_NULL && !class) - buflen++; /* NULL tag */ - else if (!length) - buflen++; /* indefinite length */ - else if (length < 128) - buflen++; - else - { - i = (length <= 0xff ? 1: - length <= 0xffff ? 2: - length <= 0xffffff ? 3: 4); - - buflen++; - if (i > 3) - buflen++; - if (i > 2) - buflen++; - if (i > 1) - buflen++; - buflen++; - } - - return buflen + length; -} - - /* ASN.1 BER parser: Parse BUFFER of length SIZE and return the tag and the length part from the TLV triplet. Update BUFFER and SIZE on success. */ gpg_error_t parse_ber_header (unsigned char const **buffer, size_t *size, int *r_class, int *r_tag, int *r_constructed, int *r_ndef, size_t *r_length, size_t *r_nhdr) { int c; unsigned long tag; const unsigned char *buf = *buffer; size_t length = *size; *r_ndef = 0; *r_length = 0; *r_nhdr = 0; /* Get the tag. */ if (!length) return gpg_err_make (default_errsource, GPG_ERR_EOF); c = *buf++; length--; ++*r_nhdr; *r_class = (c & 0xc0) >> 6; *r_constructed = !!(c & 0x20); tag = c & 0x1f; if (tag == 0x1f) { tag = 0; do { tag <<= 7; if (!length) return gpg_err_make (default_errsource, GPG_ERR_EOF); c = *buf++; length--; ++*r_nhdr; tag |= c & 0x7f; } while (c & 0x80); } *r_tag = tag; /* Get the length. */ if (!length) return gpg_err_make (default_errsource, GPG_ERR_EOF); c = *buf++; length--; ++*r_nhdr; if ( !(c & 0x80) ) *r_length = c; else if (c == 0x80) *r_ndef = 1; else if (c == 0xff) return gpg_err_make (default_errsource, GPG_ERR_BAD_BER); else { unsigned long len = 0; int count = (c & 0x7f); if (count > (sizeof(len)= '0' && *s <= '9'); s++, n--) vlen = vlen*10 + (*s - '0'); if (!n || *s != ':') return gpg_err_make (default_errsource, GPG_ERR_INV_SEXP); s++; n--; if (vlen > n) return gpg_err_make (default_errsource, GPG_ERR_INV_SEXP); *tok = s; *toklen = vlen; s += vlen; n -= vlen; *buf = s; *buflen = n; return 0; } diff --git a/common/tlv.h b/common/tlv.h index 0c915e63f..e371ca57e 100644 --- a/common/tlv.h +++ b/common/tlv.h @@ -1,124 +1,142 @@ /* tlv.h - Tag-Length-Value Utilities * Copyright (C) 2004 Free Software Foundation, Inc. * * This file is part of GnuPG. * * This file is free software; you can redistribute it and/or modify * it under the terms of either * * - the GNU Lesser General Public License as published by the Free * Software Foundation; either version 3 of the License, or (at * your option) any later version. * * or * * - the GNU General Public License as published by the Free * Software Foundation; either version 2 of the License, or (at * your option) any later version. * * or both in parallel, as here. * * This file 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 * along with this program; if not, see . */ #ifndef SCD_TLV_H #define SCD_TLV_H 1 #include "membuf.h" enum tlv_tag_class { CLASS_UNIVERSAL = 0, CLASS_APPLICATION = 1, CLASS_CONTEXT = 2, CLASS_PRIVATE =3 }; enum tlv_tag_type { TAG_NONE = 0, TAG_BOOLEAN = 1, TAG_INTEGER = 2, TAG_BIT_STRING = 3, TAG_OCTET_STRING = 4, TAG_NULL = 5, TAG_OBJECT_ID = 6, TAG_OBJECT_DESCRIPTOR = 7, TAG_EXTERNAL = 8, TAG_REAL = 9, TAG_ENUMERATED = 10, TAG_EMBEDDED_PDV = 11, TAG_UTF8_STRING = 12, TAG_REALTIVE_OID = 13, TAG_SEQUENCE = 16, TAG_SET = 17, TAG_NUMERIC_STRING = 18, TAG_PRINTABLE_STRING = 19, TAG_TELETEX_STRING = 20, TAG_VIDEOTEX_STRING = 21, TAG_IA5_STRING = 22, TAG_UTC_TIME = 23, TAG_GENERALIZED_TIME = 24, TAG_GRAPHIC_STRING = 25, TAG_VISIBLE_STRING = 26, TAG_GENERAL_STRING = 27, TAG_UNIVERSAL_STRING = 28, TAG_CHARACTER_STRING = 29, TAG_BMP_STRING = 30 }; +struct tlv_builder_s; +typedef struct tlv_builder_s *tlv_builder_t; + +/*-- tlv.c --*/ + /* Locate a TLV encoded data object in BUFFER of LENGTH and return a pointer to value as well as its length in NBYTES. Return NULL if it was not found or if the object does not fit into the buffer. */ const unsigned char *find_tlv (const unsigned char *buffer, size_t length, int tag, size_t *nbytes); /* Locate a TLV encoded data object in BUFFER of LENGTH and return a pointer to value as well as its length in NBYTES. Return NULL if it was not found. Note, that the function does not check whether the value fits into the provided buffer.*/ const unsigned char *find_tlv_unchecked (const unsigned char *buffer, size_t length, int tag, size_t *nbytes); -/* Wite a TLV header to MEMBUF. */ -void put_tlv_to_membuf (membuf_t *membuf, int class, int tag, - int constructed, size_t length); - -/* Count the length of a to be constructed TLV. */ -size_t get_tlv_length (int class, int tag, int constructed, size_t length); - - /* ASN.1 BER parser: Parse BUFFER of length SIZE and return the tag and the length part from the TLV triplet. Update BUFFER and SIZE on success. */ gpg_error_t parse_ber_header (unsigned char const **buffer, size_t *size, int *r_class, int *r_tag, int *r_constructed, int *r_ndef, size_t *r_length, size_t *r_nhdr); /* Return the next token of an canonical encoded S-expression. BUF is the pointer to the S-expression and BUFLEN is a pointer to the length of this S-expression (used to validate the syntax). Both are updated to reflect the new position. The token itself is returned as a pointer into the original buffer at TOK and TOKLEN. If a parentheses is the next token, TOK will be set to NULL. TOKLEN is checked to be within the bounds. On error an error code is returned and no pointer is not guaranteed to point to a meaningful value. DEPTH should be initialized to 0 and will reflect on return the actual depth of the tree. To detect the end of the S-expression it is advisable to check DEPTH after a successful return. */ gpg_error_t parse_sexp (unsigned char const **buf, size_t *buflen, int *depth, unsigned char const **tok, size_t *toklen); +/*-- tlv-builder.c --*/ + +tlv_builder_t tlv_builder_new (int use_secure); +void tlv_builder_add_ptr (tlv_builder_t tb, int class, int tag, + void *value, size_t valuelen); +void tlv_builder_add_val (tlv_builder_t tb, int class, int tag, + const void *value, size_t valuelen); +void tlv_builder_add_tag (tlv_builder_t tb, int class, int tag); +void tlv_builder_add_end (tlv_builder_t tb); +gpg_error_t tlv_builder_finalize (tlv_builder_t tb, + void **r_obj, size_t *r_objlen); + +/* Wite a TLV header to MEMBUF. */ +void put_tlv_to_membuf (membuf_t *membuf, int class, int tag, + int constructed, size_t length); + +/* Count the length of a to be constructed TLV. */ +size_t get_tlv_length (int class, int tag, int constructed, size_t length); + + + #endif /* SCD_TLV_H */