#ifndef _SOSINTERNAL_H_
#define _SOSINTERNAL_H_
#include <CoreFoundation/CoreFoundation.h>
#include <Security/SecKey.h>
#include <Security/SecItemShim.h>
#include <Security/SecureObjectSync/SOSCloudCircle.h>
#include <utilities/SecCFWrappers.h>
#include <corecrypto/ccec.h>
__BEGIN_DECLS
#define ENABLE_IDS 0
#define kSOSPeerIDLengthMax (26)
#define CC_STATISVALID 0x8000000000000000
#define CC_UKEY_TRUSTED 0x4000000000000000
#define CC_CAN_AUTH 0x2000000000000000
#define CC_PEER_IS_IN 0x1000000000000000
#define CC_MASK 0x0fffffffffffffff
enum {
kSOSErrorFirstPrivateError = 1024,
kSOSErrorAllocationFailure = 1024,
kSOSErrorEncodeFailure = 1025,
kSOSErrorNameMismatch = 1026,
kSOSErrorSendFailure = 1027,
kSOSErrorProcessingFailure = 1028,
kSOSErrorDecodeFailure = 1029,
kSOSErrorAlreadyPeer = 1030,
kSOSErrorNotApplicant = 1031,
kSOSErrorPeerNotFound = 1032,
kSOSErrorNoKey = 1033,
kSOSErrorBadKey = 1034,
kSOSErrorBadFormat = 1035,
kSOSErrorNoCircleName = 1036,
kSOSErrorNoCircle = 1037,
kSOSErrorBadSignature = 1038,
kSOSErrorReplay = 1039,
kSOSErrorUnexpectedType = 1040,
kSOSErrorUnsupported = 1041,
kSOSErrorInvalidMessage = 1042,
kSOSErrorNoRing = 1043,
kSOSErrorNoiCloudPeer = 1044,
kSOSErrorParam = 1045,
kSOSErrorNotInCircle = 1046,
kSOSErrorKeysNeedAttention = 1047,
};
extern const CFStringRef SOSTransportMessageTypeIDSV2;
extern const CFStringRef SOSTransportMessageTypeKVS;
extern const CFStringRef kSOSDSIDKey;
extern const SOSCCStatus kSOSNoCachedValue;
bool SOSErrorCreate(CFIndex errorCode, CFErrorRef *error, CFDictionaryRef formatOptions, CFStringRef descriptionString, ...);
bool SOSCreateError(CFIndex errorCode, CFStringRef descriptionString, CFErrorRef previousError, CFErrorRef *newError);
bool SOSCreateErrorWithFormat(CFIndex errorCode, CFErrorRef previousError, CFErrorRef *newError,
CFDictionaryRef formatOptions, CFStringRef formatString, ...)
CF_FORMAT_FUNCTION(5,6);
bool SOSCreateErrorWithFormatAndArguments(CFIndex errorCode, CFErrorRef previousError, CFErrorRef *newError,
CFDictionaryRef formatOptions, CFStringRef formatString, va_list args)
CF_FORMAT_FUNCTION(5,0);
static inline bool SOSClearErrorIfTrue(bool condition, CFErrorRef *error) {
if(condition && error && *error) {
secdebug("errorBug", "Got Success and Error (dropping error): %@", *error);
CFReleaseNull(*error);
}
return true;
}
static inline bool isSOSErrorCoded(CFErrorRef error, CFIndex sosErrorCode) {
return error && CFErrorGetCode(error) == sosErrorCode && CFEqualSafe(CFErrorGetDomain(error), kSOSErrorDomain);
}
ccec_const_cp_t SOSGetBackupKeyCurveParameters(void);
bool SOSGenerateDeviceBackupFullKey(ccec_full_ctx_t generatedKey, ccec_const_cp_t cp, CFDataRef entropy, CFErrorRef* error);
bool SOSPerformWithDeviceBackupFullKey(ccec_const_cp_t cp, CFDataRef entropy, CFErrorRef *error, void (^operation)(ccec_full_ctx_t fullKey));
CFMutableDataRef SOSCopyECWrappedData(ccec_pub_ctx_t ec_ctx, CFDataRef data, CFErrorRef *error);
bool SOSPerformWithUnwrappedData(ccec_full_ctx_t ec_ctx, CFDataRef data, CFErrorRef *error,
void (^operation)(size_t size, uint8_t *buffer));
CFMutableDataRef SOSCopyECUnwrappedData(ccec_full_ctx_t ec_ctx, CFDataRef data, CFErrorRef *error);
OSStatus GenerateECPair(int keySize, SecKeyRef* public, SecKeyRef *full);
OSStatus GeneratePermanentECPair(int keySize, SecKeyRef* public, SecKeyRef *full);
CFStringRef SOSItemsChangedCopyDescription(CFDictionaryRef changes, bool is_sender);
CFStringRef SOSCopyHashBufAsString(uint8_t *digest, size_t len);
CFStringRef SOSCopyIDOfDataBuffer(CFDataRef data, CFErrorRef *error);
CFStringRef SOSCopyIDOfDataBufferWithLength(CFDataRef data, CFIndex len, CFErrorRef *error);
CFStringRef SOSCopyIDOfKey(SecKeyRef key, CFErrorRef *error);
CFStringRef SOSCopyIDOfKeyWithLength(SecKeyRef key, CFIndex len, CFErrorRef *error);
OS_WARN_RESULT
static inline bool accumulate_size(size_t *accumulator, size_t size) {
*accumulator += size;
return size != 0;
}
CFDataRef SOSDateCreate(void);
CFDataRef CFDataCreateWithDER(CFAllocatorRef allocator, CFIndex size, uint8_t*(^operation)(size_t size, uint8_t *buffer));
#if __OBJC__
@interface SOSCachedNotification : NSObject
- (instancetype)init NS_UNAVAILABLE;
+ (NSString *)notificationName:(const char *)notificationString;
@end
#endif
bool SOSCachedNotificationOperation(const char *notificationString, bool (^operation) (int token, bool gtg));
uint64_t SOSGetCachedCircleBitmask(void);
SOSCCStatus SOSGetCachedCircleStatus(CFErrorRef *error);
uint64_t SOSCachedViewBitmask(void);
CFSetRef SOSCreateCachedViewStatus(void);
#if __OBJC__
NSDate *SOSCreateRandomDateBetweenNowPlus(NSTimeInterval starting, NSTimeInterval ending);
#endif
__END_DECLS
#endif