Skip to content

Commit 4a8b5c1

Browse files
committedFeb 3, 2021
8257858: [macOS]: Remove JNF dependency from libosxsecurity/KeystoreImpl.m
8257860: [macOS]: Remove JNF dependency from libosxkrb5/SCDynamicStoreConfig.m Reviewed-by: erikj, prr, ihse, valeriep
1 parent 472bf62 commit 4a8b5c1

File tree

12 files changed

+488
-436
lines changed

12 files changed

+488
-436
lines changed
 

‎make/common/TestFilesCompilation.gmk

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ define SetupTestFilesCompilationBody
8181

8282
# Locate all files with the matching prefix
8383
$1_FILE_LIST := \
84-
$$(call FindFiles, $$($1_SOURCE_DIRS), $$($1_PREFIX)*.c $$($1_PREFIX)*.cpp)
84+
$$(call FindFiles, $$($1_SOURCE_DIRS), $$($1_PREFIX)*.c $$($1_PREFIX)*.cpp $$($1_PREFIX)*.m)
8585

8686
$1_EXCLUDE_PATTERN := $$(addprefix %/, $$($1_EXCLUDE))
8787
$1_FILTERED_FILE_LIST := $$(filter-out $$($1_EXCLUDE_PATTERN), $$($1_FILE_LIST))

‎make/modules/java.base/Lib.gmk

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#
2-
# Copyright (c) 2011, 2020, Oracle and/or its affiliates. All rights reserved.
2+
# Copyright (c) 2011, 2021, Oracle and/or its affiliates. All rights reserved.
33
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
#
55
# This code is free software; you can redistribute it and/or modify it
@@ -110,9 +110,9 @@ ifeq ($(call isTargetOs, macosx), true)
110110
$(call SET_SHARED_LIBRARY_ORIGIN), \
111111
LIBS := \
112112
-lobjc \
113-
-framework JavaNativeFoundation \
114113
-framework CoreServices \
115114
-framework Security \
115+
-framework Foundation \
116116
$(JDKLIB_LIBS), \
117117
))
118118

‎make/modules/java.security.jgss/Lib.gmk

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#
2-
# Copyright (c) 2011, 2019, Oracle and/or its affiliates. All rights reserved.
2+
# Copyright (c) 2011, 2021, Oracle and/or its affiliates. All rights reserved.
33
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
#
55
# This code is free software; you can redistribute it and/or modify it
@@ -80,8 +80,8 @@ ifneq ($(BUILD_CRYPTO), false)
8080
DISABLED_WARNINGS_clang := deprecated-declarations, \
8181
LDFLAGS := $(LDFLAGS_JDKLIB) \
8282
$(call SET_SHARED_LIBRARY_ORIGIN), \
83-
LIBS := -framework JavaNativeFoundation -framework Cocoa \
84-
-framework SystemConfiguration -framework Kerberos, \
83+
LIBS := -framework Cocoa -framework SystemConfiguration \
84+
-framework Kerberos, \
8585
))
8686

8787
TARGETS += $(BUILD_LIBOSXKRB5)

‎make/test/JtregNativeJdk.gmk

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#
2-
# Copyright (c) 2015, 2020, Oracle and/or its affiliates. All rights reserved.
2+
# Copyright (c) 2015, 2021, Oracle and/or its affiliates. All rights reserved.
33
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
#
55
# This code is free software; you can redistribute it and/or modify it
@@ -77,13 +77,15 @@ endif
7777

7878
ifeq ($(call isTargetOs, macosx), true)
7979
BUILD_JDK_JTREG_EXCLUDE += exelauncher.c
80-
BUILD_JDK_JTREG_LIBRARIES_CFLAGS_libTestMainKeyWindow := -ObjC
8180
BUILD_JDK_JTREG_LIBRARIES_LIBS_libTestMainKeyWindow := \
8281
-framework Cocoa -framework JavaNativeFoundation
8382
BUILD_JDK_JTREG_EXECUTABLES_LIBS_exeJniInvocationTest := -ljli
83+
BUILD_JDK_JTREG_LIBRARIES_LIBS_libTestDynamicStore := \
84+
-framework Cocoa -framework SystemConfiguration
8485
else
85-
BUILD_JDK_JTREG_EXCLUDE += libTestMainKeyWindow.c
86+
BUILD_JDK_JTREG_EXCLUDE += libTestMainKeyWindow.m
8687
BUILD_JDK_JTREG_EXCLUDE += exeJniInvocationTest.c
88+
BUILD_JDK_JTREG_EXCLUDE += libTestDynamicStore.m
8789
endif
8890

8991
ifeq ($(call isTargetOs, linux), true)

