From ba5f7d9262f87f1e24a73f23274019312587b83f Mon Sep 17 00:00:00 2001 From: Ander Juaristi Date: Sat, 3 Feb 2018 15:13:01 +0100 Subject: psk_parser --> psk_ext_parser Signed-off-by: Ander Juaristi --- lib/Makefile.am | 2 +- lib/ext/pre_shared_key.c | 2 +- lib/tls13/psk_ext_parser.c | 171 +++++++++++++++++++++++++++++++++++++++++++++ lib/tls13/psk_ext_parser.h | 50 +++++++++++++ lib/tls13/psk_parser.c | 171 --------------------------------------------- lib/tls13/psk_parser.h | 50 ------------- 6 files changed, 223 insertions(+), 223 deletions(-) create mode 100644 lib/tls13/psk_ext_parser.c create mode 100644 lib/tls13/psk_ext_parser.h delete mode 100644 lib/tls13/psk_parser.c delete mode 100644 lib/tls13/psk_parser.h diff --git a/lib/Makefile.am b/lib/Makefile.am index 71727515b8..2360b8b6c9 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -98,7 +98,7 @@ COBJECTS += tls13/encrypted_extensions.c tls13/encrypted_extensions.h \ tls13/session_ticket.c tls13/session_ticket.h \ tls13/certificate.c tls13/certificate.h \ tls13/post_handshake.c \ - tls13/psk_parser.c tls13/psk_parser.h + tls13/psk_ext_parser.c tls13/psk_ext_parser.h if ENABLE_PKCS11 COBJECTS += pkcs11.c pkcs11x.c pkcs11_privkey.c pkcs11_write.c pkcs11_secret.c \ diff --git a/lib/ext/pre_shared_key.c b/lib/ext/pre_shared_key.c index 6ba985a829..82311463a2 100644 --- a/lib/ext/pre_shared_key.c +++ b/lib/ext/pre_shared_key.c @@ -26,11 +26,11 @@ #include "mem.h" #include "str.h" #include "tls13/finished.h" -#include "tls13/psk_parser.h" #include "tls13/session_ticket.h" #include "auth/psk_passwd.h" #include #include +#include "tls13/psk_ext_parser.h" typedef struct { struct tls13_nst_st *session_ticket; diff --git a/lib/tls13/psk_ext_parser.c b/lib/tls13/psk_ext_parser.c new file mode 100644 index 0000000000..b6d5c12f40 --- /dev/null +++ b/lib/tls13/psk_ext_parser.c @@ -0,0 +1,171 @@ +/* + * Copyright (C) 2017 Free Software Foundation, Inc. + * + * Author: Ander Juaristi + * + * This file is part of GnuTLS. + * + * The GnuTLS is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see + * + */ +#include "tls13/psk_ext_parser.h" + +static int advance_to_end_of_object(struct psk_ext_parser_st *p) +{ + size_t adv; + + /* Advance the pointer to the end of the current object */ + if (p->obj_read < p->obj_len) { + adv = p->obj_len - p->obj_read; + DECR_LEN(p->len, adv); + p->data += adv; + } + + return 0; +} + +int _gnutls13_psk_ext_parser_init(struct psk_ext_parser_st *p, + const unsigned char *data, size_t len) +{ + uint16_t identities_len; + + memset(p, 0, sizeof(struct psk_ext_parser_st)); + + identities_len = _gnutls_read_uint16(data); + + if (identities_len > 0) { + DECR_LEN(len, 2); + data += 2; + + p->obj_len = identities_len; + p->data = (unsigned char *) data; + p->len = len; + } + + return identities_len; +} + +int _gnutls13_psk_ext_parser_deinit(struct psk_ext_parser_st *p, + const unsigned char **data, size_t *len) +{ + if (p->obj_len == 0) + goto end; + + if (advance_to_end_of_object(p) < 0) + return gnutls_assert_val(GNUTLS_E_UNEXPECTED_PACKET_LENGTH); + + if (data) + *data = p->data; + if (len) + *len = p->len; + +end: + memset(p, 0, sizeof(struct psk_ext_parser_st)); + return 0; +} + +int _gnutls13_psk_ext_parser_next_psk(struct psk_ext_parser_st *p, struct psk_st *psk) +{ + if (p->obj_read >= p->obj_len) + return GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE; + + /* Read a PskIdentity structure */ + psk->identity.size = _gnutls_read_uint16(p->data); + if (psk->identity.size == 0) + return GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE; + + DECR_LEN(p->len, 2); + p->data += 2; + p->obj_read += 2; + + psk->identity.data = p->data; + + DECR_LEN(p->len, psk->identity.size); + p->data += psk->identity.size; + p->obj_read += psk->identity.size; + + psk->ob_ticket_age = _gnutls_read_uint32(p->data); + DECR_LEN(p->len, 4); + p->data += 4; + p->obj_read += 4; + + psk->selected_index = p->next_index++; + return psk->selected_index; +} + +int _gnutls13_psk_ext_parser_find_binder(struct psk_ext_parser_st *p, int psk_index, + gnutls_datum_t *binder_out) +{ + uint16_t binders_len; + uint8_t binder_len; + int cur_index = 0, binder_found = 0; + + if (p == NULL || psk_index < 0 || binder_out == NULL) + return gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR); + + if (p->obj_len == 0) + return gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR); + + /* Place the pointer at the start of the binders */ + if (advance_to_end_of_object(p) < 0) + return gnutls_assert_val(GNUTLS_E_UNEXPECTED_PACKET_LENGTH); + + binders_len = _gnutls_read_uint16(p->data); + if (binders_len > 0) { + DECR_LEN(p->len, 2); + p->data += 2; + + p->obj_len = binders_len; + p->obj_read = 0; + } else { + return gnutls_assert_val(GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE); + } + + /* Start traversing the binders */ + while (p->len > 0) { + binder_len = *p->data; + if (binder_len == 0) + return gnutls_assert_val(GNUTLS_E_INSUFFICIENT_CREDENTIALS); + + DECR_LEN(p->len, 1); + p->data++; + p->obj_read++; + + if (cur_index == psk_index) { + /* We found the binder with the supplied index */ + binder_out->data = gnutls_malloc(binder_len); + if (!binder_out->data) + return gnutls_assert_val(GNUTLS_E_MEMORY_ERROR); + + binder_out->size = binder_len; + memcpy(binder_out->data, p->data, binder_len); + + binder_found = 1; + } + + DECR_LEN(p->len, binder_len); + p->data += binder_len; + p->obj_read += binder_len; + + if (binder_found) + break; + + binder_len = 0; + cur_index++; + } + + return (binder_found ? + 0 : + gnutls_assert_val(GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE)); +} diff --git a/lib/tls13/psk_ext_parser.h b/lib/tls13/psk_ext_parser.h new file mode 100644 index 0000000000..de908ead98 --- /dev/null +++ b/lib/tls13/psk_ext_parser.h @@ -0,0 +1,50 @@ +/* + * Copyright (C) 2017 Free Software Foundation, Inc. + * + * Author: Ander Juaristi + * + * This file is part of GnuTLS. + * + * The GnuTLS is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see + * + */ + +#ifndef PSK_PARSER_H +#define PSK_PARSER_H +#include "gnutls_int.h" + +struct psk_ext_parser_st { + unsigned char *data; + ssize_t len; + uint16_t obj_len; + uint16_t obj_read; + int next_index; +}; + +struct psk_st { + gnutls_datum_t identity; + uint32_t ob_ticket_age; + int selected_index; +}; + +int _gnutls13_psk_ext_parser_init(struct psk_ext_parser_st *p, + const unsigned char *data, size_t len); +int _gnutls13_psk_ext_parser_deinit(struct psk_ext_parser_st *p, + const unsigned char **data, size_t *len); +int _gnutls13_psk_ext_parser_next_psk(struct psk_ext_parser_st *p, struct psk_st *psk); +int _gnutls13_psk_ext_parser_find_binder(struct psk_ext_parser_st *p, int psk_index, + gnutls_datum_t *binder_out); + +#endif + diff --git a/lib/tls13/psk_parser.c b/lib/tls13/psk_parser.c deleted file mode 100644 index 86186eb215..0000000000 --- a/lib/tls13/psk_parser.c +++ /dev/null @@ -1,171 +0,0 @@ -/* - * Copyright (C) 2017 Free Software Foundation, Inc. - * - * Author: Ander Juaristi - * - * This file is part of GnuTLS. - * - * The GnuTLS is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public License - * as published by the Free Software Foundation; either version 2.1 of - * the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program. If not, see - * - */ -#include "tls13/psk_parser.h" - -static int advance_to_end_of_object(struct psk_ext_parser_st *p) -{ - size_t adv; - - /* Advance the pointer to the end of the current object */ - if (p->obj_read < p->obj_len) { - adv = p->obj_len - p->obj_read; - DECR_LEN(p->len, adv); - p->data += adv; - } - - return 0; -} - -int _gnutls13_psk_ext_parser_init(struct psk_ext_parser_st *p, - const unsigned char *data, size_t len) -{ - uint16_t identities_len; - - memset(p, 0, sizeof(struct psk_ext_parser_st)); - - identities_len = _gnutls_read_uint16(data); - - if (identities_len > 0) { - DECR_LEN(len, 2); - data += 2; - - p->obj_len = identities_len; - p->data = (unsigned char *) data; - p->len = len; - } - - return identities_len; -} - -int _gnutls13_psk_ext_parser_deinit(struct psk_ext_parser_st *p, - const unsigned char **data, size_t *len) -{ - if (p->obj_len == 0) - goto end; - - if (advance_to_end_of_object(p) < 0) - return gnutls_assert_val(GNUTLS_E_UNEXPECTED_PACKET_LENGTH); - - if (data) - *data = p->data; - if (len) - *len = p->len; - -end: - memset(p, 0, sizeof(struct psk_ext_parser_st)); - return 0; -} - -int _gnutls13_psk_ext_parser_next_psk(struct psk_ext_parser_st *p, struct psk_st *psk) -{ - if (p->obj_read >= p->obj_len) - return GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE; - - /* Read a PskIdentity structure */ - psk->identity.size = _gnutls_read_uint16(p->data); - if (psk->identity.size == 0) - return GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE; - - DECR_LEN(p->len, 2); - p->data += 2; - p->obj_read += 2; - - psk->identity.data = p->data; - - DECR_LEN(p->len, psk->identity.size); - p->data += psk->identity.size; - p->obj_read += psk->identity.size; - - psk->ob_ticket_age = _gnutls_read_uint32(p->data); - DECR_LEN(p->len, 4); - p->data += 4; - p->obj_read += 4; - - psk->selected_index = p->next_index++; - return psk->selected_index; -} - -int _gnutls13_psk_ext_parser_find_binder(struct psk_ext_parser_st *p, int psk_index, - gnutls_datum_t *binder_out) -{ - uint16_t binders_len; - uint8_t binder_len; - int cur_index = 0, binder_found = 0; - - if (p == NULL || psk_index < 0 || binder_out == NULL) - return gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR); - - if (p->obj_len == 0) - return gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR); - - /* Place the pointer at the start of the binders */ - if (advance_to_end_of_object(p) < 0) - return gnutls_assert_val(GNUTLS_E_UNEXPECTED_PACKET_LENGTH); - - binders_len = _gnutls_read_uint16(p->data); - if (binders_len > 0) { - DECR_LEN(p->len, 2); - p->data += 2; - - p->obj_len = binders_len; - p->obj_read = 0; - } else { - return gnutls_assert_val(GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE); - } - - /* Start traversing the binders */ - while (p->len > 0) { - binder_len = *p->data; - if (binder_len == 0) - return gnutls_assert_val(GNUTLS_E_INSUFFICIENT_CREDENTIALS); - - DECR_LEN(p->len, 1); - p->data++; - p->obj_read++; - - if (cur_index == psk_index) { - /* We found the binder with the supplied index */ - binder_out->data = gnutls_malloc(binder_len); - if (!binder_out->data) - return gnutls_assert_val(GNUTLS_E_MEMORY_ERROR); - - binder_out->size = binder_len; - memcpy(binder_out->data, p->data, binder_len); - - binder_found = 1; - } - - DECR_LEN(p->len, binder_len); - p->data += binder_len; - p->obj_read += binder_len; - - if (binder_found) - break; - - binder_len = 0; - cur_index++; - } - - return (binder_found ? - 0 : - gnutls_assert_val(GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE)); -} diff --git a/lib/tls13/psk_parser.h b/lib/tls13/psk_parser.h deleted file mode 100644 index de908ead98..0000000000 --- a/lib/tls13/psk_parser.h +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Copyright (C) 2017 Free Software Foundation, Inc. - * - * Author: Ander Juaristi - * - * This file is part of GnuTLS. - * - * The GnuTLS is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public License - * as published by the Free Software Foundation; either version 2.1 of - * the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program. If not, see - * - */ - -#ifndef PSK_PARSER_H -#define PSK_PARSER_H -#include "gnutls_int.h" - -struct psk_ext_parser_st { - unsigned char *data; - ssize_t len; - uint16_t obj_len; - uint16_t obj_read; - int next_index; -}; - -struct psk_st { - gnutls_datum_t identity; - uint32_t ob_ticket_age; - int selected_index; -}; - -int _gnutls13_psk_ext_parser_init(struct psk_ext_parser_st *p, - const unsigned char *data, size_t len); -int _gnutls13_psk_ext_parser_deinit(struct psk_ext_parser_st *p, - const unsigned char **data, size_t *len); -int _gnutls13_psk_ext_parser_next_psk(struct psk_ext_parser_st *p, struct psk_st *psk); -int _gnutls13_psk_ext_parser_find_binder(struct psk_ext_parser_st *p, int psk_index, - gnutls_datum_t *binder_out); - -#endif - -- cgit v1.2.1