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 // SOSPersistentState.m
29 #import <Security/Security.h>
30 #import <Foundation/NSPropertyList.h>
31 #import <Foundation/NSData.h>
32 #import <Foundation/NSDictionary.h>
33 #import <utilities/debugging.h>
34 #import <utilities/SecFileLocations.h>
36 #import "CKDPersistentState.h"
38 #if ! __has_feature(objc_arc)
39 #error This file must be compiled with ARC. Either turn on ARC for the project or use -fobjc-arc flag
42 // may want to have this hold incoming events in file as well
44 // TODO: Sandbox stuff
46 static CFStringRef kRegistrationFileName = CFSTR("com.apple.security.cloudkeychainproxy3.keysToRegister.plist");
48 #define DEBUGSCOPEPW "keytrace"
50 @implementation SOSPersistentState
52 + (BOOL)write:(NSURL *)path data:(id)plist error:(NSError **)error
54 if (![NSPropertyListSerialization propertyList: plist isValidForFormat: NSPropertyListXMLFormat_v1_0])
56 secerror("can't save PersistentState as XML");
60 NSData *data = [NSPropertyListSerialization dataWithPropertyList: plist
61 format: NSPropertyListXMLFormat_v1_0 options: 0 error: error];
64 secerror("error serializing PersistentState to xml: %@", *error);
68 BOOL writeStatus = [data writeToURL: path options: NSDataWritingAtomic error: error];
70 secerror("error writing PersistentState to file: %@", *error);
75 + (id)read: (NSURL *)path error:(NSError **)error
77 NSData *data = [NSData dataWithContentsOfURL: path options: 0 error: error];
80 secdebug(DEBUGSCOPEPW, "error reading PersistentState from %@: %@", path, *error);
84 // Now the deserializing:
86 NSPropertyListFormat format;
87 id plist = [NSPropertyListSerialization propertyListWithData: data
88 options: NSPropertyListMutableContainersAndLeaves format: &format error: error];
91 secerror("could not deserialize PersistentState from %@: %@", path, *error);
96 + (NSURL *)registrationFileURL
98 return (NSURL *)CFBridgingRelease(SecCopyURLForFileInPreferencesDirectory(kRegistrationFileName));
101 + (NSMutableDictionary *)registeredKeys
103 NSError *error = NULL;
104 id stateDictionary = [SOSPersistentState read:[[self class] registrationFileURL] error:&error];
105 secdebug(DEBUGSCOPEPW, "Read persistent stateDictionary: %@", stateDictionary);
106 // Ignore older states with an NSArray
107 if (![stateDictionary isKindOfClass:[NSDictionary class]])
109 return [NSMutableDictionary dictionaryWithDictionary:stateDictionary];
112 + (void)setRegisteredKeys: (NSDictionary *)keysToRegister
114 NSError *error = NULL;
115 secdebug(DEBUGSCOPEPW, "Write persistent keysToRegister: %@", keysToRegister);
116 [SOSPersistentState write:[[self class] registrationFileURL] data:keysToRegister error:&error];