Skip to content

Commit b721606

Browse files
committed
more wrap, less copy
1 parent 7129df6 commit b721606

File tree

7 files changed

+27
-38
lines changed

7 files changed

+27
-38
lines changed

src/java.base/share/classes/com/sun/crypto/provider/PBKDF2Core.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,9 @@ protected SecretKey engineTranslateKey(SecretKey key)
158158
} finally {
159159
if (password != null) {
160160
Arrays.fill(password, (char) 0);
161+
spec.clearPassword();
161162
}
162163
Arrays.fill(encoding, (byte)0);
163-
spec.clearPassword();
164164
}
165165
}
166166
}

src/java.base/share/classes/com/sun/crypto/provider/PBKDF2HmacSHA1Factory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,9 @@ protected SecretKey engineTranslateKey(SecretKey key)
158158
} finally {
159159
if (password != null) {
160160
Arrays.fill(password, (char) 0);
161+
spec.clearPassword();
161162
}
162163
Arrays.fill(encoding, (byte)0);
163-
spec.clearPassword();
164164
}
165165
}
166166
}

src/java.base/share/classes/sun/security/pkcs/PKCS8Key.java

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -218,20 +218,13 @@ public String getFormat() {
218218
private synchronized byte[] getEncodedInternal() {
219219
if (encodedKey == null) {
220220
try {
221-
DerOutputStream out = new DerOutputStream ();
222221
DerOutputStream tmp = new DerOutputStream();
223222
tmp.putInteger(V1);
224223
algid.encode(tmp);
225-
// Write zeroes of correct length into intermediate
226-
// DerOutputStream so that the real content will not
227-
// appear everywhere. The actual bytes will be copied
228-
// into the final encoding so they can be cleared
229-
// effectively once required.
230-
tmp.putOctetString(new byte[key.length]);
231-
out.write(DerValue.tag_Sequence, tmp);
224+
tmp.putOctetString(key);
225+
DerValue out = DerValue.wrap(DerValue.tag_Sequence, tmp);
232226
encodedKey = out.toByteArray();
233-
// Copy the actual bytes
234-
System.arraycopy(key, 0, encodedKey, encodedKey.length - key.length, key.length);
227+
out.clear();
235228
} catch (IOException e) {
236229
// encodedKey is still null
237230
}

src/java.base/share/classes/sun/security/pkcs12/PKCS12KeyStore.java

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -661,30 +661,23 @@ private void setKeyEntry(String alias, Key key,
661661
keyEntry.date = new Date();
662662

663663
// Encode secret key in a PKCS#8
664-
DerOutputStream pkcs8 = new DerOutputStream();
665664
DerOutputStream secretKeyInfo = new DerOutputStream();
666665
secretKeyInfo.putInteger(0);
667666
AlgorithmId algId = AlgorithmId.get(key.getAlgorithm());
668667
algId.encode(secretKeyInfo);
668+
669669
byte[] encoded = key.getEncoded();
670-
// Write zeroes of correct length into intermediate
671-
// DerOutputStream so that the real content will not
672-
// appear everywhere. The actual bytes will be copied
673-
// into the final encoding so they can be cleared
674-
// effectively once required.
675-
secretKeyInfo.putOctetString(new byte[encoded.length]);
676-
pkcs8.write(DerValue.tag_Sequence, secretKeyInfo);
670+
secretKeyInfo.putOctetString(encoded);
671+
Arrays.fill(encoded, (byte)0);
677672

673+
DerValue pkcs8 = DerValue.wrap(DerValue.tag_Sequence, secretKeyInfo);
678674
byte[] p8Array = pkcs8.toByteArray();
679-
// Copy the actual bytes
680-
System.arraycopy(encoded, 0, p8Array,
681-
p8Array.length - encoded.length, encoded.length);
675+
pkcs8.clear();
682676
try {
683677
// Encrypt the secret key (using same PBE as for private keys)
684678
keyEntry.protectedSecretKey =
685679
encryptPrivateKey(p8Array, passwordProtection);
686680
} finally {
687-
Arrays.fill(encoded, (byte)0);
688681
Arrays.fill(p8Array, (byte)0);
689682
}
690683

src/java.base/share/classes/sun/security/rsa/RSAPrivateKeyImpl.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2003, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2003, 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
@@ -31,6 +31,7 @@
3131
import java.security.*;
3232
import java.security.spec.AlgorithmParameterSpec;
3333
import java.security.interfaces.*;
34+
import java.util.Arrays;
3435

3536
import sun.security.util.*;
3637
import sun.security.pkcs.PKCS8Key;
@@ -90,19 +91,25 @@ public final class RSAPrivateKeyImpl extends PKCS8Key implements RSAPrivateKey {
9091

9192
try {
9293
// generate the key encoding
93-
DerOutputStream out = new DerOutputStream();
94+
byte[] nbytes = n.toByteArray();
95+
byte[] dbytes = d.toByteArray();
96+
DerOutputStream out = new DerOutputStream(
97+
nbytes.length + dbytes.length + 50);
98+
// Enough for 7 zeroes (21) and 2 tag+length(4)
9499
out.putInteger(0); // version must be 0
95-
out.putInteger(n);
100+
out.putInteger(nbytes);
101+
Arrays.fill(nbytes, (byte)0);
96102
out.putInteger(0);
97-
out.putInteger(d);
103+
out.putInteger(dbytes);
104+
Arrays.fill(dbytes, (byte)0);
98105
out.putInteger(0);
99106
out.putInteger(0);
100107
out.putInteger(0);
101108
out.putInteger(0);
102109
out.putInteger(0);
103-
DerValue val =
104-
new DerValue(DerValue.tag_Sequence, out.toByteArray());
110+
DerValue val = DerValue.wrap(DerValue.tag_Sequence, out);
105111
key = val.toByteArray();
112+
val.clear();
106113
} catch (IOException exc) {
107114
// should never occur
108115
throw new InvalidKeyException(exc);

test/jdk/sun/security/pkcs11/rsa/TestKeyFactory.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,7 @@ public String getFormat() {
9898
}
9999
@Override
100100
public byte[] getEncoded() {
101-
// skip cloning for testing key.
102-
return encodedPriv;
101+
return encodedPriv.clone();
103102
}
104103
};
105104
byte[] encodedPub = Base64.getDecoder().decode(PKCS1_PUB_STR);
@@ -114,8 +113,7 @@ public String getFormat() {
114113
}
115114
@Override
116115
public byte[] getEncoded() {
117-
// skip cloning for testing key.
118-
return encodedPub;
116+
return encodedPub.clone();
119117
}
120118
};
121119
}

test/jdk/sun/security/rsa/TestKeyFactory.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,7 @@ public String getFormat() {
9797
}
9898
@Override
9999
public byte[] getEncoded() {
100-
// skip cloning for testing key.
101-
return encodedPriv;
100+
return encodedPriv.clone();
102101
}
103102
};
104103
byte[] encodedPub = Base64.getDecoder().decode(PKCS1_PUB_STR);
@@ -113,8 +112,7 @@ public String getFormat() {
113112
}
114113
@Override
115114
public byte[] getEncoded() {
116-
// skip cloning for testing key.
117-
return encodedPub;
115+
return encodedPub.clone();
118116
}
119117
};
120118
}

0 commit comments

Comments
 (0)