4

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

我使用Bouncy Castle C# Crypto API在简化推送通知的证书和私钥的管理方面取得了成功,基本上消除了我们所有推送通知证书的钥匙串的需要

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

不幸的是,Mac 钥匙串不喜欢我生成的 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 生成的 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”属性,但证书不包含。

如果我谷歌 "1.2.840.113549.1.9.21",我发现这个 OID 表示 PKCS_12_LOCAL_KEY_ID。我唯一的理论是钥匙串依靠它来匹配证书和私钥,而我生成的文件没有这个,所以它失败了。

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

所以我很困惑。也许这个属性是 Bouncy Castle API 中的一个小故障?也许我做错了什么。也许钥匙串对传入的 p12 文件有荒谬的非标准要求。无论如何,我们将不胜感激任何可以提供的帮助。

4

1 回答 1

1

BouncyCastle 的 Pkcs12Store 负责为您设置友好名称和本地密钥 ID 属性(或者至少在 2011 年 4 月左右的 1.7 版本中这样做)。我的猜测是,您一定使用了旧版本,而这不起作用。

以下是我将 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 本身生成的证书和密钥一样。

附带说明一下,似乎 Pkcs12Store 使用证书的公钥来生成证书和密钥条目共享的 LocalKeyId 属性的值。

您可以在此处查看Pkcs12Store 源代码的相关部分。

于 2012-03-28T20:40:29.753 回答