]> git.saurik.com Git - apple/security.git/blob - OSX/sec/SOSCircle/Regressions/sc-30-peerinfo.c
Security-58286.270.3.0.1.tar.gz
[apple/security.git] / OSX / sec / SOSCircle / Regressions / sc-30-peerinfo.c
1 /*
2 * Copyright (c) 2012-2014 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
26 #include <Security/SecBase.h>
27 #include <Security/SecItem.h>
28 #include <Security/SecKey.h>
29 #include <Security/SecKeyPriv.h>
30 #include <SOSPeerInfoDER.h>
31
32 #include <Security/SecureObjectSync/SOSCircle.h>
33 #include <Security/SecureObjectSync/SOSPeerInfo.h>
34 #include <Security/SecureObjectSync/SOSPeerInfoCollections.h>
35 #include <Security/SecureObjectSync/SOSInternal.h>
36 #include <Security/SecureObjectSync/SOSUserKeygen.h>
37
38 #include <utilities/SecCFWrappers.h>
39
40 #include <CoreFoundation/CoreFoundation.h>
41
42 #include <stdlib.h>
43 #include <unistd.h>
44
45 #include "SOSCircle_regressions.h"
46
47 #include "SOSRegressionUtilities.h"
48
49 #if TARGET_OS_IPHONE
50 #include <MobileGestalt.h>
51 #endif
52
53 static CFDataRef CopyTestBackupKey(void) {
54 static uint8_t data[] = { 'A', 'b', 'c' };
55
56 return CFDataCreate(kCFAllocatorDefault, data, sizeof(data));
57 }
58
59 static bool PeerInfoRoundTrip(SOSPeerInfoRef pi) {
60 bool retval = false;
61 size_t size = SOSPeerInfoGetDEREncodedSize(pi, NULL);
62 uint8_t buffer[size];
63 const uint8_t *buffer_p = SOSPeerInfoEncodeToDER(pi, NULL, buffer, buffer + sizeof(buffer));
64 ok(buffer_p != NULL, "encode");
65 if(buffer_p == NULL) return false;
66 SOSPeerInfoRef pi2 = SOSPeerInfoCreateFromDER(NULL, NULL, &buffer_p, buffer + sizeof(buffer));
67 ok(pi2 != NULL, "decode");
68 if(!pi2) return false;
69 ok(CFEqual(pi, pi2), "Decode matches");
70 if(CFEqual(pi, pi2)) retval = true;
71 CFReleaseNull(pi2);
72 return retval;
73 }
74
75 static bool FullPeerInfoRoundTrip(SOSFullPeerInfoRef fpi) {
76 bool retval = false;
77 size_t size = SOSFullPeerInfoGetDEREncodedSize(fpi, NULL);
78 uint8_t buffer[size];
79 const uint8_t *buffer_p = SOSFullPeerInfoEncodeToDER(fpi, NULL, buffer, buffer + sizeof(buffer));
80 ok(buffer_p != NULL, "encode");
81 if(buffer_p == NULL) return false;
82 SOSFullPeerInfoRef fpi2 = SOSFullPeerInfoCreateFromDER(NULL, NULL, &buffer_p, buffer + sizeof(buffer));
83 ok(fpi2 != NULL, "decode");
84 if(!fpi2) return false;
85 ok(CFEqual(fpi, fpi2), "Decode matches");
86 if(CFEqual(fpi, fpi2)) retval = true;
87 CFReleaseNull(fpi2);
88 return retval;
89 }
90
91 static int kTestTestCount = 24;
92 static void tests(void)
93 {
94 SecKeyRef signingKey = NULL;
95 SecKeyRef octagonSigningKey = NULL;
96 SecKeyRef octagonEncryptionKey = NULL;
97 SOSFullPeerInfoRef fpi = SOSCreateFullPeerInfoFromName(CFSTR("Test Peer"), &signingKey, &octagonSigningKey, &octagonEncryptionKey, NULL);
98 SOSPeerInfoRef pi = SOSFullPeerInfoGetPeerInfo(fpi);
99
100 ok(NULL != pi, "info creation");
101
102 ok(PeerInfoRoundTrip(pi), "PeerInfo safely round-trips");
103 ok(FullPeerInfoRoundTrip(fpi), "FullPeerInfo safely round-trips");
104
105 // Application ticket time.
106 CFDataRef cfpassword = CFDataCreate(NULL, (uint8_t *) "FooFooFoo", 10);
107 CFErrorRef error = NULL;
108
109 CFDataRef parameters = SOSUserKeyCreateGenerateParameters(&error);
110 ok(parameters, "No parameters!");
111 ok(error == NULL, "Error: (%@)", error);
112 CFReleaseNull(error);
113
114 SecKeyRef user_privkey = SOSUserKeygen(cfpassword, parameters, &error);
115 CFReleaseSafe(cfpassword);
116 CFReleaseNull(parameters);
117 SecKeyRef user_pubkey = SecKeyCreatePublicFromPrivate(user_privkey);
118
119 ok(SOSFullPeerInfoPromoteToApplication(fpi, user_privkey, &error), "Promote to Application");
120 ok(SOSPeerInfoApplicationVerify(SOSFullPeerInfoGetPeerInfo(fpi), user_pubkey, &error), "Promote to Application");
121
122 pi = SOSFullPeerInfoGetPeerInfo(fpi);
123 ok(PeerInfoRoundTrip(pi), "PeerInfo safely round-trips");
124
125 CFDataRef testBackupKey = CopyTestBackupKey();
126
127 ok(SOSFullPeerInfoUpdateBackupKey(fpi, testBackupKey, &error), "Set Backup (%@)", error);
128 CFReleaseNull(error);
129
130 CFReleaseNull(testBackupKey); // Make sure our ref doesn't save them.
131 testBackupKey = CopyTestBackupKey();
132
133 pi = SOSFullPeerInfoGetPeerInfo(fpi);
134 CFDataRef piBackupKey = SOSPeerInfoCopyBackupKey(pi);
135
136 ok(CFEqualSafe(testBackupKey, piBackupKey), "Same Backup Key");
137
138 ok(PeerInfoRoundTrip(pi), "PeerInfo safely round-trips with backup key");
139
140 CFReleaseNull(piBackupKey);
141 piBackupKey = SOSPeerInfoCopyBackupKey(pi);
142 ok(CFEqualSafe(testBackupKey, piBackupKey), "Same Backup Key after round trip");
143
144 // Don't own the piBackupKey key
145 CFReleaseNull(testBackupKey);
146 CFReleaseNull(piBackupKey);
147 CFReleaseNull(user_privkey);
148 CFReleaseNull(user_pubkey);
149
150 CFReleaseNull(signingKey);
151 CFReleaseNull(octagonSigningKey);
152 CFReleaseNull(octagonEncryptionKey);
153 CFReleaseNull(fpi);
154 }
155
156 int sc_30_peerinfo(int argc, char *const *argv)
157 {
158 plan_tests(kTestTestCount);
159
160 tests();
161
162 return 0;
163 }