Improve certificate handling a bit and fix up tests.