OpenCoverage

bs_cbb.c

Absolute File Name:/home/opencoverage/opencoverage/guest-scripts/libressl/src/ssl/bs_cbb.c
Source codeSwitch to Preprocessed file
LineSourceCount
1/* $OpenBSD: bs_cbb.c,v 1.19 2018/08/16 18:39:37 jsing Exp $ */-
2/*-
3 * Copyright (c) 2014, Google Inc.-
4 *-
5 * Permission to use, copy, modify, and/or distribute this software for any-
6 * purpose with or without fee is hereby granted, provided that the above-
7 * copyright notice and this permission notice appear in all copies.-
8 *-
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES-
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF-
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY-
12 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES-
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION-
14 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN-
15 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */-
16-
17#include <assert.h>-
18#include <stdlib.h>-
19#include <string.h>-
20-
21#include <openssl/opensslconf.h>-
22-
23#include "bytestring.h"-
24-
25#define CBB_INITIAL_SIZE 64-
26-
27static int-
28cbb_init(CBB *cbb, uint8_t *buf, size_t cap)-
29{-
30 struct cbb_buffer_st *base;-
31-
32 base = malloc(sizeof(struct cbb_buffer_st));-
33 if (base == NULL)
base == ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 1859 times by 9 tests
Evaluated by:
  • asn1test
  • bytestringtest
  • cipher_list
  • cipherstest
  • clienttest
  • servertest
  • ssltest
  • tlsexttest
  • tlstest
0-1859
34 return 0;
never executed: return 0;
0
35-
36 base->buf = buf;-
37 base->len = 0;-
38 base->cap = cap;-
39 base->can_resize = 1;-
40-
41 cbb->base = base;-
42 cbb->is_top_level = 1;-
43-
44 return 1;
executed 1859 times by 9 tests: return 1;
Executed by:
  • asn1test
  • bytestringtest
  • cipher_list
  • cipherstest
  • clienttest
  • servertest
  • ssltest
  • tlsexttest
  • tlstest
1859
45}-
46-
47int-
48CBB_init(CBB *cbb, size_t initial_capacity)-
49{-
50 uint8_t *buf = NULL;-
51-
52 memset(cbb, 0, sizeof(*cbb));-
53-
54 if (initial_capacity == 0)
initial_capacity == 0Description
TRUEevaluated 171 times by 6 tests
Evaluated by:
  • asn1test
  • bytestringtest
  • servertest
  • ssltest
  • tlsexttest
  • tlstest
FALSEevaluated 586 times by 5 tests
Evaluated by:
  • bytestringtest
  • clienttest
  • servertest
  • ssltest
  • tlstest
171-586
55 initial_capacity = CBB_INITIAL_SIZE;
executed 171 times by 6 tests: initial_capacity = 64;
Executed by:
  • asn1test
  • bytestringtest
  • servertest
  • ssltest
  • tlsexttest
  • tlstest
171
56-
57 if ((buf = malloc(initial_capacity)) == NULL)
(buf = malloc(...== ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 757 times by 7 tests
Evaluated by:
  • asn1test
  • bytestringtest
  • clienttest
  • servertest
  • ssltest
  • tlsexttest
  • tlstest
0-757
58 return 0;
never executed: return 0;
0
59-
60 if (!cbb_init(cbb, buf, initial_capacity)) {
!cbb_init(cbb,...tial_capacity)Description
TRUEnever evaluated
FALSEevaluated 757 times by 7 tests
Evaluated by:
  • asn1test
  • bytestringtest
  • clienttest
  • servertest
  • ssltest
  • tlsexttest
  • tlstest
0-757
61 free(buf);-
62 return 0;
never executed: return 0;
0
63 }-
64-
65 return 1;
executed 757 times by 7 tests: return 1;
Executed by:
  • asn1test
  • bytestringtest
  • clienttest
  • servertest
  • ssltest
  • tlsexttest
  • tlstest
757
66}-
67-
68int-
69CBB_init_fixed(CBB *cbb, uint8_t *buf, size_t len)-
70{-
71 memset(cbb, 0, sizeof(*cbb));-
72-
73 if (!cbb_init(cbb, buf, len))
!cbb_init(cbb, buf, len)Description
TRUEnever evaluated
FALSEevaluated 1102 times by 7 tests
Evaluated by:
  • bytestringtest
  • cipher_list
  • cipherstest
  • clienttest
  • servertest
  • ssltest
  • tlstest
0-1102
74 return 0;
never executed: return 0;
0
75-
76 cbb->base->can_resize = 0;-
77-
78 return 1;
executed 1102 times by 7 tests: return 1;
Executed by:
  • bytestringtest
  • cipher_list
  • cipherstest
  • clienttest
  • servertest
  • ssltest
  • tlstest
1102
79}-
80-
81void-
82CBB_cleanup(CBB *cbb)-
83{-
84 if (cbb->base) {
cbb->baseDescription
TRUEevaluated 1859 times by 9 tests
Evaluated by:
  • asn1test
  • bytestringtest
  • cipher_list
  • cipherstest
  • clienttest
  • servertest
  • ssltest
  • tlsexttest
  • tlstest
FALSEevaluated 95 times by 4 tests
Evaluated by:
  • asn1test
  • servertest
  • ssltest
  • tlsexttest
95-1859
85 if (cbb->base->can_resize)
cbb->base->can_resizeDescription
TRUEevaluated 757 times by 7 tests
Evaluated by:
  • asn1test
  • bytestringtest
  • clienttest
  • servertest
  • ssltest
  • tlsexttest
  • tlstest
FALSEevaluated 1102 times by 7 tests
Evaluated by:
  • bytestringtest
  • cipher_list
  • cipherstest
  • clienttest
  • servertest
  • ssltest
  • tlstest
757-1102
86 freezero(cbb->base->buf, cbb->base->cap);
executed 757 times by 7 tests: freezero(cbb->base->buf, cbb->base->cap);
Executed by:
  • asn1test
  • bytestringtest
  • clienttest
  • servertest
  • ssltest
  • tlsexttest
  • tlstest
757
87 free(cbb->base);-
88 }
executed 1859 times by 9 tests: end of block
Executed by:
  • asn1test
  • bytestringtest
  • cipher_list
  • cipherstest
  • clienttest
  • servertest
  • ssltest
  • tlsexttest
  • tlstest
1859
89 cbb->base = NULL;-
90 cbb->child = NULL;-
91}
executed 1954 times by 9 tests: end of block
Executed by:
  • asn1test
  • bytestringtest
  • cipher_list
  • cipherstest
  • clienttest
  • servertest
  • ssltest
  • tlsexttest
  • tlstest
1954
92-
93static int-
94cbb_buffer_add(struct cbb_buffer_st *base, uint8_t **out, size_t len)-
95{-
96 size_t newlen;-
97-
98 if (base == NULL)
base == ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 11386 times by 9 tests
Evaluated by:
  • asn1test
  • bytestringtest
  • cipher_list
  • cipherstest
  • clienttest
  • servertest
  • ssltest
  • tlsexttest
  • tlstest
0-11386
99 return 0;
never executed: return 0;
0
100-
101 newlen = base->len + len;-
102 if (newlen < base->len)
newlen < base->lenDescription
TRUEnever evaluated
FALSEevaluated 11386 times by 9 tests
Evaluated by:
  • asn1test
  • bytestringtest
  • cipher_list
  • cipherstest
  • clienttest
  • servertest
  • ssltest
  • tlsexttest
  • tlstest
0-11386
103 /* Overflow */-
104 return 0;
never executed: return 0;
0
105-
106 if (newlen > base->cap) {
newlen > base->capDescription
TRUEevaluated 183 times by 5 tests
Evaluated by:
  • asn1test
  • bytestringtest
  • servertest
  • ssltest
  • tlsexttest
FALSEevaluated 11203 times by 9 tests
Evaluated by:
  • asn1test
  • bytestringtest
  • cipher_list
  • cipherstest
  • clienttest
  • servertest
  • ssltest
  • tlsexttest
  • tlstest
183-11203
107 size_t newcap = base->cap * 2;-
108 uint8_t *newbuf;-
109-
110 if (!base->can_resize)
!base->can_resizeDescription
TRUEevaluated 2 times by 1 test
Evaluated by:
  • bytestringtest
FALSEevaluated 181 times by 5 tests
Evaluated by:
  • asn1test
  • bytestringtest
  • servertest
  • ssltest
  • tlsexttest
2-181
111 return 0;
executed 2 times by 1 test: return 0;
Executed by:
  • bytestringtest
2
112-
113 if (newcap < base->cap || newcap < newlen)
newcap < base->capDescription
TRUEnever evaluated
FALSEevaluated 181 times by 5 tests
Evaluated by:
  • asn1test
  • bytestringtest
  • servertest
  • ssltest
  • tlsexttest
newcap < newlenDescription
TRUEevaluated 37 times by 5 tests
Evaluated by:
  • asn1test
  • bytestringtest
  • servertest
  • ssltest
  • tlsexttest
FALSEevaluated 144 times by 4 tests
Evaluated by:
  • asn1test
  • bytestringtest
  • servertest
  • ssltest
0-181
114 newcap = newlen;
executed 37 times by 5 tests: newcap = newlen;
Executed by:
  • asn1test
  • bytestringtest
  • servertest
  • ssltest
  • tlsexttest
37
115-
116 newbuf = recallocarray(base->buf, base->cap, newcap, 1);-
117 if (newbuf == NULL)
newbuf == ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 181 times by 5 tests
Evaluated by:
  • asn1test
  • bytestringtest
  • servertest
  • ssltest
  • tlsexttest
0-181
118 return 0;
never executed: return 0;
0
119-
120 base->buf = newbuf;-
121 base->cap = newcap;-
122 }
executed 181 times by 5 tests: end of block
Executed by:
  • asn1test
  • bytestringtest
  • servertest
  • ssltest
  • tlsexttest
181
123-
124 if (out)
outDescription
TRUEevaluated 11358 times by 9 tests
Evaluated by:
  • asn1test
  • bytestringtest
  • cipher_list
  • cipherstest
  • clienttest
  • servertest
  • ssltest
  • tlsexttest
  • tlstest
FALSEevaluated 26 times by 3 tests
Evaluated by:
  • asn1test
  • bytestringtest
  • ssltest
26-11358
125 *out = base->buf + base->len;
executed 11358 times by 9 tests: *out = base->buf + base->len;
Executed by:
  • asn1test
  • bytestringtest
  • cipher_list
  • cipherstest
  • clienttest
  • servertest
  • ssltest
  • tlsexttest
  • tlstest
11358
126-
127 base->len = newlen;-
128 return 1;
executed 11384 times by 9 tests: return 1;
Executed by:
  • asn1test
  • bytestringtest
  • cipher_list
  • cipherstest
  • clienttest
  • servertest
  • ssltest
  • tlsexttest
  • tlstest
11384
129}-
130-
131static int-
132cbb_add_u(CBB *cbb, uint32_t v, size_t len_len)-
133{-
134 uint8_t *buf;-
135 size_t i;-
136-
137 if (len_len == 0)
len_len == 0Description
TRUEnever evaluated
FALSEevaluated 7688 times by 9 tests
Evaluated by:
  • asn1test
  • bytestringtest
  • cipher_list
  • cipherstest
  • clienttest
  • servertest
  • ssltest
  • tlsexttest
  • tlstest
0-7688
138 return 1;
never executed: return 1;
0
139-
140 if (len_len > 4)
len_len > 4Description
TRUEnever evaluated
FALSEevaluated 7688 times by 9 tests
Evaluated by:
  • asn1test
  • bytestringtest
  • cipher_list
  • cipherstest
  • clienttest
  • servertest
  • ssltest
  • tlsexttest
  • tlstest
0-7688
141 return 0;
never executed: return 0;
0
142-
143 if (!CBB_flush(cbb) || !cbb_buffer_add(cbb->base, &buf, len_len))
!CBB_flush(cbb)Description
TRUEevaluated 3 times by 1 test
Evaluated by:
  • bytestringtest
FALSEevaluated 7685 times by 9 tests
Evaluated by:
  • asn1test
  • bytestringtest
  • cipher_list
  • cipherstest
  • clienttest
  • servertest
  • ssltest
  • tlsexttest
  • tlstest
!cbb_buffer_ad...&buf, len_len)Description
TRUEevaluated 2 times by 1 test
Evaluated by:
  • bytestringtest
FALSEevaluated 7683 times by 9 tests
Evaluated by:
  • asn1test
  • bytestringtest
  • cipher_list
  • cipherstest
  • clienttest
  • servertest
  • ssltest
  • tlsexttest
  • tlstest
2-7685
144 return 0;
executed 5 times by 1 test: return 0;
Executed by:
  • bytestringtest
5
145-
146 for (i = len_len - 1; i < len_len; i--) {
i < len_lenDescription
TRUEevaluated 12843 times by 9 tests
Evaluated by:
  • asn1test
  • bytestringtest
  • cipher_list
  • cipherstest
  • clienttest
  • servertest
  • ssltest
  • tlsexttest
  • tlstest
FALSEevaluated 7683 times by 9 tests
Evaluated by:
  • asn1test
  • bytestringtest
  • cipher_list
  • cipherstest
  • clienttest
  • servertest
  • ssltest
  • tlsexttest
  • tlstest
7683-12843
147 buf[i] = v;-
148 v >>= 8;-
149 }
executed 12843 times by 9 tests: end of block
Executed by:
  • asn1test
  • bytestringtest
  • cipher_list
  • cipherstest
  • clienttest
  • servertest
  • ssltest
  • tlsexttest
  • tlstest
12843
150 return 1;
executed 7683 times by 9 tests: return 1;
Executed by:
  • asn1test
  • bytestringtest
  • cipher_list
  • cipherstest
  • clienttest
  • servertest
  • ssltest
  • tlsexttest
  • tlstest
7683
151}-
152-
153int-
154CBB_finish(CBB *cbb, uint8_t **out_data, size_t *out_len)-
155{-
156 if (!cbb->is_top_level)
!cbb->is_top_levelDescription
TRUEevaluated 1 time by 1 test
Evaluated by:
  • bytestringtest
FALSEevaluated 1847 times by 9 tests
Evaluated by:
  • asn1test
  • bytestringtest
  • cipher_list
  • cipherstest
  • clienttest
  • servertest
  • ssltest
  • tlsexttest
  • tlstest
1-1847
157 return 0;
executed 1 time by 1 test: return 0;
Executed by:
  • bytestringtest
1
158-
159 if (!CBB_flush(cbb))
!CBB_flush(cbb)Description
TRUEnever evaluated
FALSEevaluated 1847 times by 9 tests
Evaluated by:
  • asn1test
  • bytestringtest
  • cipher_list
  • cipherstest
  • clienttest
  • servertest
  • ssltest
  • tlsexttest
  • tlstest
0-1847
160 return 0;
never executed: return 0;
0
161-
162 if (cbb->base->can_resize && (out_data == NULL || out_len == NULL))
cbb->base->can_resizeDescription
TRUEevaluated 745 times by 7 tests
Evaluated by:
  • asn1test
  • bytestringtest
  • clienttest
  • servertest
  • ssltest
  • tlsexttest
  • tlstest
FALSEevaluated 1102 times by 7 tests
Evaluated by:
  • bytestringtest
  • cipher_list
  • cipherstest
  • clienttest
  • servertest
  • ssltest
  • tlstest
out_data == ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 745 times by 7 tests
Evaluated by:
  • asn1test
  • bytestringtest
  • clienttest
  • servertest
  • ssltest
  • tlsexttest
  • tlstest
out_len == ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 745 times by 7 tests
Evaluated by:
  • asn1test
  • bytestringtest
  • clienttest
  • servertest
  • ssltest
  • tlsexttest
  • tlstest
0-1102
163 /*-
164 * |out_data| and |out_len| can only be NULL if the CBB is-
165 * fixed.-
166 */-
167 return 0;
never executed: return 0;
0
168-
169 if (out_data != NULL)
out_data != ((void *)0)Description
TRUEevaluated 747 times by 7 tests
Evaluated by:
  • asn1test
  • bytestringtest
  • clienttest
  • servertest
  • ssltest
  • tlsexttest
  • tlstest
FALSEevaluated 1100 times by 6 tests
Evaluated by:
  • cipher_list
  • cipherstest
  • clienttest
  • servertest
  • ssltest
  • tlstest
747-1100
170 *out_data = cbb->base->buf;
executed 747 times by 7 tests: *out_data = cbb->base->buf;
Executed by:
  • asn1test
  • bytestringtest
  • clienttest
  • servertest
  • ssltest
  • tlsexttest
  • tlstest
747
171-
172 if (out_len != NULL)
out_len != ((void *)0)Description
TRUEevaluated 875 times by 8 tests
Evaluated by:
  • asn1test
  • bytestringtest
  • cipher_list
  • clienttest
  • servertest
  • ssltest
  • tlsexttest
  • tlstest
FALSEevaluated 972 times by 4 tests
Evaluated by:
  • cipherstest
  • clienttest
  • servertest
  • ssltest
875-972
173 *out_len = cbb->base->len;
executed 875 times by 8 tests: *out_len = cbb->base->len;
Executed by:
  • asn1test
  • bytestringtest
  • cipher_list
  • clienttest
  • servertest
  • ssltest
  • tlsexttest
  • tlstest
875
174-
175 cbb->base->buf = NULL;-
176 CBB_cleanup(cbb);-
177 return 1;
executed 1847 times by 9 tests: return 1;
Executed by:
  • asn1test
  • bytestringtest
  • cipher_list
  • cipherstest
  • clienttest
  • servertest
  • ssltest
  • tlsexttest
  • tlstest
1847
178}-
179-
180/*-
181 * CBB_flush recurses and then writes out any pending length prefix. The current-
182 * length of the underlying base is taken to be the length of the-
183 * length-prefixed data.-
184 */-
185int-
186CBB_flush(CBB *cbb)-
187{-
188 size_t child_start, i, len;-
189-
190 if (cbb->base == NULL)
cbb->base == ((void *)0)Description
TRUEevaluated 7 times by 1 test
Evaluated by:
  • bytestringtest
FALSEevaluated 18091 times by 9 tests
Evaluated by:
  • asn1test
  • bytestringtest
  • cipher_list
  • cipherstest
  • clienttest
  • servertest
  • ssltest
  • tlsexttest
  • tlstest
7-18091
191 return 0;
executed 7 times by 1 test: return 0;
Executed by:
  • bytestringtest
7
192-
193 if (cbb->child == NULL || cbb->pending_len_len == 0)
cbb->child == ((void *)0)Description
TRUEevaluated 15138 times by 9 tests
Evaluated by:
  • asn1test
  • bytestringtest
  • cipher_list
  • cipherstest
  • clienttest
  • servertest
  • ssltest
  • tlsexttest
  • tlstest
FALSEevaluated 2953 times by 7 tests
Evaluated by:
  • asn1test
  • bytestringtest
  • clienttest
  • servertest
  • ssltest
  • tlsexttest
  • tlstest
cbb->pending_len_len == 0Description
TRUEnever evaluated
FALSEevaluated 2953 times by 7 tests
Evaluated by:
  • asn1test
  • bytestringtest
  • clienttest
  • servertest
  • ssltest
  • tlsexttest
  • tlstest
0-15138
194 return 1;
executed 15138 times by 9 tests: return 1;
Executed by:
  • asn1test
  • bytestringtest
  • cipher_list
  • cipherstest
  • clienttest
  • servertest
  • ssltest
  • tlsexttest
  • tlstest
15138
195-
196 child_start = cbb->offset + cbb->pending_len_len;-
197-
198 if (!CBB_flush(cbb->child) || child_start < cbb->offset ||
!CBB_flush(cbb->child)Description
TRUEnever evaluated
FALSEevaluated 2953 times by 7 tests
Evaluated by:
  • asn1test
  • bytestringtest
  • clienttest
  • servertest
  • ssltest
  • tlsexttest
  • tlstest
child_start < cbb->offsetDescription
TRUEnever evaluated
FALSEevaluated 2953 times by 7 tests
Evaluated by:
  • asn1test
  • bytestringtest
  • clienttest
  • servertest
  • ssltest
  • tlsexttest
  • tlstest
0-2953
199 cbb->base->len < child_start)
cbb->base->len < child_startDescription
TRUEnever evaluated
FALSEevaluated 2953 times by 7 tests
Evaluated by:
  • asn1test
  • bytestringtest
  • clienttest
  • servertest
  • ssltest
  • tlsexttest
  • tlstest
0-2953
200 return 0;
never executed: return 0;
0
201-
202 len = cbb->base->len - child_start;-
203-
204 if (cbb->pending_is_asn1) {
cbb->pending_is_asn1Description
TRUEevaluated 828 times by 3 tests
Evaluated by:
  • asn1test
  • bytestringtest
  • ssltest
FALSEevaluated 2125 times by 6 tests
Evaluated by:
  • bytestringtest
  • clienttest
  • servertest
  • ssltest
  • tlsexttest
  • tlstest
828-2125
205 /*-
206 * For ASN.1, we assumed that we were using short form which-
207 * only requires a single byte for the length octet.-
208 *-
209 * If it turns out that we need long form, we have to move-
210 * the contents along in order to make space for more length-
211 * octets.-
212 */-
213 size_t len_len = 1; /* total number of length octets */-
214 uint8_t initial_length_byte;-
215-
216 /* We already wrote 1 byte for the length. */-
217 assert (cbb->pending_len_len == 1);-
218-
219 /* Check for long form */-
220 if (len > 0xfffffffe)
len > 0xfffffffeDescription
TRUEnever evaluated
FALSEevaluated 828 times by 3 tests
Evaluated by:
  • asn1test
  • bytestringtest
  • ssltest
0-828
221 return 0; /* 0xffffffff is reserved */
never executed: return 0;
0
222 else if (len > 0xffffff)
len > 0xffffffDescription
TRUEnever evaluated
FALSEevaluated 828 times by 3 tests
Evaluated by:
  • asn1test
  • bytestringtest
  • ssltest
0-828
223 len_len = 5;
never executed: len_len = 5;
0
224 else if (len > 0xffff)
len > 0xffffDescription
TRUEevaluated 2 times by 1 test
Evaluated by:
  • bytestringtest
FALSEevaluated 826 times by 3 tests
Evaluated by:
  • asn1test
  • bytestringtest
  • ssltest
2-826
225 len_len = 4;
executed 2 times by 1 test: len_len = 4;
Executed by:
  • bytestringtest
2
226 else if (len > 0xff)
len > 0xffDescription
TRUEevaluated 19 times by 3 tests
Evaluated by:
  • asn1test
  • bytestringtest
  • ssltest
FALSEevaluated 807 times by 3 tests
Evaluated by:
  • asn1test
  • bytestringtest
  • ssltest
19-807
227 len_len = 3;
executed 19 times by 3 tests: len_len = 3;
Executed by:
  • asn1test
  • bytestringtest
  • ssltest
19
228 else if (len > 0x7f)
len > 0x7fDescription
TRUEevaluated 5 times by 2 tests
Evaluated by:
  • asn1test
  • bytestringtest
FALSEevaluated 802 times by 3 tests
Evaluated by:
  • asn1test
  • bytestringtest
  • ssltest
5-802
229 len_len = 2;
executed 5 times by 2 tests: len_len = 2;
Executed by:
  • asn1test
  • bytestringtest
5
230-
231 if (len_len == 1) {
len_len == 1Description
TRUEevaluated 802 times by 3 tests
Evaluated by:
  • asn1test
  • bytestringtest
  • ssltest
FALSEevaluated 26 times by 3 tests
Evaluated by:
  • asn1test
  • bytestringtest
  • ssltest
26-802
232 /* For short form, the initial byte is the length. */-
233 initial_length_byte = len;-
234 len = 0;-
235-
236 } else {
executed 802 times by 3 tests: end of block
Executed by:
  • asn1test
  • bytestringtest
  • ssltest
802
237 /*-
238 * For long form, the initial byte is the number of-
239 * subsequent length octets (plus bit 8 set).-
240 */-
241 initial_length_byte = 0x80 | (len_len - 1);-
242-
243 /*-
244 * We need to move the contents along in order to make-
245 * space for the long form length octets.-
246 */-
247 size_t extra_bytes = len_len - 1;-
248 if (!cbb_buffer_add(cbb->base, NULL, extra_bytes))
!cbb_buffer_ad..., extra_bytes)Description
TRUEnever evaluated
FALSEevaluated 26 times by 3 tests
Evaluated by:
  • asn1test
  • bytestringtest
  • ssltest
0-26
249 return 0;
never executed: return 0;
0
250-
251 memmove(cbb->base->buf + child_start + extra_bytes,-
252 cbb->base->buf + child_start, len);-
253 }
executed 26 times by 3 tests: end of block
Executed by:
  • asn1test
  • bytestringtest
  • ssltest
26
254 cbb->base->buf[cbb->offset++] = initial_length_byte;-
255 cbb->pending_len_len = len_len - 1;-
256 }
executed 828 times by 3 tests: end of block
Executed by:
  • asn1test
  • bytestringtest
  • ssltest
828
257-
258 for (i = cbb->pending_len_len - 1; i < cbb->pending_len_len; i--) {
i < cbb->pending_len_lenDescription
TRUEevaluated 4590 times by 7 tests
Evaluated by:
  • asn1test
  • bytestringtest
  • clienttest
  • servertest
  • ssltest
  • tlsexttest
  • tlstest
FALSEevaluated 2953 times by 7 tests
Evaluated by:
  • asn1test
  • bytestringtest
  • clienttest
  • servertest
  • ssltest
  • tlsexttest
  • tlstest
2953-4590
259 cbb->base->buf[cbb->offset + i] = len;-
260 len >>= 8;-
261 }
executed 4590 times by 7 tests: end of block
Executed by:
  • asn1test
  • bytestringtest
  • clienttest
  • servertest
  • ssltest
  • tlsexttest
  • tlstest
4590
262 if (len != 0)
len != 0Description
TRUEnever evaluated
FALSEevaluated 2953 times by 7 tests
Evaluated by:
  • asn1test
  • bytestringtest
  • clienttest
  • servertest
  • ssltest
  • tlsexttest
  • tlstest
0-2953
263 return 0;
never executed: return 0;
0
264-
265 cbb->child->base = NULL;-
266 cbb->child = NULL;-
267 cbb->pending_len_len = 0;-
268 cbb->pending_is_asn1 = 0;-
269 cbb->offset = 0;-
270-
271 return 1;
executed 2953 times by 7 tests: return 1;
Executed by:
  • asn1test
  • bytestringtest
  • clienttest
  • servertest
  • ssltest
  • tlsexttest
  • tlstest
2953
272}-
273-
274void-
275CBB_discard_child(CBB *cbb)-
276{-
277 if (cbb->child == NULL)
cbb->child == ((void *)0)Description
TRUEevaluated 1 time by 1 test
Evaluated by:
  • bytestringtest
FALSEevaluated 4 times by 3 tests
Evaluated by:
  • bytestringtest
  • servertest
  • tlsexttest
1-4
278 return;
executed 1 time by 1 test: return;
Executed by:
  • bytestringtest
1
279-
280 cbb->base->len = cbb->offset;-
281 -
282 cbb->child->base = NULL;-
283 cbb->child = NULL;-
284 cbb->pending_len_len = 0;-
285 cbb->pending_is_asn1 = 0;-
286 cbb->offset = 0;-
287}
executed 4 times by 3 tests: end of block
Executed by:
  • bytestringtest
  • servertest
  • tlsexttest
