]> git.saurik.com Git - apple/security.git/blob - OSX/sec/SOSCircle/Regressions/sc-30-peerinfo.c
Security-58286.1.32.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 SOSFullPeerInfoRef fpi = SOSCreateFullPeerInfoFromName(CFSTR("Test Peer"), &signingKey, &octagonSigningKey, NULL);
97 SOSPeerInfoRef pi = SOSFullPeerInfoGetPeerInfo(fpi);
98
99 ok(NULL != pi, "info creation");
100
101 ok(PeerInfoRoundTrip(pi), "PeerInfo safely round-trips");
102 ok(FullPeerInfoRoundTrip(fpi), "FullPeerInfo safely round-trips");
103
104 // Application ticket time.
105 CFDataRef cfpassword = CFDataCreate(NULL, (uint8_t *) "FooFooFoo", 10);
106 CFErrorRef error = NULL;
107
108 CFDataRef parameters = SOSUserKeyCreateGenerateParameters(&error);
109 ok(parameters, "No parameters!");
110 ok(error == NULL, "Error: (%@)", error);
111 CFReleaseNull(error);
112
113 SecKeyRef user_privkey = SOSUserKeygen(cfpassword, parameters, &error);
114 CFReleaseSafe(cfpassword);
115 CFReleaseNull(parameters);
116 SecKeyRef user_pubkey = SecKeyCreatePublicFromPrivate(user_privkey);
117
118 ok(SOSFullPeerInfoPromoteToApplication(fpi, user_privkey, &error), "Promote to Application");
119 ok(SOSPeerInfoApplicationVerify(SOSFullPeerInfoGetPeerInfo(fpi), user_pubkey, &error), "Promote to Application");
120
121 pi = SOSFullPeerInfoGetPeerInfo(fpi);
122 ok(PeerInfoRoundTrip(pi), "PeerInfo safely round-trips");
123
124 CFDataRef testBackupKey = CopyTestBackupKey();
125
126 ok(SOSFullPeerInfoUpdateBackupKey(fpi, testBackupKey, &error), "Set Backup (%@)", error);
127 CFReleaseNull(error);
128
129 CFReleaseNull(testBackupKey); // Make sure our ref doesn't save them.
130 testBackupKey = CopyTestBackupKey();
131
132 pi = SOSFullPeerInfoGetPeerInfo(fpi);
133 CFDataRef piBackupKey = SOSPeerInfoCopyBackupKey(pi);
134
135 ok(CFEqualSafe(testBackupKey, piBackupKey), "Same Backup Key");
136
137 ok(PeerInfoRoundTrip(pi), "PeerInfo safely round-trips with backup key");
138
139 CFReleaseNull(piBackupKey);
140 piBackupKey = SOSPeerInfoCopyBackupKey(pi);
141 ok(CFEqualSafe(testBackupKey, piBackupKey), "Same Backup Key after round trip");
142
143 // Don't own the piBackupKey key
144 CFReleaseNull(testBackupKey);
145 CFReleaseNull(piBackupKey);
146 CFReleaseNull(user_privkey);
147 CFReleaseNull(user_pubkey);
148
149 CFReleaseNull(signingKey);
150 CFReleaseNull(octagonSigningKey);
151 CFReleaseNull(fpi);
152 }
153
154 int sc_30_peerinfo(int argc, char *const *argv)
155 {
156 plan_tests(kTestTestCount);
157
158 tests();
159
160 return 0;
161 }