1 // Licensed to the .NET Foundation under one or more agreements. 2 // The .NET Foundation licenses this file to you under the MIT license. 3 // See the LICENSE file in the project root for more information. 4 5 using System; 6 using System.Diagnostics; 7 using System.Runtime.InteropServices; 8 using System.Security.Cryptography; 9 using System.Security.Cryptography.Apple; 10 using System.Security.Cryptography.X509Certificates; 11 12 internal static partial class Interop 13 { 14 internal static partial class AppleCrypto 15 { 16 [DllImport(Libraries.AppleCryptoNative)] AppleCryptoNative_X509StoreAddCertificate( SafeKeychainItemHandle cert, SafeKeychainHandle keychain, out int pOSStatus)17 private static extern int AppleCryptoNative_X509StoreAddCertificate( 18 SafeKeychainItemHandle cert, 19 SafeKeychainHandle keychain, 20 out int pOSStatus); 21 22 [DllImport(Libraries.AppleCryptoNative)] AppleCryptoNative_X509StoreRemoveCertificate( SafeSecCertificateHandle cert, SafeKeychainHandle keychain, out int pOSStatus)23 private static extern int AppleCryptoNative_X509StoreRemoveCertificate( 24 SafeSecCertificateHandle cert, 25 SafeKeychainHandle keychain, 26 out int pOSStatus); 27 X509StoreAddCertificate(SafeKeychainItemHandle certOrIdentity, SafeKeychainHandle keychain)28 internal static void X509StoreAddCertificate(SafeKeychainItemHandle certOrIdentity, SafeKeychainHandle keychain) 29 { 30 int osStatus; 31 int ret = AppleCryptoNative_X509StoreAddCertificate(certOrIdentity, keychain, out osStatus); 32 33 if (ret == 0) 34 { 35 throw CreateExceptionForOSStatus(osStatus); 36 } 37 38 if (ret != 1) 39 { 40 Debug.Fail($"Unexpected result from AppleCryptoNative_X509StoreAddCertificate: {ret}"); 41 throw new CryptographicException(); 42 } 43 } 44 X509StoreRemoveCertificate(SafeSecCertificateHandle certHandle, SafeKeychainHandle keychain)45 internal static void X509StoreRemoveCertificate(SafeSecCertificateHandle certHandle, SafeKeychainHandle keychain) 46 { 47 int osStatus; 48 int ret = AppleCryptoNative_X509StoreRemoveCertificate(certHandle, keychain, out osStatus); 49 50 if (ret == 0) 51 { 52 throw CreateExceptionForOSStatus(osStatus); 53 } 54 55 const int SuccessOrNoMatch = 1; 56 const int UserTrustExists = 2; 57 const int AdminTrustExists = 3; 58 59 switch (ret) 60 { 61 case SuccessOrNoMatch: 62 break; 63 case UserTrustExists: 64 throw new CryptographicException(SR.Cryptography_X509Store_WouldModifyUserTrust); 65 case AdminTrustExists: 66 throw new CryptographicException(SR.Cryptography_X509Store_WouldModifyAdminTrust); 67 default: 68 Debug.Fail($"Unexpected result from AppleCryptoNative_X509StoreRemoveCertificate: {ret}"); 69 throw new CryptographicException(); 70 } 71 } 72 } 73 }