4
288-
289static int-
290cbb_add_length_prefixed(CBB *cbb, CBB *out_contents, size_t len_len)-
291{-
292 uint8_t *prefix_bytes;-
293-
294 if (!CBB_flush(cbb))
!CBB_flush(cbb)Description
TRUEevaluated 2 times by 1 test
Evaluated by:
  • bytestringtest
FALSEevaluated 2130 times by 6 tests
Evaluated by:
  • bytestringtest
  • clienttest
  • servertest
  • ssltest
  • tlsexttest
  • tlstest
2-2130
295 return 0;
executed 2 times by 1 test: return 0;
Executed by:
  • bytestringtest
2
296-
297 cbb->offset = cbb->base->len;-
298 if (!cbb_buffer_add(cbb->base, &prefix_bytes, len_len))
!cbb_buffer_ad...ytes, len_len)Description
TRUEnever evaluated
FALSEevaluated 2130 times by 6 tests
Evaluated by:
  • bytestringtest
  • clienttest
  • servertest
  • ssltest
  • tlsexttest
  • tlstest
0-2130
299 return 0;
never executed: return 0;
0
300-
301 memset(prefix_bytes, 0, len_len);-
302 memset(out_contents, 0, sizeof(CBB));-
303 out_contents->base = cbb->base;-
304 cbb->child = out_contents;-
305 cbb->pending_len_len = len_len;-
306 cbb->pending_is_asn1 = 0;-
307-
308 return 1;
executed 2130 times by 6 tests: return 1;
Executed by:
  • bytestringtest
  • clienttest
  • servertest
  • ssltest
  • tlsexttest
  • tlstest
