Imported code from github.com/gwitmond/ecca-ca.