我正在尝试在我的.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之间的结果并不相同。有人可以帮我吗?

提前致谢!

最佳答案

我终于完成了这项工作!

首先,您必须去这里,并安装apple ROOT证书:

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

(这是第一个)。将其安装到您受信任的根权限中。 Mac已经有了这个。

其次,在开发人员门户中将开发者证书安装为受信任的根证书颁发机构,您可以在其中添加设备,创建密码包密钥以及所有这些

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

(您需要为此登录)

那么您需要在开发门户中生成密码包密钥,下载它,将其安装在您的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中!

10-05 20:29
查看更多