mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-26 14:24:46 +02:00
8312306: Add more Reference.reachabilityFence() calls to the security classes using Cleaner
Reviewed-by: ascarpino
This commit is contained in:
parent
351c31ea58
commit
2436fb010e
6 changed files with 228 additions and 83 deletions
|
@ -89,12 +89,12 @@ final class DESKey implements SecretKey {
|
|||
public byte[] getEncoded() {
|
||||
// Return a copy of the key, rather than a reference,
|
||||
// so that the key data cannot be modified from outside
|
||||
|
||||
// The key is zeroized by finalize()
|
||||
// The reachability fence ensures finalize() isn't called early
|
||||
byte[] result = key.clone();
|
||||
Reference.reachabilityFence(this);
|
||||
return result;
|
||||
try {
|
||||
return key.clone();
|
||||
} finally {
|
||||
// prevent this from being cleaned for the above block
|
||||
Reference.reachabilityFence(this);
|
||||
}
|
||||
}
|
||||
|
||||
public String getAlgorithm() {
|
||||
|
@ -111,25 +111,35 @@ final class DESKey implements SecretKey {
|
|||
*/
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Arrays.hashCode(this.key) ^ "des".hashCode();
|
||||
try {
|
||||
return Arrays.hashCode(this.key) ^ "des".hashCode();
|
||||
} finally {
|
||||
// prevent this from being cleaned for the above block
|
||||
Reference.reachabilityFence(this);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
try {
|
||||
if (this == obj)
|
||||
return true;
|
||||
|
||||
if (!(obj instanceof SecretKey that))
|
||||
return false;
|
||||
if (!(obj instanceof SecretKey that))
|
||||
return false;
|
||||
|
||||
String thatAlg = that.getAlgorithm();
|
||||
if (!(thatAlg.equalsIgnoreCase("DES")))
|
||||
return false;
|
||||
String thatAlg = that.getAlgorithm();
|
||||
if (!(thatAlg.equalsIgnoreCase("DES")))
|
||||
return false;
|
||||
|
||||
byte[] thatKey = that.getEncoded();
|
||||
boolean ret = MessageDigest.isEqual(this.key, thatKey);
|
||||
java.util.Arrays.fill(thatKey, (byte)0x00);
|
||||
return ret;
|
||||
byte[] thatKey = that.getEncoded();
|
||||
boolean ret = MessageDigest.isEqual(this.key, thatKey);
|
||||
java.util.Arrays.fill(thatKey, (byte)0x00);
|
||||
return ret;
|
||||
} finally {
|
||||
// prevent this from being cleaned for the above block
|
||||
Reference.reachabilityFence(this);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -141,7 +151,13 @@ final class DESKey implements SecretKey {
|
|||
throws java.io.IOException, ClassNotFoundException
|
||||
{
|
||||
s.defaultReadObject();
|
||||
key = key.clone();
|
||||
byte[] temp = key;
|
||||
key = temp.clone();
|
||||
Arrays.fill(temp, (byte)0x00);
|
||||
// Use the cleaner to zero the key when no longer referenced
|
||||
final byte[] k = this.key;
|
||||
CleanerFactory.cleaner().register(this,
|
||||
() -> java.util.Arrays.fill(k, (byte)0x00));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -154,9 +170,14 @@ final class DESKey implements SecretKey {
|
|||
*/
|
||||
@java.io.Serial
|
||||
private Object writeReplace() throws java.io.ObjectStreamException {
|
||||
return new KeyRep(KeyRep.Type.SECRET,
|
||||
try {
|
||||
return new KeyRep(KeyRep.Type.SECRET,
|
||||
getAlgorithm(),
|
||||
getFormat(),
|
||||
key);
|
||||
} finally {
|
||||
// prevent this from being cleaned for the above block
|
||||
Reference.reachabilityFence(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -89,11 +89,12 @@ final class DESedeKey implements SecretKey {
|
|||
}
|
||||
|
||||
public byte[] getEncoded() {
|
||||
// The key is zeroized by finalize()
|
||||
// The reachability fence ensures finalize() isn't called early
|
||||
byte[] result = key.clone();
|
||||
Reference.reachabilityFence(this);
|
||||
return result;
|
||||
try {
|
||||
return key.clone();
|
||||
} finally {
|
||||
// prevent this from being cleaned for the above block
|
||||
Reference.reachabilityFence(this);
|
||||
}
|
||||
}
|
||||
|
||||
public String getAlgorithm() {
|
||||
|
@ -110,26 +111,36 @@ final class DESedeKey implements SecretKey {
|
|||
*/
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Arrays.hashCode(this.key) ^ "desede".hashCode();
|
||||
try {
|
||||
return Arrays.hashCode(this.key) ^ "desede".hashCode();
|
||||
} finally {
|
||||
// prevent this from being cleaned for the above block
|
||||
Reference.reachabilityFence(this);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
try {
|
||||
if (this == obj)
|
||||
return true;
|
||||
|
||||
if (!(obj instanceof SecretKey that))
|
||||
return false;
|
||||
if (!(obj instanceof SecretKey that))
|
||||
return false;
|
||||
|
||||
String thatAlg = that.getAlgorithm();
|
||||
if (!(thatAlg.equalsIgnoreCase("DESede"))
|
||||
&& !(thatAlg.equalsIgnoreCase("TripleDES")))
|
||||
return false;
|
||||
String thatAlg = that.getAlgorithm();
|
||||
if (!(thatAlg.equalsIgnoreCase("DESede"))
|
||||
&& !(thatAlg.equalsIgnoreCase("TripleDES")))
|
||||
return false;
|
||||
|
||||
byte[] thatKey = that.getEncoded();
|
||||
boolean ret = MessageDigest.isEqual(this.key, thatKey);
|
||||
java.util.Arrays.fill(thatKey, (byte)0x00);
|
||||
return ret;
|
||||
byte[] thatKey = that.getEncoded();
|
||||
boolean ret = MessageDigest.isEqual(this.key, thatKey);
|
||||
java.util.Arrays.fill(thatKey, (byte)0x00);
|
||||
return ret;
|
||||
} finally {
|
||||
// prevent this from being cleaned for the above block
|
||||
Reference.reachabilityFence(this);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -141,7 +152,13 @@ final class DESedeKey implements SecretKey {
|
|||
throws java.io.IOException, ClassNotFoundException
|
||||
{
|
||||
s.defaultReadObject();
|
||||
key = key.clone();
|
||||
byte[] temp = key;
|
||||
this.key = temp.clone();
|
||||
java.util.Arrays.fill(temp, (byte)0x00);
|
||||
// Use the cleaner to zero the key when no longer referenced
|
||||
final byte[] k = this.key;
|
||||
CleanerFactory.cleaner().register(this,
|
||||
() -> java.util.Arrays.fill(k, (byte)0x00));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -154,9 +171,14 @@ final class DESedeKey implements SecretKey {
|
|||
*/
|
||||
@java.io.Serial
|
||||
private Object writeReplace() throws java.io.ObjectStreamException {
|
||||
return new KeyRep(KeyRep.Type.SECRET,
|
||||
getAlgorithm(),
|
||||
getFormat(),
|
||||
key);
|
||||
try {
|
||||
return new KeyRep(KeyRep.Type.SECRET,
|
||||
getAlgorithm(),
|
||||
getFormat(),
|
||||
key);
|
||||
} finally {
|
||||
// prevent this from being cleaned for the above block
|
||||
Reference.reachabilityFence(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (c) 1998, 2022, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 1998, 2023, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
|
@ -129,7 +129,7 @@ final class KeyProtector {
|
|||
SecretKey sKey = null;
|
||||
PBEWithMD5AndTripleDESCipher cipher;
|
||||
try {
|
||||
sKey = new PBEKey(pbeKeySpec, "PBEWithMD5AndTripleDES", false);
|
||||
sKey = new PBEKey(pbeKeySpec, "PBEWithMD5AndTripleDES");
|
||||
// encrypt private key
|
||||
cipher = new PBEWithMD5AndTripleDESCipher();
|
||||
cipher.engineInit(Cipher.ENCRYPT_MODE, sKey, pbeSpec, null);
|
||||
|
@ -193,7 +193,7 @@ final class KeyProtector {
|
|||
|
||||
// create PBE key from password
|
||||
PBEKeySpec pbeKeySpec = new PBEKeySpec(this.password);
|
||||
sKey = new PBEKey(pbeKeySpec, "PBEWithMD5AndTripleDES", false);
|
||||
sKey = new PBEKey(pbeKeySpec, "PBEWithMD5AndTripleDES");
|
||||
pbeKeySpec.clearPassword();
|
||||
|
||||
// decrypt private key
|
||||
|
@ -339,7 +339,7 @@ final class KeyProtector {
|
|||
SecretKey sKey = null;
|
||||
Cipher cipher;
|
||||
try {
|
||||
sKey = new PBEKey(pbeKeySpec, "PBEWithMD5AndTripleDES", false);
|
||||
sKey = new PBEKey(pbeKeySpec, "PBEWithMD5AndTripleDES");
|
||||
pbeKeySpec.clearPassword();
|
||||
|
||||
// seal key
|
||||
|
@ -366,8 +366,7 @@ final class KeyProtector {
|
|||
try {
|
||||
// create PBE key from password
|
||||
PBEKeySpec pbeKeySpec = new PBEKeySpec(this.password);
|
||||
sKey = new PBEKey(pbeKeySpec,
|
||||
"PBEWithMD5AndTripleDES", false);
|
||||
sKey = new PBEKey(pbeKeySpec, "PBEWithMD5AndTripleDES");
|
||||
pbeKeySpec.clearPassword();
|
||||
|
||||
SealedObjectForKeyProtector soForKeyProtector = null;
|
||||
|
|
|
@ -26,6 +26,7 @@
|
|||
package com.sun.crypto.provider;
|
||||
|
||||
import java.lang.ref.Reference;
|
||||
import java.lang.ref.Cleaner.Cleanable;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.KeyRep;
|
||||
import java.security.spec.InvalidKeySpecException;
|
||||
|
@ -51,13 +52,14 @@ final class PBEKey implements SecretKey {
|
|||
|
||||
private String type;
|
||||
|
||||
private transient Cleanable cleanable;
|
||||
|
||||
/**
|
||||
* Creates a PBE key from a given PBE key specification.
|
||||
*
|
||||
* @param keytype the given PBE key specification
|
||||
*/
|
||||
PBEKey(PBEKeySpec keySpec, String keytype, boolean useCleaner)
|
||||
throws InvalidKeySpecException {
|
||||
PBEKey(PBEKeySpec keySpec, String keytype) throws InvalidKeySpecException {
|
||||
char[] passwd = keySpec.getPassword();
|
||||
if (passwd == null) {
|
||||
// Should allow an empty password.
|
||||
|
@ -78,19 +80,18 @@ final class PBEKey implements SecretKey {
|
|||
type = keytype;
|
||||
|
||||
// Use the cleaner to zero the key when no longer referenced
|
||||
if (useCleaner) {
|
||||
final byte[] k = this.key;
|
||||
CleanerFactory.cleaner().register(this,
|
||||
() -> Arrays.fill(k, (byte) 0x00));
|
||||
}
|
||||
final byte[] k = this.key;
|
||||
cleanable = CleanerFactory.cleaner().register(this,
|
||||
() -> java.util.Arrays.fill(k, (byte)0x00));
|
||||
}
|
||||
|
||||
public byte[] getEncoded() {
|
||||
// The key is zeroized by finalize()
|
||||
// The reachability fence ensures finalize() isn't called early
|
||||
byte[] result = key.clone();
|
||||
Reference.reachabilityFence(this);
|
||||
return result;
|
||||
try {
|
||||
return key.clone();
|
||||
} finally {
|
||||
// prevent this from being cleaned for the above block
|
||||
Reference.reachabilityFence(this);
|
||||
}
|
||||
}
|
||||
|
||||
public String getAlgorithm() {
|
||||
|
@ -107,25 +108,40 @@ final class PBEKey implements SecretKey {
|
|||
*/
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Arrays.hashCode(this.key)
|
||||
^ getAlgorithm().toLowerCase(Locale.ENGLISH).hashCode();
|
||||
try {
|
||||
return Arrays.hashCode(this.key)
|
||||
^ getAlgorithm().toLowerCase(Locale.ENGLISH).hashCode();
|
||||
} finally {
|
||||
// prevent this from being cleaned for the above block
|
||||
Reference.reachabilityFence(this);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (obj == this)
|
||||
return true;
|
||||
try {
|
||||
if (obj == this)
|
||||
return true;
|
||||
|
||||
if (!(obj instanceof SecretKey that))
|
||||
return false;
|
||||
if (!(obj instanceof SecretKey that))
|
||||
return false;
|
||||
|
||||
if (!(that.getAlgorithm().equalsIgnoreCase(type)))
|
||||
return false;
|
||||
// destroyed keys are considered different
|
||||
if (isDestroyed() || that.isDestroyed()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
byte[] thatEncoded = that.getEncoded();
|
||||
boolean ret = MessageDigest.isEqual(this.key, thatEncoded);
|
||||
Arrays.fill(thatEncoded, (byte)0x00);
|
||||
return ret;
|
||||
if (!(that.getAlgorithm().equalsIgnoreCase(type)))
|
||||
return false;
|
||||
|
||||
byte[] thatEncoded = that.getEncoded();
|
||||
boolean ret = MessageDigest.isEqual(this.key, thatEncoded);
|
||||
Arrays.fill(thatEncoded, (byte)0x00);
|
||||
return ret;
|
||||
} finally {
|
||||
// prevent this from being cleaned for the above block
|
||||
Reference.reachabilityFence(this);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -134,12 +150,17 @@ final class PBEKey implements SecretKey {
|
|||
*/
|
||||
@Override
|
||||
public void destroy() {
|
||||
if (key != null) {
|
||||
Arrays.fill(key, (byte) 0x00);
|
||||
key = null;
|
||||
if (cleanable != null) {
|
||||
cleanable.clean();
|
||||
cleanable = null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isDestroyed() {
|
||||
return (cleanable == null);
|
||||
}
|
||||
|
||||
/**
|
||||
* readObject is called to restore the state of this key from
|
||||
* a stream.
|
||||
|
@ -149,7 +170,13 @@ final class PBEKey implements SecretKey {
|
|||
throws java.io.IOException, ClassNotFoundException
|
||||
{
|
||||
s.defaultReadObject();
|
||||
key = key.clone();
|
||||
byte[] temp = key;
|
||||
key = temp.clone();
|
||||
Arrays.fill(temp, (byte)0x00);
|
||||
// Use cleaner to zero the key when no longer referenced
|
||||
final byte[] k = this.key;
|
||||
cleanable = CleanerFactory.cleaner().register(this,
|
||||
() -> java.util.Arrays.fill(k, (byte)0x00));
|
||||
}
|
||||
|
||||
|
||||
|
@ -163,9 +190,14 @@ final class PBEKey implements SecretKey {
|
|||
*/
|
||||
@java.io.Serial
|
||||
private Object writeReplace() throws java.io.ObjectStreamException {
|
||||
return new KeyRep(KeyRep.Type.SECRET,
|
||||
getAlgorithm(),
|
||||
getFormat(),
|
||||
key);
|
||||
try {
|
||||
return new KeyRep(KeyRep.Type.SECRET,
|
||||
getAlgorithm(),
|
||||
getFormat(),
|
||||
key);
|
||||
} finally {
|
||||
// prevent this from being cleaned for the above block
|
||||
Reference.reachabilityFence(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (c) 1997, 2022, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 1997, 2023, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
|
@ -239,7 +239,7 @@ abstract class PBEKeyFactory extends SecretKeyFactorySpi {
|
|||
if (!(keySpec instanceof PBEKeySpec)) {
|
||||
throw new InvalidKeySpecException("Invalid key spec");
|
||||
}
|
||||
return new PBEKey((PBEKeySpec)keySpec, type, true);
|
||||
return new PBEKey((PBEKeySpec)keySpec, type);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue