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