|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Runtime.InteropServices; |
| 6 | +using System.Text; |
| 7 | + |
| 8 | +namespace Azure.Identity |
| 9 | +{ |
| 10 | + internal static class MacosNativeMethods |
| 11 | + { |
| 12 | + public const int SecStatusCodeSuccess = 0; |
| 13 | + public const int SecStatusCodeNoSuchKeychain = -25294; |
| 14 | + public const int SecStatusCodeInvalidKeychain = -25295; |
| 15 | + public const int SecStatusCodeAuthFailed = -25293; |
| 16 | + public const int SecStatusCodeDuplicateItem = -25299; |
| 17 | + public const int SecStatusCodeItemNotFound = -25300; |
| 18 | + public const int SecStatusCodeInteractionNotAllowed = -25308; |
| 19 | + public const int SecStatusCodeInteractionRequired = -25315; |
| 20 | + public const int SecStatusCodeNoSuchAttr = -25303; |
| 21 | + |
| 22 | + public readonly struct CFRange |
| 23 | + { |
| 24 | + public readonly int Location, Length; |
| 25 | + public CFRange (int location, int length) |
| 26 | + { |
| 27 | + Location = location; |
| 28 | + Length = length; |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + public static void SecKeychainFindGenericPassword(IntPtr keychainOrArray, string serviceName, string accountName, out int passwordLength, out IntPtr credentialsPtr, out IntPtr itemRef) |
| 33 | + { |
| 34 | + byte[] serviceNameBytes = Encoding.UTF8.GetBytes(serviceName); |
| 35 | + byte[] accountNameBytes = Encoding.UTF8.GetBytes(accountName); |
| 36 | + |
| 37 | + ThrowIfError(Imports.SecKeychainFindGenericPassword(keychainOrArray, serviceNameBytes.Length, serviceNameBytes, accountNameBytes.Length, accountNameBytes, out passwordLength, out credentialsPtr, out itemRef)); |
| 38 | + } |
| 39 | + |
| 40 | + public static void SecKeychainAddGenericPassword(IntPtr keychainOrArray, string serviceName, string accountName, string password, out IntPtr itemRef) |
| 41 | + { |
| 42 | + byte[] serviceNameBytes = Encoding.UTF8.GetBytes(serviceName); |
| 43 | + byte[] accountNameBytes = Encoding.UTF8.GetBytes(accountName); |
| 44 | + byte[] passwordBytes = Encoding.UTF8.GetBytes(password); |
| 45 | + |
| 46 | + ThrowIfError(Imports.SecKeychainAddGenericPassword(keychainOrArray, serviceNameBytes.Length, serviceNameBytes, accountNameBytes.Length, accountNameBytes, password.Length, passwordBytes, out itemRef)); |
| 47 | + } |
| 48 | + |
| 49 | + public static void SecKeychainItemDelete(IntPtr itemRef) => ThrowIfError(Imports.SecKeychainItemDelete(itemRef)); |
| 50 | + |
| 51 | + public static void SecKeychainItemFreeContent(IntPtr attrList, IntPtr data) => ThrowIfError(Imports.SecKeychainItemFreeContent(attrList, data)); |
| 52 | + |
| 53 | + public static void CFRelease(IntPtr cfRef) |
| 54 | + { |
| 55 | + if (cfRef != IntPtr.Zero) |
| 56 | + { |
| 57 | + Imports.CFRelease(cfRef); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + private static void ThrowIfError(int status) |
| 62 | + { |
| 63 | + if (status != SecStatusCodeSuccess) |
| 64 | + { |
| 65 | + throw new InvalidOperationException(GetErrorMessageString(status)); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + private static string GetErrorMessageString(int status) |
| 70 | + { |
| 71 | + IntPtr messagePtr = IntPtr.Zero; |
| 72 | + try |
| 73 | + { |
| 74 | + messagePtr = Imports.SecCopyErrorMessageString(status, IntPtr.Zero); |
| 75 | + return GetStringFromCFStringPtr(messagePtr); |
| 76 | + } |
| 77 | + catch |
| 78 | + { |
| 79 | + return status switch |
| 80 | + { |
| 81 | + SecStatusCodeNoSuchKeychain => $"The keychain does not exist. [0x{status:x}]", |
| 82 | + SecStatusCodeInvalidKeychain => $"The keychain is not valid. [0x{status:x}]", |
| 83 | + SecStatusCodeAuthFailed => $"Authorization/Authentication failed. [0x{status:x}]", |
| 84 | + SecStatusCodeDuplicateItem => $"The item already exists. [0x{status:x}]", |
| 85 | + SecStatusCodeItemNotFound => $"The item cannot be found. [0x{status:x}]", |
| 86 | + SecStatusCodeInteractionNotAllowed => $"Interaction with the Security Server is not allowed. [0x{status:x}]", |
| 87 | + SecStatusCodeInteractionRequired => $"User interaction is required. [0x{status:x}]", |
| 88 | + SecStatusCodeNoSuchAttr => $"The attribute does not exist. [0x{status:x}]", |
| 89 | + _ => $"Unknown error. [0x{status:x}]", |
| 90 | + }; |
| 91 | + } |
| 92 | + finally |
| 93 | + { |
| 94 | + CFRelease(messagePtr); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + private static string GetStringFromCFStringPtr(IntPtr handle) |
| 99 | + { |
| 100 | + IntPtr stringPtr = IntPtr.Zero; |
| 101 | + try |
| 102 | + { |
| 103 | + int length = Imports.CFStringGetLength (handle); |
| 104 | + stringPtr = Imports.CFStringGetCharactersPtr(handle); |
| 105 | + |
| 106 | + if (stringPtr == IntPtr.Zero) |
| 107 | + { |
| 108 | + var range = new CFRange(0, length); |
| 109 | + stringPtr = Marshal.AllocCoTaskMem(length * 2); |
| 110 | + Imports.CFStringGetCharacters(handle, range, stringPtr); |
| 111 | + } |
| 112 | + |
| 113 | + return Marshal.PtrToStringAuto(stringPtr, length); |
| 114 | + } |
| 115 | + finally |
| 116 | + { |
| 117 | + if (stringPtr != IntPtr.Zero) |
| 118 | + { |
| 119 | + Marshal.FreeCoTaskMem (stringPtr); |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + public static class Imports |
| 125 | + { |
| 126 | + private const string CoreFoundationLibrary = "/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation"; |
| 127 | + private const string SecurityLibrary = "/System/Library/Frameworks/Security.framework/Security"; |
| 128 | + |
| 129 | + [DllImport (CoreFoundationLibrary, CharSet=CharSet.Unicode)] |
| 130 | + [DefaultDllImportSearchPaths(DllImportSearchPath.AssemblyDirectory | DllImportSearchPath.SafeDirectories)] |
| 131 | + public static extern int CFStringGetLength (IntPtr handle); |
| 132 | + |
| 133 | + [DllImport (CoreFoundationLibrary, CharSet=CharSet.Unicode)] |
| 134 | + [DefaultDllImportSearchPaths(DllImportSearchPath.AssemblyDirectory | DllImportSearchPath.SafeDirectories)] |
| 135 | + public static extern IntPtr CFStringGetCharactersPtr (IntPtr handle); |
| 136 | + |
| 137 | + [DllImport (CoreFoundationLibrary, CharSet=CharSet.Unicode)] |
| 138 | + [DefaultDllImportSearchPaths(DllImportSearchPath.AssemblyDirectory | DllImportSearchPath.SafeDirectories)] |
| 139 | + public static extern IntPtr CFStringGetCharacters (IntPtr handle, CFRange range, IntPtr buffer); |
| 140 | + |
| 141 | + [DllImport (CoreFoundationLibrary, CharSet=CharSet.Unicode)] |
| 142 | + [DefaultDllImportSearchPaths(DllImportSearchPath.AssemblyDirectory | DllImportSearchPath.SafeDirectories)] |
| 143 | + public static extern void CFRelease(IntPtr cfRef); |
| 144 | + |
| 145 | + [DllImport (SecurityLibrary)] |
| 146 | + [DefaultDllImportSearchPaths(DllImportSearchPath.AssemblyDirectory | DllImportSearchPath.SafeDirectories)] |
| 147 | + public static extern int SecKeychainFindGenericPassword ( |
| 148 | + IntPtr keychainOrArray, |
| 149 | + int serviceNameLength, |
| 150 | + byte[] serviceName, |
| 151 | + int accountNameLength, |
| 152 | + byte[] accountName, |
| 153 | + out int passwordLength, |
| 154 | + out IntPtr passwordData, |
| 155 | + out IntPtr itemRef); |
| 156 | + |
| 157 | + [DllImport (SecurityLibrary)] |
| 158 | + [DefaultDllImportSearchPaths(DllImportSearchPath.AssemblyDirectory | DllImportSearchPath.SafeDirectories)] |
| 159 | + public static extern int SecKeychainAddGenericPassword ( |
| 160 | + IntPtr keychain, |
| 161 | + int serviceNameLength, |
| 162 | + byte[] serviceName, |
| 163 | + int accountNameLength, |
| 164 | + byte[] accountName, |
| 165 | + int passwordLength, |
| 166 | + byte[] passwordData, |
| 167 | + out IntPtr itemRef); |
| 168 | + |
| 169 | + [DllImport (SecurityLibrary)] |
| 170 | + [DefaultDllImportSearchPaths(DllImportSearchPath.AssemblyDirectory | DllImportSearchPath.SafeDirectories)] |
| 171 | + public static extern int SecKeychainItemDelete(IntPtr itemRef); |
| 172 | + |
| 173 | + [DllImport (SecurityLibrary)] |
| 174 | + [DefaultDllImportSearchPaths(DllImportSearchPath.AssemblyDirectory | DllImportSearchPath.SafeDirectories)] |
| 175 | + public static extern int SecKeychainItemFreeContent (IntPtr attrList, IntPtr data); |
| 176 | + |
| 177 | + [DllImport (SecurityLibrary)] |
| 178 | + [DefaultDllImportSearchPaths(DllImportSearchPath.AssemblyDirectory | DllImportSearchPath.SafeDirectories)] |
| 179 | + public static extern IntPtr SecCopyErrorMessageString (int status, IntPtr reserved); |
| 180 | + } |
| 181 | + } |
| 182 | +} |
0 commit comments