我试图做的是复制以下命令在Mac上通过终端运行,但在 iPhone / in Cocoa:
openssl smime -binary -sign -signer cert.pem -inkey key.pem -in file.txt -out encrypted -outform DER
其中“encrypted”是由命令产生的加密文件。
虽然它指定了2个单独的密钥(公钥和私钥),但可以将它们作为单个.p12
文件。
在使用this cocoa片段加密使用.p12
证书加密文件之后,我不确定这是否是正确的方法。
在iPhone上复制smime命令的最佳方法是什么(根据上面的Terminal命令),或者甚至根本不可能通过可用的Security.framework
/ CommonCrypto方法?
答案 0 :(得分:3)
据我所知 - 你有点上了一条小溪 - 把paddle锁在appstore中。
我过去用openssl和Chilkat解决了这个问题。
然而,在每种情况下,我都会缓存'私钥的副本 - 一旦进入钥匙串 - 我可以取回的只是一个SecKeyRef(您需要与苹果签订额外的协议/许可才能将其取回并仍然在appstore中。对任何VPN(例如juniper one)应用程序进行反向工程,以查看要链接的方法/框架。
对于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 /错误管理和/或用断言替换它,否则它有点太过分了。
谢谢,
DW传递。