Commit Graph

86 Commits

Author SHA1 Message Date
Nicolas Williams
e4311f3a82 asn1: Fix UB and incorrect codec for unconstrained INTEGER values of -1 2022-11-01 16:10:57 -05:00
Jeffrey Altman
190263bb7a assert non-NULL ptrs before calling mem funcs
The definitions of memcpy(), memmove(), and memset() state that
the behaviour is undefined if any of the pointer arguments are
NULL, and some compilers are known to make use of this to
optimise away existing NULL checks in the source.

Change-Id: I489bc256e3eac7ff41d91becb0b43aba73dbb3f9
Link: https://www.imperialviolet.org/2016/06/26/nonnull.html
2022-01-24 00:07:51 -05:00
Jeffrey Altman
38536d7313 lib/asn1: set *size output to zero at start of der funcs
Assign zero to the output size parameter at the start so that
callers that use the value when an error occurs do not see
garbage that might be misinterpreted.

Change-Id: Iccfcf4f6944b1bf72789c83919901d9b9d6f9153
2022-01-23 00:10:14 -05:00
Nicolas Williams
5bb5887f8c asn1: Fix recent der_match_tag2() bug
Recently introduced.
2021-03-11 09:12:43 -06:00
Nicolas Williams
659e444e3d asn1: Fix crash found by AFL 2021-03-10 12:23:51 -06:00
Nicolas Williams
81195acafa asn1: Further IMPLICIT tagging fixes
Commit 89389bc7a (asn1: Fix long-standing IMPLICIT tagging brokenness)
was incomplete.  Removing the hacks in lib/asn1/cms.asn1 revealed this.

Now the ASN.1 compiler generates enums to indicate what is the class and
tag of each type.  This is needed so the decoder functions generated by
the compiler can know what tag to restore.

Now, too, the compiler does handle IMPLICIT tags whose encoded length is
different from that of the underlying type.

However, we now don't handle indefinite BER and non-DER definite lengths
(DCE) following IMPLICIT tags.  This would affect only CMS in-tree.
2021-01-20 21:04:34 -06:00
Nicolas Williams
89389bc7a0 asn1: Fix long-standing IMPLICIT tagging brokenness
This commit _mostly_ fixes the Heimdal ASN.1 compiler to properly
support IMPLICIT tagging in most if not all the many cases where it
didn't already, as you could see in lib/asn1/canthandle.asn1 prior to
this commit.

This fix is a bit of a hack in that a proper fix would change the
function prototypes of the encode/decode/length functions generated by
the compiler to take an optional IMPLICIT tag to tag with instead of the
type they code.  That fix would not be localized to lib/asn1/ however,
and would change the API and ABI of generated code (which is mostly not
an ABI for Heimdal, but still, some external projects would have to make
changes).

Instead, for IMPLICIT tags we currently depend on the IMPLICIT tag and
the sub-type's tag having the same size -- this can be fixed with extra
allocation on the encoder side as we do on the decoder side, but we
might leave it for later.

The issue we're fixing manifested as:

  -- The [CONTEXT 0] tag in Bar below was turned into an EXPLICIT tag
  -- instead of an IMPLICIT one, netting the DER encoding for the `foo`
  -- member as:
  --    [CONTEXT 0] [UNIVERSAL Seq] [UNIVERSAL Int] <encoding of i>
  -- instead of the correct:
  --    [CONTEXT 0] [UNIVERSAL Int] <encoding of i>
  Foo ::= SEQUENCE { i INTEGER }
  Bar ::= SEQUENCE { foo [0] IMPLICIT Foo }

or

  Foo ::= INTEGER
  Bar ::= SEQUENCE { foo [0] IMPLICIT Foo } -- tag context 0 marked
                                            -- constructed!

I've reviewed this in part by reviewing the output of the compiler
before and after this change using this procedure:

 - Run an earlier version of the ASN.1 compiler output for all
   modules in lib/asn1/.  Save these in a different location.

 - Run this (or later) version of the ASN.1 compiler output for
   the same modules, adding --original-order for modules that
   have been manually sorted already (e.g., rfc2459.asn1).

 - Run clang-format on the saved and newest generated C source
   and header files.

 - Diff the generated output.  Substantial differences will
   relate to handling of IMPLICIT tagging.  These are
   particularly evident in the tcg.asn1 module, which uses a lot
   of those.

