libcli/smb/smb2_signing: rename smb2_key_deviration -> smb2_key_derivation
[ira/wip.git] / libcli / smb / smbXcli_base.c
index b35db9efcda128ed8ef3c0cd3984c724a70b46b7..bdb6e48030894bded07d5f43e5047e0a5cac285b 100644 (file)
@@ -96,6 +96,8 @@ struct smbXcli_conn {
 
                struct smb_signing_state *signing;
                struct smb_trans_enc_state *trans_enc;
+
+               struct tevent_req *read_braw_req;
        } smb1;
 
        struct {
@@ -131,10 +133,10 @@ struct smbXcli_session {
        struct {
                uint64_t session_id;
                uint16_t session_flags;
+               DATA_BLOB application_key;
                DATA_BLOB signing_key;
-               DATA_BLOB session_key;
                bool should_sign;
-               bool channel_setup;
+               DATA_BLOB channel_signing_key;
        } smb2;
 };
 
@@ -164,7 +166,7 @@ struct smbXcli_req_state {
                uint16_t *vwv;
                uint8_t bytecount_buf[2];
 
-#define MAX_SMB_IOV 5
+#define MAX_SMB_IOV 10
                /* length_hdr, hdr, words, byte_count, buffers */
                struct iovec iov[1 + 3 + MAX_SMB_IOV];
                int iov_count;
@@ -212,7 +214,7 @@ static int smbXcli_conn_destructor(struct smbXcli_conn *conn)
        }
 
        if (conn->smb1.trans_enc) {
-               common_free_encryption_state(&conn->smb1.trans_enc);
+               TALLOC_FREE(conn->smb1.trans_enc);
        }
 
        return 0;
@@ -422,6 +424,103 @@ const struct GUID *smbXcli_conn_server_guid(struct smbXcli_conn *conn)
        return &conn->smb1.server.guid;
 }
 
+struct smbXcli_conn_samba_suicide_state {
+       struct smbXcli_conn *conn;
+       struct iovec iov;
+       uint8_t buf[9];
+};
+
+static void smbXcli_conn_samba_suicide_done(struct tevent_req *subreq);
+
+struct tevent_req *smbXcli_conn_samba_suicide_send(TALLOC_CTX *mem_ctx,
+                                                  struct tevent_context *ev,
+                                                  struct smbXcli_conn *conn,
+                                                  uint8_t exitcode)
+{
+       struct tevent_req *req, *subreq;
+       struct smbXcli_conn_samba_suicide_state *state;
+
+       req = tevent_req_create(mem_ctx, &state,
+                               struct smbXcli_conn_samba_suicide_state);
+       if (req == NULL) {
+               return NULL;
+       }
+       state->conn = conn;
+       SIVAL(state->buf, 4, 0x74697865);
+       SCVAL(state->buf, 8, exitcode);
+       _smb_setlen_nbt(state->buf, sizeof(state->buf)-4);
+
+       state->iov.iov_base = state->buf;
+       state->iov.iov_len = sizeof(state->buf);
+
+       subreq = writev_send(state, ev, conn->outgoing, conn->write_fd,
+                            false, &state->iov, 1);
+       if (tevent_req_nomem(subreq, req)) {
+               return tevent_req_post(req, ev);
+       }
+       tevent_req_set_callback(subreq, smbXcli_conn_samba_suicide_done, req);
+       return req;
+}
+
+static void smbXcli_conn_samba_suicide_done(struct tevent_req *subreq)
+{
+       struct tevent_req *req = tevent_req_callback_data(
+               subreq, struct tevent_req);
+       struct smbXcli_conn_samba_suicide_state *state = tevent_req_data(
+               req, struct smbXcli_conn_samba_suicide_state);
+       ssize_t nwritten;
+       int err;
+
+       nwritten = writev_recv(subreq, &err);
+       TALLOC_FREE(subreq);
+       if (nwritten == -1) {
+               NTSTATUS status = map_nt_error_from_unix_common(err);
+               smbXcli_conn_disconnect(state->conn, status);
+               return;
+       }
+       tevent_req_done(req);
+}
+
+NTSTATUS smbXcli_conn_samba_suicide_recv(struct tevent_req *req)
+{
+       return tevent_req_simple_recv_ntstatus(req);
+}
+
+NTSTATUS smbXcli_conn_samba_suicide(struct smbXcli_conn *conn,
+                                   uint8_t exitcode)
+{
+       TALLOC_CTX *frame = talloc_stackframe();
+       struct tevent_context *ev;
+       struct tevent_req *req;
+       NTSTATUS status = NT_STATUS_NO_MEMORY;
+       bool ok;
+
+       if (smbXcli_conn_has_async_calls(conn)) {
+               /*
+                * Can't use sync call while an async call is in flight
+                */
+               status = NT_STATUS_INVALID_PARAMETER_MIX;
+               goto fail;
+       }
+       ev = tevent_context_init(frame);
+       if (ev == NULL) {
+               goto fail;
+       }
+       req = smbXcli_conn_samba_suicide_send(frame, ev, conn, exitcode);
+       if (req == NULL) {
+               goto fail;
+       }
+       ok = tevent_req_poll(req, ev);
+       if (!ok) {
+               status = map_nt_error_from_unix_common(errno);
+               goto fail;
+       }
+       status = smbXcli_conn_samba_suicide_recv(req);
+ fail:
+       TALLOC_FREE(frame);
+       return status;
+}
+
 uint32_t smb1cli_conn_capabilities(struct smbXcli_conn *conn)
 {
        return conn->smb1.capabilities;
@@ -447,6 +546,26 @@ uint16_t smb1cli_conn_server_security_mode(struct smbXcli_conn *conn)
        return conn->smb1.server.security_mode;
 }
 
