<div dir="ltr">Hello,<div><br></div><div>I'm attempting to use libssl to verify a RSA with SHA256 signature, but I can't get the public key to be read. I'm using a couple of Ubuntu systems, package libssl-dev 1.0.2 and 3.0.2, both experiencing the issue.</div><div><br></div><div>To ensure I'm working with good keys, I ran the following steps to set up the test:</div><div><br></div><div><font face="monospace">$ openssl genrsa -out key.pem 4096<br>$ openssl rsa -in key.pem -pubout > key.pub</font></div><font face="monospace">$ echo "The message to sign" > msg<br>$ openssl dgst -sign key.pem -keyform PEM -sha256 -out msg.sign -binary msg<br>$ openssl dgst -verify key.pub -keyform PEM -sha256 -signature msg.sign -binary msg<br></font><div><font face="monospace">Verified OK<br></font></div><div><br></div><div>At this point I have a pair of rsa keys that can be used to sign and verify a message.</div><div><br></div><div>I tried to use PEM_read_RSAPublicKey to read the key from file, and PEM_read_bio_RSAPublicKey to first read the contents and then use a BIO to read it, but in both cases I get the following error message: <font face="monospace">error:0480006C:PEM routines::no start line</font></div><div><br></div><div>Here's a sample c++ program that experiences the issue</div><div><br></div><div><font face="monospace">#include <iostream><br>#include <memory><br>#include <cstdio><br><br>#include <openssl/rsa.h><br>#include <openssl/pem.h><br>#include <openssl/err.h><br><br>struct RsaDeleter{ void operator()(RSA* ptr){RSA_free(ptr);}};<br>using RsaPtr = std::unique_ptr<RSA, RsaDeleter>;<br><br>struct BioDeleter{ void operator()(BIO* ptr){BIO_free(ptr);}};<br>using BioPtr = std::unique_ptr<BIO, BioDeleter>;<br><br>struct FileDeleter{ void operator()(FILE* ptr){fclose(ptr);}};<br>using FilePtr = std::unique_ptr<FILE, FileDeleter>;<br><br>RsaPtr PublicKey(const char* key, size_t keySz) {<br>  auto keyBio = BioPtr{BIO_new_mem_buf(key, keySz)};<br>  char pass[] = "";<br>  return RsaPtr{PEM_read_bio_RSAPublicKey(keyBio.get(), nullptr, nullptr, pass)};<br>}<br><br>RsaPtr PublicKey(FILE* keyFile) {<br>  char pass[] = "";<br>  return RsaPtr{PEM_read_RSAPublicKey(keyFile, nullptr, nullptr, pass)};<br>}<br><br>int main() {<br>  constexpr auto pubKey = "-----BEGIN PUBLIC KEY-----\n"<br>"MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAmeleGL76annhN96BaxhZ\n"<br>"iZXZ+6r9Ya+qNH9ulQcKOk6/T8S1T1/cdMDM6GDAb98quemk0bc8yTgTmvHl+JLu\n"<br>"hNea0zYSfr0mgEcG0Yjjc/jG2V9M5xejRkgwexP/FuMY3P621k2wBLLbne0QI/k2\n"<br>"REi+ryd6CXoY8JpURVY75t3BlKpnPc6wttWhyn7ScnMYU8rIXQI/4ppLX1SDgHQw\n"<br>"Mwbp2Vqou9QZBEeZ4cUNvZJ0bHMghz0CCpxihzNFqPfuGkZ6kRC7TUFybSydSU2V\n"<br>"+a9WdEf63q1Vj09pyq6zvnIYHaIzfTsMfL1j6t3TMeRMV75ieTYvYXIHCW+U4UuI\n"<br>"kwCkP4b1/ScJ17MkkPIH1TMiTqUyXQSWIJCMfHjZ3pZ1LfAJpYG0f0rfbXAY0XBj\n"<br>"EO+mm/zwH77/8stKkFKUCoiXi0duq99oEvMrIU+s0mJrvQdOhmA7U3k4UKImLRs8\n"<br>"JLWGfI4GYes40ly9ld1D52l0HWGE9wXVtjIY4ZDDtmf5SyvF9QOctQ6IQeuX4hHj\n"<br>"1ewjX6offF6t4TqpaVMJKLNZRG/rnXX/pomhm+Sm2BEaNcJHfhGIRLTV3dxV26X5\n"<br>"FDxhY6mEz8W7f2tucc1D9FbA9iemfTsCfs5KAPZYZ9ytc9ADcUQGMN7xByzVYR7S\n"<br>"7/KGw3T3A+/gduseXjvmxV8CAwEAAQ==\n"<br>"-----END PUBLIC KEY-----\n";<br>  auto memKey = PublicKey(pubKey, sizeof(pubKey)-1);<br>  if ( !memKey )</font></div><div><font face="monospace">    std::cout << "ERROR : " << ERR_error_string(ERR_get_error(), nullptr) << '\n';<br>  auto filePtr = FilePtr{std::fopen("key.pub", "r")};<br>  auto fileKey = PublicKey(filePtr.get());<br>  if ( !fileKey )</font></div><div><font face="monospace">    std::cout << "ERROR : " << ERR_error_string(ERR_get_error(), nullptr) << '\n';<br>  return 0;<br>}</font><br></div><div><br></div><div>The output from calling this program is:</div><div><br></div><div><font face="monospace">$ g++ -o read-pubkey -std=c++17 -DOPENSSL_API_COMPAT=10101 main.cpp -lssl -lcrypto</font></div><div><font face="monospace">$ ./read-pubkey<br>ERROR : error:0480006C:PEM routines::no start line<br>ERROR : error:0480006C:PEM routines::no start line<br></font><br></div><div>The hardcoded pubKey string holds the contents of the key.pub I created before. I try to load the key both from file and from memory and both return the "no start line" error I described above.</div><div><br></div><div>I am able to load the private key generated above with <span style="font-family:monospace">PEM_read_bio_RSAPrivateKey </span><font face="arial, sans-serif">which is why I am trying to use the corresponding RSAPublicKey, but I've tried with the other pubkey functions and they all have similar errors. Even when attempting to load it into a EVP with </font><span style="background-color:transparent;font-style:inherit;font-variant-ligatures:inherit;font-variant-caps:inherit;font-weight:inherit;white-space:inherit"><font face="monospace">PEM_read_bio_PUBKEY</font></span><font face="arial, sans-serif"> to create the RSA from it I get </font><font face="monospace">error:1E08010C:DECODER routines::unsupported</font></div><div><font face="monospace"><br></font></div><div><font face="arial, sans-serif">So at this point I'm pretty stumped, and was hoping someone could indicate what it is I am doing wrong.</font></div><div><font face="arial, sans-serif"><br></font></div><div><font face="arial, sans-serif">Thanks,</font></div><div><font face="arial, sans-serif">Juan</font></div></div>