Skip to content

Commit d3ba391

Browse files
committed
Add "endfirst" writing to WPACKET
Support the concept of writing to the end of the packet first. Reviewed-by: Richard Levitte <[email protected]> (Merged from openssl#11462)
1 parent e2bf331 commit d3ba391

File tree

2 files changed

+71
-1
lines changed

2 files changed

+71
-1
lines changed

crypto/packet.c

+54-1
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,21 @@ int WPACKET_reserve_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes)
6565
if (BUF_MEM_grow(pkt->buf, newlen) == 0)
6666
return 0;
6767
}
68-
if (allocbytes != NULL)
68+
if (allocbytes != NULL) {
6969
*allocbytes = WPACKET_get_curr(pkt);
70+
if (pkt->endfirst && *allocbytes != NULL)
71+
*allocbytes -= len;
72+
}
7073

7174
return 1;
7275
}
7376

7477
int WPACKET_sub_reserve_bytes__(WPACKET *pkt, size_t len,
7578
unsigned char **allocbytes, size_t lenbytes)
7679
{
80+
if (pkt->endfirst && lenbytes > 0)
81+
return 0;
82+
7783
if (!WPACKET_reserve_bytes(pkt, lenbytes + len, allocbytes))
7884
return 0;
7985

@@ -131,10 +137,25 @@ int WPACKET_init_static_len(WPACKET *pkt, unsigned char *buf, size_t len,
131137
pkt->staticbuf = buf;
132138
pkt->buf = NULL;
133139
pkt->maxsize = (max < len) ? max : len;
140+
pkt->endfirst = 0;
134141

135142
return wpacket_intern_init_len(pkt, lenbytes);
136143
}
137144

145+
int WPACKET_init_der(WPACKET *pkt, unsigned char *buf, size_t len)
146+
{
147+
/* Internal API, so should not fail */
148+
if (!ossl_assert(buf != NULL && len > 0))
149+
return 0;
150+
151+
pkt->staticbuf = buf;
152+
pkt->buf = NULL;
153+
pkt->maxsize = len;
154+
pkt->endfirst = 1;
155+
156+
return wpacket_intern_init_len(pkt, 0);
157+
}
158+
138159
int WPACKET_init_len(WPACKET *pkt, BUF_MEM *buf, size_t lenbytes)
139160
{
140161
/* Internal API, so should not fail */
@@ -144,6 +165,7 @@ int WPACKET_init_len(WPACKET *pkt, BUF_MEM *buf, size_t lenbytes)
144165
pkt->staticbuf = NULL;
145166
pkt->buf = buf;
146167
pkt->maxsize = maxmaxsize(lenbytes);
168+
pkt->endfirst = 0;
147169

148170
return wpacket_intern_init_len(pkt, lenbytes);
149171
}
@@ -158,6 +180,17 @@ int WPACKET_init_null(WPACKET *pkt, size_t lenbytes)
158180
pkt->staticbuf = NULL;
159181
pkt->buf = NULL;
160182
pkt->maxsize = maxmaxsize(lenbytes);
183+
pkt->endfirst = 0;
184+
185+
return wpacket_intern_init_len(pkt, 0);
186+
}
187+
188+
int WPACKET_init_null_der(WPACKET *pkt)
189+
{
190+
pkt->staticbuf = NULL;
191+
pkt->buf = NULL;
192+
pkt->maxsize = SIZE_MAX;
193+
pkt->endfirst = 1;
161194

162195
return wpacket_intern_init_len(pkt, 0);
163196
}
@@ -232,6 +265,19 @@ static int wpacket_intern_close(WPACKET *pkt, WPACKET_SUB *sub, int doclose)
232265
&& !put_value(&buf[sub->packet_len], packlen,
233266
sub->lenbytes))
234267
return 0;
268+
} else if (pkt->endfirst && sub->parent != NULL) {
269+
size_t tmplen = packlen;
270+
size_t numlenbytes = 1;
271+
272+
while ((tmplen = tmplen >> 8) > 0)
273+
numlenbytes++;
274+
if (!WPACKET_put_bytes__(pkt, packlen, numlenbytes))
275+
return 0;
276+
if (packlen > 0x7f) {
277+
numlenbytes |= 0x80;
278+
if (!WPACKET_put_bytes_u8(pkt, numlenbytes))
279+
return 0;
280+
}
235281
}
236282

237283
if (doclose) {
@@ -298,6 +344,10 @@ int WPACKET_start_sub_packet_len__(WPACKET *pkt, size_t lenbytes)
298344
if (!ossl_assert(pkt->subs != NULL))
299345
return 0;
300346

347+
/* We don't support lenbytes greater than 0 when doing endfirst writing */
348+
if (lenbytes > 0 && pkt->endfirst)
349+
return 0;
350+
301351
if ((sub = OPENSSL_zalloc(sizeof(*sub))) == NULL) {
302352
SSLerr(SSL_F_WPACKET_START_SUB_PACKET_LEN__, ERR_R_MALLOC_FAILURE);
303353
return 0;
@@ -436,6 +486,9 @@ unsigned char *WPACKET_get_curr(WPACKET *pkt)
436486
if (buf == NULL)
437487
return NULL;
438488

489+
if (pkt->endfirst)
490+
return buf + pkt->maxsize - pkt->curr;
491+
439492
return buf + pkt->curr;
440493
}
441494

include/internal/packet.h

+17
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,9 @@ struct wpacket_st {
638638

639639
/* Our sub-packets (always at least one if not finished) */
640640
WPACKET_SUB *subs;
641+
642+
/* Writing from the end first? */
643+
unsigned int endfirst : 1;
641644
};
642645

643646
/* Flags */
@@ -676,13 +679,27 @@ int WPACKET_init(WPACKET *pkt, BUF_MEM *buf);
676679
*/
677680
int WPACKET_init_null(WPACKET *pkt, size_t lenbytes);
678681

682+
/*
683+
* Same as WPACKET_init_null except we set the WPACKET to assume DER length
684+
* encoding for sub-packets.
685+
*/
686+
int WPACKET_init_null_der(WPACKET *pkt);
687+
679688
/*
680689
* Same as WPACKET_init_len except we do not use a growable BUF_MEM structure.
681690
* A fixed buffer of memory |buf| of size |len| is used instead. A failure will
682691
* occur if you attempt to write beyond the end of the buffer
683692
*/
684693
int WPACKET_init_static_len(WPACKET *pkt, unsigned char *buf, size_t len,
685694
size_t lenbytes);
695+
696+
/*
697+
* Same as WPACKET_init_static_len except lenbytes is always 0, and we set the
698+
* WPACKET to write to the end of the buffer moving towards the start and use
699+
* DER length encoding for sub-packets.
700+
*/
701+
int WPACKET_init_der(WPACKET *pkt, unsigned char *buf, size_t len);
702+
686703
/*
687704
* Set the flags to be applied to the current sub-packet
688705
*/

0 commit comments

Comments
 (0)