在 iPhone/Cocoa 上复制 OpenSSL smime 命令



我试图做的是通过Mac上的终端复制以下命令,但在iPhone/in Cocoa上:

openssl smime -binary -sign -signer cert.pem -inkey key.pem -in file.txt -out encrypted -outform DER

其中"加密"是由命令生成的加密文件。

尽管它指定了 2 个单独的密钥(公钥和私钥),但可以将这些密钥作为单个.p12文件。

遵循此 cocoa 片段以使用 .p12 证书加密文件后,我不确定这是否是正确的方法。

在iPhone上复制smime命令的最佳方法是什么(根据上面的终端命令),或者甚至通过可用的Security.framework/CommonCrypto方法根本不可能?

据我所知 - 你有点小溪 - 桨锁在应用商店中。

  • 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/错误管理和/或用断言替换它,否则它变得有点太笨拙了。

谢谢

呵。

最新更新