2130
309}-
310-
311int-
312CBB_add_u8_length_prefixed(CBB *cbb, CBB *out_contents)-
313{-
314 return cbb_add_length_prefixed(cbb, out_contents, 1);
executed 452 times by 6 tests: return cbb_add_length_prefixed(cbb, out_contents, 1);
Executed by:
  • bytestringtest
  • clienttest
  • servertest
  • ssltest
  • tlsexttest
  • tlstest
452
315}-
316-
317int-
318CBB_add_u16_length_prefixed(CBB *cbb, CBB *out_contents)-
319{-
320 return cbb_add_length_prefixed(cbb, out_contents, 2);
executed 939 times by 6 tests: return cbb_add_length_prefixed(cbb, out_contents, 2);
Executed by:
  • bytestringtest
  • clienttest
  • servertest
  • ssltest
  • tlsexttest
  • tlstest
939
321}-
322-
323int-
324CBB_add_u24_length_prefixed(CBB *cbb, CBB *out_contents)-
325{-
326 return cbb_add_length_prefixed(cbb, out_contents, 3);
executed 741 times by 5 tests: return cbb_add_length_prefixed(cbb, out_contents, 3);
Executed by:
  • bytestringtest
  • clienttest
  • servertest
  • ssltest
  • tlstest
741
327}-
328-
329int-
330CBB_add_asn1(CBB *cbb, CBB *out_contents, unsigned int tag)-
331{-
332 if (tag > UINT8_MAX)
tag > (255)Description
TRUEnever evaluated
FALSEevaluated 833 times by 3 tests
Evaluated by:
  • asn1test
  • bytestringtest
  • ssltest
0-833
333 return 0;
never executed: return 0;
0
334-
335 /* Long form identifier octets are not supported. */-
336 if ((tag & 0x1f) == 0x1f)
(tag & 0x1f) == 0x1fDescription
TRUEnever evaluated
FALSEevaluated 833 times by 3 tests
Evaluated by:
  • asn1test
  • bytestringtest
  • ssltest
0-833
337 return 0;
never executed: return 0;
0
338-
339 /* Short-form identifier octet only needs a single byte */-
340 if (!CBB_flush(cbb) || !CBB_add_u8(cbb, tag))
!CBB_flush(cbb)Description
TRUEevaluated 1 time by 1 test
Evaluated by:
  • bytestringtest
FALSEevaluated 832 times by 3 tests
Evaluated by:
  • asn1test
  • bytestringtest
  • ssltest
!CBB_add_u8(cbb, tag)Description
TRUEnever evaluated
FALSEevaluated 832 times by 3 tests
Evaluated by:
  • asn1test
  • bytestringtest
  • ssltest
0-832
341 return 0;
executed 1 time by 1 test: return 0;
Executed by:
  • bytestringtest
1
342-
343 /*-
344 * Add 1 byte to cover the short-form length octet case. If it turns-
345 * out we need long-form, it will be extended later.-
346 */-
347 cbb->offset = cbb->base->len;-
348 if (!CBB_add_u8(cbb, 0))
!CBB_add_u8(cbb, 0)Description
TRUEnever evaluated
FALSEevaluated 832 times by 3 tests
Evaluated by:
  • asn1test
  • bytestringtest
  • ssltest
0-832
349 return 0;
never executed: return 0;
0
350-
351 memset(out_contents, 0, sizeof(CBB));-
352 out_contents->base = cbb->base;-
353 cbb->child = out_contents;-
354 cbb->pending_len_len = 1;-
355 cbb->pending_is_asn1 = 1;-
356-
357 return 1;
executed 832 times by 3 tests: return 1;
Executed by:
  • asn1test
  • bytestringtest
  • ssltest
832
358}-
359-
360int-
361CBB_add_bytes(CBB *cbb, const uint8_t *data, size_t len)-
362{-
363 uint8_t *dest;-
364-
365 if (!CBB_add_space(cbb, &dest, len))
!CBB_add_space...b, &dest, len)Description
TRUEevaluated 1 time by 1 test
Evaluated by:
  • bytestringtest
