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/NSArray.h>
32 #import <Foundation/NSPropertyList.h>
33 #import <Foundation/NSData.h>
34 #import <Foundation/NSDictionary.h>
35 #import <utilities/debugging.h>
36 #import <utilities/SecFileLocations.h>
38 #import "CKDPersistentState.h"
40 #if ! __has_feature(objc_arc)
41 #error This file must be compiled with ARC. Either turn on ARC for the project or use -fobjc-arc flag
44 // may want to have this hold incoming events in file as well
46 // TODO: Sandbox stuff
48 static CFStringRef kRegistrationFileName = CFSTR("com.apple.security.cloudkeychainproxy3.keysToRegister.plist");
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("keyregister", "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 + (NSString *)dictionaryDescription: (NSDictionary *)state
103 NSMutableArray *elements = [NSMutableArray array];
104 [state enumerateKeysAndObjectsUsingBlock: ^(NSString *key, id obj, BOOL *stop) {
105 [elements addObject: [key stringByAppendingString: @":"]];
106 if ([obj isKindOfClass:[NSArray class]]) {
107 [elements addObject: [(NSArray *)obj componentsJoinedByString: @" "]];
109 [elements addObject: [NSString stringWithFormat:@"%@", obj]];
112 return [elements componentsJoinedByString: @" "];
115 + (NSMutableDictionary *)registeredKeys
117 NSError *error = NULL;
118 id stateDictionary = [SOSPersistentState read:[[self class] registrationFileURL] error:&error];
119 secdebug("keyregister", "Read registeredKeys: <%@>", [self dictionaryDescription: stateDictionary]);
120 // Ignore older states with an NSArray
121 if (![stateDictionary isKindOfClass:[NSDictionary class]])
123 return [NSMutableDictionary dictionaryWithDictionary:stateDictionary];
126 + (void)setRegisteredKeys: (NSDictionary *)keysToRegister
128 NSError *error = NULL;
129 secdebug("keyregister", "Write registeredKeys: <%@>", [self dictionaryDescription: keysToRegister]);
130 [SOSPersistentState write:[[self class] registrationFileURL] data:keysToRegister error:&error];