m_safeCertContext 是无效句柄

发布于 2024-07-11 23:12:07 字数 926 浏览 11 评论 0原文

我一直在努力解决一个问题,也许你们可以为我指明正确的方向。

我正在尝试通过 https 连接在网络服务器上对 pdf 进行数字签名。

在页面加载时,我这样做:

HttpClientCertificate cs = Request.ClientCertificate;
X509Certificate card = new X509Certificate(cs.Certificate);
Org.BouncyCastle.X509.X509CertificateParser cp = new Org.BouncyCastle.X509.X509CertificateParser();
 Org.BouncyCastle.X509.X509Certificate[] chain = new Org.BouncyCastle.X509.X509Certificate[] { cp.ReadCertificate(card.GetRawCertData())};

我在最后一行代码中收到错误“m_safeCertContext 是无效句柄”。

请注意:

  • 我使用两个完全不同的证书遇到相同的错误。
  • 证书正在被检索到“card”变量中。
  • 我曾经将卡获取到 X509Certificate2,但我昨天在某处读到,我无法发现可以通过转换为 X509Certificate 然后向下转换为 X509Certificate2 来解决该错误。 这是那些“嗯......这没有任何意义,但我还没有尝试过”的时刻之一。
  • 我尝试将 [System.Security.SecurityCritical, System.Security.SecurityTreatAsSafe] 属性添加到所有方法甚至类中,以查看它是否有效......没有这样的运气。

谁能给我一个提示吗?

I've been wrestling with a problem, maybe you guys can point me in the right direction.

I'm trying to digitally sign a pdf, on the webserver, over an https connection.

At page load i'm doing as so:

HttpClientCertificate cs = Request.ClientCertificate;
X509Certificate card = new X509Certificate(cs.Certificate);
Org.BouncyCastle.X509.X509CertificateParser cp = new Org.BouncyCastle.X509.X509CertificateParser();
 Org.BouncyCastle.X509.X509Certificate[] chain = new Org.BouncyCastle.X509.X509Certificate[] { cp.ReadCertificate(card.GetRawCertData())};

I'm getting the error "m_safeCertContext is an invalid handle" at that last line of code.

Please note that:

  • I am getting the same error using 2 completely different certificates.
  • The certificate is being retrieved to the "card" variable ok.
  • I used to get the card to X509Certificate2 but i read yesterday somewhere I'm not being able to find that the error could be solved by casting as a X509Certificate and then downcasting to X509Certificate2. It was one of those "well... this does not makes any sense but i havent tried it yet" moments.
  • I have tried to add [System.Security.SecurityCritical, System.Security.SecurityTreatAsSafe] property to all methods and even the class to see if it would work... no such luck.

Can anyone one give me a hint?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

独夜无伴 2024-07-18 23:12:07

每当您访问密码学中未初始化的字段时,都可能发生这种情况。

在您的代码中,如果 Request.ClientCertificate 返回一个没有原始证书数据的对象,那么当您在第四行调用 card.GetRawCertData() 时,您将看到错误。

作为一个简单的测试,请尝试以下操作:

var cert = new System.Security.Cryptography.X509Certificates.X509Certificate2();
Console.WriteLine(cert.Thumbprint);

这将引发以下异常,因为没有可用的指纹:

m_safeCertContext is an invalid handle.

使用给定的堆栈跟踪:

at System.Security.Cryptography.X509Certificates.X509Certificate.ThrowIfContextInvalid()
at System.Security.Cryptography.X509Certificates.X509Certificate.SetThumbprint()
at System.Security.Cryptography.X509Certificates.X509Certificate.GetCertHashString()
at System.Security.Cryptography.X509Certificates.X509Certificate2.get_Thumbprint()
at MyEncryptionUtility.EncryptionUtilityForm.button1_Click(Object sender, EventArgs e) in C:\MyEncryptionUtility\EncryptionUtilityForm.cs:line 2864

This can happen any time you access uninitialized fields in cryptography.

In your code, if Request.ClientCertificate returns an object with no raw certificate data then you will see the error when you call card.GetRawCertData() on your fourth line.

As a simple test, try the following:

var cert = new System.Security.Cryptography.X509Certificates.X509Certificate2();
Console.WriteLine(cert.Thumbprint);

This will throw the following exception because there is no thumbprint available:

m_safeCertContext is an invalid handle.

with the given stack trace:

at System.Security.Cryptography.X509Certificates.X509Certificate.ThrowIfContextInvalid()
at System.Security.Cryptography.X509Certificates.X509Certificate.SetThumbprint()
at System.Security.Cryptography.X509Certificates.X509Certificate.GetCertHashString()
at System.Security.Cryptography.X509Certificates.X509Certificate2.get_Thumbprint()
at MyEncryptionUtility.EncryptionUtilityForm.button1_Click(Object sender, EventArgs e) in C:\MyEncryptionUtility\EncryptionUtilityForm.cs:line 2864
深者入戏 2024-07-18 23:12:07

看起来这不是您的问题,但对于其他人来说:请确保在尝试访问任何与证书相关的属性或方法之前不要调用 X509Certificate2.Reset() 。

Looks like this is not your problem, but for others: make sure you don't call X509Certificate2.Reset() before trying to access any certificate related properties or methods.

蓦然回首 2024-07-18 23:12:07
 public bool ReadCertFromSignedFile(X509Certificate2 cert, string filename)
    {
        if (!string.IsNullOrWhiteSpace(filename) && File.Exists(filename))
        {
            var cert509 = X509Certificate.CreateFromSignedFile(filename);
            cert = new X509Certificate2(cert509.GetRawCertData());

            return CheckSertificate(cert);
        }
        else
        { throw new Exception("Сертификат не заполнен"); }
    }

从另一个代码调用方法,例如

   if (_digitalSignatureService.ReadCertFromSignedFile(fileCert, file.SignFilePath))
                 {
                    if (!cert.Equals(fileCert))
                    {

Equals - 调用错误“m_safeCertContext 是无效句柄。” 因为 X509Certificate 不存在

决定

 public bool ReadCertFromSignedFile(X509Certificate2 cert, string filename)
    {
        if (!string.IsNullOrWhiteSpace(filename) && File.Exists(filename))
        {
            var cert509 = X509Certificate.CreateFromSignedFile(filename);

            cert.Import(cert509.GetRawCertData());

此代码有效!

 public bool ReadCertFromSignedFile(X509Certificate2 cert, string filename)
    {
        if (!string.IsNullOrWhiteSpace(filename) && File.Exists(filename))
        {
            var cert509 = X509Certificate.CreateFromSignedFile(filename);
            cert = new X509Certificate2(cert509.GetRawCertData());

            return CheckSertificate(cert);
        }
        else
        { throw new Exception("Сертификат не заполнен"); }
    }

method calling from another code like this

   if (_digitalSignatureService.ReadCertFromSignedFile(fileCert, file.SignFilePath))
                 {
                    if (!cert.Equals(fileCert))
                    {

Equals - calling error "m_safeCertContext is an invalid handle." because X509Certificate not exist

decision

 public bool ReadCertFromSignedFile(X509Certificate2 cert, string filename)
    {
        if (!string.IsNullOrWhiteSpace(filename) && File.Exists(filename))
        {
            var cert509 = X509Certificate.CreateFromSignedFile(filename);

            cert.Import(cert509.GetRawCertData());

this code works!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文