2 * Copyright (c) 2017 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@
24 #import "SecKeybagSupport.h"
30 #import <libaks_ref_key.h>
33 #import <Foundation/Foundation.h>
34 #import <CoreData/CoreData.h>
35 #import <SecurityFoundation/APIMacros.h>
37 @
class SecCDKeychainItemMetadata
;
38 @
class SecCDKeychainLookupTuple
;
39 @
class SecCDKeychainManagedItemType
;
40 @
class SecCDKeychainAccessControlEntity
;
41 @
class SFKeychainServerConnection
;
44 NS_ASSUME_NONNULL_BEGIN
46 @
class SecCDKeychainItem
;
48 @protocol SecCDKeychainLookupValueType
<NSObject
>
50 typedef NSString
<SecCDKeychainLookupValueType
> SecCDKeychainLookupValueType
;
52 extern SecCDKeychainLookupValueType
* const SecCDKeychainLookupValueTypeString
;
53 extern SecCDKeychainLookupValueType
* const SecCDKeychainLookupValueTypeData
;
54 extern SecCDKeychainLookupValueType
* const SecCDKeychainLookupValueTypeNumber
;
55 extern SecCDKeychainLookupValueType
* const SecCDKeychainLookupValueTypeDate
;
56 extern SecCDKeychainLookupValueType
* const SecCDKeychainLookupValueTypeArray
;
57 extern SecCDKeychainLookupValueType
* const SecCDKeychainLookupValueTypeDictionary
;
59 @interface SecCDKeychain
: NSObject
61 - (instancetype
)init NS_UNAVAILABLE
;
62 - (instancetype
)initWithStorageURL
:(NSURL
*)persistentStoreURL modelURL
:(NSURL
*)managedObjectURL encryptDatabase
:(bool)encryptDatabase
;
64 - (void)insertItems
:(NSArray
<SecCDKeychainItem
*>*)items withConnection
:(SFKeychainServerConnection
*)connection completionHandler
:(void (^)(bool success
, NSError
* _Nullable error
))completionHandler
;
66 - (void)fetchItemForPersistentID
:(NSUUID
*)persistentID withConnection
:(SFKeychainServerConnection
*)connection completionHandler
:(void (^)(SecCDKeychainItem
* _Nullable item
, NSError
* _Nullable error
))completionHandler
;
67 - (void)fetchItemsWithValue
:(NSString
*)value forLookupKey
:(NSString
*)lookupKey ofType
:(SecCDKeychainLookupValueType
*)lookupValueType withConnection
:(SFKeychainServerConnection
*)connection completionHandler
:(void (^)(NSArray
<SecCDKeychainItemMetadata
*>* items
, NSError
* error
))completionHandler
;
69 - (void)deleteItemWithPersistentID
:(NSUUID
*)persistentID withConnection
:(SFKeychainServerConnection
*)connection completionHandler
:(void (^)(bool success
, NSError
* _Nullable error
))completionHandler
;
73 @interface SecCDKeychainItemType
: NSObject
75 @
property (readonly
, copy
) NSString
* name
;
76 @
property (readonly
) int32_t version
;
78 // for both primaryKeys and syncableKeys, nil means "all the attributes"
79 @
property (readonly
, copy
, nullable
) NSArray
* primaryKeys
;
80 @
property (readonly
, copy
, nullable
) NSArray
* syncableKeys
;
82 @
property (readonly
) SecCDKeychainManagedItemType
* managedItemType
;
84 // subclasses must override
85 + (nullable instancetype
)itemType
;
86 + (nullable instancetype
)itemTypeForVersion
:(int32_t)version
;
88 // to be called only by subclass implementations of +itemType
89 - (instancetype
)_initWithName
:(NSString
*)name version
:(int32_t)version primaryKeys
:(nullable NSArray
*)primaryKeys syncableKeys
:(nullable NSArray
*)syncableKeys
;
93 @interface SecCDKeychainItemMetadata
: NSObject
95 @
property (readonly
) SecCDKeychainItemType
* itemType
;
96 @
property (readonly
) SecCDKeychainAccessControlEntity
* owner
;
97 @
property (readonly
) NSUUID
* persistentID
;
98 @
property (readonly
, copy
) NSDictionary
* attributes
;
99 @
property (readonly
, copy
) NSArray
<SecCDKeychainLookupTuple
*>* lookupAttributes
;
100 @
property (readonly
) keyclass_t keyclass
;
102 - (instancetype
)init NS_UNAVAILABLE
;
103 - (void)fetchFullItemWithKeychain
:(SecCDKeychain
*)keychain withConnection
:(SFKeychainServerConnection
*)connection completionHandler
:(void (^)(SecCDKeychainItem
* _Nullable item
, NSError
* _Nullable error
))completionHandler
;
107 @interface SecCDKeychainItem
: NSObject
109 @
property (readonly
) SecCDKeychainItemType
* itemType
;
110 @
property (readonly
) SecCDKeychainAccessControlEntity
* owner
;
111 @
property (readonly
) NSUUID
* persistentID
;
112 @
property (readonly
) NSDictionary
* attributes
;
113 @
property (readonly
) NSArray
<SecCDKeychainLookupTuple
*>* lookupAttributes
;
114 @
property (readonly
) keyclass_t keyclass
;
115 @
property (readonly
) NSDictionary
* secrets
;
117 @
property (readonly
) SecCDKeychainItemMetadata
* metadata
;
119 - (instancetype
)init NS_UNAVAILABLE
;
120 - (instancetype
)initItemType
:(SecCDKeychainItemType
*)itemType withPersistentID
:(NSUUID
*)persistentID attributes
:(NSDictionary
*)attributes lookupAttributes
:(nullable NSArray
<SecCDKeychainLookupTuple
*>*)lookupAttributes secrets
:(NSDictionary
*)secrets owner
:(SecCDKeychainAccessControlEntity
*)owner keyclass
:(keyclass_t
)keyclass
;
124 @interface SecCDKeychainLookupTuple
: NSObject
126 @
property (readonly
, copy
) NSString
* key
;
127 @
property (readonly
, copy
) id
<NSCopying
, NSObject
> value
;
128 @
property (readonly
, copy
) SecCDKeychainLookupValueType
* valueType
;
129 @
property (readonly
, copy
) NSString
* stringRepresentation
;
131 + (instancetype
)lookupTupleWithKey
:(NSString
*)key value
:(id
<NSCopying
, NSObject
>)value
;
133 - (instancetype
)init NS_UNAVAILABLE
;
134 - (instancetype
)initWithKey
:(NSString
*)key value
:(id
<NSCopying
, NSObject
>)value
;
138 typedef NS_ENUM(NSInteger
, SecCDKeychainAccessControlEntityType
) {
139 SecCDKeychainAccessControlEntityTypeAccessGroup
= 0,
142 @interface SecCDKeychainAccessControlEntity
: NSObject
144 @
property (nonatomic
, readonly
) SecCDKeychainAccessControlEntityType entityType
;
145 @
property (nonatomic
, readonly
) NSString
* stringRepresentation
;
147 + (instancetype
)accessControlEntityWithType
:(SecCDKeychainAccessControlEntityType
)type stringRepresentation
:(NSString
*)stringRepresentation
;
149 - (instancetype
)init NS_UNAVAILABLE
;
155 @protocol SecAKSRefKey
<NSObject
>
157 @
property (readonly
) NSData
* refKeyBlob
;
159 - (instancetype
)initWithKeybag
:(keybag_handle_t
)keybag keyclass
:(keyclass_t
)keyclass
;
160 - (instancetype
)initWithBlob
:(NSData
*)blob keybag
:(keybag_handle_t
)keybag
;
162 - (nullable NSData
*)wrappedDataForKey
:(SFAESKey
*)key
;
163 - (nullable SFAESKey
*)keyWithWrappedData
:(NSData
*)wrappedKeyData
;
167 @interface SecAKSRefKey
: NSObject
<SecAKSRefKey
>
170 #endif // USE_KEYSTORE
172 NS_ASSUME_NONNULL_END
174 #endif // !TARGET_OS_BRIDGE