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

Runtime check for EC keys in Secure Session #693

Conversation

iamnotacake
Copy link
Contributor

@iamnotacake iamnotacake commented Aug 11, 2020

  • Deny non-EC keys when creating/negotiating Secure Session.
    It was possible to create Secure Session by passing private RSA key to
    secure_session_create(). But then it would fail while negotiating
    connection with another peer since Secure Session doesn't actually
    support RSA keys. This PR make it fail a little bit earlier.
  • Move test keys to different file
  • Add tests to make sure secure_session_create fails with EC pub key, RSA priv/pub keys.
  • Add test to make sure secure_session_create fail with empty peer ID (id == NULL || id_len == 0).
  • Mention the key limitation in changelog.

Checklist

  • Change is covered by automated tests
  • The coding guidelines are followed
  • Changelog is updated (in case of notable or breaking changes)

* Deny non-EC keys when creating/negotiating Secure Session.
  It was possible to create Secure Session by passing private RSA key to
  `secure_session_create`. But then it would fail while negotiating
  connection with another peer since Secure Session doesn't actually
  support RSA keys. This commit make it fail a little bit earlier.
* Add test to make sure `secure_session_create` fail with RSA key.
* Add test to make sure `secure_session_create` fail with empty peer ID
  (peer_id_len == 0).
@iamnotacake iamnotacake added core Themis Core written in C, its packages tests Themis test suite labels Aug 11, 2020
@iamnotacake iamnotacake changed the title Anatolii t1739 runtime check ec keys in secure session Runtime check for EC keys in Secure Session Aug 11, 2020
* Move test keys to different file, make `clang-tidy` satisfied
* Rewrite messages in test functions
CHANGELOG.md Outdated Show resolved Hide resolved
src/themis/secure_session_peer.c Outdated Show resolved Hide resolved
0x1f, 0xe9, 0xea, 0x48, 0x11, 0xe1, 0xf9, 0x71, 0x8e, 0x24, 0x11, 0xcb, 0xfd, 0xc0, 0xa3,
0x6e, 0xd6, 0xac, 0x88, 0xb6, 0x44, 0xc2, 0x9a, 0x24, 0x84, 0xee, 0x50, 0x4c, 0x3e, 0xa0,
};
/* Test keys can be found in themis_secure_session_test_keys.h */
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

@@ -668,11 +666,40 @@ static void test_basic_flow_no_transport(void)
}
}

static void test_empty_peer_id(void)
{
memcpy(&(client.transport), &transport, sizeof(secure_session_user_callbacks_t));
Copy link
Contributor

Choose a reason for hiding this comment

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

remember: using memcpy is ok only for tests / non-sensitive data

testsuite_fail_if(client.session != NULL, "secure_session_create must fail with empty Peer ID");
}

static void test_invalid_private_key_type(void)
Copy link
Contributor

@vixentael vixentael Aug 11, 2020

Choose a reason for hiding this comment

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

Let's test public key and make sure that tests are passing and write comment why is that.

Copy link
Collaborator

Choose a reason for hiding this comment

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

IMO, it's would be better to split this into separate tests: RSA private keys, RSA public keys, EC public keys.

Copy link
Contributor

Choose a reason for hiding this comment

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

agree

* Mention GitHub issue # in changelog.
* Add one more check to `secure_session.c` to only allow EC private keys
  in `secure_session_create()`.
* Add more tests to make sure the session cannot be created with public EC
  and public RSA keys.
@iamnotacake iamnotacake marked this pull request as ready for review August 12, 2020 06:50
CHANGELOG.md Outdated Show resolved Hide resolved
src/themis/secure_session.c Outdated Show resolved Hide resolved
src/themis/secure_session_peer.c Outdated Show resolved Hide resolved
Comment on lines +682 to +683
memcpy(&(client_rsa.transport), &transport, sizeof(secure_session_user_callbacks_t));
client_rsa.transport.user_data = &client_rsa;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Well, this a weird way to setup a Secure Session – with global variables and memcpy() – but I'll let it pass given that all tests here are like that. It's not nice coding practice because it makes impossible to run the tests in parallel. However, refactoring this is a topic for another day.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I guess the easiest way to refactor this is to mark those glogal vars as thread local and then create as many threads as we like, so they will run in parallel. Another question is whether the test framework is ready for such changes. And these tests are really fast, like almost instant, and the only computation-heavy check is soter rand: NIST STS, which, btw, only uses one CPU core.

Copy link
Collaborator

Choose a reason for hiding this comment

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

You can also just create a new client context and callbacks for each test :) Which is arguably more readable and obvious than thread-local variables. It's more verbose, yeah, but dumb test code is better than smart one, IMO.

testsuite_fail_if(client.session != NULL, "secure_session_create must fail with empty Peer ID");
}

static void test_invalid_private_key_type(void)
Copy link
Collaborator

Choose a reason for hiding this comment

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

IMO, it's would be better to split this into separate tests: RSA private keys, RSA public keys, EC public keys.

tests/themis/themis_secure_session.c Show resolved Hide resolved
* Changed text in changelog
* Removed unneeded checks:
  - `themis_get_asym_key_kind()` already checks pointer and length
  - non-EC keys won't reach `secure_session_peer_init()` so we actually
    don't need that check
* Split tests into more funcs
Copy link
Collaborator

@ilammy ilammy left a comment

Choose a reason for hiding this comment

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

Nice work 👍

Comment on lines 21 to 22
#include "soter/soter_wipe.h"
#include "themis/secure_keygen.h"
Copy link
Collaborator

Choose a reason for hiding this comment

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

It guess it's not necessary to include this any more.

Forgot to remove in prev commit
@iamnotacake iamnotacake merged commit bef80bc into cossacklabs:master Aug 12, 2020
@iamnotacake iamnotacake deleted the anatolii-T1739-runtime-check-ec-keys-in-secure-session branch August 12, 2020 13:39
@ilammy ilammy mentioned this pull request Aug 17, 2020
2 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core Themis Core written in C, its packages tests Themis test suite
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants