Skip to content

support encrypt-then-mac (etm) MACs - #655

Closed
palmin wants to merge 43 commits into
libssh2:masterfrom
palmin:encrypt-then-mac
Closed

palmin wants to merge 43 commits into
libssh2:masterfrom
palmin:encrypt-then-mac

Conversation

@palmin

@palmin palmin commented Dec 25, 2021

Copy link
Copy Markdown
Contributor

Support for calculating MAC (message authentication code) on encrypted data instead of plain text data.

This adds support for the following MACs:
[email protected]
[email protected]
[email protected]

Some hardened SSH servers only accept etm MACs.

@palmin palmin mentioned this pull request Dec 25, 2021
@willco007

Copy link
Copy Markdown
Member

@palmin I do want to review this; I've been up against a deadline so my time is limited to this project. If you'd submit a resolved update I'll take a look.

@jakob

jakob commented Jul 21, 2022

Copy link
Copy Markdown
Contributor

I've built my app against this PR and tried connecting to the SSH server with each of the following settings in /etc/sshd_config:

It seemed to work fine with all of them!

Comment thread src/transport.c
p->payload,
session->fullpacket_payload_len,
&session->remote.mac_abstract);
int etm = session->remote.mac->etm;

@willco007 willco007 Sep 29, 2022 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

