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"
29 #if __has_include(<libaks.h>)
32 #if __has_include(<libaks_ref_key.h>)
33 #import <libaks_ref_key.h>
37 #import <Foundation/Foundation.h>
38 #import <CoreData/CoreData.h>
39 #import <SecurityFoundation/APIMacros.h>
41 @
class SecCDKeychainItemMetadata
;
42 @
class SecCDKeychainLookupTuple
;
43 @
class SecCDKeychainManagedItemType
;
44 @
class SecCDKeychainAccessControlEntity
;
45 @
class SFKeychainServerConnection
;
48 NS_ASSUME_NONNULL_BEGIN
50 @
class SecCDKeychainItem
;
52 @protocol SecCDKeychainLookupValueType
<NSObject
>
54 typedef NSString
<SecCDKeychainLookupValueType
> SecCDKeychainLookupValueType
;
56 extern SecCDKeychainLookupValueType
* const SecCDKeychainLookupValueTypeString
;
57 extern SecCDKeychainLookupValueType
* const SecCDKeychainLookupValueTypeData
;
58 extern SecCDKeychainLookupValueType
* const SecCDKeychainLookupValueTypeNumber
;
59 extern SecCDKeychainLookupValueType
* const SecCDKeychainLookupValueTypeDate
;
60 extern SecCDKeychainLookupValueType
* const SecCDKeychainLookupValueTypeArray
;
61 extern SecCDKeychainLookupValueType
* const SecCDKeychainLookupValueTypeDictionary
;
63 @interface SecCDKeychain
: NSObject
65 - (instancetype
)init NS_UNAVAILABLE
;
66 - (instancetype
)initWithStorageURL
:(NSURL
*)persistentStoreURL modelURL
:(NSURL
*)managedObjectURL encryptDatabase
:(bool)encryptDatabase
;
68 - (void)insertItems
:(NSArray
<SecCDKeychainItem
*>*)items withConnection
:(SFKeychainServerConnection
*)connection completionHandler
:(void (^)(bool success
, NSError
* _Nullable error
))completionHandler
;
70 - (void)fetchItemForPersistentID
:(NSUUID
*)persistentID withConnection
:(SFKeychainServerConnection
*)connection completionHandler
:(void (^)(SecCDKeychainItem
* _Nullable item
, NSError
* _Nullable error
))completionHandler
;
71 - (void)fetchItemsWithValue
:(NSString
*)value forLookupKey
:(NSString
*)lookupKey ofType
:(SecCDKeychainLookupValueType
*)lookupValueType withConnection
:(SFKeychainServerConnection
*)connection completionHandler
:(void (^)(NSArray
<SecCDKeychainItemMetadata
*>* items
, NSError
* error
))completionHandler
;
73 - (void)deleteItemWithPersistentID
:(NSUUID
*)persistentID withConnection
:(SFKeychainServerConnection
*)connection completionHandler
:(void (^)(bool success
, NSError
* _Nullable error
))completionHandler
;
77 @interface SecCDKeychainItemType
: NSObject
79 @
property (readonly
, copy
) NSString
* name
;
80 @
property (readonly
) int32_t version
;
82 // for both primaryKeys and syncableKeys, nil means "all the attributes"
83 @
property (readonly
, copy
, nullable
) NSArray
* primaryKeys
;
84 @
property (readonly
, copy
, nullable
) NSArray
* syncableKeys
;
86 @
property (readonly
) SecCDKeychainManagedItemType
* managedItemType
;
88 // subclasses must override
89 + (nullable instancetype
)itemType
;
90 + (nullable instancetype
)itemTypeForVersion
:(int32_t)version
;
92 // to be called only by subclass implementations of +itemType
93 - (instancetype
)_initWithName
:(NSString
*)name version
:(int32_t)version primaryKeys
:(nullable NSArray
*)primaryKeys syncableKeys
:(nullable NSArray
*)syncableKeys
;
97 @interface SecCDKeychainItemMetadata
: NSObject
99 @
property (readonly
) SecCDKeychainItemType
* itemType
;
100 @
property (readonly
) SecCDKeychainAccessControlEntity
* owner
;
101 @
property (readonly
) NSUUID
* persistentID
;
102 @
property (readonly
, copy
) NSDictionary
* attributes
;
103 @
property (readonly
, copy
) NSArray
<SecCDKeychainLookupTuple
*>* lookupAttributes
;
104 @
property (readonly
) keyclass_t keyclass
;
106 - (instancetype
)init NS_UNAVAILABLE
;
107 - (void)fetchFullItemWithKeychain
:(SecCDKeychain
*)keychain withConnection
:(SFKeychainServerConnection
*)connection completionHandler
:(void (^)(SecCDKeychainItem
* _Nullable item
, NSError
* _Nullable error
))completionHandler
;
111 @interface SecCDKeychainItem
: NSObject
113 @
property (readonly
) SecCDKeychainItemType
* itemType
;
114 @
property (readonly
) SecCDKeychainAccessControlEntity
* owner
;
115 @
property (readonly
) NSUUID
* persistentID
;
116 @
property (readonly
) NSDictionary
* attributes
;
117 @
property (readonly
) NSArray
<SecCDKeychainLookupTuple
*>* lookupAttributes
;
118 @
property (readonly
) keyclass_t keyclass
;
119 @
property (readonly
) NSDictionary
* secrets
;
121 @
property (readonly
) SecCDKeychainItemMetadata
* metadata
;
123 - (instancetype
)init NS_UNAVAILABLE
;
124 - (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
;
128 @interface SecCDKeychainLookupTuple
: NSObject
130 @
property (readonly
, copy
) NSString
* key
;
131 @
property (readonly
, copy
) id
<NSCopying
, NSObject
> value
;
132 @
property (readonly
, copy
) SecCDKeychainLookupValueType
* valueType
;
133 @
property (readonly
, copy
) NSString
* stringRepresentation
;
135 + (instancetype
)lookupTupleWithKey
:(NSString
*)key value
:(id
<NSCopying
, NSObject
>)value
;
137 - (instancetype
)init NS_UNAVAILABLE
;
138 - (instancetype
)initWithKey
:(NSString
*)key value
:(id
<NSCopying
, NSObject
>)value
;
142 typedef NS_ENUM(NSInteger
, SecCDKeychainAccessControlEntityType
) {
143 SecCDKeychainAccessControlEntityTypeAccessGroup
= 0,
146 @interface SecCDKeychainAccessControlEntity
: NSObject
148 @
property (nonatomic
, readonly
) SecCDKeychainAccessControlEntityType entityType
;
149 @
property (nonatomic
, readonly
) NSString
* stringRepresentation
;
151 + (instancetype
)accessControlEntityWithType
:(SecCDKeychainAccessControlEntityType
)type stringRepresentation
:(NSString
*)stringRepresentation
;
153 - (instancetype
)init NS_UNAVAILABLE
;
159 @protocol SecAKSRefKey
<NSObject
>
161 @
property (readonly
) NSData
* refKeyBlob
;
163 - (instancetype
)initWithKeybag
:(keybag_handle_t
)keybag keyclass
:(keyclass_t
)keyclass
;
164 - (instancetype
)initWithBlob
:(NSData
*)blob keybag
:(keybag_handle_t
)keybag
;
166 - (nullable NSData
*)wrappedDataForKey
:(SFAESKey
*)key
;
167 - (nullable SFAESKey
*)keyWithWrappedData
:(NSData
*)wrappedKeyData
;
171 @interface SecAKSRefKey
: NSObject
<SecAKSRefKey
>
174 #endif // USE_KEYSTORE
176 NS_ASSUME_NONNULL_END
178 #endif // !TARGET_OS_BRIDGE