据我所知——你有点过分了——桨被锁在了应用商店里。
- iOS 缺少您需要的 CMSEncoderAddSigners、CMSEncoderUpdateContent、CMSEncoderCopyEncodedContent。
- 使用 openssl 或 Chilkat 也不理想 - 因为 iOS 的钥匙串 API 不会让您在导入后(不再)访问私钥。
我过去用 openssl 和 Chilkat 解决了这个问题。
但是,在每种情况下,我都会“缓存”私钥的副本——因为一旦它进入钥匙串——我能得到的只是一个 SecKeyRef(你需要与苹果签订额外的协议/许可才能获得它退出并仍然在应用商店。对任何 VPN(例如瞻博网络)应用程序进行逆向工程以查看要链接的方法/框架)。
对于 openssl - 只需在 openssl 的应用程序中获取 smime.c 代码并进行修改。对于 chilkat 来说,事情要简单得多:
CkoCert * mine = [identity ckoCert];
assert([mime AddEncryptCert: mine] == YES);
for(id cc in backupCerts) {
assert([mime AddEncryptCert:cc] == YES);
}
for(id key in [headers allKeys]) {
[mime SetHeaderField:[NSString stringWithFormat:@"%s%@", X_HDR_PREFIX, key]
value:[headers objectForKey:key]
];
};
[mime SetBodyFromBinary:data];
assert([mime EncryptN] == YES);
return [mime GetMimeBytes];
并且身份字段具有“保留自己的缓存”作弊:
-(id)initWithPKCS12:(NSData*)pkcs12der password:(NSString *)password {
if (password == nil)
password = [APPSETTINGS wellKnownPkcsPassword];
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
password, kSecImportExportPassphrase,
nil];
CFArrayRef items;
OSStatus status = SecPKCS12Import((__bridge CFDataRef)pkcs12der,
(__bridge CFDictionaryRef)options, &items);
if (status != noErr) {
NSLog(@"PKCS12 importAsDer failed: Error %ld",(long)status);
...
}
if (!items || CFArrayGetCount(items) < 1) {
NSLog(@"PKCS12 importAsDer failed - nothing returned (%ld bytes DER)",
(long)[pkcs12der length]);
...
}
CFDictionaryRef dict0 = (CFDictionaryRef) CFArrayGetValueAtIndex(items, 0);
if (!dict0)
return nil;
SecIdentityRef iRef = (SecIdentityRef) CFDictionaryGetValue(dict0,
kSecImportItemIdentity);
CFArrayRef cRef = (CFArrayRef) CFDictionaryGetValue(dict0, kSecImportItemCertChain);
self = [self initWithIdentityRef:iRef withChainArrayRef:cRef];
CFRelease(items);
#if TARGET_OS_IPHONE
// We lack SecPrivate* on iOS. So we cheat a bit - rather than
// use the keychain we limt ourselves to our own *.p12's and
// keep a copy of the private key in memory.
//
# ifdef WITH_OPENSSL
const unsigned char * ptr = [pkcs12der bytes];
PKCS12 * p12 = d2i_PKCS12(NULL, &ptr, len);
char buff[1024];
if (!p12) {
NSLog(@"Could not decode PKCS#12: %s", ERR_error_string(ERR_get_error(), buff));
...
};
const char * pass = [password cStringUsingEncoding:NSASCIIStringEncoding];
if (PKCS12_parse(p12, pass, &pkey, &x509, NULL) != 1) {
NSLog(@"Could not parse PKCS#12: %s", ERR_error_string(ERR_get_error(), buff));
...
};
....
# else
ckoCert = [[CkoCert alloc] init];
if (!([ckoCert LoadPfxData:pkcs12der password:[APPSETTINGS wellKnownPkcsPassword]])) {
NSLog(@"PKCS12 loadPfxData failed: %@", [ckoCert LastErrorText]);
...
}
ckoPrivateKey = [ckoCert ExportPrivateKey];
# endif // chilkat or openssl
#endif // iOS
return self;
}
警告:在上面我已经剥离了大多数 mngt/错误管理和/或用断言替换它,否则它变得有点太笨拙了。
谢谢,
德。