|
13 | 13 | import java.util.Map.Entry; |
14 | 14 |
|
15 | 15 | import org.apache.catalina.connector.Connector; |
16 | | -import org.apache.catalina.core.AprLifecycleListener; |
17 | 16 | import org.apache.coyote.ProtocolHandler; |
18 | 17 | import org.apache.coyote.http11.Http11Nio2Protocol; |
19 | 18 | import org.apache.coyote.http11.Http11NioProtocol; |
20 | 19 | import org.apache.tomcat.util.IntrospectionUtils; |
| 20 | +import org.apache.tomcat.util.net.SSLHostConfig; |
| 21 | +import org.apache.tomcat.util.net.SSLHostConfigCertificate; |
21 | 22 | import org.red5.logging.Red5LoggerFactory; |
22 | 23 | import org.slf4j.Logger; |
23 | 24 |
|
@@ -72,26 +73,89 @@ public void init() { |
72 | 73 | // set connection properties |
73 | 74 | if (connectionProperties != null) { |
74 | 75 | for (String key : connectionProperties.keySet()) { |
| 76 | + // skip ssl related properties |
| 77 | + if (key.startsWith("keystore") || key.startsWith("truststore") || key.startsWith("certificate") || key.equals("clientAuth") || key.equals("allowUnsafeLegacyRenegotiation")) { |
| 78 | + continue; |
| 79 | + } |
75 | 80 | connector.setProperty(key, connectionProperties.get(key)); |
76 | 81 | } |
77 | 82 | } |
78 | 83 | // turn off native apr support |
79 | | - AprLifecycleListener listener = new AprLifecycleListener(); |
80 | | - listener.setSSLEngine("off"); |
81 | | - connector.addLifecycleListener(listener); |
82 | | - // determine if https support is requested |
83 | | - if (secure) { |
84 | | - // set connection properties |
85 | | - connector.setSecure(true); |
86 | | - connector.setScheme("https"); |
87 | | - } |
| 84 | + //AprLifecycleListener listener = new AprLifecycleListener(); |
| 85 | + //listener.setSSLEngine("off"); |
| 86 | + //connector.addLifecycleListener(listener); |
88 | 87 | // apply the bind address to the handler |
89 | 88 | ProtocolHandler handler = connector.getProtocolHandler(); |
90 | 89 | if (handler instanceof Http11Nio2Protocol) { |
91 | 90 | ((Http11Nio2Protocol) handler).setAddress(address.getAddress()); |
92 | 91 | } else if (handler instanceof Http11NioProtocol) { |
93 | 92 | ((Http11NioProtocol) handler).setAddress(address.getAddress()); |
94 | 93 | } |
| 94 | + // Reference https://tomcat.apache.org/tomcat-11.0-doc/ssl-howto.html#SSL_and_Tomcat |
| 95 | + // determine if https support is requested |
| 96 | + if (secure) { |
| 97 | + // set connection properties |
| 98 | + connector.setSecure(true); |
| 99 | + connector.setScheme("https"); |
| 100 | + connector.setProperty("SSLEnabled", "true"); |
| 101 | + // create a new ssl host config |
| 102 | + SSLHostConfig sslHostConfig = new SSLHostConfig(); |
| 103 | + /* |
| 104 | + <entry key="sslProtocol" value="TLS" /> |
| 105 | + <entry key="keystoreFile" value="${rtmps.keystorefile}" /> |
| 106 | + <entry key="keystorePass" value="${rtmps.keystorepass}" /> |
| 107 | + <entry key="truststoreFile" value="${rtmps.truststorefile}" /> |
| 108 | + <entry key="truststorePass" value="${rtmps.truststorepass}" /> |
| 109 | + <entry key="clientAuth" value="false" /> |
| 110 | + <entry key="allowUnsafeLegacyRenegotiation" value="true" /> |
| 111 | + */ |
| 112 | + sslHostConfig.setSslProtocol("TLS"); |
| 113 | + sslHostConfig.setTruststoreFile(connectionProperties.get("truststoreFile")); |
| 114 | + sslHostConfig.setTruststorePassword(connectionProperties.get("truststorePass")); |
| 115 | + if (connectionProperties.containsKey("truststoreType")) { |
| 116 | + sslHostConfig.setTruststoreType(connectionProperties.get("truststoreType")); |
| 117 | + } else { |
| 118 | + sslHostConfig.setTruststoreType("JKS"); |
| 119 | + } |
| 120 | + // set the protocols |
| 121 | + if (connectionProperties.containsKey("protocols")) { |
| 122 | + String[] protocols = connectionProperties.get("protocols").split(","); |
| 123 | + //sslHostConfig.setProtocols(protocols); |
| 124 | + sslHostConfig.setEnabledProtocols(protocols); |
| 125 | + } else { |
| 126 | + sslHostConfig.setProtocols("TLSv1.2"); |
| 127 | + sslHostConfig.setEnabledProtocols(new String[] { "TLSv1.2" }); |
| 128 | + } |
| 129 | + // set the ciphers |
| 130 | + if (connectionProperties.containsKey("ciphers")) { |
| 131 | + String[] ciphers = connectionProperties.get("ciphers").split(","); |
| 132 | + //sslHostConfig.setCiphers(ciphers); |
| 133 | + sslHostConfig.setEnabledCiphers(ciphers); |
| 134 | + } else { |
| 135 | + //sslHostConfig.setCiphers("TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256,TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384"); |
| 136 | + } |
| 137 | + // dont allow unsafe renegotiation |
| 138 | + sslHostConfig.setInsecureRenegotiation(!secure); |
| 139 | + // create a new ssl host config certificate |
| 140 | + SSLHostConfigCertificate sslHostConfigCert = new SSLHostConfigCertificate(sslHostConfig, SSLHostConfigCertificate.Type.RSA); |
| 141 | + sslHostConfigCert.setCertificateKeystoreFile(connectionProperties.get("keystoreFile")); |
| 142 | + sslHostConfigCert.setCertificateKeystorePassword(connectionProperties.get("keystorePass")); |
| 143 | + if (connectionProperties.containsKey("keystoreType")) { |
| 144 | + sslHostConfigCert.setCertificateKeystoreType(connectionProperties.get("keystoreType")); |
| 145 | + } else { |
| 146 | + sslHostConfigCert.setCertificateKeystoreType("JKS"); |
| 147 | + } |
| 148 | + // set the certificate key alias |
| 149 | + if (connectionProperties.containsKey("certificateKeyAlias")) { |
| 150 | + sslHostConfigCert.setCertificateKeyAlias(connectionProperties.get("certificateKeyAlias")); |
| 151 | + } else { |
| 152 | + //sslHostConfigCert.setCertificateKeyAlias("red5"); |
| 153 | + } |
| 154 | + // add the ssl host config certificate to the ssl host config |
| 155 | + sslHostConfig.addCertificate(sslHostConfigCert); |
| 156 | + // add the ssl host config to the handler |
| 157 | + handler.addSslHostConfig(sslHostConfig); |
| 158 | + } |
95 | 159 | // set initialized flag |
96 | 160 | initialized = true; |
97 | 161 | } catch (Throwable t) { |
|
0 commit comments