‎src/java.base/macosx/classes/apple/security/KeychainStore.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2011, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2011, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -815,8 +815,8 @@ private void createTrustedCertEntry(String alias, long keychainItemRef, long cre
815815
* Callback method from _scanKeychain. If an identity is found, this method will be called to create Java certificate
816816
* and private key objects from the keychain data.
817817
*/
818-
private void createKeyEntry(String alias, long creationDate, long secKeyRef, long[] secCertificateRefs, byte[][] rawCertData)
819-
throws IOException, NoSuchAlgorithmException, UnrecoverableKeyException {
818+
private void createKeyEntry(String alias, long creationDate, long secKeyRef,
819+
long[] secCertificateRefs, byte[][] rawCertData) {
820820
KeyEntry ke = new KeyEntry();
821821

822822
// First, store off the private key information. This is the easy part.

‎src/java.base/macosx/native/libosxsecurity/KeystoreImpl.m

+101-80
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2011, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2011, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -25,16 +25,10 @@
2525

2626
#import "apple_security_KeychainStore.h"
2727
#import "jni_util.h"
28-
2928
#import <Security/Security.h>
3029
#import <Security/SecImportExport.h>
3130
#import <CoreServices/CoreServices.h> // (for require() macros)
32-
#import <JavaNativeFoundation/JavaNativeFoundation.h>
33-
34-
35-
static JNF_CLASS_CACHE(jc_KeychainStore, "apple/security/KeychainStore");
36-
static JNF_MEMBER_CACHE(jm_createTrustedCertEntry, jc_KeychainStore, "createTrustedCertEntry", "(Ljava/lang/String;JJ[B)V");
37-
static JNF_MEMBER_CACHE(jm_createKeyEntry, jc_KeychainStore, "createKeyEntry", "(Ljava/lang/String;JJ[J[[B)V");
31+
#import <Cocoa/Cocoa.h>
3832

3933
static jstring getLabelFromItem(JNIEnv *env, SecKeychainItemRef inItem)
4034
{
@@ -290,6 +284,10 @@ static void addIdentitiesToKeystore(JNIEnv *env, jobject keyStore)
290284
SecIdentityRef theIdentity = NULL;
291285
OSErr searchResult = noErr;
292286

287+
jclass jc_KeychainStore = (*env)->FindClass(env, "apple/security/KeychainStore");
288+
CHECK_NULL(jc_KeychainStore);
289+
jmethodID jm_createKeyEntry = (*env)->GetMethodID(env, jc_KeychainStore, "createKeyEntry", "(Ljava/lang/String;JJ[J[[B)V");
290+
CHECK_NULL(jm_createKeyEntry);
293291
do {
294292
searchResult = SecIdentitySearchCopyNext(identitySearch, &theIdentity);
295293

@@ -359,7 +357,8 @@ static void addIdentitiesToKeystore(JNIEnv *env, jobject keyStore)
359357

360358
// Call back to the Java object to create Java objects corresponding to this security object.
361359
jlong nativeKeyRef = ptr_to_jlong(privateKeyRef);
362-
JNFCallVoidMethod(env, keyStore, jm_createKeyEntry, alias, creationDate, nativeKeyRef, certRefArray, javaCertArray);
360+
(*env)->CallVoidMethod(env, keyStore, jm_createKeyEntry, alias, creationDate, nativeKeyRef, certRefArray, javaCertArray);
361+
JNU_CHECK_EXCEPTION(env);
363362
}
364363
} while (searchResult == noErr);
365364

@@ -377,6 +376,11 @@ static void addCertificatesToKeystore(JNIEnv *env, jobject keyStore)
377376
SecKeychainItemRef theItem = NULL;
378377
OSErr searchResult = noErr;
379378

379+
jclass jc_KeychainStore = (*env)->FindClass(env, "apple/security/KeychainStore");
380+
CHECK_NULL(jc_KeychainStore);
381+
jmethodID jm_createTrustedCertEntry = (*env)->GetMethodID(
382+
env, jc_KeychainStore, "createTrustedCertEntry", "(Ljava/lang/String;JJ[B)V");
383+
CHECK_NULL(jm_createTrustedCertEntry);
380384
do {
381385
searchResult = SecKeychainSearchCopyNext(keychainItemSearch, &theItem);
382386

@@ -402,7 +406,8 @@ static void addCertificatesToKeystore(JNIEnv *env, jobject keyStore)
402406

403407
// Call back to the Java object to create Java objects corresponding to this security object.
404408
jlong nativeRef = ptr_to_jlong(certRef);
405-
JNFCallVoidMethod(env, keyStore, jm_createTrustedCertEntry, alias, nativeRef, creationDate, certData);
409+
(*env)->CallVoidMethod(env, keyStore, jm_createTrustedCertEntry, alias, nativeRef, creationDate, certData);
410+
JNU_CHECK_EXCEPTION(env);
406411
}
407412
} while (searchResult == noErr);
408413

@@ -500,6 +505,20 @@ static void addCertificatesToKeystore(JNIEnv *env, jobject keyStore)
500505

501506
}
502507

508+
NSString* JavaStringToNSString(JNIEnv *env, jstring jstr) {
509+
if (jstr == NULL) {
510+
return NULL;
511+
}
512+
jsize len = (*env)->GetStringLength(env, jstr);
513+
const jchar *chars = (*env)->GetStringChars(env, jstr, NULL);
514+
if (chars == NULL) {
515+
return NULL;
516+
}
517+
NSString *result = [NSString stringWithCharacters:(UniChar *)chars length:len];
518+
(*env)->ReleaseStringChars(env, jstr, chars);
519+
return result;
520+
}
521+
503522
/*
504523
* Class: apple_security_KeychainStore
505524
* Method: _addItemToKeychain
@@ -511,95 +530,97 @@ static void addCertificatesToKeystore(JNIEnv *env, jobject keyStore)
511530
OSStatus err;
512531
jlong returnValue = 0;
513532

514-
JNF_COCOA_ENTER(env);
533+
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; \
534+
@try {
535+
jsize dataSize = (*env)->GetArrayLength(env, rawDataObj);
536+
jbyte *rawData = (*env)->GetByteArrayElements(env, rawDataObj, NULL);
537+
if (rawData == NULL) {
538+
goto errOut;
539+
}
515540

516-
jsize dataSize = (*env)->GetArrayLength(env, rawDataObj);
517-
jbyte *rawData = (*env)->GetByteArrayElements(env, rawDataObj, NULL);
518-
if (rawData == NULL) {
519-
goto errOut;
520-
}
541+
CFDataRef cfDataToImport = CFDataCreate(kCFAllocatorDefault, (UInt8 *)rawData, dataSize);
542+
CFArrayRef createdItems = NULL;
521543

522-
CFDataRef cfDataToImport = CFDataCreate(kCFAllocatorDefault, (UInt8 *)rawData, dataSize);
523-
CFArrayRef createdItems = NULL;
544+
SecKeychainRef defaultKeychain = NULL;
545+
SecKeychainCopyDefault(&defaultKeychain);
524546

525-
SecKeychainRef defaultKeychain = NULL;
526-
SecKeychainCopyDefault(&defaultKeychain);
547+
SecExternalFormat dataFormat = (isCertificate == JNI_TRUE ? kSecFormatX509Cert : kSecFormatWrappedPKCS8);
527548

528-
SecExternalFormat dataFormat = (isCertificate == JNI_TRUE ? kSecFormatX509Cert : kSecFormatWrappedPKCS8);
549+
// Convert the password obj into a CFStringRef that the keychain importer can use for encryption.
550+
SecKeyImportExportParameters paramBlock;
551+
CFStringRef passwordStrRef = NULL;
529552

530-
// Convert the password obj into a CFStringRef that the keychain importer can use for encryption.
531-
SecKeyImportExportParameters paramBlock;
532-
CFStringRef passwordStrRef = NULL;
553+
jsize passwordLen = 0;
554+
jchar *passwordChars = NULL;
533555

534-
jsize passwordLen = 0;
535-
jchar *passwordChars = NULL;
556+
if (passwordObj) {
557+
passwordLen = (*env)->GetArrayLength(env, passwordObj);
536558

537-
if (passwordObj) {
538-
passwordLen = (*env)->GetArrayLength(env, passwordObj);
539-
540-
if (passwordLen > 0) {
541-
passwordChars = (*env)->GetCharArrayElements(env, passwordObj, NULL);
542-
if (passwordChars == NULL) {
543-
goto errOut;
544-
}
559+
if (passwordLen > 0) {
560+
passwordChars = (*env)->GetCharArrayElements(env, passwordObj, NULL);
561+
if (passwordChars == NULL) {
562+
goto errOut;
563+
}
545564

546-
passwordStrRef = CFStringCreateWithCharactersNoCopy(NULL, passwordChars, passwordLen, kCFAllocatorNull);
547-
if (passwordStrRef == NULL) {
548-
goto errOut;
565+
passwordStrRef = CFStringCreateWithCharactersNoCopy(NULL, passwordChars, passwordLen, kCFAllocatorNull);
566+
if (passwordStrRef == NULL) {
567+
goto errOut;
568+
}
549569
}
550570
}
551-
}
552571

553-
paramBlock.version = SEC_KEY_IMPORT_EXPORT_PARAMS_VERSION;
554-
// Note that setting the flags field **requires** you to pass in a password of some kind. The keychain will not prompt you.
555-
paramBlock.flags = 0;
556-
paramBlock.passphrase = passwordStrRef;
557-
paramBlock.alertTitle = NULL;
558-
paramBlock.alertPrompt = NULL;
559-
paramBlock.accessRef = NULL;
560-
paramBlock.keyUsage = CSSM_KEYUSE_ANY;
561-
paramBlock.keyAttributes = CSSM_KEYATTR_RETURN_DEFAULT;
572+
paramBlock.version = SEC_KEY_IMPORT_EXPORT_PARAMS_VERSION;
573+
// Note that setting the flags field **requires** you to pass in a password of some kind. The keychain will not prompt you.
574+
paramBlock.flags = 0;
575+
paramBlock.passphrase = passwordStrRef;
576+
paramBlock.alertTitle = NULL;
577+
paramBlock.alertPrompt = NULL;
578+
paramBlock.accessRef = NULL;
579+
paramBlock.keyUsage = CSSM_KEYUSE_ANY;
580+
paramBlock.keyAttributes = CSSM_KEYATTR_RETURN_DEFAULT;
581+
582+
err = SecKeychainItemImport(cfDataToImport, NULL, &dataFormat, NULL,
583+
0, &paramBlock, defaultKeychain, &createdItems);
584+
if (cfDataToImport != NULL) {
585+
CFRelease(cfDataToImport);
586+
}
562587

563-
err = SecKeychainItemImport(cfDataToImport, NULL, &dataFormat, NULL,
564-
0, &paramBlock, defaultKeychain, &createdItems);
565-
if (cfDataToImport != NULL) {
566-
CFRelease(cfDataToImport);
567-
}
588+
if (err == noErr) {
589+
SecKeychainItemRef anItem = (SecKeychainItemRef)CFArrayGetValueAtIndex(createdItems, 0);
568590

569-
if (err == noErr) {
570-
SecKeychainItemRef anItem = (SecKeychainItemRef)CFArrayGetValueAtIndex(createdItems, 0);
591+
// Don't bother labeling keys. They become part of an identity, and are not an accessible part of the keychain.
592+
if (CFGetTypeID(anItem) == SecCertificateGetTypeID()) {
593+
setLabelForItem(JavaStringToNSString(env, alias), anItem);
594+
}
571595

572-
// Don't bother labeling keys. They become part of an identity, and are not an accessible part of the keychain.
573-
if (CFGetTypeID(anItem) == SecCertificateGetTypeID()) {
574-
setLabelForItem(JNFJavaToNSString(env, alias), anItem);
596+
// Retain the item, since it will be released once when the array holding it gets released.
597+
CFRetain(anItem);
598+
returnValue = ptr_to_jlong(anItem);
599+
} else {
600+
cssmPerror("_addItemToKeychain: SecKeychainItemImport", err);
575601
}
576602

577-
// Retain the item, since it will be released once when the array holding it gets released.
578-
CFRetain(anItem);
579-
returnValue = ptr_to_jlong(anItem);
580-
} else {
581-
cssmPerror("_addItemToKeychain: SecKeychainItemImport", err);
582-
}
583-
584-
if (createdItems != NULL) {
585-
CFRelease(createdItems);
586-
}
603+
if (createdItems != NULL) {
604+
CFRelease(createdItems);
605+
}
587606

588-
errOut:
589-
if (rawData) {
590-
(*env)->ReleaseByteArrayElements(env, rawDataObj, rawData, JNI_ABORT);
591-
}
607+
errOut:
608+
if (rawData) {
609+
(*env)->ReleaseByteArrayElements(env, rawDataObj, rawData, JNI_ABORT);
610+
}
592611

593-
if (passwordStrRef) CFRelease(passwordStrRef);
594-
if (passwordChars) {
595-
// clear the password and release
596-
memset(passwordChars, 0, passwordLen);
597-
(*env)->ReleaseCharArrayElements(env, passwordObj, passwordChars,
598-
JNI_ABORT);
612+
if (passwordStrRef) CFRelease(passwordStrRef);
613+
if (passwordChars) {
614+
// clear the password and release
615+
memset(passwordChars, 0, passwordLen);
616+
(*env)->ReleaseCharArrayElements(env, passwordObj, passwordChars,
617+
JNI_ABORT);
618+
}
619+
} @catch (NSException *e) {
620+
NSLog(@"%@", [e callStackSymbols]);
621+
} @finally {
622+
[pool drain];
599623
}
600-
601-
JNF_COCOA_EXIT(env);
602-
603624
return returnValue;
604625
}
605626

0 commit comments

Comments
 (0)
Failed to load comments.