使用BouncyCastle和外部Azure KeyVault(HSM)密钥创建证书时出现无效签名

11

我正在尝试生成一个由存储在Azure KeyVault中的KeyPair自签名的证书。

我的最终结果是一个带有无效签名的证书:

输入图像描述

生成证书参数:

     DateTime startDate = DateTime.Now.AddDays(-30);
     DateTime expiryDate = startDate.AddYears(100);

     BigInteger serialNumber = new BigInteger(32, new Random());
     X509V1CertificateGenerator certGen = new X509V1CertificateGenerator();

     X509Name selfSignedCA = new X509Name("CN=Test Root CA");

     certGen.SetSerialNumber(serialNumber);
     certGen.SetIssuerDN(selfSignedCA); //Self Signed
     certGen.SetNotBefore(startDate);
     certGen.SetNotAfter(expiryDate);
     certGen.SetSubjectDN(selfSignedCA);
      

获取指向 Azure KeyVault 存储密钥(类似 HSM 服务)的引用:


    //Create a client connector to Azure KeyVault
    var keyClient = new Azure.Security.KeyVault.Keys.KeyClient(
         vaultUri: new Uri("https://xxxx.vault.azure.net/"),
         credential: new ClientSecretCredential(
             tenantId: "xxxx", //Active Directory
             clientId: "xxxx", //Application id?
             clientSecret: "xxxx"
             )
         );

        var x = keyClient.GetKey("key-new-ec"); //Fetch the reference to the key

成功检索到密钥。 接下来,我尝试使用密钥的公共数据生成ECPublicKeyParameters对象:

    X9ECParameters x9 = ECNamedCurveTable.GetByName("P-256");
    Org.BouncyCastle.Math.EC.ECCurve curve = x9.Curve;

    var ecPoint = curve.CreatePoint(new Org.BouncyCastle.Math.BigInteger(1, x.Value.Key.X), new Org.BouncyCastle.Math.BigInteger(1, x.Value.Key.Y));
    ECDomainParameters dParams = new ECDomainParameters(curve, ecPoint, x9.N);
    ECPublicKeyParameters pubKey = new ECPublicKeyParameters(ecPoint, dParams);

    certGen.SetPublicKey(pubKey); //Setting the certificate's public key with the fetched one

下一步是生成由该密钥签名的证书。我实现了一个新的ISignatureFactory对象,应该使用KeyVault的外部签名函数进行签名:

      AzureKeyVaultSignatureFactory customSignatureFactory = new AzureKeyVaultSignatureFactory(1);
      Org.BouncyCastle.X509.X509Certificate cert = certGen.Generate(customSignatureFactory);

这是我的自定义AzureKeyVaultSignatureFactory

public class AzureKeyVaultSignatureFactory : ISignatureFactory
{
    private readonly int _keyHandle;

    public AzureKeyVaultSignatureFactory(int keyHandle)
    {
        this._keyHandle = keyHandle;
    }

    public IStreamCalculator CreateCalculator()
    {
        var sig = new CustomAzureKeyVaultDigestSigner(this._keyHandle);

        sig.Init(true, null);

        return new DefaultSignatureCalculator(sig);
    }

    internal class CustomAzureKeyVaultDigestSigner : ISigner
    {
        private readonly int _keyHandle;
        private byte[] _input;

        public CustomAzureKeyVaultDigestSigner(int keyHandle)
        {
            this._keyHandle = keyHandle;
        }

        public void Init(bool forSigning, ICipherParameters parameters)
        {
            this.Reset();
        }

        public void Update(byte input)
        {
            return;
        }

        public void BlockUpdate(byte[] input, int inOff, int length)
        {
            this._input = input.Skip(inOff).Take(length).ToArray();
        }

        public byte[] GenerateSignature()
        {
            //Crypto Client (Specific Key)
            try
            {

                //Crypto Client (Specific Key)
                CryptographyClient identitiesCAKey_cryptoClient = new CryptographyClient(
                    keyId: new Uri("https://xxxx.vault.azure.net/keys/key-new-ec/xxxx"),
                    credential: new ClientSecretCredential(

                          tenantId: "xxxx", //Active Directory
                          clientId: "xxxx", //Application id?
                          clientSecret: "xxxx"
                          )
                );

                SignResult signResult = identitiesCAKey_cryptoClient.SignData(SignatureAlgorithm.ES256, this._input);
                return signResult.Signature;


            }
            catch (Exception ex)
            {

                throw ex;
            }

            return null;
        }

        public bool VerifySignature(byte[] signature)
        {
            return false;
        }

        public void Reset() { }

        public string AlgorithmName => "SHA-256withECDSA";
    }

    public object AlgorithmDetails => new AlgorithmIdentifier(X9ObjectIdentifiers.ECDsaWithSha256, DerNull.Instance);
}

然后我将证书转换并写入文件:

 //convert to windows type 2 and get Base64 
 X509Certificate2 cert2 = new X509Certificate2(DotNetUtilities.ToX509Certificate(cert));
 byte[] encoded = cert2.GetRawCertData();
 string certOutString = Convert.ToBase64String(encoded);
 System.IO.File.WriteAllBytes(@"test-signed2.cer", encoded); //-this is good!

我做错了什么?也许只用X/Y构建ECCurve还不够吗?

谢谢!


哇,对于一个简单的ecdsa签名来说,这太复杂了。有一件事是确定的,你的签名算法不是public string AlgorithmName => "SHA-256withRSA"; - President James K. Polk
这是因为签名是在外部进行的,而且显然这不是一种常见做法。如果Azure有PKI服务就太好了,但他们没有。谢谢,我会查看算法名称。 - NOP-MOV
已更改为SHA-256withECDSA,但仍然出现相同的错误。 - NOP-MOV
@mnistic BYOK?那是什么?只是一个简单的自签名 ECDSA with SHA256 证书。我不知道,这是我问题的一部分... - NOP-MOV
你是按照那些指示自己生成了这个密钥吗? - mnistic
显示剩余4条评论
1个回答

2
问题在于密钥库返回的签名格式为“原始”(64字节)格式,其中前32个字节是R,后32个字节是S。为了在Bouncy Castle中正常运行,您的GenerateSignature方法需要返回一个ASN.1格式的字节数组,最终大小将在70到72字节之间。
您可以在网上查找这个实际意义,但您需要:
  1. 为结果创建一个新的字节数组
  2. 最初将来自密钥库的输出拆分为两个32位数组RS
  3. 如果RS数组的第0个元素具有高MSB,则需要在相应数组的开头插入0(否则不执行任何操作,数组长度保持为32个字节)。
  4. 构建必要的ASN.1标头(手动方式如下所示,也可以使用Bouncy Castle的一些库功能来创建ASN.1消息)。因此,输出的字节数组应包含
0x30
one byte containing the length of the rest of the array*
0x02
a byte containing the length of the R array (either 32 or 33 depending on if + or -)
0x02
a byte containing the length of the S array (either 32 or 33 depending on if + or -)
the entire S array

  1. 将这个数组作为GenerateSignature的输出返回。

* 因此,整个长度将是R的长度加上S的长度再加上4个头字节(R长度、R头、S长度、S头)

我已经使用自己的密钥进行了测试,该密钥由云服务返回,该服务还返回64字节的R+S响应,并且它有效。


非常感谢,我会尝试并回报。 - NOP-MOV

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