27

我正在尝试制作一个在 Delphi XE 中使用某些 Web 服务的程序。要连接到 Web 服务,我必须使用存储在 Windows 证书存储中的自签名证书。我使用 CertOpenSystemStore 打开证书存储,获取证书CertFindCertificateInStore并使用SSL_CTX_use_certificate. 这没问题。然后我得到公钥 blobCryptExportKey并组成这样的私钥:

function PrivKeyBlob2RSA(const AKeyBlob: PByte; const ALength: Integer; const ASSLCtx: PSSL_CTX): IdSSLOpenSSLHeaders.PEVP_PKEY;
var
  modulus: PByte;
  bh: PBLOBHEADER;
  rp: PRSAPUBKEY;
  rsa_modlen: DWORD;
  rsa_modulus: PAnsiChar;
  rkey: PRSA;
begin
  bh := PBLOBHEADER(AKeyBlob);
  Assert(bh^.bType = PUBLICKEYBLOB);
  rp := PRSAPUBKEY(AKeyBlob + 8);
  Assert(rp.magic = $31415352);
  rsa_modulus := PAnsiChar(Integer(Pointer(rp))+12);
  rkey := RSA_new_method(ASSLCtx.client_cert_engine);
  rkey^.References := 1;
  rkey^.e := BN_new;
  rkey^.n := BN_new;
  BN_set_word(rkey^.e, rp^.pubexp);
  rsa_modlen := (rp^.bitlen div 8) + 1;
  modulus := AllocMem(rsa_modlen);
  CopyMemory(modulus, rsa_modulus, rsa_modlen);
  RevBuffer(modulus, rsa_modlen);
  BN_bin2bn(modulus, rsa_modlen, rkey^.n);
  Result := EVP_PKEY_new;
  EVP_PKEY_assign_RSA(Result, PAnsiChar(rkey));
end;

SSL_CTX_use_PrivateKey然后我设置它SSL_CTX_check_private_key- 到目前为止没有问题。但是当数据传输开始时,我在 libeay32.dll 中遇到了访问冲突。如果我从 .pem 文件加载密钥,一切都很好。我看不出我做错了什么,请帮忙:)

这是确切的错误消息:

模块“libeay32.dll”中地址 09881C5F 的访问冲突。读取地址 00000000。

libeay32.dll 版本为 1.0.0.5。尝试使用版本 0.9.something 也有同样的错误,只是地址不同。

以下是我进入的 RSA 结构PrivKeyBlob2RSA

pad    0
version  0
meth       $898030C
engine     nil
n      $A62D508
e      $A62D4D8
d      nil
p      nil
q      nil
dmp1       nil
dmq1       nil
iqmp       nil
ex_data (nil, -1163005939 {$BAADF00D})
references  1
flags      6
_method_mod_n   nil
_method_mod_p   nil
_method_mod_q   nil
bignum_data nil {#0}
blinding    nil
mt_blinding nil

我检查了 n 和 e bignums,它们是正确的,其他一切看起来都很好。是的,调用函数时会发生错误ssl_read

4

1 回答 1

1

在我看来,您会收到这些错误的最合理原因包括:

  1. 错误版本的 OpenSSL dll (libeay32 ssleay.dll) 或声明 SSL 包装器时出错(在这种情况下,您可能需要 Indy 版本 10 升级)。

  2. 根据 Ken 的评论,已经释放了要传递给 DLL 的内存块。

  3. 您发布的代码中有一些微妙的指针取消引用错误。对 CopyMemory 的调用可能会丢失通过“PointerVariableName^”而不仅仅是“PointerVariableName”的指针间接级别。如果您不清楚,请阅读“pascal 中的无类型 var 参数和指针”。

于 2012-06-09T19:56:26.270 回答