Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wipe output on failure: key generation #586

Merged
merged 3 commits into from
Feb 3, 2020

Conversation

ilammy
Copy link
Collaborator

@ilammy ilammy commented Jan 30, 2020

More wiping 🧹

Refactor high-level EC/RSA key generation functions in Themis so that they are easier to follow and it is easier to insert additional actions into the error path. Then insert soter_wipe() for already exported public key if private key export fails or vice versa.

While we're here, I've noticed and fixed clang-tidy complaints about mismatched integer types in OpenSSL code for RSA key generation. (BoringSSL code is fine, it already uses correct types.)

Checklist

Current code is hard to follow and has complicated management for key
generation contexts. However, this is not essential complexity.

The core insight here is that while the context is shared, exporting
private and public key are two independent operations. Therefore we can
try exporting both keys, free the context, and only then deal with the
results. It then becomes really natural to introduce a helper function
to combine two results into one.

While we're here, make sure that all local variables are initialized.
This too makes error handling path easier to write and understand.
Now, let's make sure that we don't leak any information if key
generation fails. For example, if the private key has been extracted
just fine but extracting public key fails then the private key has
already been written out into the user buffer. This is not nice.

We can easily insert soter_wipe() calls to remove results from output
buffers if key generation is not succesful. Note that we *do not* wipe
the buffer if it is reported to be too small. In this case the length
is now equal to the desired buffer length, not actual one provided by
the caller. We'd probably overrun the buffer if we touch it. Instead,
we can simply not wipe it because in this case we have not written
anything into it yet.

Symmetric key generation simply calls soter_rand() which wipes after
itself like an adult. We leave a comment just in case the code gets
more complex in the future, so that the developers don't forget to add
a soter_wipe() call if necessary.

This covers Themis code for key generation API:

  - themis_gen_sym_key
  - themis_gen_rsa_key_pair
  - themis_gen_ec_key_pair
In OpenSSL EVP_PKEY_CTX_ctrl() penultimate argument "p1" has "int" type.
We use this function to set RSA key length in bits, and we incorrectly
use "unsigned" type when interfacing with EVP_PKEY_CTX_ctrl(). Update
the type to just "int" to avoid warnings by clang-tidy about narrowing
casts. We can use "int" in rsa_key_length() because it's wide enough
for current maximum length of 4096 bits.
@ilammy ilammy added core Themis Core written in C, its packages C-OpenSSL Crypto provider: OpenSSL/LibreSSL labels Jan 30, 2020
Copy link
Collaborator

@Lagovas Lagovas left a comment

Choose a reason for hiding this comment

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

lgtm

Copy link
Contributor

@vixentael vixentael left a comment

Choose a reason for hiding this comment

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

Looks suspicious, but if tests are green, then i trust this code :D

@ilammy
Copy link
Collaborator Author

ilammy commented Feb 3, 2020

@vixentael,

Looks suspicious

Which parts, exactly? I can elaborate if you wish.

I do need to know this so that I can hide backdoors better next time! /s

@ilammy ilammy merged commit e26e720 into cossacklabs:master Feb 3, 2020
@ilammy ilammy deleted the wipe-output/key-generation branch February 12, 2020 19:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-OpenSSL Crypto provider: OpenSSL/LibreSSL core Themis Core written in C, its packages
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants