]> git.saurik.com Git - apple/security.git/blob - OSX/sec/SOSCircle/SecureObjectSync/SOSInternal.h
Security-58286.1.32.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 enum {
41 // Public errors are first (See SOSCloudCircle)
42
43 kSOSErrorFirstPrivateError = 1024,
44
45 kSOSErrorAllocationFailure = 1024,
46 kSOSErrorEncodeFailure = 1025,
47 kSOSErrorNameMismatch = 1026,
48 kSOSErrorSendFailure = 1027,
49 kSOSErrorProcessingFailure = 1028,
50 kSOSErrorDecodeFailure = 1029,
51
52 kSOSErrorAlreadyPeer = 1030,
53 kSOSErrorNotApplicant = 1031,
54 kSOSErrorPeerNotFound = 1032,
55
56 kSOSErrorNoKey = 1033,
57 kSOSErrorBadKey = 1034,
58 kSOSErrorBadFormat = 1035,
59 kSOSErrorNoCircleName = 1036,
60 kSOSErrorNoCircle = 1037,
61 kSOSErrorBadSignature = 1038,
62 kSOSErrorReplay = 1039,
63
64 kSOSErrorUnexpectedType = 1040,
65
66 kSOSErrorUnsupported = 1041,
67 kSOSErrorInvalidMessage = 1042,
68 kSOSErrorNoRing = 1043,
69
70 kSOSErrorNoiCloudPeer = 1044,
71 kSOSErrorParam = 1045,
72 kSOSErrorNotInCircle = 1046,
73 };
74
75 typedef enum {
76 kSecIDSErrorNoDeviceID = -1, //default case
77 kSecIDSErrorNotRegistered = -2,
78 kSecIDSErrorFailedToSend=-3,
79 kSecIDSErrorCouldNotFindMatchingAuthToken = -4,
80 kSecIDSErrorDeviceIsLocked = -5,
81 kSecIDSErrorNoPeersAvailable = -6
82
83 } idsError;
84
85
86 extern const CFStringRef SOSTransportMessageTypeIDSV2;
87 extern const CFStringRef SOSTransportMessageTypeKVS;
88 extern const CFStringRef SOSTransportMessageTypeIDS;
89 extern const CFStringRef kSOSDSIDKey;
90
91 // Returns false unless errorCode is 0.
92 bool SOSErrorCreate(CFIndex errorCode, CFErrorRef *error, CFDictionaryRef formatOptions, CFStringRef descriptionString, ...);
93
94 bool SOSCreateError(CFIndex errorCode, CFStringRef descriptionString, CFErrorRef previousError, CFErrorRef *newError);
95
96 bool SOSCreateErrorWithFormat(CFIndex errorCode, CFErrorRef previousError, CFErrorRef *newError,
97 CFDictionaryRef formatOptions, CFStringRef formatString, ...)
98 CF_FORMAT_FUNCTION(5,6);
99
100 bool SOSCreateErrorWithFormatAndArguments(CFIndex errorCode, CFErrorRef previousError, CFErrorRef *newError,
101 CFDictionaryRef formatOptions, CFStringRef formatString, va_list args)
102 CF_FORMAT_FUNCTION(5,0);
103
104
105 static inline bool SOSClearErrorIfTrue(bool condition, CFErrorRef *error) {
106 if(condition && error && *error) {
107 secdebug("errorBug", "Got Success and Error (dropping error): %@", *error);
108 CFReleaseNull(*error);
109 }
110 return true;
111 }
112
113 static inline bool isSOSErrorCoded(CFErrorRef error, CFIndex sosErrorCode) {
114 return error && CFErrorGetCode(error) == sosErrorCode && CFEqualSafe(CFErrorGetDomain(error), kSOSErrorDomain);
115 }
116
117 //
118 // Backup Key handling
119 //
120 ccec_const_cp_t SOSGetBackupKeyCurveParameters(void);
121 bool SOSGenerateDeviceBackupFullKey(ccec_full_ctx_t generatedKey, ccec_const_cp_t cp, CFDataRef entropy, CFErrorRef* error);
122
123 bool SOSPerformWithDeviceBackupFullKey(ccec_const_cp_t cp, CFDataRef entropy, CFErrorRef *error, void (^operation)(ccec_full_ctx_t fullKey));
124 CFDataRef SOSCopyDeviceBackupPublicKey(CFDataRef entropy, CFErrorRef *error);
125
126 //
127 // Wrapping and Unwrapping
128 //
129
130 CFMutableDataRef SOSCopyECWrappedData(ccec_pub_ctx_t ec_ctx, CFDataRef data, CFErrorRef *error);
131 bool SOSPerformWithUnwrappedData(ccec_full_ctx_t ec_ctx, CFDataRef data, CFErrorRef *error,
132 void (^operation)(size_t size, uint8_t *buffer));
133 CFMutableDataRef SOSCopyECUnwrappedData(ccec_full_ctx_t ec_ctx, CFDataRef data, CFErrorRef *error);
134 //
135 // Utility Functions
136 //
137 OSStatus GenerateECPair(int keySize, SecKeyRef* public, SecKeyRef *full);
138 OSStatus GeneratePermanentECPair(int keySize, SecKeyRef* public, SecKeyRef *full);
139
140 CFStringRef SOSItemsChangedCopyDescription(CFDictionaryRef changes, bool is_sender);
141
142 CFStringRef SOSCopyIDOfDataBuffer(CFDataRef data, CFErrorRef *error);
143 CFStringRef SOSCopyIDOfDataBufferWithLength(CFDataRef data, CFIndex len, CFErrorRef *error);
144
145 CFStringRef SOSCopyIDOfKey(SecKeyRef key, CFErrorRef *error);
146 CFStringRef SOSCopyIDOfKeyWithLength(SecKeyRef key, CFIndex len, CFErrorRef *error);
147
148 //
149 // Der encoding accumulation
150 //
151 static inline bool accumulate_size(size_t *accumulator, size_t size) {
152 *accumulator += size;
153 return size != 0;
154 }
155
156 // Used for simple timestamping that's DERable (not durable)
157 CFDataRef SOSDateCreate(void);
158
159 CFDataRef CFDataCreateWithDER(CFAllocatorRef allocator, CFIndex size, uint8_t*(^operation)(size_t size, uint8_t *buffer));
160
161 extern const CFStringRef kSecIDSErrorDomain;
162 extern const CFStringRef kIDSOperationType;
163 extern const CFStringRef kIDSMessageToSendKey;
164 extern const CFStringRef kIDSMessageUniqueID;
165 extern const CFStringRef kIDSMessageRecipientPeerID;
166 extern const CFStringRef kIDSMessageRecipientDeviceID;
167 extern const CFStringRef kIDSMessageUsesAckModel;
168
169 __END_DECLS
170
171 #endif