将私钥导入Keychain在iphone中无法正常工作

时间:2012-10-28 03:52:06

标签: iphone ios cryptography rsa keychain

我需要在将请求发送到后端服务器之前对其进行签名。但是私钥是给我的。所以我需要导入它然后用它来签名。我可以导入和登录,但该数据与使用openssl签名时获得的数据不同。我知道它做错了,因为当我导入公钥时,我也无法验证它。如果有一种方法可以避免导入到钥匙串,那也会很棒。 几天来一直在努力工作,这对我们来说是一项很高的工作。请有人帮忙。

- (SecKeyRef) getPrivateKey {
//RSA KEY BELOW IS DUMMY. 

key = @"-----BEGIN RSA PRIVATE KEY-----\nORtMei3ImKI2ZKI636I4+uNCwFfZv9pyJzXyfr1ZNo7iaiW7A0NjLxikNxrWpr/M\n6HD8B2j/CSjRPW3bhsgDXAx/AI1aSfJFxazjiTxx2Lk2Ke3jbhE=\n-----END RSA PRIVATE KEY-----\n";

NSString * tag = @"adpPrivateKey";

    NSString *s_key = [NSString string];
    NSArray  *a_key = [key componentsSeparatedByString:@"\n"];
    BOOL     f_key  = FALSE;

    for (NSString *a_line in a_key) {
        if ([a_line isEqualToString:@"-----BEGIN RSA PRIVATE KEY-----"]) {
            f_key = TRUE;
        }
        else if ([a_line isEqualToString:@"-----END RSA PRIVATE KEY-----"]) {
            f_key = FALSE;
        }
        else if (f_key) {
            s_key = [s_key stringByAppendingString:a_line];
        }
    }
    if (s_key.length == 0) return(nil);

    // This will be base64 encoded, decode it.
    NSData *d_key = [NSData dataFromBase64String:s_key];

if(d_key == nil) return nil;

    NSData *d_tag = [NSData dataWithBytes:[tag UTF8String] length:[tag length]];

    // Delete any old lingering key with the same tag
    NSMutableDictionary *privateKey = [[NSMutableDictionary alloc] init];
    [privateKey setObject:(id) kSecClassKey forKey:(id)kSecClass];
    [privateKey setObject:(id) kSecAttrKeyTypeRSA forKey:(id)kSecAttrKeyType];
    [privateKey setObject:d_tag forKey:(id)kSecAttrApplicationTag];
    SecItemDelete((CFDictionaryRef)privateKey);

    CFTypeRef persistKey = nil;

    // Add persistent version of the key to system keychain
    [privateKey setObject:d_key forKey:(id)kSecValueData];
    [privateKey setObject:(id) kSecAttrKeyClassPrivate forKey:(id)
     kSecAttrKeyClass];
    [privateKey setObject:[NSNumber numberWithBool:YES] forKey:(id)
     kSecReturnPersistentRef];

    OSStatus secStatus = SecItemAdd((CFDictionaryRef)privateKey, &persistKey);
    if (persistKey != nil) CFRelease(persistKey);

    if ((secStatus != noErr) && (secStatus != errSecDuplicateItem)) {
        [privateKey release];
        return(nil);
    }

    // Now fetch the SecKeyRef version of the key
    SecKeyRef keyRef = nil;

    [privateKey removeObjectForKey:(id)kSecValueData];
    [privateKey removeObjectForKey:(id)kSecReturnPersistentRef];
    [privateKey setObject:[NSNumber numberWithBool:YES] forKey:(id)kSecReturnRef
     ];
    [privateKey setObject:(id) kSecAttrKeyTypeRSA forKey:(id)kSecAttrKeyType];
    secStatus = SecItemCopyMatching((CFDictionaryRef)privateKey,
                                    (CFTypeRef *)&keyRef);

    if(secStatus != noErr)
        return nil;

    [privateKey release];

    return keyRef;
}

