jdk/src/java.base/share/classes/sun/security/ssl/KerberosClientKeyExchange.java
changeset 34071 654e1a23e215
parent 34070 792098bae87e
parent 31008 5b500c93ce48
child 34072 287dc5c0072b
equal deleted inserted replaced
34070:792098bae87e 34071:654e1a23e215
     1 /*
       
     2  * Copyright (c) 2003, 2013, 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 package sun.security.ssl;
       
    27 
       
    28 import java.io.IOException;
       
    29 import java.io.PrintStream;
       
    30 import java.security.AccessController;
       
    31 import java.security.AccessControlContext;
       
    32 import java.security.Principal;
       
    33 import java.security.PrivilegedAction;
       
    34 import java.security.SecureRandom;
       
    35 import javax.crypto.SecretKey;
       
    36 
       
    37 /**
       
    38  * A helper class that calls the KerberosClientKeyExchange implementation.
       
    39  */
       
    40 public class KerberosClientKeyExchange extends HandshakeMessage {
       
    41 
       
    42     private static final String IMPL_CLASS =
       
    43         "sun.security.ssl.krb5.KerberosClientKeyExchangeImpl";
       
    44 
       
    45     private static final Class<?> implClass = AccessController.doPrivileged(
       
    46             new PrivilegedAction<Class<?>>() {
       
    47                 @Override
       
    48                 public Class<?> run() {
       
    49                     try {
       
    50                         return Class.forName(IMPL_CLASS, true, null);
       
    51                     } catch (ClassNotFoundException cnf) {
       
    52                         return null;
       
    53                     }
       
    54                 }
       
    55             }
       
    56         );
       
    57     private final KerberosClientKeyExchange impl = createImpl();
       
    58 
       
    59     private KerberosClientKeyExchange createImpl() {
       
    60         if (implClass != null &&
       
    61                 getClass() == KerberosClientKeyExchange.class) {
       
    62             try {
       
    63                 return (KerberosClientKeyExchange)implClass.newInstance();
       
    64             } catch (InstantiationException e) {
       
    65                 throw new AssertionError(e);
       
    66             } catch (IllegalAccessException e) {
       
    67                 throw new AssertionError(e);
       
    68             }
       
    69         }
       
    70         return null;
       
    71     }
       
    72 
       
    73     // This constructor will be called when constructing an instance of its
       
    74     // subclass -- KerberosClientKeyExchangeImpl.  Please won't check the
       
    75     // value of impl variable in this constructor.
       
    76     protected KerberosClientKeyExchange() {
       
    77         // please won't check the value of impl variable
       
    78     }
       
    79 
       
    80     public KerberosClientKeyExchange(String serverName,
       
    81         AccessControlContext acc, ProtocolVersion protocolVersion,
       
    82         SecureRandom rand) throws IOException {
       
    83 
       
    84         if (impl != null) {
       
    85             init(serverName, acc, protocolVersion, rand);
       
    86         } else {
       
    87             throw new IllegalStateException("Kerberos is unavailable");
       
    88         }
       
    89     }
       
    90 
       
    91     public KerberosClientKeyExchange(ProtocolVersion protocolVersion,
       
    92             ProtocolVersion clientVersion, SecureRandom rand,
       
    93             HandshakeInStream input, AccessControlContext acc,
       
    94             Object serverKeys) throws IOException {
       
    95 
       
    96         if (impl != null) {
       
    97             init(protocolVersion, clientVersion, rand, input, acc, serverKeys);
       
    98         } else {
       
    99             throw new IllegalStateException("Kerberos is unavailable");
       
   100         }
       
   101     }
       
   102 
       
   103     @Override
       
   104     int messageType() {
       
   105         return ht_client_key_exchange;
       
   106     }
       
   107 
       
   108     @Override
       
   109     public int messageLength() {
       
   110         return impl.messageLength();
       
   111     }
       
   112 
       
   113     @Override
       
   114     public void send(HandshakeOutStream s) throws IOException {
       
   115         impl.send(s);
       
   116     }
       
   117 
       
   118     @Override
       
   119     public void print(PrintStream p) throws IOException {
       
   120         impl.print(p);
       
   121     }
       
   122 
       
   123     public void init(String serverName,
       
   124         AccessControlContext acc, ProtocolVersion protocolVersion,
       
   125         SecureRandom rand) throws IOException {
       
   126 
       
   127         if (impl != null) {
       
   128             impl.init(serverName, acc, protocolVersion, rand);
       
   129         }
       
   130     }
       
   131 
       
   132     public void init(ProtocolVersion protocolVersion,
       
   133             ProtocolVersion clientVersion, SecureRandom rand,
       
   134             HandshakeInStream input, AccessControlContext acc,
       
   135             Object ServiceCreds) throws IOException {
       
   136 
       
   137         if (impl != null) {
       
   138             impl.init(protocolVersion, clientVersion,
       
   139                                     rand, input, acc, ServiceCreds);
       
   140         }
       
   141     }
       
   142 
       
   143     public byte[] getUnencryptedPreMasterSecret() {
       
   144         return impl.getUnencryptedPreMasterSecret();
       
   145     }
       
   146 
       
   147     public Principal getPeerPrincipal(){
       
   148         return impl.getPeerPrincipal();
       
   149     }
       
   150 
       
   151     public Principal getLocalPrincipal(){
       
   152         return impl.getLocalPrincipal();
       
   153     }
       
   154 }