Add proper support for intermediate certificates, rework config parsing