簡體   English   中英

iOS和MacOSX上的私鑰簽名不同

[英]Private key signature different on iOS and MacOSX

我在NSData類上實現了一個類別方法,它使用SHA-1哈希返回數據的簽名,並使用私鑰進行后續加密,如下所示:

- (NSData *)signatureWithKey:(SecKeyRef)keyRef {

    if (keyRef == NULL) {
        return nil;
    }

    NSData *sha1Digest = [self dataWithSHA1Digest];

    size_t maxLength = SecKeyGetBlockSize(keyRef) - 11;

    if ([sha1Digest length] > maxLength) {
        NSString *reason = [NSString stringWithFormat:@"Digest is too long to sign with this key, max length is %ld and actual length is %ld", maxLength, (unsigned long)[self length]];
        NSException *ex = [NSException exceptionWithName:@"BMInvalidArgumentException" reason:reason userInfo:nil];
        @throw ex;
    }

#if TARGET_OS_IPHONE
    OSStatus status = noErr;

    uint8_t *plainBuffer = (uint8_t *)[sha1Digest bytes];
    size_t plainBufferSize = [sha1Digest length];
    size_t cipherBufferSize = SecKeyGetBlockSize(keyRef);
    uint8_t *cipherBuffer = malloc(cipherBufferSize * sizeof(uint8_t));

    status = SecKeyRawSign(keyRef,
                           kSecPaddingPKCS1SHA1,
                           plainBuffer,
                           plainBufferSize,
                           &cipherBuffer[0],
                           &cipherBufferSize
                           );

    if (status == noErr) {
        return [NSData dataWithBytesNoCopy:cipherBuffer length:cipherBufferSize freeWhenDone:YES];
    }

    free(cipherBuffer);
    return nil;
#else
    CFErrorRef error = NULL;
    SecTransformRef signer = NULL;
    CFTypeRef signature = NULL;
    if ((signer = SecSignTransformCreate(keyRef, &error))) {
        if (SecTransformSetAttribute(
                                 signer,
                                 kSecTransformInputAttributeName,
                                 (CFDataRef)sha1Digest,
                                     &error)) {
            signature = SecTransformExecute(signer, &error);
        }
    }

    if (error) {
        LogWarn(@"Could not sign: %@", error);
        CFRelease(error);
    }

    if (signer) {
        CFRelease(signer);
    }

    if (signature) {
        NSData *data = [NSData dataWithData:(NSData *)signature];
        CFRelease(signature);
        return data;
    } else {
        return nil;
    }

#endif

}

現在奇怪的是,使用相同的私鑰(從p12文件加載),在簽署相同數據時,iOS和MacOSX會得到兩個不同的結果。 我完全對此感到困惑。 您可能會注意到上面的方法使用安全轉換對MacOSX使用不同的實現,但即使我在MacOSX上使用iOS實現(它提供編譯警告但工作正常),我得到相同的結果。

用於從文件加載私鑰的方法如下:

+ (SecKeyRef)newPrivateKeyRefWithPassword:(NSString *)password fromData:(NSData *)data {
    NSMutableDictionary * options = [[NSMutableDictionary alloc] init];

    SecKeyRef privateKeyRef = NULL;

    // Set the public key query dictionary
    //change to your .pfx  password here
    [options setObject:password forKey:(id)kSecImportExportPassphrase];

    CFArrayRef items = CFArrayCreate(NULL, 0, 0, NULL);

    OSStatus securityError = SecPKCS12Import((CFDataRef)data,
                                             (CFDictionaryRef)options, &items);

    if (securityError == noErr && CFArrayGetCount(items) > 0) {
        CFDictionaryRef identityDict = CFArrayGetValueAtIndex(items, 0);
        SecIdentityRef identityApp =
        (SecIdentityRef)CFDictionaryGetValue(identityDict,
                                             kSecImportItemIdentity);

        securityError = SecIdentityCopyPrivateKey(identityApp, &privateKeyRef);
        if (securityError != noErr) {
            privateKeyRef = NULL;
        }
    }
    [options release];
    if (items) CFRelease(items);
    return privateKeyRef;
}

這是我使用的測試用例。 請注意,在iOS和MacOSX上打印了兩個不同的字符串:

    NSString *test = @"bla";
    NSData *testData = [test dataUsingEncoding:NSUTF8StringEncoding];

    NSString *p12Path= [[NSBundle mainBundle] pathForResource:@"private_key" ofType:@"p12"];

    NSData *p12Data = [NSData dataWithContentsOfFile:p12Path];

    SecKeyRef keyRef = [BMSecurityHelper newPrivateKeyRefWithPassword:@"xxxxxxxx" fromData:p12Data];

    NSData *signatureData = [testData signatureWithKey:keyRef];
    NSString *signatureString = [BMEncodingHelper base64EncodedStringForData:signatureData withLineLength:0];

    if (keyRef) CFRelease(keyRef);

    NSLog(@"signatureString: %@", signatureString);

如果你能回答你自己的問題,那總是很好。 我錯過了以下內容:在MacOSX下,安全性轉換還自動計算SHA-1哈希,與iOS實現相反。

我通過在MacOSX實現中添加以下內容來解決問題:

SecTransformSetAttribute(signer, kSecInputIsAttributeName, kSecInputIsDigest, &error)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM