Correctly treat error case and memory management
Merge pull request #906 from hardening/hardening8

Correctly treat error case and memory management when reading certificate