]> git.saurik.com Git - apple/security.git/blob - OSX/sec/SOSCircle/SecureObjectSync/SOSInternal.h
Security-58286.270.3.0.1.tar.gz
[apple/security.git] / OSX / sec / SOSCircle / SecureObjectSync / SOSInternal.h
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 #ifndef _SOSINTERNAL_H_
26 #define _SOSINTERNAL_H_
27
28 #include <CoreFoundation/CoreFoundation.h>
29
30 #include <Security/SecKey.h>
31
32 #include <Security/SecureObjectSync/SOSCloudCircle.h>
33
34 #include <utilities/SecCFWrappers.h>
35
36 #include <corecrypto/ccec.h>
37
38 __BEGIN_DECLS
39
40 #define ENABLE_IDS 0
41
42 #define kSOSPeerIDLengthMax (26)
43 #define CC_STATISVALID 0x8000000000000000
44 #define CC_UKEY_TRUSTED 0x4000000000000000
45 #define CC_CAN_AUTH 0x2000000000000000
46 #define CC_PEER_IS_IN 0x1000000000000000
47 #define CC_MASK 0x0fffffffffffffff
48
49 enum {
50 // Public errors are first (See SOSCloudCircle)
51
52 kSOSErrorFirstPrivateError = 1024,
53
54 kSOSErrorAllocationFailure = 1024,
55 kSOSErrorEncodeFailure = 1025,
56 kSOSErrorNameMismatch = 1026,
57 kSOSErrorSendFailure = 1027,
58 kSOSErrorProcessingFailure = 1028,
59 kSOSErrorDecodeFailure = 1029,
60
61 kSOSErrorAlreadyPeer = 1030,
62 kSOSErrorNotApplicant = 1031,
63 kSOSErrorPeerNotFound = 1032,
64
65 kSOSErrorNoKey = 1033,
66 kSOSErrorBadKey = 1034,
67 kSOSErrorBadFormat = 1035,
68 kSOSErrorNoCircleName = 1036,
69 kSOSErrorNoCircle = 1037,
70 kSOSErrorBadSignature = 1038,
71 kSOSErrorReplay = 1039,
72
73 kSOSErrorUnexpectedType = 1040,
74
75 kSOSErrorUnsupported = 1041,
76 kSOSErrorInvalidMessage = 1042,
77 kSOSErrorNoRing = 1043,
78
79 kSOSErrorNoiCloudPeer = 1044,
80 kSOSErrorParam = 1045,
81 kSOSErrorNotInCircle = 1046,
82 };
83
84 extern const CFStringRef SOSTransportMessageTypeIDSV2;
85 extern const CFStringRef SOSTransportMessageTypeKVS;
86 extern const CFStringRef kSOSDSIDKey;
87 extern const SOSCCStatus kSOSNoCachedValue;
88
89 // Returns false unless errorCode is 0.
90 bool SOSErrorCreate(CFIndex errorCode, CFErrorRef *error, CFDictionaryRef formatOptions, CFStringRef descriptionString, ...);
91
92 bool SOSCreateError(CFIndex errorCode, CFStringRef descriptionString, CFErrorRef previousError, CFErrorRef *newError);
93
94 bool SOSCreateErrorWithFormat(CFIndex errorCode, CFErrorRef previousError, CFErrorRef *newError,
95 CFDictionaryRef formatOptions, CFStringRef formatString, ...)
96 CF_FORMAT_FUNCTION(5,6);
97
98 bool SOSCreateErrorWithFormatAndArguments(CFIndex errorCode, CFErrorRef previousError, CFErrorRef *newError,
99 CFDictionaryRef formatOptions, CFStringRef formatString, va_list args)
100 CF_FORMAT_FUNCTION(5,0);
101
102
103 static inline bool SOSClearErrorIfTrue(bool condition, CFErrorRef *error) {
104 if(condition && error && *error) {
105 secdebug("errorBug", "Got Success and Error (dropping error): %@", *error);
106 CFReleaseNull(*error);
107 }
108 return true;
109 }
110
111 static inline bool isSOSErrorCoded(CFErrorRef error, CFIndex sosErrorCode) {
112 return error && CFErrorGetCode(error) == sosErrorCode && CFEqualSafe(CFErrorGetDomain(error), kSOSErrorDomain);
113 }
114
115 //
116 // Backup Key handling
117 //
118 ccec_const_cp_t SOSGetBackupKeyCurveParameters(void);
119 bool SOSGenerateDeviceBackupFullKey(ccec_full_ctx_t generatedKey, ccec_const_cp_t cp, CFDataRef entropy, CFErrorRef* error);
120
121 bool SOSPerformWithDeviceBackupFullKey(ccec_const_cp_t cp, CFDataRef entropy, CFErrorRef *error, void (^operation)(ccec_full_ctx_t fullKey));
122 CFDataRef SOSCopyDeviceBackupPublicKey(CFDataRef entropy, CFErrorRef *error);
123
124 //
125 // Wrapping and Unwrapping
126 //
127
128 CFMutableDataRef SOSCopyECWrappedData(ccec_pub_ctx_t ec_ctx, CFDataRef data, CFErrorRef *error);
129 bool SOSPerformWithUnwrappedData(ccec_full_ctx_t ec_ctx, CFDataRef data, CFErrorRef *error,
130 void (^operation)(size_t size, uint8_t *buffer));
131 CFMutableDataRef SOSCopyECUnwrappedData(ccec_full_ctx_t ec_ctx, CFDataRef data, CFErrorRef *error);
132 //
133 // Utility Functions
134 //
135 OSStatus GenerateECPair(int keySize, SecKeyRef* public, SecKeyRef *full);
136 OSStatus GeneratePermanentECPair(int keySize, SecKeyRef* public, SecKeyRef *full);
137
138 CFStringRef SOSItemsChangedCopyDescription(CFDictionaryRef changes, bool is_sender);
139
140 CFStringRef SOSCopyHashBufAsString(uint8_t *digest, size_t len);
141 CFStringRef SOSCopyIDOfDataBuffer(CFDataRef data, CFErrorRef *error);
142 CFStringRef SOSCopyIDOfDataBufferWithLength(CFDataRef data, CFIndex len, CFErrorRef *error);
143
144 CFStringRef SOSCopyIDOfKey(SecKeyRef key, CFErrorRef *error);
145 CFStringRef SOSCopyIDOfKeyWithLength(SecKeyRef key, CFIndex len, CFErrorRef *error);
146
147 //
148 // Der encoding accumulation
149 //
150 static inline bool accumulate_size(size_t *accumulator, size_t size) {
151 *accumulator += size;
152 return size != 0;
153 }
154
155 // Used for simple timestamping that's DERable (not durable)
156 CFDataRef SOSDateCreate(void);
157
158 CFDataRef CFDataCreateWithDER(CFAllocatorRef allocator, CFIndex size, uint8_t*(^operation)(size_t size, uint8_t *buffer));
159
160
161 // Expanded notification utilities
162 #if __OBJC__
163 @interface SOSCachedNotification : NSObject
164 - (instancetype)init NS_UNAVAILABLE;
165 + (NSString *)notificationName:(const char *)notificationString;
166 @end
167 #endif
168
169 bool SOSCachedNotificationOperation(const char *notificationString, bool (^operation) (int token, bool gtg));
170 uint64_t SOSGetCachedCircleBitmask(void);
171 SOSCCStatus SOSGetCachedCircleStatus(CFErrorRef *error);
172 uint64_t SOSCachedViewBitmask(void);
173 CFSetRef SOSCreateCachedViewStatus(void);
174
175
176
177 __END_DECLS
178
179 #endif