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 "keychain/SecureObjectSync/SOSPeerInfoCollections.h"
27 #include <utilities/SecCFWrappers.h>
28 #include <Security/SecRandom.h>
30 #include "keychain/SecureObjectSync/SOSInternal.h"
32 #include "SOSCircle_regressions.h"
33 #include "SOSRegressionUtilities.h"
37 #define encode_decode_count 2
39 static CF_RETURNS_RETAINED SOSBackupSliceKeyBagRef
EncodeDecode(SOSBackupSliceKeyBagRef bag
)
41 SOSBackupSliceKeyBagRef result
= NULL
;
42 CFErrorRef localError
= NULL
;
44 skip("No bag to use", 2, bag
);
45 CFDataRef encoded
= SOSBSKBCopyEncoded(bag
, &localError
);
46 ok(encoded
, "encode (%@)", localError
);
47 CFReleaseNull(localError
);
49 skip("Encode failed", 1, encoded
);
50 result
= SOSBackupSliceKeyBagCreateFromData(kCFAllocatorDefault
, encoded
, &localError
);
51 ok(result
, "create (%@)", localError
);
52 CFReleaseNull(localError
);
54 CFReleaseNull(encoded
);
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
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
72 static void tests(void)
74 CFErrorRef localError
= NULL
;
75 CFMutableSetRef piSet
= CFSetCreateMutableForSOSPeerInfosByID(kCFAllocatorDefault
);
77 CFDataRef entropy1
= CFDataCreateWithBytesNoCopy(kCFAllocatorDefault
, sEntropy1
, sizeof(sEntropy1
), kCFAllocatorNull
);
78 CFDataRef entropy2
= CFDataCreateWithBytesNoCopy(kCFAllocatorDefault
, sEntropy2
, sizeof(sEntropy2
), kCFAllocatorNull
);
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
);
87 CFDataRef peer1BackupPublic
= SOSCopyDeviceBackupPublicKey(entropy1
, &localError
);
88 ok(peer1BackupPublic
, "Got backup key (%@)", localError
);
89 CFReleaseNull(localError
);
91 SOSFullPeerInfoUpdateBackupKey(fullPeer1WithBackup
, peer1BackupPublic
, &localError
);
93 SOSPeerInfoRef peer1WithBackup
= SOSFullPeerInfoGetPeerInfo(fullPeer1WithBackup
);
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
);
102 CFDataRef peer2BackupPublic
= SOSCopyDeviceBackupPublicKey(entropy2
, &localError
);
103 ok(peer2BackupPublic
, "Got backup key (%@)", localError
);
104 CFReleaseNull(localError
);
106 SOSFullPeerInfoUpdateBackupKey(fullPeer2WithBackup
, peer2BackupPublic
, &localError
);
108 SOSPeerInfoRef peer2WithBackup
= SOSFullPeerInfoGetPeerInfo(fullPeer2WithBackup
);
110 SOSBackupSliceKeyBagRef vb
= SOSBackupSliceKeyBagCreate(kCFAllocatorDefault
, piSet
, &localError
);
111 ok(vb
== NULL
, "Should fail with no peers (%@)", localError
);
112 CFReleaseNull(localError
);
115 CFSetAddValue(piSet
, peer1WithBackup
);
116 CFSetAddValue(piSet
, peer2WithBackup
);
118 SOSBackupSliceKeyBagRef vb2
= NULL
;
120 vb
= SOSBackupSliceKeyBagCreate(kCFAllocatorDefault
, piSet
, &localError
);
121 ok(vb
!= NULL
, "Allocation: (%@)", localError
);
122 CFReleaseNull(localError
);
124 vb2
= EncodeDecode(vb
);
126 ok(vb2
!= NULL
, "transcoded");
130 CFReleaseNull(piSet
);
132 CFReleaseNull(peer1SigningKey
);
133 CFReleaseNull(peer2OctagonSigningKey
);
134 CFReleaseNull(peer1BackupPublic
);
135 CFReleaseNull(fullPeer1WithBackup
);
137 CFReleaseNull(peer2SigningKey
);
138 CFReleaseNull(peer2OctagonSigningKey
);
139 CFReleaseNull(peer2BackupPublic
);
140 CFReleaseNull(fullPeer2WithBackup
);
142 CFReleaseNull(entropy1
);
143 CFReleaseNull(entropy2
);
147 int sc_153_backupslicekeybag(int argc
, char *const *argv
)