title: Openssl DSA sign
body: <p>I am trying to sign some data using a DSA certificate. I am saving the certificate in memory (it was generated with the openssl gendsa command).</p>
  
  <p>My function looks like this, and my problem is with <code>res = EVP_SignFinal</code>. Here the function returns 0, and sets the signature_len on 0.</p>
  
  <pre><code>bool my_dsa_sign(const Certificate&amp; certificate, const char* messageData, size_t messageLength, Signature &amp;outSignature) {
      bool resultOk = false;
      BIO* bio = BIO_new_mem_buf((void*) certificate.getPEMData(), certificate.getSize());
      if(NULL != bio) {
          EVP_PKEY *pkey = NULL;
          if(NULL != (pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL))) {
              unsigned int signature_len;
              EVP_MD_CTX ctx;
              int res = EVP_SignInit(&amp;ctx, EVP_sha512());
              if(1 == res) {
                  res = EVP_SignUpdate(&amp;ctx, messageData, messageLength);
                  if(1 == res) {
                      unsigned char* s = (unsigned char*)malloc(EVP_PKEY_size(pkey));
  
                      // problem here
                      res = EVP_SignFinal(&amp;ctx, s, &amp;signature_len, pkey);
                      if(1 == res) {
                          resultOk = true;
                          signature.setData(s, signature_len);
                      }
                  }
              }
              EVP_PKEY_free(pkey);
              EVP_MD_CTX_cleanup(&amp;ctx);
          }
          BIO_free(bio);
      }
      return resultOk;
  }
  </code></pre>
  
  <p>any idea what the problem might be ?
  I have checked with the debugger, the value of the certificate is correct, in the form:</p>
  
  <pre><code>-----BEGIN DSA PRIVATE KEY-----
  MIID....
  .......
  -----END DSA PRIVATE KEY-----
  </code></pre>
  
  <p>Also the length is correct, matches with that of the file.</p>