if (BUF_MEM_grow(pkt->buf, newlen) == 0)
return 0;
}
- if (allocbytes != NULL)
+ if (allocbytes != NULL) {
*allocbytes = WPACKET_get_curr(pkt);
+ if (pkt->endfirst && *allocbytes != NULL)
+ *allocbytes -= len;
+ }
return 1;
}
int WPACKET_sub_reserve_bytes__(WPACKET *pkt, size_t len,
unsigned char **allocbytes, size_t lenbytes)
{
+ if (pkt->endfirst && lenbytes > 0)
+ return 0;
+
if (!WPACKET_reserve_bytes(pkt, lenbytes + len, allocbytes))
return 0;
pkt->staticbuf = buf;
pkt->buf = NULL;
pkt->maxsize = (max < len) ? max : len;
+ pkt->endfirst = 0;
return wpacket_intern_init_len(pkt, lenbytes);
}
+int WPACKET_init_der(WPACKET *pkt, unsigned char *buf, size_t len)
+{
+ /* Internal API, so should not fail */
+ if (!ossl_assert(buf != NULL && len > 0))
+ return 0;
+
+ pkt->staticbuf = buf;
+ pkt->buf = NULL;
+ pkt->maxsize = len;
+ pkt->endfirst = 1;
+
+ return wpacket_intern_init_len(pkt, 0);
+}
+
int WPACKET_init_len(WPACKET *pkt, BUF_MEM *buf, size_t lenbytes)
{
/* Internal API, so should not fail */
pkt->staticbuf = NULL;
pkt->buf = buf;
pkt->maxsize = maxmaxsize(lenbytes);
+ pkt->endfirst = 0;
return wpacket_intern_init_len(pkt, lenbytes);
}
pkt->staticbuf = NULL;
pkt->buf = NULL;
pkt->maxsize = maxmaxsize(lenbytes);
+ pkt->endfirst = 0;
+
+ return wpacket_intern_init_len(pkt, 0);
+}
+
+int WPACKET_init_null_der(WPACKET *pkt)
+{
+ pkt->staticbuf = NULL;
+ pkt->buf = NULL;
+ pkt->maxsize = SIZE_MAX;
+ pkt->endfirst = 1;
return wpacket_intern_init_len(pkt, 0);
}
&& !put_value(&buf[sub->packet_len], packlen,
sub->lenbytes))
return 0;
+ } else if (pkt->endfirst && sub->parent != NULL) {
+ size_t tmplen = packlen;
+ size_t numlenbytes = 1;
+
+ while ((tmplen = tmplen >> 8) > 0)
+ numlenbytes++;
+ if (!WPACKET_put_bytes__(pkt, packlen, numlenbytes))
+ return 0;
+ if (packlen > 0x7f) {
+ numlenbytes |= 0x80;
+ if (!WPACKET_put_bytes_u8(pkt, numlenbytes))
+ return 0;
+ }
}
if (doclose) {
if (!ossl_assert(pkt->subs != NULL))
return 0;
+ /* We don't support lenbytes greater than 0 when doing endfirst writing */
+ if (lenbytes > 0 && pkt->endfirst)
+ return 0;
+
if ((sub = OPENSSL_zalloc(sizeof(*sub))) == NULL) {
SSLerr(SSL_F_WPACKET_START_SUB_PACKET_LEN__, ERR_R_MALLOC_FAILURE);
return 0;
if (buf == NULL)
return NULL;
+ if (pkt->endfirst)
+ return buf + pkt->maxsize - pkt->curr;
+
return buf + pkt->curr;
}
/* Our sub-packets (always at least one if not finished) */
WPACKET_SUB *subs;
+
+ /* Writing from the end first? */
+ unsigned int endfirst : 1;
};
/* Flags */
*/
int WPACKET_init_null(WPACKET *pkt, size_t lenbytes);
+/*
+ * Same as WPACKET_init_null except we set the WPACKET to assume DER length
+ * encoding for sub-packets.
+ */
+int WPACKET_init_null_der(WPACKET *pkt);
+
/*
* Same as WPACKET_init_len except we do not use a growable BUF_MEM structure.
* A fixed buffer of memory |buf| of size |len| is used instead. A failure will
*/
int WPACKET_init_static_len(WPACKET *pkt, unsigned char *buf, size_t len,
size_t lenbytes);
+
+/*
+ * Same as WPACKET_init_static_len except lenbytes is always 0, and we set the
+ * WPACKET to write to the end of the buffer moving towards the start and use
+ * DER length encoding for sub-packets.
+ */
+int WPACKET_init_der(WPACKET *pkt, unsigned char *buf, size_t len);
+
/*
* Set the flags to be applied to the current sub-packet
*/