以下代码用于签名。部分代码来自Apple示例(http://developer.apple.com/library/ios/#samplecode/CryptoExercise/Listings/Classes_SecKeyWrapper_m.html#//apple_ref/doc/uid/DTS40008019-Classes_SecKeyWrapper_m-DontLinkElementID_17

- (NSData *)getSignatureBytes:(NSString *)plainText {

OSStatus sanityCheck = noErr;
NSData * signedHash = nil;
uint8_t * signedHashBytes = NULL;
size_t signedHashBytesSize = 0;
SecKeyRef privateKey = NULL;

privateKey = [self getPrivateKey];

signedHashBytesSize = SecKeyGetBlockSize(privateKey);

//Create a SHA Encoded 
NSString * shaEncoded = [self sha256:plainText];
NSLog(@"%@", shaEncoded);


// Malloc a buffer to hold signature.

signedHashBytes = malloc( signedHashBytesSize * sizeof(uint8_t) );
memset((void *)signedHashBytes, 0x0, signedHashBytesSize);


NSData *inputData = [self getHashBytes:[plainText dataUsingEncoding:NSUTF8StringEncoding]];
int bytesLengthUINT8 = [inputData length]; 

sanityCheck =  SecKeyRawSign ( privateKey, kSecPaddingPKCS1, (const uint8_t *)inputData, CC_SHA256_DIGEST_LENGTH,(uint8_t *)signedHashBytes, &signedHashBytesSize);


if(sanityCheck != noErr)
    return nil;


signedHash = [NSData dataWithBytes:(const void *)signedHashBytes length:(NSUInteger)signedHashBytesSize];    
NSString *string = [signedHash base64EncodedString];

NSLog(@"%@", string);


if (signedHashBytes) free(signedHashBytes);
return signedHash;

}

我使用http://blog.flirble.org/2011/01/05/rsa-public-key-openssl-ios/中的代码示例导入公钥并验证其失败。

3 个答案:

答案 0 :(得分:3)

查看接受的答案中的最后一个方法:Converting NSData to SecKeyRef

问题在于iOS处理公钥和私钥的方式略有不同,因为iOS中通常不存在其他安全API(例如Java)所期望的标识头。所以你必须将它们剥离出来。

答案 1 :(得分:1)

也许不是将所有密钥存储在钥匙串中,而是可以在密钥链中存储一个简单的字符串(作为secret_hash)。此外,使用广泛采用的常用AFNetworking库对后端Web服务进行安全调用。

因此,如果您需要使用私钥对后端服务的请求进行签名,我建议您通过以下方式执行此操作:(a)使用强大的包装程序库进行服务调用(AFNetworking)和(b)将私钥存储为.pfx文件位于应用程序可访问的位置(我将.pfx文件保留在项目根目录中。)

因此,创建自己的AFHTTPClient子类,并使用子类创建AFHTTPRequestOperations,并将挑战块设置为使用从.pfx中提取的凭据。

这样,不是直接创建AFHTTPRequestOperation,而是使用AFHTTPClient子类的MySignedAFHTTPRequestOperation方法创建它们。此方法应创建AFHTTPRequestOperation,然后像这样设置挑战块......

    [myOperationObject setAuthenticationChallengeBlock:^(NSURLConnection *connection, NSURLAuthenticationChallenge *challenge)
    {
             NSString * pfxPath = [[NSBundle mainBundle]
                           pathForResource:@“pvtKeyFile” ofType:@"pfx"];

            NSData *PKCS12Data = [[NSData alloc] initWithContentsOfFile: pfxPath];
            CFDataRef inPKCS12Data = (__bridge CFDataRef)PKCS12Data;    
            SecIdentityRef identity;
            SecTrustRef trust;
            myIdentityAndTrustExtractionHelper(inPKCS12Data, &identity, &trust);

            SecCertificateRef certificate = NULL;
            SecIdentityCopyCertificate (identity, &certificate); 

            const void *certs[] = {certificate};
            CFArrayRef certArray = CFArrayCreate(kCFAllocatorDefault, certs, 1, NULL);

            NSURLCredential *credential = [NSURLCredential
                                           credentialWithIdentity:identity
                                           certificates:(__bridge NSArray*)certArray
                                           persistence:NSURLCredentialPersistencePermanent];

            [[challenge sender] useCredential:credential
                forAuthenticationChallenge:challenge];
            CFRelease(certArray);
    } 

以上是上面使用的身份提取帮助函数的更多细节......

OSStatus myIdentityAndTrustExtractionHelper(CFDataRef inPKCS12Data,        
                                 SecIdentityRef *mySecIdentityRef,
                                 SecTrustRef *myTrustRef)
{
    //modify to get secret-hash from keychain 
    CFStringRef mySecretHash = CFSTR(secret_hash);
    const void *keys[] =   { kSecImportExportPassphrase };
    const void *values[] = { mySecretHash };


    CFArrayRef pkscItems = CFArrayCreate(NULL, 0, 0, NULL);
    OSStatus mySecurityError = SecPKCS12Import(inPKCS12Data,
                                CFDictionaryCreate(NULL,keys, values, 1,
                                NULL, NULL),
                                &pkscItems);
    if (mySecurityError == 0) 
    {                                 
        CFDictionaryRef myIdentityAndTrust = CFArrayGetValueAtIndex (pkscItems, 0);
        const void *tempIdentity = NULL;
        tempIdentity = CFDictionaryGetValue (myIdentityAndTrust,
                                             kSecImportItemIdentity);
        *mySecIdentityRef = (SecIdentityRef)tempIdentity;
        const void *tempTrust = NULL;
        tempTrust = CFDictionaryGetValue (myIdentityAndTrust, kSecImportItemTrust);
        *myTrustRef = (SecTrustRef)tempTrust;
    }

    return mySecurityError;
}

以这种方式创建AFHTTPRequest后(即通过AFHTTPClient子类的MySignedAFHTTPRequestOperation方法),将其添加到NSOperationsQueue中执行(当然,您需要在创建操作时适当地设置成功和失败处理程序块)< / p>

总结:

  • 使用强大的AFNetworking框架进行网络服务调用
  • 将cert私钥存储为应用程序中的.pfx文件,并使用它创建凭据和密钥
  • 允许AFNetworking通过在您创建的每个操作对象中设置凭据来为您进行加密。

希望这有帮助。

答案 2 :(得分:0)

如果您只想将标准PHP兼容的base64字符串类型格式的AES-256 RSA PEM存储到系统钥匙串中,这对我有用。

我在这里张贴这个因为很多地方,我读到你不能只是将PEM文件直接粘贴到iOS上;否则你必须从中删除一些标题;然而,至少在iOS 9.3上,这现在有效,如果我在某个地方看到过它,那么它将节省我很多时间。 (注意:以下是来自https://github.com/ideawu/Objective-C-RSA Objective-C-RSA 的一部分经过大量修改的版本,请参阅适用的许可,我不认可。他们在这里也有一个Swift版本:{ {3}}看起来功能更完整,可以为很多人解决问题。)

-Wshadow