jdk/src/share/classes/sun/security/ssl/DHClientKeyExchange.java

Print this page
rev 5679 : 7192392: Better validation of client keys
Summary: Also reviewed by Andrew Gross<Andrew.Gross@Oracle.COM>
Reviewed-by: vinnie
   1 /*
   2  * Copyright (c) 1997, 2007, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 
  27 package sun.security.ssl;
  28 
  29 import java.io.IOException;
  30 import java.io.PrintStream;
  31 import java.math.BigInteger;
  32 
  33 
  34 /*
  35  * Message used by clients to send their Diffie-Hellman public
  36  * keys to servers.
  37  *
  38  * @author David Brownell
  39  */
  40 final class DHClientKeyExchange extends HandshakeMessage {
  41 
  42     int messageType() {
  43         return ht_client_key_exchange;
  44     }
  45 
  46     /*
  47      * This value may be empty if it was included in the
  48      * client's certificate ...
  49      */
  50     private byte dh_Yc[];               // 1 to 2^16 -1 bytes
  51 
  52     BigInteger getClientPublicKey() {
  53         return new BigInteger(1, dh_Yc);
  54     }
  55 
  56     /*
  57      * Either pass the client's public key explicitly (because it's
  58      * using DHE or DH_anon), or implicitly (the public key was in the
  59      * certificate).
  60      */
  61     DHClientKeyExchange(BigInteger publicKey) {
  62         dh_Yc = toByteArray(publicKey);
  63     }
  64 
  65     DHClientKeyExchange() {
  66         dh_Yc = null;
  67     }
  68 
  69     /*
  70      * Get the client's public key either explicitly or implicitly.
  71      * (It's ugly to have an empty record be sent in the latter case,
  72      * but that's what the protocol spec requires.)
  73      */
  74     DHClientKeyExchange(HandshakeInStream input) throws IOException {

  75         dh_Yc = input.getBytes16();






  76     }
  77 
  78     int messageLength() {
  79         if (dh_Yc == null) {
  80             return 0;
  81         } else {
  82             return dh_Yc.length + 2;
  83         }
  84     }
  85 
  86     void send(HandshakeOutStream s) throws IOException {

  87         s.putBytes16(dh_Yc);

  88     }
  89 
  90     void print(PrintStream s) throws IOException {
  91         s.println("*** ClientKeyExchange, DH");
  92 
  93         if (debug != null && Debug.isOn("verbose")) {
  94             Debug.println(s, "DH Public key", dh_Yc);
  95         }
  96     }
  97 }
   1 /*
   2  * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 
  27 package sun.security.ssl;
  28 
  29 import java.io.IOException;
  30 import java.io.PrintStream;
  31 import java.math.BigInteger;
  32 import javax.net.ssl.SSLHandshakeException;
  33 
  34 /*
  35  * Message used by clients to send their Diffie-Hellman public
  36  * keys to servers.
  37  *
  38  * @author David Brownell
  39  */
  40 final class DHClientKeyExchange extends HandshakeMessage {
  41 
  42     int messageType() {
  43         return ht_client_key_exchange;
  44     }
  45 
  46     /*
  47      * This value may be empty if it was included in the
  48      * client's certificate ...
  49      */
  50     private byte dh_Yc[];               // 1 to 2^16 -1 bytes
  51 
  52     BigInteger getClientPublicKey() {
  53         return dh_Yc == null ? null : new BigInteger(1, dh_Yc);
  54     }
  55 
  56     /*
  57      * Either pass the client's public key explicitly (because it's
  58      * using DHE or DH_anon), or implicitly (the public key was in the
  59      * certificate).
  60      */
  61     DHClientKeyExchange(BigInteger publicKey) {
  62         dh_Yc = toByteArray(publicKey);
  63     }
  64 
  65     DHClientKeyExchange() {
  66         dh_Yc = null;
  67     }
  68 
  69     /*
  70      * Get the client's public key either explicitly or implicitly.
  71      * (It's ugly to have an empty record be sent in the latter case,
  72      * but that's what the protocol spec requires.)
  73      */
  74     DHClientKeyExchange(HandshakeInStream input) throws IOException {
  75         if (input.available() >= 2) {
  76             dh_Yc = input.getBytes16();
  77         } else {
  78             // currently, we don't support cipher suites that requires
  79             // implicit public key of client.
  80             throw new SSLHandshakeException(
  81                     "Unsupported implicit client DiffieHellman public key");
  82         }
  83     }
  84 
  85     int messageLength() {
  86         if (dh_Yc == null) {
  87             return 0;
  88         } else {
  89             return dh_Yc.length + 2;
  90         }
  91     }
  92 
  93     void send(HandshakeOutStream s) throws IOException {
  94         if (dh_Yc != null && dh_Yc.length != 0) {
  95             s.putBytes16(dh_Yc);
  96         }
  97     }
  98 
  99     void print(PrintStream s) throws IOException {
 100         s.println("*** ClientKeyExchange, DH");
 101 
 102         if (debug != null && Debug.isOn("verbose")) {
 103             Debug.println(s, "DH Public key", dh_Yc);
 104         }
 105     }
 106 }