]> git.saurik.com Git - apple/security.git/blob - OSX/sec/Security/Regressions/rk_01_recoverykey.m
Security-58286.1.32.tar.gz
[apple/security.git] / OSX / sec / Security / Regressions / rk_01_recoverykey.m
1 //
2 // rk_01_recoverykey.m
3 //
4
5 #define __KEYCHAINCORE__ 1
6
7 #include <Foundation/Foundation.h>
8 #include <Security/Security.h>
9
10 #include "SecRecoveryKey.h"
11 #include "shared_regressions.h"
12
13 int rk_01_recoverykey(int argc, char *const *argv)
14 {
15 NSArray *testData = @[
16 @{
17 @"recoverykey" : @"AAAA-AAAA-AAAA-AAAA-AAAA-AAAA-AAGW",
18 @"publicKey" : @"UUjq5Wv572RSsKahddvUPQAEIeErSHMK9J+NKb6sVdo=",
19 @"privateKey" : @"UUjq5Wv572RSsKahddvUPQAEIeErSHMK9J+NKb6sVdpi00pR5UGzfoARLnpxCFmqCh1XCRtjCptztGfN1XW11w==",
20 @"password" : @"Ze14tkzC8keZEnoIv+LoWvicxOTSSqUwhE8xyChmZAs=",
21 @"masterID" : @"Ndh2auLEx1zdQ+1KIAbpH4vbDN4L/O+C49AgAKhPGY0="
22 },
23 @{ // same again to make sure it works
24 @"recoverykey" : @"AAAA-AAAA-AAAA-AAAA-AAAA-AAAA-AAGW",
25 @"publicKey" : @"UUjq5Wv572RSsKahddvUPQAEIeErSHMK9J+NKb6sVdo=",
26 @"privateKey" : @"UUjq5Wv572RSsKahddvUPQAEIeErSHMK9J+NKb6sVdpi00pR5UGzfoARLnpxCFmqCh1XCRtjCptztGfN1XW11w==",
27 @"password" : @"Ze14tkzC8keZEnoIv+LoWvicxOTSSqUwhE8xyChmZAs=",
28 @"masterID" : @"Ndh2auLEx1zdQ+1KIAbpH4vbDN4L/O+C49AgAKhPGY0="
29
30 },
31 @{
32 @"recoverykey" : @"BBBB-BBBB-BBBB-BBBB-BBBB-BBBB-BBAY",
33 @"publicKey" : @"fomczHhXphIMaCbuQlKPefXO8YEIH2M9TFslcBjvJXY=",
34 @"privateKey" : @"fomczHhXphIMaCbuQlKPefXO8YEIH2M9TFslcBjvJXa/W5BWvgJmZO9xShq1sePpLDfGf5lOkwhwzFzFypiXgw==",
35 @"password" : @"P7nC1leKBTJ3aMsXZImVsR2kIlqlsvoSEI8yFKv6xdw=",
36 @"masterID" : @"aMGzcl99B0wBMQgKzSPCkpa8uG6g6As0+/aUgZOEpAI="
37 },
38 ];
39
40
41 plan_tests(9 * (int)[testData count]);
42
43 [testData enumerateObjectsUsingBlock:^(NSDictionary * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
44 NSString *recoveryKey = obj[@"recoverykey"];
45 NSString *knownPublicKey = obj[@"publicKey"];
46 NSString *knownPrivateKey = obj[@"privateKey"];
47 NSString *knownPassword = obj[@"password"];
48 #if !(defined(__i386__) || TARGET_IPHONE_SIMULATOR || TARGET_OS_BRIDGE)
49 NSString *knownMasterID = obj[@"masterID"];
50 #endif
51
52 SecRecoveryKey *rk = SecRKCreateRecoveryKeyWithError(recoveryKey, NULL);
53 ok(rk, "got recovery key");
54
55 NSData *publicKey = SecRKCopyBackupPublicKey(rk);
56 ok(publicKey, "got publicKey");
57
58 ok([publicKey isEqualToData:[[NSData alloc] initWithBase64EncodedString:knownPublicKey options:0]],
59 "public key same: %@", [publicKey base64EncodedStringWithOptions:0]);
60
61 NSData *privateKey = SecRKCopyBackupFullKey(rk);
62 ok(privateKey, "got privateKey");
63
64 ok([privateKey isEqualToData:[[NSData alloc] initWithBase64EncodedString:knownPrivateKey options:0]],
65 "privateKey key same: %@", [privateKey base64EncodedStringWithOptions:0]);
66
67 NSString *recoveryPassword = SecRKCopyAccountRecoveryPassword(rk);
68 ok(recoveryPassword, "got account recovery password");
69
70 ok([recoveryPassword isEqualToString:knownPassword], "password same: %@", recoveryPassword);
71 #if defined(__i386__) || TARGET_IPHONE_SIMULATOR || TARGET_OS_BRIDGE
72 ok(true, "skipping recovery verifier test for unsupported platform");
73 #else
74 NSDictionary *recoveryVerifier = SecRKCopyAccountRecoveryVerifier(recoveryKey, NULL);
75 NSData *masterID = [recoveryVerifier objectForKey:kSecRVMasterID];
76 ok(masterID, "got masterID");
77 ok([masterID isEqualToData:[[NSData alloc] initWithBase64EncodedString:knownMasterID options:0]],
78 "masterID key same: %@", [masterID base64EncodedStringWithOptions:0]);
79
80 // this can be uncommented to get some quick sample output
81 // NSString *fileName = [NSString stringWithFormat:@"/tmp/%@.plist", recoveryKey];
82 // [recoveryVerifier writeToFile:fileName atomically:YES];
83
84 #endif
85 }];
86
87 return 0;
88 }