在 Mono 上使用 X509Certificate2 - 同时加载公钥和私钥?

Using X509Certificate2 on Mono - loading with both public and private key?(在 Mono 上使用 X509Certificate2 - 同时加载公钥和私钥?)
本文介绍了在 Mono 上使用 X509Certificate2 - 同时加载公钥和私钥?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在,我尝试像这样实例化 X509Certificate2:

Right now, I try instantiating an X509Certificate2 like this:

cert = new X509Certificate2(Resources.cred);  

其中 Resources.cred 是代表 .pfx 文件的 byte[].
这在 Windows/.NET 上工作得非常好.

Where Resources.cred is a byte[] representing a .pfx file.
This works absolutely fine on Windows/.NET.

但是,在 Mono JIT 编译器版本 3.2.8 (Debian 3.2.8+dfsg-4ubuntu1)(Ubuntu Server 14.04 LTS 上的 Mono)下运行相同的代码,我得到以下异常:

However, running the same code under Mono JIT compiler version 3.2.8 (Debian 3.2.8+dfsg-4ubuntu1) (Mono on Ubuntu Server 14.04 LTS), I get the following exception:

System.TypeInitializationException: An exception was thrown by the type initializer for <snipped irrelevant type name> ---> System.Security.Cryptography.CryptographicException: Unable to decode certificate. ---> System.Security.Cryptography.CryptographicException: Input data cannot be coded as a valid certificate. ---> System.Security.Cryptography.CryptographicException: Input data cannot be coded as a valid certificate.
  at Mono.Security.X509.X509Certificate.Parse (System.Byte[] data) [0x00000] in <filename unknown>:0
  --- End of inner exception stack trace ---
  at Mono.Security.X509.X509Certificate.Parse (System.Byte[] data) [0x00000] in <filename unknown>:0
  at Mono.Security.X509.X509Certificate..ctor (System.Byte[] data) [0x00000] in <filename unknown>:0
  at System.Security.Cryptography.X509Certificates.X509Certificate2.Import (System.Byte[] rawData, System.String password, X509KeyStorageFlags keyStorageFlags) [0x00000] in <filename unknown>:0
  --- End of inner exception stack trace ---
  at System.Security.Cryptography.X509Certificates.X509Certificate2.Import (System.Byte[] rawData, System.String password, X509KeyStorageFlags keyStorageFlags) [0x00000] in <filename unknown>:0
  at System.Security.Cryptography.X509Certificates.X509Certificate2..ctor (System.Byte[] rawData) [0x00000] in <filename unknown>:0
  --- End of relevant stack trace ---  

如果有关系,这个证书是用我自己的 CA 签名的,并用于原始 RSA.

Should it matter, this certificated is signed with my own CA, and is used in raw RSA.

我有可用于此证书的 .pfx.cer.pvk 文件.
我必须如何继续在 Mono 下使用私钥加载此证书?

I have the .pfx, .cer and .pvk files available for this certificate.
How must I proceed to load this certificate with the private key under Mono?

推荐答案

这个构造函数抛出异常:

This constructor throws an exception:

byte[] pkcs12 = ...;
X509Certificate2 cert = X509Certificate2(pkcs12);

这个构造函数有效:

byte[] pkcs12 = ...;
X509Certificate2 cert = X509Certificate2(pkcs12, string.Empty);

这似乎是一个错误,所以我将修复它并将补丁发送给上游开发人员.我会告诉你进展的.

This seems to be a bug so I am going to fix it and send patch to the upstream developers. I will let you know of the progress.

这篇关于在 Mono 上使用 X509Certificate2 - 同时加载公钥和私钥?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)