Re: [TLS] Length of a variable-length vector: Could it be an odd multiple?

Benjamin Kaduk <bkaduk@akamai.com> Sat, 23 January 2016 00:50 UTC

Return-Path: <bkaduk@akamai.com>
X-Original-To: tls@ietfa.amsl.com
Delivered-To: tls@ietfa.amsl.com
Received: from localhost (ietfa.amsl.com [127.0.0.1]) by ietfa.amsl.com (Postfix) with ESMTP id 0EEB51B2E50 for <tls@ietfa.amsl.com>; Fri, 22 Jan 2016 16:50:38 -0800 (PST)
X-Virus-Scanned: amavisd-new at amsl.com
X-Spam-Flag: NO
X-Spam-Score: -2.702
X-Spam-Level:
X-Spam-Status: No, score=-2.702 tagged_above=-999 required=5 tests=[BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RP_MATCHES_RCVD=-0.001, SPF_PASS=-0.001] autolearn=ham
Received: from mail.ietf.org ([4.31.198.44]) by localhost (ietfa.amsl.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id hcCWQoV11Gb8 for <tls@ietfa.amsl.com>; Fri, 22 Jan 2016 16:50:34 -0800 (PST)
Received: from prod-mail-xrelay05.akamai.com (prod-mail-xrelay05.akamai.com [23.79.238.179]) by ietfa.amsl.com (Postfix) with ESMTP id 56D221B2E47 for <tls@ietf.org>; Fri, 22 Jan 2016 16:50:34 -0800 (PST)
Received: from prod-mail-xrelay05.akamai.com (localhost.localdomain [127.0.0.1]) by postfix.imss70 (Postfix) with ESMTP id 5220E3F4002; Sat, 23 Jan 2016 00:50:33 +0000 (GMT)
Received: from prod-mail-relay10.akamai.com (prod-mail-relay10.akamai.com [172.27.118.251]) by prod-mail-xrelay05.akamai.com (Postfix) with ESMTP id 012CF3F4008; Sat, 23 Jan 2016 00:50:33 +0000 (GMT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=akamai.com; s=a1; t=1453510233; bh=a+VzDaB3MFqXI32V8AtXdki1VkIR0p9XHFQe047dEVI=; l=4740; h=To:References:From:Date:In-Reply-To:From; b=riaxxKZIpbVvOWfUUc2hMPKesPpJ/0gA8Dy7kppCnj0B6SKN2l3gqDG42lZtCosuS gN4cOJq3MWVyb7IV0XEgDo0JDr5OtGZIpjHdm9LlNFZ8I0n/5tvFRv+tUn7CREbl1G 1H56iz/e5hGrzhXF+6KHQaNMYjmTsWWyIdGeLSHw=
Received: from [172.19.0.25] (bos-lpczi.kendall.corp.akamai.com [172.19.0.25]) by prod-mail-relay10.akamai.com (Postfix) with ESMTP id C6175202C; Sat, 23 Jan 2016 00:50:32 +0000 (GMT)
To: =JeffH <Jeff.Hodges@KingsMountain.com>, IETF TLS WG <tls@ietf.org>
References: <56A2CC9F.7070007@KingsMountain.com>
From: Benjamin Kaduk <bkaduk@akamai.com>
Message-ID: <56A2CE58.1030904@akamai.com>
Date: Fri, 22 Jan 2016 18:50:32 -0600
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.5.1
MIME-Version: 1.0
In-Reply-To: <56A2CC9F.7070007@KingsMountain.com>
Content-Type: text/plain; charset="windows-1252"
Content-Transfer-Encoding: 7bit
Archived-At: <http://mailarchive.ietf.org/arch/msg/tls/tyoecNGorqz-4XRksF7rpb9IFHc>
Subject: Re: [TLS] Length of a variable-length vector: Could it be an odd multiple?
X-BeenThere: tls@ietf.org
X-Mailman-Version: 2.1.15
Precedence: list
List-Id: "This is the mailing list for the Transport Layer Security working group of the IETF." <tls.ietf.org>
List-Unsubscribe: <https://www.ietf.org/mailman/options/tls>, <mailto:tls-request@ietf.org?subject=unsubscribe>
List-Archive: <https://mailarchive.ietf.org/arch/browse/tls/>
List-Post: <mailto:tls@ietf.org>
List-Help: <mailto:tls-request@ietf.org?subject=help>
List-Subscribe: <https://www.ietf.org/mailman/listinfo/tls>, <mailto:tls-request@ietf.org?subject=subscribe>
X-List-Received-Date: Sat, 23 Jan 2016 00:50:38 -0000

On 01/22/2016 06:43 PM, =JeffH wrote:
> ON Fri, 22 Jan 2016 15:53:27 Benjamin Kaduk noted:
> > On 01/22/2016 03:26 PM, =JeffH wrote:
> > > On 01/22/2016 12:29 PM, =JeffH wrote:
> > >> [ fixed pitch font advised here ]
> > >
> > > the below is corrected to use "byte count" rather than "index" or
> > > "indicies" (and to ditch the tabs)..
> > >
> > >
> > > > On 01/22/2016 09:42 AM, =JeffH wrote:
> > > > > [ resending from different account - my work addr ends up in spam
> > > > > bucket for many it seems ]
> > > > >
> > > > > On 1/20/16, 11:01 AM, "Benjamin Kaduk" <bkaduk@akamai.com> wrote:
> > > > > >On 01/20/2016 12:47 PM, Hodges, Jeff wrote:
> > > > > >> On 1/13/16, 12:53 PM, "Benjamin Kaduk" <bkaduk@akamai.com>
> wrote:
> > > > > >>> On 01/13/2016 02:44 PM, Jong-Shian Wu wrote:
> >>>>>>>> I have a question about the even-vs-odd restrictions on the
> >>>>>>>> length of a valid variable-length vector defined in TLS
> >>>>>>>> specification  after reading the section 4.3 of RFC 5246
> >>>>>>>> [1] which states that: "The length of an encoded vector
> >>>>>>>> must be an even multiple of the length of a single element
> >>>>>>>> (for example, a 17-byte vector of uint16 would be
> >>>>>>>> illegal)."
> >>>>>>>>
> >>>>>>> It means "whole-number" as opposed to fractional, i.e., there
> >>>>>>> should not be unused "junk bytes" at the end.
> >>>>>>
> >>>>>> In case it's helpful, here's a suggested re-write of that
> >>>>>> quoted sentence above..
> > > > > >>
> > > > > >> The length of an encoded variable-length vector must be an
> > > > > >> exact multiple of the length of a single element. For example,
> > > > > >> an encoded 17-byte vector of uint16 would be illegal, and an
> > > > > >> encoded variable-length vector of four 32 byte elements,
> > > > > >> having a ceiling of 2^16-1, will be 130 bytes long overall
> > > > > >> (2 byte length field followed by 128 bytes of data).
> > > > > >
> > > > > >Wouldn't the ceiling more properly be 2^16-4 in that case?
> > > > >
> > > > > hm, I'm not sure -- what would be the rationale? The exact
> multiple
> > > > > criteria? but 2^16 / 32 = 2048 while (2^16-4) / 32 = 2047.875
> > > >
> > > > Ah, I seem to have conflated bits and bytes due to reading too
> quickly
> > > > and should have said (2^16-32), as Ilari alluded to with "or
> rounding
> > > > thereof to integral multiple of
> > > > elements".
> > >
> > > hm, but in this case it seems that a variable-length vector declared
> > > with a length range of <0..2^16-1> would exactly accommodate up to
> > > 2048 32-byte elements..
> > >
> > > opaque Foo[32] ;
> > >
> > > Foo fooSequence<0..2^16-1>; /* will accommodate up to 2048
> > > Foo instances */
> > >
> > > ..because it has a zero-based byte count, as in this example..
> > >
> > >
> > > opaque Array<0..2^2-1> ; /* should accommodate
> > > 2^2 = 4 1-byte elements */
> > >
> > > /*
> > > Array with 4
> > > elements in memory: [ xx xx xx xx ]
> > > byte count (hex): 0 1 2 3 3 = 2^2-1
> > >
> > > byte count (binary): 00 01 10 11
> > >
> > > */
> > >
> > >
> > > ..yes? or am I missing something?
> >
> > You are missing something.
> >
> > The encoded length represents the actual number of bytes that will
> > follow, so that a zero-length array with maximum length 2^16-1 is
> > encoded as just 00 00. So, even though the index into the array starts
> > at zero, the actual length "starts at" 1, just like in C.
>
> Ok, thanks, yes, I was confusing byte count with indices.
>
> To test my understanding:
>
> Given..
>
>    opaque Foo[32] ;
>
>    Foo fooSequence<0..2^16-32> ;
>
>
> ..fooSequence will accommodate up to 2047 Foo instances, and the
> length prefix of fooSequence will itself be two bytes long, yes?
>

Yes.

>
> AFAICT, though, from [1] (and prior TLS specs), it seems legit to
> declare fooSequence like so..
>
>  Foo fooSequence<0..2^16-1> ;
>
> ..with the expectation that a correct implementation will allow only
> 2047 Foo instances to be encoded into a fooSequence on the wire, yes ?
>

I believe a correct implementation would do so, yes, though I am not
quite enough of a spec lawyer to be able to state that authoritatively. 
The <0..2^16-32> form is perhaps friendlier to the reader.

>
> Also, if one actually needs to be able to accommodate up to _2048_ Foo
> instances, and can tolerate having the length prefix be three bytes
> long, then one could declare fooSequence as..
>
>    Foo fooSequence<0..2^16>;
>
> ..or..
>
>    Foo fooSequence<0..2^24-1>;
>
> ..yes?

Yes.  (The latter form might permit an attacker to force a peer to
consume a lot of memory processing a request, of course.)

-Ben