I was able to solve my problem finally.
I had to increase the main thread stack size (4096 by default) by adding
"rtos.main-thread-stack-size": 8192,
to the target_overrides
field of my mbed_app.json
file.
It is strange how 4096 is enough to get the TLS socket to work on the DISCO board but not on my PCB.
Anyways, I am happy I was able to solve this problem, thanks @JohnnyK .