Skip to content

Commit

Permalink
[CoreWLan] Add XCode13 beta 1 support.
Browse files Browse the repository at this point in the history
Added support for MacCatalyst and cleaned the backlog.
  • Loading branch information
mandel-macaque committed Jul 12, 2021
1 parent 6ce5f80 commit a4b36ec
Show file tree
Hide file tree
Showing 12 changed files with 524 additions and 234 deletions.
231 changes: 231 additions & 0 deletions src/CoreWlan/CWKeychain.cs
@@ -0,0 +1,231 @@
using System;
using System.Runtime.InteropServices;

using CoreFoundation;
using Foundation;
using ObjCRuntime;
using Security;

#if NET
using System.Runtime.Versioning;
#endif


#nullable enable

using OSStatus = System.Int32;
using SecIdentityRef = System.IntPtr;
using CFArrayRef = System.IntPtr;
using NSDataRef = System.IntPtr;
using NSStringRef = System.IntPtr;

namespace CoreWlan {
#if NET
[SupportedOSPlatform ("maccatalyst15.0")]
#else
[MacCatalyst (15,0)]
#endif
public static partial class CWKeychain {

#if NET
[SupportedOSPlatform ("macos10.9")]
#else
[Mac (10,9)]
#endif
[DllImport (Constants.CoreWlanLibrary)]
static extern OSStatus CWKeychainCopyWiFiEAPIdentity (CWKeychainDomain domain, NSDataRef ssid, out SecIdentityRef identity);

#if NET
[SupportedOSPlatform ("macos10.9")]
#else
[Mac (10,9)]
#endif
public static bool FindWiFiEAPIdentity (CWKeychainDomain domain, NSData ssid, out SecIdentity? identity)
{
identity = null;
IntPtr outPtr = IntPtr.Zero;
var result = CWKeychainCopyWiFiEAPIdentity (domain, ssid.GetHandle (), out outPtr);
if (result == 0)
{
identity = new SecIdentity (outPtr, true);
}

return result == 0;
}

#if NET
[SupportedOSPlatform ("macos10.9")]
#else
[Mac (10,9)]
#endif
[DllImport (Constants.CoreWlanLibrary)]
static extern OSStatus CWKeychainDeleteWiFiEAPUsernameAndPassword (CWKeychainDomain domain, NSDataRef ssid);

#if NET
[SupportedOSPlatform ("macos10.9")]
#else
[Mac (10,9)]
#endif
public static bool TryDeleteWiFiEAPUsernameAndPassword (CWKeychainDomain domain, NSData ssid)
=> CWKeychainDeleteWiFiEAPUsernameAndPassword (domain, ssid.GetHandle ()) == 0;

#if NET
[SupportedOSPlatform ("macos10.9")]
#else
[Mac (10,9)]
#endif
[DllImport (Constants.CoreWlanLibrary)]
static extern OSStatus CWKeychainDeleteWiFiPassword (CWKeychainDomain domain, NSDataRef ssid);

#if NET
[SupportedOSPlatform ("macos10.9")]
#else
[Mac (10,9)]
#endif
public static bool TryDeleteWiFiPassword (CWKeychainDomain domain, NSData ssid)
=> CWKeychainDeleteWiFiPassword (domain, ssid.GetHandle ()) == 0;

#if NET
[SupportedOSPlatform ("macos10.9")]
#else
[Mac (10,9)]
#endif
[DllImport (Constants.CoreWlanLibrary)]
static extern OSStatus CWKeychainFindWiFiEAPUsernameAndPassword (CWKeychainDomain domain, NSDataRef ssid, out NSStringRef username, out NSStringRef password);

#if NET
[SupportedOSPlatform ("macos10.9")]
#else
[Mac (10,9)]
#endif
public static bool TryFindWiFiEAPUsernameAndPassword (CWKeychainDomain domain, NSData ssid, out NSString? username, out NSString? password)
{
username = null;
password = null;
NSStringRef usernamePtr = IntPtr.Zero;
NSStringRef passwordPtr = IntPtr.Zero;
var result = CWKeychainFindWiFiEAPUsernameAndPassword (domain, ssid.GetHandle (), out usernamePtr, out passwordPtr);
if (usernamePtr != IntPtr.Zero) {
username = Runtime.GetNSObject<NSString> (usernamePtr, true);
}
if (passwordPtr != IntPtr.Zero) {
password= Runtime.GetNSObject<NSString> (passwordPtr, true);
}
return result == 0;
}

#if NET
[SupportedOSPlatform ("macos10.9")]
#else
[Mac (10,9)]
#endif
[DllImport (Constants.CoreWlanLibrary)]
static extern OSStatus CWKeychainFindWiFiPassword (CWKeychainDomain domain, NSDataRef ssid, out NSStringRef password);

#if NET
[SupportedOSPlatform ("macos10.9")]
#else
[Mac (10,9)]
#endif
public static bool TryFindWiFiPassword (CWKeychainDomain domain, NSData ssid, out NSString? password)
{
password = null;
NSStringRef passwordPtr = IntPtr.Zero;
var result = CWKeychainFindWiFiPassword (domain, ssid.GetHandle (), out passwordPtr);
if (passwordPtr != IntPtr.Zero) {
password= Runtime.GetNSObject<NSString> (passwordPtr, true);
}
return result == 0;
}

#if NET
[SupportedOSPlatform ("macos10.9")]
#else
[Mac (10,9)]
#endif
[DllImport (Constants.CoreWlanLibrary)]
static extern OSStatus CWKeychainSetWiFiEAPIdentity (CWKeychainDomain domain, NSDataRef ssid, SecIdentityRef identity);

#if NET
[SupportedOSPlatform ("macos10.9")]
#else
[Mac (10,9)]
#endif
public static bool TrySetWiFiEAPIdentity (CWKeychainDomain domain, NSData ssid, SecIdentity? identity)
=> CWKeychainSetWiFiEAPIdentity (domain, ssid.GetHandle (), identity.GetHandle ()!) == 0;

#if NET
[SupportedOSPlatform ("macos10.9")]
#else
[Mac (10,9)]
#endif
[DllImport (Constants.CoreWlanLibrary)]
static extern OSStatus CWKeychainSetWiFiEAPUsernameAndPassword (CWKeychainDomain domain, NSDataRef ssid, NSStringRef username, NSStringRef password);

#if NET
[SupportedOSPlatform ("macos10.9")]
#else
[Mac (10,9)]
#endif
public static bool TrySetWiFiEAPUsernameAndPassword (CWKeychainDomain domain, NSData ssid, NSString? username, NSString? password)
=> CWKeychainSetWiFiEAPUsernameAndPassword (domain, ssid.GetHandle (), username.GetHandle ()!, password.GetHandle ()!) == 0;

#if NET
[SupportedOSPlatform ("macos10.9")]
#else
[Mac (10, 9)]
#endif
public static bool TrySetWiFiEAPUsernameAndPassword (CWKeychainDomain domain, NSData ssid, string? username, string? password)
{
using (NSString? nsUsername = (username == null)? null : new NSString (username))
using (NSString? nsPassword = (password == null)? null : new NSString (password)) {
return TrySetWiFiEAPUsernameAndPassword (domain, ssid, nsUsername, nsPassword);
}
}

#if NET
[SupportedOSPlatform ("macos10.9")]
#else
[Mac (10,9)]
#endif
[DllImport (Constants.CoreWlanLibrary)]
static extern OSStatus CWKeychainSetWiFiPassword (CWKeychainDomain domain, NSDataRef ssid, NSStringRef password);

#if NET
[SupportedOSPlatform ("macos10.9")]
#else
[Mac (10,9)]
#endif
public static bool TrySetWiFiPassword (CWKeychainDomain domain, NSData ssid, NSString password)
=> CWKeychainSetWiFiPassword (domain, ssid.GetHandle (), password.GetHandle ()) == 0;

#if NET
[SupportedOSPlatform ("macos10.9")]
#else
[Mac (10,9)]
#endif
public static bool TrySetWiFiPassword (CWKeychainDomain domain, NSData ssid, string password)
{
using (NSString nsPassword = new NSString (password)) {
return TrySetWiFiPassword (domain, ssid, nsPassword);
}
}


#if NET
[SupportedOSPlatform ("macos10.7")]
#else
[Mac (10,7)]
#endif
[DllImport (Constants.CoreWlanLibrary)]
static extern OSStatus CWKeychainCopyEAPIdentityList (CFArrayRef list);

#if NET
[SupportedOSPlatform ("macos10.7")]
#else
[Mac (10,7)]
#endif
public static bool TryGetEAPIdentityList (NSArray? list)
=> CWKeychainCopyEAPIdentityList (list.GetHandle ()!) == 0;
}
}
50 changes: 50 additions & 0 deletions src/CoreWlan/Enums.cs
Expand Up @@ -5,8 +5,17 @@
using CoreFoundation;
using ObjCRuntime;
using System;
#if NET
using System.Runtime.Versioning;
#endif

