Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

PORTING.md 11 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. # Porting from OpenSSL to BoringSSL
  2. BoringSSL is an OpenSSL derivative and is mostly source-compatible, for the
  3. subset of OpenSSL retained. Libraries ideally need little to no changes for
  4. BoringSSL support, provided they do not use removed APIs. In general, see if the
  5. library compiles and, on failure, consult the documentation in the header files
  6. and see if problematic features can be removed.
  7. In some cases, BoringSSL-specific code may be necessary. In that case, the
  8. `OPENSSL_IS_BORINGSSL` preprocessor macro may be used in `#ifdef`s. This macro
  9. should also be used in lieu of the presence of any particular function to detect
  10. OpenSSL vs BoringSSL in configure scripts, etc., where those are necessary.
  11. Before using the preprocessor, however, contact the BoringSSL maintainers about
  12. the missing APIs. If not an intentionally removed feature, BoringSSL will
  13. typically add compatibility functions for convenience.
  14. For convenience, BoringSSL defines upstream's `OPENSSL_NO_*` feature macros
  15. corresponding to removed features. These may also be used to disable code which
  16. uses a removed feature.
  17. Note: BoringSSL does *not* have a stable API or ABI. It must be updated with its
  18. consumers. It is not suitable for, say, a system library in a traditional Linux
  19. distribution. For instance, Chromium statically links the specific revision of
  20. BoringSSL it was built against. Likewise, Android's system-internal copy of
  21. BoringSSL is not exposed by the NDK and must not be used by third-party
  22. applications.
  23. ## Major API changes
  24. ### Integer types
  25. Some APIs have been converted to use `size_t` for consistency and to avoid
  26. integer overflows at the API boundary. (Existing logic uses a mismash of `int`,
  27. `long`, and `unsigned`.) For the most part, implicit casts mean that existing
  28. code continues to compile. In some cases, this may require BoringSSL-specific
  29. code, particularly to avoid compiler warnings.
  30. Most notably, the `STACK_OF(T)` types have all been converted to use `size_t`
  31. instead of `int` for indices and lengths.
  32. ### Reference counts
  33. Some external consumers increment reference counts directly by calling
  34. `CRYPTO_add` with the corresponding `CRYPTO_LOCK_*` value.
  35. These APIs no longer exist in BoringSSL. Instead, code which increments
  36. reference counts should call the corresponding `FOO_up_ref` function, such as
  37. `EVP_PKEY_up_ref`. Note that not all of these APIs are present in OpenSSL and
  38. may require `#ifdef`s.
  39. ### Error codes
  40. OpenSSL's errors are extremely specific, leaking internals of the library,
  41. including even a function code for the function which emitted the error! As some
  42. logic in BoringSSL has been rewritten, code which conditions on the error may
  43. break (grep for `ERR_GET_REASON` and `ERR_GET_FUNC`). This danger also exists
  44. when upgrading OpenSSL versions.
  45. Where possible, avoid conditioning on the exact error reason. Otherwise, a
  46. BoringSSL `#ifdef` may be necessary. Exactly how best to resolve this issue is
  47. still being determined. It's possible some new APIs will be added in the future.
  48. Function codes have been completely removed. Remove code which conditions on
  49. these as it will break with the slightest change in the library, OpenSSL or
  50. BoringSSL.
  51. ### `*_ctrl` functions
  52. Some OpenSSL APIs are implemented with `ioctl`-style functions such as
  53. `SSL_ctrl` and `EVP_PKEY_CTX_ctrl`, combined with convenience macros, such as
  54. # define SSL_CTX_set_mode(ctx,op) \
  55. SSL_CTX_ctrl((ctx),SSL_CTRL_MODE,(op),NULL)
  56. In BoringSSL, these macros have been replaced with proper functions. The
  57. underlying `_ctrl` functions have been removed.
  58. For convenience, `SSL_CTRL_*` values are retained as macros to `doesnt_exist` so
  59. existing code which uses them (or the wrapper macros) in `#ifdef` expressions
  60. will continue to function. However, the macros themselves will not work.
  61. Switch any `*_ctrl` callers to the macro/function versions. This works in both
  62. OpenSSL and BoringSSL. Note that BoringSSL's function versions will be
  63. type-checked and may require more care with types. See the end of this
  64. document for a table of functions to use.
  65. ### HMAC `EVP_PKEY`s
  66. `EVP_PKEY_HMAC` is removed. Use the `HMAC_*` functions in `hmac.h` instead. This
  67. is compatible with OpenSSL.
  68. ### DSA `EVP_PKEY`s
  69. `EVP_PKEY_DSA` is deprecated. It is currently still possible to parse DER into a
  70. DSA `EVP_PKEY`, but signing or verifying with those objects will not work.
  71. ### DES
  72. The `DES_cblock` type has been switched from an array to a struct to avoid the
  73. pitfalls around array types in C. Where features which require DES cannot be
  74. disabled, BoringSSL-specific codepaths may be necessary.
  75. ### TLS renegotiation
  76. OpenSSL enables TLS renegotiation by default and accepts renegotiation requests
  77. from the peer transparently. Renegotiation is an extremely problematic protocol
  78. feature, so BoringSSL rejects peer renegotiations by default.
  79. To enable renegotiation, call `SSL_set_renegotiate_mode` and set it to
  80. `ssl_renegotiate_once` or `ssl_renegotiate_freely`. Renegotiation is only
  81. supported as a client in SSL3/TLS and the HelloRequest must be received at a
  82. quiet point in the application protocol. This is sufficient to support the
  83. common use of requesting a new client certificate between an HTTP request and
  84. response in (unpipelined) HTTP/1.1.
  85. Things which do not work:
  86. * There is no support for renegotiation as a server.
  87. * There is no support for renegotiation in DTLS.
  88. * There is no support for initiating renegotiation; `SSL_renegotiate` always
  89. fails and `SSL_set_state` does nothing.
  90. * Interleaving application data with the new handshake is forbidden.
  91. * If a HelloRequest is received while `SSL_write` has unsent application data,
  92. the renegotiation is rejected.
  93. ### Lowercase hexadecimal
  94. BoringSSL's `BN_bn2hex` function uses lowercase hexadecimal digits instead of
  95. uppercase. Some code may require changes to avoid being sensitive to this
  96. difference.
  97. ### Legacy ASN.1 functions
  98. OpenSSL's ASN.1 stack uses `d2i` functions for parsing. They have the form:
  99. RSA *d2i_RSAPrivateKey(RSA **out, const uint8_t **inp, long len);
  100. In addition to returning the result, OpenSSL places it in `*out` if `out` is
  101. not `NULL`. On input, if `*out` is not `NULL`, OpenSSL will usually (but not
  102. always) reuse that object rather than allocating a new one. In BoringSSL, these
  103. functions are compatibility wrappers over a newer ASN.1 stack. Even if `*out`
  104. is not `NULL`, these wrappers will always allocate a new object and free the
  105. previous one.
  106. Ensure that callers do not rely on this object reuse behavior. It is
  107. recommended to avoid the `out` parameter completely and always pass in `NULL`.
  108. Note that less error-prone APIs are available for BoringSSL-specific code (see
  109. below).
  110. ## Optional BoringSSL-specific simplifications
  111. BoringSSL makes some changes to OpenSSL which simplify the API but remain
  112. compatible with OpenSSL consumers. In general, consult the BoringSSL
  113. documentation for any functions in new BoringSSL-only code.
  114. ### Return values
  115. Most OpenSSL APIs return 1 on success and either 0 or -1 on failure. BoringSSL
  116. has narrowed most of these to 1 on success and 0 on failure. BoringSSL-specific
  117. code may take advantage of the less error-prone APIs and use `!` to check for
  118. errors.
  119. ### Initialization
  120. OpenSSL has a number of different initialization functions for setting up error
  121. strings and loading algorithms, etc. All of these functions still exist in
  122. BoringSSL for convenience, but they do nothing and are not necessary.
  123. The one exception is `CRYPTO_library_init`. In `BORINGSSL_NO_STATIC_INITIALIZER`
  124. builds, it must be called to query CPU capabitilies before the rest of the
  125. library. In the default configuration, this is done with a static initializer
  126. and is also unnecessary.
  127. ### Threading
  128. OpenSSL provides a number of APIs to configure threading callbacks and set up
  129. locks. Without initializing these, the library is not thread-safe. Configuring
  130. these does nothing in BoringSSL. Instead, BoringSSL calls pthreads and the
  131. corresponding Windows APIs internally and is always thread-safe where the API
  132. guarantees it.
  133. ### ASN.1
  134. BoringSSL is in the process of deprecating OpenSSL's `d2i` and `i2d` in favor of
  135. new functions using the much less error-prone `CBS` and `CBB` types.
  136. BoringSSL-only code should use those functions where available.
  137. ## Replacements for `CTRL` values
  138. When porting code which uses `SSL_CTX_ctrl` or `SSL_ctrl`, use the replacement
  139. functions below. If a function has both `SSL_CTX` and `SSL` variants, only the
  140. `SSL_CTX` version is listed.
  141. Note some values correspond to multiple functions depending on the `larg`
  142. parameter.
  143. `CTRL` value | Replacement function(s)
  144. -------------|-------------------------
  145. `DTLS_CTRL_GET_TIMEOUT` | `DTLSv1_get_timeout`
  146. `DTLS_CTRL_HANDLE_TIMEOUT` | `DTLSv1_handle_timeout`
  147. `SSL_CTRL_CHAIN` | `SSL_CTX_set0_chain` or `SSL_CTX_set1_chain`
  148. `SSL_CTRL_CHAIN_CERT` | `SSL_add0_chain_cert` or `SSL_add1_chain_cert`
  149. `SSL_CTRL_CLEAR_EXTRA_CHAIN_CERTS` | `SSL_CTX_clear_extra_chain_certs`
  150. `SSL_CTRL_CLEAR_MODE` | `SSL_CTX_clear_mode`
  151. `SSL_CTRL_CLEAR_OPTIONS` | `SSL_CTX_clear_options`
  152. `SSL_CTRL_EXTRA_CHAIN_CERT` | `SSL_CTX_add_extra_chain_cert`
  153. `SSL_CTRL_GET_CHAIN_CERTS` | `SSL_CTX_get0_chain_certs`
  154. `SSL_CTRL_GET_CLIENT_CERT_TYPES` | `SSL_get0_certificate_types`
  155. `SSL_CTRL_GET_EXTRA_CHAIN_CERTS` | `SSL_CTX_get_extra_chain_certs` or `SSL_CTX_get_extra_chain_certs_only`
  156. `SSL_CTRL_GET_MAX_CERT_LIST` | `SSL_CTX_get_max_cert_list`
  157. `SSL_CTRL_GET_NUM_RENEGOTIATIONS` | `SSL_num_renegotiations`
  158. `SSL_CTRL_GET_READ_AHEAD` | `SSL_CTX_get_read_ahead`
  159. `SSL_CTRL_GET_RI_SUPPORT` | `SSL_get_secure_renegotiation_support`
  160. `SSL_CTRL_GET_SESSION_REUSED` | `SSL_session_reused`
  161. `SSL_CTRL_GET_SESS_CACHE_MODE` | `SSL_CTX_get_session_cache_mode`
  162. `SSL_CTRL_GET_SESS_CACHE_SIZE` | `SSL_CTX_sess_get_cache_size`
  163. `SSL_CTRL_GET_TLSEXT_TICKET_KEYS` | `SSL_CTX_get_tlsext_ticket_keys`
  164. `SSL_CTRL_GET_TOTAL_RENEGOTIATIONS` | `SSL_total_renegotiations`
  165. `SSL_CTRL_MODE` | `SSL_CTX_get_mode` or `SSL_CTX_set_mode`
  166. `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.)
  167. `SSL_CTRL_OPTIONS` | `SSL_CTX_get_options` or `SSL_CTX_set_options`
  168. `SSL_CTRL_SESS_NUMBER` | `SSL_CTX_sess_number`
  169. `SSL_CTRL_SET_CURVES` | `SSL_CTX_set1_curves`
  170. `SSL_CTRL_SET_ECDH_AUTO` | `SSL_CTX_set_ecdh_auto`
  171. `SSL_CTRL_SET_MAX_CERT_LIST` | `SSL_CTX_set_max_cert_list`
  172. `SSL_CTRL_SET_MAX_SEND_FRAGMENT` | `SSL_CTX_set_max_send_fragment`
  173. `SSL_CTRL_SET_MSG_CALLBACK` | `SSL_set_msg_callback`
  174. `SSL_CTRL_SET_MSG_CALLBACK_ARG` | `SSL_set_msg_callback_arg`
  175. `SSL_CTRL_SET_MTU` | `SSL_set_mtu`
  176. `SSL_CTRL_SET_READ_AHEAD` | `SSL_CTX_set_read_ahead`
  177. `SSL_CTRL_SET_SESS_CACHE_MODE` | `SSL_CTX_set_session_cache_mode`
  178. `SSL_CTRL_SET_SESS_CACHE_SIZE` | `SSL_CTX_sess_set_cache_size`
  179. `SSL_CTRL_SET_TLSEXT_HOSTNAME` | `SSL_set_tlsext_host_name`
  180. `SSL_CTRL_SET_TLSEXT_SERVERNAME_ARG` | `SSL_CTX_set_tlsext_servername_arg`
  181. `SSL_CTRL_SET_TLSEXT_SERVERNAME_CB` | `SSL_CTX_set_tlsext_servername_callback`
  182. `SSL_CTRL_SET_TLSEXT_TICKET_KEYS` | `SSL_CTX_set_tlsext_ticket_keys`
  183. `SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB` | `SSL_CTX_set_tlsext_ticket_key_cb`
  184. `SSL_CTRL_SET_TMP_DH` | `SSL_CTX_set_tmp_dh`
  185. `SSL_CTRL_SET_TMP_DH_CB` | `SSL_CTX_set_tmp_dh_callback`
  186. `SSL_CTRL_SET_TMP_ECDH` | `SSL_CTX_set_tmp_ecdh`
  187. `SSL_CTRL_SET_TMP_ECDH_CB` | `SSL_CTX_set_tmp_ecdh_callback`
  188. `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.)
  189. `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.)