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