You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PORTING.md 7.8 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. For convenience, BoringSSL defines upstream's `OPENSSL_NO_*` feature macros
  12. corresponding to removed features. These may also be used to disable code which
  13. uses a removed feature.
  14. Note: BoringSSL does *not* have a stable API or ABI. It must be updated with its
  15. consumers. It is not suitable for, say, a system library in a traditional Linux
  16. distribution. For instance, Chromium statically links the specific revision of
  17. BoringSSL it was built against. Likewise, Android's system-internal copy of
  18. BoringSSL is not exposed by the NDK and must not be used by third-party
  19. applications.
  20. ## Major API changes
  21. ### Integer types
  22. Some APIs have been converted to use `size_t` for consistency and to avoid
  23. integer overflows at the API boundary. (Existing logic uses a mismash of `int`,
  24. `long`, and `unsigned`.) For the most part, implicit casts mean that existing
  25. code continues to compile. In some cases, this may require BoringSSL-specific
  26. code, particularly to avoid compiler warnings.
  27. Most notably, the `STACK_OF(T)` types have all been converted to use `size_t`
  28. instead of `int` for indices and lengths.
  29. ### Reference counts
  30. Some external consumers increment reference counts directly by calling
  31. `CRYPTO_add` with the corresponding `CRYPTO_LOCK_*` value.
  32. These APIs no longer exist in BoringSSL. Instead, code which increments
  33. reference counts should call the corresponding `FOO_up_ref` function, such as
  34. `EVP_PKEY_up_ref`. Note that not all of these APIs are present in OpenSSL and
  35. may require `#ifdef`s.
  36. ### Error codes
  37. OpenSSL's errors are extremely specific, leaking internals of the library,
  38. including even a function code for the function which emitted the error! As some
  39. logic in BoringSSL has been rewritten, code which conditions on the error may
  40. break (grep for `ERR_GET_REASON` and `ERR_GET_FUNC`). This danger also exists
  41. when upgrading OpenSSL versions.
  42. Where possible, avoid conditioning on the exact error reason. Otherwise, a
  43. BoringSSL `#ifdef` may be necessary. Exactly how best to resolve this issue is
  44. still being determined. It's possible some new APIs will be added in the future.
  45. Function codes have been completely removed. Remove code which conditions on
  46. these as it will break with the slightest change in the library, OpenSSL or
  47. BoringSSL.
  48. ### `*_ctrl` functions
  49. Some OpenSSL APIs are implemented with `ioctl`-style functions such as
  50. `SSL_ctrl` and `EVP_PKEY_CTX_ctrl`, combined with convenience macros, such as
  51. # define SSL_CTX_set_mode(ctx,op) \
  52. SSL_CTX_ctrl((ctx),SSL_CTRL_MODE,(op),NULL)
  53. In BoringSSL, these macros have been replaced with proper functions. The
  54. underlying `_ctrl` functions have been removed.
  55. For convenience, `SSL_CTRL_*` values are retained as macros to `doesnt_exist` so
  56. existing code which uses them (or the wrapper macros) in `#ifdef` expressions
  57. will continue to function. However, the macros themselves will not work.
  58. Switch any `*_ctrl` callers to the macro/function versions. This works in both
  59. OpenSSL and BoringSSL. Note that BoringSSL's function versions will be
  60. type-checked and may require more care with types.
  61. ### HMAC `EVP_PKEY`s
  62. `EVP_PKEY_HMAC` is removed. Use the `HMAC_*` functions in `hmac.h` instead. This
  63. is compatible with OpenSSL.
  64. ### DSA `EVP_PKEY`s
  65. `EVP_PKEY_DSA` is deprecated. It is currently still possible to parse DER into a
  66. DSA `EVP_PKEY`, but signing or verifying with those objects will not work.
  67. ### DES
  68. The `DES_cblock` type has been switched from an array to a struct to avoid the
  69. pitfalls around array types in C. Where features which require DES cannot be
  70. disabled, BoringSSL-specific codepaths may be necessary.
  71. ### TLS renegotiation
  72. OpenSSL enables TLS renegotiation by default and accepts renegotiation requests
  73. from the peer transparently. Renegotiation is an extremely problematic protocol
  74. feature, so BoringSSL rejects peer renegotiations by default.
  75. To enable renegotiation, call `SSL_set_renegotiate_mode` and set it to
  76. `ssl_renegotiate_once` or `ssl_renegotiate_freely`. Renegotiation is only
  77. supported as a client in SSL3/TLS and the HelloRequest must be received at a
  78. quiet point in the application protocol. This is sufficient to support the
  79. common use of requesting a new client certificate between an HTTP request and
  80. response in (unpipelined) HTTP/1.1.
  81. Things which do not work:
  82. * There is no support for renegotiation as a server.
  83. * There is no support for renegotiation in DTLS.
  84. * There is no support for initiating renegotiation; `SSL_renegotiate` always
  85. fails and `SSL_set_state` does nothing.
  86. * Interleaving application data with the new handshake is forbidden.
  87. * If a HelloRequest is received while `SSL_write` has unsent application data,
  88. the renegotiation is rejected.
  89. ### Lowercase hexadecimal
  90. BoringSSL's `BN_bn2hex` function uses lowercase hexadecimal digits instead of
  91. uppercase. Some code may require changes to avoid being sensitive to this
  92. difference.
  93. ### Legacy ASN.1 functions
  94. OpenSSL's ASN.1 stack uses `d2i` functions for parsing. They have the form:
  95. RSA *d2i_RSAPrivateKey(RSA **out, const uint8_t **inp, long len);
  96. In addition to returning the result, OpenSSL places it in `*out` if `out` is
  97. not `NULL`. On input, if `*out` is not `NULL`, OpenSSL will usually (but not
  98. always) reuse that object rather than allocating a new one. In BoringSSL, these
  99. functions are compatibility wrappers over a newer ASN.1 stack. Even if `*out`
  100. is not `NULL`, these wrappers will always allocate a new object and free the
  101. previous one.
  102. Ensure that callers do not rely on this object reuse behavior. It is
  103. recommended to avoid the `out` parameter completely and always pass in `NULL`.
  104. Note that less error-prone APIs are available for BoringSSL-specific code (see
  105. below).
  106. ## Optional BoringSSL-specific simplifications
  107. BoringSSL makes some changes to OpenSSL which simplify the API but remain
  108. compatible with OpenSSL consumers. In general, consult the BoringSSL
  109. documentation for any functions in new BoringSSL-only code.
  110. ### Return values
  111. Most OpenSSL APIs return 1 on success and either 0 or -1 on failure. BoringSSL
  112. has narrowed most of these to 1 on success and 0 on failure. BoringSSL-specific
  113. code may take advantage of the less error-prone APIs and use `!` to check for
  114. errors.
  115. ### Initialization
  116. OpenSSL has a number of different initialization functions for setting up error
  117. strings and loading algorithms, etc. All of these functions still exist in
  118. BoringSSL for convenience, but they do nothing and are not necessary.
  119. The one exception is `CRYPTO_library_init`. In `BORINGSSL_NO_STATIC_INITIALIZER`
  120. builds, it must be called to query CPU capabitilies before the rest of the
  121. library. In the default configuration, this is done with a static initializer
  122. and is also unnecessary.
  123. ### Threading
  124. OpenSSL provides a number of APIs to configure threading callbacks and set up
  125. locks. Without initializing these, the library is not thread-safe. Configuring
  126. these does nothing in BoringSSL. Instead, BoringSSL calls pthreads and the
  127. corresponding Windows APIs internally and is always thread-safe where the API
  128. guarantees it.
  129. ### ASN.1
  130. BoringSSL is in the process of deprecating OpenSSL's `d2i` and `i2d` in favor of
  131. new functions using the much less error-prone `CBS` and `CBB` types.
  132. BoringSSL-only code should use those functions where available.