tls: refactor ctx_write function to avoid allocate new memory every time