diff --git a/src/openvpn/tls_crypt.c b/src/openvpn/tls_crypt.c index 50228e78..386aaf12 100644 --- a/src/openvpn/tls_crypt.c +++ b/src/openvpn/tls_crypt.c @@ -229,7 +229,6 @@ tls_crypt_unwrap(const struct buffer *src, struct buffer *dst, gc_init(&gc); ASSERT(opt); - ASSERT(src->len > 0); ASSERT(ctx->cipher); ASSERT(packet_id_initialized(&opt->packet_id) || (opt->flags & CO_IGNORE_PACKET_ID)); @@ -627,7 +626,8 @@ tls_crypt_v2_extract_client_key(struct buffer *buf, struct buffer wrapped_client_key = *buf; uint16_t net_len = 0; - if (BLEN(&wrapped_client_key) < sizeof(net_len)) + if (!buf_advance(&wrapped_client_key, 1) + || BLEN(&wrapped_client_key) < 1 + sizeof(net_len)) { msg(D_TLS_ERRORS, "Can not read tls-crypt-v2 client key length"); return false; diff --git a/tests/unit_tests/openvpn/test_tls_crypt.c b/tests/unit_tests/openvpn/test_tls_crypt.c index bf5a8cef..fcf6f9a0 100644 --- a/tests/unit_tests/openvpn/test_tls_crypt.c +++ b/tests/unit_tests/openvpn/test_tls_crypt.c @@ -534,7 +534,16 @@ tls_crypt_v2_wrap_unwrap_max_metadata(void **state) .mode = TLS_WRAP_CRYPT, .tls_crypt_v2_server_key = ctx->server_keys.encrypt, }; - assert_true(tls_crypt_v2_extract_client_key(&ctx->wkc, &wrap_ctx, NULL, true)); + + /* a buffer that only contains the wrapped key should fail */ + assert_false(tls_crypt_v2_extract_client_key(&ctx->wkc, &wrap_ctx, NULL, true)); + + /* add a opcode in front of the key to make it valid to extract */ + struct buffer wkcop = alloc_buf_gc(buf_len(&ctx->wkc) + 1, &ctx->gc); + buf_write_u8(&wkcop, 0x50); + buf_copy(&wkcop, &ctx->wkc); + assert_true(tls_crypt_v2_extract_client_key(&wkcop, &wrap_ctx, NULL, true)); + tls_wrap_free(&wrap_ctx); }