Part of one story: hardening cryptographic key handling in open-source wallet libraries during my time at Coinbase. The companion post covers my libsecp256k1 API proposals from the same month.
In 2021 I was a Security Architect at Coinbase, working on wallet and key security. Alongside the C library side of that work in libsecp256k1, I looked at the Ruby side: money-tree, a Ruby library for BIP32 hierarchical deterministic (HD) wallets. BIP32 is the scheme that derives a whole tree of wallet keys from one seed, so a library that implements it sits directly on the path of every key it derives.
The problem: patching OpenSSL from inside a wallet library
money-tree did its elliptic-curve math with OpenSSL, but it reached into OpenSSL in two ways that a key-handling library shouldn’t.
- It monkey-patched OpenSSL’s elliptic-curve Point class. Ruby lets any library reopen and change a class that another library defines. money-tree changed
OpenSSL::PKey::EC::Pointto add the operations it needed. Those changes were written for secp256k1, the Bitcoin curve, but they applied to every Point in the process. On curves other than secp256k1 they could give wrong cryptographic results, and any other code in the same program that used OpenSSL’s EC classes would get the patched behavior too. - It loaded OpenSSL twice. The gem bound to OpenSSL through FFI and also loaded it with
require 'openssl'. Those two routes don’t have to find the same library, so one Ruby process could end up bound to two different OpenSSL builds at once.
Issue #44 and PR #43: removing both
I filed issue #44, “money-tree performs unsafe FFI and Monkey Patching of OpenSSL”, as a bug report describing both problems. On March 23, 2021 I opened pull request #43, “Remove unsafe FFI and monkey patches to OpenSSL”, which removed the Point monkey patches and the FFI binding, so the gem used one OpenSSL through Ruby’s own openssl extension and left its classes as OpenSSL defines them.
Maintainer q9f approved and merged the pull request in December 2021. When they approved it, they said they would add more tests in a follow-up pull request. Other users in the thread said downstream projects were waiting on the fix.
The ruby/openssl side: APIs so nobody has to patch
A wallet library patches OpenSSL because the Ruby bindings don’t expose something it needs. So in March and April 2021 I also opened pull requests to ruby/openssl, Ruby’s OpenSSL extension, to add the elliptic-curve and bignum APIs a wallet library needs, so it doesn’t have to patch OpenSSL.
- #430,
OpenSSL::BN#abs: absolute value for OpenSSL bignums. Merged in April 2021. - #432, reading curve parameters from an
EC::Group: a read API for the parameters of a named or explicit curve. Closed without being merged. - #434, optional deterministic k for ECDSA signing: lets a caller supply the per-signature value k, which is what RFC 6979 deterministic signatures need. Still open and not merged. The maintainer explained it relied on
ECDSA_sign_ex(), which OpenSSL 3.0 deprecates. - #435, getting and setting affine coordinates on
EC::Point: direct access to a point’s x and y. Still open and not merged.
Where this fits
The libsecp256k1 proposals and the money-tree fix come from the same question: where in a wallet library can a key be mishandled without anyone noticing? In libsecp256k1 the answer was the API boundary, where a secret key was just 32 bytes. In money-tree it was the library’s relationship with OpenSSL, where global patches and a second binding meant the code doing the math wasn’t the code you thought you had loaded. The money-tree fix was merged; the libsecp256k1 pull requests were not. Read Hardening the libsecp256k1 API: my 2021 proposals for opaque secret keys for that half, and the ECC curve research guide for my later work on secp256k1 itself.