Skip to content

Commit

Permalink
Proper fix for compilation issue caused by deprecated API in macOS Mo…
Browse files Browse the repository at this point in the history
…jave by

using dlsym to call available API rather than suppressing deprecation warnings.

Fixes: #30599
  • Loading branch information
maryamariyan committed Jul 3, 2018
1 parent 331f135 commit f12652e
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ set(NATIVECRYPTO_SOURCES
pal_x509chain.c
)

# Temporary workaround for dotnet/corefx issue #30599
add_compile_options(-Wno-deprecated-declarations)

add_library(System.Security.Cryptography.Native.Apple
SHARED
${NATIVECRYPTO_SOURCES}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information.

#include "pal_x509.h"
#include <dlfcn.h>

static const int32_t kErrOutItemsNull = -3;
static const int32_t kErrOutItemsEmpty = -2;
Expand Down Expand Up @@ -52,7 +53,27 @@ AppleCryptoNative_X509GetPublicKey(SecCertificateRef cert, SecKeyRef* pPublicKey
if (cert == NULL || pPublicKeyOut == NULL || pOSStatusOut == NULL)
return kErrorBadInput;

*pOSStatusOut = SecCertificateCopyPublicKey(cert, pPublicKeyOut);
// SecCertificateCopyPublicKey was deprecated in 10.14, so use SecCertificateCopyKey on the systems that have it (10.14+),
// and SecCertificateCopyPublicKey on the systems that don’t.
static SecKeyRef (*secCertificateCopyKey)(SecCertificateRef);
static OSStatus (*secCertificateCopyPublicKey)(SecCertificateRef, SecKeyRef*);
static int checked;

if (!checked)
{
secCertificateCopyKey = (SecKeyRef (*)(SecCertificateRef))dlsym(RTLD_DEFAULT, "SecCertificateCopyKey");
secCertificateCopyPublicKey = (OSStatus (*)(SecCertificateRef, SecKeyRef*))dlsym(RTLD_DEFAULT, "SecCertificateCopyPublicKey");
checked = 1;
}
if (secCertificateCopyKey != NULL)
{
*pPublicKeyOut = (*secCertificateCopyKey)(cert);
}
else
{
assert(secCertificateCopyPublicKey != NULL);
*pOSStatusOut = (*secCertificateCopyPublicKey)(cert, pPublicKeyOut);
}
return (*pOSStatusOut == noErr);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Returns 1 on success, 0 on failure, any other value on invalid state.
Output:
pPublicKeyOut: Receives a CFRetain()ed SecKeyRef for the public key
pOSStatusOut: Receives the result of SecCertificateCopyPublicKey
pOSStatusOut: Receives the result of SecCertificateCopyKey
*/
DLLEXPORT int32_t
AppleCryptoNative_X509GetPublicKey(SecCertificateRef cert, SecKeyRef* pPublicKeyOut, int32_t* pOSStatusOut);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ public AsymmetricAlgorithm DecodePublicKey(Oid oid, byte[] encodedKeyValue, byte
case Oids.RsaRsa:
return new RSAImplementation.RSASecurityTransforms(key);
case Oids.DsaDsa:
if (key.IsInvalid)
{
// SecCertificateCopyKey returns null for DSA, so fall back to manually building it.
return DecodeDsaPublicKey(encodedKeyValue, encodedParameters);
}
return new DSAImplementation.DSASecurityTransforms(key);
case Oids.Ecc:
return new ECDsaImplementation.ECDsaSecurityTransforms(key);
Expand Down

0 comments on commit f12652e

Please sign in to comment.