FALSEevaluated 1148 times by 7 tests
Evaluated by:
  • asn1test
  • bytestringtest
  • clienttest
  • servertest
  • ssltest
  • tlsexttest
  • tlstest
1-1148
366 return 0;
executed 1 time by 1 test: return 0;
Executed by:
  • bytestringtest
1
367-
368 memcpy(dest, data, len);-
369 return 1;
executed 1148 times by 7 tests: return 1;
Executed by:
  • asn1test
  • bytestringtest
  • clienttest
  • servertest
  • ssltest
  • tlsexttest
  • tlstest
1148
370}-
371-
372int-
373CBB_add_space(CBB *cbb, uint8_t **out_data, size_t len)-
374{-
375 if (!CBB_flush(cbb) || !cbb_buffer_add(cbb->base, out_data, len))
!CBB_flush(cbb)Description
TRUEevaluated 1 time by 1 test
Evaluated by:
  • bytestringtest
FALSEevaluated 1545 times by 7 tests
Evaluated by:
  • asn1test
  • bytestringtest
  • clienttest
  • servertest
  • ssltest
  • tlsexttest
  • tlstest
!cbb_buffer_ad...out_data, len)Description
TRUEnever evaluated
FALSEevaluated 1545 times by 7 tests
Evaluated by:
  • asn1test
  • bytestringtest
  • clienttest
  • servertest
  • ssltest
  • tlsexttest
  • tlstest
