3

我正在尝试在我的 .Net 程序中使用 OpenSSL 代码。这是代码:

openssl pkcs12 -in "My PassKit Cert.p12" -clcerts -nokeys -out certificate.pem
openssl pkcs12 -in "My PassKit Cert.p12" -nocerts -out key.pem
smime -binary -sign -signer certificate.pem -inkey key.pem -in manifest.json -out signature -outform DER

我尝试使用 .Net OpenSSL,但我完全不知道如何使用它,也找不到合适的文档。我决定使用 .Net 来执行相同的签名过程,代码如下:

var dataToSign = System.IO.File.ReadAllBytes(filePathToSign);
ContentInfo contentInfo = new ContentInfo(dataToSign);

X509Certificate2 signerCert = new X509Certificate2(System.IO.File.ReadAllBytes(signerPfxCertPath), signerPfxCertPassword);

var signedCms = new SignedCms(contentInfo, true);
var signer = new CmsSigner(SubjectIdentifierType.IssuerAndSerialNumber, signerCert);

signer.IncludeOption = X509IncludeOption.EndCertOnly;
signedCms.ComputeSignature(signer);

var myCmsMessage = signedCms.Encode();

var buf = Encoding.Convert(Encoding.UTF7, Encoding.UTF8, myCmsMessage);

return Encoding.UTF8.GetString(buf, 0, buf.Length);

但是 C# 和 OpenSSL 之间的结果并不相同。有人可以帮我吗?

提前致谢!

4

1 回答 1

4

我最终得到了这个工作!

首先,你必须到这里,安装苹果ROOT证书:

http://www.apple.com/certificateauthority/

(这是第一个)。将其安装到您信任的根权限中。Mac 已经具备此功能。

其次,将开发者证书安装为受信任的根权限,它位于开发者门户中,您可以在其中添加设备,制作 passkit 密钥,所有这些

https://developer.apple.com/ios/manage/certificates/team/index.action

(您需要为此登录)

然后您需要在开发门户中生成您的 passkit 密钥,下载它,将其安装在您的 Mac 上,然后将其与私钥一起导出为 .p12 文件。

然后,您可以将此文件移动到 Windows 机器并使用它。在生成清单后,我使用了这段代码:

var cert = new X509Certificate2(@"path-to-your.p12", "password");

var buffer = File.ReadAllBytes(Path.Combine(basePath, "manifest.json"));

ContentInfo cont = new ContentInfo(buffer);
var cms = new SignedCms(cont, true);
var signer = new CmsSigner(SubjectIdentifierType.SubjectKeyIdentifier, cert);

signer.IncludeOption = X509IncludeOption.ExcludeRoot;

cms.ComputeSignature(signer);

var myCmsMessage = cms.Encode();


File.WriteAllBytes(Path.Combine(basePath, "signature"), myCmsMessage);

抱歉,这是相当混乱的代码,但它可以工作:)

不要忘记设置

“passTypeIdentifier”:“pass.com.yourcompany.NameOfYourPass”,“teamIdentifier”:“您的团队 ID”,

在 pass.json 中!

于 2012-07-26T14:47:34.863 回答