Skip to content

Commit 02492b7

Browse files
authored
[iOS/macOS] Split S.S.C.Native.Apple into platform specific files where needed
Instead of using #if to remove functions that don't work/make sense on iOS, move them to a file that is conditionally included in the library compile.
1 parent a24f323 commit 02492b7

19 files changed

+1017
-999
lines changed

src/libraries/Native/Unix/System.Security.Cryptography.Native.Apple/CMakeLists.txt

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,28 @@ set(NATIVECRYPTO_SOURCES
1010
pal_ecc.c
1111
pal_hmac.c
1212
pal_keyagree.c
13-
pal_keyderivation.c
14-
pal_keychain.c
1513
pal_random.c
1614
pal_rsa.c
1715
pal_sec.c
1816
pal_seckey.c
1917
pal_signverify.c
2018
pal_ssl.c
2119
pal_symmetric.c
22-
pal_trust.c
2320
pal_x509.c
2421
pal_x509chain.c
2522
)
2623

24+
if (NOT CLR_CMAKE_TARGET_MACCATALYST AND NOT CLR_CMAKE_TARGET_IOS AND NOT CLR_CMAKE_TARGET_TVOS)
25+
set(NATIVECRYPTO_SOURCES
26+
${NATIVECRYPTO_SOURCES}
27+
pal_keychain_macos.c
28+
pal_keyderivation_macos.c
29+
pal_seckey_macos.c
30+
pal_trust_macos.c
31+
pal_x509_macos.c
32+
)
33+
endif()
34+
2735
if (CLR_CMAKE_TARGET_MACCATALYST)
2836
add_definitions(-DTARGET_MACCATALYST)
2937
endif()

src/libraries/Native/Unix/System.Security.Cryptography.Native.Apple/entrypoints.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
#if !defined(TARGET_MACCATALYST) && !defined(TARGET_IOS) && !defined(TARGET_TVOS)
5-
64
#include "../../AnyOS/entrypoints.h"
75

86
// Include System.Security.Cryptography.Native.Apple headers
97
#include "pal_digest.h"
108
#include "pal_ecc.h"
119
#include "pal_hmac.h"
12-
#include "pal_keychain.h"
10+
#include "pal_keychain_macos.h"
1311
#include "pal_random.h"
1412
#include "pal_rsa.h"
1513
#include "pal_sec.h"
1614
#include "pal_seckey.h"
15+
#include "pal_seckey_macos.h"
1716
#include "pal_signverify.h"
1817
#include "pal_ssl.h"
1918
#include "pal_symmetric.h"
20-
#include "pal_trust.h"
19+
#include "pal_trust_macos.h"
2120
#include "pal_x509.h"
21+
#include "pal_x509_macos.h"
2222
#include "pal_x509chain.h"
23-
#include "pal_keyderivation.h"
23+
#include "pal_keyderivation_macos.h"
2424

