]> git.saurik.com Git - apple/security.git/blob - keychain/ckks/CloudKitCategories.m
Security-59754.80.3.tar.gz
[apple/security.git] / keychain / ckks / CloudKitCategories.m
1 /*
2 * Copyright (c) 2017 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 #if OCTAGON
25
26 #import "keychain/ckks/CloudKitCategories.h"
27
28 @implementation CKOperationGroup (CKKS)
29 +(instancetype) CKKSGroupWithName:(NSString*)name {
30 CKOperationGroup* operationGroup = [[CKOperationGroup alloc] init];
31 operationGroup.expectedSendSize = CKOperationGroupTransferSizeKilobytes;
32 operationGroup.expectedReceiveSize = CKOperationGroupTransferSizeKilobytes;
33 operationGroup.name = name;
34 return operationGroup;
35 }
36 @end
37
38 @implementation NSError (CKKS)
39
40 -(bool) ckksIsCKErrorRecordChangedError {
41 NSDictionary<CKRecordID*,NSError*>* partialErrors = self.userInfo[CKPartialErrorsByItemIDKey];
42 if([self.domain isEqualToString:CKErrorDomain] && self.code == CKErrorPartialFailure && partialErrors) {
43 // Check if this error was "you're out of date"
44
45 for(NSError* error in partialErrors.objectEnumerator) {
46 if((![error.domain isEqualToString:CKErrorDomain]) || (error.code != CKErrorBatchRequestFailed && error.code != CKErrorServerRecordChanged && error.code != CKErrorUnknownItem)) {
47 // There's an error in there that isn't CKErrorServerRecordChanged, CKErrorBatchRequestFailed, or CKErrorUnknownItem. Don't handle nicely...
48 return false;
49 }
50 }
51
52 return true;
53 }
54 return false;
55 }
56
57 - (BOOL)isCuttlefishError:(CuttlefishErrorCode)cuttlefishErrorCode
58 {
59 NSError *error = self;
60
61 if ([error.domain isEqualToString:CKErrorDomain] && error.code == CKErrorServerRejectedRequest) {
62 NSError* underlyingError = error.userInfo[NSUnderlyingErrorKey];
63
64 if([underlyingError.domain isEqualToString:CKInternalErrorDomain] && underlyingError.code == CKErrorInternalPluginError) {
65 NSError* cuttlefishError = underlyingError.userInfo[NSUnderlyingErrorKey];
66
67 if([cuttlefishError.domain isEqualToString:CuttlefishErrorDomain] && cuttlefishError.code == cuttlefishErrorCode) {
68 return YES;
69 }
70 }
71 }
72 return NO;
73 }
74
75 - (NSTimeInterval)cuttlefishRetryAfter {
76 NSError *error = self;
77
78 if ([error.domain isEqualToString:CKErrorDomain] && error.code == CKErrorServerRejectedRequest) {
79 NSError* underlyingError = error.userInfo[NSUnderlyingErrorKey];
80
81 if([underlyingError.domain isEqualToString:CKInternalErrorDomain] && underlyingError.code == CKErrorInternalPluginError) {
82 NSError* cuttlefishError = underlyingError.userInfo[NSUnderlyingErrorKey];
83
84 if([cuttlefishError.domain isEqualToString:CuttlefishErrorDomain]) {
85 NSNumber* val = cuttlefishError.userInfo[CuttlefishErrorRetryAfterKey];
86 if (val != nil) {
87 return (NSTimeInterval)val.doubleValue;
88 }
89 }
90 }
91 }
92 return 0;
93 }
94
95
96 - (BOOL)isCKKSServerPluginError:(NSInteger)code
97 {
98 NSError* underlyingError = self.userInfo[NSUnderlyingErrorKey];
99 NSError* thirdLevelError = underlyingError.userInfo[NSUnderlyingErrorKey];
100
101 return ([self.domain isEqualToString:CKErrorDomain] &&
102 self.code == CKErrorServerRejectedRequest &&
103 underlyingError &&
104 [underlyingError.domain isEqualToString:CKInternalErrorDomain] &&
105 underlyingError.code == CKErrorInternalPluginError &&
106 thirdLevelError &&
107 [thirdLevelError.domain isEqualToString:@"CloudkitKeychainService"] &&
108 thirdLevelError.code == code);
109 }
110
111 @end
112
113 @implementation CKAccountInfo (CKKS)
114 // Ugly, and might break if CloudKit changes how they print objects. Sorry, CloudKit!
115 - (NSString*)description {
116 NSString* ckprop = [self CKPropertiesDescription];
117 NSString* description = [NSString stringWithFormat: @"<CKAccountInfo: %@>", ckprop];
118 return description;
119 }
120 @end
121
122 #endif //OCTAGON