]> git.saurik.com Git - apple/security.git/blob - OSX/sec/SOSCircle/Regressions/sc-130-resignationticket.c
d565e7977a9059b2d4ad6dacc69b7c456ac19173
[apple/security.git] / OSX / sec / SOSCircle / Regressions / sc-130-resignationticket.c
1 /*
2 * Copyright (c) 2013-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 #include <Security/SecBase.h>
26 #include <Security/SecItem.h>
27 #include <Security/SecKey.h>
28 #include <SOSPeerInfoDER.h>
29 #include <Security/SecureObjectSync/SOSCircle.h>
30 #include <Security/SecureObjectSync/SOSPeerInfo.h>
31 #include <Security/SecureObjectSync/SOSInternal.h>
32
33 #include <utilities/SecCFWrappers.h>
34
35 #include <CoreFoundation/CoreFoundation.h>
36
37 #include <stdlib.h>
38 #include <unistd.h>
39
40 #include "SOSCircle_regressions.h"
41
42 #include "SOSRegressionUtilities.h"
43
44 typedef struct piStuff_t {
45 SecKeyRef signingKey;
46 SecKeyRef octagonSigningKey;
47 SOSFullPeerInfoRef fpi;
48 SOSPeerInfoRef pi;
49 SOSPeerInfoRef resignation_ticket;
50 } piStuff;
51
52 static piStuff *makeSimplePeer(char *name) {
53 piStuff *pi = malloc(sizeof(piStuff));
54
55 if(!pi) return NULL;
56 pi->signingKey = NULL;
57 CFStringRef cfName = CFStringCreateWithCString(kCFAllocatorDefault, name, kCFStringEncodingMacRoman);
58 pi->fpi = SOSCreateFullPeerInfoFromName(cfName, &pi->signingKey, &pi->octagonSigningKey, NULL);
59 CFReleaseSafe(cfName);
60 pi->pi = SOSFullPeerInfoGetPeerInfo(pi->fpi);
61 pi->resignation_ticket = SOSPeerInfoCreateRetirementTicket(kCFAllocatorDefault, pi->signingKey, pi->pi, NULL);
62 return pi;
63 }
64
65 static void freeSimplePeer(piStuff *pi)
66 {
67 CFReleaseSafe(pi->fpi);
68 CFReleaseSafe(pi->signingKey);
69 CFReleaseSafe(pi->octagonSigningKey);
70 CFReleaseSafe(pi->resignation_ticket);
71 free(pi);
72 }
73
74 static inline bool retire_me(piStuff *pi, size_t seconds) {
75 return SOSPeerInfoRetireRetirementTicket(seconds, pi->resignation_ticket);
76 }
77
78
79 static inline bool chkBasicTicket(piStuff *pi) {
80 return CFEqual(SOSPeerInfoInspectRetirementTicket(pi->resignation_ticket, NULL), SOSPeerInfoGetPeerID(pi->pi));
81 }
82
83 static bool in_between_time(CFDateRef before, piStuff *pi, CFDateRef after) {
84 CFDateRef during = SOSPeerInfoGetRetirementDate(pi->resignation_ticket);
85 CFTimeInterval time1 = CFDateGetTimeIntervalSinceDate(before, during);
86 CFTimeInterval time2 = CFDateGetTimeIntervalSinceDate(during, after);
87 CFReleaseNull(during);
88 if(time1 >= 0.0) return false;
89 if(time2 >= 0.0) return false;
90 return true;
91 }
92
93 static bool PeerInfoRoundTrip(SOSPeerInfoRef pi) {
94 bool retval = false;
95 size_t size = SOSPeerInfoGetDEREncodedSize(pi, NULL);
96 uint8_t buffer[size];
97 const uint8_t *buffer_p = SOSPeerInfoEncodeToDER(pi, NULL, buffer, buffer + sizeof(buffer));
98 ok(buffer_p != NULL, "encode");
99 if(buffer_p == NULL) return false;
100 SOSPeerInfoRef pi2 = SOSPeerInfoCreateFromDER(NULL, NULL, &buffer_p, buffer + sizeof(buffer));
101 ok(pi2 != NULL, "decode");
102 if(!pi2) return false;
103 ok(CFEqual(pi, pi2), "Decode matches");
104 if(CFEqual(pi, pi2)) retval = true;
105 CFReleaseNull(pi2);
106 return retval;
107 }
108
109 static void tests(void)
110 {
111 CFDateRef before_time = CFDateCreate(NULL, CFAbsoluteTimeGetCurrent());
112 sleep(1);
113 piStuff *iPhone = makeSimplePeer("iPhone");
114 piStuff *iPad = makeSimplePeer("iPad");
115 piStuff *iMac = makeSimplePeer("iMac");
116 piStuff *iDrone = makeSimplePeer("iDrone");
117 sleep(1);
118 CFDateRef after_time = CFDateCreate(NULL, CFAbsoluteTimeGetCurrent());
119
120 ok(in_between_time(before_time, iPhone, after_time), "retirement date recorded correctly");
121 CFReleaseSafe(before_time);
122 CFReleaseSafe(after_time);
123 ok(chkBasicTicket(iPhone), "peer ID's Match");
124 ok(chkBasicTicket(iPad), "peer ID's Match");
125 ok(chkBasicTicket(iMac), "peer ID's Match");
126 ok(chkBasicTicket(iDrone), "peer ID's Match");
127
128 // ok(miss_signature(iDrone, iPad), "signature failure detected");
129
130 ok(!retire_me(iPhone, 10000), "ticket still valid");
131 sleep(2);
132 ok(retire_me(iPhone, 1), "ticket not valid");
133
134 CFDateRef retdate = SOSPeerInfoGetRetirementDate(iPhone->resignation_ticket);
135 ok(retdate != NULL, "got retirement date %@", retdate);
136 CFReleaseSafe(retdate);
137
138 ok(PeerInfoRoundTrip(iPhone->resignation_ticket), "retirement ticket safely DERs");
139 #if 0
140 CFDateRef appdate = NULL;
141 ok((appdate = SOSPeerInfoGetApplicationDate(iPhone->resignation_ticket)) != NULL, "got application date %@", appdate);
142 CFReleaseSafe(appdate);
143 #endif
144
145 freeSimplePeer(iPhone);
146 freeSimplePeer(iPad);
147 freeSimplePeer(iMac);
148 freeSimplePeer(iDrone);
149 }
150
151 static int kTestTestCount = 12;
152
153 int sc_130_resignationticket(int argc, char *const *argv)
154 {
155 plan_tests(kTestTestCount);
156
157 tests();
158
159 return 0;
160 }