Hardening money-tree: removing unsafe OpenSSL patches from a Ruby BIP32 wallet library

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.

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.

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.