1 /*
   2  * Copyright (c) 2017, 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.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import java.util.ArrayList;
  25 import java.util.List;
  26 
  27 /*
  28  * The TLS communication use case.
  29  */
  30 public class UseCase {
  31 
  32     private static final boolean FULL_CASES
  33             = Utils.getBoolProperty("fullCases");
  34 
  35     private static final Parameter[][] PARAMS = new Parameter[][] {
  36             FULL_CASES ? Protocol.values() : Protocol.getMandatoryValues(),
  37             FULL_CASES ? CipherSuite.values() : CipherSuite.getMandatoryValues(),
  38             FULL_CASES ? ClientAuth.values() : ClientAuth.getMandatoryValues(),
  39             FULL_CASES ? ServerName.values() : ServerName.getMandatoryValues(),
  40             FULL_CASES ? AppProtocol.values() : AppProtocol.getMandatoryValues() };
  41 
  42     public final Protocol protocol;
  43     public final CipherSuite cipherSuite;
  44     public final ClientAuth clientAuth;
  45     public final ServerName serverName;
  46     public final AppProtocol appProtocol;
  47 
  48     public final boolean negativeCase;
  49 
  50     public UseCase(
  51             Protocol protocol,
  52             CipherSuite cipherSuite,
  53             ClientAuth clientAuth,
  54             ServerName serverName,
  55             AppProtocol appProtocol) {
  56         this.protocol = protocol;
  57         this.cipherSuite = cipherSuite;
  58         this.clientAuth = clientAuth;
  59         this.serverName = serverName;
  60         this.appProtocol = appProtocol;
  61 
  62         negativeCase = !cipherSuite.supportedByProtocol(protocol);
  63     }
  64 
  65     // JDK 6 doesn't support EC key algorithm.
  66     public boolean ignoredByJdk(JdkInfo jdkInfo) {
  67         return cipherSuite.name().contains("_EC") && !jdkInfo.supportsECKey;
  68     }
  69 
  70     @Override
  71     public String toString() {
  72         return Utils.join(Utils.PARAM_DELIMITER,
  73                 "Protocol=" + protocol.version,
  74                 "CipherSuite=" + cipherSuite,
  75                 "ClientAuth=" + clientAuth,
  76                 "ServerName=" + serverName,
  77                 "AppProtocols=" + appProtocol);
  78     }
  79 
  80     public static List<UseCase> getAllUseCases() {
  81         List<UseCase> useCases = new ArrayList<>();
  82         getUseCases(PARAMS, 0, new Parameter[PARAMS.length], useCases);
  83         return useCases;
  84     }
  85 
  86     private static void getUseCases(Parameter[][] params, int index,
  87             Parameter[] currentValues, List<UseCase> useCases) {
  88         if (index == params.length) {
  89             Protocol protocol = (Protocol) currentValues[0];
  90             CipherSuite cipherSuite = (CipherSuite) currentValues[1];
  91 
  92             UseCase useCase = new UseCase(
  93                     protocol,
  94                     cipherSuite,
  95                     (ClientAuth) currentValues[2],
  96                     (ServerName) currentValues[3],
  97                     (AppProtocol) currentValues[4]);
  98             useCases.add(useCase);
  99         } else {
 100             Parameter[] values = params[index];
 101             for (int i = 0; i < values.length; i++) {
 102                 currentValues[index] = values[i];
 103                 getUseCases(params, index + 1, currentValues, useCases);
 104             }
 105         }
 106     }
 107 }