forked from openjdk/jdk8u
-
Notifications
You must be signed in to change notification settings - Fork 1
RH2117972 - Extend the support for NSS DBs (PKCS11) in FIPS mode #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
martinuy
wants to merge
7
commits into
rh-openjdk:fips
Choose a base branch
from
martinuy:RH2117972
base: fips
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6ad6252
JDK-8195607 dependency (jdk8u-dev PR 106 - Webrev 00)
martinuy bd910b8
JDK-8292688 dependency (jdk8u-dev PR 107 - Webrev 00)
martinuy b14a469
RH2117972 - Extend the support for NSS DBs (PKCS11) in FIPS mode
martinuy 39755a2
The '@modules' jtreg tag is not needed in 8u.
martinuy e783882
Various improvements as discussed after @franferrax's review.
martinuy af084e9
Moving the initialization of the fips.nssdb.path System property from…
martinuy 1d9ee95
Fix to the order in which the fips.nssdb.path System property is set:…
martinuy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
150 changes: 150 additions & 0 deletions
150
jdk/src/share/classes/sun/security/pkcs11/FIPSTokenLoginHandler.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
/* | ||
* Copyright (c) 2022, Red Hat, Inc. | ||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | ||
* | ||
* This code is free software; you can redistribute it and/or modify it | ||
* under the terms of the GNU General Public License version 2 only, as | ||
* published by the Free Software Foundation. Oracle designates this | ||
* particular file as subject to the "Classpath" exception as provided | ||
* by Oracle in the LICENSE file that accompanied this code. | ||
* | ||
* This code is distributed in the hope that it will be useful, but WITHOUT | ||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
* version 2 for more details (a copy is included in the LICENSE file that | ||
* accompanied this code). | ||
* | ||
* You should have received a copy of the GNU General Public License version | ||
* 2 along with this work; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | ||
* | ||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | ||
* or visit www.oracle.com if you need additional information or have any | ||
* questions. | ||
*/ | ||
|
||
package sun.security.pkcs11; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.ByteArrayInputStream; | ||
import java.io.InputStream; | ||
import java.io.InputStreamReader; | ||
import java.io.IOException; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.nio.file.StandardOpenOption; | ||
import java.security.ProviderException; | ||
|
||
import javax.security.auth.callback.Callback; | ||
import javax.security.auth.callback.CallbackHandler; | ||
import javax.security.auth.callback.PasswordCallback; | ||
import javax.security.auth.callback.UnsupportedCallbackException; | ||
|
||
import sun.misc.IOUtils; | ||
import sun.security.util.Debug; | ||
import sun.security.util.SecurityProperties; | ||
|
||
final class FIPSTokenLoginHandler implements CallbackHandler { | ||
|
||
private static final String FIPS_NSSDB_PIN_PROP = "fips.nssdb.pin"; | ||
|
||
private static final Debug debug = Debug.getInstance("sunpkcs11"); | ||
|
||
public void handle(Callback[] callbacks) | ||
throws IOException, UnsupportedCallbackException { | ||
if (!(callbacks[0] instanceof PasswordCallback)) { | ||
throw new UnsupportedCallbackException(callbacks[0]); | ||
} | ||
PasswordCallback pc = (PasswordCallback)callbacks[0]; | ||
pc.setPassword(getFipsNssdbPin()); | ||
} | ||
|
||
private static char[] getFipsNssdbPin() throws ProviderException { | ||
if (debug != null) { | ||
debug.println("FIPS: Reading NSS DB PIN for token..."); | ||
} | ||
String pinProp = SecurityProperties | ||
.privilegedGetOverridable(FIPS_NSSDB_PIN_PROP); | ||
if (pinProp != null && !pinProp.isEmpty()) { | ||
String[] pinPropParts = pinProp.split(":", 2); | ||
if (pinPropParts.length < 2) { | ||
throw new ProviderException("Invalid " + FIPS_NSSDB_PIN_PROP + | ||
" property value."); | ||
} | ||
String prefix = pinPropParts[0].toUpperCase(); | ||
String value = pinPropParts[1]; | ||
String pin = null; | ||
if (prefix.equals("ENV")) { | ||
if (debug != null) { | ||
debug.println("FIPS: PIN value from the '" + value + | ||
"' environment variable."); | ||
} | ||
pin = System.getenv(value); | ||
} else if (prefix.equals("FILE")) { | ||
if (debug != null) { | ||
debug.println("FIPS: PIN value from the '" + value + | ||
"' file."); | ||
} | ||
pin = getPinFromFile(Paths.get(value)); | ||
} else if (prefix.equals("PIN")) { | ||
if (debug != null) { | ||
debug.println("FIPS: PIN value from the " + | ||
FIPS_NSSDB_PIN_PROP + " property."); | ||
} | ||
pin = value; | ||
} else { | ||
throw new ProviderException("Unsupported prefix for " + | ||
FIPS_NSSDB_PIN_PROP + "."); | ||
} | ||
if (pin != null && !pin.isEmpty()) { | ||
if (debug != null) { | ||
debug.println("FIPS: non-empty PIN."); | ||
} | ||
/* | ||
* C_Login in libj2pkcs11 receives the PIN in a char[] and | ||
* discards the upper byte of each char, before passing | ||
* the value to the NSS Software Token. However, the | ||
* NSS Software Token accepts any UTF-8 PIN value. Thus, | ||
* expand the PIN here to account for later truncation. | ||
*/ | ||
byte[] pinUtf8 = pin.getBytes(StandardCharsets.UTF_8); | ||
char[] pinChar = new char[pinUtf8.length]; | ||
for (int i = 0; i < pinChar.length; i++) { | ||
pinChar[i] = (char)(pinUtf8[i] & 0xFF); | ||
} | ||
return pinChar; | ||
} | ||
} | ||
if (debug != null) { | ||
debug.println("FIPS: empty PIN."); | ||
} | ||
return new char[] {}; | ||
} | ||
|
||
/* | ||
* This method extracts the token PIN from the first line of a password | ||
* file in the same way as NSS modutil. See for example the -newpwfile | ||
* argument used to change the password for an NSS DB. | ||
*/ | ||
private static String getPinFromFile(Path f) throws ProviderException { | ||
try (InputStream is = | ||
Files.newInputStream(f, StandardOpenOption.READ)) { | ||
/* | ||
* SECU_FilePasswd in NSS (nss/cmd/lib/secutil.c), used by modutil, | ||
* reads up to 4096 bytes. In addition, the NSS Software Token | ||
* does not accept PINs longer than 500 bytes (see SFTK_MAX_PIN | ||
* in nss/lib/softoken/pkcs11i.h). | ||
*/ | ||
BufferedReader in = | ||
new BufferedReader(new InputStreamReader( | ||
new ByteArrayInputStream(IOUtils.readNBytes(is, 4096)), | ||
StandardCharsets.UTF_8)); | ||
return in.readLine(); | ||
} catch (IOException ioe) { | ||
throw new ProviderException("Error reading " + FIPS_NSSDB_PIN_PROP + | ||
" from the '" + f + "' file.", ioe); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.