Can't download in chuncks using https


christian tavares
 

I'm developing an http_client library, this library needs to download a flash image on the server and install the image on the card.
When I download the flash image from the server if it is larger than the request_buf of the http_client_set_tls function, an error occurs to save the encrypted packets on the stack. I would like to know if you have a way to download the larger image by checking the encryption in pieces?
I've been researching a lot how to solve this case, but I have not found a solution. I'll be grateful if anyone can find a solution. Below I am sending what happens.

[https/client] [DBG] on_status: (0x2000d1c4): HTTP response status OK
[https/client] [DBG] print_header_field: (0x2000d1c4): [12] X-Powered-By
[https/client] [DBG] print_header_field: (0x2000d1c4): [7] Express
[https/client] [DBG] print_header_field: (0x2000d1c4): [13] Accept-Ranges
[https/client] [DBG] print_header_field: (0x2000d1c4): [5] bytes
[https/client] [DBG] print_header_field: (0x2000d1c4): [13] Cache-Control
[https/client] [DBG] print_header_field: (0x2000d1c4): [17] public, max-age=0
[https/client] [DBG] print_header_field: (0x2000d1c4): [13] Last-Modified
[https/client] [DBG] print_header_field: (0x2000d1c4): [29] Thu, 08 Mar 2018 19:14:53 GMT
[https/client] [DBG] print_header_field: (0x2000d1c4): [4] ETag
[https/client] [DBG] print_header_field: (0x2000d1c4): [21] W/"60000-16207099be3"
[https/client] [DBG] print_header_field: (0x2000d1c4): [12] Content-Type
[https/client] [DBG] print_header_field: (0x2000d1c4): [24] application/octet-stream
[https/client] [DBG] print_header_field: (0x2000d1c4): [14] Content-Length
[https/client] [DBG] print_header_field: (0x2000d1c4): [6] 393216
[https/client] [DBG] print_header_field: (0x2000d1c4): [4] Date
[https/client] [DBG] print_header_field: (0x2000d1c4): [29] Tue, 08 May 2018 21:02:56 GMT
[https/client] [DBG] print_header_field: (0x2000d1c4): [10] Connection
[https/client] [DBG] print_header_field: (0x2000d1c4): [5] close
[https/client] [DBG] on_headers_complete: (0x2000d1c4): Headers complete
[net/app] [DBG] _net_app_tls_received: (0x2000be5c): Encrypted data received in pkt 0x2000f714
[net/app] [DBG] _net_app_ssl_mux: (0x2000d1c4): Receiving encrypted data in pkt 0x2000f714 (len 576)
[net/app] [DBG] my_debug: (0x2000d1c4): ssl_tls.c:3572: |1| bad message length
[net/app] [DBG] my_debug: (0x2000d1c4): ssl_tls.c:3783: |1| mbedtls_ssl_read_record_layer() returned -29184 (-0x7200)
[net/app] [DBG] my_debug: (0x2000d1c4): ssl_tls.c:6944: |1| mbedtls_ssl_read_record() returned -29184 (-0x7200)
[net/app] [ERR] _net_app_ssl_mainloop: mbedtls_ssl_read returned -0x7200 (SSL - An invalid SSL record was received)
[net/app] [ERR] _net_app_ssl_mainloop: Closing connection -0x7200 (SSL - An invalid SSL record was received)
[net/app] [ERR] tls_client_handler: TLS mainloop startup failed (-29184)
[net/app] [DBG] tls_client_handler: (0x2000d1c4): Shutting down TLS handler
[https/client] [DBG] http_closed: (0x2000d1c4): [0x2000d118] connection closed
[net/app] [DBG] _net_app_tls_handler_stop: (0x2000d1c4): TLS thread 0x2000d1c4 stopped
[net/app] [DBG] _net_app_tls_received: (0x2000be5c): Encrypted data received in pkt 0x2000f768
[net/app] [DBG] _net_app_tls_received: (0x2000be5c): Encrypted data received in pkt 0x2000f714
[net/app] [DBG] _net_app_tls_received: (0x2000be5c): Encrypted data received in pkt 0x2000f6c0
[net/app] [DBG] _net_app_tls_received: (0x2000be5c): Encrypted data received in pkt 0x2000f66c
[net/app] [DBG] _net_app_tls_received: (0x2000be5c): Encrypted data received in pkt 0x2000f618
[net/app] [DBG] _net_app_tls_received: (0x2000be5c): Encrypted data received in pkt 0x2000f5c4
[net/app] [DBG] _net_app_tls_received: (0x2000be5c): Encrypted data received in pkt 0x2000f570
[net/app] [DBG] _net_app_tls_received: (0x2000be5c): Encrypted data received in pkt 0x2000f51c
[net/app] [DBG] _net_app_tls_received: (0x2000be5c): Encrypted data received in pkt 0x2000f4c8
[net/app] [DBG] _net_app_tls_received: (0x2000be5c): Encrypted data received in pkt 0x2000f474
[net/app] [DBG] _net_app_tls_received: (0x2000be5c): Encrypted data received in pkt 0x2000f420
[net/app] [DBG] _net_app_tls_received: (0x2000be5c): Encrypted data received in pkt 0x2000f3cc
[net/app] [DBG] _net_app_tls_received: (0x2000be5c): Encrypted data received in pkt 0x2000f378
[net/app] [DBG] _net_app_tls_received: (0x2000be5c): Encrypted data received in pkt 0x2000f324

Here shows the size that defined the variables used by the functions:

RESULT_BUF_SIZE = MBEDTLS_SSL_MAX_CONTENT_LEN (1500) 
#define HTTPS_STACK_SIZE (4096)
NET_STACK_DEFINE(HTTPS, https_stack, HTTPS_STACK_SIZE, HTTPS_STACK_SIZE);
NET_APP_TLS_POOL_DEFINE(ssl_pool, 10);
#define TCP_RECV_BUFFER_SIZE 1024 
#define APP_TIMEOUT K_SECONDS(10)

static u8_t https_result_buf[RESULT_BUF_SIZE];

ret = http_client_init(&ctx->http_client.http_ctx,
                                  UPDATEHUB_SERVER,
                                UPDATEHUB_PORT, NULL,
                                   APP_TIMEOUT);

ret = http_client_set_tls(&ctx->http_client.http_ctx,
                                       https_result_buf,
                                       RESULT_BUF_SIZE,
                                         NULL,
                                        NULL,
                                       setup_cert,
                                        NULL,
                                        NULL,
                                     &ssl_pool,
                                      https_stack,
                                     HTTPS_STACK_SIZE);

ret = http_client_send_req(&ctx->http_client.http_ctx,
                                              &ctx->http_client.req,
                                            install_update_cb ,
                                            ctx->http_client.tcp_buffer,
                                            TCP_RECV_BUFFER_SIZE, ctx,
                                              APP_TIMEOUT);

The install_update_cb function is responsible for downloading parts of the image and saving this image in flash area 1.

For a better view, I am sending the link from the library. The call occurs in function "updatehub_install_update"
https://github.com/OSSystems/zephyr/blob/master/subsys/updatehub/updatehub.c


christian tavares
 

I was able to resolve this problem by configuring the server to send smaller fragments than it was sending