2525
static const Entry s_cryptoAppleNative[] =
2626
{
@@ -113,5 +113,3 @@ EXTERN_C const void* CryptoAppleResolveDllImport(const char* name)
113113
{
114114
return ResolveDllImport(s_cryptoAppleNative, lengthof(s_cryptoAppleNative), name);
115115
}
116-
117-
#endif // !defined(TARGET_MACCATALYST) && !defined(TARGET_IOS) && !defined(TARGET_TVOS)

src/libraries/Native/Unix/System.Security.Cryptography.Native.Apple/pal_keychain.c renamed to src/libraries/Native/Unix/System.Security.Cryptography.Native.Apple/pal_keychain_macos.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
#include "pal_keychain.h"
4+
#include "pal_keychain_macos.h"
55
#include "pal_utilities.h"
66

7-
#if !defined(TARGET_MACCATALYST) && !defined(TARGET_IOS) && !defined(TARGET_TVOS)
87
int32_t AppleCryptoNative_SecKeychainItemCopyKeychain(SecKeychainItemRef item, SecKeychainRef* pKeychainOut)
98
{
109
if (pKeychainOut != NULL)
@@ -465,4 +464,3 @@ AppleCryptoNative_X509StoreRemoveCertificate(CFTypeRef certOrIdentity, SecKeycha
465464
CFRelease(cert);
466465
return *pOSStatus == noErr;
467466
}
468-
#endif

src/libraries/Native/Unix/System.Security.Cryptography.Native.Apple/pal_keychain.h renamed to src/libraries/Native/Unix/System.Security.Cryptography.Native.Apple/pal_keychain_macos.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
#include <Security/Security.h>
1010

11-
#if !defined(TARGET_MACCATALYST) && !defined(TARGET_IOS) && !defined(TARGET_TVOS)
1211
/*
1312
Get a CFRetain()ed SecKeychainRef value for the keychain to which the keychain item belongs.
1413
@@ -137,4 +136,3 @@ pOSStatus: Receives the last OSStatus value..
137136
*/
138137
PALEXPORT int32_t
139138
AppleCryptoNative_X509StoreRemoveCertificate(CFTypeRef certOrIdentity, SecKeychainRef keychain, uint8_t isReadOnlyMode, int32_t* pOSStatus);
140-
#endif

src/libraries/Native/Unix/System.Security.Cryptography.Native.Apple/pal_keyderivation.c renamed to src/libraries/Native/Unix/System.Security.Cryptography.Native.Apple/pal_keyderivation_macos.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
#include "pal_keyderivation.h"
5-
6-
#if !defined(TARGET_IOS) && !defined(TARGET_TVOS)
4+
#include "pal_keyderivation_macos.h"
75

86
static int32_t PrfAlgorithmFromHashAlgorithm(PAL_HashAlgorithm hashAlgorithm, CCPseudoRandomAlgorithm* algorithm)
97
{
@@ -80,4 +78,3 @@ int32_t AppleCryptoNative_Pbkdf2(PAL_HashAlgorithm prfAlgorithm,
8078
*errorCode = result;
8179
return result == kCCSuccess ? 1 : 0;
8280
}
83-
#endif

src/libraries/Native/Unix/System.Security.Cryptography.Native.Apple/pal_keyderivation.h renamed to src/libraries/Native/Unix/System.Security.Cryptography.Native.Apple/pal_keyderivation_macos.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
#include "pal_digest.h"
77
#include <Security/Security.h>
88

9-
#if !defined(TARGET_IOS) && !defined(TARGET_TVOS)
109
/*
1110
Filled the derivedKey buffer with PBKDF2 derived data.
1211
@@ -37,4 +36,3 @@ PALEXPORT int32_t AppleCryptoNative_Pbkdf2(PAL_HashAlgorithm prfAlgorithm,
3736
uint8_t* derivedKey,
3837
uint32_t derivedKeyLen,
3938
int32_t* errorCode);
40-
#endif

src/libraries/Native/Unix/System.Security.Cryptography.Native.Apple/pal_sec.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33

44
#include "pal_sec.h"
55

6-
CFStringRef AppleCryptoNative_SecCopyErrorMessageString(int32_t osStatus)
6+
CFStringRef AppleCryptoNative_SecCopyErrorMessageString(OSStatus osStatus)
77
{
88
#if (defined(TARGET_IOS) && __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_11_3) || (defined(TARGET_TVOS) && __IPHONE_OS_VERSION_MIN_REQUIRED < __TVOS_11_3)
9-
return CFStringCreateWithCString(NULL, "", kCFStringEncodingUTF8);
9+
return CFStringCreateWithFormat(NULL, NULL, CFSTR("OSStatus %d"), (int)osStatus);
1010
#else
1111
return SecCopyErrorMessageString(osStatus, NULL);
1212
#endif

src/libraries/Native/Unix/System.Security.Cryptography.Native.Apple/pal_sec.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,9 @@
88

99
#include <Security/Security.h>
1010

11-
#if !defined(TARGET_MACCATALYST) && !defined(TARGET_IOS) && !defined(TARGET_TVOS)
1211
/*
1312
Get an error message for an OSStatus error from the security library.
1413
1514
Returns NULL if no message is available for the code.
1615
*/
1716
PALEXPORT CFStringRef AppleCryptoNative_SecCopyErrorMessageString(OSStatus osStatus);
18-
#endif

src/libraries/Native/Unix/System.Security.Cryptography.Native.Apple/pal_seckey.c

Lines changed: 0 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -4,132 +4,6 @@
44
#include "pal_seckey.h"
55
#include "pal_utilities.h"
66

7-
#if !defined(TARGET_MACCATALYST) && !defined(TARGET_IOS) && !defined(TARGET_TVOS)
8-
int32_t AppleCryptoNative_SecKeyExport(
9-
SecKeyRef pKey, int32_t exportPrivate, CFStringRef cfExportPassphrase, CFDataRef* ppDataOut, int32_t* pOSStatus)
10-
{
11-
if (ppDataOut != NULL)
12-
*ppDataOut = NULL;
13-
if (pOSStatus != NULL)
14-
*pOSStatus = noErr;
15-
16-
if (pKey == NULL || ppDataOut == NULL || pOSStatus == NULL)
17-
{
18-
return kErrorBadInput;
19-
}
20-
21-
SecExternalFormat dataFormat = kSecFormatOpenSSL;
22-
SecItemImportExportKeyParameters keyParams;
23-
memset(&keyParams, 0, sizeof(SecItemImportExportKeyParameters));
24-
25-
keyParams.version = SEC_KEY_IMPORT_EXPORT_PARAMS_VERSION;
26-
27-
if (exportPrivate)
28-
{
29-
if (cfExportPassphrase == NULL)
30-
{
31-
return kErrorBadInput;
32-
}
33-
34-
keyParams.passphrase = cfExportPassphrase;
35-
dataFormat = kSecFormatWrappedPKCS8;
36-
}
37-
38-
*pOSStatus = SecItemExport(pKey, dataFormat, 0, &keyParams, ppDataOut);
39-
40-
return (*pOSStatus == noErr);
41-
}
42-
43-
int32_t AppleCryptoNative_SecKeyImportEphemeral(
44-
uint8_t* pbKeyBlob, int32_t cbKeyBlob, int32_t isPrivateKey, SecKeyRef* ppKeyOut, int32_t* pOSStatus)
45-
{
46-
if (ppKeyOut != NULL)
47-
*ppKeyOut = NULL;
48-
if (pOSStatus != NULL)
49-
*pOSStatus = noErr;
50-
51-
if (pbKeyBlob == NULL || cbKeyBlob < 0 || isPrivateKey < 0 || isPrivateKey > 1 || ppKeyOut == NULL ||
52-
pOSStatus == NULL)
53-
{
54-
return kErrorBadInput;
55-
}
56-
57-
int32_t ret = 0;
58-
CFDataRef cfData = CFDataCreateWithBytesNoCopy(NULL, pbKeyBlob, cbKeyBlob, kCFAllocatorNull);
59-
60-
SecExternalFormat dataFormat = kSecFormatOpenSSL;
61-
SecExternalFormat actualFormat = dataFormat;
62-
63-
SecExternalItemType itemType = isPrivateKey ? kSecItemTypePrivateKey : kSecItemTypePublicKey;
64-
SecExternalItemType actualType = itemType;
65-
66-
CFIndex itemCount;
67-
CFArrayRef outItems = NULL;
68-
CFTypeRef outItem = NULL;
69-
70-
*pOSStatus = SecItemImport(cfData, NULL, &actualFormat, &actualType, 0, NULL, NULL, &outItems);
71-
72-
if (*pOSStatus != noErr)
73-
{
74-
ret = 0;
75-
goto cleanup;
76-
}
77-
78-
if (actualFormat != dataFormat || actualType != itemType)
79-
{
80-
ret = -2;
81-
goto cleanup;
82-
}
83-
84-
if (outItems == NULL)
85-
{
86-
ret = -3;
87-
goto cleanup;
88-
}
89-
90-
itemCount = CFArrayGetCount(outItems);
91-
92-
if (itemCount == 0)
93-
{
94-
ret = -4;
95-
goto cleanup;
96-
}
97-
98-
if (itemCount > 1)
99-
{
100-
ret = -5;
101-
goto cleanup;
102-
}
103-
104-
outItem = CFArrayGetValueAtIndex(outItems, 0);
105-
106-
if (outItem == NULL)
107-
{
108-
ret = -6;
109-
goto cleanup;
110-
}
111-
112-
if (CFGetTypeID(outItem) != SecKeyGetTypeID())
113-
{
114-
ret = -7;
115-
goto cleanup;
116-
}
117-
118-
CFRetain(outItem);
119-
*ppKeyOut = (SecKeyRef)CONST_CAST(void *, outItem);
120-
ret = 1;
121-
122-
cleanup:
123-
if (outItems != NULL)
124-
{
125-
CFRelease(outItems);
126-
}
127-
128-
CFRelease(cfData);
129-
return ret;
130-
}
131-
#endif
132-
1337
uint64_t AppleCryptoNative_SecKeyGetSimpleKeySizeInBytes(SecKeyRef publicKey)
1348
{
1359
if (publicKey == NULL)

src/libraries/Native/Unix/System.Security.Cryptography.Native.Apple/pal_seckey.h

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -25,40 +25,6 @@ enum
2525
};
2626
typedef uint32_t PAL_KeyAlgorithm;
2727

28-
#if !defined(TARGET_MACCATALYST) && !defined(TARGET_IOS) && !defined(TARGET_TVOS)
29-
/*
30-
Export a key object.
31-
32-
Public keys are exported using the "OpenSSL" format option, which means, essentially,
33-
"whatever format the openssl CLI would use for this algorithm by default".
34-
35-
Private keys are exported using the "Wrapped PKCS#8" format. These formats are available via
36-
`openssl pkcs8 -topk8 ...`. While the PKCS#8 container is the same for all key types, the
37-
payload is algorithm-dependent (though identified by the PKCS#8 wrapper).
38-
39-
An export passphrase is required for private keys, and ignored for public keys.
40-
41-
Follows pal_seckey return conventions.
42-
*/
43-
PALEXPORT int32_t AppleCryptoNative_SecKeyExport(
44-
SecKeyRef pKey, int32_t exportPrivate, CFStringRef cfExportPassphrase, CFDataRef* ppDataOut, int32_t* pOSStatus);
45-
46-
/*
47-
Import a key from a key blob.
48-
49-
Imports are always done using the "OpenSSL" format option, which means the format used for an
50-
unencrypted private key via the openssl CLI verb of the algorithm being imported.
51-
52-
For public keys the "OpenSSL" format is NOT the format used by the openssl CLI for that algorithm,
53-
but is in fact the X.509 SubjectPublicKeyInfo structure.
54-
55-
Returns 1 on success, 0 on failure (*pOSStatus should be set) and negative numbers for various
56-
state machine errors.
57-
*/
58-
PALEXPORT int32_t AppleCryptoNative_SecKeyImportEphemeral(
59-
uint8_t* pbKeyBlob, int32_t cbKeyBlob, int32_t isPrivateKey, SecKeyRef* ppKeyOut, int32_t* pOSStatus);
60-
#endif
61-
6228
/*
6329
For RSA and DSA this function returns the number of bytes in "the key", which corresponds to
6430
the length of n/Modulus for RSA and for P in DSA.

0 commit comments

Comments
 (0)