1 /* $OpenBSD: tls13_handshake_msg.c,v 1.2 2019/11/20 16:21:20 beck Exp $ */ 2 /* 3 * Copyright (c) 2018, 2019 Joel Sing <jsing@openbsd.org> 4 * 5 * Permission to use, copy, modify, and 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 12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 */ 17 18 #include "bytestring.h" 19 #include "ssl_locl.h" 20 #include "tls13_internal.h" 21 22 #define TLS13_HANDSHAKE_MSG_HEADER_LEN 4 23 #define TLS13_HANDSHAKE_MSG_INITIAL_LEN 256 24 #define TLS13_HANDSHAKE_MSG_MAX_LEN (256 * 1024) 25 26 struct tls13_handshake_msg { 27 uint8_t msg_type; 28 uint32_t msg_len; 29 uint8_t *data; 30 size_t data_len; 31 32 struct tls13_buffer *buf; 33 CBS cbs; 34 CBB cbb; 35 }; 36 37 struct tls13_handshake_msg * 38 tls13_handshake_msg_new() 39 { 40 struct tls13_handshake_msg *msg = NULL; 41 42 if ((msg = calloc(1, sizeof(struct tls13_handshake_msg))) == NULL) 43 goto err; 44 if ((msg->buf = tls13_buffer_new(0)) == NULL) 45 goto err; 46 47 return msg; 48 49 err: 50 tls13_handshake_msg_free(msg); 51 52 return NULL; 53 } 54 55 void 56 tls13_handshake_msg_free(struct tls13_handshake_msg *msg) 57 { 58 if (msg == NULL) 59 return; 60 61 tls13_buffer_free(msg->buf); 62 63 CBB_cleanup(&msg->cbb); 64 65 freezero(msg->data, msg->data_len); 66 freezero(msg, sizeof(struct tls13_handshake_msg)); 67 } 68 69 void 70 tls13_handshake_msg_data(struct tls13_handshake_msg *msg, CBS *cbs) 71 { 72 CBS_init(cbs, msg->data, msg->data_len); 73 } 74 75 int 76 tls13_handshake_msg_set_buffer(struct tls13_handshake_msg *msg, CBS *cbs) 77 { 78 return tls13_buffer_set_data(msg->buf, cbs); 79 } 80 81 uint8_t 82 tls13_handshake_msg_type(struct tls13_handshake_msg *msg) 83 { 84 return msg->msg_type; 85 } 86 87 int 88 tls13_handshake_msg_content(struct tls13_handshake_msg *msg, CBS *cbs) 89 { 90 tls13_handshake_msg_data(msg, cbs); 91 92 return CBS_skip(cbs, TLS13_HANDSHAKE_MSG_HEADER_LEN); 93 } 94 95 int 96 tls13_handshake_msg_start(struct tls13_handshake_msg *msg, CBB *body, 97 uint8_t msg_type) 98 { 99 if (!CBB_init(&msg->cbb, TLS13_HANDSHAKE_MSG_INITIAL_LEN)) 100 return 0; 101 if (!CBB_add_u8(&msg->cbb, msg_type)) 102 return 0; 103 if (!CBB_add_u24_length_prefixed(&msg->cbb, body)) 104 return 0; 105 106 return 1; 107 } 108 109 int 110 tls13_handshake_msg_finish(struct tls13_handshake_msg *msg) 111 { 112 if (!CBB_finish(&msg->cbb, &msg->data, &msg->data_len)) 113 return 0; 114 115 CBS_init(&msg->cbs, msg->data, msg->data_len); 116 117 return 1; 118 } 119 120 static ssize_t 121 tls13_handshake_msg_read_cb(void *buf, size_t n, void *cb_arg) 122 { 123 struct tls13_record_layer *rl = cb_arg; 124 125 return tls13_read_handshake_data(rl, buf, n); 126 } 127 128 int 129 tls13_handshake_msg_recv(struct tls13_handshake_msg *msg, 130 struct tls13_record_layer *rl) 131 { 132 uint8_t msg_type; 133 uint32_t msg_len; 134 CBS cbs; 135 int ret; 136 137 if (msg->data != NULL) 138 return TLS13_IO_FAILURE; 139 140 if (msg->msg_type == 0) { 141 if ((ret = tls13_buffer_extend(msg->buf, 142 TLS13_HANDSHAKE_MSG_HEADER_LEN, 143 tls13_handshake_msg_read_cb, rl)) <= 0) 144 return ret; 145 146 tls13_buffer_cbs(msg->buf, &cbs); 147 148 if (!CBS_get_u8(&cbs, &msg_type)) 149 return TLS13_IO_FAILURE; 150 if (!CBS_get_u24(&cbs, &msg_len)) 151 return TLS13_IO_FAILURE; 152 153 /* XXX - do we want to make this variable on message type? */ 154 if (msg_len > TLS13_HANDSHAKE_MSG_MAX_LEN) 155 return TLS13_IO_FAILURE; 156 157 msg->msg_type = msg_type; 158 msg->msg_len = msg_len; 159 } 160 161 if ((ret = tls13_buffer_extend(msg->buf, 162 TLS13_HANDSHAKE_MSG_HEADER_LEN + msg->msg_len, 163 tls13_handshake_msg_read_cb, rl)) <= 0) 164 return ret; 165 166 if (!tls13_buffer_finish(msg->buf, &msg->data, &msg->data_len)) 167 return TLS13_IO_FAILURE; 168 169 return TLS13_IO_SUCCESS; 170 } 171 172 int 173 tls13_handshake_msg_send(struct tls13_handshake_msg *msg, 174 struct tls13_record_layer *rl) 175 { 176 ssize_t ret; 177 178 if (msg->data == NULL) 179 return TLS13_IO_FAILURE; 180 181 if (CBS_len(&msg->cbs) == 0) 182 return TLS13_IO_FAILURE; 183 184 while (CBS_len(&msg->cbs) > 0) { 185 if ((ret = tls13_write_handshake_data(rl, CBS_data(&msg->cbs), 186 CBS_len(&msg->cbs))) <= 0) 187 return ret; 188 189 if (!CBS_skip(&msg->cbs, ret)) 190 return TLS13_IO_FAILURE; 191 } 192 193 return TLS13_IO_SUCCESS; 194 } 195