Mat*_*att 7 iphone cocoa openssl smime
我试图做的是复制以下命令在Mac上通过终端运行,但在iPhone/Cocoa中:
openssl smime -binary -sign -signer cert.pem -inkey key.pem -in file.txt -out encrypted -outform DER
Run Code Online (Sandbox Code Playgroud)
其中"encrypted"是由命令产生的加密文件.
虽然它指定了2个单独的密钥(公钥和私钥),但可以将它们作为单个.p12文件.
按照这个可可片段使用.p12证书加密文件后,我不确定这是否是正确的方法.
什么是在iPhone上复制SMIME命令(按照上面的终端命令)最好的方法,或者是它甚至无法在所有通过可用Security.framework/ CommonCrypto方法?
据我所知 - 你有点陷入困境 - 桨被锁定在应用商店中。
我过去用 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];
Run Code Online (Sandbox Code Playgroud)
其中身份字段具有“保留自己的缓存”作弊:
-(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;
}
Run Code Online (Sandbox Code Playgroud)
警告:在上面我已经剥离了大部分 mngt/错误管理和/或用断言替换它,否则它有点太实用了。
谢谢,
深度。
| 归档时间: |
|
| 查看次数: |
781 次 |
| 最近记录: |