blob: ca9f6a4446156b26fda16027fd538be19dba4c04 [file] [log] [blame] [view]
David Benjamin65703b42015-09-02 16:51:42 -04001# Porting from OpenSSL to BoringSSL
2
3BoringSSL is an OpenSSL derivative and is mostly source-compatible, for the
4subset of OpenSSL retained. Libraries ideally need little to no changes for
5BoringSSL support, provided they do not use removed APIs. In general, see if the
6library compiles and, on failure, consult the documentation in the header files
7and see if problematic features can be removed.
8
9In some cases, BoringSSL-specific code may be necessary. In that case, the
10`OPENSSL_IS_BORINGSSL` preprocessor macro may be used in `#ifdef`s. This macro
11should also be used in lieu of the presence of any particular function to detect
12OpenSSL vs BoringSSL in configure scripts, etc., where those are necessary.
David Benjamin707720c2016-08-11 11:04:15 -040013Before using the preprocessor, however, contact the BoringSSL maintainers about
14the missing APIs. If not an intentionally removed feature, BoringSSL will
15typically add compatibility functions for convenience.
David Benjamin65703b42015-09-02 16:51:42 -040016
17For convenience, BoringSSL defines upstream's `OPENSSL_NO_*` feature macros
18corresponding to removed features. These may also be used to disable code which
19uses a removed feature.
20
21Note: BoringSSL does *not* have a stable API or ABI. It must be updated with its
22consumers. It is not suitable for, say, a system library in a traditional Linux
23distribution. For instance, Chromium statically links the specific revision of
24BoringSSL it was built against. Likewise, Android's system-internal copy of
25BoringSSL is not exposed by the NDK and must not be used by third-party
26applications.
27
28
29## Major API changes
30
31### Integer types
32
33Some APIs have been converted to use `size_t` for consistency and to avoid
34integer overflows at the API boundary. (Existing logic uses a mismash of `int`,
35`long`, and `unsigned`.) For the most part, implicit casts mean that existing
36code continues to compile. In some cases, this may require BoringSSL-specific
37code, particularly to avoid compiler warnings.
38
39Most notably, the `STACK_OF(T)` types have all been converted to use `size_t`
40instead of `int` for indices and lengths.
41
42### Reference counts
43
44Some external consumers increment reference counts directly by calling
45`CRYPTO_add` with the corresponding `CRYPTO_LOCK_*` value.
46
47These APIs no longer exist in BoringSSL. Instead, code which increments
48reference counts should call the corresponding `FOO_up_ref` function, such as
49`EVP_PKEY_up_ref`. Note that not all of these APIs are present in OpenSSL and
50may require `#ifdef`s.
51
52### Error codes
53
54OpenSSL's errors are extremely specific, leaking internals of the library,
55including even a function code for the function which emitted the error! As some
56logic in BoringSSL has been rewritten, code which conditions on the error may
57break (grep for `ERR_GET_REASON` and `ERR_GET_FUNC`). This danger also exists
58when upgrading OpenSSL versions.
59
60Where possible, avoid conditioning on the exact error reason. Otherwise, a
61BoringSSL `#ifdef` may be necessary. Exactly how best to resolve this issue is
62still being determined. It's possible some new APIs will be added in the future.
63
64Function codes have been completely removed. Remove code which conditions on
65these as it will break with the slightest change in the library, OpenSSL or
66BoringSSL.
67
68### `*_ctrl` functions
69
70Some OpenSSL APIs are implemented with `ioctl`-style functions such as
71`SSL_ctrl` and `EVP_PKEY_CTX_ctrl`, combined with convenience macros, such as
72
73 # define SSL_CTX_set_mode(ctx,op) \
74 SSL_CTX_ctrl((ctx),SSL_CTRL_MODE,(op),NULL)
75
76In BoringSSL, these macros have been replaced with proper functions. The
77underlying `_ctrl` functions have been removed.
78
79For convenience, `SSL_CTRL_*` values are retained as macros to `doesnt_exist` so
80existing code which uses them (or the wrapper macros) in `#ifdef` expressions
81will continue to function. However, the macros themselves will not work.
82
83Switch any `*_ctrl` callers to the macro/function versions. This works in both
84OpenSSL and BoringSSL. Note that BoringSSL's function versions will be
David Benjamind8ba86d2016-07-16 00:29:27 +020085type-checked and may require more care with types. See the end of this
86document for a table of functions to use.
David Benjamin65703b42015-09-02 16:51:42 -040087
88### HMAC `EVP_PKEY`s
89
90`EVP_PKEY_HMAC` is removed. Use the `HMAC_*` functions in `hmac.h` instead. This
91is compatible with OpenSSL.
92
93### DSA `EVP_PKEY`s
94
95`EVP_PKEY_DSA` is deprecated. It is currently still possible to parse DER into a
96DSA `EVP_PKEY`, but signing or verifying with those objects will not work.
97
98### DES
99
100The `DES_cblock` type has been switched from an array to a struct to avoid the
101pitfalls around array types in C. Where features which require DES cannot be
102disabled, BoringSSL-specific codepaths may be necessary.
103
104### TLS renegotiation
105
106OpenSSL enables TLS renegotiation by default and accepts renegotiation requests
107from the peer transparently. Renegotiation is an extremely problematic protocol
108feature, so BoringSSL rejects peer renegotiations by default.
109
David Benjamin471abb12015-10-18 23:09:17 -0400110To enable renegotiation, call `SSL_set_renegotiate_mode` and set it to
111`ssl_renegotiate_once` or `ssl_renegotiate_freely`. Renegotiation is only
112supported as a client in SSL3/TLS and the HelloRequest must be received at a
113quiet point in the application protocol. This is sufficient to support the
114common use of requesting a new client certificate between an HTTP request and
115response in (unpipelined) HTTP/1.1.
David Benjamin65703b42015-09-02 16:51:42 -0400116
117Things which do not work:
118
Adam Langleya6b86892017-07-25 13:37:51 -0700119* There is no support for renegotiation as a server. (Attempts by clients will
120 result in a fatal alert so that ClientHello messages cannot be used to flood
121 a server and escape higher-level limits.)
David Benjamin65703b42015-09-02 16:51:42 -0400122
123* There is no support for renegotiation in DTLS.
124
David Benjamin809829e2015-09-03 11:48:12 -0400125* There is no support for initiating renegotiation; `SSL_renegotiate` always
126 fails and `SSL_set_state` does nothing.
127
David Benjamin65703b42015-09-02 16:51:42 -0400128* Interleaving application data with the new handshake is forbidden.
129
130* If a HelloRequest is received while `SSL_write` has unsent application data,
131 the renegotiation is rejected.
132
David Benjamin9a798ec2015-09-28 19:05:24 -0400133### Lowercase hexadecimal
134
135BoringSSL's `BN_bn2hex` function uses lowercase hexadecimal digits instead of
136uppercase. Some code may require changes to avoid being sensitive to this
137difference.
138
David Benjamin4aafe6a2016-01-23 11:53:33 -0500139### Legacy ASN.1 functions
140
141OpenSSL's ASN.1 stack uses `d2i` functions for parsing. They have the form:
142
143 RSA *d2i_RSAPrivateKey(RSA **out, const uint8_t **inp, long len);
144
145In addition to returning the result, OpenSSL places it in `*out` if `out` is
146not `NULL`. On input, if `*out` is not `NULL`, OpenSSL will usually (but not
147always) reuse that object rather than allocating a new one. In BoringSSL, these
148functions are compatibility wrappers over a newer ASN.1 stack. Even if `*out`
149is not `NULL`, these wrappers will always allocate a new object and free the
150previous one.
151
152Ensure that callers do not rely on this object reuse behavior. It is
153recommended to avoid the `out` parameter completely and always pass in `NULL`.
154Note that less error-prone APIs are available for BoringSSL-specific code (see
155below).
David Benjamin65703b42015-09-02 16:51:42 -0400156
157## Optional BoringSSL-specific simplifications
158
David Benjamin809829e2015-09-03 11:48:12 -0400159BoringSSL makes some changes to OpenSSL which simplify the API but remain
David Benjamin65703b42015-09-02 16:51:42 -0400160compatible with OpenSSL consumers. In general, consult the BoringSSL
161documentation for any functions in new BoringSSL-only code.
162
163### Return values
164
165Most OpenSSL APIs return 1 on success and either 0 or -1 on failure. BoringSSL
166has narrowed most of these to 1 on success and 0 on failure. BoringSSL-specific
167code may take advantage of the less error-prone APIs and use `!` to check for
168errors.
169
170### Initialization
171
172OpenSSL has a number of different initialization functions for setting up error
173strings and loading algorithms, etc. All of these functions still exist in
174BoringSSL for convenience, but they do nothing and are not necessary.
175
David Benjamin7a1eefd2015-10-17 23:39:22 -0400176The one exception is `CRYPTO_library_init`. In `BORINGSSL_NO_STATIC_INITIALIZER`
177builds, it must be called to query CPU capabitilies before the rest of the
178library. In the default configuration, this is done with a static initializer
179and is also unnecessary.
David Benjamin65703b42015-09-02 16:51:42 -0400180
181### Threading
182
183OpenSSL provides a number of APIs to configure threading callbacks and set up
184locks. Without initializing these, the library is not thread-safe. Configuring
185these does nothing in BoringSSL. Instead, BoringSSL calls pthreads and the
186corresponding Windows APIs internally and is always thread-safe where the API
187guarantees it.
David Benjamin4aafe6a2016-01-23 11:53:33 -0500188
189### ASN.1
190
191BoringSSL is in the process of deprecating OpenSSL's `d2i` and `i2d` in favor of
192new functions using the much less error-prone `CBS` and `CBB` types.
193BoringSSL-only code should use those functions where available.
David Benjamind8ba86d2016-07-16 00:29:27 +0200194
195
196## Replacements for `CTRL` values
197
198When porting code which uses `SSL_CTX_ctrl` or `SSL_ctrl`, use the replacement
199functions below. If a function has both `SSL_CTX` and `SSL` variants, only the
200`SSL_CTX` version is listed.
201
202Note some values correspond to multiple functions depending on the `larg`
203parameter.
204
205`CTRL` value | Replacement function(s)
206-------------|-------------------------
207`DTLS_CTRL_GET_TIMEOUT` | `DTLSv1_get_timeout`
208`DTLS_CTRL_HANDLE_TIMEOUT` | `DTLSv1_handle_timeout`
209`SSL_CTRL_CHAIN` | `SSL_CTX_set0_chain` or `SSL_CTX_set1_chain`
210`SSL_CTRL_CHAIN_CERT` | `SSL_add0_chain_cert` or `SSL_add1_chain_cert`
211`SSL_CTRL_CLEAR_EXTRA_CHAIN_CERTS` | `SSL_CTX_clear_extra_chain_certs`
212`SSL_CTRL_CLEAR_MODE` | `SSL_CTX_clear_mode`
213`SSL_CTRL_CLEAR_OPTIONS` | `SSL_CTX_clear_options`
214`SSL_CTRL_EXTRA_CHAIN_CERT` | `SSL_CTX_add_extra_chain_cert`
215`SSL_CTRL_GET_CHAIN_CERTS` | `SSL_CTX_get0_chain_certs`
216`SSL_CTRL_GET_CLIENT_CERT_TYPES` | `SSL_get0_certificate_types`
217`SSL_CTRL_GET_EXTRA_CHAIN_CERTS` | `SSL_CTX_get_extra_chain_certs` or `SSL_CTX_get_extra_chain_certs_only`
218`SSL_CTRL_GET_MAX_CERT_LIST` | `SSL_CTX_get_max_cert_list`
219`SSL_CTRL_GET_NUM_RENEGOTIATIONS` | `SSL_num_renegotiations`
220`SSL_CTRL_GET_READ_AHEAD` | `SSL_CTX_get_read_ahead`
221`SSL_CTRL_GET_RI_SUPPORT` | `SSL_get_secure_renegotiation_support`
222`SSL_CTRL_GET_SESSION_REUSED` | `SSL_session_reused`
223`SSL_CTRL_GET_SESS_CACHE_MODE` | `SSL_CTX_get_session_cache_mode`
224`SSL_CTRL_GET_SESS_CACHE_SIZE` | `SSL_CTX_sess_get_cache_size`
225`SSL_CTRL_GET_TLSEXT_TICKET_KEYS` | `SSL_CTX_get_tlsext_ticket_keys`
226`SSL_CTRL_GET_TOTAL_RENEGOTIATIONS` | `SSL_total_renegotiations`
227`SSL_CTRL_MODE` | `SSL_CTX_get_mode` or `SSL_CTX_set_mode`
228`SSL_CTRL_NEED_TMP_RSA` | `SSL_CTX_need_tmp_RSA` is equivalent, but [*do not use this function*](https://freakattack.com/). (It is a no-op in BoringSSL.)
229`SSL_CTRL_OPTIONS` | `SSL_CTX_get_options` or `SSL_CTX_set_options`
230`SSL_CTRL_SESS_NUMBER` | `SSL_CTX_sess_number`
231`SSL_CTRL_SET_CURVES` | `SSL_CTX_set1_curves`
David Benjamin48e1d182017-03-13 16:56:30 -0400232`SSL_CTRL_SET_ECDH_AUTO` | `SSL_CTX_set_ecdh_auto`
David Benjamind8ba86d2016-07-16 00:29:27 +0200233`SSL_CTRL_SET_MAX_CERT_LIST` | `SSL_CTX_set_max_cert_list`
234`SSL_CTRL_SET_MAX_SEND_FRAGMENT` | `SSL_CTX_set_max_send_fragment`
235`SSL_CTRL_SET_MSG_CALLBACK` | `SSL_set_msg_callback`
236`SSL_CTRL_SET_MSG_CALLBACK_ARG` | `SSL_set_msg_callback_arg`
237`SSL_CTRL_SET_MTU` | `SSL_set_mtu`
238`SSL_CTRL_SET_READ_AHEAD` | `SSL_CTX_set_read_ahead`
239`SSL_CTRL_SET_SESS_CACHE_MODE` | `SSL_CTX_set_session_cache_mode`
240`SSL_CTRL_SET_SESS_CACHE_SIZE` | `SSL_CTX_sess_set_cache_size`
241`SSL_CTRL_SET_TLSEXT_HOSTNAME` | `SSL_set_tlsext_host_name`
242`SSL_CTRL_SET_TLSEXT_SERVERNAME_ARG` | `SSL_CTX_set_tlsext_servername_arg`
243`SSL_CTRL_SET_TLSEXT_SERVERNAME_CB` | `SSL_CTX_set_tlsext_servername_callback`
244`SSL_CTRL_SET_TLSEXT_TICKET_KEYS` | `SSL_CTX_set_tlsext_ticket_keys`
245`SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB` | `SSL_CTX_set_tlsext_ticket_key_cb`
246`SSL_CTRL_SET_TMP_DH` | `SSL_CTX_set_tmp_dh`
247`SSL_CTRL_SET_TMP_DH_CB` | `SSL_CTX_set_tmp_dh_callback`
248`SSL_CTRL_SET_TMP_ECDH` | `SSL_CTX_set_tmp_ecdh`
249`SSL_CTRL_SET_TMP_ECDH_CB` | `SSL_CTX_set_tmp_ecdh_callback`
250`SSL_CTRL_SET_TMP_RSA` | `SSL_CTX_set_tmp_rsa` is equivalent, but [*do not use this function*](https://freakattack.com/). (It is a no-op in BoringSSL.)
251`SSL_CTRL_SET_TMP_RSA_CB` | `SSL_CTX_set_tmp_rsa_callback` is equivalent, but [*do not use this function*](https://freakattack.com/). (It is a no-op in BoringSSL.)
Adam Langley61c98382017-07-28 12:20:58 -0700252
253## Significant API additions
254
255In some places, BoringSSL has added significant APIs. Use of these APIs goes beyound porting and means giving up on OpenSSL compatibility.
256
257One example of this has already been mentioned: the [CBS and CBB](https://commondatastorage.googleapis.com/chromium-boringssl-docs/bytestring.h.html) functions should be used whenever parsing or serialising data.
258
259### CRYPTO\_BUFFER
260
261With the standard OpenSSL APIs, when making many TLS connections, the certificate data for each connection is retained in memory in an expensive `X509` structure. Additionally, common certificates often appear in the chains for multiple connections and are needlessly duplicated in memory.
262
263A [`CRYPTO_BUFFER`](https://commondatastorage.googleapis.com/chromium-boringssl-docs/pool.h.html) is just an opaque byte string. A `CRYPTO_BUFFER_POOL` is an intern table for these buffers, i.e. it ensures that only a single copy of any given byte string is kept for each pool.
264
265The function `TLS_with_buffers_method` returns an `SSL_METHOD` that avoids creating `X509` objects for certificates. Additionally, `SSL_CTX_set0_buffer_pool` can be used to install a pool on an `SSL_CTX` so that certificates can be deduplicated across connections and across `SSL_CTX`s.
266
267When using these functions, the application also needs to ensure that it doesn't call other functions that deal with `X509` or `X509_NAME` objects. For example, `SSL_get_peer_certificate` or `SSL_get_peer_cert_chain`. Doing so will trigger an assert in debug mode and will result in NULLs in release mode. Instead, call the buffer-based alternatives such as `SSL_get0_peer_certificates`. (See [ssl.h](https://commondatastorage.googleapis.com/chromium-boringssl-docs/ssl.h.html) for functions taking or returning `CRYPTO_BUFFER`.) The buffer-based alternative functions will work even when not using `TLS_with_buffers_method`, thus application code can transition gradually.
268
269In order to use buffers, the application code also needs to implement its own certificate verification using `SSL_[CTX_]set_custom_verify`. Otherwise all connections will fail with a verification error. Auto-chaining is also disabled when using buffers.
270
271Once those changes have been completed, the whole of the OpenSSL X.509 and ASN.1 code should be eliminated by the linker if BoringSSL is linked statically.
Adam Langleyc90be3b2017-08-04 12:31:20 -0700272
273### Asynchronous and opaque private keys
274
275OpenSSL offers the ENGINE API for implementing opaque private keys (i.e. private keys where software only has oracle access because the secrets are held in special hardware or on another machine). While the ENGINE API has been mostly removed from BoringSSL, it is still possible to support opaque keys in this way. However, when using such keys with TLS and BoringSSL, you should strongly prefer using `SSL_PRIVATE_KEY_METHOD` via `SSL[_CTX]_set_private_key_method`. This allows a handshake to be suspended while the private operation is in progress. It also supports more forms of opaque key as it exposes higher-level information about the operation to be performed.