gpt4 book ai didi

c# - 让 Apple Keychain 识别 Bouncy CaSTLe .NET 创建的 PKCS12 (.p12) 存储

转载 作者:可可西里 更新时间:2023-11-01 06:12:44 27 4
gpt4 key购买 nike

我们的组织为多个客户管理稳定的 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 中,然后检查 key ,在 Keychain p12 上,证书和私钥都具有 key 为“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)依靠它来匹配证书和私钥,而我生成的文件没有这个,所以它失败了。

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

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

最佳答案

BouncyCaSTLe 的 Pkcs12Store 负责为您设置 Friendly Name 和 Local Key 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 中导入商店正确地将证书和私钥放入钥匙串(keychain)中,并将证书放入 UI 中的私钥中,就像 Keychain Access 本身生成的证书和 key 一样。

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

可以查看Pkcs12Store源码的相关部分here .

关于c# - 让 Apple Keychain 识别 Bouncy CaSTLe .NET 创建的 PKCS12 (.p12) 存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4947419/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com