| 
									
										
										
										
											2016-08-04 00:06:39 +08:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2018-04-17 21:18:40 +08:00
										 |  |  |  * Copyright 2015-2018 The OpenSSL Project Authors. All Rights Reserved. | 
					
						
							| 
									
										
										
										
											2016-08-04 00:06:39 +08:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2018-12-06 20:08:51 +08:00
										 |  |  |  * Licensed under the Apache License 2.0 (the "License").  You may not use | 
					
						
							| 
									
										
										
										
											2016-08-04 00:06:39 +08:00
										 |  |  |  * this file except in compliance with the License.  You can obtain a copy | 
					
						
							|  |  |  |  * in the file LICENSE in the source distribution or at | 
					
						
							|  |  |  |  * https://www.openssl.org/source/license.html
 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-02 21:46:31 +08:00
										 |  |  | #include "internal/cryptlib.h"
 | 
					
						
							| 
									
										
										
										
											2019-06-07 23:32:49 +08:00
										 |  |  | #include "internal/packet.h"
 | 
					
						
							| 
									
										
										
										
											2018-04-03 23:31:16 +08:00
										 |  |  | #include <openssl/sslerr.h>
 | 
					
						
							| 
									
										
										
										
											2016-08-04 00:06:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-08 16:58:29 +08:00
										 |  |  | #define DEFAULT_BUF_SIZE    256
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-06 22:09:51 +08:00
										 |  |  | int WPACKET_allocate_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes) | 
					
						
							| 
									
										
										
										
											2016-09-29 18:43:37 +08:00
										 |  |  | { | 
					
						
							|  |  |  |     if (!WPACKET_reserve_bytes(pkt, len, allocbytes)) | 
					
						
							|  |  |  |         return 0; | 
					
						
							| 
									
										
										
										
											2016-09-29 21:39:47 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-29 18:43:37 +08:00
										 |  |  |     pkt->written += len; | 
					
						
							|  |  |  |     pkt->curr += len; | 
					
						
							|  |  |  |     return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int WPACKET_sub_allocate_bytes__(WPACKET *pkt, size_t len, | 
					
						
							|  |  |  |                                  unsigned char **allocbytes, size_t lenbytes) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (!WPACKET_start_sub_packet_len__(pkt, lenbytes) | 
					
						
							|  |  |  |             || !WPACKET_allocate_bytes(pkt, len, allocbytes) | 
					
						
							|  |  |  |             || !WPACKET_close(pkt)) | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-08 18:33:35 +08:00
										 |  |  | #define GETBUF(p)   (((p)->staticbuf != NULL) \
 | 
					
						
							| 
									
										
										
										
											2019-06-11 00:48:26 +08:00
										 |  |  |                      ? (p)->staticbuf \ | 
					
						
							|  |  |  |                      : ((p)->buf != NULL \ | 
					
						
							|  |  |  |                         ? (unsigned char *)(p)->buf->data \ | 
					
						
							|  |  |  |                         : NULL)) | 
					
						
							| 
									
										
										
										
											2016-11-08 18:33:35 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-29 18:43:37 +08:00
										 |  |  | int WPACKET_reserve_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes) | 
					
						
							| 
									
										
										
										
											2016-08-04 00:06:39 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-09-09 16:49:16 +08:00
										 |  |  |     /* Internal API, so should not fail */ | 
					
						
							| 
									
										
										
										
											2017-05-22 19:33:42 +08:00
										 |  |  |     if (!ossl_assert(pkt->subs != NULL && len != 0)) | 
					
						
							| 
									
										
										
										
											2016-09-06 22:09:51 +08:00
										 |  |  |         return 0; | 
					
						
							| 
									
										
										
										
											2016-08-04 00:06:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-09 06:08:53 +08:00
										 |  |  |     if (pkt->maxsize - pkt->written < len) | 
					
						
							| 
									
										
										
										
											2016-08-04 00:06:39 +08:00
										 |  |  |         return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-11 00:48:26 +08:00
										 |  |  |     if (pkt->buf != NULL && (pkt->buf->length - pkt->written < len)) { | 
					
						
							| 
									
										
										
										
											2016-08-04 00:06:39 +08:00
										 |  |  |         size_t newlen; | 
					
						
							| 
									
										
										
										
											2016-09-23 23:41:50 +08:00
										 |  |  |         size_t reflen; | 
					
						
							| 
									
										
										
										
											2016-08-04 00:06:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-23 23:41:50 +08:00
										 |  |  |         reflen = (len > pkt->buf->length) ? len : pkt->buf->length; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (reflen > SIZE_MAX / 2) { | 
					
						
							| 
									
										
										
										
											2016-08-04 00:06:39 +08:00
										 |  |  |             newlen = SIZE_MAX; | 
					
						
							| 
									
										
										
										
											2016-09-08 16:58:29 +08:00
										 |  |  |         } else { | 
					
						
							| 
									
										
										
										
											2016-09-23 23:41:50 +08:00
										 |  |  |             newlen = reflen * 2; | 
					
						
							|  |  |  |             if (newlen < DEFAULT_BUF_SIZE) | 
					
						
							|  |  |  |                 newlen = DEFAULT_BUF_SIZE; | 
					
						
							| 
									
										
										
										
											2016-09-08 16:58:29 +08:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2016-09-06 22:09:51 +08:00
										 |  |  |         if (BUF_MEM_grow(pkt->buf, newlen) == 0) | 
					
						
							|  |  |  |             return 0; | 
					
						
							| 
									
										
										
										
											2016-08-04 00:06:39 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-11-19 00:34:01 +08:00
										 |  |  |     if (allocbytes != NULL) | 
					
						
							| 
									
										
										
										
											2016-12-02 19:10:16 +08:00
										 |  |  |         *allocbytes = WPACKET_get_curr(pkt); | 
					
						
							| 
									
										
										
										
											2016-08-04 00:06:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-06 22:09:51 +08:00
										 |  |  |     return 1; | 
					
						
							| 
									
										
										
										
											2016-08-04 00:06:39 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-29 18:43:37 +08:00
										 |  |  | int WPACKET_sub_reserve_bytes__(WPACKET *pkt, size_t len, | 
					
						
							|  |  |  |                                 unsigned char **allocbytes, size_t lenbytes) | 
					
						
							| 
									
										
										
										
											2016-09-13 18:32:52 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-09-29 18:43:37 +08:00
										 |  |  |     if (!WPACKET_reserve_bytes(pkt, lenbytes + len, allocbytes)) | 
					
						
							| 
									
										
										
										
											2016-09-13 18:32:52 +08:00
										 |  |  |         return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-11 00:48:26 +08:00
										 |  |  |     if (*allocbytes != NULL) | 
					
						
							|  |  |  |         *allocbytes += lenbytes; | 
					
						
							| 
									
										
										
										
											2016-09-29 18:43:37 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-13 18:32:52 +08:00
										 |  |  |     return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-08 16:58:29 +08:00
										 |  |  | static size_t maxmaxsize(size_t lenbytes) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (lenbytes >= sizeof(size_t) || lenbytes == 0) | 
					
						
							|  |  |  |         return SIZE_MAX; | 
					
						
							| 
									
										
										
										
											2016-09-12 16:41:01 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return ((size_t)1 << (lenbytes * 8)) - 1 + lenbytes; | 
					
						
							| 
									
										
										
										
											2016-09-08 16:58:29 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-08 18:33:35 +08:00
										 |  |  | static int wpacket_intern_init_len(WPACKET *pkt, size_t lenbytes) | 
					
						
							| 
									
										
										
										
											2016-08-04 00:06:39 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-09-09 07:13:41 +08:00
										 |  |  |     unsigned char *lenchars; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     pkt->curr = 0; | 
					
						
							| 
									
										
										
										
											2016-09-06 22:09:51 +08:00
										 |  |  |     pkt->written = 0; | 
					
						
							| 
									
										
										
										
											2016-08-04 00:06:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-03 23:31:16 +08:00
										 |  |  |     if ((pkt->subs = OPENSSL_zalloc(sizeof(*pkt->subs))) == NULL) { | 
					
						
							|  |  |  |         SSLerr(SSL_F_WPACKET_INTERN_INIT_LEN, ERR_R_MALLOC_FAILURE); | 
					
						
							| 
									
										
										
										
											2016-09-06 22:09:51 +08:00
										 |  |  |         return 0; | 
					
						
							| 
									
										
										
										
											2018-04-03 23:31:16 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-08-04 00:06:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-06 22:09:51 +08:00
										 |  |  |     if (lenbytes == 0) | 
					
						
							| 
									
										
										
										
											2016-08-04 00:06:39 +08:00
										 |  |  |         return 1; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-06 22:09:51 +08:00
										 |  |  |     pkt->subs->pwritten = lenbytes; | 
					
						
							|  |  |  |     pkt->subs->lenbytes = lenbytes; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-09 07:13:41 +08:00
										 |  |  |     if (!WPACKET_allocate_bytes(pkt, lenbytes, &lenchars)) { | 
					
						
							| 
									
										
										
										
											2016-09-06 22:09:51 +08:00
										 |  |  |         OPENSSL_free(pkt->subs); | 
					
						
							|  |  |  |         pkt->subs = NULL; | 
					
						
							| 
									
										
										
										
											2016-08-04 00:06:39 +08:00
										 |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-06-11 00:48:26 +08:00
										 |  |  |     pkt->subs->packet_len = 0; | 
					
						
							| 
									
										
										
										
											2016-08-04 00:06:39 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-08 18:33:35 +08:00
										 |  |  | int WPACKET_init_static_len(WPACKET *pkt, unsigned char *buf, size_t len, | 
					
						
							|  |  |  |                             size_t lenbytes) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     size_t max = maxmaxsize(lenbytes); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* Internal API, so should not fail */ | 
					
						
							| 
									
										
										
										
											2017-05-22 19:33:42 +08:00
										 |  |  |     if (!ossl_assert(buf != NULL && len > 0)) | 
					
						
							| 
									
										
										
										
											2016-11-08 18:33:35 +08:00
										 |  |  |         return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     pkt->staticbuf = buf; | 
					
						
							|  |  |  |     pkt->buf = NULL; | 
					
						
							|  |  |  |     pkt->maxsize = (max < len) ? max : len; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return wpacket_intern_init_len(pkt, lenbytes); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int WPACKET_init_len(WPACKET *pkt, BUF_MEM *buf, size_t lenbytes) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     /* Internal API, so should not fail */ | 
					
						
							| 
									
										
										
										
											2017-05-22 19:33:42 +08:00
										 |  |  |     if (!ossl_assert(buf != NULL)) | 
					
						
							| 
									
										
										
										
											2016-11-08 18:33:35 +08:00
										 |  |  |         return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     pkt->staticbuf = NULL; | 
					
						
							|  |  |  |     pkt->buf = buf; | 
					
						
							|  |  |  |     pkt->maxsize = maxmaxsize(lenbytes); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return wpacket_intern_init_len(pkt, lenbytes); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-06 00:34:04 +08:00
										 |  |  | int WPACKET_init(WPACKET *pkt, BUF_MEM *buf) | 
					
						
							| 
									
										
										
										
											2016-08-04 00:06:39 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-09-06 00:34:04 +08:00
										 |  |  |     return WPACKET_init_len(pkt, buf, 0); | 
					
						
							| 
									
										
										
										
											2016-08-04 00:06:39 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-11 00:48:26 +08:00
										 |  |  | int WPACKET_init_null(WPACKET *pkt, size_t lenbytes) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     pkt->staticbuf = NULL; | 
					
						
							|  |  |  |     pkt->buf = NULL; | 
					
						
							|  |  |  |     pkt->maxsize = maxmaxsize(lenbytes); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return wpacket_intern_init_len(pkt, 0); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-06 00:34:04 +08:00
										 |  |  | int WPACKET_set_flags(WPACKET *pkt, unsigned int flags) | 
					
						
							| 
									
										
										
										
											2016-08-04 00:06:39 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-09-09 16:49:16 +08:00
										 |  |  |     /* Internal API, so should not fail */ | 
					
						
							| 
									
										
										
										
											2017-05-22 19:33:42 +08:00
										 |  |  |     if (!ossl_assert(pkt->subs != NULL)) | 
					
						
							| 
									
										
										
										
											2016-09-06 22:09:51 +08:00
										 |  |  |         return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     pkt->subs->flags = flags; | 
					
						
							| 
									
										
										
										
											2016-08-04 00:06:39 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-13 16:40:38 +08:00
										 |  |  | /* Store the |value| of length |len| at location |data| */ | 
					
						
							|  |  |  | static int put_value(unsigned char *data, size_t value, size_t len) | 
					
						
							| 
									
										
										
										
											2016-09-12 16:39:10 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-06-11 00:48:26 +08:00
										 |  |  |     if (data == NULL) | 
					
						
							|  |  |  |         return 1; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-13 16:40:38 +08:00
										 |  |  |     for (data += len - 1; len > 0; len--) { | 
					
						
							| 
									
										
										
										
											2016-09-12 16:39:10 +08:00
										 |  |  |         *data = (unsigned char)(value & 0xff); | 
					
						
							|  |  |  |         data--; | 
					
						
							|  |  |  |         value >>= 8; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* Check whether we could fit the value in the assigned number of bytes */ | 
					
						
							|  |  |  |     if (value > 0) | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-06 22:09:51 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-04 00:06:39 +08:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2017-01-14 00:59:18 +08:00
										 |  |  |  * Internal helper function used by WPACKET_close(), WPACKET_finish() and | 
					
						
							|  |  |  |  * WPACKET_fill_lengths() to close a sub-packet and write out its length if | 
					
						
							|  |  |  |  * necessary. If |doclose| is 0 then it goes through the motions of closing | 
					
						
							|  |  |  |  * (i.e. it fills in all the lengths), but doesn't actually close anything. | 
					
						
							| 
									
										
										
										
											2016-08-04 00:06:39 +08:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2017-01-14 00:59:18 +08:00
										 |  |  | static int wpacket_intern_close(WPACKET *pkt, WPACKET_SUB *sub, int doclose) | 
					
						
							| 
									
										
										
										
											2016-08-04 00:06:39 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-09-09 07:13:41 +08:00
										 |  |  |     size_t packlen = pkt->written - sub->pwritten; | 
					
						
							| 
									
										
										
										
											2016-08-04 00:06:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-06 22:09:51 +08:00
										 |  |  |     if (packlen == 0 | 
					
						
							| 
									
										
										
										
											2016-09-13 16:40:38 +08:00
										 |  |  |             && (sub->flags & WPACKET_FLAGS_NON_ZERO_LENGTH) != 0) | 
					
						
							| 
									
										
										
										
											2016-08-04 00:06:39 +08:00
										 |  |  |         return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (packlen == 0 | 
					
						
							| 
									
										
										
										
											2016-09-09 07:13:41 +08:00
										 |  |  |             && sub->flags & WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH) { | 
					
						
							| 
									
										
										
										
											2017-01-14 00:59:18 +08:00
										 |  |  |         /* We can't handle this case. Return an error */ | 
					
						
							|  |  |  |         if (!doclose) | 
					
						
							|  |  |  |             return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-06 00:34:04 +08:00
										 |  |  |         /* Deallocate any bytes allocated for the length of the WPACKET */ | 
					
						
							| 
									
										
										
										
											2016-09-06 22:09:51 +08:00
										 |  |  |         if ((pkt->curr - sub->lenbytes) == sub->packet_len) { | 
					
						
							|  |  |  |             pkt->written -= sub->lenbytes; | 
					
						
							|  |  |  |             pkt->curr -= sub->lenbytes; | 
					
						
							| 
									
										
										
										
											2016-08-04 00:06:39 +08:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         /* Don't write out the packet length */ | 
					
						
							| 
									
										
										
										
											2016-09-09 07:13:41 +08:00
										 |  |  |         sub->packet_len = 0; | 
					
						
							|  |  |  |         sub->lenbytes = 0; | 
					
						
							| 
									
										
										
										
											2016-08-04 00:06:39 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-06 00:34:04 +08:00
										 |  |  |     /* Write out the WPACKET length if needed */ | 
					
						
							| 
									
										
										
										
											2019-06-11 00:48:26 +08:00
										 |  |  |     if (sub->lenbytes > 0) { | 
					
						
							|  |  |  |         unsigned char *buf = GETBUF(pkt); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (buf != NULL | 
					
						
							|  |  |  |                 && !put_value(&buf[sub->packet_len], packlen, | 
					
						
							| 
									
										
										
										
											2016-11-08 18:33:35 +08:00
										 |  |  |                               sub->lenbytes)) | 
					
						
							| 
									
										
										
										
											2016-08-04 00:06:39 +08:00
										 |  |  |             return 0; | 
					
						
							| 
									
										
										
										
											2019-06-11 00:48:26 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-08-04 00:06:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-14 00:59:18 +08:00
										 |  |  |     if (doclose) { | 
					
						
							|  |  |  |         pkt->subs = sub->parent; | 
					
						
							|  |  |  |         OPENSSL_free(sub); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int WPACKET_fill_lengths(WPACKET *pkt) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     WPACKET_SUB *sub; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-22 19:33:42 +08:00
										 |  |  |     if (!ossl_assert(pkt->subs != NULL)) | 
					
						
							| 
									
										
										
										
											2017-01-14 00:59:18 +08:00
										 |  |  |         return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-27 20:05:52 +08:00
										 |  |  |     for (sub = pkt->subs; sub != NULL; sub = sub->parent) { | 
					
						
							| 
									
										
										
										
											2017-01-14 00:59:18 +08:00
										 |  |  |         if (!wpacket_intern_close(pkt, sub, 0)) | 
					
						
							|  |  |  |             return 0; | 
					
						
							| 
									
										
										
										
											2017-01-27 20:05:52 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-08-04 00:06:39 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-06 22:09:51 +08:00
										 |  |  | int WPACKET_close(WPACKET *pkt) | 
					
						
							| 
									
										
										
										
											2016-08-04 00:06:39 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-09-09 16:49:16 +08:00
										 |  |  |     /*
 | 
					
						
							|  |  |  |      * Internal API, so should not fail - but we do negative testing of this | 
					
						
							|  |  |  |      * so no assert (otherwise the tests fail) | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-09-06 22:09:51 +08:00
										 |  |  |     if (pkt->subs == NULL || pkt->subs->parent == NULL) | 
					
						
							| 
									
										
										
										
											2016-08-04 00:06:39 +08:00
										 |  |  |         return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-14 00:59:18 +08:00
										 |  |  |     return wpacket_intern_close(pkt, pkt->subs, 1); | 
					
						
							| 
									
										
										
										
											2016-08-04 00:06:39 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-06 22:09:51 +08:00
										 |  |  | int WPACKET_finish(WPACKET *pkt) | 
					
						
							| 
									
										
										
										
											2016-08-04 00:06:39 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-09-06 22:09:51 +08:00
										 |  |  |     int ret; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-09 16:49:16 +08:00
										 |  |  |     /*
 | 
					
						
							|  |  |  |      * Internal API, so should not fail - but we do negative testing of this | 
					
						
							|  |  |  |      * so no assert (otherwise the tests fail) | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-09-06 22:09:51 +08:00
										 |  |  |     if (pkt->subs == NULL || pkt->subs->parent != NULL) | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-14 00:59:18 +08:00
										 |  |  |     ret = wpacket_intern_close(pkt, pkt->subs, 1); | 
					
						
							| 
									
										
										
										
											2016-09-08 16:58:29 +08:00
										 |  |  |     if (ret) { | 
					
						
							|  |  |  |         OPENSSL_free(pkt->subs); | 
					
						
							|  |  |  |         pkt->subs = NULL; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-09-09 07:13:41 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-06 22:09:51 +08:00
										 |  |  |     return ret; | 
					
						
							| 
									
										
										
										
											2016-08-04 00:06:39 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-13 22:42:12 +08:00
										 |  |  | int WPACKET_start_sub_packet_len__(WPACKET *pkt, size_t lenbytes) | 
					
						
							| 
									
										
										
										
											2016-08-04 00:06:39 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-09-06 22:09:51 +08:00
										 |  |  |     WPACKET_SUB *sub; | 
					
						
							| 
									
										
										
										
											2016-09-09 07:13:41 +08:00
										 |  |  |     unsigned char *lenchars; | 
					
						
							| 
									
										
										
										
											2016-08-04 00:06:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-09 16:49:16 +08:00
										 |  |  |     /* Internal API, so should not fail */ | 
					
						
							| 
									
										
										
										
											2017-05-22 19:33:42 +08:00
										 |  |  |     if (!ossl_assert(pkt->subs != NULL)) | 
					
						
							| 
									
										
										
										
											2016-08-04 00:06:39 +08:00
										 |  |  |         return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-03 23:31:16 +08:00
										 |  |  |     if ((sub = OPENSSL_zalloc(sizeof(*sub))) == NULL) { | 
					
						
							|  |  |  |         SSLerr(SSL_F_WPACKET_START_SUB_PACKET_LEN__, ERR_R_MALLOC_FAILURE); | 
					
						
							| 
									
										
										
										
											2016-08-04 00:06:39 +08:00
										 |  |  |         return 0; | 
					
						
							| 
									
										
										
										
											2018-04-03 23:31:16 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-08-04 00:06:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-06 22:09:51 +08:00
										 |  |  |     sub->parent = pkt->subs; | 
					
						
							|  |  |  |     pkt->subs = sub; | 
					
						
							|  |  |  |     sub->pwritten = pkt->written + lenbytes; | 
					
						
							|  |  |  |     sub->lenbytes = lenbytes; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (lenbytes == 0) { | 
					
						
							| 
									
										
										
										
											2016-09-09 07:13:41 +08:00
										 |  |  |         sub->packet_len = 0; | 
					
						
							| 
									
										
										
										
											2016-09-06 22:09:51 +08:00
										 |  |  |         return 1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-11 00:48:26 +08:00
										 |  |  |     sub->packet_len = pkt->written; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-09 07:13:41 +08:00
										 |  |  |     if (!WPACKET_allocate_bytes(pkt, lenbytes, &lenchars)) | 
					
						
							| 
									
										
										
										
											2016-09-06 22:09:51 +08:00
										 |  |  |         return 0; | 
					
						
							| 
									
										
										
										
											2016-08-04 00:06:39 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-06 22:09:51 +08:00
										 |  |  | int WPACKET_start_sub_packet(WPACKET *pkt) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2016-09-13 22:42:12 +08:00
										 |  |  |     return WPACKET_start_sub_packet_len__(pkt, 0); | 
					
						
							| 
									
										
										
										
											2016-09-06 22:09:51 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-20 21:47:44 +08:00
										 |  |  | int WPACKET_put_bytes__(WPACKET *pkt, unsigned int val, size_t size) | 
					
						
							| 
									
										
										
										
											2016-08-04 00:06:39 +08:00
										 |  |  | { | 
					
						
							|  |  |  |     unsigned char *data; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-09 16:49:16 +08:00
										 |  |  |     /* Internal API, so should not fail */ | 
					
						
							| 
									
										
										
										
											2017-05-22 19:33:42 +08:00
										 |  |  |     if (!ossl_assert(size <= sizeof(unsigned int)) | 
					
						
							| 
									
										
										
										
											2016-09-12 16:39:10 +08:00
										 |  |  |             || !WPACKET_allocate_bytes(pkt, size, &data) | 
					
						
							|  |  |  |             || !put_value(data, val, size)) | 
					
						
							| 
									
										
										
										
											2016-08-04 00:06:39 +08:00
										 |  |  |         return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-06 00:34:04 +08:00
										 |  |  | int WPACKET_set_max_size(WPACKET *pkt, size_t maxsize) | 
					
						
							| 
									
										
										
										
											2016-08-04 00:06:39 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-09-08 16:58:29 +08:00
										 |  |  |     WPACKET_SUB *sub; | 
					
						
							|  |  |  |     size_t lenbytes; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-09 16:49:16 +08:00
										 |  |  |     /* Internal API, so should not fail */ | 
					
						
							| 
									
										
										
										
											2017-05-22 19:33:42 +08:00
										 |  |  |     if (!ossl_assert(pkt->subs != NULL)) | 
					
						
							| 
									
										
										
										
											2016-09-08 16:58:29 +08:00
										 |  |  |         return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* Find the WPACKET_SUB for the top level */ | 
					
						
							| 
									
										
										
										
											2016-09-09 07:13:41 +08:00
										 |  |  |     for (sub = pkt->subs; sub->parent != NULL; sub = sub->parent) | 
					
						
							|  |  |  |         continue; | 
					
						
							| 
									
										
										
										
											2016-09-08 16:58:29 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     lenbytes = sub->lenbytes; | 
					
						
							|  |  |  |     if (lenbytes == 0) | 
					
						
							|  |  |  |         lenbytes = sizeof(pkt->maxsize); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (maxmaxsize(lenbytes) < maxsize || maxsize < pkt->written) | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-06 22:09:51 +08:00
										 |  |  |     pkt->maxsize = maxsize; | 
					
						
							| 
									
										
										
										
											2016-08-04 00:06:39 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-06 00:35:25 +08:00
										 |  |  | int WPACKET_memset(WPACKET *pkt, int ch, size_t len) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     unsigned char *dest; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (len == 0) | 
					
						
							|  |  |  |         return 1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!WPACKET_allocate_bytes(pkt, len, &dest)) | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-11 00:48:26 +08:00
										 |  |  |     if (dest != NULL) | 
					
						
							|  |  |  |         memset(dest, ch, len); | 
					
						
							| 
									
										
										
										
											2017-04-06 00:35:25 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-06 00:34:04 +08:00
										 |  |  | int WPACKET_memcpy(WPACKET *pkt, const void *src, size_t len) | 
					
						
							| 
									
										
										
										
											2016-08-04 00:06:39 +08:00
										 |  |  | { | 
					
						
							|  |  |  |     unsigned char *dest; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (len == 0) | 
					
						
							|  |  |  |         return 1; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-06 00:34:04 +08:00
										 |  |  |     if (!WPACKET_allocate_bytes(pkt, len, &dest)) | 
					
						
							| 
									
										
										
										
											2016-08-04 00:06:39 +08:00
										 |  |  |         return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-11 00:48:26 +08:00
										 |  |  |     if (dest != NULL) | 
					
						
							|  |  |  |         memcpy(dest, src, len); | 
					
						
							| 
									
										
										
										
											2016-08-04 00:06:39 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-13 22:42:12 +08:00
										 |  |  | int WPACKET_sub_memcpy__(WPACKET *pkt, const void *src, size_t len, | 
					
						
							|  |  |  |                          size_t lenbytes) | 
					
						
							| 
									
										
										
										
											2016-09-06 22:19:32 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-09-13 22:42:12 +08:00
										 |  |  |     if (!WPACKET_start_sub_packet_len__(pkt, lenbytes) | 
					
						
							| 
									
										
										
										
											2016-09-06 22:19:32 +08:00
										 |  |  |             || !WPACKET_memcpy(pkt, src, len) | 
					
						
							|  |  |  |             || !WPACKET_close(pkt)) | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-06 00:34:04 +08:00
										 |  |  | int WPACKET_get_total_written(WPACKET *pkt, size_t *written) | 
					
						
							| 
									
										
										
										
											2016-08-04 00:06:39 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-09-09 16:49:16 +08:00
										 |  |  |     /* Internal API, so should not fail */ | 
					
						
							| 
									
										
										
										
											2017-05-22 19:33:42 +08:00
										 |  |  |     if (!ossl_assert(written != NULL)) | 
					
						
							| 
									
										
										
										
											2016-08-04 00:06:39 +08:00
										 |  |  |         return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-06 22:09:51 +08:00
										 |  |  |     *written = pkt->written; | 
					
						
							| 
									
										
										
										
											2016-08-04 00:06:39 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-06 00:34:04 +08:00
										 |  |  | int WPACKET_get_length(WPACKET *pkt, size_t *len) | 
					
						
							| 
									
										
										
										
											2016-08-04 00:06:39 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-09-09 16:49:16 +08:00
										 |  |  |     /* Internal API, so should not fail */ | 
					
						
							| 
									
										
										
										
											2017-05-22 19:33:42 +08:00
										 |  |  |     if (!ossl_assert(pkt->subs != NULL && len != NULL)) | 
					
						
							| 
									
										
										
										
											2016-08-04 00:06:39 +08:00
										 |  |  |         return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-06 22:09:51 +08:00
										 |  |  |     *len = pkt->written - pkt->subs->pwritten; | 
					
						
							| 
									
										
										
										
											2016-08-04 00:06:39 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return 1; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2016-09-08 16:58:29 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-19 00:34:01 +08:00
										 |  |  | unsigned char *WPACKET_get_curr(WPACKET *pkt) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2019-06-11 00:48:26 +08:00
										 |  |  |     unsigned char *buf = GETBUF(pkt); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (buf == NULL) | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return buf + pkt->curr; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int WPACKET_is_null_buf(WPACKET *pkt) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return pkt->buf == NULL && pkt->staticbuf == NULL; | 
					
						
							| 
									
										
										
										
											2016-11-19 00:34:01 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-08 16:58:29 +08:00
										 |  |  | void WPACKET_cleanup(WPACKET *pkt) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     WPACKET_SUB *sub, *parent; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for (sub = pkt->subs; sub != NULL; sub = parent) { | 
					
						
							|  |  |  |         parent = sub->parent; | 
					
						
							|  |  |  |         OPENSSL_free(sub); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     pkt->subs = NULL; | 
					
						
							|  |  |  | } |