26
I need to sign requests before sending it to backend server. However the private key is given to me. So I need to import it and then use it to sign. I am able to import and sign in but that data is different from what I get by signing using openssl. I know its doing it wrong because when I import public key, i cannot verify it too. If there is a way i can avoid importing to keychain, that would be great too. Have been working hard on this for couple of days and this is a high pri work for us. Can some one please help.
- (SecKeyRef) getPrivateKey {
//RSA KEY BELOW IS DUMMY.
key = @"-----BEGIN RSA PRIVATE KEY-----\nORtMei3ImKI2ZKI636I4+uNCwFfZv9pyJzXyfr1ZNo7iaiW7A0NjLxikNxrWpr/M\n6HD8B2j/CSjRPW3bhsgDXAx/AI1aSfJFxazjiTxx2Lk2Ke3jbhE=\n-----END RSA PRIVATE KEY-----\n";
NSString * tag = @"adpPrivateKey";
NSString *s_key = [NSString string];
NSArray *a_key = [key componentsSeparatedByString:@"\n"];
BOOL f_key = FALSE;
for (NSString *a_line in a_key) {
if ([a_line isEqualToString:@"-----BEGIN RSA PRIVATE KEY-----"]) {
f_key = TRUE;
}
else if ([a_line isEqualToString:@"-----END RSA PRIVATE KEY-----"]) {
f_key = FALSE;
}
else if (f_key) {
s_key = [s_key stringByAppendingString:a_line];
}
}
if (s_key.length == 0) return(nil);
// This will be base64 encoded, decode it.
NSData *d_key = [NSData dataFromBase64String:s_key];
if(d_key == nil) return nil;
NSData *d_tag = [NSData dataWithBytes:[tag UTF8String] length:[tag length]];
// Delete any old lingering key with the same tag
NSMutableDictionary *privateKey = [[NSMutableDictionary alloc] init];
[privateKey setObject:(id) kSecClassKey forKey:(id)kSecClass];
[privateKey setObject:(id) kSecAttrKeyTypeRSA forKey:(id)kSecAttrKeyType];
[privateKey setObject:d_tag forKey:(id)kSecAttrApplicationTag];
SecItemDelete((CFDictionaryRef)privateKey);
CFTyp