NativeCrypto.java revision ffeba5dd766602f6e2be9caa9081744348a53c04
1/* 2 * Copyright (C) 2008 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package org.apache.harmony.xnet.provider.jsse; 18 19import java.io.FileDescriptor; 20import java.io.IOException; 21import java.lang.reflect.Field; 22import java.net.Socket; 23import java.net.SocketImpl; 24import java.net.SocketTimeoutException; 25import java.security.cert.Certificate; 26import java.security.cert.CertificateEncodingException; 27import java.security.cert.CertificateException; 28import java.security.cert.X509Certificate; 29import java.security.interfaces.RSAPublicKey; 30import java.util.ArrayList; 31import java.util.HashMap; 32import java.util.LinkedHashMap; 33import java.util.List; 34import java.util.Map; 35import java.util.Set; 36import javax.net.ssl.SSLException; 37 38/** 39 * Provides the Java side of our JNI glue for OpenSSL. 40 */ 41public final class NativeCrypto { 42 43 // --- OpenSSL library initialization -------------------------------------- 44 static { 45 clinit(); 46 } 47 48 private native static void clinit(); 49 50 // --- DSA/RSA public/private key handling functions ----------------------- 51 52 public static native int EVP_PKEY_new_DSA(byte[] p, byte[] q, byte[] g, 53 byte[] priv_key, byte[] pub_key); 54 55 public static native int EVP_PKEY_new_RSA(byte[] n, byte[] e, byte[] d, byte[] p, byte[] q); 56 57 public static native void EVP_PKEY_free(int pkey); 58 59 // --- General context handling functions (despite the names) -------------- 60 61 public static native int EVP_MD_CTX_create(); 62 63 public static native void EVP_MD_CTX_destroy(int ctx); 64 65 public static native int EVP_MD_CTX_copy(int ctx); 66 67 // --- Digest handling functions ------------------------------------------- 68 69 public static native void EVP_DigestInit(int ctx, String algorithm); 70 71 public static native void EVP_DigestUpdate(int ctx, byte[] buffer, int offset, int length); 72 73 public static native int EVP_DigestFinal(int ctx, byte[] hash, int offset); 74 75 public static native int EVP_MD_CTX_size(int ctx); 76 77 public static native int EVP_MD_CTX_block_size(int ctx); 78 79 // --- Signature handling functions ---------------------------------------- 80 81 public static native void EVP_VerifyInit(int ctx, String algorithm); 82 83 public static native void EVP_VerifyUpdate(int ctx, byte[] buffer, 84 int offset, int length); 85 86 public static native int EVP_VerifyFinal(int ctx, byte[] signature, 87 int offset, int length, int key); 88 89 // --- Legacy Signature handling ------------------------------------------- 90 // TODO rewrite/replace with EVP_Verify* 91 /** 92 * Verifies an RSA signature. Conceptually, this method doesn't really 93 * belong here, but due to its native code being closely tied to OpenSSL 94 * (just like the rest of this class), we put it here for the time being. 95 * This also solves potential problems with native library initialization. 96 * 97 * @param message The message to verify 98 * @param signature The signature to verify 99 * @param algorithm The hash/sign algorithm to use, i.e. "RSA-SHA1" 100 * @param key The RSA public key to use 101 * @return true if the verification succeeds, false otherwise 102 */ 103 public static boolean verifySignature( 104 byte[] message, byte[] signature, String algorithm, RSAPublicKey key) { 105 byte[] modulus = key.getModulus().toByteArray(); 106 byte[] exponent = key.getPublicExponent().toByteArray(); 107 108 return verifySignature(message, signature, algorithm, modulus, exponent) == 1; 109 } 110 111 private static native int verifySignature(byte[] message, byte[] signature, 112 String algorithm, byte[] modulus, byte[] exponent); 113 114 // --- RAND seeding -------------------------------------------------------- 115 116 public static final int RAND_SEED_LENGTH_IN_BYTES = 1024; 117 118 public static native void RAND_seed(byte[] seed); 119 120 public static native int RAND_load_file(String filename, long max_bytes); 121 122 // --- SSL handling -------------------------------------------------------- 123 124 private static final Field JAVA_NET_SOCKET_IMPL; 125 private static final Field JAVA_NET_SOCKETIMPL_FD; 126 static { 127 try { 128 JAVA_NET_SOCKET_IMPL = Socket.class.getDeclaredField("impl"); 129 JAVA_NET_SOCKET_IMPL.setAccessible(true); 130 JAVA_NET_SOCKETIMPL_FD = SocketImpl.class.getDeclaredField("fd"); 131 JAVA_NET_SOCKETIMPL_FD.setAccessible(true); 132 } catch (Exception e) { 133 throw new AssertionError(e); 134 } 135 } 136 /** 137 * Return the FileDescriptor associated with the provided socket. 138 */ 139 public static FileDescriptor getFileDescriptor(Socket socket) { 140 try { 141 SocketImpl socketImpl = (SocketImpl) JAVA_NET_SOCKET_IMPL.get(socket); 142 FileDescriptor fd = (FileDescriptor) JAVA_NET_SOCKETIMPL_FD.get(socketImpl); 143 return fd; 144 } catch (IllegalAccessException e) { 145 throw new AssertionError(e); 146 } 147 } 148 149 private static final String SUPPORTED_PROTOCOL_SSLV3 = "SSLv3"; 150 private static final String SUPPORTED_PROTOCOL_TLSV1 = "TLSv1"; 151 152 public static final Map<String, String> OPENSSL_TO_STANDARD_CIPHER_SUITES 153 = new HashMap<String, String>(); 154 public static final Map<String, String> STANDARD_TO_OPENSSL_CIPHER_SUITES 155 = new LinkedHashMap<String, String>(); 156 157 private static void add(String standard, String openssl) { 158 OPENSSL_TO_STANDARD_CIPHER_SUITES.put(openssl, standard); 159 STANDARD_TO_OPENSSL_CIPHER_SUITES.put(standard, openssl); 160 } 161 162 /** 163 * TLS_EMPTY_RENEGOTIATION_INFO_SCSV is RFC 5746's renegotiation 164 * indication signaling cipher suite value. It is not a real 165 * cipher suite. It is just an indication in the default and 166 * supported cipher suite lists indicates that the implementation 167 * supports secure renegotiation. 168 * 169 * In the RI, its presence means that the SCSV is sent in the 170 * cipher suite list to indicate secure renegotiation support and 171 * its absense means to send an empty TLS renegotiation info 172 * extension instead. 173 * 174 * However, OpenSSL doesn't provide an API to give this level of 175 * control, instead always sending the SCSV and always including 176 * the empty renegotiation info if TLS is used (as opposed to 177 * SSL). So we simply allow TLS_EMPTY_RENEGOTIATION_INFO_SCSV to 178 * be passed for compatibility as to provide the hint that we 179 * support secure renegotiation. 180 */ 181 public static String TLS_EMPTY_RENEGOTIATION_INFO_SCSV 182 = "TLS_EMPTY_RENEGOTIATION_INFO_SCSV"; 183 184 static { 185 // Note these are added in priority order 186 add("SSL_RSA_WITH_RC4_128_MD5", "RC4-MD5"); 187 add("SSL_RSA_WITH_RC4_128_SHA", "RC4-SHA"); 188 add("TLS_RSA_WITH_AES_128_CBC_SHA", "AES128-SHA"); 189 add("TLS_RSA_WITH_AES_256_CBC_SHA", "AES256-SHA"); 190 add("TLS_ECDH_ECDSA_WITH_RC4_128_SHA", "ECDH-ECDSA-RC4-SHA"); 191 add("TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA", "ECDH-ECDSA-AES128-SHA"); 192 add("TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA", "ECDH-ECDSA-AES256-SHA"); 193 add("TLS_ECDH_RSA_WITH_RC4_128_SHA", "ECDH-RSA-RC4-SHA"); 194 add("TLS_ECDH_RSA_WITH_AES_128_CBC_SHA", "ECDH-RSA-AES128-SHA"); 195 add("TLS_ECDH_RSA_WITH_AES_256_CBC_SHA", "ECDH-RSA-AES256-SHA"); 196 add("TLS_ECDHE_ECDSA_WITH_RC4_128_SHA", "ECDHE-ECDSA-RC4-SHA"); 197 add("TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA", "ECDHE-ECDSA-AES128-SHA"); 198 add("TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA", "ECDHE-ECDSA-AES256-SHA"); 199 add("TLS_ECDHE_RSA_WITH_RC4_128_SHA", "ECDHE-RSA-RC4-SHA"); 200 add("TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA", "ECDHE-RSA-AES128-SHA"); 201 add("TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA", "ECDHE-RSA-AES256-SHA"); 202 add("TLS_DHE_RSA_WITH_AES_128_CBC_SHA", "DHE-RSA-AES128-SHA"); 203 add("TLS_DHE_RSA_WITH_AES_256_CBC_SHA", "DHE-RSA-AES256-SHA"); 204 add("TLS_DHE_DSS_WITH_AES_128_CBC_SHA", "DHE-DSS-AES128-SHA"); 205 add("TLS_DHE_DSS_WITH_AES_256_CBC_SHA", "DHE-DSS-AES256-SHA"); 206 add("SSL_RSA_WITH_3DES_EDE_CBC_SHA", "DES-CBC3-SHA"); 207 add("TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA", "ECDH-ECDSA-DES-CBC3-SHA"); 208 add("TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA", "ECDH-RSA-DES-CBC3-SHA"); 209 add("TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA", "ECDHE-ECDSA-DES-CBC3-SHA"); 210 add("TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA", "ECDHE-RSA-DES-CBC3-SHA"); 211 add("SSL_DHE_RSA_WITH_3DES_EDE_CBC_SHA", "EDH-RSA-DES-CBC3-SHA"); 212 add("SSL_DHE_DSS_WITH_3DES_EDE_CBC_SHA", "EDH-DSS-DES-CBC3-SHA"); 213 add("SSL_RSA_WITH_DES_CBC_SHA", "DES-CBC-SHA"); 214 add("SSL_DHE_RSA_WITH_DES_CBC_SHA", "EDH-RSA-DES-CBC-SHA"); 215 add("SSL_DHE_DSS_WITH_DES_CBC_SHA", "EDH-DSS-DES-CBC-SHA"); 216 add("SSL_RSA_EXPORT_WITH_RC4_40_MD5", "EXP-RC4-MD5"); 217 add("SSL_RSA_EXPORT_WITH_DES40_CBC_SHA", "EXP-DES-CBC-SHA"); 218 add("SSL_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA", "EXP-EDH-RSA-DES-CBC-SHA"); 219 add("SSL_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA", "EXP-EDH-DSS-DES-CBC-SHA"); 220 add("SSL_RSA_WITH_NULL_MD5", "NULL-MD5"); 221 add("SSL_RSA_WITH_NULL_SHA", "NULL-SHA"); 222 add("TLS_ECDH_ECDSA_WITH_NULL_SHA", "ECDH-ECDSA-NULL-SHA"); 223 add("TLS_ECDH_RSA_WITH_NULL_SHA", "ECDH-RSA-NULL-SHA"); 224 add("TLS_ECDHE_ECDSA_WITH_NULL_SHA", "ECDHE-ECDSA-NULL-SHA"); 225 add("TLS_ECDHE_RSA_WITH_NULL_SHA", "ECDHE-RSA-NULL-SHA"); 226 add("SSL_DH_anon_WITH_RC4_128_MD5", "ADH-RC4-MD5"); 227 add("TLS_DH_anon_WITH_AES_128_CBC_SHA", "ADH-AES128-SHA"); 228 add("TLS_DH_anon_WITH_AES_256_CBC_SHA", "ADH-AES256-SHA"); 229 add("SSL_DH_anon_WITH_3DES_EDE_CBC_SHA", "ADH-DES-CBC3-SHA"); 230 add("SSL_DH_anon_WITH_DES_CBC_SHA", "ADH-DES-CBC-SHA"); 231 add("TLS_ECDH_anon_WITH_RC4_128_SHA", "AECDH-RC4-SHA"); 232 add("TLS_ECDH_anon_WITH_AES_128_CBC_SHA", "AECDH-AES128-SHA"); 233 add("TLS_ECDH_anon_WITH_AES_256_CBC_SHA", "AECDH-AES256-SHA"); 234 add("TLS_ECDH_anon_WITH_3DES_EDE_CBC_SHA", "AECDH-DES-CBC3-SHA"); 235 add("SSL_DH_anon_EXPORT_WITH_RC4_40_MD5", "EXP-ADH-RC4-MD5"); 236 add("SSL_DH_anon_EXPORT_WITH_DES40_CBC_SHA", "EXP-ADH-DES-CBC-SHA"); 237 add("TLS_ECDH_anon_WITH_NULL_SHA", "AECDH-NULL-SHA"); 238 239 // No Kerberos in Android 240 // add("TLS_KRB5_WITH_RC4_128_SHA", "KRB5-RC4-SHA"); 241 // add("TLS_KRB5_WITH_RC4_128_MD5", "KRB5-RC4-MD5"); 242 // add("TLS_KRB5_WITH_3DES_EDE_CBC_SHA", "KRB5-DES-CBC3-SHA"); 243 // add("TLS_KRB5_WITH_3DES_EDE_CBC_MD5", "KRB5-DES-CBC3-MD5"); 244 // add("TLS_KRB5_WITH_DES_CBC_SHA", "KRB5-DES-CBC-SHA"); 245 // add("TLS_KRB5_WITH_DES_CBC_MD5", "KRB5-DES-CBC-MD5"); 246 // add("TLS_KRB5_EXPORT_WITH_RC4_40_SHA", "EXP-KRB5-RC4-SHA"); 247 // add("TLS_KRB5_EXPORT_WITH_RC4_40_MD5", "EXP-KRB5-RC4-MD5"); 248 // add("TLS_KRB5_EXPORT_WITH_DES_CBC_40_SHA", "EXP-KRB5-DES-CBC-SHA"); 249 // add("TLS_KRB5_EXPORT_WITH_DES_CBC_40_MD5", "EXP-KRB5-DES-CBC-MD5"); 250 251 // not implemented by either RI or OpenSSL 252 // add("SSL_DH_DSS_EXPORT_WITH_DES40_CBC_SHA", null); 253 // add("SSL_DH_RSA_EXPORT_WITH_DES40_CBC_SHA", null); 254 255 // EXPORT1024 suites were never standardized but were widely implemented. 256 // OpenSSL 0.9.8c and later have disabled TLS1_ALLOW_EXPERIMENTAL_CIPHERSUITES 257 // add("SSL_RSA_EXPORT1024_WITH_DES_CBC_SHA", "EXP1024-DES-CBC-SHA"); 258 // add("SSL_RSA_EXPORT1024_WITH_RC4_56_SHA", "EXP1024-RC4-SHA"); 259 260 // No RC2 261 // add("SSL_RSA_EXPORT_WITH_RC2_CBC_40_MD5", "EXP-RC2-CBC-MD5"); 262 // add("TLS_KRB5_EXPORT_WITH_RC2_CBC_40_SHA", "EXP-KRB5-RC2-CBC-SHA"); 263 // add("TLS_KRB5_EXPORT_WITH_RC2_CBC_40_MD5", "EXP-KRB5-RC2-CBC-MD5"); 264 265 // PSK is Private Shared Key - didn't exist in Froyo's openssl - no JSSE equivalent 266 // add(null, "PSK-3DES-EDE-CBC-SHA"); 267 // add(null, "PSK-AES128-CBC-SHA"); 268 // add(null, "PSK-AES256-CBC-SHA"); 269 // add(null, "PSK-RC4-SHA"); 270 271 // Signaling Cipher Suite Value for secure renegotiation handled as special case. 272 // add("TLS_EMPTY_RENEGOTIATION_INFO_SCSV", null); 273 } 274 275 private static final String[] SUPPORTED_CIPHER_SUITES; 276 static { 277 int size = STANDARD_TO_OPENSSL_CIPHER_SUITES.size(); 278 SUPPORTED_CIPHER_SUITES = new String[size + 1]; 279 STANDARD_TO_OPENSSL_CIPHER_SUITES.keySet().toArray(SUPPORTED_CIPHER_SUITES); 280 SUPPORTED_CIPHER_SUITES[size] = TLS_EMPTY_RENEGOTIATION_INFO_SCSV; 281 } 282 283 // SSL mode from ssl.h 284 public static long SSL_MODE_HANDSHAKE_CUTTHROUGH = 0x00000040L; 285 286 // SSL options from ssl.h 287 public static long SSL_OP_NO_TICKET = 0x00004000L; 288 public static long SSL_OP_NO_COMPRESSION = 0x00020000L; 289 public static long SSL_OP_NO_SSLv3 = 0x02000000L; 290 public static long SSL_OP_NO_TLSv1 = 0x04000000L; 291 292 public static native int SSL_CTX_new(); 293 294 public static String[] getDefaultCipherSuites() { 295 return new String[] { 296 "SSL_RSA_WITH_RC4_128_MD5", 297 "SSL_RSA_WITH_RC4_128_SHA", 298 "TLS_RSA_WITH_AES_128_CBC_SHA", 299 "TLS_RSA_WITH_AES_256_CBC_SHA", 300 "TLS_ECDH_ECDSA_WITH_RC4_128_SHA", 301 "TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA", 302 "TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA", 303 "TLS_ECDH_RSA_WITH_RC4_128_SHA", 304 "TLS_ECDH_RSA_WITH_AES_128_CBC_SHA", 305 "TLS_ECDH_RSA_WITH_AES_256_CBC_SHA", 306 "TLS_ECDHE_ECDSA_WITH_RC4_128_SHA", 307 "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA", 308 "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA", 309 "TLS_ECDHE_RSA_WITH_RC4_128_SHA", 310 "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA", 311 "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA", 312 "TLS_DHE_RSA_WITH_AES_128_CBC_SHA", 313 "TLS_DHE_RSA_WITH_AES_256_CBC_SHA", 314 "TLS_DHE_DSS_WITH_AES_128_CBC_SHA", 315 "TLS_DHE_DSS_WITH_AES_256_CBC_SHA", 316 "SSL_RSA_WITH_3DES_EDE_CBC_SHA", 317 "TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA", 318 "TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA", 319 "TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA", 320 "TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA", 321 "SSL_DHE_RSA_WITH_3DES_EDE_CBC_SHA", 322 "SSL_DHE_DSS_WITH_3DES_EDE_CBC_SHA", 323 "SSL_RSA_WITH_DES_CBC_SHA", 324 "SSL_DHE_RSA_WITH_DES_CBC_SHA", 325 "SSL_DHE_DSS_WITH_DES_CBC_SHA", 326 "SSL_RSA_EXPORT_WITH_RC4_40_MD5", 327 "SSL_RSA_EXPORT_WITH_DES40_CBC_SHA", 328 "SSL_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA", 329 "SSL_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA", 330 TLS_EMPTY_RENEGOTIATION_INFO_SCSV 331 }; 332 } 333 334 public static String[] getSupportedCipherSuites() { 335 return SUPPORTED_CIPHER_SUITES.clone(); 336 } 337 338 public static native void SSL_CTX_free(int ssl_ctx); 339 340 public static native int SSL_new(int ssl_ctx) throws SSLException; 341 342 public static byte[][] encodeCertificates(Certificate[] certificates) 343 throws CertificateEncodingException { 344 byte[][] certificateBytes = new byte[certificates.length][]; 345 for (int i = 0; i < certificates.length; i++) { 346 certificateBytes[i] = certificates[i].getEncoded(); 347 } 348 return certificateBytes; 349 } 350 351 public static native void SSL_use_certificate(int ssl, byte[][] asn1DerEncodedCertificateChain); 352 353 public static native void SSL_use_PrivateKey(int ssl, byte[] pkcs8EncodedPrivateKey); 354 355 public static native void SSL_check_private_key(int ssl) throws SSLException; 356 357 public static byte[][] encodeIssuerX509Principals(X509Certificate[] certificates) 358 throws CertificateEncodingException { 359 byte[][] principalBytes = new byte[certificates.length][]; 360 for (int i = 0; i < certificates.length; i++) { 361 principalBytes[i] = certificates[i].getIssuerX500Principal().getEncoded(); 362 } 363 return principalBytes; 364 } 365 366 public static native void SSL_set_client_CA_list(int ssl, byte[][] asn1DerEncodedX500Principals); 367 368 public static native long SSL_get_mode(int ssl); 369 370 public static native long SSL_set_mode(int ssl, long mode); 371 372 public static native long SSL_clear_mode(int ssl, long mode); 373 374 public static native long SSL_get_options(int ssl); 375 376 public static native long SSL_set_options(int ssl, long options); 377 378 public static native long SSL_clear_options(int ssl, long options); 379 380 public static String[] getSupportedProtocols() { 381 return new String[] { SUPPORTED_PROTOCOL_SSLV3, SUPPORTED_PROTOCOL_TLSV1 }; 382 } 383 384 public static void setEnabledProtocols(int ssl, String[] protocols) { 385 checkEnabledProtocols(protocols); 386 // openssl uses negative logic letting you disable protocols. 387 // so first, assume we need to set all (disable all) and clear none (enable none). 388 // in the loop, selectively move bits from set to clear (from disable to enable) 389 long optionsToSet = (SSL_OP_NO_SSLv3 | SSL_OP_NO_TLSv1); 390 long optionsToClear = 0; 391 for (int i = 0; i < protocols.length; i++) { 392 String protocol = protocols[i]; 393 if (protocol.equals(SUPPORTED_PROTOCOL_SSLV3)) { 394 optionsToSet &= ~SSL_OP_NO_SSLv3; 395 optionsToClear |= SSL_OP_NO_SSLv3; 396 } else if (protocol.equals(SUPPORTED_PROTOCOL_TLSV1)) { 397 optionsToSet &= ~SSL_OP_NO_TLSv1; 398 optionsToClear |= SSL_OP_NO_TLSv1; 399 } else { 400 // error checked by checkEnabledProtocols 401 throw new IllegalStateException(); 402 } 403 } 404 405 SSL_set_options(ssl, optionsToSet); 406 SSL_clear_options(ssl, optionsToClear); 407 } 408 409 public static String[] checkEnabledProtocols(String[] protocols) { 410 if (protocols == null) { 411 throw new IllegalArgumentException("protocols == null"); 412 } 413 for (int i = 0; i < protocols.length; i++) { 414 String protocol = protocols[i]; 415 if (protocol == null) { 416 throw new IllegalArgumentException("protocols[" + i + "] == null"); 417 } 418 if ((!protocol.equals(SUPPORTED_PROTOCOL_SSLV3)) 419 && (!protocol.equals(SUPPORTED_PROTOCOL_TLSV1))) { 420 throw new IllegalArgumentException("protocol " + protocol 421 + " is not supported"); 422 } 423 } 424 return protocols; 425 } 426 427 public static native void SSL_set_cipher_lists(int ssl, String[] ciphers); 428 429 public static void setEnabledCipherSuites(int ssl, String[] cipherSuites) { 430 checkEnabledCipherSuites(cipherSuites); 431 List<String> opensslSuites = new ArrayList<String>(); 432 for (int i = 0; i < cipherSuites.length; i++) { 433 String cipherSuite = cipherSuites[i]; 434 if (cipherSuite.equals(TLS_EMPTY_RENEGOTIATION_INFO_SCSV)) { 435 continue; 436 } 437 String openssl = STANDARD_TO_OPENSSL_CIPHER_SUITES.get(cipherSuite); 438 String cs = (openssl == null) ? cipherSuite : openssl; 439 opensslSuites.add(cs); 440 } 441 SSL_set_cipher_lists(ssl, opensslSuites.toArray(new String[opensslSuites.size()])); 442 } 443 444 public static String[] checkEnabledCipherSuites(String[] cipherSuites) { 445 if (cipherSuites == null) { 446 throw new IllegalArgumentException("cipherSuites == null"); 447 } 448 // makes sure all suites are valid, throwing on error 449 for (int i = 0; i < cipherSuites.length; i++) { 450 String cipherSuite = cipherSuites[i]; 451 if (cipherSuite == null) { 452 throw new IllegalArgumentException("cipherSuites[" + i + "] == null"); 453 } 454 if (cipherSuite.equals(TLS_EMPTY_RENEGOTIATION_INFO_SCSV)) { 455 continue; 456 } 457 if (STANDARD_TO_OPENSSL_CIPHER_SUITES.containsKey(cipherSuite)) { 458 continue; 459 } 460 if (OPENSSL_TO_STANDARD_CIPHER_SUITES.containsKey(cipherSuite)) { 461 // TODO log warning about using backward compatability 462 continue; 463 } 464 throw new IllegalArgumentException("cipherSuite " + cipherSuite + " is not supported."); 465 } 466 return cipherSuites; 467 } 468 469 private static final String SUPPORTED_COMPRESSION_METHOD_ZLIB = "ZLIB"; 470 private static final String SUPPORTED_COMPRESSION_METHOD_NULL = "NULL"; 471 472 private static final String[] SUPPORTED_COMPRESSION_METHODS 473 = { SUPPORTED_COMPRESSION_METHOD_ZLIB, SUPPORTED_COMPRESSION_METHOD_NULL }; 474 475 public static String[] getSupportedCompressionMethods() { 476 return SUPPORTED_COMPRESSION_METHODS.clone(); 477 } 478 479 public static final String[] getDefaultCompressionMethods() { 480 return new String[] { SUPPORTED_COMPRESSION_METHOD_NULL }; 481 } 482 483 public static String[] checkEnabledCompressionMethods(String[] methods) { 484 if (methods == null) { 485 throw new IllegalArgumentException("methods == null"); 486 } 487 if (methods.length < 1 488 && !methods[methods.length-1].equals(SUPPORTED_COMPRESSION_METHOD_NULL)) { 489 throw new IllegalArgumentException("last method must be NULL"); 490 } 491 for (int i = 0; i < methods.length; i++) { 492 String method = methods[i]; 493 if (method == null) { 494 throw new IllegalArgumentException("methods[" + i + "] == null"); 495 } 496 if (!method.equals(SUPPORTED_COMPRESSION_METHOD_ZLIB) 497 && !method.equals(SUPPORTED_COMPRESSION_METHOD_NULL)) { 498 throw new IllegalArgumentException("method " + method 499 + " is not supported"); 500 } 501 } 502 return methods; 503 } 504 505 public static void setEnabledCompressionMethods(int ssl, String[] methods) { 506 checkEnabledCompressionMethods(methods); 507 // openssl uses negative logic letting you disable compression. 508 // so first, assume we need to set all (disable all) and clear none (enable none). 509 // in the loop, selectively move bits from set to clear (from disable to enable) 510 long optionsToSet = (SSL_OP_NO_COMPRESSION); 511 long optionsToClear = 0; 512 for (int i = 0; i < methods.length; i++) { 513 String method = methods[i]; 514 if (method.equals(SUPPORTED_COMPRESSION_METHOD_NULL)) { 515 // nothing to do to support NULL 516 } else if (method.equals(SUPPORTED_COMPRESSION_METHOD_ZLIB)) { 517 optionsToSet &= ~SSL_OP_NO_COMPRESSION; 518 optionsToClear |= SSL_OP_NO_COMPRESSION; 519 } else { 520 // error checked by checkEnabledCompressionMethods 521 throw new IllegalStateException(); 522 } 523 } 524 525 SSL_set_options(ssl, optionsToSet); 526 SSL_clear_options(ssl, optionsToClear); 527 } 528 529 /* 530 * See the OpenSSL ssl.h header file for more information. 531 */ 532 public static final int SSL_VERIFY_NONE = 0x00; 533 public static final int SSL_VERIFY_PEER = 0x01; 534 public static final int SSL_VERIFY_FAIL_IF_NO_PEER_CERT = 0x02; 535 536 public static native void SSL_set_verify(int sslNativePointer, int mode); 537 538 public static native void SSL_set_session(int sslNativePointer, int sslSessionNativePointer) 539 throws SSLException; 540 541 public static native void SSL_set_session_creation_enabled( 542 int sslNativePointer, boolean creationEnabled) throws SSLException; 543 544 public static native void SSL_set_tlsext_host_name(int sslNativePointer, String hostname) 545 throws SSLException; 546 public static native String SSL_get_servername(int sslNativePointer); 547 548 /** 549 * Returns the sslSessionNativePointer of the negotiated session 550 */ 551 public static native int SSL_do_handshake(int sslNativePointer, 552 FileDescriptor fd, 553 SSLHandshakeCallbacks shc, 554 int timeout, 555 boolean client_mode) 556 throws SSLException, SocketTimeoutException, CertificateException; 557 558 /** 559 * Currently only intended for forcing renegotiation for testing. 560 * Not used within OpenSSLSocketImpl. 561 */ 562 public static native void SSL_renegotiate(int sslNativePointer) throws SSLException; 563 564 /** 565 * Returns the local ASN.1 DER encoded X509 certificates. 566 */ 567 public static native byte[][] SSL_get_certificate(int sslNativePointer); 568 569 /** 570 * Returns the peer ASN.1 DER encoded X509 certificates. 571 */ 572 public static native byte[][] SSL_get_peer_cert_chain(int sslNativePointer); 573 574 /** 575 * Reads with the native SSL_read function from the encrypted data stream 576 * @return -1 if error or the end of the stream is reached. 577 */ 578 public static native int SSL_read_byte(int sslNativePointer, 579 FileDescriptor fd, 580 SSLHandshakeCallbacks shc, 581 int timeout) throws IOException; 582 public static native int SSL_read(int sslNativePointer, 583 FileDescriptor fd, 584 SSLHandshakeCallbacks shc, 585 byte[] b, int off, int len, int timeout) 586 throws IOException; 587 588 /** 589 * Writes with the native SSL_write function to the encrypted data stream. 590 */ 591 public static native void SSL_write_byte(int sslNativePointer, 592 FileDescriptor fd, 593 SSLHandshakeCallbacks shc, 594 int b) throws IOException; 595 public static native void SSL_write(int sslNativePointer, 596 FileDescriptor fd, 597 SSLHandshakeCallbacks shc, 598 byte[] b, int off, int len) 599 throws IOException; 600 601 public static native void SSL_interrupt(int sslNativePointer) throws IOException; 602 public static native void SSL_shutdown(int sslNativePointer, 603 FileDescriptor fd, 604 SSLHandshakeCallbacks shc) throws IOException; 605 606 public static native void SSL_free(int sslNativePointer); 607 608 public static native byte[] SSL_SESSION_session_id(int sslSessionNativePointer); 609 610 public static native long SSL_SESSION_get_time(int sslSessionNativePointer); 611 612 public static native String SSL_SESSION_get_version(int sslSessionNativePointer); 613 614 public static native String SSL_SESSION_cipher(int sslSessionNativePointer); 615 616 public static native String SSL_SESSION_compress_meth(int sslCtxNativePointer, 617 int sslSessionNativePointer); 618 619 public static native void SSL_SESSION_free(int sslSessionNativePointer); 620 621 public static native byte[] i2d_SSL_SESSION(int sslSessionNativePointer); 622 623 public static native int d2i_SSL_SESSION(byte[] data); 624 625 /** 626 * A collection of callbacks from the native OpenSSL code that are 627 * related to the SSL handshake initiated by SSL_do_handshake. 628 */ 629 public interface SSLHandshakeCallbacks { 630 /** 631 * Verify that we trust the certificate chain is trusted. 632 * 633 * @param asn1DerEncodedCertificateChain A chain of ASN.1 DER encoded certficates 634 * @param authMethod auth algorithm name 635 * 636 * @throws CertificateException if the certificate is untrusted 637 */ 638 public void verifyCertificateChain(byte[][] asn1DerEncodedCertificateChain, String authMethod) 639 throws CertificateException; 640 641 /** 642 * Called on an SSL client when the server requests (or 643 * requires a certificate). The client can respond by using 644 * SSL_use_certificate and SSL_use_PrivateKey to set a 645 * certificate if has an appropriate one available, similar to 646 * how the server provides its certificate. 647 * 648 * @param keyTypes key types supported by the server, 649 * convertible to strings with #keyType 650 * @param asn1DerEncodedX500Principals CAs known to the server 651 */ 652 public void clientCertificateRequested(byte[] keyTypes, 653 byte[][] asn1DerEncodedX500Principals) 654 throws CertificateEncodingException, SSLException; 655 656 /** 657 * Called when SSL handshake is completed. Note that this can 658 * be after SSL_do_handshake returns when handshake cutthrough 659 * is enabled. 660 */ 661 public void handshakeCompleted(); 662 } 663} 664