title: SSL_CTX_new:unable to load ssl2 md5 routines
body: <p>We are having trouble running a program built with OpenSSL. The specific error that we are encountering is: <strong><em><code>global error: SSL Context init failed error:140A90F1:SSL routines:SSL_CTX_new:unable to load ssl2 md5 routines</code></em></strong>.</p>
  
  <p>The context is a <a href="https://github.com/bitpay/bitpay-c-keyutils" rel="nofollow">C library</a> which is being wrapped by a <a href="https://github.com/bitpay/bitpay-perl-keyutils" rel="nofollow">Perl library</a> using SWIG. The error occurs when attempting to connect to an HTTPS address using the Perl library.</p>
  
  <p>For example, this code will cause the error:</p>
  
  <pre><code>#These are just the imports I felt were relevant to this problem
  use LWP::UserAgent;
  use HTTP::Request;
  use LWP::Protocol::https;
  use IO::Socket::SSL;
  
  sub test_can_connect_to_bitpay_api {
    my $uri = "https://www.google.com";
    my $pem = Business::OnlinePayment::BitPay::KeyUtils::bpGeneratePem();
    my $sin = Business::OnlinePayment::BitPay::KeyUtils::bpGenerateSinFromPem($pem);
    my $request = HTTP::Request-&gt;new(POST =&gt; $uri);
    my $ua = LWP::UserAgent-&gt;new;
    my $response = $ua-&gt;request($request);
    ok($response-&gt;is_success, "TEST CONNECTION");
  }
  </code></pre>
  
  <p>But the same code will not error if the call to <code>bpGenerateSinFromPem()</code> is not made.</p>
  
  <p><code>bpGenerateSinFromPem</code> is the wrapper function in the *.i file.</p>
  
  <pre><code>%inline %{
    char *bpGenerateSinFromPem(char *pem) {
      char *ret = malloc(sizeof(char)*36);
      char *err = "ERROR";
      int errorCode;
  
      errorCode = generateSinFromPem(pem, &amp;ret);
  
      if (errorCode == NOERROR) {
        return ret;
      } else {
        return err;
      }
  
    }
  %}
  </code></pre>
  
  <p>Which calls the C function:</p>
  
  <pre><code>int GenerateSinFromPem(char *pem, char **sin) {
  
      char *pub =     calloc(67, sizeof(char));
  
      u_int8_t *outBytesPub = calloc(SHA256_STRING, sizeof(u_int8_t));
      u_int8_t *outBytesOfStep1 = calloc(SHA256_STRING, sizeof(u_int8_t));
      u_int8_t *outBytesOfStep3 = calloc(RIPEMD_AND_PADDING_STRING, sizeof(u_int8_t));
      u_int8_t *outBytesOfStep4a = calloc(SHA256_STRING, sizeof(u_int8_t));
  
      char *step1 =   calloc(SHA256_HEX_STRING, sizeof(char));
      char *step2 =   calloc(RIPEMD_HEX_STRING, sizeof(char));
      char *step3 =   calloc(RIPEMD_AND_PADDING_HEX_STRING, sizeof(char));
      char *step4a =  calloc(SHA256_HEX_STRING, sizeof(char));
      char *step4b =  calloc(SHA256_HEX_STRING, sizeof(char));
      char *step5 =   calloc(CHECKSUM_STRING, sizeof(char));
      char *step6 =   calloc(RIPEMD_AND_PADDING_HEX + CHECKSUM_STRING, sizeof(char));
  
      char *base58OfStep6 = calloc(SIN_STRING, sizeof(char));
  
      getPublicKeyFromPem(pem, &amp;pub);
      pub[66] = '\0';
  
      createDataWithHexString(pub, &amp;outBytesPub);
      digestOfBytes(outBytesPub, &amp;step1, "sha256", SHA256_STRING);
      step1[64] = '\0';
  
      createDataWithHexString(step1, &amp;outBytesOfStep1);
      digestOfBytes(outBytesOfStep1, &amp;step2, "ripemd160", SHA256_DIGEST_LENGTH);
      step2[40] = '\0';
  
      memcpy(step3, "0F02", 4);
      memcpy(step3+4, step2, RIPEMD_HEX);
      step3[44] = '\0';
  
      createDataWithHexString(step3, &amp;outBytesOfStep3);
      digestOfBytes(outBytesOfStep3, &amp;step4a, "sha256", RIPEMD_AND_PADDING);
      step4a[64] = '\0';
  
      createDataWithHexString(step4a, &amp;outBytesOfStep4a);
      digestOfBytes(outBytesOfStep4a, &amp;step4b, "sha256", SHA256_DIGEST_LENGTH);
      step4b[64] = '\0';
  
      memcpy(step5, step4b, CHECKSUM);
  
      sprintf(step6, "%s%s", step3, step5);
      step6[RIPEMD_AND_PADDING_HEX + CHECKSUM] = '\0';
  
      base58encode(step6, base58OfStep6);
      base58OfStep6[SIN] = '\0';
      memcpy(*sin, base58OfStep6, SIN_STRING);
  
      free(pub);
      free(step1);
      free(step2);
      free(step3);
      free(step4a);
      free(step4b);
      free(step5);
      free(step6);
      free(base58OfStep6);
  
      free(outBytesPub);
      free(outBytesOfStep1);
      free(outBytesOfStep3);
      free(outBytesOfStep4a);
      return NOERROR;
  }
  </code></pre>
  
  <p>Which finally calls the digestOfBytes() method:</p>
  
  <pre><code>static int digestOfBytes(uint8_t *message, char **output, char *type, int inLength) {
      EVP_MD_CTX *mdctx;
      const EVP_MD *md;
      unsigned char md_value[EVP_MAX_MD_SIZE];
      unsigned int md_len, i;
  
      OpenSSL_add_all_digests();
  
      md = EVP_get_digestbyname(type);
      mdctx = EVP_MD_CTX_create();
      EVP_DigestInit_ex(mdctx, md, NULL);
      EVP_DigestUpdate(mdctx, message, inLength);
      EVP_DigestFinal_ex(mdctx, md_value, &amp;md_len);
      EVP_MD_CTX_destroy(mdctx);
  
      char *digest = calloc((md_len*2) + 1, sizeof(char));
      for(i = 0; i &lt; md_len; i++){
        sprintf(&amp;digest[2*i], "%02x", md_value[i]);
      };
      digest[md_len * 2] = '\0';
      memcpy(*output, digest, strlen(digest));
      free(digest);
      /* Call this once before exit. */
      EVP_cleanup();
      return 0;
  }
  </code></pre>
  
  <p>There seems to be some possibility that this is being caused by not releasing some resources in our C library, but it looks like we are freeing all the memory that can be freed. Why would we be seeing this conflict?</p>