2 * Copyright (c) 2015 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
25 #include <Security/SecureObjectSync/SOSBackupSliceKeyBag.h>
26 #include <Security/SecureObjectSync/SOSPeerInfoCollections.h>
27 #include <utilities/SecCFWrappers.h>
28 #include <Security/SecRandom.h>
30 #include "SOSInternal.h"
32 #include "SOSCircle_regressions.h"
33 #include "SOSRegressionUtilities.h"
35 #define encode_decode_count 2
36 #if !TARGET_IPHONE_SIMULATOR
37 static CF_RETURNS_RETAINED SOSBackupSliceKeyBagRef
EncodeDecode(SOSBackupSliceKeyBagRef bag
)
39 SOSBackupSliceKeyBagRef result
= NULL
;
40 CFErrorRef localError
= NULL
;
42 skip("No bag to use", 2, bag
);
43 CFDataRef encoded
= SOSBSKBCopyEncoded(bag
, &localError
);
44 ok(encoded
, "encode (%@)", localError
);
45 CFReleaseNull(localError
);
47 skip("Encode failed", 1, encoded
);
48 result
= SOSBackupSliceKeyBagCreateFromData(kCFAllocatorDefault
, encoded
, &localError
);
49 ok(result
, "create (%@)", localError
);
50 CFReleaseNull(localError
);
52 CFReleaseNull(encoded
);
60 static CFDataRef
CFDataCreateWithRandom(CFAllocatorRef allocator
, size_t size
) {
61 CFMutableDataRef result
= CFDataCreateMutableWithScratch(allocator
, size
);
63 SecRandomCopyBytes(kSecRandomDefault
, size
, CFDataGetMutableBytePtr(result
));
69 static const uint8_t sEntropy1
[] = {
70 0xc4, 0xb9, 0xa6, 0x6e, 0xeb, 0x56, 0xa1, 0x5c, 0x1d, 0x30, 0x09, 0x40,
71 0x41, 0xe9, 0x68, 0xb4, 0x12, 0xe0, 0xc6, 0x69, 0xfb, 0xdf, 0xcb, 0xe0,
72 0x27, 0x4b, 0x54, 0xf0, 0xdd, 0x62, 0x10, 0x78
75 static const uint8_t sEntropy2
[] = {
76 0xef, 0xbd, 0x72, 0x57, 0x02, 0xe6, 0xbd, 0x0a, 0x22, 0x6e, 0x77, 0x93,
77 0x17, 0xb3, 0x27, 0x12, 0x1b, 0x1f, 0xdf, 0xa0, 0x5b, 0xc6, 0x66, 0x54,
78 0x3a, 0x91, 0x0d, 0xc1, 0x5f, 0x57, 0x98, 0x44
81 #if !TARGET_IPHONE_SIMULATOR
82 #define tests_count (8 + encode_decode_count)
84 #define tests_count (6)
87 static void tests(void)
89 CFErrorRef localError
= NULL
;
90 CFMutableSetRef piSet
= CFSetCreateMutableForSOSPeerInfosByID(kCFAllocatorDefault
);
92 CFDataRef entropy1
= CFDataCreateWithBytesNoCopy(kCFAllocatorDefault
, sEntropy1
, sizeof(sEntropy1
), kCFAllocatorNull
);
93 CFDataRef entropy2
= CFDataCreateWithBytesNoCopy(kCFAllocatorDefault
, sEntropy2
, sizeof(sEntropy2
), kCFAllocatorNull
);
95 SecKeyRef peer1SigningKey
= NULL
;
96 SecKeyRef peer1OctagonSigningKey
= NULL
;
97 SecKeyRef peer1OctagonEncryptionKey
= NULL
;
98 SOSFullPeerInfoRef fullPeer1WithBackup
= SOSCreateFullPeerInfoFromName(CFSTR("peer1WithBackupID"), &peer1SigningKey
, &peer1OctagonSigningKey
, &peer1OctagonEncryptionKey
, &localError
);
99 ok(fullPeer1WithBackup
, "Allocate peer 1 (%@)", localError
);
100 CFReleaseNull(localError
);
102 CFDataRef peer1BackupPublic
= SOSCopyDeviceBackupPublicKey(entropy1
, &localError
);
103 ok(peer1BackupPublic
, "Got backup key (%@)", localError
);
104 CFReleaseNull(localError
);
106 SOSFullPeerInfoUpdateBackupKey(fullPeer1WithBackup
, peer1BackupPublic
, &localError
);
108 SOSPeerInfoRef peer1WithBackup
= SOSFullPeerInfoGetPeerInfo(fullPeer1WithBackup
);
110 SecKeyRef peer2SigningKey
= NULL
;
111 SecKeyRef peer2OctagonSigningKey
= NULL
;
112 SecKeyRef peer2OctagonEncryptionKey
= NULL
;
113 SOSFullPeerInfoRef fullPeer2WithBackup
= SOSCreateFullPeerInfoFromName(CFSTR("peer2WithBackupID"), &peer2SigningKey
, &peer2OctagonSigningKey
, &peer2OctagonEncryptionKey
, &localError
);
114 ok(fullPeer2WithBackup
, "Allocate peer 2 (%@)", localError
);
115 CFReleaseNull(localError
);
117 CFDataRef peer2BackupPublic
= SOSCopyDeviceBackupPublicKey(entropy2
, &localError
);
118 ok(peer2BackupPublic
, "Got backup key (%@)", localError
);
119 CFReleaseNull(localError
);
121 SOSFullPeerInfoUpdateBackupKey(fullPeer2WithBackup
, peer2BackupPublic
, &localError
);
123 SOSPeerInfoRef peer2WithBackup
= SOSFullPeerInfoGetPeerInfo(fullPeer2WithBackup
);
125 SOSBackupSliceKeyBagRef vb
= SOSBackupSliceKeyBagCreate(kCFAllocatorDefault
, piSet
, &localError
);
126 ok(vb
== NULL
, "Should fail with no peers (%@)", localError
);
127 CFReleaseNull(localError
);
130 CFSetAddValue(piSet
, peer1WithBackup
);
131 CFSetAddValue(piSet
, peer2WithBackup
);
133 SOSBackupSliceKeyBagRef vb2
= NULL
;
135 #if !TARGET_IPHONE_SIMULATOR
136 vb
= SOSBackupSliceKeyBagCreate(kCFAllocatorDefault
, piSet
, &localError
);
137 ok(vb
!= NULL
, "Allocation: (%@)", localError
);
138 CFReleaseNull(localError
);
140 vb2
= EncodeDecode(vb
);
142 ok(vb2
!= NULL
, "transcoded");
145 // <rdar://problem/20561988> Have helper functions for new security object that load bags
146 keybag_handle_t ourHandle
= SOSBSKBLoadAndUnlockWithPeerSecret(vb
, peer2WithBackup
, entropy2
, &localError
);
147 ok(ourHandle
!= bad_keybag_handle
, "loaded with peer secret, handle %d (%@)", ourHandle
, localError
);
148 CFReleaseNull(localError
);
150 aks_unload_bag(ourHandle
);
153 todo("no simulator supprt");
160 CFReleaseNull(piSet
);
162 CFReleaseNull(peer1SigningKey
);
163 CFReleaseNull(peer2OctagonSigningKey
);
164 CFReleaseNull(peer1BackupPublic
);
165 CFReleaseNull(fullPeer1WithBackup
);
167 CFReleaseNull(peer2SigningKey
);
168 CFReleaseNull(peer2OctagonSigningKey
);
169 CFReleaseNull(peer2BackupPublic
);
170 CFReleaseNull(fullPeer2WithBackup
);
172 CFReleaseNull(entropy1
);
173 CFReleaseNull(entropy2
);
176 static int kTestTestCount
= tests_count
;
178 int sc_153_backupslicekeybag(int argc
, char *const *argv
)
180 plan_tests(kTestTestCount
);