Sto tentando di aggiungere una chiave privata nel portachiavi iOS. Il certificato (chiave pubblica) funziona bene ma la chiave privata si rifiuta ... Sono totalmente confuso perché il seguente codice non funziona.Aggiunta di una chiave privata nel portachiavi iOS
Prima di tutto sto verificando se la chiave corrente (= il tasto nel caso in cui il portachiavi sia un archivio di chiavi/valori) sia "libero" nel portachiavi. Quindi aggiungerò la chiave privata.
CFStringRef labelstring = CFStringCreateWithCString(NULL, [key cStringUsingEncoding:NSUTF8StringEncoding], kCFStringEncodingUTF8);
NSArray* keys = [NSArray arrayWithObjects:(__bridge id)kSecClass,kSecAttrLabel,kSecReturnData,kSecAttrAccessible,nil];
NSArray* values = [NSArray arrayWithObjects:(__bridge id)kSecClassKey,labelstring,kCFBooleanTrue,kSecAttrAccessibleWhenUnlocked,nil];
NSMutableDictionary* searchdict = [NSMutableDictionary dictionaryWithObjects:values forKeys:keys];
CFRelease(labelstring);
NSMutableDictionary *query = searchdict;
CFTypeRef item = NULL;
OSStatus error = SecItemCopyMatching((__bridge_retained CFDictionaryRef) query, &item);
if (error)
{
NSLog(@"Error: %ld (statuscode)", error);
}
if(error != errSecItemNotFound)
{
SecItemDelete((__bridge_retained CFDictionaryRef) query);
}
[query setObject:(id)data forKey:(__bridge id)kSecValueData];
OSStatus status = SecItemAdd((__bridge_retained CFDictionaryRef) query, &item);
if(status)
{
NSLog(@"Keychain error occured: %ld (statuscode)", status);
return NO;
}
uscita Il debug è la seguente:
2012-07-26 15:33:03.772 App[15529:1b03] Error: -25300 (statuscode)
2012-07-26 15:33:11.195 App[15529:1b03] Keychain error occured: -25299 (statuscode)
Il primo codice di errore -25300
rappresenta errSecItemNotFound
. Quindi non esiste alcun valore memorizzato per questa chiave. Quindi, quando provo ad aggiungere la chiave privata nel Portachiavi, ricevo -25299
che significa errSecDuplicateItem
. Questo non lo capisco. Perché sta succedendo?
Qualcuno ha un indizio o un suggerimento su questo?
codici di errore di Apple:
errSecSuccess = 0, /* No error. */
errSecUnimplemented = -4, /* Function or operation not implemented. */
errSecParam = -50, /* One or more parameters passed to a function where not valid. */
errSecAllocate = -108, /* Failed to allocate memory. */
errSecNotAvailable = -25291, /* No keychain is available. You may need to restart your computer. */
errSecDuplicateItem = -25299, /* The specified item already exists in the keychain. */
errSecItemNotFound = -25300, /* The specified item could not be found in the keychain. */
errSecInteractionNotAllowed = -25308, /* User interaction is not allowed. */
errSecDecode = -26275, /* Unable to decode the provided data. */
errSecAuthFailed = -25293, /* The user name or passphrase you entered is not correct. */
Grazie in anticipo!
Aggiornamento n. 1: ho capito che funziona solo per la prima volta. Anche quando i dati e la chiave sono diversi, dopo la prima volta memorizzato nel portachiavi non posso memorizzare ulteriori chiavi.
sto affrontando lo stesso problema esatto. Prima chiave aggiunta usando SecItemAdd senza problemi, quindi qualsiasi chiamata consecutiva a SecItemAdd fallisce con errSecDuplicateItem sebbene nonostante SecItemCopyMatching restituisca errSecItemNotFound. Hai già trovato una soluzione? – 100grams