Skip to content

client, server: Fix padding with OpenSSL backend - #708

Open
michaelkuhn wants to merge 2 commits into
linux-credentials:mainfrom
michaelkuhn:openssl-dh-padding
Open

michaelkuhn wants to merge 2 commits into
linux-credentials:mainfrom
michaelkuhn:openssl-dh-padding

Conversation

@michaelkuhn

Copy link
Copy Markdown

libsecret sometimes doesn't find stored secrets, for example, when using git-credential-libsecret:

libsecret-CRITICAL **: secret_value_get_text: assertion 'value' failed
libsecret-CRITICAL **: secret_value_unref: assertion 'value != NULL' failed

client/src/crypto/openssl.rs::generate_aes_key() uses rust-openssl's Dh::compute_key(). It allocates a DH_size() buffer (128 bytes), calls DH_compute_key() and ignores the returned length. DH_compute_key() strips leading zero bytes: it moves the secret to the front of the buffer and zero-fills the tail. If the shared secret starts with a zero byte (about 1 in 256), oo7 gets secret||00, sees a 128-byte value and adds no padding. libsecret (>= 0.21.8, and gcrypt builds) uses the correct 00||secret. HKDF then produces different AES keys, and that session can't decrypt anything.

server/src/gnome/crypto/openssl.rs::generate_aes_key_for_secret_exchange() has the same bug with the 1536-bit group. It's used for the key exchange with the GNOME prompter, so occasionally an unlock prompt probably fails.

Fixes: #707

libsecret sometimes doesn't find stored secrets, for example, when using
`git-credential-libsecret`:

```
libsecret-CRITICAL **: secret_value_get_text: assertion 'value' failed
libsecret-CRITICAL **: secret_value_unref: assertion 'value != NULL' failed
```

`client/src/crypto/openssl.rs::generate_aes_key()` uses rust-openssl's
`Dh::compute_key()`. It allocates a `DH_size()` buffer (128 bytes),
calls `DH_compute_key()` and ignores the returned length.
`DH_compute_key()` strips leading zero bytes: it moves the secret to the
front of the buffer and zero-fills the tail. If the shared secret starts
with a zero byte (about 1 in 256), oo7 gets `secret||00`, sees a
128-byte value and adds no padding. libsecret (>= 0.21.8, and gcrypt
builds) uses the correct `00||secret`. HKDF then produces different AES
keys, and that session can't decrypt anything.

`server/src/gnome/crypto/openssl.rs::generate_aes_key_for_secret_exchange()`
has the same bug with the 1536-bit group. It's used for the key exchange
with the GNOME prompter, so occasionally an unlock prompt probably
fails.
@github-actions

github-actions Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

📊 Code Coverage Report

Metric Value
Current PR Coverage 88.27%
Main Branch Coverage 88.31%
Coverage Change 📉 -0.04%

Coverage report generated by cargo-tarpaulin

@bilelmoussaoui bilelmoussaoui left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a test case based on the script you shared confirming it works as expected?

@michaelkuhn

Copy link
Copy Markdown
Author

I've pushed a new commit that adds two tests that should catch the problem:

main, openssl_crypto:

running 3 tests
.. 2/3
key::tests::aes_key_with_leading_zero_in_shared_secret --- FAILED

failures:

---- key::tests::aes_key_with_leading_zero_in_shared_secret stdout ----

thread 'key::tests::aes_key_with_leading_zero_in_shared_secret' (177861) panicked at client/src/key.rs:188:9:
assertion `left == right` failed
  left: [154, 107, 212, 95, 27, 58, 199, 238, 127, 56, 220, 111, 170, 24, 142, 47]
 right: [45, 9, 43, 36, 239, 251, 6, 77, 119, 146, 59, 198, 169, 6, 54, 11]
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace


failures:
    key::tests::aes_key_with_leading_zero_in_shared_secret

test result: FAILED. 2 passed; 1 failed; 0 ignored; 0 measured; 35 filtered out; finished in 0.00s

error: test failed, to rerun pass `-p oo7 --lib`

running 2 tests
. 1/2
gnome::crypto::tests::aes_key_with_leading_zero_in_shared_secret --- FAILED

failures:

---- gnome::crypto::tests::aes_key_with_leading_zero_in_shared_secret stdout ----

thread 'gnome::crypto::tests::aes_key_with_leading_zero_in_shared_secret' (179035) panicked at server/src/gnome/crypto/mod.rs:91:9:
assertion `left == right` failed
  left: [251, 115, 44, 187, 231, 227, 216, 243, 60, 120, 156, 41, 128, 114, 57, 242]
 right: [50, 213, 186, 140, 94, 189, 52, 75, 137, 14, 75, 68, 123, 187, 127, 87]
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace


failures:
    gnome::crypto::tests::aes_key_with_leading_zero_in_shared_secret

test result: FAILED. 1 passed; 1 failed; 0 ignored; 0 measured; 162 filtered out; finished in 0.00s

error: test failed, to rerun pass `-p oo7-daemon --bin oo7-daemon`

main, native_crypto:

running 3 tests
...
test result: ok. 3 passed; 0 failed; 0 ignored; 0 measured; 35 filtered out; finished in 0.07s


running 2 tests
..
test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 162 filtered out; finished in 0.11s

main + patch, openssl_crypto:

running 3 tests
...
test result: ok. 3 passed; 0 failed; 0 ignored; 0 measured; 35 filtered out; finished in 0.00s


running 2 tests
..
test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 162 filtered out; finished in 0.00s

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

libsecret sometimes doesn't find stored secrets

2 participants