0

I have problem to write data to file. What I'm doing wrong?

SecTrustRef trust = [protectionSpace serverTrust];
SecCertificateRef certificate = SecTrustGetCertificateAtIndex(trust, 0);
NSData* ServerCertificateData = (__bridge NSData *)(certificate);
NSError* error;
[ServerCertificateData writeToFile:ServerCertPath options:NSDataWritingAtomic error:&error];
if(error != nil)
     NSLog(@"%@", error);
kubo
  • 492
  • 7
  • 19

1 Answers1

1

You can't convert the SecCertificateRef into a NSData * with a cast as you did. You need to use SecCertificateCopyData() on your certificate to get the data out of it.

Guillaume
  • 4,331
  • 2
  • 28
  • 31
  • Thanks for reply. Now when I get the `CFDataRef` from `SecCertificateCopyData()` how should i store it in local file? Should i use `CFURLWriteDataAndPropertiesToResource()` or is some better solution? – kubo Oct 11 '12 at 10:14
  • `CFData` is toll-free bridged to `NSData`, so once you get a `CFDataRef`, you can cast to a `NSData *` just as you tried to do in your sample code. – Guillaume Oct 11 '12 at 10:17
  • Now how to correct bridge it. Is there possibility to use `__bridge_transfer` so it will be released with NSData? Now i have something like this `CFDataRef body = SecCertificateCopyData(certificate); NSData *ServerCertificateData = (__bridge NSData *) body; CFRelease(body);` – kubo Oct 12 '12 at 09:21
  • What you wrote is correct. If you use `__bridge_transfer` then you won't have to release `body`, the result is the same, you can choose whatever. Personally, I would use the `CFRelease` and the `__bridge` if I had two different variables `body` and `ServerCertificateData` as you did, or I would use use `__bridge_transfer` if I cast directly the result of the function to a `NSData *` in one line. – Guillaume Oct 12 '12 at 09:46