Automatic management of OpenSSL error stack

This removes some complexity in remembering to free the OpenSSL error
stack. If you forget, the error will stick around until you make another
call.

Change-Id: I245a525dcc93077b2bf9909a14a0ef469a2daca4
Relax checks for key vs cert for wrapped keys

If a key is a wrapped platform key, we must relax the check. The reason
is that we may not have the public values we need to pass the
EVP_PKEY_cmp checks that this does.

Change-Id: I7ab2be51b0968a9cf771edea01d33fe2367c8185
Merge "Automatic management of OpenSSL error stack"
Revert "Automatic management of OpenSSL error stack"

This reverts commit 35666e4cb0fcd063a21d17eebbb571b4e4e822b8.

Change-Id: I926d159c4c4b99250caef750732976c1e601e9ef
Merge "Revert "Automatic management of OpenSSL error stack""
Fix debugging with unbundled conscrypt

When JNI_TRACE was enabled, there were missing defines for the debugging
code since no platform code is included.

Also clang complains about more of the debugging statement formats, so
we have to move some things around to get it to be happy.

Change-Id: I1a6695c2ef2639cc01cfc3d3a8603f010c659844
Revert "Revert "Automatic management of OpenSSL error stack""

The "else" statement in OpenSslError::reset wasn't properly resetting
the error state which made a second call into sslRead jump into
sslSelect when it should have just returned immediately.

Change-Id: I22e8025c0497a04e78daa07cef78191a6ca1a70c
OpenSSLSocketImpl: Move state checks inside mutex

Checking the state of the connection is unreliable if SSL_read and
SSL_write are happening in another thread. Move the state checks inside
our application mutex so we don't run into another thread mutating the
state at the same time.

Bug: 15606096
Change-Id: I5ecdeb1551a13098d1b66c5e4009607c9951fa38
OpenSSLEngineImpl: reduce number of copies needed

When the ByteBuffer didn't line up exactly with the backing array, it
would allocate a new buffer to write into. Instead, add the ability for
OpenSSL to read at an offset in the given array so a copy isn't needed.

Change-Id: I149d3f94e4b5cbdc010df80439ae3300cbdc87a5
Use consistent naming for SSLSocket arguments

This changes all the 'host' to be 'hostname' and anything that takes an
'InetAddress' will have the name of 'address' to avoid confusing it with
a hostname.

Bug: 16658420
Bug: 17059757
Change-Id: Iac0628d2d156023dbb80c2e636af6bfe63f46650
Rename hostname fields and methods to reflect usage

The hostname that was supplied when the socket was created is stored as
the "peerHostname"  This is the only one that should be used for Server
Name Indication (SNI) purposes.

The "peerHostname" or the resolved IP address may be used for
certificate validation, so keep the use of "getHostname()" for
cerificate validation.

Bug: 16658420
Bug: 17059757
Change-Id: Ifd87dead44fb2f00bbfd5eac7e69fb3fc98e94b4
Implement write socket timeouts for unbundled apps

Change-Id: I4fd604f057ba4288d4f31bf6b3b93307376023d5
Validate hostname is usable for SNI

According to RFC 6066 section 3, the hostname listed in the Server Name
Indication (SNI) field is a fully qualified domain name and IP
addresses are not permitted.

Bug: 16658420
Bug: 17059757
Change-Id: I804e46b6e66599b2770f0f4f0534467987e51208
Read property to enable SNI

Read the system property "jsse.enableSNIExtension" on whether to enable
Server Name Indication (SNI) extension. For unbundled builds, this will
be enabled by default. For platform builds, this will be disabled by
default.

Bug: 16658420
Bug: 17059757
Change-Id: I774f5406bf3fe601a42c4ef5e708b31800147eb9
Add pre-Honeycomb literal IP matching

This will allow us to run this code on Gingerbread devices and others
that don't have the InetAddress#isNumeric API.

Bug: 16658420
Bug: 17059757
Change-Id: I597d539979d58eeaa2677d6f99e911313a550cc1
Comment out unused parameters and function.

BUG: 17281763
Change-Id: I6874dc0ef6ddaba66d465c07cabeb5e781337716
Reject TLS/SSL certificates with very short (EC)DSA keys.

TLS/SSL certificates that use RSA keys with modulus shorter than 1024
bits are already being rejected (see
https://android-review.googlesource.com/#/c/44621/). This CL adds
similar minimum length checks for (EC)DSA keys.

The cutoff is at the 80-bit symmetric strength equivalent from NIST
800-57:
* RSA: modulus length: 1024 bits.
* ECDSA: field size: 160 bits.
* DSA: p: 1024 bits, q: 160 bits.

Bug: 17177978
Change-Id: Ifbb4a759494665e614bd89cd4bf7fc4d69d27532
Fix the ENGINE_finish/ENGINE_free mixup

The tests were calling finish when it meant free. This caused tests to
segmentation fault next time the ENGINE was looked up in the dynamic
engine list.

(cherry picked from commit 984b7ec6f5aab314117949a48e448ff4f6b65f16)

Bug: 14994037
Change-Id: If7379fee26f7e79fa0b43104ac9d13b4ffb62ba8