首页 > 解决方案 > 无法从 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>重载,但收效甚微。

有没有人有任何想法/解决方法?

谢谢

标签: c#.net-coressl-certificate

解决方案


我在这里向 corefx 团队发布了一张票(我现在已经关闭):https ://github.com/dotnet/corefx/issues/35163

异常是由实际包含证书链的文件引起的(它包含多个-----END CERTIFICATE-----标记) - 底层互操作在 macOS 上不支持这一点。

显然,Windows 也只使用它遇到的第一个,因此解决方案只是从流中读取足够的字节,直到该标记被命中(尽管没有理由不能将整个链提取为 的集合X509Certificate2) .

奇怪的是,我在程序集中拥有的文件(包含证书链)与我上面链接到的文件不同......我向@bartonjs 表示感谢和道歉,因为这没有帮助!


推荐阅读