@@ -396,14 +396,14 @@ void initialize(String hostname, OpenSSLKey channelIdPrivateKey) throws IOExcept
396396 NativeCrypto .SSL_set1_groups (ssl , this , toBoringSslGroups (paramsNamedGroups ));
397397 } else {
398398 // Use default named group.
399- String namedGroupsProperty = System .getProperty ("jdk.tls.namedGroups" );
400- if (namedGroupsProperty == null || namedGroupsProperty .isEmpty ()) {
401- // If the property is not set or empty, use the default named groups. See:
399+ int [] parsedNamedGroups = getParsedTlsNamedGroupsPropertyOrNull ();
400+ if (parsedNamedGroups == null ) {
401+ // The jdk.tls.namedGroups property has not been set or is empty. We have to use the
402+ // default named groups. See:
402403 // https://docs.oracle.com/javase/8/docs/technotes/guides/security/jsse/JSSERefGuide.html
403404 setDefaultNamedGroups (ssl , this );
404405 } else {
405- int [] groups = parseNamedGroupsProperty (namedGroupsProperty );
406- NativeCrypto .SSL_set1_groups (ssl , this , groups );
406+ NativeCrypto .SSL_set1_groups (ssl , this , parsedNamedGroups );
407407 }
408408 }
409409
@@ -798,4 +798,46 @@ void close() {
798798 }
799799 }
800800 }
801+
802+ /**
803+ * Returns the parsed value of the "jdk.tls.namedGroups" property.
804+ *
805+ * <p>Is null if the property is not set or empty.
806+ */
807+ static synchronized int [] getParsedTlsNamedGroupsPropertyOrNull () {
808+ try {
809+ parseTlsNamedGroupsProperty ();
810+ } catch (IllegalArgumentException e ) {
811+ // This may happen if the user set the property to an invalid value.
812+ // Since the last time the property was parsed successfully. We ignore it
813+ // and return the previously parsed value.
814+ }
815+ return parsedTlsNamedGroupsProperty ;
816+ }
817+
818+ private static int [] parsedTlsNamedGroupsProperty = null ;
819+ private static String unparsedTlsNamedGroupsProperty = "" ;
820+
821+ /**
822+ * Parses the "jdk.tls.namedGroups" property and stores the result in {@code
823+ * parsedTlsNamedGroupsProperty}.
824+ *
825+ * <p>May throw an {@link IllegalArgumentException} if the property contains invalid values.
826+ */
827+ static synchronized void parseTlsNamedGroupsProperty () {
828+ String property = System .getProperty ("jdk.tls.namedGroups" );
829+ if (property == null || property .isEmpty ()) {
830+ parsedTlsNamedGroupsProperty = null ;
831+ unparsedTlsNamedGroupsProperty = "" ;
832+ } else {
833+ if (!property .equals (unparsedTlsNamedGroupsProperty )) {
834+ unparsedTlsNamedGroupsProperty = property ;
835+ parsedTlsNamedGroupsProperty = parseNamedGroupsProperty (property );
836+ }
837+ }
838+ }
839+
840+ static {
841+ parseTlsNamedGroupsProperty ();
842+ }
801843}
0 commit comments