title: Segmentation Fault while verifying JWT token using a public key through openSSL in C++
body: <p>I am very new to C++ and OpenSSL. I have to verify a given JWT token  (algorithm RS256) using a public key through OpenSSL in C++. I am using following algorithm to verify the JWT token.</p>
  
  <p><code>// signature algorithm
  data = base64urlEncode( header ) + “.” + base64urlEncode( payload )
  hashedData = hash( data, secret )
  signature = base64urlEncode( hashedData )</code></p>
  
  <p>I am on a Mac system and using g++ to compile my code. <code>openssl version</code> on terminal shows <code>LibreSSL 2.6.5</code>.</p>
  
  <pre><code>// Assume that base64 encode and decode functions are available
  bool RSAVerifySignature( RSA* rsa, std::string token, std::string pub_key) {
  
    std::vector&lt;std::string&gt; tokenParts;
    split(token, tokenParts, '.');
  
    std::string decoded_header = tokenParts[0];
    std::string header = base64_encode(reinterpret_cast&lt;const unsigned char*&gt;(decoded_header.c_str()),
      decoded_header.length());
  
    std::string decoded_body = tokenParts[1];
    std::string body = base64_encode(reinterpret_cast&lt;const unsigned char*&gt;(decoded_body.c_str()),
      decoded_body.length());
  
  
    std::string sig = tokenParts[2];
  
    EVP_PKEY* pubKey  = EVP_PKEY_new();
    EVP_PKEY_assign_RSA(pubKey, rsa);
    EVP_MD_CTX* m_RSAVerifyCtx = EVP_MD_CTX_create();
  
    if (1 != EVP_DigestVerifyInit(m_RSAVerifyCtx, NULL, EVP_sha256(), NULL, pubKey)) {
        printf("verify init failed....\n");
    } else {
        printf("verify init passed....\n");
    }
    if (1 != EVP_DigestVerifyUpdate(m_RSAVerifyCtx, (unsigned char *)header.data(), header.length())) {
        printf("DigestVerifyUpdate for header failed....\n");
    } else {
            printf("DigestVerifyUpdate for header passed....\n");
    }
    if (1 != EVP_DigestVerifyUpdate(m_RSAVerifyCtx, ".", 1)) {
        printf("DigestVerifyUpdate for dot failed\n");
    } else {
        printf("DigestVerifyUpdate for dot passed\n");
    }
    if (1 != EVP_DigestVerifyUpdate(m_RSAVerifyCtx, (unsigned char *)body.data(), body.length())) {
        printf("DigestVerifyUpdate for body failed\n");
    } else {
        printf("DigestVerifyUpdate for body passed\n");
    }
  
    int result = EVP_DigestVerifyFinal(m_RSAVerifyCtx, (unsigned char *)sig.data(), sig.length());
    return result;
  }
  
  RSA* createPublicRSA(std::string key) {
    RSA *rsa = NULL;
    BIO *keybio;
    const char* c_string = key.c_str();
    keybio = BIO_new_mem_buf((void*)c_string, -1);
    if (keybio==NULL) {
        return 0;
    }
    rsa = PEM_read_bio_RSA_PUBKEY(keybio, &amp;rsa,NULL, NULL);
    return rsa;
  }
  
  int main()
  {
      std::string token = "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWUsImlhdCI6MTUxNjIzOTAyMn0.POstGetfAytaZS82wHcjoTyoqhMyxXiWdR7Nn7A29DNSl0EiXLdwJ6xC6AfgZWF1bOsS_TuYI3OG85AmiExREkrS6tDfTQ2B3WXlrr-wp5AokiRbz3_oB4OxG-W9KcEEbDRcZc0nH3L7LzYptiy1PtAylQGxHTWZXtGz4ht0bAecBgmpdgXMguEIcoqPJ1n3pIWk_dUZegpqx0Lka21H6XxUTxiy8OcaarA8zdnPUnV6AmNP3ecFawIFYdvJB_cm-GvpCSbr8G8y_Mllj8f4x9nBH8pQux89_6gUY618iYv7tuPWBFfEbLxtF2pZS6YC1aSfLQxeNe8djT9YjpvRZA";
  
      std::string publicKey = "-----BEGIN PUBLIC KEY-----"\
  "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAnzyis1ZjfNB0bBgKFMSv"\
  "vkTtwlvBsaJq7S5wA+kzeVOVpVWwkWdVha4s38XM/pa/yr47av7+z3VTmvDRyAHc"\
  "aT92whREFpLv9cj5lTeJSibyr/Mrm/YtjCZVWgaOYIhwrXwKLqPr/11inWsAkfIy"\
  "tvHWTxZYEcXLgAXFuUuaS3uF9gEiNQwzGTU1v0FqkqTBr4B8nW3HCN47XUu0t8Y0"\
  "e+lf4s4OxQawWD79J9/5d3Ry0vbV3Am1FtGJiJvOwRsIfVChDpYStTcHTCMqtvWb"\
  "V6L11BWkpzGXSW4Hv43qa+GSYOD2QU68Mb59oSk2OB+BtOLpJofmbGEGgvmwyCI9"\
  "MwIDAQAB"\
  "-----END PUBLIC KEY-----";
  
      RSA* publicRSA = createPublicRSA(publicKey);
      bool result = RSAVerifySignature(publicRSA, token, publicKey);
      return 0;
  }
  
  </code></pre>
  
  <p>I am getting <code>Segmentation fault: 11</code> at <code>EVP_DigestVerifyFinal</code> call. I have no idea where am I wrong. Please help.</p>