]> git.saurik.com Git - apple/security.git/blob - OSX/sec/SOSCircle/CloudKeychainProxy/CKDPersistentState.m
Security-57336.10.29.tar.gz
[apple/security.git] / OSX / sec / SOSCircle / CloudKeychainProxy / CKDPersistentState.m
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 // SOSPersistentState.m
26 // ckdxpc
27 //
28
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>
37
38 #import "CKDPersistentState.h"
39
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
42 #endif
43
44 // may want to have this hold incoming events in file as well
45
46 // TODO: Sandbox stuff
47
48 static CFStringRef kRegistrationFileName = CFSTR("com.apple.security.cloudkeychainproxy3.keysToRegister.plist");
49
50 @implementation SOSPersistentState
51
52 + (BOOL)write:(NSURL *)path data:(id)plist error:(NSError **)error
53 {
54 if (![NSPropertyListSerialization propertyList: plist isValidForFormat: NSPropertyListXMLFormat_v1_0])
55 {
56 secerror("can't save PersistentState as XML");
57 return false;
58 }
59
60 NSData *data = [NSPropertyListSerialization dataWithPropertyList: plist
61 format: NSPropertyListXMLFormat_v1_0 options: 0 error: error];
62 if (data == nil)
63 {
64 secerror("error serializing PersistentState to xml: %@", *error);
65 return false;
66 }
67
68 BOOL writeStatus = [data writeToURL: path options: NSDataWritingAtomic error: error];
69 if (!writeStatus)
70 secerror("error writing PersistentState to file: %@", *error);
71
72 return writeStatus;
73 }
74
75 + (id)read: (NSURL *)path error:(NSError **)error
76 {
77 NSData *data = [NSData dataWithContentsOfURL: path options: 0 error: error];
78 if (data == nil)
79 {
80 secdebug("keyregister", "error reading PersistentState from %@: %@", path, *error);
81 return nil;
82 }
83
84 // Now the deserializing:
85
86 NSPropertyListFormat format;
87 id plist = [NSPropertyListSerialization propertyListWithData: data
88 options: NSPropertyListMutableContainersAndLeaves format: &format error: error];
89
90 if (plist == nil)
91 secerror("could not deserialize PersistentState from %@: %@", path, *error);
92
93 return plist;
94 }
95
96 + (NSURL *)registrationFileURL
97 {
98 return (NSURL *)CFBridgingRelease(SecCopyURLForFileInPreferencesDirectory(kRegistrationFileName));
99 }
100
101 + (NSString *)dictionaryDescription: (NSDictionary *)state
102 {
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: @" "]];
108 } else {
109 [elements addObject: [NSString stringWithFormat:@"%@", obj]];
110 }
111 }];
112 return [elements componentsJoinedByString: @" "];
113 }
114
115 + (NSMutableDictionary *)registeredKeys
116 {
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]])
122 return NULL;
123 return [NSMutableDictionary dictionaryWithDictionary:stateDictionary];
124 }
125
126 + (void)setRegisteredKeys: (NSDictionary *)keysToRegister
127 {
128 NSError *error = NULL;
129 secdebug("keyregister", "Write registeredKeys: <%@>", [self dictionaryDescription: keysToRegister]);
130 [SOSPersistentState write:[[self class] registrationFileURL] data:keysToRegister error:&error];
131 }
132
133 @end