4

I am trying to import private key into keychain usingSecItemAdd method returns OSStatus 0 but when I try to retrieve that key from key chain using SecItemCopyMatch, It returns nil data but OSStatus is 0 means success

Please refer from Apple developer forum link

jailani
  • 2,260
  • 2
  • 21
  • 45

2 Answers2

1

The bug occurs due to malformed public key refer https://forums.developer.apple.com/thread/15129

If you use Basic Encoding Rules library here's the solution.

To fix your public key you need to insert nil byte before modulus data. https://github.com/StCredZero/SCZ-BasicEncodingRules-iOS/issues/6#issuecomment-136601437

P.S. For me fix was as simple as:

const char fixByte = 0;
NSMutableData * fixedModule = [NSMutableData dataWithBytes:&fixByte length:1];
[fixedModule appendData:modulusData];
Stas
  • 9,925
  • 9
  • 42
  • 77
  • No My problem is importing private key not public key... I haven't tried to import public key – jailani Sep 02 '15 at 08:08
  • 1
    You just save my life,thank you very much,how can this simple solution do the trick... – carl Sep 12 '15 at 09:46
1

Thank you! you're my hero!

In my case.

  • Before

    NSData *modBits = [[NSData alloc] initWithBase64EncodedString:mod options:NSDataBase64DecodingIgnoreUnknownCharacters];
    
  • After

    const char fixByte = 0;
    NSMutableData * modBits = [NSMutableData dataWithBytes:&fixByte length:1];
    
    NSData *tmpmodBits = [[NSData alloc] initWithBase64EncodedString:mod options:NSDataBase64DecodingIgnoreUnknownCharacters];
    
    [modBits appendData:tmpmodBits];
    
ArtKorchagin
  • 4,801
  • 13
  • 42
  • 58