如何从Objective-C中的.p12生成ds:X509证书值?
我有一个私钥文件.p12如何从Objective-C中的.p12生成ds:X509证书值?,objective-c,xml,openssl,digital-signature,xml-signature,Objective C,Xml,Openssl,Digital Signature,Xml Signature,我有一个私钥文件.p12 我正在使用Objective-C。 我需要生成签名的数字签名XML,如下所示: 问题是,我无法找到如何在中生成值。我知道这是一个基于64编码的公钥。但是,在测试了各种编码方法之后,我无法得到相同的字符串 需要你的帮助 我自己找到了解决办法。这太容易了。 使用苹果的安全框架: NSString* pfile = [[NSBundle mainBundle] pathForResource:@"rsa_user" ofType:@"p12" ]; // Load C
我正在使用Objective-C。
我需要生成签名的数字签名XML,如下所示:
问题是,我无法找到如何在
中生成值。我知道这是一个基于64编码的公钥。但是,在测试了各种编码方法之后,我无法得到相同的字符串需要你的帮助 我自己找到了解决办法。这太容易了。
使用苹果的安全框架:
NSString* pfile = [[NSBundle mainBundle] pathForResource:@"rsa_user" ofType:@"p12" ];
// Load Certificate
NSData *p12data = [NSData dataWithContentsOfFile:pfile];
CFDataRef inP12data = (__bridge CFDataRef)p12data;
SecIdentityRef myIdentity;
SecTrustRef myTrust;
extractIdentityAndTrust(inP12data, &myIdentity, &myTrust);
SecCertificateRef myCertificate;
SecIdentityCopyCertificate(myIdentity, &myCertificate);
NSData *certificateData = (__bridge NSData *) SecCertificateCopyData(myCertificate);
// Output certificate base64 value. (This value goes to <ds:X509Certificate> tag )
NSLog(@"%@", [certificateData base64EncodedString]);
// Helper function
OSStatus extractIdentityAndTrust(CFDataRef inP12data, SecIdentityRef *identity, SecTrustRef *trust)
{
OSStatus securityError = errSecSuccess;
CFStringRef password = CFSTR("password");
const void *keys[] = { kSecImportExportPassphrase };
const void *values[] = { password };
CFDictionaryRef options = CFDictionaryCreate(NULL, keys, values, 1, NULL, NULL);
CFArrayRef items = CFArrayCreate(NULL, 0, 0, NULL);
securityError = SecPKCS12Import(inP12data, options, &items);
if (securityError == 0) {
CFDictionaryRef myIdentityAndTrust = CFArrayGetValueAtIndex(items, 0);
const void *tempIdentity = NULL;
tempIdentity = CFDictionaryGetValue(myIdentityAndTrust, kSecImportItemIdentity);
*identity = (SecIdentityRef)tempIdentity;
const void *tempTrust = NULL;
tempTrust = CFDictionaryGetValue(myIdentityAndTrust, kSecImportItemTrust);
*trust = (SecTrustRef)tempTrust;
}
if (options) {
CFRelease(options);
}
return securityError;
}
NSString*pfile=[[NSBundle mainBundle]pathForResource:@“p12”类型的“rsa_用户”;
//装载证书
NSData*p12data=[NSData DATA WITH CONTENTS OFFILE:pfile];
CFDataRef inP12data=(uu桥CFDataRef)p12data;
SecIdentityRef myIdentity;
我的信托;
提取标识和信任(inP12data、&myIdentity、&myTrust);
菌丝体的二级证书;
SecIdentityCopyCertificate(myIdentity和myCertificate);
NSData*certificateData=(uuu桥NSData*)SecCertificateCopyData(myCertificate);
//输出证书base64值。(此值转到标记)
NSLog(@“%@,[Certificatedatabase64EncodedString]);
//辅助函数
OSStatus ExtractIdentity和dTrust(CFDataRef inP12data,SecIdentityRef*identity,SecTrustRef*trust)
{
OSStatus securityError=errSecSuccess;
CFStringRef password=CFSTR(“密码”);
常量void*键[]={kSecImportExportPassphrase};
const void*值[]={password};
CFDictionaryRef options=CFDictionaryCreate(NULL,键,值,1,NULL,NULL);
CFArrayRef items=CFArrayCreate(NULL,0,0,NULL);
securityError=SecPKCS12Import(inp12数据、选项和项目);
if(securityError==0){
CFDictionaryRef myIdentityAndTrust=CFArrayGetValueAtIndex(项目,0);
常量void*tempIdentity=NULL;
tempIdentity=CFDictionaryGetValue(MyIdentity和Dtrust,kSecImportItemIdentity);
*identity=(SecIdentityRef)tempeidentity;
const void*testrust=NULL;
试探信任=CFDictionaryGetValue(MyIdentity和Dtrust,kSecImportItemTrust);
*信任=(SecTrustRef)trust;
}
如果(选项){
CFR发布(选项);
}
返回安全性错误;
}
X509证书是公钥+证书中的所有其他部分。您是在使用X509证书中的证书还是其他什么?@pd40我只有.p12;我已经成功地从it中提取了证书和密钥。您好,您能在这里发布生成xml数字签名的所有步骤吗?@Christian我使用私有库来完成这项工作。实现了特定的加密提供者。所以我认为这取决于你到底需要什么。我在SO等网站上看到了许多实施和共享的示例。你能告诉我你使用的是什么图书馆吗?@Christian这是一个地方政府创建的图书馆,我们通过发送一封官方电子邮件请求获得了它。我认为标准实现也应该起作用。