|
| 1 | +// Copyright 2019 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include "auth/src/desktop/secure/user_secure_darwin_internal.h" |
| 16 | + |
| 17 | +#import <Foundation/Foundation.h> |
| 18 | +#import <Security/Security.h> |
| 19 | + |
| 20 | +namespace firebase { |
| 21 | +namespace auth { |
| 22 | +namespace secure { |
| 23 | + |
| 24 | +static const char kServicePrefix[] = "firebase.auth.cpp.cred/"; |
| 25 | +static const int kMaxAllowedKeychainEntries = INT_MAX; |
| 26 | + |
| 27 | +UserSecureDarwinInternal::UserSecureDarwinInternal(const char* service) { |
| 28 | + service_ = std::string(kServicePrefix) + service; |
| 29 | +} |
| 30 | + |
| 31 | +UserSecureDarwinInternal::~UserSecureDarwinInternal() {} |
| 32 | + |
| 33 | +NSMutableDictionary* GetQueryForApp(const char* service, const char* app) { |
| 34 | + // Create a query dictionary representing our service and the (optional) app name. |
| 35 | + // You can further narrow down this query to use it for saving or loading data. |
| 36 | + NSMutableDictionary* query = [[NSMutableDictionary alloc] init]; |
| 37 | + query[(__bridge id)kSecClass] = (__bridge id)kSecClassGenericPassword; |
| 38 | + query[(__bridge id)kSecAttrService] = @(service); |
| 39 | + if (app) { |
| 40 | + query[(__bridge id)kSecAttrAccount] = @(app); |
| 41 | + } |
| 42 | + return query; |
| 43 | +} |
| 44 | + |
| 45 | +std::string UserSecureDarwinInternal::GetKeystoreLocation(const std::string& app) { |
| 46 | + return service_ + "/" + app; |
| 47 | +} |
| 48 | + |
| 49 | +std::string UserSecureDarwinInternal::LoadUserData(const std::string& app_name) { |
| 50 | + NSMutableDictionary* query = GetQueryForApp(service_.c_str(), app_name.c_str()); |
| 51 | + std::string keystore_location = GetKeystoreLocation(app_name); |
| 52 | + // We want to return the data and attributes. |
| 53 | + query[(__bridge id)kSecReturnData] = @YES; |
| 54 | + query[(__bridge id)kSecReturnAttributes] = @YES; |
| 55 | + // Request up to 2 matches, so we can find out if there is more than one, which is an error. |
| 56 | + query[(__bridge id)kSecMatchLimit] = @2; |
| 57 | + |
| 58 | + CFArrayRef result = NULL; |
| 59 | + OSStatus status = SecItemCopyMatching((__bridge CFDictionaryRef)query, (CFTypeRef*)&result); |
| 60 | + |
| 61 | + if (status == errSecItemNotFound) { |
| 62 | + LogDebug("LoadUserData: Key %s not found", keystore_location.c_str()); |
| 63 | + return ""; |
| 64 | + } else if (status != noErr) { |
| 65 | + // Some other error occurred. |
| 66 | + NSString* error_string = (__bridge_transfer NSString*)SecCopyErrorMessageString(status, NULL); |
| 67 | + LogError("LoadUserData: Error %d reading %s: %s", status, keystore_location.c_str(), |
| 68 | + error_string.UTF8String); |
| 69 | + return ""; |
| 70 | + } |
| 71 | + NSArray* items = (__bridge_transfer NSArray*)result; |
| 72 | + if (items.count > 1) { |
| 73 | + LogError("LoadUserData: Error reading %s: Returned %d entries instead of 1. Deleting " |
| 74 | + "extraneous entries.", |
| 75 | + keystore_location.c_str(), items.count); |
| 76 | + // Delete all the invalid data so it doesn't mess us up next time. |
| 77 | + DeleteUserData(app_name); |
| 78 | + return ""; |
| 79 | + } |
| 80 | + NSDictionary* item = items[0]; |
| 81 | + // Grab the data from the single item in the array. |
| 82 | + NSData* data = item[(__bridge id)kSecValueData]; |
| 83 | + return std::string(reinterpret_cast<const char*>(data.bytes), data.length); |
| 84 | +} |
| 85 | + |
| 86 | +void UserSecureDarwinInternal::SaveUserData(const std::string& app_name, |
| 87 | + const std::string& user_data) { |
| 88 | + DeleteUserData(app_name); |
| 89 | + NSMutableDictionary* query = GetQueryForApp(service_.c_str(), app_name.c_str()); |
| 90 | + std::string keystore_location = GetKeystoreLocation(app_name); |
| 91 | + std::string comment = |
| 92 | + std::string("Firebase Auth persistent user data for ") + GetKeystoreLocation(app_name); |
| 93 | + // Set the binary data, what type of accesibility it should have, and a comment. |
| 94 | + NSDictionary* attributes = @{ |
| 95 | + (__bridge id) |
| 96 | + kSecValueData : [NSData dataWithBytes:reinterpret_cast<const void*>(user_data.c_str()) |
| 97 | + length:user_data.length()], |
| 98 | + (__bridge id)kSecAttrAccessible : (__bridge id)kSecAttrAccessibleWhenUnlockedThisDeviceOnly, |
| 99 | + (__bridge id)kSecAttrComment : @(comment.c_str()), |
| 100 | + }; |
| 101 | + |
| 102 | + NSMutableDictionary* combined = [attributes mutableCopy]; |
| 103 | + [combined addEntriesFromDictionary:query]; |
| 104 | + |
| 105 | + OSStatus status = SecItemAdd((__bridge CFDictionaryRef)combined, nil); |
| 106 | + |
| 107 | + if (status != noErr) { |
| 108 | + NSString* error_string = (__bridge_transfer NSString*)SecCopyErrorMessageString(status, NULL); |
| 109 | + LogError("SaveUserData: Error %d adding %s: %s", status, keystore_location.c_str(), |
| 110 | + error_string.UTF8String); |
| 111 | + return; |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +void UserSecureDarwinInternal::DeleteData(const char* app_name, const char* func_name) { |
| 116 | + NSMutableDictionary* query = GetQueryForApp(service_.c_str(), app_name); |
| 117 | + std::string keystore_location = app_name ? GetKeystoreLocation(app_name) : service_; |
| 118 | + |
| 119 | + // In case keychain data is invalid and there is more than one |
| 120 | + // matching entry, set the match limit to delete them all. |
| 121 | + query[(__bridge id)kSecMatchLimit] = [NSNumber numberWithInt:INT_MAX]; |
| 122 | + |
| 123 | + // Delete a specific matching entry. |
| 124 | + OSStatus status = SecItemDelete((__bridge CFDictionaryRef)query); |
| 125 | + |
| 126 | + if (status == errSecItemNotFound) { |
| 127 | + LogDebug("%s: Key %s not found", keystore_location.c_str()); |
| 128 | + return; |
| 129 | + } else if (status != noErr) { |
| 130 | + NSString* error_string = (__bridge_transfer NSString*)SecCopyErrorMessageString(status, NULL); |
| 131 | + LogError("%s: Error %d deleting %s: %s", status, keystore_location.c_str(), |
| 132 | + error_string.UTF8String); |
| 133 | + return; |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +void UserSecureDarwinInternal::DeleteUserData(const std::string& app_name) { |
| 138 | + DeleteData(app_name.c_str(), "DeleteUserData"); |
| 139 | +} |
| 140 | + |
| 141 | +void UserSecureDarwinInternal::DeleteAllData() { DeleteData(nullptr, "DeleteAllData"); } |
| 142 | + |
| 143 | +} // namespace secure |
| 144 | +} // namespace auth |
| 145 | +} // namespace firebase |
0 commit comments