c# - 无法从 macOS 上的资源文件加载 .pem
问题描述
我有一个从资源文件加载 pem 证书的简单方法:
/// <summary>
/// Helper method to load a .pem and return it as a X509Certificate2
/// </summary>
private static X509Certificate2 GetX509Certificate2(string path)
{
using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(path))
{
byte[] data = new byte[stream.Length];
stream.Read(data, 0, (int)stream.Length);
return new X509Certificate2(data);
}
}
它在 Windows 和 Ubuntu 上运行良好,但在 macOS 上失败,原因如下:
Interop+AppleCrypto+AppleCommonCryptoCryptoCryptographicException:导入中的未知格式。
我尝试使用Span<byte>
重载,但收效甚微。
有没有人有任何想法/解决方法?
谢谢
解决方案
我在这里向 corefx 团队发布了一张票(我现在已经关闭):https ://github.com/dotnet/corefx/issues/35163
异常是由实际包含证书链的文件引起的(它包含多个-----END CERTIFICATE-----
标记) - 底层互操作在 macOS 上不支持这一点。
显然,Windows 也只使用它遇到的第一个,因此解决方案只是从流中读取足够的字节,直到该标记被命中(尽管没有理由不能将整个链提取为 的集合X509Certificate2
) .
奇怪的是,我在程序集中拥有的文件(包含证书链)与我上面链接到的文件不同......我向@bartonjs 表示感谢和道歉,因为这没有帮助!
推荐阅读
- c# - 在 LINQ 查询中添加 Lambda 以替换 foreach 中的一行
- javascript - 从 Cheerio 抓取 Youtube 数据
- c++ - 堆分配对象的设置器
- php - curl 7.22 中的 PHP new CURLFile 不起作用,为什么?
- python - 来自另一个文件夹的 Python 3 模块导入错误
- java - Java线程连接()
- search - 按标题响应搜索 Google 结果
- javascript - 在 Gatsby / React 中,导航到页面时无法滚动,直到重新加载(在移动设备上)
- docker-compose - 如何让 RubyMine 使用已经运行的 docker-compose 服务,而不是尝试启动它?
- php - 循环遍历 Woocommerce 购物车项目以获取跟踪代码