获取Apple Keychain以识别Bouncy Castle .NET创建的PKCS12(.p12)商店

时间:2011-02-09 16:08:57

标签: c# ios bouncycastle keychain

我们的组织为多个客户管理稳定的iOS应用程序,这意味着处理许多不同的开发者身份证书和推送通知证书。

我已成功使用Bouncy Castle C# Crypto API简化推送通知的证书和私钥管理essentially eliminating the need for the Keychain for all our push notification certificates

我想将此扩展到开发人员身份证书。目标是将每个开发者身份的所有私钥和证书信息存储在数据库中。然后,当需要配置新的开发人员或构建计算机时,服务器端代码可以将所有证书和私钥包装到一个p12存档中,其中一个密码可以导入到目标Mac的钥匙串中。

不幸的是,Mac Keychain不喜欢我正在生成的p12文件。这很烦人,因为我可以成功地将这些文件导入Windows证书管理器。

我正在使用的代码(重要部分)如下所示:

private byte[] GetP12Bytes(List<DevIdentity> identities, string password)
{
    Pkcs12Store store = new Pkcs12Store();

    foreach(DevIdentity ident in identities)
    {
        // Easiest to create a Bouncy Castle cert by converting from .NET
        var dotNetCert = new X509Certificate2(ident.CertificateBytes);
        // This method (not shown) parses the CN= attribute out of the cert's distinguished name
        string friendlyName = GetFriendlyName(dotNetCert.Subject); 

        // Now reconstitute the private key from saved value strings
        BigInteger modulus = new BigInteger(ident.PrivateKey.Modulus);
        BigInteger publicExponent = new BigInteger(ident.PrivateKey.PublicExponent);
        BigInteger privateExponent = new BigInteger(ident.PrivateKey.Exponent);
        BigInteger p = new BigInteger(ident.PrivateKey.P);
        BigInteger q = new BigInteger(ident.PrivateKey.Q);
        BigInteger dP = new BigInteger(ident.PrivateKey.DP);
        BigInteger dQ = new BigInteger(ident.PrivateKey.DQ);
        BigInteger qInv = new BigInteger(ident.PrivateKey.QInv);
        RsaKeyParameters kp = new RsaPrivateCrtKeyParameters(modulus, publicExponent, privateExponent, p, q, dP, dQ, qInv);
        AsymmetricKeyEntry privateKey = new AsymmetricKeyEntry(kp);

        // Now let's convert to a Bouncy Castle cert and wrap it for packaging
        Org.BouncyCastle.X509.X509Certificate cert = DotNetUtilities.FromX509Certificate(dotNetCert);
        X509CertificateEntry certEntry = new X509CertificateEntry(cert);

        // Set the private key and certificate into the store
        store.SetCertificateEntry(friendlyName, certEntry);
        store.SetKeyEntry(ident.PrivateKeyName, privateKey, new X509CertificateEntry[] { certEntry });
    }

    using (MemoryStream ms = new MemoryStream())
    {
        store.Save(ms, password.ToCharArray(), new SecureRandom());
        ms.Flush();
        byte[] p12Bytes = ms.ToArray();
        return p12Bytes;
    }
}

就像我说的,这适用于在Windows上导入,但在导入Mac Keychain时失败并出现非常一般的错误。

在加载Keychain生成的p12和我自己生成的p12文件时,我可以看到一个主要区别,但我不知道这是否是原因。

如果我将Mac Keychain生成的p12加载到Bouncy Castle PKCS12Store中,然后检查密钥,则在Keychain p12上,证书和私钥都具有密钥为“1.2.840.113549.1.9.21”的属性具有等效值(DerOctetString,其值为#af8a1d6891efeb32756c12b7bdd96b5ec673e11e)。

如果我对生成的p12文件执行相同操作,则私钥包含“1.2.840.113549.1.9.21”属性,但证书不包含。

如果我Google "1.2.840.113549.1.9.21",我find out that this OID means PKCS_12_LOCAL_KEY_ID。我唯一的理论是Keychain依赖于此来匹配证书和私钥,而我生成的文件没有这个,所以它失败了。

但是,我尝试将这些值添加到Hashtable,然后使用带有属性哈希表的CertificateEntry构造函数。如果我这样做,然后保存字节,然后重新加载字节,那么该属性将再次丢失。

所以我很沮丧。也许这个属性是Bouncy Castle API的一个小故障?也许有些事我做错了。也许Keychain对传入的p12文件有非常荒谬的非标准要求。无论如何,我们将非常感谢您提供的任何帮助。

1 个答案:

答案 0 :(得分:1)

BouncyCastle的Pkcs12Store负责为您设置Friendly Name和Local Key ID属性(或者至少在1.7版本中,大约在2011年4月)。我的猜测是你必须使用旧的版本才能使用它。

以下是我如何将iPhone开发人员身份保存到Pkcs12Store实例(省略额外资料和安全性):

var store = new Pkcs12Store();

// pairs is IEnumerable<Tuple<X509Certificate, AsymmetricKeyParameter>>
foreach (var pair in pairs)
{
    var cn = pair.Item1.SubjectDN
         .GetValueList(X509Name.CN).OfType<string>().Single();

    var certEntry = new X509CertificateEntry(pair.Item1);
    store.SetCertificateEntry(cn, certEntry);

    var keyEntry = new AsymmetricKeyEntry(pair.Item2);
    store.SetKeyEntry("Developer Name", keyEntry, new[] { certEntry });
}

store.Save(stream, string.Empty.ToArray(), new SecureRandom());

在OS X 10.7上的Keychain Access.app中导入商店正确地将证书和私钥放在钥匙串中,并将证书放在UI中的私钥内,就像Keychain Access本身生成的证书和密钥一样。 / p>

另外,Pkcs12Store似乎使用证书的公钥来生成证书和密钥条目共享的LocalKeyId属性的值。

您可以看到Pkcs12Store来源here的相关部分。