title: Saving a symmetric SecKeyRef to OSX keychain
body: <p>I'm trying to create a symmetric encryption key using Security Transforms in OSX and then save that key to the keychain. I'm able to create a working key and save it to the keychain, but when I recall the key and try to use it, I get a CDSA error that "a missing value was detected". </p>
  
  <p>Also pertinent. After I've saved the key to the keychain, if I try to delete it manually, I get an error that says "A missing value was detected" and if I try to create a second key with a different label, I'm told that the key already exists in the keychain. </p>
  
  <p>Here's the code I'm using to save the SecKeyRef to the keychain, cobbled together from one of the Security Transforms examples. Would welcome any suggestions on what I might be doing wrong. Are there other required attributes for the SecKeychainAttributeList that I'm missing? Am I storing the key data itself incorrectly? I'm not so familiar with working in C.</p>
  
  <pre><code>+ (BOOL)addKey:(SecKeyRef)key withLabel:(NSString*)label
  {
      OSStatus err;
      SecKeychainItemRef item = nil;
  
     const char *itemLabelUTF8 = [label UTF8String];
  
      SecKeychainAttribute attrs[] = {
          { kSecLabelItemAttr, strlen(itemLabelUTF8), (char *)itemLabelUTF8 }
      };
  
      SecKeychainAttributeList attributes = { sizeof(attrs) / sizeof(attrs[0]),
          attrs };
  
      err = SecKeychainItemCreateFromContent(
                                         kSecSymmetricKeyItemClass,//class of item being     created
                                         &amp;attributes,//attributes dictionary
                                         sizeof(key),//length of buffer holding data to store
                                         key, //buffer with data to store
                                         NULL, // use the default keychain
                                         NULL,//access object to define access. null = access for this app
                                         &amp;item);//reference to created item on return
  
      if (item) CFRelease(item);
  
      if (err == errSecSuccess) {
          DDLogVerbose(@"created keychain item");
          return YES;
      } else if (err == errSecDuplicateItem){
          DDLogVerbose(@"key already exists in keychain");
          return NO;
      } else {
          DDLogVerbose(@"error: %d", err);
          return NO;
      }
  }
  </code></pre>