c# – 获取Apple Keychain以识别Bouncy Castle .NET创建的PKCS12
我们的组织为多个客户管理稳定的iOS应用程序,这意味着处理许多不同的开发者身份证书和推送通知证书.
我已经成功使用Bouncy Castle C# Crypto API简化了推送通知的证书和私钥管理,essentially eliminating the need for the Keychain for all our push notification certificates. 我想将此扩展到开发人员身份证书.目标是将每个开发者身份的所有私钥和证书信息存储在数据库中.然后,当需要配置新的开发人员或构建计算机时,服务器端代码可以将所有证书和私钥包装到一个p12存档中,其中一个密码可以导入到目标Mac的Keychain中. 不幸的是,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”的属性(值为#af8a1d6891efeb32756c12b7bdd96b5ec673e11e的DerOctetString). 如果我对生成的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文件有非常荒谬的非标准要求.无论如何,我们将非常感谢您提供的任何帮助. 解决方法
BouncyCastle的Pkcs12Store负责为您设置Friendly Name和Local Key ID属性(或者至少在1.7版本中,大约在2011年4月).我的猜测是你必须使用旧版本才能使用.
以下是我将iPhone Developer身份保存到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源here的相关部分. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |