3

I create public key using SecKeyCreateWithData. Key is created from Pem after stripping the headers.

I have tried to verify signature using

  1. SecKeyRawVerify returns -9809 error (iOS)
  2. SecKeyVerifySignature returns -67808 "RSA signature verification failed, no match" (iOS)
  3. SecTransformExecute returns false. (OSx)

We use SHA256 and Unicode encoding to sign the message(C# RSACryptoProvider).

Code for creating public key:

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);

Verification Code 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);

Verification Code 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);

I am stuck any help would be appreciated.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
D12sd
  • 31
  • 1
  • Please post the code of how you create the signature (in C#), and debug print some test message data that was encoded, the signature data, and the received dataToSign and signature. – battlmonstr May 14 '18 at 09:39

0 Answers0