I have three things: a file, a signature file, and an x509.cer certificate file. The file must be verified using the public key in the certificate and the signature file. I want to do this with Security.h / CommonCrypto.
What I have tried so far:
NSData* fileData = [NSData dataWithContentsOfFile:(...)];
NSData* signatureData = [NSData dataWithContentsOfFile:(...)];
NSData* certificateData = [NSData dataWithContentsOfFile:(...)];
SecCertificateRef certificate = SecCertificateCreateWithData(NULL, CFBridgingRetain(certificateData));
The certificate loads just fine. His name can be verified using
CFStringRef certificateDescription = SecCertificateCopySubjectSummary(certificate);
which is working. Since there seems to be no method in iOS for directly copying the public key, I first create trust.
SecTrustRef trust;
OSStatus statusTrust = SecTrustCreateWithCertificates( certificate, secPolicy, &trust);
SecTrustResultType resultType;
OSStatus statusTrustEval = SecTrustEvaluate(trust, &resultType);
All this works fine with the errSecSuccess result.
Now I am trying to get the public key.
SecKeyRef publicKey;
publicKey = SecTrustCopyPublicKey(trust);
size_t keysize = SecKeyGetBlockSize(publicKey);
But the contents of publicKey
NSData* keyData = [NSData dataWithBytes:publicKey length:keysize];
- This is not the same as the public key that opens when you open the .cer file. So this is the number one problem.
, , . .
OSStatus verficationResult = SecKeyRawVerify(publicKey, kSecPaddingPKCS1, [fileData bytes], [fileData length], [signatureData bytes], [signatureData length]);
OSStatus -9809 ( ). , -25293 errSecAuthFailed.
- ?