Resolve tls issues 5.0

Backported from master to 5.0 (see PR as #503).