DH left padding of public key JDK-8145252-TLS13-branch
authorxuelei
Fri, 25 May 2018 12:24:17 -0700
branchJDK-8145252-TLS13-branch
changeset 56610 4933c5e1ed63
parent 56609 62d3e1d0be91
child 56611 f8f7e604e1f8
DH left padding of public key
src/java.base/share/classes/sun/security/ssl/DHKeyExchange.java
--- a/src/java.base/share/classes/sun/security/ssl/DHKeyExchange.java	Fri May 25 11:42:54 2018 -0700
+++ b/src/java.base/share/classes/sun/security/ssl/DHKeyExchange.java	Fri May 25 12:24:17 2018 -0700
@@ -214,8 +214,18 @@
 
         @Override
         public byte[] encode() {
-            // TODO: cannonical the return byte array length.
-            return publicKey.getY().toByteArray();
+            // Note: the DH public value is encoded as a big-endian integer
+            // and padded to the left with zeros to the size of p in bytes.
+            byte[] encoded = publicKey.getY().toByteArray();
+            int pSize = KeyUtil.getKeySize(publicKey);
+            if (pSize > 0 && encoded.length < pSize) {
+                byte[] buffer = new byte[pSize];
+                System.arraycopy(encoded, 0,
+                        buffer, pSize - encoded.length, encoded.length);  
+                encoded = buffer;
+            }
+
+            return encoded;
         }
     }