]> git.saurik.com Git - apple/security.git/blob - keychain/SecureObjectSync/Regressions/sc-153-backupslicekeybag.c
Security-59754.41.1.tar.gz
[apple/security.git] / keychain / SecureObjectSync / Regressions / sc-153-backupslicekeybag.c
1 /*
2 * Copyright (c) 2015 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
11 * file.
12 *
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24
25 #include <Security/SecureObjectSync/SOSBackupSliceKeyBag.h>
26 #include "keychain/SecureObjectSync/SOSPeerInfoCollections.h"
27 #include <utilities/SecCFWrappers.h>
28 #include <Security/SecRandom.h>
29
30 #include "keychain/SecureObjectSync/SOSInternal.h"
31
32 #include "SOSCircle_regressions.h"
33 #include "SOSRegressionUtilities.h"
34
35 #if SOS_ENABLED
36
37 #define encode_decode_count 2
38
39 static CF_RETURNS_RETAINED SOSBackupSliceKeyBagRef EncodeDecode(SOSBackupSliceKeyBagRef bag)
40 {
41 SOSBackupSliceKeyBagRef result = NULL;
42 CFErrorRef localError = NULL;
43 SKIP: {
44 skip("No bag to use", 2, bag);
45 CFDataRef encoded = SOSBSKBCopyEncoded(bag, &localError);
46 ok(encoded, "encode (%@)", localError);
47 CFReleaseNull(localError);
48
49 skip("Encode failed", 1, encoded);
50 result = SOSBackupSliceKeyBagCreateFromData(kCFAllocatorDefault, encoded, &localError);
51 ok(result, "create (%@)", localError);
52 CFReleaseNull(localError);
53
54 CFReleaseNull(encoded);
55 }
56
57 return result;
58 }
59
60 static const uint8_t sEntropy1[] = {
61 0xc4, 0xb9, 0xa6, 0x6e, 0xeb, 0x56, 0xa1, 0x5c, 0x1d, 0x30, 0x09, 0x40,
62 0x41, 0xe9, 0x68, 0xb4, 0x12, 0xe0, 0xc6, 0x69, 0xfb, 0xdf, 0xcb, 0xe0,
63 0x27, 0x4b, 0x54, 0xf0, 0xdd, 0x62, 0x10, 0x78
64 };
65
66 static const uint8_t sEntropy2[] = {
67 0xef, 0xbd, 0x72, 0x57, 0x02, 0xe6, 0xbd, 0x0a, 0x22, 0x6e, 0x77, 0x93,
68 0x17, 0xb3, 0x27, 0x12, 0x1b, 0x1f, 0xdf, 0xa0, 0x5b, 0xc6, 0x66, 0x54,
69 0x3a, 0x91, 0x0d, 0xc1, 0x5f, 0x57, 0x98, 0x44
70 };
71
72 static void tests(void)
73 {
74 CFErrorRef localError = NULL;
75 CFMutableSetRef piSet = CFSetCreateMutableForSOSPeerInfosByID(kCFAllocatorDefault);
76
77 CFDataRef entropy1 = CFDataCreateWithBytesNoCopy(kCFAllocatorDefault, sEntropy1, sizeof(sEntropy1), kCFAllocatorNull);
78 CFDataRef entropy2 = CFDataCreateWithBytesNoCopy(kCFAllocatorDefault, sEntropy2, sizeof(sEntropy2), kCFAllocatorNull);
79
80 SecKeyRef peer1SigningKey = NULL;
81 SecKeyRef peer1OctagonSigningKey = NULL;
82 SecKeyRef peer1OctagonEncryptionKey = NULL;
83 SOSFullPeerInfoRef fullPeer1WithBackup = SOSCreateFullPeerInfoFromName(CFSTR("peer1WithBackupID"), &peer1SigningKey, &peer1OctagonSigningKey, &peer1OctagonEncryptionKey, &localError);
84 ok(fullPeer1WithBackup, "Allocate peer 1 (%@)", localError);
85 CFReleaseNull(localError);
86
87 CFDataRef peer1BackupPublic = SOSCopyDeviceBackupPublicKey(entropy1, &localError);
88 ok(peer1BackupPublic, "Got backup key (%@)", localError);
89 CFReleaseNull(localError);
90
91 SOSFullPeerInfoUpdateBackupKey(fullPeer1WithBackup, peer1BackupPublic, &localError);
92
93 SOSPeerInfoRef peer1WithBackup = SOSFullPeerInfoGetPeerInfo(fullPeer1WithBackup);
94
95 SecKeyRef peer2SigningKey = NULL;
96 SecKeyRef peer2OctagonSigningKey = NULL;
97 SecKeyRef peer2OctagonEncryptionKey = NULL;
98 SOSFullPeerInfoRef fullPeer2WithBackup = SOSCreateFullPeerInfoFromName(CFSTR("peer2WithBackupID"), &peer2SigningKey, &peer2OctagonSigningKey, &peer2OctagonEncryptionKey, &localError);
99 ok(fullPeer2WithBackup, "Allocate peer 2 (%@)", localError);
100 CFReleaseNull(localError);
101
102 CFDataRef peer2BackupPublic = SOSCopyDeviceBackupPublicKey(entropy2, &localError);
103 ok(peer2BackupPublic, "Got backup key (%@)", localError);
104 CFReleaseNull(localError);
105
106 SOSFullPeerInfoUpdateBackupKey(fullPeer2WithBackup, peer2BackupPublic, &localError);
107
108 SOSPeerInfoRef peer2WithBackup = SOSFullPeerInfoGetPeerInfo(fullPeer2WithBackup);
109
110 SOSBackupSliceKeyBagRef vb = SOSBackupSliceKeyBagCreate(kCFAllocatorDefault, piSet, &localError);
111 ok(vb == NULL, "Should fail with no peers (%@)", localError);
112 CFReleaseNull(localError);
113 CFReleaseNull(vb);
114
115 CFSetAddValue(piSet, peer1WithBackup);
116 CFSetAddValue(piSet, peer2WithBackup);
117
118 SOSBackupSliceKeyBagRef vb2 = NULL;
119
120 vb = SOSBackupSliceKeyBagCreate(kCFAllocatorDefault, piSet, &localError);
121 ok(vb != NULL, "Allocation: (%@)", localError);
122 CFReleaseNull(localError);
123
124 vb2 = EncodeDecode(vb);
125
126 ok(vb2 != NULL, "transcoded");
127
128 CFReleaseNull(vb);
129 CFReleaseNull(vb2);
130 CFReleaseNull(piSet);
131
132 CFReleaseNull(peer1SigningKey);
133 CFReleaseNull(peer2OctagonSigningKey);
134 CFReleaseNull(peer1BackupPublic);
135 CFReleaseNull(fullPeer1WithBackup);
136
137 CFReleaseNull(peer2SigningKey);
138 CFReleaseNull(peer2OctagonSigningKey);
139 CFReleaseNull(peer2BackupPublic);
140 CFReleaseNull(fullPeer2WithBackup);
141
142 CFReleaseNull(entropy1);
143 CFReleaseNull(entropy2);
144 }
145 #endif
146
147 int sc_153_backupslicekeybag(int argc, char *const *argv)
148 {
149 #if SOS_ENABLED
150 plan_tests(12);
151 tests();
152 #else
153 plan_tests(0);
154 #endif
155 return 0;
156 }