The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 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 | |
| 17 | package org.apache.harmony.xnet.provider.jsse; |
| 18 | |
Brian Carlstrom | ecaf759 | 2010-03-02 16:55:35 -0800 | [diff] [blame] | 19 | import java.io.IOException; |
Brian Carlstrom | bcfb325 | 2010-05-02 11:27:52 -0700 | [diff] [blame] | 20 | import java.net.Socket; |
Brian Carlstrom | ecaf759 | 2010-03-02 16:55:35 -0800 | [diff] [blame] | 21 | import java.security.PrivateKey; |
Brian Carlstrom | bcfb325 | 2010-05-02 11:27:52 -0700 | [diff] [blame] | 22 | import java.security.cert.CertificateException; |
Brian Carlstrom | 3e24c53 | 2010-05-06 00:23:21 -0700 | [diff] [blame] | 23 | import java.security.interfaces.RSAPublicKey; |
Brian Carlstrom | bcfb325 | 2010-05-02 11:27:52 -0700 | [diff] [blame] | 24 | import java.util.ArrayList; |
Brian Carlstrom | 9acacc3 | 2010-05-14 11:14:18 -0700 | [diff] [blame] | 25 | import java.util.HashMap; |
| 26 | import java.util.LinkedHashMap; |
| 27 | import java.util.List; |
| 28 | import java.util.Map; |
Brian Carlstrom | ecaf759 | 2010-03-02 16:55:35 -0800 | [diff] [blame] | 29 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 30 | /** |
Brian Carlstrom | 12cd1f0 | 2010-06-22 23:43:20 -0700 | [diff] [blame^] | 31 | * Provides the Java side of our JNI glue for OpenSSL. |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 32 | */ |
Brian Carlstrom | 12cd1f0 | 2010-06-22 23:43:20 -0700 | [diff] [blame^] | 33 | public final class NativeCrypto { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 34 | |
Brian Carlstrom | ecaf759 | 2010-03-02 16:55:35 -0800 | [diff] [blame] | 35 | // --- OpenSSL library initialization -------------------------------------- |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 36 | static { |
Brian Carlstrom | ecaf759 | 2010-03-02 16:55:35 -0800 | [diff] [blame] | 37 | clinit(); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 38 | } |
| 39 | |
Brian Carlstrom | ecaf759 | 2010-03-02 16:55:35 -0800 | [diff] [blame] | 40 | private native static void clinit(); |
| 41 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 42 | // --- DSA/RSA public/private key handling functions ----------------------- |
Brian Carlstrom | ecaf759 | 2010-03-02 16:55:35 -0800 | [diff] [blame] | 43 | |
Brian Carlstrom | 6df6339 | 2010-05-18 11:33:13 -0700 | [diff] [blame] | 44 | public static native int EVP_PKEY_new_DSA(byte[] p, byte[] q, byte[] g, |
| 45 | byte[] priv_key, byte[] pub_key); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 46 | |
| 47 | public static native int EVP_PKEY_new_RSA(byte[] n, byte[] e, byte[] d, byte[] p, byte[] q); |
Brian Carlstrom | ecaf759 | 2010-03-02 16:55:35 -0800 | [diff] [blame] | 48 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 49 | public static native void EVP_PKEY_free(int pkey); |
Brian Carlstrom | ecaf759 | 2010-03-02 16:55:35 -0800 | [diff] [blame] | 50 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 51 | // --- General context handling functions (despite the names) -------------- |
Brian Carlstrom | ecaf759 | 2010-03-02 16:55:35 -0800 | [diff] [blame] | 52 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 53 | public static native int EVP_new(); |
Brian Carlstrom | ecaf759 | 2010-03-02 16:55:35 -0800 | [diff] [blame] | 54 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 55 | public static native void EVP_free(int ctx); |
Brian Carlstrom | ecaf759 | 2010-03-02 16:55:35 -0800 | [diff] [blame] | 56 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 57 | // --- Digest handling functions ------------------------------------------- |
Brian Carlstrom | ecaf759 | 2010-03-02 16:55:35 -0800 | [diff] [blame] | 58 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 59 | public static native void EVP_DigestInit(int ctx, String algorithm); |
Brian Carlstrom | ecaf759 | 2010-03-02 16:55:35 -0800 | [diff] [blame] | 60 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 61 | public static native void EVP_DigestUpdate(int ctx, byte[] buffer, int offset, int length); |
| 62 | |
| 63 | public static native int EVP_DigestFinal(int ctx, byte[] hash, int offset); |
| 64 | |
| 65 | public static native int EVP_DigestSize(int ctx); |
| 66 | |
| 67 | public static native int EVP_DigestBlockSize(int ctx); |
Brian Carlstrom | ecaf759 | 2010-03-02 16:55:35 -0800 | [diff] [blame] | 68 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 69 | // --- Signature handling functions ---------------------------------------- |
Brian Carlstrom | ecaf759 | 2010-03-02 16:55:35 -0800 | [diff] [blame] | 70 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 71 | public static native void EVP_VerifyInit(int ctx, String algorithm); |
Brian Carlstrom | ecaf759 | 2010-03-02 16:55:35 -0800 | [diff] [blame] | 72 | |
Brian Carlstrom | 6df6339 | 2010-05-18 11:33:13 -0700 | [diff] [blame] | 73 | public static native void EVP_VerifyUpdate(int ctx, byte[] buffer, |
| 74 | int offset, int length); |
Brian Carlstrom | ecaf759 | 2010-03-02 16:55:35 -0800 | [diff] [blame] | 75 | |
Brian Carlstrom | 6df6339 | 2010-05-18 11:33:13 -0700 | [diff] [blame] | 76 | public static native int EVP_VerifyFinal(int ctx, byte[] signature, |
| 77 | int offset, int length, int key); |
Brian Carlstrom | ecaf759 | 2010-03-02 16:55:35 -0800 | [diff] [blame] | 78 | |
Brian Carlstrom | 3e24c53 | 2010-05-06 00:23:21 -0700 | [diff] [blame] | 79 | // --- Legacy Signature handling ------------------------------------------- |
| 80 | // TODO rewrite/replace with EVP_Verify* |
| 81 | /** |
| 82 | * Verifies an RSA signature. Conceptually, this method doesn't really |
| 83 | * belong here, but due to its native code being closely tied to OpenSSL |
| 84 | * (just like the rest of this class), we put it here for the time being. |
| 85 | * This also solves potential problems with native library initialization. |
| 86 | * |
| 87 | * @param message The message to verify |
| 88 | * @param signature The signature to verify |
| 89 | * @param algorithm The hash/sign algorithm to use, i.e. "RSA-SHA1" |
| 90 | * @param key The RSA public key to use |
| 91 | * @return true if the verification succeeds, false otherwise |
| 92 | */ |
Brian Carlstrom | 6df6339 | 2010-05-18 11:33:13 -0700 | [diff] [blame] | 93 | public static boolean verifySignature( |
| 94 | byte[] message, byte[] signature, String algorithm, RSAPublicKey key) { |
Brian Carlstrom | 3e24c53 | 2010-05-06 00:23:21 -0700 | [diff] [blame] | 95 | byte[] modulus = key.getModulus().toByteArray(); |
| 96 | byte[] exponent = key.getPublicExponent().toByteArray(); |
| 97 | |
| 98 | return verifySignature(message, signature, algorithm, modulus, exponent) == 1; |
| 99 | } |
| 100 | |
| 101 | private static native int verifySignature(byte[] message, byte[] signature, |
| 102 | String algorithm, byte[] modulus, byte[] exponent); |
| 103 | |
Brian Carlstrom | 6df6339 | 2010-05-18 11:33:13 -0700 | [diff] [blame] | 104 | // --- RAND seeding -------------------------------------------------------- |
| 105 | |
| 106 | public static final int RAND_SEED_LENGTH_IN_BYTES = 1024; |
| 107 | |
| 108 | public static native void RAND_seed(byte[] seed); |
| 109 | |
| 110 | public static native int RAND_load_file(String filename, long max_bytes); |
| 111 | |
Brian Carlstrom | ecaf759 | 2010-03-02 16:55:35 -0800 | [diff] [blame] | 112 | // --- SSL handling -------------------------------------------------------- |
| 113 | |
| 114 | private static final String SUPPORTED_PROTOCOL_SSLV3 = "SSLv3"; |
| 115 | private static final String SUPPORTED_PROTOCOL_TLSV1 = "TLSv1"; |
| 116 | |
Brian Carlstrom | 6df6339 | 2010-05-18 11:33:13 -0700 | [diff] [blame] | 117 | public static final Map<String, String> OPENSSL_TO_STANDARD |
| 118 | = new HashMap<String, String>(); |
| 119 | public static final Map<String, String> STANDARD_TO_OPENSSL |
| 120 | = new LinkedHashMap<String, String>(); |
Brian Carlstrom | 9acacc3 | 2010-05-14 11:14:18 -0700 | [diff] [blame] | 121 | |
| 122 | private static void add(String standard, String openssl) { |
| 123 | OPENSSL_TO_STANDARD.put(openssl, standard); |
| 124 | STANDARD_TO_OPENSSL.put(standard, openssl); |
| 125 | } |
| 126 | |
| 127 | static { |
| 128 | // Note these are added in priority order |
Brian Carlstrom | aacf6f9 | 2010-05-20 01:02:17 -0700 | [diff] [blame] | 129 | // Android doesn't currently support Elliptic Curve |
Brian Carlstrom | 9acacc3 | 2010-05-14 11:14:18 -0700 | [diff] [blame] | 130 | add("SSL_RSA_WITH_RC4_128_MD5", "RC4-MD5"); |
| 131 | add("SSL_RSA_WITH_RC4_128_SHA", "RC4-SHA"); |
| 132 | add("TLS_RSA_WITH_AES_128_CBC_SHA", "AES128-SHA"); |
| 133 | add("TLS_RSA_WITH_AES_256_CBC_SHA", "AES256-SHA"); |
Brian Carlstrom | 204cab3 | 2010-05-17 15:23:45 -0700 | [diff] [blame] | 134 | // add("TLS_ECDH_ECDSA_WITH_RC4_128_SHA", "ECDH-ECDSA-RC4-SHA"); |
| 135 | // add("TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA", "ECDH-ECDSA-AES128-SHA"); |
| 136 | // add("TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA", "ECDH-ECDSA-AES256-SHA"); |
| 137 | // add("TLS_ECDH_RSA_WITH_RC4_128_SHA", "ECDH-RSA-RC4-SHA"); |
| 138 | // add("TLS_ECDH_RSA_WITH_AES_128_CBC_SHA", "ECDH-RSA-AES128-SHA"); |
| 139 | // add("TLS_ECDH_RSA_WITH_AES_256_CBC_SHA", "ECDH-RSA-AES256-SHA"); |
| 140 | // add("TLS_ECDHE_ECDSA_WITH_RC4_128_SHA", "ECDHE-ECDSA-RC4-SHA"); |
| 141 | // add("TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA", "ECDHE-ECDSA-AES128-SHA"); |
| 142 | // add("TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA", "ECDHE-ECDSA-AES256-SHA"); |
| 143 | // add("TLS_ECDHE_RSA_WITH_RC4_128_SHA", "ECDHE-RSA-RC4-SHA"); |
| 144 | // add("TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA", "ECDHE-RSA-AES128-SHA"); |
| 145 | // add("TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA", "ECDHE-RSA-AES256-SHA"); |
Brian Carlstrom | aacf6f9 | 2010-05-20 01:02:17 -0700 | [diff] [blame] | 146 | add("TLS_DHE_RSA_WITH_AES_128_CBC_SHA", "DHE-RSA-AES128-SHA"); |
| 147 | add("TLS_DHE_RSA_WITH_AES_256_CBC_SHA", "DHE-RSA-AES256-SHA"); |
| 148 | add("TLS_DHE_DSS_WITH_AES_128_CBC_SHA", "DHE-DSS-AES128-SHA"); |
| 149 | add("TLS_DHE_DSS_WITH_AES_256_CBC_SHA", "DHE-DSS-AES256-SHA"); |
Brian Carlstrom | 9acacc3 | 2010-05-14 11:14:18 -0700 | [diff] [blame] | 150 | add("SSL_RSA_WITH_3DES_EDE_CBC_SHA", "DES-CBC3-SHA"); |
Brian Carlstrom | 204cab3 | 2010-05-17 15:23:45 -0700 | [diff] [blame] | 151 | // add("TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA", "ECDH-ECDSA-DES-CBC3-SHA"); |
| 152 | // add("TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA", "ECDH-RSA-DES-CBC3-SHA"); |
| 153 | // add("TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA", "ECDHE-ECDSA-DES-CBC3-SHA"); |
| 154 | // add("TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA", "ECDHE-RSA-DES-CBC3-SHA"); |
Brian Carlstrom | aacf6f9 | 2010-05-20 01:02:17 -0700 | [diff] [blame] | 155 | add("SSL_DHE_RSA_WITH_3DES_EDE_CBC_SHA", "EDH-RSA-DES-CBC3-SHA"); |
| 156 | add("SSL_DHE_DSS_WITH_3DES_EDE_CBC_SHA", "EDH-DSS-DES-CBC3-SHA"); |
Brian Carlstrom | 9acacc3 | 2010-05-14 11:14:18 -0700 | [diff] [blame] | 157 | add("SSL_RSA_WITH_DES_CBC_SHA", "DES-CBC-SHA"); |
Brian Carlstrom | aacf6f9 | 2010-05-20 01:02:17 -0700 | [diff] [blame] | 158 | add("SSL_DHE_RSA_WITH_DES_CBC_SHA", "EDH-RSA-DES-CBC-SHA"); |
| 159 | add("SSL_DHE_DSS_WITH_DES_CBC_SHA", "EDH-DSS-DES-CBC-SHA"); |
Brian Carlstrom | 9acacc3 | 2010-05-14 11:14:18 -0700 | [diff] [blame] | 160 | add("SSL_RSA_EXPORT_WITH_RC4_40_MD5", "EXP-RC4-MD5"); |
| 161 | add("SSL_RSA_EXPORT_WITH_DES40_CBC_SHA", "EXP-DES-CBC-SHA"); |
Brian Carlstrom | aacf6f9 | 2010-05-20 01:02:17 -0700 | [diff] [blame] | 162 | add("SSL_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA", "EXP-EDH-RSA-DES-CBC-SHA"); |
| 163 | add("SSL_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA", "EXP-EDH-DSS-DES-CBC-SHA"); |
Brian Carlstrom | 9acacc3 | 2010-05-14 11:14:18 -0700 | [diff] [blame] | 164 | add("SSL_RSA_WITH_NULL_MD5", "NULL-MD5"); |
| 165 | add("SSL_RSA_WITH_NULL_SHA", "NULL-SHA"); |
Brian Carlstrom | 204cab3 | 2010-05-17 15:23:45 -0700 | [diff] [blame] | 166 | // add("TLS_ECDH_ECDSA_WITH_NULL_SHA", "ECDH-ECDSA-NULL-SHA"); |
| 167 | // add("TLS_ECDH_RSA_WITH_NULL_SHA", "ECDH-RSA-NULL-SHA"); |
| 168 | // add("TLS_ECDHE_ECDSA_WITH_NULL_SHA", "ECDHE-ECDSA-NULL-SHA"); |
| 169 | // add("TLS_ECDHE_RSA_WITH_NULL_SHA", "ECDHE-RSA-NULL-SHA"); |
Brian Carlstrom | aacf6f9 | 2010-05-20 01:02:17 -0700 | [diff] [blame] | 170 | add("SSL_DH_anon_WITH_RC4_128_MD5", "ADH-RC4-MD5"); |
| 171 | add("TLS_DH_anon_WITH_AES_128_CBC_SHA", "ADH-AES128-SHA"); |
| 172 | add("TLS_DH_anon_WITH_AES_256_CBC_SHA", "ADH-AES256-SHA"); |
| 173 | add("SSL_DH_anon_WITH_3DES_EDE_CBC_SHA", "ADH-DES-CBC3-SHA"); |
| 174 | add("SSL_DH_anon_WITH_DES_CBC_SHA", "ADH-DES-CBC-SHA"); |
Brian Carlstrom | 204cab3 | 2010-05-17 15:23:45 -0700 | [diff] [blame] | 175 | // add("TLS_ECDH_anon_WITH_RC4_128_SHA", "AECDH-RC4-SHA"); |
| 176 | // add("TLS_ECDH_anon_WITH_AES_128_CBC_SHA", "AECDH-AES128-SHA"); |
| 177 | // add("TLS_ECDH_anon_WITH_AES_256_CBC_SHA", "AECDH-AES256-SHA"); |
| 178 | // add("TLS_ECDH_anon_WITH_3DES_EDE_CBC_SHA", "AECDH-DES-CBC3-SHA"); |
Brian Carlstrom | aacf6f9 | 2010-05-20 01:02:17 -0700 | [diff] [blame] | 179 | add("SSL_DH_anon_EXPORT_WITH_RC4_40_MD5", "EXP-ADH-RC4-MD5"); |
| 180 | add("SSL_DH_anon_EXPORT_WITH_DES40_CBC_SHA", "EXP-ADH-DES-CBC-SHA"); |
Brian Carlstrom | 204cab3 | 2010-05-17 15:23:45 -0700 | [diff] [blame] | 181 | // add("TLS_ECDH_anon_WITH_NULL_SHA", "AECDH-NULL-SHA"); |
| 182 | |
Brian Carlstrom | 9acacc3 | 2010-05-14 11:14:18 -0700 | [diff] [blame] | 183 | // No Kerberos in Android |
Brian Carlstrom | 204cab3 | 2010-05-17 15:23:45 -0700 | [diff] [blame] | 184 | // add("TLS_KRB5_WITH_RC4_128_SHA", "KRB5-RC4-SHA"); |
| 185 | // add("TLS_KRB5_WITH_RC4_128_MD5", "KRB5-RC4-MD5"); |
| 186 | // add("TLS_KRB5_WITH_3DES_EDE_CBC_SHA", "KRB5-DES-CBC3-SHA"); |
| 187 | // add("TLS_KRB5_WITH_3DES_EDE_CBC_MD5", "KRB5-DES-CBC3-MD5"); |
| 188 | // add("TLS_KRB5_WITH_DES_CBC_SHA", "KRB5-DES-CBC-SHA"); |
| 189 | // add("TLS_KRB5_WITH_DES_CBC_MD5", "KRB5-DES-CBC-MD5"); |
| 190 | // add("TLS_KRB5_EXPORT_WITH_RC4_40_SHA", "EXP-KRB5-RC4-SHA"); |
| 191 | // add("TLS_KRB5_EXPORT_WITH_RC4_40_MD5", "EXP-KRB5-RC4-MD5"); |
| 192 | // add("TLS_KRB5_EXPORT_WITH_DES_CBC_40_SHA", "EXP-KRB5-DES-CBC-SHA"); |
| 193 | // add("TLS_KRB5_EXPORT_WITH_DES_CBC_40_MD5", "EXP-KRB5-DES-CBC-MD5"); |
Brian Carlstrom | 9acacc3 | 2010-05-14 11:14:18 -0700 | [diff] [blame] | 194 | |
| 195 | // not implemented by either RI or OpenSSL |
Brian Carlstrom | 204cab3 | 2010-05-17 15:23:45 -0700 | [diff] [blame] | 196 | // add("SSL_DH_DSS_EXPORT_WITH_DES40_CBC_SHA", null); |
| 197 | // add("SSL_DH_RSA_EXPORT_WITH_DES40_CBC_SHA", null); |
Brian Carlstrom | 9acacc3 | 2010-05-14 11:14:18 -0700 | [diff] [blame] | 198 | |
| 199 | // EXPORT1024 suites were never standardized but were widely implemented. |
| 200 | // OpenSSL 0.9.8c and later have disabled TLS1_ALLOW_EXPERIMENTAL_CIPHERSUITES |
Brian Carlstrom | 204cab3 | 2010-05-17 15:23:45 -0700 | [diff] [blame] | 201 | // add("SSL_RSA_EXPORT1024_WITH_DES_CBC_SHA", "EXP1024-DES-CBC-SHA"); |
| 202 | // add("SSL_RSA_EXPORT1024_WITH_RC4_56_SHA", "EXP1024-RC4-SHA"); |
Brian Carlstrom | 9acacc3 | 2010-05-14 11:14:18 -0700 | [diff] [blame] | 203 | |
| 204 | // No RC2 |
Brian Carlstrom | 204cab3 | 2010-05-17 15:23:45 -0700 | [diff] [blame] | 205 | // add("SSL_RSA_EXPORT_WITH_RC2_CBC_40_MD5", "EXP-RC2-CBC-MD5"); |
| 206 | // add("TLS_KRB5_EXPORT_WITH_RC2_CBC_40_SHA", "EXP-KRB5-RC2-CBC-SHA"); |
| 207 | // add("TLS_KRB5_EXPORT_WITH_RC2_CBC_40_MD5", "EXP-KRB5-RC2-CBC-MD5"); |
Brian Carlstrom | 9acacc3 | 2010-05-14 11:14:18 -0700 | [diff] [blame] | 208 | |
| 209 | // PSK is Private Shared Key - didn't exist in Froyo's openssl - no JSSE equivalent |
Brian Carlstrom | 204cab3 | 2010-05-17 15:23:45 -0700 | [diff] [blame] | 210 | // add(null, "PSK-3DES-EDE-CBC-SHA"); |
| 211 | // add(null, "PSK-AES128-CBC-SHA"); |
| 212 | // add(null, "PSK-AES256-CBC-SHA"); |
| 213 | // add(null, "PSK-RC4-SHA"); |
Brian Carlstrom | 9acacc3 | 2010-05-14 11:14:18 -0700 | [diff] [blame] | 214 | |
| 215 | } |
| 216 | |
| 217 | private static final String[] SUPPORTED_CIPHER_SUITES |
| 218 | = STANDARD_TO_OPENSSL.keySet().toArray(new String[0]); |
| 219 | |
Brian Carlstrom | bcfb325 | 2010-05-02 11:27:52 -0700 | [diff] [blame] | 220 | // SSL mode |
| 221 | public static long SSL_MODE_HANDSHAKE_CUTTHROUGH = 0x00000040L; |
| 222 | |
| 223 | // SSL options |
Brian Carlstrom | ecaf759 | 2010-03-02 16:55:35 -0800 | [diff] [blame] | 224 | public static long SSL_OP_NO_SSLv3 = 0x02000000L; |
| 225 | public static long SSL_OP_NO_TLSv1 = 0x04000000L; |
| 226 | |
| 227 | public static native int SSL_CTX_new(); |
| 228 | |
Brian Carlstrom | ecaf759 | 2010-03-02 16:55:35 -0800 | [diff] [blame] | 229 | public static String[] getDefaultCipherSuites() { |
Brian Carlstrom | 9acacc3 | 2010-05-14 11:14:18 -0700 | [diff] [blame] | 230 | return new String[] { |
| 231 | "SSL_RSA_WITH_RC4_128_MD5", |
| 232 | "SSL_RSA_WITH_RC4_128_SHA", |
| 233 | "TLS_RSA_WITH_AES_128_CBC_SHA", |
Brian Carlstrom | aacf6f9 | 2010-05-20 01:02:17 -0700 | [diff] [blame] | 234 | "TLS_DHE_RSA_WITH_AES_128_CBC_SHA", |
| 235 | "TLS_DHE_DSS_WITH_AES_128_CBC_SHA", |
Brian Carlstrom | 9acacc3 | 2010-05-14 11:14:18 -0700 | [diff] [blame] | 236 | "SSL_RSA_WITH_3DES_EDE_CBC_SHA", |
Brian Carlstrom | aacf6f9 | 2010-05-20 01:02:17 -0700 | [diff] [blame] | 237 | "SSL_DHE_RSA_WITH_3DES_EDE_CBC_SHA", |
| 238 | "SSL_DHE_DSS_WITH_3DES_EDE_CBC_SHA", |
Brian Carlstrom | 9acacc3 | 2010-05-14 11:14:18 -0700 | [diff] [blame] | 239 | "SSL_RSA_WITH_DES_CBC_SHA", |
Brian Carlstrom | aacf6f9 | 2010-05-20 01:02:17 -0700 | [diff] [blame] | 240 | "SSL_DHE_RSA_WITH_DES_CBC_SHA", |
| 241 | "SSL_DHE_DSS_WITH_DES_CBC_SHA", |
Brian Carlstrom | 9acacc3 | 2010-05-14 11:14:18 -0700 | [diff] [blame] | 242 | "SSL_RSA_EXPORT_WITH_RC4_40_MD5", |
| 243 | "SSL_RSA_EXPORT_WITH_DES40_CBC_SHA", |
Brian Carlstrom | aacf6f9 | 2010-05-20 01:02:17 -0700 | [diff] [blame] | 244 | "SSL_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA", |
| 245 | "SSL_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA" |
Brian Carlstrom | 9acacc3 | 2010-05-14 11:14:18 -0700 | [diff] [blame] | 246 | }; |
Brian Carlstrom | ecaf759 | 2010-03-02 16:55:35 -0800 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | public static String[] getSupportedCipherSuites() { |
Brian Carlstrom | 9acacc3 | 2010-05-14 11:14:18 -0700 | [diff] [blame] | 250 | return SUPPORTED_CIPHER_SUITES.clone(); |
Brian Carlstrom | ecaf759 | 2010-03-02 16:55:35 -0800 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | public static native void SSL_CTX_free(int ssl_ctx); |
| 254 | |
Brian Carlstrom | 6df6339 | 2010-05-18 11:33:13 -0700 | [diff] [blame] | 255 | public static native int SSL_new(int ssl_ctx) throws IOException; |
Brian Carlstrom | ecaf759 | 2010-03-02 16:55:35 -0800 | [diff] [blame] | 256 | |
Brian Carlstrom | 0c131a2 | 2010-05-20 15:27:31 -0700 | [diff] [blame] | 257 | public static final String[] KEY_TYPES = new String[] { "RSA", "DSA", "DH_RSA" , "DH_DSA" }; |
Brian Carlstrom | ecaf759 | 2010-03-02 16:55:35 -0800 | [diff] [blame] | 258 | |
Brian Carlstrom | 12cd1f0 | 2010-06-22 23:43:20 -0700 | [diff] [blame^] | 259 | public static native void SSL_use_certificate(int ssl, byte[][] asn1DerEncodedCertificate); |
Brian Carlstrom | ecaf759 | 2010-03-02 16:55:35 -0800 | [diff] [blame] | 260 | |
Brian Carlstrom | 12cd1f0 | 2010-06-22 23:43:20 -0700 | [diff] [blame^] | 261 | public static native void SSL_use_PrivateKey(int ssl, byte[] pkcs8EncodedPrivateKey); |
Brian Carlstrom | ecaf759 | 2010-03-02 16:55:35 -0800 | [diff] [blame] | 262 | |
Brian Carlstrom | 6df6339 | 2010-05-18 11:33:13 -0700 | [diff] [blame] | 263 | public static native void SSL_check_private_key(int ssl); |
Brian Carlstrom | ecaf759 | 2010-03-02 16:55:35 -0800 | [diff] [blame] | 264 | |
Brian Carlstrom | bcfb325 | 2010-05-02 11:27:52 -0700 | [diff] [blame] | 265 | public static native long SSL_get_mode(int ssl); |
| 266 | |
Brian Carlstrom | e688a41 | 2010-05-04 15:40:51 -0700 | [diff] [blame] | 267 | public static native long SSL_set_mode(int ssl, long mode); |
Brian Carlstrom | bcfb325 | 2010-05-02 11:27:52 -0700 | [diff] [blame] | 268 | |
Brian Carlstrom | e688a41 | 2010-05-04 15:40:51 -0700 | [diff] [blame] | 269 | public static native long SSL_clear_mode(int ssl, long mode); |
Brian Carlstrom | bcfb325 | 2010-05-02 11:27:52 -0700 | [diff] [blame] | 270 | |
Brian Carlstrom | ecaf759 | 2010-03-02 16:55:35 -0800 | [diff] [blame] | 271 | public static native long SSL_get_options(int ssl); |
| 272 | |
| 273 | public static native long SSL_set_options(int ssl, long options); |
| 274 | |
Brian Carlstrom | bcfb325 | 2010-05-02 11:27:52 -0700 | [diff] [blame] | 275 | public static native long SSL_clear_options(int ssl, long options); |
| 276 | |
Brian Carlstrom | ecaf759 | 2010-03-02 16:55:35 -0800 | [diff] [blame] | 277 | public static String[] getSupportedProtocols() { |
| 278 | return new String[] { SUPPORTED_PROTOCOL_SSLV3, SUPPORTED_PROTOCOL_TLSV1 }; |
| 279 | } |
| 280 | |
Brian Carlstrom | ecaf759 | 2010-03-02 16:55:35 -0800 | [diff] [blame] | 281 | public static void setEnabledProtocols(int ssl, String[] protocols) { |
| 282 | if (protocols == null) { |
Brian Carlstrom | bcfb325 | 2010-05-02 11:27:52 -0700 | [diff] [blame] | 283 | throw new IllegalArgumentException("protocols == null"); |
Brian Carlstrom | ecaf759 | 2010-03-02 16:55:35 -0800 | [diff] [blame] | 284 | } |
Brian Carlstrom | bcfb325 | 2010-05-02 11:27:52 -0700 | [diff] [blame] | 285 | |
Brian Carlstrom | ecaf759 | 2010-03-02 16:55:35 -0800 | [diff] [blame] | 286 | // openssl uses negative logic letting you disable protocols. |
Brian Carlstrom | 0c131a2 | 2010-05-20 15:27:31 -0700 | [diff] [blame] | 287 | // so first, assume we need to set all (disable all) and clear none (enable none). |
Brian Carlstrom | bcfb325 | 2010-05-02 11:27:52 -0700 | [diff] [blame] | 288 | // in the loop, selectively move bits from set to clear (from disable to enable) |
| 289 | long optionsToSet = (SSL_OP_NO_SSLv3 | SSL_OP_NO_TLSv1); |
| 290 | long optionsToClear = 0; |
Brian Carlstrom | ecaf759 | 2010-03-02 16:55:35 -0800 | [diff] [blame] | 291 | for (int i = 0; i < protocols.length; i++) { |
Brian Carlstrom | bcfb325 | 2010-05-02 11:27:52 -0700 | [diff] [blame] | 292 | String protocol = protocols[i]; |
| 293 | if (protocol == null) { |
| 294 | throw new IllegalArgumentException("protocols[" + i + "] == null"); |
| 295 | } |
| 296 | if (protocol.equals(SUPPORTED_PROTOCOL_SSLV3)) { |
| 297 | optionsToSet &= ~SSL_OP_NO_SSLv3; |
| 298 | optionsToClear |= SSL_OP_NO_SSLv3; |
| 299 | } else if (protocol.equals(SUPPORTED_PROTOCOL_TLSV1)) { |
| 300 | optionsToSet &= ~SSL_OP_NO_TLSv1; |
| 301 | optionsToClear |= SSL_OP_NO_TLSv1; |
Brian Carlstrom | ecaf759 | 2010-03-02 16:55:35 -0800 | [diff] [blame] | 302 | } else { |
Brian Carlstrom | 12cd1f0 | 2010-06-22 23:43:20 -0700 | [diff] [blame^] | 303 | throw new IllegalArgumentException("protocol " + protocol |
| 304 | + " is not supported"); |
Brian Carlstrom | ecaf759 | 2010-03-02 16:55:35 -0800 | [diff] [blame] | 305 | } |
| 306 | } |
Brian Carlstrom | bcfb325 | 2010-05-02 11:27:52 -0700 | [diff] [blame] | 307 | |
| 308 | SSL_set_options(ssl, optionsToSet); |
| 309 | SSL_clear_options(ssl, optionsToClear); |
| 310 | } |
| 311 | |
| 312 | public static String[] checkEnabledProtocols(String[] protocols) { |
| 313 | if (protocols == null) { |
Brian Carlstrom | 0c131a2 | 2010-05-20 15:27:31 -0700 | [diff] [blame] | 314 | throw new IllegalArgumentException("protocols == null"); |
Brian Carlstrom | bcfb325 | 2010-05-02 11:27:52 -0700 | [diff] [blame] | 315 | } |
| 316 | for (int i = 0; i < protocols.length; i++) { |
| 317 | String protocol = protocols[i]; |
| 318 | if (protocol == null) { |
| 319 | throw new IllegalArgumentException("protocols[" + i + "] == null"); |
| 320 | } |
| 321 | if ((!protocol.equals(SUPPORTED_PROTOCOL_SSLV3)) |
Brian Carlstrom | 0c131a2 | 2010-05-20 15:27:31 -0700 | [diff] [blame] | 322 | && (!protocol.equals(SUPPORTED_PROTOCOL_TLSV1))) { |
Brian Carlstrom | 12cd1f0 | 2010-06-22 23:43:20 -0700 | [diff] [blame^] | 323 | throw new IllegalArgumentException("protocol " + protocol |
| 324 | + " is not supported"); |
Brian Carlstrom | bcfb325 | 2010-05-02 11:27:52 -0700 | [diff] [blame] | 325 | } |
| 326 | } |
| 327 | return protocols; |
Brian Carlstrom | ecaf759 | 2010-03-02 16:55:35 -0800 | [diff] [blame] | 328 | } |
| 329 | |
Brian Carlstrom | 9acacc3 | 2010-05-14 11:14:18 -0700 | [diff] [blame] | 330 | public static native void SSL_set_cipher_lists(int ssl, String[] ciphers); |
Brian Carlstrom | ecaf759 | 2010-03-02 16:55:35 -0800 | [diff] [blame] | 331 | |
Brian Carlstrom | bcfb325 | 2010-05-02 11:27:52 -0700 | [diff] [blame] | 332 | public static void setEnabledCipherSuites(int ssl, String[] cipherSuites) { |
| 333 | checkEnabledCipherSuites(cipherSuites); |
Brian Carlstrom | 9acacc3 | 2010-05-14 11:14:18 -0700 | [diff] [blame] | 334 | List<String> opensslSuites = new ArrayList<String>(); |
Brian Carlstrom | bcfb325 | 2010-05-02 11:27:52 -0700 | [diff] [blame] | 335 | for (int i = 0; i < cipherSuites.length; i++) { |
| 336 | String cipherSuite = cipherSuites[i]; |
Brian Carlstrom | 9acacc3 | 2010-05-14 11:14:18 -0700 | [diff] [blame] | 337 | String openssl = STANDARD_TO_OPENSSL.get(cipherSuite); |
| 338 | String cs = (openssl == null) ? cipherSuite : openssl; |
| 339 | opensslSuites.add(cs); |
Brian Carlstrom | ecaf759 | 2010-03-02 16:55:35 -0800 | [diff] [blame] | 340 | } |
Brian Carlstrom | 9acacc3 | 2010-05-14 11:14:18 -0700 | [diff] [blame] | 341 | SSL_set_cipher_lists(ssl, opensslSuites.toArray(new String[opensslSuites.size()])); |
Brian Carlstrom | ecaf759 | 2010-03-02 16:55:35 -0800 | [diff] [blame] | 342 | } |
| 343 | |
Brian Carlstrom | bcfb325 | 2010-05-02 11:27:52 -0700 | [diff] [blame] | 344 | public static String[] checkEnabledCipherSuites(String[] cipherSuites) { |
| 345 | if (cipherSuites == null) { |
| 346 | throw new IllegalArgumentException("cipherSuites == null"); |
| 347 | } |
| 348 | // makes sure all suites are valid, throwing on error |
Brian Carlstrom | bcfb325 | 2010-05-02 11:27:52 -0700 | [diff] [blame] | 349 | for (int i = 0; i < cipherSuites.length; i++) { |
| 350 | String cipherSuite = cipherSuites[i]; |
| 351 | if (cipherSuite == null) { |
| 352 | throw new IllegalArgumentException("cipherSuites[" + i + "] == null"); |
| 353 | } |
Brian Carlstrom | 9acacc3 | 2010-05-14 11:14:18 -0700 | [diff] [blame] | 354 | if (STANDARD_TO_OPENSSL.containsKey(cipherSuite)) { |
| 355 | continue; |
| 356 | } |
| 357 | if (OPENSSL_TO_STANDARD.containsKey(cipherSuite)) { |
| 358 | // TODO log warning about using backward compatability |
| 359 | continue; |
| 360 | } |
| 361 | throw new IllegalArgumentException("cipherSuite " + cipherSuite + " is not supported."); |
Brian Carlstrom | bcfb325 | 2010-05-02 11:27:52 -0700 | [diff] [blame] | 362 | } |
| 363 | return cipherSuites; |
| 364 | } |
| 365 | |
Brian Carlstrom | bcfb325 | 2010-05-02 11:27:52 -0700 | [diff] [blame] | 366 | /* |
| 367 | * See the OpenSSL ssl.h header file for more information. |
| 368 | */ |
| 369 | public static final int SSL_VERIFY_NONE = 0x00; |
| 370 | public static final int SSL_VERIFY_PEER = 0x01; |
| 371 | public static final int SSL_VERIFY_FAIL_IF_NO_PEER_CERT = 0x02; |
| 372 | public static final int SSL_VERIFY_CLIENT_ONCE = 0x04; |
| 373 | |
| 374 | public static native void SSL_set_verify(int sslNativePointer, int mode) throws IOException; |
| 375 | |
Brian Carlstrom | 6df6339 | 2010-05-18 11:33:13 -0700 | [diff] [blame] | 376 | public static native void SSL_set_session(int sslNativePointer, int sslSessionNativePointer) |
| 377 | throws IOException; |
Brian Carlstrom | bcfb325 | 2010-05-02 11:27:52 -0700 | [diff] [blame] | 378 | |
Brian Carlstrom | 6df6339 | 2010-05-18 11:33:13 -0700 | [diff] [blame] | 379 | public static native void SSL_set_session_creation_enabled( |
| 380 | int sslNativePointer, boolean creationEnabled) throws IOException; |
Brian Carlstrom | bcfb325 | 2010-05-02 11:27:52 -0700 | [diff] [blame] | 381 | |
| 382 | /** |
| 383 | * Returns the sslSessionNativePointer of the negotiated session |
| 384 | */ |
Brian Carlstrom | 6df6339 | 2010-05-18 11:33:13 -0700 | [diff] [blame] | 385 | public static native int SSL_do_handshake(int sslNativePointer, |
| 386 | Socket sock, |
| 387 | SSLHandshakeCallbacks shc, |
| 388 | int timeout, |
| 389 | boolean client_mode) |
| 390 | throws IOException, CertificateException; |
Brian Carlstrom | bcfb325 | 2010-05-02 11:27:52 -0700 | [diff] [blame] | 391 | |
| 392 | public static native byte[][] SSL_get_certificate(int sslNativePointer); |
| 393 | |
Brian Carlstrom | 3e24c53 | 2010-05-06 00:23:21 -0700 | [diff] [blame] | 394 | /** |
| 395 | * Reads with the native SSL_read function from the encrypted data stream |
| 396 | * @return -1 if error or the end of the stream is reached. |
| 397 | */ |
| 398 | public static native int SSL_read_byte(int sslNativePointer, int timeout) throws IOException; |
Brian Carlstrom | 6df6339 | 2010-05-18 11:33:13 -0700 | [diff] [blame] | 399 | public static native int SSL_read(int sslNativePointer, byte[] b, int off, int len, int timeout) |
| 400 | throws IOException; |
Brian Carlstrom | 3e24c53 | 2010-05-06 00:23:21 -0700 | [diff] [blame] | 401 | |
| 402 | /** |
| 403 | * Writes with the native SSL_write function to the encrypted data stream. |
| 404 | */ |
| 405 | public static native void SSL_write_byte(int sslNativePointer, int b) throws IOException; |
Brian Carlstrom | 6df6339 | 2010-05-18 11:33:13 -0700 | [diff] [blame] | 406 | public static native void SSL_write(int sslNativePointer, byte[] b, int off, int len) |
| 407 | throws IOException; |
Brian Carlstrom | 3e24c53 | 2010-05-06 00:23:21 -0700 | [diff] [blame] | 408 | |
| 409 | public static native void SSL_interrupt(int sslNativePointer) throws IOException; |
| 410 | public static native void SSL_shutdown(int sslNativePointer) throws IOException; |
| 411 | |
Brian Carlstrom | bcfb325 | 2010-05-02 11:27:52 -0700 | [diff] [blame] | 412 | public static native void SSL_free(int sslNativePointer); |
Brian Carlstrom | ecaf759 | 2010-03-02 16:55:35 -0800 | [diff] [blame] | 413 | |
Brian Carlstrom | f002bdd | 2010-05-05 13:23:14 -0700 | [diff] [blame] | 414 | public static native byte[] SSL_SESSION_session_id(int sslSessionNativePointer); |
| 415 | |
| 416 | /** |
| 417 | * Returns the X509 certificates of the peer in the PEM format. |
| 418 | */ |
| 419 | public static native byte[][] SSL_SESSION_get_peer_cert_chain(int sslCtxNativePointer, |
| 420 | int sslSessionNativePointer); |
| 421 | |
| 422 | public static native long SSL_SESSION_get_time(int sslSessionNativePointer); |
| 423 | |
| 424 | public static native String SSL_SESSION_get_version(int sslSessionNativePointer); |
| 425 | |
| 426 | public static native String SSL_SESSION_cipher(int sslSessionNativePointer); |
| 427 | |
| 428 | public static native void SSL_SESSION_free(int sslSessionNativePointer); |
| 429 | |
| 430 | public static native byte[] i2d_SSL_SESSION(int sslSessionNativePointer); |
| 431 | |
| 432 | public static native int d2i_SSL_SESSION(byte[] data, int size); |
| 433 | |
Brian Carlstrom | 6df6339 | 2010-05-18 11:33:13 -0700 | [diff] [blame] | 434 | /** |
| 435 | * A collection of callbacks from the native OpenSSL code that are |
| 436 | * related to the SSL handshake initiated by SSL_do_handshake. |
| 437 | */ |
| 438 | public interface SSLHandshakeCallbacks { |
Brian Carlstrom | ecaf759 | 2010-03-02 16:55:35 -0800 | [diff] [blame] | 439 | /** |
| 440 | * Verify that we trust the certificate chain is trusted. |
| 441 | * |
Brian Carlstrom | bcfb325 | 2010-05-02 11:27:52 -0700 | [diff] [blame] | 442 | * @param bytes An array of certficates in PEM encode bytes |
| 443 | * @param authMethod auth algorithm name |
Brian Carlstrom | ecaf759 | 2010-03-02 16:55:35 -0800 | [diff] [blame] | 444 | * |
Brian Carlstrom | bcfb325 | 2010-05-02 11:27:52 -0700 | [diff] [blame] | 445 | * @throws CertificateException if the certificate is untrusted |
Brian Carlstrom | ecaf759 | 2010-03-02 16:55:35 -0800 | [diff] [blame] | 446 | */ |
Brian Carlstrom | 6df6339 | 2010-05-18 11:33:13 -0700 | [diff] [blame] | 447 | public void verifyCertificateChain(byte[][] bytes, String authMethod) |
| 448 | throws CertificateException; |
Brian Carlstrom | bcfb325 | 2010-05-02 11:27:52 -0700 | [diff] [blame] | 449 | |
Brian Carlstrom | 6df6339 | 2010-05-18 11:33:13 -0700 | [diff] [blame] | 450 | /** |
| 451 | * Called on an SSL client when the server requests (or |
| 452 | * requires a certificate). The client can respond by using |
| 453 | * SSL_use_certificate and SSL_use_PrivateKey to set a |
| 454 | * certificate if has an appropriate one available, similar to |
| 455 | * how the server provides its certificate. |
| 456 | * |
| 457 | * @param keyType One of KEY_TYPES such as RSA or DSA |
| 458 | */ |
| 459 | public void clientCertificateRequested(String keyType) |
| 460 | throws IOException; |
| 461 | |
Brian Carlstrom | bcfb325 | 2010-05-02 11:27:52 -0700 | [diff] [blame] | 462 | /** |
| 463 | * Called when SSL handshake is completed. Note that this can |
| 464 | * be after SSL_do_handshake returns when handshake cutthrough |
| 465 | * is enabled. |
| 466 | */ |
| 467 | public void handshakeCompleted(); |
Brian Carlstrom | ecaf759 | 2010-03-02 16:55:35 -0800 | [diff] [blame] | 468 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 469 | } |