在没有外部库的情况下验证iOS和OSx上的签名(-9809 OSStatus错误)

时间:2018-05-14 03:21:31

标签: objective-c openssl rsa seckeyref

我使用SecKeyCreateWithData创建公钥。在剥离标题后,从Pem创建密钥。

我尝试使用

验证签名
  1. SecKeyRawVerify返回-9809错误(iOS)
  2. SecKeyVerifySignature返回-67808" RSA签名验证失败,无匹配" (IOS)
  3. SecTransformExecute返回false。 (OSX)
  4. 我们使用SHA256和Unicode编码对消息进行签名(C#RSACryptoProvider)。

    创建公钥的代码:

    NSDictionary* attributes =
        @{ (id)kSecAttrKeyType:               (id)kSecAttrKeyTypeRSA,
           (id)kSecAttrKeySizeInBits:         @2048,
           (id)kSecPublicKeyAttrs:
               @{ (id)kSecAttrIsPermanent:    @YES,
                  (id)kSecAttrApplicationTag: tag1
                },
           (id)kSecAttrCanEncrypt:@YES,
           (id)kSecAttrCanVerify:@YES,
           (id)kSecAttrKeyClass: (id)kSecAttrKeyClassPublic
         };
    
        CFErrorRef error = NULL;
        SecKeyRef keyRef = SecKeyCreateWithData((__bridge CFDataRef)publicKeyData,
                                          (__bridge CFDictionaryRef)attributes,
                                          &error);
    

    验证码iOS:

    size_t signedHashBytesSize = SecKeyGetBlockSize(keyRef);
    const void* signedHashBytes = [signature bytes];
    NSData *plainData = [dataToSign dataUsingEncoding:NSUTF16StringEncoding];
    size_t hashBytesSize = CC_SHA256_DIGEST_LENGTH;
    uint8_t* hashBytes = malloc(hashBytesSize);
    if (!CC_SHA256([plainData bytes], (CC_LONG)[plainData length], hashBytes)) {
        return nil;
    }
    
    OSStatus status1 = SecKeyRawVerify(keyRef,
                                      kSecPaddingPKCS1,
                                      hashBytes,
                                      hashBytesSize,
                                      signedHashBytes,
                                      signedHashBytesSize);
    

    验证码OSx:

        verifier = SecVerifyTransformCreate(keyRef, (__bridge CFDataRef)self.digest, &errorCF);
        if (errorCF) { CFShow(errorCF);}
        SecTransformSetAttribute(verifier,
                                 kSecTransformInputAttributeName,
                                 (__bridge CFTypeRef)plainData,
                                 &errorCF);
        if (errorCF) { CFShow(errorCF); exit(-1); }
        SecTransformSetAttribute(verifier,
                                 kSecDigestTypeAttribute,
                                 kSecDigestSHA2,
                                 &errorCF);
        if (errorCF) { CFShow(errorCF); exit(-1); }
        SecTransformSetAttribute(verifier,
                                 kSecDigestLengthAttribute,
                                 (__bridge CFNumberRef)@256,
                                 &errorCF);
        if (errorCF) { CFShow(errorCF); exit(-1); }
        CFBooleanRef result1 = NULL;
        result1 = SecTransformExecute(verifier, &errorCF);
        BOOL success = (result1 != NULL);
    

    我被困了任何帮助都会受到赞赏。

0 个答案:

没有答案