title: iOS SecKeyRef to NSData
body: <p>I have two keys, public and private, that are both stored in SecKeyRef-variables. For simplicity's sake, let's start with the public one. What I wanna do is export it to an NSData object. For that, there is an almost famous code snippet provide by Apple, which is here:</p>
  
  <pre><code>- (NSData *)getPublicKeyBits {
      OSStatus sanityCheck = noErr;
      NSData * publicKeyBits = nil;
  
      NSMutableDictionary * queryPublicKey = [[NSMutableDictionary alloc] init];
  
      // Set the public key query dictionary.
      [queryPublicKey setObject:(id)kSecClassKey forKey:(id)kSecClass];
      [queryPublicKey setObject:publicTag forKey:(id)kSecAttrApplicationTag];
      [queryPublicKey setObject:(id)kSecAttrKeyTypeRSA forKey:(id)kSecAttrKeyType];
      [queryPublicKey setObject:[NSNumber numberWithBool:YES] forKey:(id)kSecReturnData];
  
      // Get the key bits.
      sanityCheck = SecItemCopyMatching((CFDictionaryRef)queryPublicKey, (CFTypeRef *)&amp;publicKeyBits);
  
      if (sanityCheck != noErr)
      {
          publicKeyBits = nil;
      }
  
      [queryPublicKey release];
  
      return publicKeyBits;
  }
  </code></pre>
  
  <p>I have Xcode 4.6.2, however, and the code appears erroneous ("__bridge" is added before each conversion to id). The new version looks like this:</p>
  
  <pre><code>- (NSData *)getPublicKeyBitsFromKey:(SecKeyRef)givenKey {
      OSStatus sanityCheck = noErr;
      NSData * publicKeyBits = nil;
  
      NSMutableDictionary * queryPublicKey = [[NSMutableDictionary alloc] init];
  
      // Set the public key query dictionary.
      [queryPublicKey setObject:(__bridge id)kSecClassKey forKey:(__bridge id)kSecClass];
      [queryPublicKey setObject:publicTag forKey:(__bridge id)kSecAttrApplicationTag];
      [queryPublicKey setObject:(__bridge id)kSecAttrKeyTypeRSA forKey:(__bridge id)kSecAttrKeyType];
      [queryPublicKey setObject:[NSNumber numberWithBool:YES] forKey:(__bridge id)kSecReturnData];
  
      // Get the key bits.
      sanityCheck = SecItemCopyMatching((__bridge CFDictionaryRef)queryPublicKey, (CFTypeRef *)&amp;publicKeyBits);
  
      if (sanityCheck != noErr)
      {
          publicKeyBits = nil;
      }
  
      return publicKeyBits;
  }
  </code></pre>
  
  <p>There are still two errors, though:</p>
  
  <ul>
  <li>use of undeclared identifier 'publicTag'</li>
  <li>Cast of an indirect pointer to an Objective-C pointer to 'CFTypeRef <em>' (aka 'const void *</em>') is disallowed with ARC</li>
  </ul>
  
  <p>Now, I hope that after your help, the first issue will no longer be a problem, for I do not want to build a query or whatnot to extract the key from the keychain. <strong>I have it in a variable and I wish to extract it from there.</strong> The variable's name is <code>givenPublicKey</code>, and that's the key I wish to convert to NSData.</p>
  
  <p>So, how would I go about doing this and solving this ARC-issue?</p>
  
  <p>Follow-up: How can I export a <em>private</em> key to NSData, since I've read several time that the function I'm trying to work with only works for public keys.</p>