forked from emeraldsanto/react-native-encrypted-storage
-
Notifications
You must be signed in to change notification settings - Fork 3
/
RNEncryptedStorage.m
177 lines (144 loc) · 6.49 KB
/
RNEncryptedStorage.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
//
// RNEncryptedStorage.m
// Starter
//
// Created by Yanick Bélanger on 2020-02-09.
// Copyright © 2020 Facebook. All rights reserved.
//
#import "RNEncryptedStorage.h"
#import <Security/Security.h>
#import <React/RCTLog.h>
void rejectPromise(NSString *message, NSError *error, RCTPromiseRejectBlock rejecter)
{
NSString* errorCode = [NSString stringWithFormat:@"%ld", error.code];
NSString* errorMessage = [NSString stringWithFormat:@"RNEncryptedStorageError: %@", message];
rejecter(errorCode, errorMessage, error);
}
@implementation RNEncryptedStorage
+ (BOOL)requiresMainQueueSetup
{
return NO;
}
CFStringRef getKeychainAccessibility(NSDictionary *options)
{
id keychainAccessibility = options[@"keychainAccessibility"];
if (keychainAccessibility == nil) {
return kSecAttrAccessibleAfterFirstUnlock;
}
NSDictionary *valueMap = @{
@"kSecAttrAccessibleWhenPasscodeSetThisDeviceOnly": (__bridge NSString *)kSecAttrAccessibleWhenPasscodeSetThisDeviceOnly,
@"kSecAttrAccessibleWhenUnlockedThisDeviceOnly": (__bridge NSString *)kSecAttrAccessibleWhenUnlockedThisDeviceOnly,
@"kSecAttrAccessibleWhenUnlocked": (__bridge NSString *)kSecAttrAccessibleWhenUnlocked,
@"kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly": (__bridge NSString *)kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly,
@"kSecAttrAccessibleAfterFirstUnlock": (__bridge NSString *)kSecAttrAccessibleAfterFirstUnlock,
};
NSString *value = valueMap[keychainAccessibility];
return (__bridge CFStringRef)value;
}
NSString *getKeychainService(NSDictionary *options)
{
id keychainService = options[@"storageName"];
if (keychainService == nil) {
return [[NSBundle mainBundle] bundleIdentifier];
}
return keychainService;
}
RCT_EXPORT_MODULE();
RCT_EXPORT_METHOD(setItem:(NSString *)key withValue:(NSString *)value withOptions:(NSDictionary *) options resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
{
NSData* dataFromValue = [value dataUsingEncoding:NSUTF8StringEncoding];
if (dataFromValue == nil) {
NSError* error = [NSError errorWithDomain:[[NSBundle mainBundle] bundleIdentifier] code:0 userInfo: nil];
rejectPromise(@"An error occured while parsing value", error, reject);
return;
}
// Prepares the insert query structure
CFStringRef keychainAccessibility = getKeychainAccessibility(options);
NSString *keychainService = getKeychainService(options);
NSDictionary* storeQuery = @{
(__bridge id)kSecClass : (__bridge id)kSecClassGenericPassword,
(__bridge id)kSecAttrAccount : key,
(__bridge id)kSecValueData : dataFromValue,
(__bridge id)kSecAttrAccessible: (__bridge id)keychainAccessibility,
(__bridge id)kSecAttrService: keychainService
};
// Deletes the existing item prior to inserting the new one
SecItemDelete((__bridge CFDictionaryRef)storeQuery);
OSStatus insertStatus = SecItemAdd((__bridge CFDictionaryRef)storeQuery, nil);
if (insertStatus == noErr) {
resolve(value);
}
else {
NSError* error = [NSError errorWithDomain:[[NSBundle mainBundle] bundleIdentifier] code:insertStatus userInfo: nil];
rejectPromise(@"An error occured while saving value", error, reject);
}
}
RCT_EXPORT_METHOD(getItem:(NSString *)key withOptions:(NSDictionary *) options resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
{
NSString *keychainService = getKeychainService(options);
/*
The unique key for kSecClassGenericPassword is composed of: kSecAttrAccount and kSecAttrService
https://stackoverflow.com/a/22519700
*/
NSDictionary* getQuery = @{
(__bridge id)kSecClass : (__bridge id)kSecClassGenericPassword,
(__bridge id)kSecAttrAccount : key,
(__bridge id)kSecAttrService: keychainService,
(__bridge id)kSecReturnData : (__bridge id)kCFBooleanTrue,
(__bridge id)kSecMatchLimit : (__bridge id)kSecMatchLimitOne
};
CFTypeRef dataRef = NULL;
OSStatus getStatus = SecItemCopyMatching((__bridge CFDictionaryRef)getQuery, &dataRef);
if (getStatus == errSecSuccess) {
NSString* storedValue = [[NSString alloc] initWithData: (__bridge NSData*)dataRef encoding: NSUTF8StringEncoding];
resolve(storedValue);
}
else if (getStatus == errSecItemNotFound) {
resolve(nil);
}
else {
NSError* error = [NSError errorWithDomain: [[NSBundle mainBundle] bundleIdentifier] code:getStatus userInfo:nil];
rejectPromise(@"An error occured while retrieving value", error, reject);
}
}
RCT_EXPORT_METHOD(removeItem:(NSString *)key withOptions:(NSDictionary *) options resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
{
CFStringRef keychainAccessibility = getKeychainAccessibility(options);
NSString *keychainService = getKeychainService(options);
NSDictionary* removeQuery = @{
(__bridge id)kSecClass : (__bridge id)kSecClassGenericPassword,
(__bridge id)kSecAttrAccount : key,
(__bridge id)kSecReturnData : (__bridge id)kCFBooleanTrue,
(__bridge id)kSecAttrAccessible: (__bridge id)keychainAccessibility,
(__bridge id)kSecAttrService: keychainService
};
OSStatus removeStatus = SecItemDelete((__bridge CFDictionaryRef)removeQuery);
if (removeStatus == noErr) {
resolve(key);
}
else {
NSError* error = [NSError errorWithDomain:[[NSBundle mainBundle] bundleIdentifier] code:removeStatus userInfo: nil];
rejectPromise(@"An error occured while removing value", error, reject);
}
}
RCT_EXPORT_METHOD(clear:(NSDictionary *) options resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
{
NSArray *secItemClasses = @[
(__bridge id)kSecClassGenericPassword,
(__bridge id)kSecClassInternetPassword,
(__bridge id)kSecClassCertificate,
(__bridge id)kSecClassKey,
(__bridge id)kSecClassIdentity
];
NSString *keychainService = getKeychainService(options);
// Maps through all Keychain classes and deletes all items that match
for (id secItemClass in secItemClasses) {
NSDictionary *spec = @{
(__bridge id)kSecClass: secItemClass,
(__bridge id)kSecAttrService: keychainService
};
SecItemDelete((__bridge CFDictionaryRef)spec);
}
resolve(nil);
}
@end