]> git.saurik.com Git - apple/security.git/blob - Security/sec/SOSCircle/CloudKeychainProxy/CKDPersistentState.m
Security-57031.30.12.tar.gz
[apple/security.git] / Security / 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/NSData.h>
32 #import <Foundation/NSDictionary.h>
33 #import <utilities/debugging.h>
34 #import <utilities/SecFileLocations.h>
35
36 #import "CKDPersistentState.h"
37
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
40 #endif
41
42 // may want to have this hold incoming events in file as well
43
44 // TODO: Sandbox stuff
45
46 static CFStringRef kRegistrationFileName = CFSTR("com.apple.security.cloudkeychainproxy3.keysToRegister.plist");
47
48 #define DEBUGSCOPEPW "keytrace"
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(DEBUGSCOPEPW, "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 + (NSMutableDictionary *)registeredKeys
102 {
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]])
108 return NULL;
109 return [NSMutableDictionary dictionaryWithDictionary:stateDictionary];
110 }
111
112 + (void)setRegisteredKeys: (NSDictionary *)keysToRegister
113 {
114 NSError *error = NULL;
115 secdebug(DEBUGSCOPEPW, "Write persistent keysToRegister: %@", keysToRegister);
116 [SOSPersistentState write:[[self class] registrationFileURL] data:keysToRegister error:&error];
117 }
118
119 @end