Later commits add test data (certificates with extensions that use
IMPLICIT tagging) taken from external specifications as well, which
exercise this fix.

Non-urgent brokenness yet to be fixed:

 - When the IMPLICIT tag and the tag of the underlying type require
   differing numbers of bytes to encode, the encoding and decoding will
   fail.  The prototypes of generated length_*() functions make it
   impossible to do much better.

 - SET OF <primitive> still crashes the compiler (not a new bug).

Futures:

 - Unwind hackery in cms.asn1 that worked around our lack of proper
   IMPLICIT tagging support.

Here are some of the generated code deltas one expects to see around
this commit:

$ git checkout $earlier_version
$ ./autogen.sh
$ mkdir build
$ cd build
$ ../configure ...
$ make -j4
$ make check
$ cd lib/asn1
$ for i in *.c; do
      [[ $i = asn1parse.? || $i = lex.? || $i = *.h ]] && continue
      clang-format -i $i $i
      cmp /tmp/save/$i $i && echo NO DIFFS: $i && continue; echo DIFF: $i
  done
NO DIFFS: asn1_cms_asn1.c
NO DIFFS: asn1_digest_asn1.c
NO DIFFS: asn1_err.c
NO DIFFS: asn1_krb5_asn1.c
/tmp/save/asn1_kx509_asn1.c asn1_kx509_asn1.c differ: byte 6433, line 264
DIFF: asn1_kx509_asn1.c
NO DIFFS: asn1_ocsp_asn1.c
NO DIFFS: asn1_pkcs10_asn1.c
/tmp/save/asn1_pkcs12_asn1.c asn1_pkcs12_asn1.c differ: byte 12934, line 455
DIFF: asn1_pkcs12_asn1.c
NO DIFFS: asn1_pkcs8_asn1.c
NO DIFFS: asn1_pkcs9_asn1.c
NO DIFFS: asn1_pkinit_asn1.c
/tmp/save/asn1_rfc2459_asn1.c asn1_rfc2459_asn1.c differ: byte 20193, line 532
DIFF: asn1_rfc2459_asn1.c
NO DIFFS: asn1_rfc4043_asn1.c
/tmp/save/asn1_rfc4108_asn1.c asn1_rfc4108_asn1.c differ: byte 595, line 26
DIFF: asn1_rfc4108_asn1.c
/tmp/save/asn1_tcg_asn1.c asn1_tcg_asn1.c differ: byte 31835, line 1229
DIFF: asn1_tcg_asn1.c
/tmp/save/asn1_test_asn1.c asn1_test_asn1.c differ: byte 384, line 21
DIFF: asn1_test_asn1.c
/tmp/save/test_template_asn1-template.c test_template_asn1-template.c differ: byte 650, line 20
DIFF: test_template_asn1-template.c
$
$ cd ../..
$ git checkout $newer_version
$ make -j4 && make check
$ cd lib/asn1
$ for i in *.[ch]; do
    [[ $i = asn1parse.? || $i = lex.? || $i = *.h ]] && continue
    clang-format -i $i $i
    cmp /tmp/save/$i $i && echo NO DIFFS: $i && continue
    diff -ubw /tmp/save/$i $i
  done | $PAGER

and one should see deltas such as the following:

 - a small enhancement to handling of OPTIONAL members:

     (data)->macData = calloc(1, sizeof(*(data)->macData));
     if ((data)->macData == NULL)
       goto fail;
     e = decode_PKCS12_MacData(p, len, (data)->macData, &l);