0-1545
376 return 0;
executed 1 time by 1 test: return 0;
Executed by:
  • bytestringtest
1
377-
378 return 1;
executed 1545 times by 7 tests: return 1;
Executed by:
  • asn1test
  • bytestringtest
  • clienttest
  • servertest
  • ssltest
  • tlsexttest
  • tlstest
1545
379}-
380-
381int-
382CBB_add_u8(CBB *cbb, size_t value)-
383{-
384 if (value > UINT8_MAX)
value > (255)Description
TRUEnever evaluated
FALSEevaluated 3570 times by 7 tests
Evaluated by:
  • asn1test
  • bytestringtest
  • clienttest
  • servertest
  • ssltest
  • tlsexttest
  • tlstest
0-3570
385 return 0;
never executed: return 0;
0
386-
387 return cbb_add_u(cbb, (uint32_t)value, 1);
executed 3570 times by 7 tests: return cbb_add_u(cbb, (uint32_t)value, 1);
Executed by:
  • asn1test
  • bytestringtest
  • clienttest
  • servertest
  • ssltest
  • tlsexttest
  • tlstest
3570
388}-
389-
390int-
391CBB_add_u16(CBB *cbb, size_t value)-
392{-
393 if (value > UINT16_MAX)
value > (65535)Description
TRUEnever evaluated
FALSEevaluated 3133 times by 9 tests
Evaluated by:
  • asn1test
  • bytestringtest
  • cipher_list
  • cipherstest
  • clienttest
  • servertest
  • ssltest
  • tlsexttest
  • tlstest
0-3133
394 return 0;
never executed: return 0;
0
395-
396 return cbb_add_u(cbb, (uint32_t)value, 2);
executed 3133 times by 9 tests: return cbb_add_u(cbb, (uint32_t)value, 2);
Executed by:
  • asn1test
  • bytestringtest
  • cipher_list
  • cipherstest
  • clienttest
  • servertest
  • ssltest
  • tlsexttest
  • tlstest
3133
397}-
398-
399int-
400CBB_add_u24(CBB *cbb, size_t value)-
401{-
402 if (value > 0xffffffUL)
value > 0xffffffULDescription
TRUEnever evaluated
FALSEevaluated 925 times by 3 tests
Evaluated by:
  • bytestringtest
  • clienttest
  • ssltest
0-925
403 return 0;
never executed: return 0;
0
404-
405 return cbb_add_u(cbb, (uint32_t)value, 3);
executed 925 times by 3 tests: return cbb_add_u(cbb, (uint32_t)value, 3);
Executed by:
  • bytestringtest
  • clienttest
  • ssltest
925
406}-
407-
408int-
409CBB_add_u32(CBB *cbb, size_t value)-
410{-
411 if (value > 0xffffffffUL)
value > 0xffffffffULDescription
TRUEnever evaluated
FALSEevaluated 60 times by 2 tests
Evaluated by:
  • bytestringtest
  • ssltest
0-60
412 return 0;
never executed: return 0;
0
413-
414 return cbb_add_u(cbb, (uint32_t)value, 4);
executed 60 times by 2 tests: return cbb_add_u(cbb, (uint32_t)value, 4);
Executed by:
  • bytestringtest
  • ssltest
60
415}-
416-
417int-
418CBB_add_asn1_uint64(CBB *cbb, uint64_t value)-
419{-
420 CBB child;-
421 size_t i;-
422 int started = 0;-
423-
424 if (!CBB_add_asn1(cbb, &child, CBS_ASN1_INTEGER))
!CBB_add_asn1(...| 0x00 | 0x2))Description
TRUEnever evaluated
FALSEevaluated 268 times by 3 tests
Evaluated by:
  • asn1test
  • bytestringtest
  • ssltest