+bool smb1cli_conn_server_readbraw(struct smbXcli_conn *conn)
+{
+       return conn->smb1.server.readbraw;
+}
+
+bool smb1cli_conn_server_writebraw(struct smbXcli_conn *conn)
+{
+       return conn->smb1.server.writebraw;
+}
+
+bool smb1cli_conn_server_lockread(struct smbXcli_conn *conn)
+{
+       return conn->smb1.server.lockread;
+}
+
+bool smb1cli_conn_server_writeunlock(struct smbXcli_conn *conn)
+{
+       return conn->smb1.server.writeunlock;
+}
+
 int smb1cli_conn_server_time_zone(struct smbXcli_conn *conn)
 {
        return conn->smb1.server.time_zone;
@@ -477,7 +596,7 @@ void smb1cli_conn_set_encryption(struct smbXcli_conn *conn,
 {
        /* Replace the old state, if any. */
        if (conn->smb1.trans_enc) {
-               common_free_encryption_state(&conn->smb1.trans_enc);
+               TALLOC_FREE(conn->smb1.trans_enc);
        }
        conn->smb1.trans_enc = es;
 }
@@ -743,6 +862,8 @@ static bool smbXcli_conn_receive_next(struct smbXcli_conn *conn)
 
 void smbXcli_conn_disconnect(struct smbXcli_conn *conn, NTSTATUS status)
 {
+       tevent_queue_stop(conn->outgoing);
+
        if (conn->read_fd != -1) {
                close(conn->read_fd);
        }
@@ -1062,7 +1183,7 @@ struct tevent_req *smb1cli_req_create(TALLOC_CTX *mem_ctx,
        SSVAL(state->smb1.hdr, HDR_PID,     pid);
        SSVAL(state->smb1.hdr, HDR_UID,     uid);
        SSVAL(state->smb1.hdr, HDR_MID,     0); /* this comes later */
-       SSVAL(state->smb1.hdr, HDR_WCT,     wct);
+       SCVAL(state->smb1.hdr, HDR_WCT,     wct);
 
        state->smb1.vwv = vwv;
 
@@ -1118,6 +1239,7 @@ static NTSTATUS smb1cli_conn_signv(struct smbXcli_conn *conn,
                                   uint32_t *seqnum,
                                   bool one_way_seqnum)
 {
+       TALLOC_CTX *frame = NULL;
        uint8_t *buf;
 
        /*
@@ -1141,7 +1263,9 @@ static NTSTATUS smb1cli_conn_signv(struct smbXcli_conn *conn,
                return NT_STATUS_INVALID_PARAMETER_MIX;
        }
 
-       buf = smbXcli_iov_concat(talloc_tos(), iov, iov_count);
+       frame = talloc_stackframe();
+
+       buf = smbXcli_iov_concat(frame, iov, iov_count);
        if (buf == NULL) {
                return NT_STATUS_NO_MEMORY;
        }
@@ -1151,7 +1275,7 @@ static NTSTATUS smb1cli_conn_signv(struct smbXcli_conn *conn,
        smb_signing_sign_pdu(conn->smb1.signing, buf, *seqnum);
        memcpy(iov[1].iov_base, buf+4, iov[1].iov_len);
 
-       TALLOC_FREE(buf);
+       TALLOC_FREE(frame);
        return NT_STATUS_OK;
 }
 
@@ -1166,6 +1290,7 @@ static NTSTATUS smb1cli_req_writev_submit(struct tevent_req *req,
 {
        struct tevent_req *subreq;
        NTSTATUS status;
+       uint8_t cmd;
        uint16_t mid;
 
        if (!smbXcli_conn_is_connected(state->conn)) {
@@ -1192,6 +1317,14 @@ static NTSTATUS smb1cli_req_writev_submit(struct tevent_req *req,
                return NT_STATUS_INVALID_PARAMETER_MIX;
        }
 
+       cmd = CVAL(iov[1].iov_base, HDR_COM);
+       if (cmd == SMBreadBraw) {
+               if (smbXcli_conn_has_async_calls(state->conn)) {
+                       return NT_STATUS_INVALID_PARAMETER_MIX;
+               }
+               state->conn->smb1.read_braw_req = req;
+       }
+
        if (state->smb1.mid != 0) {
                mid = state->smb1.mid;
        } else {
@@ -1576,6 +1709,40 @@ static NTSTATUS smb1cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
        size_t num_chained = 0;
        size_t num_responses = 0;
 
+       if (conn->smb1.read_braw_req != NULL) {
+               req = conn->smb1.read_braw_req;
+               conn->smb1.read_braw_req = NULL;
+               state = tevent_req_data(req, struct smbXcli_req_state);
+
+               smbXcli_req_unset_pending(req);
+
+               if (state->smb1.recv_iov == NULL) {
+                       /*
+                        * For requests with more than
+                        * one response, we have to readd the
+                        * recv_iov array.
+                        */
+                       state->smb1.recv_iov = talloc_zero_array(state,
+                                                                struct iovec,
+                                                                3);
+                       if (tevent_req_nomem(state->smb1.recv_iov, req)) {
+                               return NT_STATUS_OK;
+                       }
+               }
+
+               state->smb1.recv_iov[0].iov_base = (void *)(inbuf + NBT_HDR_SIZE);
+               state->smb1.recv_iov[0].iov_len = smb_len_nbt(inbuf);
+               ZERO_STRUCT(state->smb1.recv_iov[1]);
+               ZERO_STRUCT(state->smb1.recv_iov[2]);
+
+               state->smb1.recv_cmd = SMBreadBraw;
+               state->smb1.recv_status = NT_STATUS_OK;
+               state->inbuf = talloc_move(state->smb1.recv_iov, &inbuf);
+
+               tevent_req_done(req);
+               return NT_STATUS_OK;
+       }
+
        if ((IVAL(inhdr, 0) != SMB_MAGIC) /* 0xFF"SMB" */
            && (SVAL(inhdr, 0) != 0x45ff)) /* 0xFF"E" */ {
                DEBUG(10, ("Got non-SMB PDU\n"));
@@ -1672,6 +1839,20 @@ static NTSTATUS smb1cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
 
                smbXcli_req_unset_pending(req);
 
+               if (state->smb1.recv_iov == NULL) {
+                       /*
+                        * For requests with more than
+                        * one response, we have to readd the
+                        * recv_iov array.
+                        */
+                       state->smb1.recv_iov = talloc_zero_array(state,
+                                                                struct iovec,
+                                                                3);
+                       if (tevent_req_nomem(state->smb1.recv_iov, req)) {
+                               return NT_STATUS_OK;
+                       }
+               }
+
                state->smb1.recv_cmd = cmd;
                state->smb1.recv_status = status;
                state->inbuf = talloc_move(state->smb1.recv_iov, &inbuf);
@@ -1720,6 +1901,20 @@ static NTSTATUS smb1cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
                        continue;
                }
 
+               if (state->smb1.recv_iov == NULL) {
+                       /*
+                        * For requests with more than
+                        * one response, we have to readd the
+                        * recv_iov array.
+                        */
+                       state->smb1.recv_iov = talloc_zero_array(state,
+                                                                struct iovec,
+                                                                3);
+                       if (tevent_req_nomem(state->smb1.recv_iov, req)) {
+                               continue;
+                       }
+               }
+
                state->smb1.recv_cmd = cmd;
 
                if (i == (num_responses - 1)) {
@@ -1814,13 +2009,16 @@ NTSTATUS smb1cli_req_recv(struct tevent_req *req,
 
        if (state->inbuf != NULL) {
                recv_iov = state->smb1.recv_iov;
-               hdr = (uint8_t *)recv_iov[0].iov_base;
-               wct = recv_iov[1].iov_len/2;
-               vwv = (uint16_t *)recv_iov[1].iov_base;
-               vwv_offset = PTR_DIFF(vwv, hdr);
-               num_bytes = recv_iov[2].iov_len;
-               bytes = (uint8_t *)recv_iov[2].iov_base;
-               bytes_offset = PTR_DIFF(bytes, hdr);
+               state->smb1.recv_iov = NULL;
+               if (state->smb1.recv_cmd != SMBreadBraw) {
+                       hdr = (uint8_t *)recv_iov[0].iov_base;
+                       wct = recv_iov[1].iov_len/2;
+                       vwv = (uint16_t *)recv_iov[1].iov_base;
+                       vwv_offset = PTR_DIFF(vwv, hdr);
+                       num_bytes = recv_iov[2].iov_len;
+                       bytes = (uint8_t *)recv_iov[2].iov_base;
+                       bytes_offset = PTR_DIFF(bytes, hdr);
+               }
        }
 
        if (tevent_req_is_nterror(req, &status)) {
@@ -2261,7 +2459,7 @@ void smb2cli_req_set_notify_async(struct tevent_req *req)
        state->smb2.notify_async = true;
 }
 
-static void smb2cli_writev_done(struct tevent_req *subreq);
+static void smb2cli_req_writev_done(struct tevent_req *subreq);
 static NTSTATUS smb2cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
                                               TALLOC_CTX *tmp_mem,
                                               uint8_t *inbuf);
@@ -2297,7 +2495,7 @@ NTSTATUS smb2cli_req_compound_submit(struct tevent_req **reqs,
                uint16_t charge;
                uint16_t credits;
                uint64_t mid;
-               bool should_sign = false;
+               const DATA_BLOB *signing_key = NULL;
 
                if (!tevent_req_is_in_progress(reqs[i])) {
                        return NT_STATUS_INTERNAL_ERROR;
@@ -2389,16 +2587,43 @@ skip_credits:
                nbt_len += reqlen;
 
                if (state->session) {
-                       should_sign = state->session->smb2.should_sign;
-                       if (state->session->smb2.channel_setup) {
+                       bool should_sign = state->session->smb2.should_sign;
+
+                       if (opcode == SMB2_OP_SESSSETUP &&
+                           state->session->smb2.signing_key.length != 0) {
                                should_sign = true;
                        }
+
+                       /*
+                        * We prefer the channel signing key if it is
+                        * already there.
+                        */
+                       if (should_sign) {
+                               signing_key = &state->session->smb2.channel_signing_key;
+                       }
+
+                       /*
+                        * If it is a channel binding, we already have the main
+                        * signing key and try that one.
+                        */
+                       if (signing_key && signing_key->length == 0) {
+                               signing_key = &state->session->smb2.signing_key;
+                       }
+
+                       /*
+                        * If we do not have any session key yet, we skip the
+                        * signing of SMB2_OP_SESSSETUP requests.
+                        */
+                       if (signing_key && signing_key->length == 0) {
+                               signing_key = NULL;
+                       }
                }
 
-               if (should_sign) {
+               if (signing_key) {
                        NTSTATUS status;
 
-                       status = smb2_signing_sign_pdu(state->session->smb2.signing_key,
+                       status = smb2_signing_sign_pdu(*signing_key,
+                                                      state->session->conn->protocol,
                                                       &iov[hdr_iov], num_iov - hdr_iov);
                        if (!NT_STATUS_IS_OK(status)) {
                                return status;
@@ -2425,7 +2650,7 @@ skip_credits:
        if (subreq == NULL) {
                return NT_STATUS_NO_MEMORY;
        }
-       tevent_req_set_callback(subreq, smb2cli_writev_done, reqs[0]);
+       tevent_req_set_callback(subreq, smb2cli_req_writev_done, reqs[0]);
        return NT_STATUS_OK;
 }
 
@@ -2474,7 +2699,7 @@ struct tevent_req *smb2cli_req_send(TALLOC_CTX *mem_ctx,
        return req;
 }
 
-static void smb2cli_writev_done(struct tevent_req *subreq)
+static void smb2cli_req_writev_done(struct tevent_req *subreq)
 {
        struct tevent_req *req =
                tevent_req_callback_data(subreq,
@@ -2702,7 +2927,8 @@ static NTSTATUS smb2cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
 
                if (session) {
                        should_sign = session->smb2.should_sign;
-                       if (session->smb2.channel_setup) {
+                       if (opcode == SMB2_OP_SESSSETUP &&
+                           session->smb2.signing_key.length != 0) {
                                should_sign = true;
                        }
                }
@@ -2735,17 +2961,39 @@ static NTSTATUS smb2cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
                        }
 
                        last_session = session;
-                       signing_key = &session->smb2.signing_key;
+                       signing_key = &session->smb2.channel_signing_key;
                }
 
-               if ((opcode == SMB2_OP_SESSSETUP) &&
-                    NT_STATUS_IS_OK(status)) {
+               if (opcode == SMB2_OP_SESSSETUP) {
                        /*
-                        * the caller has to check the signing
-                        * as only the caller knows the correct
-                        * session key
+                        * We prefer the channel signing key, if it is
+                        * already there.
+                        *
+                        * If we do not have a channel signing key yet,
+                        * we try the main signing key, if it is not
+                        * the final response.
                         */
-                       signing_key = NULL;
+                       if (signing_key && signing_key->length == 0 &&
+                           !NT_STATUS_IS_OK(status)) {
+                               signing_key = &session->smb2.signing_key;
+                       }
+
+                       if (signing_key && signing_key->length == 0) {
+                               /*
+                                * If we do not have a session key to
+                                * verify the signature, we defer the
+                                * signing check to the caller.
+                                *
+                                * The caller gets NT_STATUS_OK, it
+                                * has to call
+                                * smb2cli_session_set_session_key()
+                                * or
+                                * smb2cli_session_set_channel_key()
+                                * which will check the signature
+                                * with the channel signing key.
+                                */
+                               signing_key = NULL;
+                       }
                }
 
                if (NT_STATUS_EQUAL(status, NT_STATUS_USER_SESSION_DELETED)) {
@@ -2755,9 +3003,7 @@ static NTSTATUS smb2cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
                         * propagate the NT_STATUS_USER_SESSION_DELETED
                         * status to the caller.
                         */
-                       if (signing_key) {
-                               signing_key = NULL;
-                       }
+                       signing_key = NULL;
                }
 
                if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_NAME_DELETED) ||
@@ -2801,7 +3047,9 @@ static NTSTATUS smb2cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
                }
 
                if (signing_key) {
-                       status = smb2_signing_check_pdu(*signing_key, cur, 3);
+                       status = smb2_signing_check_pdu(*signing_key,
+                                                       state->conn->protocol,
+                                                       cur, 3);
                        if (!NT_STATUS_IS_OK(status)) {
                                /*
                                 * If the signing check fails, we disconnect
@@ -2960,6 +3208,7 @@ static const struct {
        {PROTOCOL_SMB2_02,      SMB2_DIALECT_REVISION_202},
        {PROTOCOL_SMB2_10,      SMB2_DIALECT_REVISION_210},
        {PROTOCOL_SMB2_22,      SMB2_DIALECT_REVISION_222},
+       {PROTOCOL_SMB2_24,      SMB2_DIALECT_REVISION_224},
 };
 
 struct smbXcli_negprot_state {
@@ -3639,10 +3888,8 @@ static void smbXcli_negprot_smb2_done(struct tevent_req *subreq)
 
                /*
                 * send a SMB2 negprot, in order to negotiate
-                * the SMB2 dialect. This needs to use the
-                * message id 1.
+                * the SMB2 dialect.
                 */
-               state->conn->smb2.mid = 1;
                subreq = smbXcli_negprot_smb2_subreq(state);
                if (tevent_req_nomem(subreq, req)) {
                        return;
@@ -3722,6 +3969,11 @@ static NTSTATUS smbXcli_negprot_dispatch_incoming(struct smbXcli_conn *conn,
                        substate->smb1.recv_iov = NULL;
                }
 
+               /*
+                * we got an SMB2 answer, which consumed sequence number 0
+                * so we need to use 1 as the next one
+                */
+               conn->smb2.mid = 1;
                tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
                conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
                return smb2cli_conn_dispatch_incoming(conn, tmp_mem, inbuf);
@@ -3823,6 +4075,24 @@ uint64_t smb2cli_session_current_id(struct smbXcli_session *session)
        return session->smb2.session_id;
 }
 
+NTSTATUS smb2cli_session_application_key(struct smbXcli_session *session,
+                                        TALLOC_CTX *mem_ctx,
+                                        DATA_BLOB *key)
+{
+       *key = data_blob_null;
+
+       if (session->smb2.application_key.length == 0) {
+               return NT_STATUS_NO_USER_SESSION_KEY;
+       }
+
+       *key = data_blob_dup_talloc(mem_ctx, session->smb2.application_key);
+       if (key->data == NULL) {
+               return NT_STATUS_NO_MEMORY;
+       }
+
+       return NT_STATUS_OK;
+}
+
 void smb2cli_session_set_id_and_flags(struct smbXcli_session *session,
                                      uint64_t session_id,
                                      uint16_t session_flags)
@@ -3831,19 +4101,23 @@ void smb2cli_session_set_id_and_flags(struct smbXcli_session *session,
        session->smb2.session_flags = session_flags;
 }
 
-NTSTATUS smb2cli_session_update_session_key(struct smbXcli_session *session,
-                                           const DATA_BLOB session_key,
-                                           const struct iovec *recv_iov)
+NTSTATUS smb2cli_session_set_session_key(struct smbXcli_session *session,
+                                        const DATA_BLOB _session_key,
+                                        const struct iovec *recv_iov)
 {
        struct smbXcli_conn *conn = session->conn;
        uint16_t no_sign_flags;
-       DATA_BLOB signing_key;
+       uint8_t session_key[16];
        NTSTATUS status;
 
        if (conn == NULL) {
                return NT_STATUS_INVALID_PARAMETER_MIX;
        }
 
+       if (session->smb2.signing_key.length != 0) {
+               return NT_STATUS_INVALID_PARAMETER_MIX;
+       }
+
        no_sign_flags = SMB2_SESSION_FLAG_IS_GUEST | SMB2_SESSION_FLAG_IS_NULL;
 
        if (session->smb2.session_flags & no_sign_flags) {
@@ -3851,42 +4125,63 @@ NTSTATUS smb2cli_session_update_session_key(struct smbXcli_session *session,
                return NT_STATUS_OK;
        }
 
-       if (session->smb2.signing_key.length > 0) {
-               signing_key = session->smb2.signing_key;
-       } else {
-               signing_key = session_key;
-       }
-       if (session->smb2.channel_setup) {
-               signing_key = session_key;
-       }
+       ZERO_STRUCT(session_key);
+       memcpy(session_key, _session_key.data,
+              MIN(_session_key.length, sizeof(session_key)));
 
-       status = smb2_signing_check_pdu(signing_key, recv_iov, 3);
-       if (!NT_STATUS_IS_OK(status)) {
-               return status;
+       session->smb2.signing_key = data_blob_talloc(session,
+                                                    session_key,
+                                                    sizeof(session_key));
+       if (session->smb2.signing_key.data == NULL) {
+               ZERO_STRUCT(session_key);
+               return NT_STATUS_NO_MEMORY;
        }
 
-       if (!session->smb2.channel_setup) {
-               session->smb2.session_key = data_blob_dup_talloc(session,
-                                                                session_key);
-               if (session->smb2.session_key.data == NULL) {
-                       return NT_STATUS_NO_MEMORY;
-               }
+       if (conn->protocol >= PROTOCOL_SMB2_24) {
+#define _STRING_BLOB(x) data_blob_const((const uint8_t *)(x), sizeof(x))
+               const DATA_BLOB label = _STRING_BLOB("SMB2AESCMAC");
+               const DATA_BLOB context = _STRING_BLOB("SmbSign");
+#undef _STRING_BLOB
+
+               smb2_key_derivation(session_key, sizeof(session_key),
+                                   label.data, label.length,
+                                   context.data, context.length,
+                                   session->smb2.signing_key.data);
        }
 
-       if (session->smb2.channel_setup) {
-               data_blob_free(&session->smb2.signing_key);
-               session->smb2.channel_setup = false;
+       session->smb2.application_key = data_blob_dup_talloc(session,
+                                               session->smb2.signing_key);
+       if (session->smb2.application_key.data == NULL) {
+               ZERO_STRUCT(session_key);
+               return NT_STATUS_NO_MEMORY;
        }
 
-       if (session->smb2.signing_key.length > 0) {
-               return NT_STATUS_OK;
+       if (conn->protocol >= PROTOCOL_SMB2_24) {
+#define _STRING_BLOB(x) data_blob_const((const uint8_t *)(x), sizeof(x))
+               const DATA_BLOB label = _STRING_BLOB("SMB2APP");
+               const DATA_BLOB context = _STRING_BLOB("SmbRpc");
+#undef _STRING_BLOB
+
+               smb2_key_derivation(session_key, sizeof(session_key),
+                                   label.data, label.length,
+                                   context.data, context.length,
+                                   session->smb2.application_key.data);
        }
+       ZERO_STRUCT(session_key);
 
-       session->smb2.signing_key = data_blob_dup_talloc(session, signing_key);
-       if (session->smb2.signing_key.data == NULL) {
+       session->smb2.channel_signing_key = data_blob_dup_talloc(session,
+                                               session->smb2.signing_key);
+       if (session->smb2.channel_signing_key.data == NULL) {
                return NT_STATUS_NO_MEMORY;
        }
 
+       status = smb2_signing_check_pdu(session->smb2.channel_signing_key,
+                                       session->conn->protocol,
+                                       recv_iov, 3);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
+       }
+
        session->smb2.should_sign = false;
 
        if (conn->desire_signing) {
@@ -3906,17 +4201,6 @@ NTSTATUS smb2cli_session_create_channel(TALLOC_CTX *mem_ctx,
                                        struct smbXcli_session **_session2)
 {
        struct smbXcli_session *session2;
-       uint16_t no_sign_flags;
-
-       no_sign_flags = SMB2_SESSION_FLAG_IS_GUEST | SMB2_SESSION_FLAG_IS_NULL;
-
-       if (session1->smb2.session_flags & no_sign_flags) {
-               return NT_STATUS_INVALID_PARAMETER_MIX;
-       }
-
-       if (session1->smb2.session_key.length == 0) {
-               return NT_STATUS_INVALID_PARAMETER_MIX;
-       }
 
        if (session1->smb2.signing_key.length == 0) {
                return NT_STATUS_INVALID_PARAMETER_MIX;
@@ -3933,12 +4217,6 @@ NTSTATUS smb2cli_session_create_channel(TALLOC_CTX *mem_ctx,
        session2->smb2.session_id = session1->smb2.session_id;
        session2->smb2.session_flags = session1->smb2.session_flags;
 
-       session2->smb2.session_key = data_blob_dup_talloc(session2,
-                                               session1->smb2.session_key);
-       if (session2->smb2.session_key.data == NULL) {
-               return NT_STATUS_NO_MEMORY;
-       }
-
        session2->smb2.signing_key = data_blob_dup_talloc(session2,
                                                session1->smb2.signing_key);
        if (session2->smb2.signing_key.data == NULL) {
@@ -3946,7 +4224,6 @@ NTSTATUS smb2cli_session_create_channel(TALLOC_CTX *mem_ctx,
        }
 
        session2->smb2.should_sign = session1->smb2.should_sign;
-       session2->smb2.channel_setup = true;
 
        talloc_set_destructor(session2, smbXcli_session_destructor);
        DLIST_ADD_END(conn->sessions, session2, struct smbXcli_session *);
@@ -3955,3 +4232,54 @@ NTSTATUS smb2cli_session_create_channel(TALLOC_CTX *mem_ctx,
        *_session2 = session2;
        return NT_STATUS_OK;
 }
+
+NTSTATUS smb2cli_session_set_channel_key(struct smbXcli_session *session,
+                                        const DATA_BLOB _channel_key,
+                                        const struct iovec *recv_iov)
+{
+       struct smbXcli_conn *conn = session->conn;
+       uint8_t channel_key[16];
+       NTSTATUS status;
+
+       if (conn == NULL) {
+               return NT_STATUS_INVALID_PARAMETER_MIX;
+       }
+
+       if (session->smb2.channel_signing_key.length != 0) {
+               return NT_STATUS_INVALID_PARAMETER_MIX;
+       }
+
+       ZERO_STRUCT(channel_key);
+       memcpy(channel_key, _channel_key.data,
+              MIN(_channel_key.length, sizeof(channel_key)));
+
+       session->smb2.channel_signing_key = data_blob_talloc(session,
+                                               channel_key,
+                                               sizeof(channel_key));
+       if (session->smb2.channel_signing_key.data == NULL) {
+               ZERO_STRUCT(channel_key);
+               return NT_STATUS_NO_MEMORY;
+       }
+
+       if (conn->protocol >= PROTOCOL_SMB2_24) {
+#define _STRING_BLOB(x) data_blob_const((const uint8_t *)(x), sizeof(x))
+               const DATA_BLOB label = _STRING_BLOB("SMB2AESCMAC");
+               const DATA_BLOB context = _STRING_BLOB("SmbSign");
+#undef _STRING_BLOB
+
+               smb2_key_derivation(channel_key, sizeof(channel_key),
+                                   label.data, label.length,
+                                   context.data, context.length,
+                                   session->smb2.channel_signing_key.data);
+       }
+       ZERO_STRUCT(channel_key);
+
+       status = smb2_signing_check_pdu(session->smb2.channel_signing_key,
+                                       session->conn->protocol,
+                                       recv_iov, 3);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
+       }
+
+       return NT_STATUS_OK;
+}