-    if (e) {
+    if (e == ASN1_MISSING_FIELD) {
       free((data)->macData);
       (data)->macData = NULL;
+    } else if (e) {
+      goto fail;
     } else {
       p += l;
       len -= l;
       ret += l;

 - more complete handling of DEFAULTed members:

     e = decode_FWReceiptVersion(p, len, &(data)->version, &l);
-    if (e)
+    if (e == ASN1_MISSING_FIELD) {
+      (data)->version = 1;
+    } else if (e) {
       goto fail;
-    p += l;
-    len -= l;
-    ret += l;
+    } else {
+      p += l;
+      len -= l;
+      ret += l;
+    }
     {

 - replacement of tags with implicit tags (encode side):

   /* targetUri */
   if ((data)->targetUri) {
     size_t Top_tag_oldret HEIMDAL_UNUSED_ATTRIBUTE = ret;
     ret = 0;
     e = encode_URIReference(p, len, (data)->targetUri, &l);
     if (e)
       return e;
     p -= l;
     len -= l;
     ret += l;

-    e = der_put_length_and_tag(p, len, ret, ASN1_C_CONTEXT, PRIM, 4, &l);
+    e = der_replace_tag(p, len, ASN1_C_CONTEXT, CONS, 4);
     if (e)
       return e;
     p -= l;
     len -= l;
     ret += l;

     ret += Top_tag_oldret;
   }

 - replacement of tags with implicit tags (decode side):

         strengthOfFunction_oldlen = len;
         if (strengthOfFunction_datalen > len) {
           e = ASN1_OVERRUN;
           goto fail;
         }
         len = strengthOfFunction_datalen;
-        e = decode_StrengthOfFunction(p, len, (data)->strengthOfFunction, &l);
-        if (e)
-          goto fail;
-        p += l;
-        len -= l;
-        ret += l;
+        {
+          unsigned char *pcopy;
+          pcopy = calloc(1, len);
+          if (pcopy == 0) {
+            e = ENOMEM;
+            goto fail;
+          }
+          memcpy(pcopy, p, len);
+          e = der_replace_tag(pcopy, len, ASN1_C_UNIV, PRIM, 0);
+          if (e)
+            goto fail;
+          e = decode_StrengthOfFunction(p, len, (data)->strengthOfFunction, &l);
+          if (e)
+            goto fail;
+          p += l;
+          len -= l;
+          ret += l;
+          free(pcopy);
+        }
         len = strengthOfFunction_oldlen - strengthOfFunction_datalen;
       }
     }
     {
       size_t profileOid_datalen, profileOid_oldlen;

 - correct determination of implicit tag constructed vs no for IMPLICT-
   tagged named primitive types:

     {
       size_t profileUri_datalen, profileUri_oldlen;
       Der_type profileUri_type;
       e = der_match_tag_and_length(p, len, ASN1_C_CONTEXT, &profileUri_type, 2,
                                    &profileUri_datalen, &l);
-      if (e == 0 && profileUri_type != PRIM) {
+      if (e == 0 && profileUri_type != CONS) {
         e = ASN1_BAD_ID;
       }
       if (e) {
         (data)->profileUri = NULL;
       } else {
         (data)->profileUri = calloc(1, sizeof(*(data)->profileUri));
         if ((data)->profileUri == NULL) {
           e = ENOMEM;
           goto fail;
         }

 - correct determination of length of IMPLICT-tagged OIDs:

   if ((data)->profileOid) {
     size_t Top_tag_oldret = ret;
     ret = 0;
     ret += der_length_oid((data)->profileOid);
+    ret += 1 + der_length_len(ret);
     ret += Top_tag_oldret;
   }

These deltas should be examined with the corresponding ASN.1 module at
hand, cross-referencing the source code to the ASN.1 type definitions
and manually applying X.690 rules to double-check the choices of
primitive vs. constructed tag, and the choices of when to replace tags
and when not.
2021-01-13 20:17:58 -06:00
Jeffrey Altman
bc3270cd88 asn1: function ptrs passed as ASN1CALL ptrs must be ASN1CALL
On Windows i386 the asn1 tests would crash due to stack corruption
as a result of functions being executed with the wrong calling
conventions.

Change-Id: Ic4f8b3a05dad36e3db6397fbd9270b98f0a5dfc5
2020-07-13 15:48:06 -04:00
Jeffrey Altman
a5da5bcb96 asn1: check overflow against SIZE_MAX not +1
A comparison of (len > len + 1) is permitted to be optimized out
as dead code because it can't be true.  Overflowing is an exceptional
condition that results in undefined behavior.  The correct conditional
is (len == SIZE_MAX) when len is size_t.

Change-Id: Ia5586556a973d9fa5228430c4304ea9792c996bb
2014-06-20 20:15:13 -04:00
Love Hornquist Astrand
060474df16 quel 64bit warnings, fixup implicit encoding for template, fix spelling 2013-06-03 21:46:20 -07:00
Nicolas Williams
19d378f44d Add 64-bit integer support to ASN.1 compiler
ASN.1 INTEGERs will now compile to C int64_t or uint64_t, depending
    on whether the constraint ranges include numbers that cannot be
    represented in 32-bit ints and whether they include negative
    numbers.

    Template backend support included.  check-template is now built with
    --template, so we know we're testing it.

    Tests included.
2011-12-12 20:01:20 -06:00
Love Hornquist Astrand
f5f9014c90 Warning fixes from Christos Zoulas
- shadowed variables
- signed/unsigned confusion
- const lossage
- incomplete structure initializations
- unused code
2011-04-29 20:25:05 -07:00
Love Hornquist Astrand
fa4c84e6d6 make printablestring and ia5string octetstrings 2010-08-08 15:51:33 -07:00
Love Hornquist Astrand
b939943b07 first stange of asn1 table driven compiler 2009-11-21 10:24:56 -08:00
Love Hornquist Astrand
84111cb8e1 [HEIMDAL-646] malloc(0) checks for AIX 2009-10-11 16:25:42 -07:00
Stefan Metzmacher
772dfac438 lib/asn1: remove unused reference to vers.h
metze

Signed-off-by: Love Hornquist Astrand <lha@h5l.org>
2009-08-04 20:19:44 +02:00
Love Hornquist Astrand
147184381e Check for NUL in the middle of the string 2009-08-04 00:57:35 +02:00
Love Hörnquist Åstrand
c660c77a26 don't calculate unused stuff
git-svn-id: svn://svn.h5l.se/heimdal/trunk/heimdal@25252 ec53bebd-3082-4978-b11e-865c3cabbd6b
2009-05-28 01:20:58 +00:00
Love Hörnquist Åstrand
0e6b5c5c22 remove trailing whitespace
git-svn-id: svn://svn.h5l.se/heimdal/trunk/heimdal@25232 ec53bebd-3082-4978-b11e-865c3cabbd6b
2009-05-28 01:17:17 +00:00
Love Hörnquist Åstrand
e2c5404d6e don't include version.h here
git-svn-id: svn://svn.h5l.se/heimdal/trunk/heimdal@24348 ec53bebd-3082-4978-b11e-865c3cabbd6b
2009-01-25 00:29:18 +00:00
Love Hörnquist Åstrand
0179e12d54 remove -DHAVE_CONFIG_H, add --one-source-file, rename krb5 and cms to "better" file names
git-svn-id: svn://svn.h5l.se/heimdal/trunk/heimdal@24337 ec53bebd-3082-4978-b11e-865c3cabbd6b
2009-01-25 00:23:39 +00:00
Love Hörnquist Åstrand
b7632c038b reindent
git-svn-id: svn://svn.h5l.se/heimdal/trunk/heimdal@24208 ec53bebd-3082-4978-b11e-865c3cabbd6b
2008-12-18 04:59:47 +00:00
Love Hörnquist Åstrand
5f2da72d8a make der_get_octet_string_ber less restrictive
git-svn-id: svn://svn.h5l.se/heimdal/trunk/heimdal@24203 ec53bebd-3082-4978-b11e-865c3cabbd6b
2008-12-15 04:32:39 +00:00
Love Hörnquist Åstrand
2e8d2802c8 der_get_octet_string_ber meet reality and now works
git-svn-id: svn://svn.h5l.se/heimdal/trunk/heimdal@24201 ec53bebd-3082-4978-b11e-865c3cabbd6b
2008-12-15 04:32:20 +00:00
Love Hörnquist Åstrand
ff17f1e5a1 return of der_match_tag2
git-svn-id: svn://svn.h5l.se/heimdal/trunk/heimdal@24191 ec53bebd-3082-4978-b11e-865c3cabbd6b
2008-12-15 04:30:42 +00:00
Love Hörnquist Åstrand
26d2cd604d change prototype for der_match_tag_and_length to return the Der_type, add BER decoder for octet string
git-svn-id: svn://svn.h5l.se/heimdal/trunk/heimdal@24188 ec53bebd-3082-4978-b11e-865c3cabbd6b
2008-12-15 04:30:12 +00:00
Love Hörnquist Åstrand
5f961f1e39 copy data is there is data to copy, cid#22
git-svn-id: svn://svn.h5l.se/heimdal/trunk/heimdal@24123 ec53bebd-3082-4978-b11e-865c3cabbd6b
2008-12-11 05:03:37 +00:00
Love Hörnquist Åstrand
6937d41a02 remove trailing whitespace
git-svn-id: svn://svn.h5l.se/heimdal/trunk/heimdal@23815 ec53bebd-3082-4978-b11e-865c3cabbd6b
2008-09-13 09:21:03 +00:00
Love Hörnquist Åstrand
e172367898 switch to utf8 encoding of all files
git-svn-id: svn://svn.h5l.se/heimdal/trunk/heimdal@23814 ec53bebd-3082-4978-b11e-865c3cabbd6b
2008-09-13 08:53:55 +00:00
Love Hörnquist Åstrand
9ab118ca78 (der_get_time): avoid using wrapping of octet_string and realloc.
git-svn-id: svn://svn.h5l.se/heimdal/trunk/heimdal@21369 ec53bebd-3082-4978-b11e-865c3cabbd6b
2007-06-27 10:14:39 +00:00
Love Hörnquist Åstrand
0c489117c5 No need to undef timetm, we don't use it any more.
git-svn-id: svn://svn.h5l.se/heimdal/trunk/heimdal@21367 ec53bebd-3082-4978-b11e-865c3cabbd6b
2007-06-27 10:07:26 +00:00
Love Hörnquist Åstrand
25767cac3f Add malloc paranoid, it shouldn't matter if sizeof(uint{32,16}_t) is 4,2.
git-svn-id: svn://svn.h5l.se/heimdal/trunk/heimdal@20570 ec53bebd-3082-4978-b11e-865c3cabbd6b
2007-04-27 14:06:27 +00:00
Love Hörnquist Åstrand
6f625101d2 Try to be more correct and don't fall off the end. Pointed out by
Kevin Coffman.


git-svn-id: svn://svn.h5l.se/heimdal/trunk/heimdal@20372 ec53bebd-3082-4978-b11e-865c3cabbd6b
2007-04-17 17:47:42 +00:00
Love Hörnquist Åstrand
2e356b6607 Allow trailing NULs. We allow this since MIT Kerberos sends an strings
in the NEED_PREAUTH case that includes a trailing NUL.


git-svn-id: svn://svn.h5l.se/heimdal/trunk/heimdal@20296 ec53bebd-3082-4978-b11e-865c3cabbd6b
2007-04-11 11:09:37 +00:00
Love Hörnquist Åstrand
848c63a5fd Check for NUL characters in string and return ASN1_BAD_CHARACTER
error-code if we find them.


git-svn-id: svn://svn.h5l.se/heimdal/trunk/heimdal@20011 ec53bebd-3082-4978-b11e-865c3cabbd6b
2007-01-20 21:53:32 +00:00
Love Hörnquist Åstrand
983b89b811 Add VisibleString parsing
git-svn-id: svn://svn.h5l.se/heimdal/trunk/heimdal@19539 ec53bebd-3082-4978-b11e-865c3cabbd6b
2006-12-28 17:15:05 +00:00
Love Hörnquist Åstrand
dbc2843b90 (generalizedtime2time): always use _der_timegm.
git-svn-id: svn://svn.h5l.se/heimdal/trunk/heimdal@18609 ec53bebd-3082-4978-b11e-865c3cabbd6b
2006-10-19 16:27:44 +00:00
Love Hörnquist Åstrand
9dc0d8d6b4 rename the buildin timegm to _der_timegm
git-svn-id: svn://svn.h5l.se/heimdal/trunk/heimdal@18447 ec53bebd-3082-4978-b11e-865c3cabbd6b
2006-10-14 09:17:43 +00:00
Love Hörnquist Åstrand
f4f9013804 prefix primitive types with der_
git-svn-id: svn://svn.h5l.se/heimdal/trunk/heimdal@18443 ec53bebd-3082-4978-b11e-865c3cabbd6b
2006-10-14 05:36:34 +00:00
Love Hörnquist Åstrand
d56c2c1496 (der_get_heim_integer): revert part of previous
git-svn-id: svn://svn.h5l.se/heimdal/trunk/heimdal@17685 ec53bebd-3082-4978-b11e-865c3cabbd6b
2006-06-22 07:13:06 +00:00
Love Hörnquist Åstrand
9e5f91e48b (der_get_heim_integer): Add more checks
git-svn-id: svn://svn.h5l.se/heimdal/trunk/heimdal@17679 ec53bebd-3082-4978-b11e-865c3cabbd6b
2006-06-22 03:54:50 +00:00
Love Hörnquist Åstrand
b8c2cec844 (der_get_heim_integer): handle negative integers.
git-svn-id: svn://svn.h5l.se/heimdal/trunk/heimdal@16635 ec53bebd-3082-4978-b11e-865c3cabbd6b
2006-01-20 10:03:50 +00:00
Love Hörnquist Åstrand
dd801b3ca3 (der_match_tag): tag is unsigned int.
git-svn-id: svn://svn.h5l.se/heimdal/trunk/heimdal@15670 ec53bebd-3082-4978-b11e-865c3cabbd6b
2005-07-19 18:04:00 +00:00
Love Hörnquist Åstrand
0f15be4a2d rewrite integer overflow tests w/o SIZE_T_MAX
git-svn-id: svn://svn.h5l.se/heimdal/trunk/heimdal@15662 ec53bebd-3082-4978-b11e-865c3cabbd6b
2005-07-19 15:08:18 +00:00
Love Hörnquist Åstrand
b838707d0e Commit much improved ASN.1 compiler from joda-choice-branch.
Highlighs for the compiler is support for CHOICE and in general better
support for tags. This compiler support most of what is needed for
PK-INIT, LDAP, X.509, PKCS-12 and many other protocols.


git-svn-id: svn://svn.h5l.se/heimdal/trunk/heimdal@15617 ec53bebd-3082-4978-b11e-865c3cabbd6b
2005-07-12 06:27:42 +00:00
Love Hörnquist Åstrand
7cb5f5ba32 revert previous until I've have cleaned libvers mess
git-svn-id: svn://svn.h5l.se/heimdal/trunk/heimdal@15589 ec53bebd-3082-4978-b11e-865c3cabbd6b
2005-07-07 22:11:00 +00:00
Love Hörnquist Åstrand
421d2b645f don't include <version.h>, it forces unnecessary rebuilds
git-svn-id: svn://svn.h5l.se/heimdal/trunk/heimdal@15579 ec53bebd-3082-4978-b11e-865c3cabbd6b
2005-07-07 20:48:57 +00:00
Love Hörnquist Åstrand
8e78ed6e48 prefix Der_class with ASN1_C_ to avoid problems with system headerfiles that pollute the name space
git-svn-id: svn://svn.h5l.se/heimdal/trunk/heimdal@15255 ec53bebd-3082-4978-b11e-865c3cabbd6b
2005-05-29 14:23:01 +00:00
Love Hörnquist Åstrand
f04518dc7f (decode_*): name all tag-length variables the same
(decode_enumerated): check that the tag-length is not longer the length


git-svn-id: svn://svn.h5l.se/heimdal/trunk/heimdal@14189 ec53bebd-3082-4978-b11e-865c3cabbd6b
2004-09-05 15:29:22 +00:00
Love Hörnquist Åstrand
861f615ca9 (decode_boolean): fail if length of tag is larger then len
git-svn-id: svn://svn.h5l.se/heimdal/trunk/heimdal@14187 ec53bebd-3082-4978-b11e-865c3cabbd6b
2004-09-05 15:23:43 +00:00