Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added rjoudrey code changes for extracting certificates #18

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
GCC_BIN=`xcrun --sdk iphoneos --find gcc`
SDK=`xcrun --sdk iphoneos --show-sdk-path`
OSXSDK=`xcrun --sdk macosx --show-sdk-path`
#support iPhone 3GS and above, delete armv6 to avoid SDK error
ARCH_FLAGS=-arch armv7 -arch armv7s -arch arm64

Expand All @@ -21,10 +22,11 @@ default: main.o list
@$(GCC_ARM) $(LDFLAGS) main.o -o keychain_dumper

main.o: main.m
ln -s $(OSXSDK)/System/Library/Frameworks/Foundation.framework/Versions/C/Headers/NSTask.h .
$(GCC_ARM) -c main.m

clean:
rm -f keychain_dumper *.o
rm -f keychain_dumper *.o NSTask.h

list:
security find-identity -pcodesigning
Expand Down
50 changes: 46 additions & 4 deletions main.m
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@

#import <UIKit/UIKit.h>
#import <Security/Security.h>
#import <Foundation/Foundation.h>
#import "NSTask.h"
#import "sqlite3.h"

void printToStdOut(NSString *format, ...) {
Expand All @@ -41,6 +43,46 @@ void printToStdOut(NSString *format, ...) {
[formattedString release];
}

NSString *runProcess(NSString *executablePath, NSArray *args) {
NSPipe *pipe = [NSPipe pipe];
NSFileHandle *file = pipe.fileHandleForReading;

NSTask *task = [[NSTask alloc] init];
task.launchPath = executablePath;
task.arguments = args;
task.standardOutput = pipe;
task.standardError = [NSPipe pipe];
[task launch];

NSData *data = [file readDataToEndOfFile];
[file closeFile];

return [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
}

NSString *saveDataTemporarily(NSData *data) {
NSString *tmpPath = @"/tmp/data";
[data writeToFile:tmpPath atomically:YES];
return tmpPath;
}

NSString *runOpenSSLWithArgs(NSArray *args) {
return runProcess(@"/usr/bin/openssl", args);
}

NSString *runOpenSSLForConversion(NSString *prog, NSData *data) {
NSArray *args = @[prog, @"-inform", @"der", @"-in", saveDataTemporarily(data), @"-outform", @"pem"];
return runOpenSSLWithArgs(args);
}

void printKeyPEM(NSData *data) {
printToStdOut(@"%@\n", runOpenSSLForConversion(@"rsa", data));
}

void printCertPEM(NSData *data) {
printToStdOut(@"%@\n", runOpenSSLForConversion(@"x509", data));
}

void printUsage() {
printToStdOut(@"Usage: keychain_dumper [-e]|[-h]|[-agnick]\n");
printToStdOut(@"<no flags>: Dump Password Keychain Items (Generic Password, Internet Passwords)\n");
Expand Down Expand Up @@ -225,8 +267,8 @@ void printCertificate(NSDictionary *certificateItem) {
printToStdOut(@"Label: %@\n", [certificateItem objectForKey:(id)kSecAttrLabel]);
printToStdOut(@"Serial Number: %@\n", [certificateItem objectForKey:(id)kSecAttrSerialNumber]);
printToStdOut(@"Subject Key ID: %@\n", [certificateItem objectForKey:(id)kSecAttrSubjectKeyID]);
printToStdOut(@"Subject Key Hash: %@\n\n", [certificateItem objectForKey:(id)kSecAttrPublicKeyHash]);

printToStdOut(@"Subject Key Hash: %@\n", [certificateItem objectForKey:(id)kSecAttrPublicKeyHash]);
printCertPEM(certificateItem[@"certdata"]);
}

void printKey(NSDictionary *keyItem) {
Expand Down Expand Up @@ -258,8 +300,8 @@ void printKey(NSDictionary *keyItem) {
printToStdOut(@"For Signatures: %@\n", CFBooleanGetValue((CFBooleanRef)[keyItem objectForKey:(id)kSecAttrCanSign]) == true ? @"True" : @"False");
printToStdOut(@"For Signature Verification: %@\n", CFBooleanGetValue((CFBooleanRef)[keyItem objectForKey:(id)kSecAttrCanVerify]) == true ? @"True" : @"False");
printToStdOut(@"For Key Wrapping: %@\n", CFBooleanGetValue((CFBooleanRef)[keyItem objectForKey:(id)kSecAttrCanWrap]) == true ? @"True" : @"False");
printToStdOut(@"For Key Unwrapping: %@\n\n", CFBooleanGetValue((CFBooleanRef)[keyItem objectForKey:(id)kSecAttrCanUnwrap]) == true ? @"True" : @"False");

printToStdOut(@"For Key Unwrapping: %@\n", CFBooleanGetValue((CFBooleanRef)[keyItem objectForKey:(id)kSecAttrCanUnwrap]) == true ? @"True" : @"False");
printKeyPEM(keyItem[@"v_Data"]);
}

void printIdentity(NSDictionary *identityItem) {
Expand Down