• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.conscrypt;
18 
19 import java.io.FileDescriptor;
20 import java.io.IOException;
21 import java.io.OutputStream;
22 import java.net.SocketTimeoutException;
23 import java.security.InvalidKeyException;
24 import java.security.MessageDigest;
25 import java.security.NoSuchAlgorithmException;
26 import java.security.PrivateKey;
27 import java.security.SignatureException;
28 import java.security.cert.CertificateEncodingException;
29 import java.security.cert.CertificateException;
30 import java.security.cert.CertificateParsingException;
31 import java.util.ArrayList;
32 import java.util.Calendar;
33 import java.util.HashMap;
34 import java.util.HashSet;
35 import java.util.LinkedHashMap;
36 import java.util.List;
37 import java.util.Map;
38 import java.util.Set;
39 import javax.crypto.BadPaddingException;
40 import javax.crypto.IllegalBlockSizeException;
41 import javax.net.ssl.SSLException;
42 import javax.security.auth.x500.X500Principal;
43 
44 /**
45  * Provides the Java side of our JNI glue for OpenSSL.
46  */
47 public final class NativeCrypto {
48 
49     public static final boolean isBoringSSL;
50 
51     // --- OpenSSL library initialization --------------------------------------
52     static {
NativeCryptoJni.init()53         NativeCryptoJni.init();
54         isBoringSSL = clinit();
55     }
56 
clinit()57     private native static boolean clinit();
58 
59     // --- ENGINE functions ----------------------------------------------------
ENGINE_load_dynamic()60     public static native void ENGINE_load_dynamic();
61 
ENGINE_by_id(String id)62     public static native long ENGINE_by_id(String id);
63 
ENGINE_add(long e)64     public static native int ENGINE_add(long e);
65 
ENGINE_init(long e)66     public static native int ENGINE_init(long e);
67 
ENGINE_finish(long e)68     public static native int ENGINE_finish(long e);
69 
ENGINE_free(long e)70     public static native int ENGINE_free(long e);
71 
ENGINE_load_private_key(long e, String key_id)72     public static native long ENGINE_load_private_key(long e, String key_id) throws InvalidKeyException;
73 
ENGINE_get_id(long engineRef)74     public static native String ENGINE_get_id(long engineRef);
75 
ENGINE_ctrl_cmd_string(long engineRef, String cmd, String arg, int cmd_optional)76     public static native int ENGINE_ctrl_cmd_string(long engineRef, String cmd, String arg,
77             int cmd_optional);
78 
79     // --- DSA/RSA public/private key handling functions -----------------------
80 
EVP_PKEY_new_DSA(byte[] p, byte[] q, byte[] g, byte[] pub_key, byte[] priv_key)81     public static native long EVP_PKEY_new_DSA(byte[] p, byte[] q, byte[] g,
82                                                byte[] pub_key, byte[] priv_key);
83 
EVP_PKEY_new_RSA(byte[] n, byte[] e, byte[] d, byte[] p, byte[] q, byte[] dmp1, byte[] dmq1, byte[] iqmp)84     public static native long EVP_PKEY_new_RSA(byte[] n, byte[] e, byte[] d, byte[] p, byte[] q,
85             byte[] dmp1, byte[] dmq1, byte[] iqmp);
86 
EVP_PKEY_new_mac_key(int type, byte[] key)87     public static native long EVP_PKEY_new_mac_key(int type, byte[] key);
88 
EVP_PKEY_size(NativeRef.EVP_PKEY pkey)89     public static native int EVP_PKEY_size(NativeRef.EVP_PKEY pkey);
90 
EVP_PKEY_type(NativeRef.EVP_PKEY pkey)91     public static native int EVP_PKEY_type(NativeRef.EVP_PKEY pkey);
92 
EVP_PKEY_print_public(NativeRef.EVP_PKEY pkeyRef)93     public static native String EVP_PKEY_print_public(NativeRef.EVP_PKEY pkeyRef);
94 
EVP_PKEY_print_private(NativeRef.EVP_PKEY pkeyRef)95     public static native String EVP_PKEY_print_private(NativeRef.EVP_PKEY pkeyRef);
96 
EVP_PKEY_free(long pkey)97     public static native void EVP_PKEY_free(long pkey);
98 
EVP_PKEY_cmp(NativeRef.EVP_PKEY pkey1, NativeRef.EVP_PKEY pkey2)99     public static native int EVP_PKEY_cmp(NativeRef.EVP_PKEY pkey1, NativeRef.EVP_PKEY pkey2);
100 
i2d_PKCS8_PRIV_KEY_INFO(NativeRef.EVP_PKEY pkey)101     public static native byte[] i2d_PKCS8_PRIV_KEY_INFO(NativeRef.EVP_PKEY pkey);
102 
d2i_PKCS8_PRIV_KEY_INFO(byte[] data)103     public static native long d2i_PKCS8_PRIV_KEY_INFO(byte[] data);
104 
i2d_PUBKEY(NativeRef.EVP_PKEY pkey)105     public static native byte[] i2d_PUBKEY(NativeRef.EVP_PKEY pkey);
106 
d2i_PUBKEY(byte[] data)107     public static native long d2i_PUBKEY(byte[] data);
108 
getRSAPrivateKeyWrapper(PrivateKey key, byte[] modulus)109     public static native long getRSAPrivateKeyWrapper(PrivateKey key, byte[] modulus);
110 
getECPrivateKeyWrapper(PrivateKey key, NativeRef.EC_GROUP ecGroupRef)111     public static native long getECPrivateKeyWrapper(PrivateKey key,
112             NativeRef.EC_GROUP ecGroupRef);
113 
RSA_generate_key_ex(int modulusBits, byte[] publicExponent)114     public static native long RSA_generate_key_ex(int modulusBits, byte[] publicExponent);
115 
RSA_size(NativeRef.EVP_PKEY pkey)116     public static native int RSA_size(NativeRef.EVP_PKEY pkey);
117 
RSA_private_encrypt(int flen, byte[] from, byte[] to, NativeRef.EVP_PKEY pkey, int padding)118     public static native int RSA_private_encrypt(int flen, byte[] from, byte[] to,
119             NativeRef.EVP_PKEY pkey, int padding);
120 
RSA_public_decrypt(int flen, byte[] from, byte[] to, NativeRef.EVP_PKEY pkey, int padding)121     public static native int RSA_public_decrypt(int flen, byte[] from, byte[] to,
122             NativeRef.EVP_PKEY pkey, int padding) throws BadPaddingException, SignatureException;
123 
RSA_public_encrypt(int flen, byte[] from, byte[] to, NativeRef.EVP_PKEY pkey, int padding)124     public static native int RSA_public_encrypt(int flen, byte[] from, byte[] to,
125             NativeRef.EVP_PKEY pkey, int padding);
126 
RSA_private_decrypt(int flen, byte[] from, byte[] to, NativeRef.EVP_PKEY pkey, int padding)127     public static native int RSA_private_decrypt(int flen, byte[] from, byte[] to,
128             NativeRef.EVP_PKEY pkey, int padding) throws BadPaddingException, SignatureException;
129 
130     /**
131      * @return array of {n, e}
132      */
get_RSA_public_params(NativeRef.EVP_PKEY rsa)133     public static native byte[][] get_RSA_public_params(NativeRef.EVP_PKEY rsa);
134 
135     /**
136      * @return array of {n, e, d, p, q, dmp1, dmq1, iqmp}
137      */
get_RSA_private_params(NativeRef.EVP_PKEY rsa)138     public static native byte[][] get_RSA_private_params(NativeRef.EVP_PKEY rsa);
139 
i2d_RSAPublicKey(NativeRef.EVP_PKEY rsa)140     public static native byte[] i2d_RSAPublicKey(NativeRef.EVP_PKEY rsa);
141 
i2d_RSAPrivateKey(NativeRef.EVP_PKEY rsa)142     public static native byte[] i2d_RSAPrivateKey(NativeRef.EVP_PKEY rsa);
143 
144     // --- DH public/private key handling functions ----------------------------
145 
EVP_PKEY_new_DH(byte[] p, byte[] g, byte[] pub_key, byte[] priv_key)146     public static native long EVP_PKEY_new_DH(byte[] p, byte[] g, byte[] pub_key, byte[] priv_key);
147 
DH_generate_parameters_ex(int primeBits, long generator)148     public static native long DH_generate_parameters_ex(int primeBits, long generator);
149 
DH_generate_key(NativeRef.EVP_PKEY pkeyRef)150     public static native void DH_generate_key(NativeRef.EVP_PKEY pkeyRef);
151 
152     /**
153      * @return array of {p, g, y(pub), x(priv)}
154      */
get_DH_params(NativeRef.EVP_PKEY dh)155     public static native byte[][] get_DH_params(NativeRef.EVP_PKEY dh);
156 
157     // --- EC functions --------------------------
158 
159     /**
160      * Used to request EC_GROUP_new_curve_GFp to EC_GROUP_new_curve
161      */
162     public static final int EC_CURVE_GFP = 1;
163 
164     /**
165      * Used to request EC_GROUP_new_curve_GF2m to EC_GROUP_new_curve
166      */
167     public static final int EC_CURVE_GF2M = 2;
168 
EVP_PKEY_new_EC_KEY(NativeRef.EC_GROUP groupRef, NativeRef.EC_POINT pubkeyRef, byte[] privkey)169     public static native long EVP_PKEY_new_EC_KEY(NativeRef.EC_GROUP groupRef,
170             NativeRef.EC_POINT pubkeyRef, byte[] privkey);
171 
EC_GROUP_new_by_curve_name(String curveName)172     public static native long EC_GROUP_new_by_curve_name(String curveName);
173 
EC_GROUP_new_arbitrary(byte[] p, byte[] a, byte[] b, byte[] x, byte[] y, byte[] order, int cofactor)174     public static native long EC_GROUP_new_arbitrary(byte[] p, byte[] a, byte[] b, byte[] x,
175                                                      byte[] y, byte[] order, int cofactor);
176 
EC_GROUP_set_asn1_flag(NativeRef.EC_GROUP groupRef, int flag)177     public static native void EC_GROUP_set_asn1_flag(NativeRef.EC_GROUP groupRef, int flag);
178 
EC_GROUP_set_point_conversion_form(NativeRef.EC_GROUP groupRef, int form)179     public static native void EC_GROUP_set_point_conversion_form(NativeRef.EC_GROUP groupRef,
180             int form);
181 
EC_GROUP_get_curve_name(NativeRef.EC_GROUP groupRef)182     public static native String EC_GROUP_get_curve_name(NativeRef.EC_GROUP groupRef);
183 
EC_GROUP_get_curve(NativeRef.EC_GROUP groupRef)184     public static native byte[][] EC_GROUP_get_curve(NativeRef.EC_GROUP groupRef);
185 
EC_GROUP_clear_free(long groupRef)186     public static native void EC_GROUP_clear_free(long groupRef);
187 
EC_GROUP_cmp(NativeRef.EC_GROUP ctx1, NativeRef.EC_GROUP ctx2)188     public static native boolean EC_GROUP_cmp(NativeRef.EC_GROUP ctx1, NativeRef.EC_GROUP ctx2);
189 
EC_GROUP_get_generator(NativeRef.EC_GROUP groupRef)190     public static native long EC_GROUP_get_generator(NativeRef.EC_GROUP groupRef);
191 
get_EC_GROUP_type(NativeRef.EC_GROUP groupRef)192     public static native int get_EC_GROUP_type(NativeRef.EC_GROUP groupRef);
193 
EC_GROUP_get_order(NativeRef.EC_GROUP groupRef)194     public static native byte[] EC_GROUP_get_order(NativeRef.EC_GROUP groupRef);
195 
EC_GROUP_get_degree(NativeRef.EC_GROUP groupRef)196     public static native int EC_GROUP_get_degree(NativeRef.EC_GROUP groupRef);
197 
EC_GROUP_get_cofactor(NativeRef.EC_GROUP groupRef)198     public static native byte[] EC_GROUP_get_cofactor(NativeRef.EC_GROUP groupRef);
199 
EC_POINT_new(NativeRef.EC_GROUP groupRef)200     public static native long EC_POINT_new(NativeRef.EC_GROUP groupRef);
201 
EC_POINT_clear_free(long pointRef)202     public static native void EC_POINT_clear_free(long pointRef);
203 
EC_POINT_cmp(NativeRef.EC_GROUP groupRef, NativeRef.EC_POINT pointRef1, NativeRef.EC_POINT pointRef2)204     public static native boolean EC_POINT_cmp(NativeRef.EC_GROUP groupRef,
205             NativeRef.EC_POINT pointRef1, NativeRef.EC_POINT pointRef2);
206 
EC_POINT_get_affine_coordinates(NativeRef.EC_GROUP groupRef, NativeRef.EC_POINT pointRef)207     public static native byte[][] EC_POINT_get_affine_coordinates(NativeRef.EC_GROUP groupRef,
208             NativeRef.EC_POINT pointRef);
209 
EC_POINT_set_affine_coordinates(NativeRef.EC_GROUP groupRef, NativeRef.EC_POINT pointRef, byte[] x, byte[] y)210     public static native void EC_POINT_set_affine_coordinates(NativeRef.EC_GROUP groupRef,
211             NativeRef.EC_POINT pointRef, byte[] x, byte[] y);
212 
EC_KEY_generate_key(NativeRef.EC_GROUP groupRef)213     public static native long EC_KEY_generate_key(NativeRef.EC_GROUP groupRef);
214 
EC_KEY_get1_group(NativeRef.EVP_PKEY pkeyRef)215     public static native long EC_KEY_get1_group(NativeRef.EVP_PKEY pkeyRef);
216 
EC_KEY_get_private_key(NativeRef.EVP_PKEY keyRef)217     public static native byte[] EC_KEY_get_private_key(NativeRef.EVP_PKEY keyRef);
218 
EC_KEY_get_public_key(NativeRef.EVP_PKEY keyRef)219     public static native long EC_KEY_get_public_key(NativeRef.EVP_PKEY keyRef);
220 
EC_KEY_set_nonce_from_hash(NativeRef.EVP_PKEY keyRef, boolean enabled)221     public static native void EC_KEY_set_nonce_from_hash(NativeRef.EVP_PKEY keyRef,
222             boolean enabled);
223 
ECDH_compute_key(byte[] out, int outOffset, NativeRef.EVP_PKEY publicKeyRef, NativeRef.EVP_PKEY privateKeyRef)224     public static native int ECDH_compute_key(byte[] out, int outOffset,
225             NativeRef.EVP_PKEY publicKeyRef, NativeRef.EVP_PKEY privateKeyRef);
226 
227     // --- Message digest functions --------------
228 
229     // These return const references
EVP_get_digestbyname(String name)230     public static native long EVP_get_digestbyname(String name);
231 
EVP_MD_size(long evp_md_const)232     public static native int EVP_MD_size(long evp_md_const);
233 
EVP_MD_block_size(long evp_md_const)234     public static native int EVP_MD_block_size(long evp_md_const);
235 
236     // --- Message digest context functions --------------
237 
EVP_MD_CTX_create()238     public static native long EVP_MD_CTX_create();
239 
EVP_MD_CTX_init(NativeRef.EVP_MD_CTX ctx)240     public static native void EVP_MD_CTX_init(NativeRef.EVP_MD_CTX ctx);
241 
EVP_MD_CTX_destroy(long ctx)242     public static native void EVP_MD_CTX_destroy(long ctx);
243 
EVP_MD_CTX_copy(NativeRef.EVP_MD_CTX dst_ctx, NativeRef.EVP_MD_CTX src_ctx)244     public static native int EVP_MD_CTX_copy(NativeRef.EVP_MD_CTX dst_ctx,
245             NativeRef.EVP_MD_CTX src_ctx);
246 
247     // --- Digest handling functions -------------------------------------------
248 
EVP_DigestInit(NativeRef.EVP_MD_CTX ctx, long evp_md)249     public static native int EVP_DigestInit(NativeRef.EVP_MD_CTX ctx, long evp_md);
250 
EVP_DigestUpdate(NativeRef.EVP_MD_CTX ctx, byte[] buffer, int offset, int length)251     public static native void EVP_DigestUpdate(NativeRef.EVP_MD_CTX ctx,
252             byte[] buffer, int offset, int length);
253 
EVP_DigestFinal(NativeRef.EVP_MD_CTX ctx, byte[] hash, int offset)254     public static native int EVP_DigestFinal(NativeRef.EVP_MD_CTX ctx, byte[] hash,
255             int offset);
256 
257     // --- MAC handling functions ----------------------------------------------
258 
EVP_DigestSignInit(NativeRef.EVP_MD_CTX evp_md_ctx, long evp_md, NativeRef.EVP_PKEY evp_pkey)259     public static native void EVP_DigestSignInit(NativeRef.EVP_MD_CTX evp_md_ctx,
260             long evp_md, NativeRef.EVP_PKEY evp_pkey);
261 
EVP_DigestSignUpdate(NativeRef.EVP_MD_CTX evp_md_ctx, byte[] in)262     public static native void EVP_DigestSignUpdate(NativeRef.EVP_MD_CTX evp_md_ctx,
263             byte[] in);
264 
EVP_DigestSignFinal(NativeRef.EVP_MD_CTX evp_md_ctx)265     public static native byte[] EVP_DigestSignFinal(NativeRef.EVP_MD_CTX evp_md_ctx);
266 
267     // --- Signature handling functions ----------------------------------------
268 
EVP_SignInit(NativeRef.EVP_MD_CTX ctx, long evpRef)269     public static native int EVP_SignInit(NativeRef.EVP_MD_CTX ctx, long evpRef);
270 
EVP_SignUpdate(NativeRef.EVP_MD_CTX ctx, byte[] buffer, int offset, int length)271     public static native void EVP_SignUpdate(NativeRef.EVP_MD_CTX ctx, byte[] buffer,
272             int offset, int length);
273 
EVP_SignFinal(NativeRef.EVP_MD_CTX ctx, byte[] signature, int offset, NativeRef.EVP_PKEY key)274     public static native int EVP_SignFinal(NativeRef.EVP_MD_CTX ctx, byte[] signature,
275             int offset, NativeRef.EVP_PKEY key);
276 
EVP_VerifyInit(NativeRef.EVP_MD_CTX ctx, long evpRef)277     public static native int EVP_VerifyInit(NativeRef.EVP_MD_CTX ctx, long evpRef);
278 
EVP_VerifyUpdate(NativeRef.EVP_MD_CTX ctx, byte[] buffer, int offset, int length)279     public static native void EVP_VerifyUpdate(NativeRef.EVP_MD_CTX ctx,
280             byte[] buffer, int offset, int length);
281 
EVP_VerifyFinal(NativeRef.EVP_MD_CTX ctx, byte[] signature, int offset, int length, NativeRef.EVP_PKEY key)282     public static native int EVP_VerifyFinal(NativeRef.EVP_MD_CTX ctx,
283             byte[] signature, int offset, int length, NativeRef.EVP_PKEY key);
284 
285     // --- Block ciphers -------------------------------------------------------
286 
287     // These return const references
EVP_get_cipherbyname(String string)288     public static native long EVP_get_cipherbyname(String string);
289 
EVP_CipherInit_ex(NativeRef.EVP_CIPHER_CTX ctx, long evpCipher, byte[] key, byte[] iv, boolean encrypting)290     public static native void EVP_CipherInit_ex(NativeRef.EVP_CIPHER_CTX ctx, long evpCipher,
291             byte[] key, byte[] iv, boolean encrypting);
292 
EVP_CipherUpdate(NativeRef.EVP_CIPHER_CTX ctx, byte[] out, int outOffset, byte[] in, int inOffset, int inLength)293     public static native int EVP_CipherUpdate(NativeRef.EVP_CIPHER_CTX ctx, byte[] out,
294             int outOffset, byte[] in, int inOffset, int inLength);
295 
EVP_CipherFinal_ex(NativeRef.EVP_CIPHER_CTX ctx, byte[] out, int outOffset)296     public static native int EVP_CipherFinal_ex(NativeRef.EVP_CIPHER_CTX ctx, byte[] out,
297             int outOffset) throws BadPaddingException, IllegalBlockSizeException;
298 
EVP_CIPHER_iv_length(long evpCipher)299     public static native int EVP_CIPHER_iv_length(long evpCipher);
300 
EVP_CIPHER_CTX_new()301     public static native long EVP_CIPHER_CTX_new();
302 
EVP_CIPHER_CTX_block_size(NativeRef.EVP_CIPHER_CTX ctx)303     public static native int EVP_CIPHER_CTX_block_size(NativeRef.EVP_CIPHER_CTX ctx);
304 
get_EVP_CIPHER_CTX_buf_len(NativeRef.EVP_CIPHER_CTX ctx)305     public static native int get_EVP_CIPHER_CTX_buf_len(NativeRef.EVP_CIPHER_CTX ctx);
306 
get_EVP_CIPHER_CTX_final_used(NativeRef.EVP_CIPHER_CTX ctx)307     public static native boolean get_EVP_CIPHER_CTX_final_used(NativeRef.EVP_CIPHER_CTX ctx);
308 
EVP_CIPHER_CTX_set_padding(NativeRef.EVP_CIPHER_CTX ctx, boolean enablePadding)309     public static native void EVP_CIPHER_CTX_set_padding(NativeRef.EVP_CIPHER_CTX ctx,
310             boolean enablePadding);
311 
EVP_CIPHER_CTX_set_key_length(NativeRef.EVP_CIPHER_CTX ctx, int keyBitSize)312     public static native void EVP_CIPHER_CTX_set_key_length(NativeRef.EVP_CIPHER_CTX ctx,
313             int keyBitSize);
314 
EVP_CIPHER_CTX_free(long ctx)315     public static native void EVP_CIPHER_CTX_free(long ctx);
316 
317     // --- AEAD ----------------------------------------------------------------
EVP_aead_aes_128_gcm()318     public static native long EVP_aead_aes_128_gcm();
319 
EVP_aead_aes_256_gcm()320     public static native long EVP_aead_aes_256_gcm();
321 
EVP_AEAD_CTX_init(long evpAead, byte[] key, int tagLen)322     public static native long EVP_AEAD_CTX_init(long evpAead, byte[] key, int tagLen);
323 
EVP_AEAD_CTX_cleanup(long ctx)324     public static native void EVP_AEAD_CTX_cleanup(long ctx);
325 
EVP_AEAD_max_overhead(long evpAead)326     public static native int EVP_AEAD_max_overhead(long evpAead);
327 
EVP_AEAD_nonce_length(long evpAead)328     public static native int EVP_AEAD_nonce_length(long evpAead);
329 
EVP_AEAD_max_tag_len(long evpAead)330     public static native int EVP_AEAD_max_tag_len(long evpAead);
331 
EVP_AEAD_CTX_seal(NativeRef.EVP_AEAD_CTX ctx, byte[] out, int outOffset, byte[] nonce, byte[] in, int inOffset, int inLength, byte[] ad)332     public static native int EVP_AEAD_CTX_seal(NativeRef.EVP_AEAD_CTX ctx, byte[] out,
333             int outOffset, byte[] nonce, byte[] in, int inOffset, int inLength, byte[] ad)
334             throws BadPaddingException;
335 
EVP_AEAD_CTX_open(NativeRef.EVP_AEAD_CTX ctx, byte[] out, int outOffset, byte[] nonce, byte[] in, int inOffset, int inLength, byte[] ad)336     public static native int EVP_AEAD_CTX_open(NativeRef.EVP_AEAD_CTX ctx, byte[] out,
337             int outOffset, byte[] nonce, byte[] in, int inOffset, int inLength, byte[] ad)
338             throws BadPaddingException;
339 
340     // --- RAND seeding --------------------------------------------------------
341 
342     public static final int RAND_SEED_LENGTH_IN_BYTES = 1024;
343 
RAND_seed(byte[] seed)344     public static native void RAND_seed(byte[] seed);
345 
RAND_load_file(String filename, long max_bytes)346     public static native int RAND_load_file(String filename, long max_bytes);
347 
RAND_bytes(byte[] output)348     public static native void RAND_bytes(byte[] output);
349 
350     // --- ASN.1 objects -------------------------------------------------------
351 
OBJ_txt2nid(String oid)352     public static native int OBJ_txt2nid(String oid);
353 
OBJ_txt2nid_longName(String oid)354     public static native String OBJ_txt2nid_longName(String oid);
355 
OBJ_txt2nid_oid(String oid)356     public static native String OBJ_txt2nid_oid(String oid);
357 
358     // --- X509_NAME -----------------------------------------------------------
359 
X509_NAME_hash(X500Principal principal)360     public static int X509_NAME_hash(X500Principal principal) {
361         return X509_NAME_hash(principal, "SHA1");
362     }
X509_NAME_hash_old(X500Principal principal)363     public static int X509_NAME_hash_old(X500Principal principal) {
364         return X509_NAME_hash(principal, "MD5");
365     }
X509_NAME_hash(X500Principal principal, String algorithm)366     private static int X509_NAME_hash(X500Principal principal, String algorithm) {
367         try {
368             byte[] digest = MessageDigest.getInstance(algorithm).digest(principal.getEncoded());
369             int offset = 0;
370             return (((digest[offset++] & 0xff) <<  0) |
371                     ((digest[offset++] & 0xff) <<  8) |
372                     ((digest[offset++] & 0xff) << 16) |
373                     ((digest[offset  ] & 0xff) << 24));
374         } catch (NoSuchAlgorithmException e) {
375             throw new AssertionError(e);
376         }
377     }
378 
X509_NAME_print_ex(long x509nameCtx, long flags)379     public static native String X509_NAME_print_ex(long x509nameCtx, long flags);
380 
381     // --- X509 ----------------------------------------------------------------
382 
383     /** Used to request get_X509_GENERAL_NAME_stack get the "altname" field. */
384     public static final int GN_STACK_SUBJECT_ALT_NAME = 1;
385 
386     /**
387      * Used to request get_X509_GENERAL_NAME_stack get the issuerAlternativeName
388      * extension.
389      */
390     public static final int GN_STACK_ISSUER_ALT_NAME = 2;
391 
392     /**
393      * Used to request only non-critical types in get_X509*_ext_oids.
394      */
395     public static final int EXTENSION_TYPE_NON_CRITICAL = 0;
396 
397     /**
398      * Used to request only critical types in get_X509*_ext_oids.
399      */
400     public static final int EXTENSION_TYPE_CRITICAL = 1;
401 
d2i_X509_bio(long bioCtx)402     public static native long d2i_X509_bio(long bioCtx);
403 
d2i_X509(byte[] encoded)404     public static native long d2i_X509(byte[] encoded);
405 
PEM_read_bio_X509(long bioCtx)406     public static native long PEM_read_bio_X509(long bioCtx);
407 
i2d_X509(long x509ctx)408     public static native byte[] i2d_X509(long x509ctx);
409 
410     /** Takes an X509 context not an X509_PUBKEY context. */
i2d_X509_PUBKEY(long x509ctx)411     public static native byte[] i2d_X509_PUBKEY(long x509ctx);
412 
ASN1_seq_pack_X509(long[] x509CertRefs)413     public static native byte[] ASN1_seq_pack_X509(long[] x509CertRefs);
414 
ASN1_seq_unpack_X509_bio(long bioRef)415     public static native long[] ASN1_seq_unpack_X509_bio(long bioRef);
416 
X509_free(long x509ctx)417     public static native void X509_free(long x509ctx);
418 
X509_cmp(long x509ctx1, long x509ctx2)419     public static native int X509_cmp(long x509ctx1, long x509ctx2);
420 
get_X509_hashCode(long x509ctx)421     public static native int get_X509_hashCode(long x509ctx);
422 
X509_print_ex(long bioCtx, long x509ctx, long nmflag, long certflag)423     public static native void X509_print_ex(long bioCtx, long x509ctx, long nmflag, long certflag);
424 
X509_get_issuer_name(long x509ctx)425     public static native byte[] X509_get_issuer_name(long x509ctx);
426 
X509_get_subject_name(long x509ctx)427     public static native byte[] X509_get_subject_name(long x509ctx);
428 
get_X509_sig_alg_oid(long x509ctx)429     public static native String get_X509_sig_alg_oid(long x509ctx);
430 
get_X509_sig_alg_parameter(long x509ctx)431     public static native byte[] get_X509_sig_alg_parameter(long x509ctx);
432 
get_X509_issuerUID(long x509ctx)433     public static native boolean[] get_X509_issuerUID(long x509ctx);
434 
get_X509_subjectUID(long x509ctx)435     public static native boolean[] get_X509_subjectUID(long x509ctx);
436 
X509_get_pubkey(long x509ctx)437     public static native long X509_get_pubkey(long x509ctx) throws NoSuchAlgorithmException;
438 
get_X509_pubkey_oid(long x509ctx)439     public static native String get_X509_pubkey_oid(long x509ctx);
440 
X509_get_ext_oid(long x509ctx, String oid)441     public static native byte[] X509_get_ext_oid(long x509ctx, String oid);
442 
get_X509_ext_oids(long x509ctx, int critical)443     public static native String[] get_X509_ext_oids(long x509ctx, int critical);
444 
get_X509_GENERAL_NAME_stack(long x509ctx, int type)445     public static native Object[][] get_X509_GENERAL_NAME_stack(long x509ctx, int type)
446             throws CertificateParsingException;
447 
get_X509_ex_kusage(long x509ctx)448     public static native boolean[] get_X509_ex_kusage(long x509ctx);
449 
get_X509_ex_xkusage(long x509ctx)450     public static native String[] get_X509_ex_xkusage(long x509ctx);
451 
get_X509_ex_pathlen(long x509ctx)452     public static native int get_X509_ex_pathlen(long x509ctx);
453 
X509_get_notBefore(long x509ctx)454     public static native long X509_get_notBefore(long x509ctx);
455 
X509_get_notAfter(long x509ctx)456     public static native long X509_get_notAfter(long x509ctx);
457 
X509_get_version(long x509ctx)458     public static native long X509_get_version(long x509ctx);
459 
X509_get_serialNumber(long x509ctx)460     public static native byte[] X509_get_serialNumber(long x509ctx);
461 
X509_verify(long x509ctx, NativeRef.EVP_PKEY pkeyCtx)462     public static native void X509_verify(long x509ctx, NativeRef.EVP_PKEY pkeyCtx)
463             throws BadPaddingException;
464 
get_X509_cert_info_enc(long x509ctx)465     public static native byte[] get_X509_cert_info_enc(long x509ctx);
466 
get_X509_signature(long x509ctx)467     public static native byte[] get_X509_signature(long x509ctx);
468 
get_X509_ex_flags(long x509ctx)469     public static native int get_X509_ex_flags(long x509ctx);
470 
X509_check_issued(long ctx, long ctx2)471     public static native int X509_check_issued(long ctx, long ctx2);
472 
473     // --- PKCS7 ---------------------------------------------------------------
474 
475     /** Used as the "which" field in d2i_PKCS7_bio and PEM_read_bio_PKCS7. */
476     public static final int PKCS7_CERTS = 1;
477 
478     /** Used as the "which" field in d2i_PKCS7_bio and PEM_read_bio_PKCS7. */
479     public static final int PKCS7_CRLS = 2;
480 
481     /** Returns an array of X509 or X509_CRL pointers. */
d2i_PKCS7_bio(long bioCtx, int which)482     public static native long[] d2i_PKCS7_bio(long bioCtx, int which);
483 
484     /** Returns an array of X509 or X509_CRL pointers. */
i2d_PKCS7(long[] certs)485     public static native byte[] i2d_PKCS7(long[] certs);
486 
487     /** Returns an array of X509 or X509_CRL pointers. */
PEM_read_bio_PKCS7(long bioCtx, int which)488     public static native long[] PEM_read_bio_PKCS7(long bioCtx, int which);
489 
490     // --- X509_CRL ------------------------------------------------------------
491 
d2i_X509_CRL_bio(long bioCtx)492     public static native long d2i_X509_CRL_bio(long bioCtx);
493 
PEM_read_bio_X509_CRL(long bioCtx)494     public static native long PEM_read_bio_X509_CRL(long bioCtx);
495 
i2d_X509_CRL(long x509CrlCtx)496     public static native byte[] i2d_X509_CRL(long x509CrlCtx);
497 
X509_CRL_free(long x509CrlCtx)498     public static native void X509_CRL_free(long x509CrlCtx);
499 
X509_CRL_print(long bioCtx, long x509CrlCtx)500     public static native void X509_CRL_print(long bioCtx, long x509CrlCtx);
501 
get_X509_CRL_sig_alg_oid(long x509CrlCtx)502     public static native String get_X509_CRL_sig_alg_oid(long x509CrlCtx);
503 
get_X509_CRL_sig_alg_parameter(long x509CrlCtx)504     public static native byte[] get_X509_CRL_sig_alg_parameter(long x509CrlCtx);
505 
X509_CRL_get_issuer_name(long x509CrlCtx)506     public static native byte[] X509_CRL_get_issuer_name(long x509CrlCtx);
507 
508     /** Returns X509_REVOKED reference that is not duplicated! */
X509_CRL_get0_by_cert(long x509CrlCtx, long x509Ctx)509     public static native long X509_CRL_get0_by_cert(long x509CrlCtx, long x509Ctx);
510 
511     /** Returns X509_REVOKED reference that is not duplicated! */
X509_CRL_get0_by_serial(long x509CrlCtx, byte[] serial)512     public static native long X509_CRL_get0_by_serial(long x509CrlCtx, byte[] serial);
513 
514     /** Returns an array of X509_REVOKED that are owned by the caller. */
X509_CRL_get_REVOKED(long x509CrlCtx)515     public static native long[] X509_CRL_get_REVOKED(long x509CrlCtx);
516 
get_X509_CRL_ext_oids(long x509ctx, int critical)517     public static native String[] get_X509_CRL_ext_oids(long x509ctx, int critical);
518 
X509_CRL_get_ext_oid(long x509CrlCtx, String oid)519     public static native byte[] X509_CRL_get_ext_oid(long x509CrlCtx, String oid);
520 
X509_CRL_get_version(long x509CrlCtx)521     public static native long X509_CRL_get_version(long x509CrlCtx);
522 
X509_CRL_get_ext(long x509CrlCtx, String oid)523     public static native long X509_CRL_get_ext(long x509CrlCtx, String oid);
524 
get_X509_CRL_signature(long x509ctx)525     public static native byte[] get_X509_CRL_signature(long x509ctx);
526 
X509_CRL_verify(long x509CrlCtx, NativeRef.EVP_PKEY pkeyCtx)527     public static native void X509_CRL_verify(long x509CrlCtx, NativeRef.EVP_PKEY pkeyCtx);
528 
get_X509_CRL_crl_enc(long x509CrlCtx)529     public static native byte[] get_X509_CRL_crl_enc(long x509CrlCtx);
530 
X509_CRL_get_lastUpdate(long x509CrlCtx)531     public static native long X509_CRL_get_lastUpdate(long x509CrlCtx);
532 
X509_CRL_get_nextUpdate(long x509CrlCtx)533     public static native long X509_CRL_get_nextUpdate(long x509CrlCtx);
534 
535     // --- X509_REVOKED --------------------------------------------------------
536 
X509_REVOKED_dup(long x509RevokedCtx)537     public static native long X509_REVOKED_dup(long x509RevokedCtx);
538 
i2d_X509_REVOKED(long x509RevokedCtx)539     public static native byte[] i2d_X509_REVOKED(long x509RevokedCtx);
540 
get_X509_REVOKED_ext_oids(long x509ctx, int critical)541     public static native String[] get_X509_REVOKED_ext_oids(long x509ctx, int critical);
542 
X509_REVOKED_get_ext_oid(long x509RevokedCtx, String oid)543     public static native byte[] X509_REVOKED_get_ext_oid(long x509RevokedCtx, String oid);
544 
X509_REVOKED_get_serialNumber(long x509RevokedCtx)545     public static native byte[] X509_REVOKED_get_serialNumber(long x509RevokedCtx);
546 
X509_REVOKED_get_ext(long x509RevokedCtx, String oid)547     public static native long X509_REVOKED_get_ext(long x509RevokedCtx, String oid);
548 
549     /** Returns ASN1_TIME reference. */
get_X509_REVOKED_revocationDate(long x509RevokedCtx)550     public static native long get_X509_REVOKED_revocationDate(long x509RevokedCtx);
551 
X509_REVOKED_print(long bioRef, long x509RevokedCtx)552     public static native void X509_REVOKED_print(long bioRef, long x509RevokedCtx);
553 
554     // --- X509_EXTENSION ------------------------------------------------------
555 
X509_supported_extension(long x509ExtensionRef)556     public static native int X509_supported_extension(long x509ExtensionRef);
557 
558     // --- ASN1_TIME -----------------------------------------------------------
559 
ASN1_TIME_to_Calendar(long asn1TimeCtx, Calendar cal)560     public static native void ASN1_TIME_to_Calendar(long asn1TimeCtx, Calendar cal);
561 
562     // --- BIO stream creation -------------------------------------------------
563 
create_BIO_InputStream(OpenSSLBIOInputStream is, boolean isFinite)564     public static native long create_BIO_InputStream(OpenSSLBIOInputStream is, boolean isFinite);
565 
create_BIO_OutputStream(OutputStream os)566     public static native long create_BIO_OutputStream(OutputStream os);
567 
BIO_read(long bioRef, byte[] buffer)568     public static native int BIO_read(long bioRef, byte[] buffer);
569 
BIO_write(long bioRef, byte[] buffer, int offset, int length)570     public static native void BIO_write(long bioRef, byte[] buffer, int offset, int length)
571             throws IOException;
572 
BIO_free_all(long bioRef)573     public static native void BIO_free_all(long bioRef);
574 
575     // --- SSL handling --------------------------------------------------------
576 
577     private static final String SUPPORTED_PROTOCOL_SSLV3 = "SSLv3";
578     private static final String SUPPORTED_PROTOCOL_TLSV1 = "TLSv1";
579     private static final String SUPPORTED_PROTOCOL_TLSV1_1 = "TLSv1.1";
580     private static final String SUPPORTED_PROTOCOL_TLSV1_2 = "TLSv1.2";
581 
582     // STANDARD_TO_OPENSSL_CIPHER_SUITES is a map from OpenSSL-style
583     // cipher-suite names to the standard name for the same (i.e. the name that
584     // is registered with IANA).
585     public static final Map<String, String> OPENSSL_TO_STANDARD_CIPHER_SUITES
586             = new HashMap<String, String>();
587 
588     // STANDARD_TO_OPENSSL_CIPHER_SUITES is a map from "standard" cipher suite
589     // names (i.e. the names that are registered with IANA) to the
590     // OpenSSL-style name for the same.
591     public static final Map<String, String> STANDARD_TO_OPENSSL_CIPHER_SUITES
592             = new LinkedHashMap<String, String>();
593 
594     // SUPPORTED_CIPHER_SUITES_SET contains all the cipher suites supported by
595     // OpenSSL, named using "standard" (as opposed to OpenSSL-style) names.
596     public static final Set<String> SUPPORTED_CIPHER_SUITES_SET = new HashSet<String>();
597 
add(String openssl, String standard)598     private static void add(String openssl, String standard) {
599         OPENSSL_TO_STANDARD_CIPHER_SUITES.put(openssl, standard);
600         STANDARD_TO_OPENSSL_CIPHER_SUITES.put(standard, openssl);
601     }
602 
603     /**
604      * TLS_EMPTY_RENEGOTIATION_INFO_SCSV is RFC 5746's renegotiation
605      * indication signaling cipher suite value. It is not a real
606      * cipher suite. It is just an indication in the default and
607      * supported cipher suite lists indicates that the implementation
608      * supports secure renegotiation.
609      *
610      * In the RI, its presence means that the SCSV is sent in the
611      * cipher suite list to indicate secure renegotiation support and
612      * its absense means to send an empty TLS renegotiation info
613      * extension instead.
614      *
615      * However, OpenSSL doesn't provide an API to give this level of
616      * control, instead always sending the SCSV and always including
617      * the empty renegotiation info if TLS is used (as opposed to
618      * SSL). So we simply allow TLS_EMPTY_RENEGOTIATION_INFO_SCSV to
619      * be passed for compatibility as to provide the hint that we
620      * support secure renegotiation.
621      */
622     public static final String TLS_EMPTY_RENEGOTIATION_INFO_SCSV
623             = "TLS_EMPTY_RENEGOTIATION_INFO_SCSV";
624 
625     /**
626      * TLS_FALLBACK_SCSV is from
627      * https://tools.ietf.org/html/draft-ietf-tls-downgrade-scsv-00
628      * to indicate to the server that this is a fallback protocol
629      * request.
630      */
631     public static final String TLS_FALLBACK_SCSV = "TLS_FALLBACK_SCSV";
632 
633     static {
634         add("ADH-AES128-GCM-SHA256",		"TLS_DH_anon_WITH_AES_128_GCM_SHA256");
635         add("ADH-AES128-SHA",			"TLS_DH_anon_WITH_AES_128_CBC_SHA");
636         add("ADH-AES128-SHA256",		"TLS_DH_anon_WITH_AES_128_CBC_SHA256");
637         add("ADH-AES256-GCM-SHA384",		"TLS_DH_anon_WITH_AES_256_GCM_SHA384");
638         add("ADH-AES256-SHA",			"TLS_DH_anon_WITH_AES_256_CBC_SHA");
639         add("ADH-AES256-SHA256",		"TLS_DH_anon_WITH_AES_256_CBC_SHA256");
640         add("ADH-DES-CBC-SHA",			"SSL_DH_anon_WITH_DES_CBC_SHA");
641         add("ADH-DES-CBC3-SHA",			"SSL_DH_anon_WITH_3DES_EDE_CBC_SHA");
642         add("ADH-RC4-MD5",			"SSL_DH_anon_WITH_RC4_128_MD5");
643         add("AECDH-AES128-SHA",			"TLS_ECDH_anon_WITH_AES_128_CBC_SHA");
644         add("AECDH-AES256-SHA",			"TLS_ECDH_anon_WITH_AES_256_CBC_SHA");
645         add("AECDH-DES-CBC3-SHA",		"TLS_ECDH_anon_WITH_3DES_EDE_CBC_SHA");
646         add("AECDH-NULL-SHA",			"TLS_ECDH_anon_WITH_NULL_SHA");
647         add("AECDH-RC4-SHA",			"TLS_ECDH_anon_WITH_RC4_128_SHA");
648         add("AES128-GCM-SHA256",		"TLS_RSA_WITH_AES_128_GCM_SHA256");
649         add("AES128-SHA",			"TLS_RSA_WITH_AES_128_CBC_SHA");
650         add("AES128-SHA256",			"TLS_RSA_WITH_AES_128_CBC_SHA256");
651         add("AES256-GCM-SHA384",		"TLS_RSA_WITH_AES_256_GCM_SHA384");
652         add("AES256-SHA",			"TLS_RSA_WITH_AES_256_CBC_SHA");
653         add("AES256-SHA256",			"TLS_RSA_WITH_AES_256_CBC_SHA256");
654         add("DES-CBC-SHA",			"SSL_RSA_WITH_DES_CBC_SHA");
655         add("DES-CBC3-SHA",			"SSL_RSA_WITH_3DES_EDE_CBC_SHA");
656         add("DHE-RSA-AES128-GCM-SHA256",	"TLS_DHE_RSA_WITH_AES_128_GCM_SHA256");
657         add("DHE-RSA-AES128-SHA",		"TLS_DHE_RSA_WITH_AES_128_CBC_SHA");
658         add("DHE-RSA-AES128-SHA256",		"TLS_DHE_RSA_WITH_AES_128_CBC_SHA256");
659         add("DHE-RSA-AES256-GCM-SHA384",	"TLS_DHE_RSA_WITH_AES_256_GCM_SHA384");
660         add("DHE-RSA-AES256-SHA",		"TLS_DHE_RSA_WITH_AES_256_CBC_SHA");
661         add("DHE-RSA-AES256-SHA256",		"TLS_DHE_RSA_WITH_AES_256_CBC_SHA256");
662         add("DHE-RSA-CHACHA20-POLY1305",	"TLS_DHE_RSA_WITH_CHACHA20_POLY1305");
663         add("ECDH-ECDSA-AES128-GCM-SHA256",    	"TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256");
664         add("ECDH-ECDSA-AES128-SHA",		"TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA");
665         add("ECDH-ECDSA-AES128-SHA256",		"TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256");
666         add("ECDH-ECDSA-AES256-GCM-SHA384",    	"TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384");
667         add("ECDH-ECDSA-AES256-SHA",		"TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA");
668         add("ECDH-ECDSA-AES256-SHA384",		"TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384");
669         add("ECDH-ECDSA-DES-CBC3-SHA",		"TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA");
670         add("ECDH-ECDSA-NULL-SHA",		"TLS_ECDH_ECDSA_WITH_NULL_SHA");
671         add("ECDH-ECDSA-RC4-SHA",		"TLS_ECDH_ECDSA_WITH_RC4_128_SHA");
672         add("ECDH-RSA-AES128-GCM-SHA256",      	"TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256");
673         add("ECDH-RSA-AES128-SHA",		"TLS_ECDH_RSA_WITH_AES_128_CBC_SHA");
674         add("ECDH-RSA-AES128-SHA256",		"TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256");
675         add("ECDH-RSA-AES256-GCM-SHA384",      	"TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384");
676         add("ECDH-RSA-AES256-SHA",		"TLS_ECDH_RSA_WITH_AES_256_CBC_SHA");
677         add("ECDH-RSA-AES256-SHA384",		"TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384");
678         add("ECDH-RSA-DES-CBC3-SHA",		"TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA");
679         add("ECDH-RSA-NULL-SHA",		"TLS_ECDH_RSA_WITH_NULL_SHA");
680         add("ECDH-RSA-RC4-SHA",			"TLS_ECDH_RSA_WITH_RC4_128_SHA");
681         add("ECDHE-ECDSA-AES128-GCM-SHA256",   	"TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256");
682         add("ECDHE-ECDSA-AES128-SHA",		"TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA");
683         add("ECDHE-ECDSA-AES128-SHA256",	"TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256");
684         add("ECDHE-ECDSA-AES256-GCM-SHA384",   	"TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384");
685         add("ECDHE-ECDSA-AES256-SHA",		"TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA");
686         add("ECDHE-ECDSA-AES256-SHA384",	"TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384");
687         add("ECDHE-ECDSA-CHACHA20-POLY1305",   	"TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305");
688         add("ECDHE-ECDSA-DES-CBC3-SHA",		"TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA");
689         add("ECDHE-ECDSA-NULL-SHA",		"TLS_ECDHE_ECDSA_WITH_NULL_SHA");
690         add("ECDHE-ECDSA-RC4-SHA",		"TLS_ECDHE_ECDSA_WITH_RC4_128_SHA");
691         add("ECDHE-PSK-AES128-CBC-SHA",		"TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA");
692         add("ECDHE-PSK-AES128-GCM-SHA256",     	"TLS_ECDHE_PSK_WITH_AES_128_GCM_SHA256");
693         add("ECDHE-PSK-AES256-CBC-SHA",		"TLS_ECDHE_PSK_WITH_AES_256_CBC_SHA");
694         add("ECDHE-PSK-AES256-GCM-SHA384",     	"TLS_ECDHE_PSK_WITH_AES_256_GCM_SHA384");
695         add("ECDHE-RSA-AES128-GCM-SHA256",     	"TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256");
696         add("ECDHE-RSA-AES128-SHA",		"TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA");
697         add("ECDHE-RSA-AES128-SHA256",		"TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256");
698         add("ECDHE-RSA-AES256-GCM-SHA384",     	"TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384");
699         add("ECDHE-RSA-AES256-SHA",		"TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA");
700         add("ECDHE-RSA-AES256-SHA384",		"TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384");
701         add("ECDHE-RSA-CHACHA20-POLY1305",     	"TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305");
702         add("ECDHE-RSA-DES-CBC3-SHA",		"TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA");
703         add("ECDHE-RSA-NULL-SHA",		"TLS_ECDHE_RSA_WITH_NULL_SHA");
704         add("ECDHE-RSA-RC4-SHA",		"TLS_ECDHE_RSA_WITH_RC4_128_SHA");
705         add("EDH-RSA-DES-CBC-SHA",		"SSL_DHE_RSA_WITH_DES_CBC_SHA");
706         add("EDH-RSA-DES-CBC3-SHA",		"SSL_DHE_RSA_WITH_3DES_EDE_CBC_SHA");
707         add("EXP-ADH-DES-CBC-SHA",		"SSL_DH_anon_EXPORT_WITH_DES40_CBC_SHA");
708         add("EXP-ADH-RC4-MD5",			"SSL_DH_anon_EXPORT_WITH_RC4_40_MD5");
709         add("EXP-DES-CBC-SHA",			"SSL_RSA_EXPORT_WITH_DES40_CBC_SHA");
710         add("EXP-EDH-RSA-DES-CBC-SHA",		"SSL_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA");
711         add("EXP-RC4-MD5",			"SSL_RSA_EXPORT_WITH_RC4_40_MD5");
712         add("NULL-MD5",				"SSL_RSA_WITH_NULL_MD5");
713         add("NULL-SHA",				"SSL_RSA_WITH_NULL_SHA");
714         add("NULL-SHA256",			"TLS_RSA_WITH_NULL_SHA256");
715         add("PSK-3DES-EDE-CBC-SHA",		"TLS_PSK_WITH_3DES_EDE_CBC_SHA");
716         add("PSK-AES128-CBC-SHA",		"TLS_PSK_WITH_AES_128_CBC_SHA");
717         add("PSK-AES256-CBC-SHA",		"TLS_PSK_WITH_AES_256_CBC_SHA");
718         add("PSK-RC4-SHA",			"TLS_PSK_WITH_RC4_128_SHA");
719         add("RC4-MD5",				"SSL_RSA_WITH_RC4_128_MD5");
720         add("RC4-SHA",				"SSL_RSA_WITH_RC4_128_SHA");
721 
722         // Signaling Cipher Suite Value for secure renegotiation handled as special case.
723         // add("TLS_EMPTY_RENEGOTIATION_INFO_SCSV", null);
724 
725         // Similarly, the fallback SCSV is handled as a special case.
726         // add("TLS_FALLBACK_SCSV", null);
727     }
728 
729     private static final String[] SUPPORTED_CIPHER_SUITES;
730     static {
731         // Cipher selection string must work with OpenSSL and BoringSSL.
732         String[] allOpenSSLCipherSuites = get_cipher_names("ALL:-EXP:-SRP:-SEED:-CAMELLIA:-DSS:-RC2:-DES-CBC-MD5:-DES-CBC3-MD5");
733 
734         int size = allOpenSSLCipherSuites.length;
735         SUPPORTED_CIPHER_SUITES = new String[size + 2];
736         for (int i = 0; i < size; i++) {
737             String standardName = OPENSSL_TO_STANDARD_CIPHER_SUITES.get(allOpenSSLCipherSuites[i]);
738             if (standardName == null) {
739                 throw new IllegalArgumentException("Unknown cipher suite supported by native code: " +
740                                                    allOpenSSLCipherSuites[i]);
741             }
742             SUPPORTED_CIPHER_SUITES[i] = standardName;
743             SUPPORTED_CIPHER_SUITES_SET.add(standardName);
744         }
745         SUPPORTED_CIPHER_SUITES[size] = TLS_EMPTY_RENEGOTIATION_INFO_SCSV;
746         SUPPORTED_CIPHER_SUITES[size + 1] = TLS_FALLBACK_SCSV;
747     }
748 
SSL_CTX_new()749     public static native long SSL_CTX_new();
750 
751     // IMPLEMENTATION NOTE: The default list of cipher suites is a trade-off between what we'd like
752     // to use and what servers currently support. We strive to be secure enough by default. We thus
753     // avoid unacceptably weak suites (e.g., those with bulk cipher secret key shorter than 128
754     // bits), while maintaining the capability to connect to the majority of servers.
755     //
756     // Cipher suites are listed in preference order (favorite choice first) of the client. However,
757     // servers are not required to honor the order. The key rules governing the preference order
758     // are:
759     // * Prefer Forward Secrecy (i.e., cipher suites that use ECDHE and DHE for key agreement).
760     // * Prefer AES-GCM to AES-CBC whose MAC-pad-then-encrypt approach leads to weaknesses (e.g.,
761     //   Lucky 13).
762     // * Prefer AES to RC4 whose foundations are a bit shaky. See http://www.isg.rhul.ac.uk/tls/.
763     //   BEAST and Lucky13 mitigations are enabled.
764     // * Prefer 128-bit bulk encryption to 256-bit one, because 128-bit is safe enough while
765     //   consuming less CPU/time/energy.
766     //
767     // NOTE: Removing cipher suites from this list needs to be done with caution, because this may
768     // prevent apps from connecting to servers they were previously able to connect to.
769 
770     /** X.509 based cipher suites enabled by default (if requested), in preference order. */
771     static final String[] DEFAULT_X509_CIPHER_SUITES = new String[] {
772         "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256",
773         "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384",
774         "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256",
775         "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384",
776         "TLS_DHE_RSA_WITH_AES_128_GCM_SHA256",
777         "TLS_DHE_RSA_WITH_AES_256_GCM_SHA384",
778         "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA",
779         "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA",
780         "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA",
781         "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA",
782         "TLS_DHE_RSA_WITH_AES_128_CBC_SHA",
783         "TLS_DHE_RSA_WITH_AES_256_CBC_SHA",
784         "TLS_ECDHE_ECDSA_WITH_RC4_128_SHA",
785         "TLS_ECDHE_RSA_WITH_RC4_128_SHA",
786         "TLS_RSA_WITH_AES_128_GCM_SHA256",
787         "TLS_RSA_WITH_AES_256_GCM_SHA384",
788         "TLS_RSA_WITH_AES_128_CBC_SHA",
789         "TLS_RSA_WITH_AES_256_CBC_SHA",
790         "SSL_RSA_WITH_RC4_128_SHA",
791     };
792 
793     /** TLS-PSK cipher suites enabled by default (if requested), in preference order. */
794     static final String[] DEFAULT_PSK_CIPHER_SUITES = new String[] {
795         "TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA",
796         "TLS_ECDHE_PSK_WITH_AES_256_CBC_SHA",
797         "TLS_PSK_WITH_AES_128_CBC_SHA",
798         "TLS_PSK_WITH_AES_256_CBC_SHA",
799     };
800 
getSupportedCipherSuites()801     public static String[] getSupportedCipherSuites() {
802         return SUPPORTED_CIPHER_SUITES.clone();
803     }
804 
SSL_CTX_free(long ssl_ctx)805     public static native void SSL_CTX_free(long ssl_ctx);
806 
SSL_CTX_set_session_id_context(long ssl_ctx, byte[] sid_ctx)807     public static native void SSL_CTX_set_session_id_context(long ssl_ctx, byte[] sid_ctx);
808 
SSL_new(long ssl_ctx)809     public static native long SSL_new(long ssl_ctx) throws SSLException;
810 
SSL_enable_tls_channel_id(long ssl)811     public static native void SSL_enable_tls_channel_id(long ssl) throws SSLException;
812 
SSL_get_tls_channel_id(long ssl)813     public static native byte[] SSL_get_tls_channel_id(long ssl) throws SSLException;
814 
SSL_set1_tls_channel_id(long ssl, NativeRef.EVP_PKEY pkey)815     public static native void SSL_set1_tls_channel_id(long ssl, NativeRef.EVP_PKEY pkey);
816 
SSL_use_certificate(long ssl, long[] x509refs)817     public static native void SSL_use_certificate(long ssl, long[] x509refs);
818 
SSL_use_PrivateKey(long ssl, NativeRef.EVP_PKEY pkey)819     public static native void SSL_use_PrivateKey(long ssl, NativeRef.EVP_PKEY pkey);
820 
SSL_check_private_key(long ssl)821     public static native void SSL_check_private_key(long ssl) throws SSLException;
822 
SSL_set_client_CA_list(long ssl, byte[][] asn1DerEncodedX500Principals)823     public static native void SSL_set_client_CA_list(long ssl, byte[][] asn1DerEncodedX500Principals);
824 
SSL_get_mode(long ssl)825     public static native long SSL_get_mode(long ssl);
826 
SSL_set_mode(long ssl, long mode)827     public static native long SSL_set_mode(long ssl, long mode);
828 
SSL_clear_mode(long ssl, long mode)829     public static native long SSL_clear_mode(long ssl, long mode);
830 
SSL_get_options(long ssl)831     public static native long SSL_get_options(long ssl);
832 
SSL_set_options(long ssl, long options)833     public static native long SSL_set_options(long ssl, long options);
834 
SSL_clear_options(long ssl, long options)835     public static native long SSL_clear_options(long ssl, long options);
836 
SSL_use_psk_identity_hint(long ssl, String identityHint)837     public static native void SSL_use_psk_identity_hint(long ssl, String identityHint)
838             throws SSLException;
839 
set_SSL_psk_client_callback_enabled(long ssl, boolean enabled)840     public static native void set_SSL_psk_client_callback_enabled(long ssl, boolean enabled);
841 
set_SSL_psk_server_callback_enabled(long ssl, boolean enabled)842     public static native void set_SSL_psk_server_callback_enabled(long ssl, boolean enabled);
843 
844     /** Protocols to enable by default when "TLSv1.2" is requested. */
845     public static final String[] TLSV12_PROTOCOLS = new String[] {
846         SUPPORTED_PROTOCOL_TLSV1,
847         SUPPORTED_PROTOCOL_TLSV1_1,
848         SUPPORTED_PROTOCOL_TLSV1_2,
849     };
850 
851     /** Protocols to enable by default when "TLSv1.1" is requested. */
852     public static final String[] TLSV11_PROTOCOLS = new String[] {
853         SUPPORTED_PROTOCOL_TLSV1,
854         SUPPORTED_PROTOCOL_TLSV1_1,
855         SUPPORTED_PROTOCOL_TLSV1_2,
856     };
857 
858     /** Protocols to enable by default when "TLSv1" is requested. */
859     public static final String[] TLSV1_PROTOCOLS  = new String[] {
860         SUPPORTED_PROTOCOL_TLSV1,
861         SUPPORTED_PROTOCOL_TLSV1_1,
862         SUPPORTED_PROTOCOL_TLSV1_2,
863     };
864 
865     /** Protocols to enable by default when "SSLv3" is requested. */
866     public static final String[] SSLV3_PROTOCOLS  = new String[] {
867         SUPPORTED_PROTOCOL_SSLV3,
868         SUPPORTED_PROTOCOL_TLSV1,
869         SUPPORTED_PROTOCOL_TLSV1_1,
870         SUPPORTED_PROTOCOL_TLSV1_2,
871     };
872 
873     public static final String[] DEFAULT_PROTOCOLS = TLSV12_PROTOCOLS;
874 
getSupportedProtocols()875     public static String[] getSupportedProtocols() {
876         return new String[] { SUPPORTED_PROTOCOL_SSLV3,
877                               SUPPORTED_PROTOCOL_TLSV1,
878                               SUPPORTED_PROTOCOL_TLSV1_1,
879                               SUPPORTED_PROTOCOL_TLSV1_2,
880         };
881     }
882 
setEnabledProtocols(long ssl, String[] protocols)883     public static void setEnabledProtocols(long ssl, String[] protocols) {
884         checkEnabledProtocols(protocols);
885         // openssl uses negative logic letting you disable protocols.
886         // so first, assume we need to set all (disable all) and clear none (enable none).
887         // in the loop, selectively move bits from set to clear (from disable to enable)
888         long optionsToSet = (NativeConstants.SSL_OP_NO_SSLv3 | NativeConstants.SSL_OP_NO_TLSv1 | NativeConstants.SSL_OP_NO_TLSv1_1 | NativeConstants.SSL_OP_NO_TLSv1_2);
889         long optionsToClear = 0;
890         for (int i = 0; i < protocols.length; i++) {
891             String protocol = protocols[i];
892             if (protocol.equals(SUPPORTED_PROTOCOL_SSLV3)) {
893                 optionsToSet &= ~NativeConstants.SSL_OP_NO_SSLv3;
894                 optionsToClear |= NativeConstants.SSL_OP_NO_SSLv3;
895             } else if (protocol.equals(SUPPORTED_PROTOCOL_TLSV1)) {
896                 optionsToSet &= ~NativeConstants.SSL_OP_NO_TLSv1;
897                 optionsToClear |= NativeConstants.SSL_OP_NO_TLSv1;
898             } else if (protocol.equals(SUPPORTED_PROTOCOL_TLSV1_1)) {
899                 optionsToSet &= ~NativeConstants.SSL_OP_NO_TLSv1_1;
900                 optionsToClear |= NativeConstants.SSL_OP_NO_TLSv1_1;
901             } else if (protocol.equals(SUPPORTED_PROTOCOL_TLSV1_2)) {
902                 optionsToSet &= ~NativeConstants.SSL_OP_NO_TLSv1_2;
903                 optionsToClear |= NativeConstants.SSL_OP_NO_TLSv1_2;
904             } else {
905                 // error checked by checkEnabledProtocols
906                 throw new IllegalStateException();
907             }
908         }
909 
910         SSL_set_options(ssl, optionsToSet);
911         SSL_clear_options(ssl, optionsToClear);
912     }
913 
checkEnabledProtocols(String[] protocols)914     public static String[] checkEnabledProtocols(String[] protocols) {
915         if (protocols == null) {
916             throw new IllegalArgumentException("protocols == null");
917         }
918         for (int i = 0; i < protocols.length; i++) {
919             String protocol = protocols[i];
920             if (protocol == null) {
921                 throw new IllegalArgumentException("protocols[" + i + "] == null");
922             }
923             if ((!protocol.equals(SUPPORTED_PROTOCOL_SSLV3))
924                     && (!protocol.equals(SUPPORTED_PROTOCOL_TLSV1))
925                     && (!protocol.equals(SUPPORTED_PROTOCOL_TLSV1_1))
926                     && (!protocol.equals(SUPPORTED_PROTOCOL_TLSV1_2))) {
927                 throw new IllegalArgumentException("protocol " + protocol
928                                                    + " is not supported");
929             }
930         }
931         return protocols;
932     }
933 
SSL_set_cipher_lists(long ssl, String[] ciphers)934     public static native void SSL_set_cipher_lists(long ssl, String[] ciphers);
935 
936     /**
937      * Gets the list of cipher suites enabled for the provided {@code SSL} instance.
938      *
939      * @return array of {@code SSL_CIPHER} references.
940      */
SSL_get_ciphers(long ssl)941     public static native long[] SSL_get_ciphers(long ssl);
942 
get_SSL_CIPHER_algorithm_mkey(long sslCipher)943     public static native int get_SSL_CIPHER_algorithm_mkey(long sslCipher);
get_SSL_CIPHER_algorithm_auth(long sslCipher)944     public static native int get_SSL_CIPHER_algorithm_auth(long sslCipher);
945 
setEnabledCipherSuites(long ssl, String[] cipherSuites)946     public static void setEnabledCipherSuites(long ssl, String[] cipherSuites) {
947         checkEnabledCipherSuites(cipherSuites);
948         List<String> opensslSuites = new ArrayList<String>();
949         for (int i = 0; i < cipherSuites.length; i++) {
950             String cipherSuite = cipherSuites[i];
951             if (cipherSuite.equals(TLS_EMPTY_RENEGOTIATION_INFO_SCSV)) {
952                 continue;
953             }
954             if (cipherSuite.equals(TLS_FALLBACK_SCSV)) {
955                 SSL_set_mode(ssl, NativeConstants.SSL_MODE_SEND_FALLBACK_SCSV);
956                 continue;
957             }
958             String openssl = STANDARD_TO_OPENSSL_CIPHER_SUITES.get(cipherSuite);
959             String cs = (openssl == null) ? cipherSuite : openssl;
960             opensslSuites.add(cs);
961         }
962         SSL_set_cipher_lists(ssl, opensslSuites.toArray(new String[opensslSuites.size()]));
963     }
964 
checkEnabledCipherSuites(String[] cipherSuites)965     public static String[] checkEnabledCipherSuites(String[] cipherSuites) {
966         if (cipherSuites == null) {
967             throw new IllegalArgumentException("cipherSuites == null");
968         }
969         // makes sure all suites are valid, throwing on error
970         for (int i = 0; i < cipherSuites.length; i++) {
971             String cipherSuite = cipherSuites[i];
972             if (cipherSuite == null) {
973                 throw new IllegalArgumentException("cipherSuites[" + i + "] == null");
974             }
975             if (cipherSuite.equals(TLS_EMPTY_RENEGOTIATION_INFO_SCSV) ||
976                     cipherSuite.equals(TLS_FALLBACK_SCSV)) {
977                 continue;
978             }
979             if (SUPPORTED_CIPHER_SUITES_SET.contains(cipherSuite)) {
980                 continue;
981             }
982 
983             // For backwards compatibility, it's allowed for |cipherSuite| to
984             // be an OpenSSL-style cipher-suite name.
985             String standardName = OPENSSL_TO_STANDARD_CIPHER_SUITES.get(cipherSuite);
986             if (standardName != null && SUPPORTED_CIPHER_SUITES_SET.contains(standardName)) {
987                 // TODO log warning about using backward compatability
988                 continue;
989             }
990             throw new IllegalArgumentException("cipherSuite " + cipherSuite + " is not supported.");
991         }
992         return cipherSuites;
993     }
994 
995     /*
996      * See the OpenSSL ssl.h header file for more information.
997      */
998     public static final int SSL_VERIFY_NONE =                 0x00;
999     public static final int SSL_VERIFY_PEER =                 0x01;
1000     public static final int SSL_VERIFY_FAIL_IF_NO_PEER_CERT = 0x02;
1001 
SSL_set_accept_state(long sslNativePointer)1002     public static native void SSL_set_accept_state(long sslNativePointer);
1003 
SSL_set_connect_state(long sslNativePointer)1004     public static native void SSL_set_connect_state(long sslNativePointer);
1005 
SSL_set_verify(long sslNativePointer, int mode)1006     public static native void SSL_set_verify(long sslNativePointer, int mode);
1007 
SSL_set_session(long sslNativePointer, long sslSessionNativePointer)1008     public static native void SSL_set_session(long sslNativePointer, long sslSessionNativePointer)
1009         throws SSLException;
1010 
SSL_set_session_creation_enabled( long sslNativePointer, boolean creationEnabled)1011     public static native void SSL_set_session_creation_enabled(
1012             long sslNativePointer, boolean creationEnabled) throws SSLException;
1013 
SSL_session_reused(long sslNativePointer)1014     public static native boolean SSL_session_reused(long sslNativePointer);
1015 
SSL_set_reject_peer_renegotiations( long sslNativePointer, boolean renegotiationRejected)1016     public static native void SSL_set_reject_peer_renegotiations(
1017             long sslNativePointer, boolean renegotiationRejected) throws SSLException;
1018 
SSL_set_tlsext_host_name(long sslNativePointer, String hostname)1019     public static native void SSL_set_tlsext_host_name(long sslNativePointer, String hostname)
1020             throws SSLException;
SSL_get_servername(long sslNativePointer)1021     public static native String SSL_get_servername(long sslNativePointer);
1022 
1023     /**
1024      * Enables NPN for all SSL connections in the context.
1025      *
1026      * <p>For clients this causes the NPN extension to be included in the
1027      * ClientHello message.
1028      *
1029      * <p>For servers this causes the NPN extension to be included in the
1030      * ServerHello message. The NPN extension will not be included in the
1031      * ServerHello response if the client didn't include it in the ClientHello
1032      * request.
1033      *
1034      * <p>In either case the caller should pass a non-null byte array of NPN
1035      * protocols to {@link #SSL_do_handshake}.
1036      */
SSL_CTX_enable_npn(long sslCtxNativePointer)1037     public static native void SSL_CTX_enable_npn(long sslCtxNativePointer);
1038 
1039     /**
1040      * Disables NPN for all SSL connections in the context.
1041      */
SSL_CTX_disable_npn(long sslCtxNativePointer)1042     public static native void SSL_CTX_disable_npn(long sslCtxNativePointer);
1043 
1044     /**
1045      * For clients, sets the list of supported ALPN protocols in wire-format
1046      * (length-prefixed 8-bit strings).
1047      */
SSL_set_alpn_protos(long sslPointer, byte[] protos)1048     public static native int SSL_set_alpn_protos(long sslPointer, byte[] protos);
1049 
1050     /**
1051      * Returns the selected ALPN protocol. If the server did not select a
1052      * protocol, {@code null} will be returned.
1053      */
SSL_get0_alpn_selected(long sslPointer)1054     public static native byte[] SSL_get0_alpn_selected(long sslPointer);
1055 
1056     /**
1057      * Returns the sslSessionNativePointer of the negotiated session. If this is
1058      * a server negotiation, supplying the {@code alpnProtocols} will enable
1059      * ALPN negotiation.
1060      */
SSL_do_handshake(long sslNativePointer, FileDescriptor fd, SSLHandshakeCallbacks shc, int timeoutMillis, boolean client_mode, byte[] npnProtocols, byte[] alpnProtocols)1061     public static native long SSL_do_handshake(long sslNativePointer,
1062                                                FileDescriptor fd,
1063                                                SSLHandshakeCallbacks shc,
1064                                                int timeoutMillis,
1065                                                boolean client_mode,
1066                                                byte[] npnProtocols,
1067                                                byte[] alpnProtocols)
1068         throws SSLException, SocketTimeoutException, CertificateException;
1069 
1070     /**
1071      * Returns the sslSessionNativePointer of the negotiated session. If this is
1072      * a server negotiation, supplying the {@code alpnProtocols} will enable
1073      * ALPN negotiation.
1074      */
SSL_do_handshake_bio(long sslNativePointer, long sourceBioRef, long sinkBioRef, SSLHandshakeCallbacks shc, boolean client_mode, byte[] npnProtocols, byte[] alpnProtocols)1075     public static native long SSL_do_handshake_bio(long sslNativePointer,
1076                                                    long sourceBioRef,
1077                                                    long sinkBioRef,
1078                                                    SSLHandshakeCallbacks shc,
1079                                                    boolean client_mode,
1080                                                    byte[] npnProtocols,
1081                                                    byte[] alpnProtocols)
1082         throws SSLException, SocketTimeoutException, CertificateException;
1083 
SSL_get_npn_negotiated_protocol(long sslNativePointer)1084     public static native byte[] SSL_get_npn_negotiated_protocol(long sslNativePointer);
1085 
1086     /**
1087      * Currently only intended for forcing renegotiation for testing.
1088      * Not used within OpenSSLSocketImpl.
1089      */
SSL_renegotiate(long sslNativePointer)1090     public static native void SSL_renegotiate(long sslNativePointer) throws SSLException;
1091 
1092     /**
1093      * Returns the local X509 certificate references. Must X509_free when done.
1094      */
SSL_get_certificate(long sslNativePointer)1095     public static native long[] SSL_get_certificate(long sslNativePointer);
1096 
1097     /**
1098      * Returns the peer X509 certificate references. Must X509_free when done.
1099      */
SSL_get_peer_cert_chain(long sslNativePointer)1100     public static native long[] SSL_get_peer_cert_chain(long sslNativePointer);
1101 
1102     /**
1103      * Reads with the native SSL_read function from the encrypted data stream
1104      * @return -1 if error or the end of the stream is reached.
1105      */
SSL_read(long sslNativePointer, FileDescriptor fd, SSLHandshakeCallbacks shc, byte[] b, int off, int len, int readTimeoutMillis)1106     public static native int SSL_read(long sslNativePointer,
1107                                       FileDescriptor fd,
1108                                       SSLHandshakeCallbacks shc,
1109                                       byte[] b, int off, int len, int readTimeoutMillis)
1110         throws IOException;
1111 
SSL_read_BIO(long sslNativePointer, byte[] dest, int destOffset, int destLength, long sourceBioRef, long sinkBioRef, SSLHandshakeCallbacks shc)1112     public static native int SSL_read_BIO(long sslNativePointer,
1113                                           byte[] dest,
1114                                           int destOffset,
1115                                           int destLength,
1116                                           long sourceBioRef,
1117                                           long sinkBioRef,
1118                                           SSLHandshakeCallbacks shc)
1119         throws IOException;
1120 
1121     /**
1122      * Writes with the native SSL_write function to the encrypted data stream.
1123      */
SSL_write(long sslNativePointer, FileDescriptor fd, SSLHandshakeCallbacks shc, byte[] b, int off, int len, int writeTimeoutMillis)1124     public static native void SSL_write(long sslNativePointer,
1125                                         FileDescriptor fd,
1126                                         SSLHandshakeCallbacks shc,
1127                                         byte[] b, int off, int len, int writeTimeoutMillis)
1128         throws IOException;
1129 
SSL_write_BIO(long sslNativePointer, byte[] source, int length, long sinkBioRef, SSLHandshakeCallbacks shc)1130     public static native int SSL_write_BIO(long sslNativePointer,
1131                                            byte[] source,
1132                                            int length,
1133                                            long sinkBioRef,
1134                                            SSLHandshakeCallbacks shc)
1135         throws IOException;
1136 
SSL_interrupt(long sslNativePointer)1137     public static native void SSL_interrupt(long sslNativePointer);
SSL_shutdown(long sslNativePointer, FileDescriptor fd, SSLHandshakeCallbacks shc)1138     public static native void SSL_shutdown(long sslNativePointer,
1139                                            FileDescriptor fd,
1140                                            SSLHandshakeCallbacks shc) throws IOException;
1141 
SSL_shutdown_BIO(long sslNativePointer, long sourceBioRef, long sinkBioRef, SSLHandshakeCallbacks shc)1142     public static native void SSL_shutdown_BIO(long sslNativePointer,
1143                                                long sourceBioRef, long sinkBioRef,
1144                                                SSLHandshakeCallbacks shc) throws IOException;
1145 
SSL_get_shutdown(long sslNativePointer)1146     public static native int SSL_get_shutdown(long sslNativePointer);
1147 
SSL_free(long sslNativePointer)1148     public static native void SSL_free(long sslNativePointer);
1149 
SSL_SESSION_session_id(long sslSessionNativePointer)1150     public static native byte[] SSL_SESSION_session_id(long sslSessionNativePointer);
1151 
SSL_SESSION_get_time(long sslSessionNativePointer)1152     public static native long SSL_SESSION_get_time(long sslSessionNativePointer);
1153 
SSL_SESSION_get_version(long sslSessionNativePointer)1154     public static native String SSL_SESSION_get_version(long sslSessionNativePointer);
1155 
SSL_SESSION_cipher(long sslSessionNativePointer)1156     public static native String SSL_SESSION_cipher(long sslSessionNativePointer);
1157 
SSL_SESSION_free(long sslSessionNativePointer)1158     public static native void SSL_SESSION_free(long sslSessionNativePointer);
1159 
i2d_SSL_SESSION(long sslSessionNativePointer)1160     public static native byte[] i2d_SSL_SESSION(long sslSessionNativePointer);
1161 
d2i_SSL_SESSION(byte[] data)1162     public static native long d2i_SSL_SESSION(byte[] data);
1163 
1164     /**
1165      * A collection of callbacks from the native OpenSSL code that are
1166      * related to the SSL handshake initiated by SSL_do_handshake.
1167      */
1168     public interface SSLHandshakeCallbacks {
1169         /**
1170          * Verify that we trust the certificate chain is trusted.
1171          *
1172          * @param sslSessionNativePtr pointer to a reference of the SSL_SESSION
1173          * @param certificateChainRefs chain of X.509 certificate references
1174          * @param authMethod auth algorithm name
1175          *
1176          * @throws CertificateException if the certificate is untrusted
1177          */
verifyCertificateChain(long sslSessionNativePtr, long[] certificateChainRefs, String authMethod)1178         public void verifyCertificateChain(long sslSessionNativePtr, long[] certificateChainRefs,
1179                 String authMethod) throws CertificateException;
1180 
1181         /**
1182          * Called on an SSL client when the server requests (or
1183          * requires a certificate). The client can respond by using
1184          * SSL_use_certificate and SSL_use_PrivateKey to set a
1185          * certificate if has an appropriate one available, similar to
1186          * how the server provides its certificate.
1187          *
1188          * @param keyTypes key types supported by the server,
1189          * convertible to strings with #keyType
1190          * @param asn1DerEncodedX500Principals CAs known to the server
1191          */
clientCertificateRequested(byte[] keyTypes, byte[][] asn1DerEncodedX500Principals)1192         public void clientCertificateRequested(byte[] keyTypes,
1193                                                byte[][] asn1DerEncodedX500Principals)
1194             throws CertificateEncodingException, SSLException;
1195 
1196         /**
1197          * Gets the key to be used in client mode for this connection in Pre-Shared Key (PSK) key
1198          * exchange.
1199          *
1200          * @param identityHint PSK identity hint provided by the server or {@code null} if no hint
1201          *        provided.
1202          * @param identity buffer to be populated with PSK identity (NULL-terminated modified UTF-8)
1203          *        by this method. This identity will be provided to the server.
1204          * @param key buffer to be populated with key material by this method.
1205          *
1206          * @return number of bytes this method stored in the {@code key} buffer or {@code 0} if an
1207          *         error occurred in which case the handshake will be aborted.
1208          */
clientPSKKeyRequested(String identityHint, byte[] identity, byte[] key)1209         public int clientPSKKeyRequested(String identityHint, byte[] identity, byte[] key);
1210 
1211         /**
1212          * Gets the key to be used in server mode for this connection in Pre-Shared Key (PSK) key
1213          * exchange.
1214          *
1215          * @param identityHint PSK identity hint provided by this server to the client or
1216          *        {@code null} if no hint was provided.
1217          * @param identity PSK identity provided by the client.
1218          * @param key buffer to be populated with key material by this method.
1219          *
1220          * @return number of bytes this method stored in the {@code key} buffer or {@code 0} if an
1221          *         error occurred in which case the handshake will be aborted.
1222          */
serverPSKKeyRequested(String identityHint, String identity, byte[] key)1223         public int serverPSKKeyRequested(String identityHint, String identity, byte[] key);
1224 
1225         /**
1226          * Called when SSL state changes. This could be handshake completion.
1227          */
onSSLStateChange(long sslSessionNativePtr, int type, int val)1228         public void onSSLStateChange(long sslSessionNativePtr, int type, int val);
1229     }
1230 
ERR_peek_last_error()1231     public static native long ERR_peek_last_error();
1232 
SSL_CIPHER_get_kx_name(long cipherAddress)1233     public static native String SSL_CIPHER_get_kx_name(long cipherAddress);
1234 
get_cipher_names(String selection)1235     public static native String[] get_cipher_names(String selection);
1236 }
1237