Imported code from crypto/tls