My attempt at etm support was considerably less change in fullpacket(). Here's the changes I've made for comparison:

    int compressed;
    int required_full_packet = encrypted && session->remote.mac->etm == 1;
	const LIBSSH2_MAC_METHOD *remote_mac = NULL;
	
	if ( required_full_packet ) {
		if ( session->remote.crypt->auth_len == 0 )
			remote_mac = session->remote.mac;
	} else if ( encrypted ) {
		remote_mac = session->remote.mac;
	}
	
    if (session->fullpacket_state == libssh2_NB_state_idle) {
		
		if (!required_full_packet || (required_full_packet && remote_mac == NULL) )
            session->fullpacket_macstate = LIBSSH2_MAC_CONFIRMED;
		
		session->fullpacket_payload_len = p->packet_length - 1;

        if (encrypted) {
			
            if (!required_full_packet && remote_mac) {
                /* Calculate MAC hash */
                remote_mac->hash(session, macbuf,  /* store hash here */
                              session->remote.seqno,
                              p->init, 5,
                              p->payload,
                              session->fullpacket_payload_len,
                              &session->remote.mac_abstract);

                /* Compare the calculated hash with the MAC we just read from
                 * the network. The read one is at the very end of the payload
                 * buffer. Note that 'payload_len' here is the packet_length
                 * field which includes the padding but not the MAC.
                 */
                if (memcmp(macbuf, p->payload + session->fullpacket_payload_len,
                           session->remote.mac->mac_len)) {
                    session->fullpacket_macstate = LIBSSH2_MAC_INVALID;
                }
            }
            else {
                //etm trim off padding byte from payload
                memmove(p->payload, &p->payload[1], p->packet_length - 1);
            }
        }

@palmin palmin Nov 17, 2022 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Your change is indeed much nicer @willco007.

I merged in the latest commits from master and replaced my change with yours but it requires auth_len in _LIBSSH2_CRYPT_METHOD failing with:

[ 19%] Building C object src/CMakeFiles/libssh2.dir/transport.c.o
/Users/ander/opgaver/ShellFish/related/encrypt-then-mac/libssh2/src/transport.c:175:36: error: no member named 'auth_len' in 'struct _LIBSSH2_CRYPT_METHOD'
        if (session->remote.crypt->auth_len == 0) {
            ~~~~~~~~~~~~~~~~~~~~~  ^
1 error generated.

_LIBSSH2_CRYPT_METHOD is currently:

libssh2/src/libssh2_priv.h

Lines 926 to 948 in 821d50d

struct _LIBSSH2_CRYPT_METHOD
{
const char *name;
const char *pem_annotation;
int blocksize;
/* iv and key sizes (-1 for variable length) */
int iv_len;
int secret_len;
long flags;
int (*init) (LIBSSH2_SESSION * session,
const LIBSSH2_CRYPT_METHOD * method, unsigned char *iv,
int *free_iv, unsigned char *secret, int *free_secret,
int encrypt, void **abstract);
int (*crypt) (LIBSSH2_SESSION * session, unsigned char *block,
size_t blocksize, void **abstract);
int (*dtor) (LIBSSH2_SESSION * session, void **abstract);
_libssh2_cipher_type(algo);
};

I presume auth_len is crucial for ciphers where encryption and authentication are not separate but I'm unsure how to proceed with this pull request.

Can I just assume that auth_len is always zero?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yes, auth_len is for chacha20 support; so assuming it's zero is correct.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Great. Your suggestion has been applied and I'm hoping for the automated tests to settle on green.

Comment thread src/wincng.c Outdated
return NULL;
}

/* _libssh2_supported_key_sign_algorithms

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is currently on master; pull up should grab it.

palmin added a commit to palmin/libssh2 that referenced this pull request Nov 17, 2022
@willco007

Copy link
Copy Markdown
Member

@palmin could you pull this up one more time and hopefully we can get all green and get this merged.

@palmin

palmin commented Feb 28, 2023

Copy link
Copy Markdown
Contributor Author

@willco007 Did a merge with master, crossing my fingers and awaiting approval for tests to run.

@palmin

palmin commented Feb 28, 2023

Copy link
Copy Markdown
Contributor Author

@willco007

Copy link
Copy Markdown
Member

looks like the first error is this:

C:\projects\libssh2\src\wincng.c(2690): error C2084: function 'const char *_libssh2_supported_key_sign_algorithms(LIBSSH2_SESSION *,unsigned char *,std::size_t)' already has a body [C:\projects\libssh2\_builds\src\libssh2.vcxproj]
[300](https://ci.appveyor.com/project/libssh2org/libssh2/builds/46367540/job/w8ns0vpbrrh19p8l#L300)  C:\projects\libssh2\src\wincng.c(2664): note: see previous definition of '_libssh2_supported_key_sign_algorithms'
[301](https://ci.appveyor.com/project/libssh2org/libssh2/builds/46367540/job/w8ns0vpbrrh19p8l#L301)

@willco007

Copy link
Copy Markdown
Member

Master has _libssh2_supported_key_sign_algorithms implemented in wincng.c while your MR still has it stubbed out which doesn't seem like your merge was successful and is probably leading to this double define.

@palmin
palmin force-pushed the encrypt-then-mac branch from 85038d2 to 7c43d58 Compare March 20, 2023 10:10
@palmin
palmin force-pushed the encrypt-then-mac branch from c37ea54 to a420005 Compare March 21, 2023 13:11
Comment thread src/transport.c Resolved
Comment thread src/transport.c Resolved
Comment thread tests/CMakeLists.txt Resolved
Comment thread src/transport.c Resolved
Comment thread src/transport.c Resolved
Comment thread src/transport.c Resolved
Comment thread src/transport.c Resolved
vszakats pushed a commit to vszakats/libssh2 that referenced this pull request Apr 20, 2023
Support for calculating MAC (message authentication code) on encrypted data instead of plain text data.

This adds support for the following MACs:
  [email protected]
  [email protected]
  [email protected]

Closes libssh2#655
vszakats pushed a commit to vszakats/libssh2 that referenced this pull request Apr 20, 2023
Support for calculating MAC (message authentication code) on encrypted data instead of plain text data.

This adds support for the following MACs:
  [email protected]
  [email protected]
  [email protected]

Integration-patches-by: Viktor Szakats

* rebase on master
* fix checksrc warnings
* fix compiler warnings
* fix indent/whitespace/eol
* merge with AES-GCM patch

Closes libssh2#655
Closes libssh2#987
@vszakats

Copy link
Copy Markdown
Member

Rebase onto master is continued in #987.

vszakats added a commit that referenced this pull request Apr 21, 2023
Support for calculating MAC (message authentication code) on encrypted
data instead of plain text data.

This adds support for the following MACs:
- `[email protected]`
- `[email protected]`
- `[email protected]`

Integration-patches-by: Viktor Szakats

* rebase on master
* fix checksec warnings
* fix compiler warning
* fix indent/whitespace/eol
* rebase/manual merge onto AES-GCM patch #797
* more manual merge of `libssh2_transport_send()` based
  on dfandrich/shellfish

Fixes #582
Closes #655
Closes #987
agreppin pushed a commit to agreppin/libssh2 that referenced this pull request Jul 14, 2024
- `ctest` shows a the default timeout '10000000' (turns out to be
  in seconds), cause infinite waits e.g. in case the necessary server
  worker is not available.

  CMake CI tests take approx:
  - GitHub   / Linux  : 125 seconds
  - AppVeyor / Windows: 300 seconds

  New timeouts are: 450 and 900 seconds respectively.

- set timeouts for style-check, fuzz, Linux and Windows GitHub CI
  jobs to avoid hanging forever.

Also:

- move `choco install` to before_test to make builds start faster
  in `appveyor.yml`.

- fix some yamllint `ON`/`OFF`-confusion issue by quoting these
  values in `appveyor.yml`.

- fix indentation in `appveyor.yml`.

- convert to GitHub workflows to LF line-ending.

Ref: libssh2#655 (comment)

Closes libssh2#851
agreppin pushed a commit to agreppin/libssh2 that referenced this pull request Jul 14, 2024
Support for calculating MAC (message authentication code) on encrypted
data instead of plain text data.

This adds support for the following MACs:
- `[email protected]`
- `[email protected]`
- `[email protected]`

Integration-patches-by: Viktor Szakats

* rebase on master
* fix checksec warnings
* fix compiler warning
* fix indent/whitespace/eol
* rebase/manual merge onto AES-GCM patch libssh2#797
* more manual merge of `libssh2_transport_send()` based
  on dfandrich/shellfish

Fixes libssh2#582
Closes libssh2#655
Closes libssh2#987
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants