Repository navigation
improvement(desktop): read browser safe storage keys in-process so keychain access is granted to sim #10684
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
improvement(desktop): read browser safe storage keys in-process so keychain access is granted to sim #10684
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
aa5fe55
improvement(desktop): read browser safe storage keys in-process so ke…
waleedlatif1 92db426
fix(desktop): copy keychain bytes without writing framework storage, …
waleedlatif1 ebb338a
fix(desktop): set up the throwaway keychain before the first lookup i…
waleedlatif1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,210 @@ | ||
| // memset_s is the C11 Annex K wipe the optimizer may not elide; string.h only declares it on request. | ||
| #define __STDC_WANT_LIB_EXT1__ 1 | ||
|
|
||
| #import <Foundation/Foundation.h> | ||
| #import <Security/Security.h> | ||
|
|
||
| #include <node_api.h> | ||
|
|
||
| #include <string.h> | ||
|
|
||
| #include <iterator> | ||
| #include <string> | ||
| #include <vector> | ||
|
|
||
| /** | ||
| * Reads a generic-password Keychain item inside the Sim process, so the | ||
| * Keychain prompt names Sim and "Always Allow" trusts Sim rather than a shared | ||
| * system tool that any other process could run. Nothing here caches a secret: | ||
| * each read copies the password into JS and wipes the buffer it owns. | ||
| */ | ||
|
|
||
| enum class ReadStatus { kFound, kMissing, kDenied, kFailed }; | ||
|
|
||
| struct ReadRequest { | ||
| std::string service; | ||
| std::string account; | ||
| std::vector<char> password; | ||
| ReadStatus status = ReadStatus::kFailed; | ||
| OSStatus os_status = errSecSuccess; | ||
| napi_deferred deferred = nullptr; | ||
| napi_async_work work = nullptr; | ||
| }; | ||
|
|
||
| static void Wipe(std::vector<char>* buffer) { | ||
| if (!buffer->empty()) { | ||
| memset_s(buffer->data(), buffer->size(), 0, buffer->size()); | ||
| } | ||
| buffer->clear(); | ||
| } | ||
|
|
||
| static NSString* StringFromUTF8(const std::string& value) { | ||
| return [[NSString alloc] initWithBytes:value.data() | ||
| length:value.size() | ||
| encoding:NSUTF8StringEncoding]; | ||
| } | ||
|
|
||
| static ReadStatus StatusFor(OSStatus status) { | ||
| switch (status) { | ||
| case errSecItemNotFound: | ||
| return ReadStatus::kMissing; | ||
| case errSecUserCanceled: | ||
| case errSecAuthFailed: | ||
| case errSecInteractionNotAllowed: | ||
| return ReadStatus::kDenied; | ||
| default: | ||
| return ReadStatus::kFailed; | ||
| } | ||
| } | ||
|
|
||
| /** Runs off the JS thread: the call blocks for as long as the user sits on the Keychain prompt. */ | ||
| static void ExecuteRead(napi_env env, void* data) { | ||
| (void)env; | ||
| ReadRequest* request = static_cast<ReadRequest*>(data); | ||
| @autoreleasepool { | ||
| NSString* service = StringFromUTF8(request->service); | ||
| NSString* account = StringFromUTF8(request->account); | ||
| if (!service || !account) { | ||
| request->status = ReadStatus::kFailed; | ||
| request->os_status = errSecParam; | ||
| return; | ||
| } | ||
| NSDictionary* query = @{ | ||
| (__bridge id)kSecClass : (__bridge id)kSecClassGenericPassword, | ||
| (__bridge id)kSecAttrService : service, | ||
| (__bridge id)kSecAttrAccount : account, | ||
| (__bridge id)kSecMatchLimit : (__bridge id)kSecMatchLimitOne, | ||
| (__bridge id)kSecReturnData : @YES, | ||
| }; | ||
| CFTypeRef result = nullptr; | ||
| OSStatus status = SecItemCopyMatching((__bridge CFDictionaryRef)query, &result); | ||
| request->os_status = status; | ||
| if (status == errSecSuccess) { | ||
| if (result && CFGetTypeID(result) == CFDataGetTypeID()) { | ||
| CFDataRef password = static_cast<CFDataRef>(result); | ||
| CFIndex length = CFDataGetLength(password); | ||
| if (length > 0) { | ||
| request->password.resize(static_cast<size_t>(length)); | ||
| CFDataGetBytes(password, CFRangeMake(0, length), | ||
| reinterpret_cast<UInt8*>(request->password.data())); | ||
| } | ||
| request->status = ReadStatus::kFound; | ||
| } else { | ||
| request->status = ReadStatus::kFailed; | ||
| } | ||
| } else { | ||
| request->status = StatusFor(status); | ||
| } | ||
| if (result) { | ||
| CFRelease(result); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| static const char* StatusName(ReadStatus status) { | ||
| switch (status) { | ||
| case ReadStatus::kFound: | ||
| return "found"; | ||
| case ReadStatus::kMissing: | ||
| return "missing"; | ||
| case ReadStatus::kDenied: | ||
| return "denied"; | ||
| case ReadStatus::kFailed: | ||
| return "failed"; | ||
| } | ||
| return "failed"; | ||
| } | ||
|
|
||
| static void SetString(napi_env env, napi_value object, const char* name, const char* value, | ||
| size_t length) { | ||
| napi_value string; | ||
| napi_create_string_utf8(env, value, length, &string); | ||
| napi_set_named_property(env, object, name, string); | ||
| } | ||
|
|
||
| static void CompleteRead(napi_env env, napi_status status, void* data) { | ||
| ReadRequest* request = static_cast<ReadRequest*>(data); | ||
| if (status == napi_ok) { | ||
| napi_value result; | ||
| napi_create_object(env, &result); | ||
| SetString(env, result, "status", StatusName(request->status), NAPI_AUTO_LENGTH); | ||
| if (request->status == ReadStatus::kFound) { | ||
| SetString(env, result, "password", request->password.data(), request->password.size()); | ||
| } else { | ||
| napi_value code; | ||
| napi_create_int32(env, request->os_status, &code); | ||
| napi_set_named_property(env, result, "code", code); | ||
| } | ||
| napi_resolve_deferred(env, request->deferred, result); | ||
| } else { | ||
| napi_value message; | ||
| napi_value error; | ||
| napi_create_string_utf8(env, "Keychain read was cancelled", NAPI_AUTO_LENGTH, &message); | ||
| napi_create_error(env, nullptr, message, &error); | ||
| napi_reject_deferred(env, request->deferred, error); | ||
| } | ||
| Wipe(&request->password); | ||
| napi_delete_async_work(env, request->work); | ||
| delete request; | ||
| } | ||
|
|
||
| static bool ReadString(napi_env env, napi_value value, const char* name, std::string* output) { | ||
| napi_valuetype type; | ||
| size_t length = 0; | ||
| if (napi_typeof(env, value, &type) != napi_ok || type != napi_string || | ||
| napi_get_value_string_utf8(env, value, nullptr, 0, &length) != napi_ok) { | ||
| std::string message = std::string(name) + " must be a string"; | ||
| napi_throw_type_error(env, nullptr, message.c_str()); | ||
| return false; | ||
| } | ||
| std::vector<char> buffer(length + 1); | ||
| if (napi_get_value_string_utf8(env, value, buffer.data(), buffer.size(), &length) != napi_ok) { | ||
| std::string message = std::string("could not read ") + name; | ||
| napi_throw_type_error(env, nullptr, message.c_str()); | ||
| return false; | ||
| } | ||
| output->assign(buffer.data(), length); | ||
| return true; | ||
| } | ||
|
|
||
| /** `readGenericPassword(service, account)` resolves `{ status, password?, code? }`. */ | ||
| static napi_value ReadGenericPassword(napi_env env, napi_callback_info info) { | ||
| size_t argument_count = 2; | ||
| napi_value arguments[2]; | ||
| if (napi_get_cb_info(env, info, &argument_count, arguments, nullptr, nullptr) != napi_ok || | ||
| argument_count != 2) { | ||
| napi_throw_type_error(env, nullptr, "readGenericPassword expects a service and an account"); | ||
| return nullptr; | ||
| } | ||
| ReadRequest* request = new ReadRequest(); | ||
| if (!ReadString(env, arguments[0], "service", &request->service) || | ||
| !ReadString(env, arguments[1], "account", &request->account)) { | ||
| delete request; | ||
| return nullptr; | ||
| } | ||
| napi_value promise; | ||
| napi_value resource_name; | ||
| if (napi_create_promise(env, &request->deferred, &promise) != napi_ok || | ||
| napi_create_string_utf8(env, "SimKeychainRead", NAPI_AUTO_LENGTH, &resource_name) != | ||
| napi_ok || | ||
| napi_create_async_work(env, nullptr, resource_name, ExecuteRead, CompleteRead, request, | ||
| &request->work) != napi_ok || | ||
| napi_queue_async_work(env, request->work) != napi_ok) { | ||
| if (request->work) { | ||
| napi_delete_async_work(env, request->work); | ||
| } | ||
| delete request; | ||
| napi_throw_error(env, nullptr, "could not start the Keychain read"); | ||
| return nullptr; | ||
| } | ||
| return promise; | ||
| } | ||
|
|
||
| NAPI_MODULE_INIT() { | ||
| napi_property_descriptor properties[] = { | ||
| {"readGenericPassword", nullptr, ReadGenericPassword, nullptr, nullptr, nullptr, | ||
| napi_default, nullptr}, | ||
| }; | ||
| napi_define_properties(env, exports, std::size(properties), properties); | ||
| return exports; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.