namespace CoreWlan {

#if NET
[SupportedOSPlatform ("maccatalyst15.0")]
#else
[MacCatalyst (15,0)]
#endif
[Native]
[ErrorDomain ("CWErrorDomain")] // enum named `CWErr` in headers
public enum CWStatus : long {
Expand Down Expand Up @@ -46,6 +55,11 @@ public enum CWStatus : long {
Status = -3931,
}

#if NET
[SupportedOSPlatform ("maccatalyst15.0")]
#else
[MacCatalyst (15,0)]
#endif
[Native]
public enum CWPhyMode : ulong {
None = 0,
Expand All @@ -54,8 +68,14 @@ public enum CWPhyMode : ulong {
G = 3,
N = 4,
AC = 5,
AX = 6,
}

#if NET
[SupportedOSPlatform ("maccatalyst15.0")]
#else
[MacCatalyst (15,0)]
#endif
[Native]
public enum CWInterfaceMode : ulong {
None = 0,
Expand All @@ -64,6 +84,11 @@ public enum CWInterfaceMode : ulong {
HostAP = 3,
}

#if NET
[SupportedOSPlatform ("maccatalyst15.0")]
#else
[MacCatalyst (15,0)]
#endif
[Native]
public enum CWSecurity : ulong {
None = 0,
Expand Down Expand Up @@ -94,6 +119,11 @@ public enum CWIbssModeSecurity : ulong {
WEP104 = 2,
}

#if NET
[SupportedOSPlatform ("maccatalyst15.0")]
#else
[MacCatalyst (15,0)]
#endif
[Native]
public enum CWChannelWidth : ulong {
Unknown = 0,
Expand All @@ -103,13 +133,23 @@ public enum CWChannelWidth : ulong {
OneHundredSixtyMHz = 4,
}

#if NET
[SupportedOSPlatform ("maccatalyst15.0")]
#else
[MacCatalyst (15,0)]
#endif
[Native]
public enum CWChannelBand : ulong {
Unknown = 0,
TwoGHz = 1,
FiveGHz = 2,
}

#if NET
[SupportedOSPlatform ("maccatalyst15.0")]
#else
[MacCatalyst (15,0)]
#endif
[Native]
public enum CWCipherKeyFlags : ulong {
None = 0,
Expand All @@ -119,13 +159,23 @@ public enum CWCipherKeyFlags : ulong {
Rx = 1 << 4,
}

#if NET
[SupportedOSPlatform ("maccatalyst15.0")]
#else
[MacCatalyst (15,0)]
#endif
[Native]
public enum CWKeychainDomain : ulong {
None = 0,
User = 1,
System = 2,
}

#if NET
[SupportedOSPlatform ("maccatalyst15.0")]
#else
[MacCatalyst (15,0)]
#endif
[Native]
public enum CWEventType : long {
None = 0,
Expand Down

0 comments on commit a4b36ec

Please sign in to comment.