Guest User

Untitled

a guest
Sep 1st, 2023
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.14 KB | None | 0 0
  1. /*
  2.  * Copyright (c) 2017, Red Hat, Inc. and/or its affiliates.
  3.  * Copyright (c) 2018, 2022, Oracle and/or its affiliates. All rights reserved.
  4.  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  5.  *
  6.  * This code is free software; you can redistribute it and/or modify it
  7.  * under the terms of the GNU General Public License version 2 only, as
  8.  * published by the Free Software Foundation.  Oracle designates this
  9.  * particular file as subject to the "Classpath" exception as provided
  10.  * by Oracle in the LICENSE file that accompanied this code.
  11.  *
  12.  * This code is distributed in the hope that it will be useful, but WITHOUT
  13.  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14.  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15.  * version 2 for more details (a copy is included in the LICENSE file that
  16.  * accompanied this code).
  17.  *
  18.  * You should have received a copy of the GNU General Public License version
  19.  * 2 along with this work; if not, write to the Free Software Foundation,
  20.  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  21.  *
  22.  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  23.  * or visit www.oracle.com if you need additional information or have any
  24.  * questions.
  25.  */
  26.  
  27. package sun.security.ssl;
  28.  
  29. import java.io.IOException;
  30. import java.nio.ByteBuffer;
  31. import java.util.Map;
  32. import java.util.Random;
  33.  
  34. import javax.net.ssl.SSLProtocolException;
  35. import static sun.security.ssl.SSLExtension.TLS_GREASE_0A0A;
  36. import static sun.security.ssl.SSLExtension.TLS_GREASE_1A1A;
  37. import static sun.security.ssl.SSLExtension.TLS_GREASE_2A2A;
  38. import static sun.security.ssl.SSLExtension.TLS_GREASE_3A3A;
  39. import static sun.security.ssl.SSLExtension.TLS_GREASE_4A4A;
  40. import static sun.security.ssl.SSLExtension.TLS_GREASE_5A5A;
  41. import static sun.security.ssl.SSLExtension.TLS_GREASE_6A6A;
  42. import static sun.security.ssl.SSLExtension.TLS_GREASE_7A7A;
  43. import static sun.security.ssl.SSLExtension.TLS_GREASE_8A8A;
  44. import static sun.security.ssl.SSLExtension.TLS_GREASE_9A9A;
  45. import static sun.security.ssl.SSLExtension.TLS_GREASE_AAAA;
  46. import sun.security.ssl.SSLExtension.ExtensionConsumer;
  47. import sun.security.ssl.SSLExtension.SSLExtensionSpec;
  48. import sun.security.ssl.SSLHandshake.HandshakeMessage;
  49.  
  50. /**
  51.  * Pack of the "TLS_GREASE" extensions [RFC 7627].
  52.  */
  53. final class TLSGreaseExtension {
  54.     static final HandshakeProducer chNetworkProducer =
  55.             new TLSGreaseProducer();
  56.     static final ExtensionConsumer chOnLoadConsumer =
  57.             new TLSGreaseConsumer();
  58.     static final HandshakeAbsence chOnLoadAbsence =
  59.             new TLSGreaseAbsence();
  60.  
  61.     static final SSLStringizer emsStringizer =
  62.             new TLSGreaseStringizer();
  63.  
  64.     /**
  65.      * The "TLS_GREASE" extension.
  66.      */
  67.     static final class TLSGreaseSpec implements SSLExtensionSpec {
  68.         // A nominal object that does not hold any real renegotiation info.
  69.         static final TLSGreaseSpec NOMINAL =
  70.                 new TLSGreaseSpec();
  71.  
  72.         private TLSGreaseSpec() {
  73.             // blank
  74.         }
  75.  
  76.         private TLSGreaseSpec(HandshakeContext hc,
  77.                 ByteBuffer m) throws IOException {
  78.             // Parse the extension.
  79.             if (m.hasRemaining()) {
  80.                 throw hc.conContext.fatal(Alert.DECODE_ERROR,
  81.                         new SSLProtocolException(
  82.                     "Invalid TLS_GREASE extension data: " +
  83.                     "not empty"));
  84.             }
  85.         }
  86.  
  87.         @Override
  88.         public String toString() {
  89.             return "<empty>";
  90.         }
  91.     }
  92.  
  93.     private static final
  94.             class TLSGreaseStringizer implements SSLStringizer {
  95.         @Override
  96.         public String toString(HandshakeContext hc, ByteBuffer buffer) {
  97.             try {
  98.                 return (new TLSGreaseSpec(hc, buffer)).toString();
  99.             } catch (IOException ioe) {
  100.                 // For debug logging only, so please swallow exceptions.
  101.                 return ioe.getMessage();
  102.             }
  103.         }
  104.     }
  105.  
  106.     /**
  107.      * Network data producer of an "TLS_GREASE" extension in
  108.      * the ClientHello handshake message.
  109.      */
  110.     private static final
  111.             class TLSGreaseProducer implements HandshakeProducer {
  112.         // Prevent instantiation of this class.
  113.         private TLSGreaseProducer() {
  114.             // blank
  115.         }
  116.  
  117.         @Override
  118.         public byte[] produce(ConnectionContext context,
  119.                 HandshakeMessage message) throws IOException {
  120.             // The producing happens in client side only.
  121.             ClientHandshakeContext chc = (ClientHandshakeContext)context;
  122.  
  123.             chc.handshakeExtensions.put(TLS_GREASE_0A0A,
  124.                 TLSGreaseSpec.NOMINAL);
  125.  
  126.             byte[] extData = new byte[1];
  127.             ByteBuffer m = ByteBuffer.wrap(extData);
  128.             Record.putInt8(m, 0x0);
  129.             return extData;
  130.         }
  131.     }
  132.  
  133.     /**
  134.      * Network data producer of an "TLS_GREASE" extension in
  135.      * the ServerHello handshake message.
  136.      */
  137.     private static final
  138.             class TLSGreaseConsumer implements ExtensionConsumer {
  139.         // Prevent instantiation of this class.
  140.         private TLSGreaseConsumer() {
  141.             // blank
  142.         }
  143.  
  144.         @Override
  145.         public void consume(ConnectionContext context,
  146.             HandshakeMessage message, ByteBuffer buffer) throws IOException {
  147.  
  148.             // The consuming happens in server side only.
  149.             ServerHandshakeContext shc = (ServerHandshakeContext)context;
  150.  
  151.             // Update the context.
  152.             //
  153.             shc.handshakeExtensions.put(
  154.                 TLS_GREASE_0A0A, TLSGreaseSpec.NOMINAL);
  155.  
  156.             // No impact on session resumption.
  157.         }
  158.     }
  159.  
  160.     /**
  161.      * The absence processing if an "TLS_GREASE" extension is
  162.      * not present in the ClientHello handshake message.
  163.      */
  164.     private static final
  165.             class TLSGreaseAbsence implements HandshakeAbsence {
  166.         @Override
  167.         public void absent(ConnectionContext context,
  168.                 HandshakeMessage message) throws IOException {
  169.  
  170.         }
  171.     }
  172. }
  173.  
  174.  
Advertisement
Add Comment
Please, Sign In to add comment