在为Identity Server 4生成自签名证书时出现“error:23076071:PKCS12 routines:PKCS12_parse:mac verify failure”错误。

17

我们正在Kubernetes上开发微服务应用程序。 其中一个微服务是IdentityServer实例。 最初,我想在Docker上本地测试解决方案,以确保它能正常运行。 为此,我想将证书复制到appsettings.json中。 最终,此值将由Kubernetes secret替换。 在我的启动类中,这是我尝试加载证书的方式:

services.AddIdentityServer()
     .AddSigningCredential(GetIdentityServerCertificate())
     .AddConfigurationStore(...


    private X509Certificate2 GetIdentityServerCertificate()
    {
        var clientSecret = Configuration["Certificate"];
        var pfxBytes = Convert.FromBase64String(clientSecret);
        var certificate = new X509Certificate2(pfxBytes, "PasswordHere");
        return certificate;
    }

证书是由我使用 OpenSSL 生成的:

openssl req –newkey rsa:2048 –nodes –keyout XXXXX.key –x509 –days 365 –out XXXXX.cer

openssl pkcs12 –exportin XXXX.cer –inkey XXXX.key –out XXXX.pfx

然后我使用以下代码获取证书:

$pfxFilePath = 'C:\XXXX.pfx'
$pwd = 'PasswordHere'
$flag = [System.Security.Cryptography.X509Certificates.X509KeyStorageFlags]::Exportable
$collection = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2Collection
$collection.Import($pfxFilePath, $pwd, $flag)
$pkcs12ContentType = [System.Security.Cryptography.X509Certificates.X509ContentType]::Pkcs12
$clearBytes = $collection.Export($pkcs12ContentType)
$fileContentEncoded = [System.Convert]::ToBase64String($clearBytes)

我获取$fileContentEncoded的值并将其粘贴到appsettings.json中。

当我进行调试时,尝试使用上述方法创建X509Certificate2对象时,结果为:error:23076071:PKCS12 routines:PKCS12_parse:mac verify failure


21
密码错误会导致“mac verify failure”错误。使用“openssl pkcs12 -in XXXX.pfx”命令检查密钥的密码。请注意,翻译保持原意并尽可能简洁易懂,不包含额外解释和其他输出。 - dinidu
1个回答

4

错误的 MAC 验证失败通常是由于密码错误引起的。请检查密钥的密码。

openssl pkcs12 -in XXXX.pfx

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接