2 * Copyright (c) 2012-2014 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
25 #ifndef _SOSINTERNAL_H_
26 #define _SOSINTERNAL_H_
28 #include <CoreFoundation/CoreFoundation.h>
30 #include <Security/SecKey.h>
32 #include <Security/SecureObjectSync/SOSCloudCircle.h>
34 #include <utilities/SecCFWrappers.h>
36 #include <corecrypto/ccec.h>
42 #define kSOSPeerIDLengthMax (26)
45 // Public errors are first (See SOSCloudCircle)
47 kSOSErrorFirstPrivateError
= 1024,
49 kSOSErrorAllocationFailure
= 1024,
50 kSOSErrorEncodeFailure
= 1025,
51 kSOSErrorNameMismatch
= 1026,
52 kSOSErrorSendFailure
= 1027,
53 kSOSErrorProcessingFailure
= 1028,
54 kSOSErrorDecodeFailure
= 1029,
56 kSOSErrorAlreadyPeer
= 1030,
57 kSOSErrorNotApplicant
= 1031,
58 kSOSErrorPeerNotFound
= 1032,
60 kSOSErrorNoKey
= 1033,
61 kSOSErrorBadKey
= 1034,
62 kSOSErrorBadFormat
= 1035,
63 kSOSErrorNoCircleName
= 1036,
64 kSOSErrorNoCircle
= 1037,
65 kSOSErrorBadSignature
= 1038,
66 kSOSErrorReplay
= 1039,
68 kSOSErrorUnexpectedType
= 1040,
70 kSOSErrorUnsupported
= 1041,
71 kSOSErrorInvalidMessage
= 1042,
72 kSOSErrorNoRing
= 1043,
74 kSOSErrorNoiCloudPeer
= 1044,
75 kSOSErrorParam
= 1045,
76 kSOSErrorNotInCircle
= 1046,
80 kSecIDSErrorNoDeviceID
= -1, //default case
81 kSecIDSErrorNotRegistered
= -2,
82 kSecIDSErrorFailedToSend
=-3,
83 kSecIDSErrorCouldNotFindMatchingAuthToken
= -4,
84 kSecIDSErrorDeviceIsLocked
= -5,
85 kSecIDSErrorNoPeersAvailable
= -6
90 extern const CFStringRef SOSTransportMessageTypeIDSV2
;
91 extern const CFStringRef SOSTransportMessageTypeKVS
;
92 extern const CFStringRef kSOSDSIDKey
;
94 // Returns false unless errorCode is 0.
95 bool SOSErrorCreate(CFIndex errorCode
, CFErrorRef
*error
, CFDictionaryRef formatOptions
, CFStringRef descriptionString
, ...);
97 bool SOSCreateError(CFIndex errorCode
, CFStringRef descriptionString
, CFErrorRef previousError
, CFErrorRef
*newError
);
99 bool SOSCreateErrorWithFormat(CFIndex errorCode
, CFErrorRef previousError
, CFErrorRef
*newError
,
100 CFDictionaryRef formatOptions
, CFStringRef formatString
, ...)
101 CF_FORMAT_FUNCTION(5,6);
103 bool SOSCreateErrorWithFormatAndArguments(CFIndex errorCode
, CFErrorRef previousError
, CFErrorRef
*newError
,
104 CFDictionaryRef formatOptions
, CFStringRef formatString
, va_list args
)
105 CF_FORMAT_FUNCTION(5,0);
108 static inline bool SOSClearErrorIfTrue(bool condition
, CFErrorRef
*error
) {
109 if(condition
&& error
&& *error
) {
110 secdebug("errorBug", "Got Success and Error (dropping error): %@", *error
);
111 CFReleaseNull(*error
);
116 static inline bool isSOSErrorCoded(CFErrorRef error
, CFIndex sosErrorCode
) {
117 return error
&& CFErrorGetCode(error
) == sosErrorCode
&& CFEqualSafe(CFErrorGetDomain(error
), kSOSErrorDomain
);
121 // Backup Key handling
123 ccec_const_cp_t
SOSGetBackupKeyCurveParameters(void);
124 bool SOSGenerateDeviceBackupFullKey(ccec_full_ctx_t generatedKey
, ccec_const_cp_t cp
, CFDataRef entropy
, CFErrorRef
* error
);
126 bool SOSPerformWithDeviceBackupFullKey(ccec_const_cp_t cp
, CFDataRef entropy
, CFErrorRef
*error
, void (^operation
)(ccec_full_ctx_t fullKey
));
127 CFDataRef
SOSCopyDeviceBackupPublicKey(CFDataRef entropy
, CFErrorRef
*error
);
130 // Wrapping and Unwrapping
133 CFMutableDataRef
SOSCopyECWrappedData(ccec_pub_ctx_t ec_ctx
, CFDataRef data
, CFErrorRef
*error
);
134 bool SOSPerformWithUnwrappedData(ccec_full_ctx_t ec_ctx
, CFDataRef data
, CFErrorRef
*error
,
135 void (^operation
)(size_t size
, uint8_t *buffer
));
136 CFMutableDataRef
SOSCopyECUnwrappedData(ccec_full_ctx_t ec_ctx
, CFDataRef data
, CFErrorRef
*error
);
140 OSStatus
GenerateECPair(int keySize
, SecKeyRef
* public, SecKeyRef
*full
);
141 OSStatus
GeneratePermanentECPair(int keySize
, SecKeyRef
* public, SecKeyRef
*full
);
143 CFStringRef
SOSItemsChangedCopyDescription(CFDictionaryRef changes
, bool is_sender
);
145 CFStringRef
SOSCopyIDOfDataBuffer(CFDataRef data
, CFErrorRef
*error
);
146 CFStringRef
SOSCopyIDOfDataBufferWithLength(CFDataRef data
, CFIndex len
, CFErrorRef
*error
);
148 CFStringRef
SOSCopyIDOfKey(SecKeyRef key
, CFErrorRef
*error
);
149 CFStringRef
SOSCopyIDOfKeyWithLength(SecKeyRef key
, CFIndex len
, CFErrorRef
*error
);
152 // Der encoding accumulation
154 static inline bool accumulate_size(size_t *accumulator
, size_t size
) {
155 *accumulator
+= size
;
159 // Used for simple timestamping that's DERable (not durable)
160 CFDataRef
SOSDateCreate(void);
162 CFDataRef
CFDataCreateWithDER(CFAllocatorRef allocator
, CFIndex size
, uint8_t*(^operation
)(size_t size
, uint8_t *buffer
));
164 extern const CFStringRef kSecIDSErrorDomain
;
165 extern const CFStringRef kIDSOperationType
;
166 extern const CFStringRef kIDSMessageToSendKey
;
167 extern const CFStringRef kIDSMessageUniqueID
;
168 extern const CFStringRef kIDSMessageRecipientPeerID
;
169 extern const CFStringRef kIDSMessageRecipientDeviceID
;
170 extern const CFStringRef kIDSMessageUsesAckModel
;
171 extern const CFStringRef kIDSMessageSenderDeviceID
;