0-268
425 return 0;
never executed: return 0;
0
426-
427 for (i = 0; i < 8; i++) {
i < 8Description
TRUEevaluated 2144 times by 3 tests
Evaluated by:
  • asn1test
  • bytestringtest
  • ssltest
FALSEevaluated 268 times by 3 tests
Evaluated by:
  • asn1test
  • bytestringtest
  • ssltest
268-2144
428 uint8_t byte = (value >> 8 * (7 - i)) & 0xff;-
429-
430 /*-
431 * ASN.1 restriction: first 9 bits cannot be all zeroes or-
432 * all ones. Since this function only encodes unsigned-
433 * integers, the only concerns are not encoding leading-
434 * zeros and adding a padding byte if necessary.-
435 *-
436 * In practice, this means:-
437 * 1) Skip leading octets of all zero bits in the value-
438 * 2) After skipping the leading zero octets, if the next 9-
439 * bits are all ones, add an all zero prefix octet (and-
440 * set the high bit of the prefix octet if negative).-
441 *-
442 * Additionally, for an unsigned value, add an all zero-
443 * prefix if the high bit of the first octet would be one.-
444 */-
445 if (!started) {
!startedDescription
TRUEevaluated 1812 times by 3 tests
Evaluated by:
  • asn1test
  • bytestringtest
  • ssltest
FALSEevaluated 332 times by 3 tests
Evaluated by:
  • asn1test
  • bytestringtest
  • ssltest
332-1812
446 if (byte == 0)
byte == 0Description
TRUEevaluated 1545 times by 3 tests
Evaluated by:
  • asn1test
  • bytestringtest
  • ssltest
FALSEevaluated 267 times by 3 tests
Evaluated by:
  • asn1test
  • bytestringtest
  • ssltest
267-1545
447 /* Don't encode leading zeros. */-
448 continue;
executed 1545 times by 3 tests: continue;
Executed by:
  • asn1test
  • bytestringtest
  • ssltest
1545
449-
450 /*-
451 * If the high bit is set, add a padding byte to make it-
452 * unsigned.-
453 */-
454 if ((byte & 0x80) && !CBB_add_u8(&child, 0))
(byte & 0x80)Description
TRUEevaluated 14 times by 2 tests
Evaluated by:
  • bytestringtest
  • ssltest
FALSEevaluated 253 times by 3 tests
Evaluated by:
  • asn1test
  • bytestringtest
  • ssltest
!CBB_add_u8(&child, 0)Description
TRUEnever evaluated
FALSEevaluated 14 times by 2 tests
Evaluated by:
  • bytestringtest
  • ssltest
0-253
455 return 0;
never executed: return 0;
0
456-
457 started = 1;-
458 }
executed 267 times by 3 tests: end of block
Executed by:
  • asn1test
  • bytestringtest
  • ssltest
267
459 if (!CBB_add_u8(&child, byte))
!CBB_add_u8(&child, byte)Description
TRUEnever evaluated
FALSEevaluated 599 times by 3 tests
Evaluated by:
  • asn1test
  • bytestringtest
  • ssltest
0-599
460 return 0;
never executed: return 0;
0
461 }
executed 599 times by 3 tests: end of block
Executed by:
  • asn1test
  • bytestringtest
  • ssltest
599
462-
463 /* 0 is encoded as a single 0, not the empty string. */-
464 if (!started && !CBB_add_u8(&child, 0))
!startedDescription
TRUEevaluated 1 time by 1 test
Evaluated by:
  • bytestringtest
FALSEevaluated 267 times by 3 tests
Evaluated by:
  • asn1test
  • bytestringtest
  • ssltest
!CBB_add_u8(&child, 0)Description
TRUEnever evaluated
FALSEevaluated 1 time by 1 test
Evaluated by:
  • bytestringtest
0-267
465 return 0;
never executed: return 0;
0
466-
467 return CBB_flush(cbb);
executed 268 times by 3 tests: return CBB_flush(cbb);
Executed by:
  • asn1test
  • bytestringtest
  • ssltest
268
468}-
Source codeSwitch to Preprocessed file

Generated by Squish Coco 4.2.2