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