05-16-2019, 12:13 PM
Hello,
I'm calling openssl from Delphi (10.3.1) using the indy libraries. This works perfectly when using a 32 bit application. However when the application is compiled to 64 bits, I get an access violation on this line: rc := EVP_PKEY_set1_RSA(@key, FRSA);
I use dll's copied from https://indy.fulgan.com/SSL/
Any idea what is causing this?
I have attached a test application but you'll need a private key to use it (I can send a temporary key when needed)
Thanks,
Kees
I'm calling openssl from Delphi (10.3.1) using the indy libraries. This works perfectly when using a 32 bit application. However when the application is compiled to 64 bits, I get an access violation on this line: rc := EVP_PKEY_set1_RSA(@key, FRSA);
I use dll's copied from https://indy.fulgan.com/SSL/
Any idea what is causing this?
I have attached a test application but you'll need a private key to use it (I can send a temporary key when needed)
Thanks,
Kees
Code:
function TOAuth1SignatureMethod_RSA_SHA1.Hash_HMAC_SHA1(const AData, AKey: string): string;
var
buffer: TBytes;
clean_key: string;
mdLength: TIdC_UInt;
mdctx: EVP_MD_CTX;
outbuf: TBytes;
KeyBuffer: pBIO;
FRSA: PRSA;
md: PEVP_MD;
rc: Integer;
key: EVP_PKEY;
p: Integer;
begin
if not IdSSLOpenSSL.LoadOpenSSLLibrary then
raise Exception.Create('LoadOpenSSLLibrary failed');
// Load private key (key must include header and footer)
//-----BEGIN PRIVATE KEY-----
// ....
// ....
//-----END PRIVATE KEY-----
p := AKey.IndexOf('-----END PRIVATE KEY-----');
if p < 0 then
raise Exception.Create('Private key error');
clean_key := AKey.Substring(0, p + Length('-----END PRIVATE KEY-----'));
buffer := TEncoding.ANSI.GetBytes(clean_key);
KeyBuffer := BIO_new_mem_buf(buffer, Length(buffer));
if KeyBuffer = nil then
raise Exception.Create('RSA out of memory');
try
FRSA := PEM_read_bio_RSAPrivateKey(KeyBuffer, nil, nil, nil);
if not Assigned(FRSA) then
raise Exception.Create('Private key error');
finally
BIO_free(KeyBuffer);
end;
md := EVP_get_digestbyname('SHA1');
rc := EVP_DigestInit(@mdctx, md);
if rc <> 1 then
raise Exception.Create('EVP_DigestInit failed: ' + rc.ToString);
rc := EVP_PKEY_set1_RSA(@key, FRSA); <== Exception occurs here
if rc <> 1 then
raise Exception.Create('EVP_PKEY_set1_RSA failed: ' + rc.ToString);
rc := EVP_DigestSignInit(@mdctx, nil, md, nil, @key);
if rc <> 1 then
raise Exception.Create('EVP_DigestSignInit failed: ' + rc.ToString);
buffer := TEncoding.ANSI.GetBytes(AData);
rc := EVP_DigestSignUpdate(@mdctx, buffer, Length(buffer));
if rc <> 1 then
raise Exception.Create('EVP_DigestSignUpdate failed: ' + rc.ToString);
rc := EVP_DigestSignFinal(@mdctx, nil, @mdLength);
if rc <> 1 then
raise Exception.Create('EVP_DigestFinal failed: ' + rc.ToString);
SetLength(outbuf, mdLength);
rc := EVP_DigestSignFinal(@mdctx, PIdAnsiChar(@outbuf[0]), @mdLength);
if rc <> 1 then
raise Exception.Create('EVP_DigestFinal failed: ' + rc.ToString);
//EVP_Cleanup();
Result := TNetEncoding.Base64.EncodeBytesToString(outbuf);
Result := Result.Replace(#13#10, '', [rfReplaceAll]);
end;