s3-librpc Use gensec_sig_size() instead of a fixed NTLMSSP_SIG_SIZE
[ddiss/samba.git] / source3 / librpc / rpc / dcerpc_helpers.c
index 592832bdd8085d127cda0e434d752d1fd7f3fcf1..1e8edc334715cb4c383df04cb7fff44b2bc7f38a 100644 (file)
 #include "librpc/gen_ndr/ndr_schannel.h"
 #include "../libcli/auth/schannel.h"
 #include "../libcli/auth/spnego.h"
-#include "../libcli/auth/ntlmssp.h"
+#include "../auth/ntlmssp/ntlmssp.h"
 #include "ntlmssp_wrap.h"
-#include "librpc/rpc/dcerpc_gssapi.h"
-#include "librpc/rpc/dcerpc_spnego.h"
+#include "librpc/crypto/gse.h"
+#include "librpc/crypto/spnego.h"
+#include "auth/gensec/gensec.h"
 
 #undef DBGC_CLASS
 #define DBGC_CLASS DBGC_RPC_PARSE
@@ -93,6 +94,7 @@ NTSTATUS dcerpc_push_ncacn_packet(TALLOC_CTX *mem_ctx,
 *                      elements
 * @param blob          The blob of data to decode
 * @param r             An empty ncacn_packet, must not be NULL
+* @param bigendian     Whether the packet is bignedian encoded
 *
 * @return a NTSTATUS error code
 */
@@ -241,11 +243,148 @@ NTSTATUS dcerpc_pull_dcerpc_auth(TALLOC_CTX *mem_ctx,
        return NT_STATUS_OK;
 }
 
+/**
+* @brief Calculate how much data we can in a packet, including calculating
+*       auth token and pad lengths.
+*
+* @param auth          The pipe_auth_data structure for this pipe.
+* @param header_len    The length of the packet header
+* @param data_left     The data left in the send buffer
+* @param max_xmit_frag The max fragment size.
+* @param pad_alignment The NDR padding size.
+* @param data_to_send  [out] The max data we will send in the pdu
+* @param frag_len      [out] The total length of the fragment
+* @param auth_len      [out] The length of the auth trailer
+* @param pad_len       [out] The padding to be applied
+*
+* @return A NT Error status code.
+*/
+NTSTATUS dcerpc_guess_sizes(struct pipe_auth_data *auth,
+                           size_t header_len, size_t data_left,
+                           size_t max_xmit_frag, size_t pad_alignment,
+                           size_t *data_to_send, size_t *frag_len,
+                           size_t *auth_len, size_t *pad_len)
+{
+       size_t max_len;
+       size_t mod_len;
+       struct gensec_security *gensec_security;
+       struct schannel_state *schannel_auth;
+       struct spnego_context *spnego_ctx;
+       struct gse_context *gse_ctx;
+       enum spnego_mech auth_type;
+       void *auth_ctx;
+       bool seal = false;
+       NTSTATUS status;
+
+       /* no auth token cases first */
+       switch (auth->auth_level) {
+       case DCERPC_AUTH_LEVEL_NONE:
+       case DCERPC_AUTH_LEVEL_CONNECT:
+       case DCERPC_AUTH_LEVEL_PACKET:
+               max_len = max_xmit_frag - header_len;
+               *data_to_send = MIN(max_len, data_left);
+               *pad_len = 0;
+               *auth_len = 0;
+               *frag_len = header_len + *data_to_send;
+               return NT_STATUS_OK;
+
+       case DCERPC_AUTH_LEVEL_PRIVACY:
+               seal = true;
+               break;
+
+       case DCERPC_AUTH_LEVEL_INTEGRITY:
+               break;
+
+       default:
+               return NT_STATUS_INVALID_PARAMETER;
+       }
+
+
+       /* Sign/seal case, calculate auth and pad lengths */
+
+       max_len = max_xmit_frag - header_len - DCERPC_AUTH_TRAILER_LENGTH;
+
+       /* Treat the same for all authenticated rpc requests. */
+       switch (auth->auth_type) {
+       case DCERPC_AUTH_TYPE_SPNEGO:
+               spnego_ctx = talloc_get_type_abort(auth->auth_ctx,
+                                                  struct spnego_context);
+               status = spnego_get_negotiated_mech(spnego_ctx,
+                                                   &auth_type, &auth_ctx);
+               if (!NT_STATUS_IS_OK(status)) {
+                       return status;
+               }
+               switch (auth_type) {
+               case SPNEGO_NTLMSSP:
+                       gensec_security = talloc_get_type_abort(auth_ctx,
+                                                               struct gensec_security);
+                       *auth_len = gensec_sig_size(gensec_security, max_len);
+                       break;
+
+               case SPNEGO_KRB5:
+                       gse_ctx = talloc_get_type_abort(auth_ctx,
+                                                       struct gse_context);
+                       if (!gse_ctx) {
+                               return NT_STATUS_INVALID_PARAMETER;
+                       }
+                       *auth_len = gse_get_signature_length(gse_ctx,
+                                                            seal, max_len);
+                       break;
+
+               default:
+                       return NT_STATUS_INVALID_PARAMETER;
+               }
+               break;
+
+       case DCERPC_AUTH_TYPE_NTLMSSP:
+               gensec_security = talloc_get_type_abort(auth->auth_ctx,
+                                                       struct gensec_security);
+               *auth_len = gensec_sig_size(gensec_security, max_len);
+               break;
+
+       case DCERPC_AUTH_TYPE_SCHANNEL:
+               schannel_auth = talloc_get_type_abort(auth->auth_ctx,
+                                                     struct schannel_state);
+               *auth_len = netsec_outgoing_sig_size(schannel_auth);
+               break;
+
+       case DCERPC_AUTH_TYPE_KRB5:
+               gse_ctx = talloc_get_type_abort(auth->auth_ctx,
+                                               struct gse_context);
+               *auth_len = gse_get_signature_length(gse_ctx,
+                                                    seal, max_len);
+               break;
+
+       default:
+               return NT_STATUS_INVALID_PARAMETER;
+       }
+
+       max_len -= *auth_len;
+
+       *data_to_send = MIN(max_len, data_left);
+
+       mod_len = (header_len + *data_to_send) % pad_alignment;
+       if (mod_len) {
+               *pad_len = pad_alignment - mod_len;
+       } else {
+               *pad_len = 0;
+       }
+
+       if (*data_to_send + *pad_len > max_len) {
+               *data_to_send -= pad_alignment;
+       }
+
+       *frag_len = header_len + *data_to_send + *pad_len
+                       + DCERPC_AUTH_TRAILER_LENGTH + *auth_len;
+
+       return NT_STATUS_OK;
+}
+
 /*******************************************************************
  Create and add the NTLMSSP sign/seal auth data.
  ********************************************************************/
 
-static NTSTATUS add_ntlmssp_auth_footer(struct auth_ntlmssp_state *auth_state,
+static NTSTATUS add_ntlmssp_auth_footer(struct gensec_security *gensec_security,
                                        enum dcerpc_AuthLevel auth_level,
                                        DATA_BLOB *rpc_out)
 {
@@ -255,21 +394,21 @@ static NTSTATUS add_ntlmssp_auth_footer(struct auth_ntlmssp_state *auth_state,
        DATA_BLOB auth_blob;
        NTSTATUS status;
 
-       if (!auth_state) {
+       if (!gensec_security) {
                return NT_STATUS_INVALID_PARAMETER;
        }
 
        switch (auth_level) {
        case DCERPC_AUTH_LEVEL_PRIVACY:
                /* Data portion is encrypted. */
-               status = auth_ntlmssp_seal_packet(auth_state,
-                                            rpc_out->data,
-                                            rpc_out->data
-                                               + DCERPC_RESPONSE_LENGTH,
-                                            data_and_pad_len,
-                                            rpc_out->data,
-                                            rpc_out->length,
-                                            &auth_blob);
+               status = gensec_seal_packet(gensec_security,
+                                           rpc_out->data,
+                                           rpc_out->data
+                                           + DCERPC_RESPONSE_LENGTH,
+                                           data_and_pad_len,
+                                           rpc_out->data,
+                                           rpc_out->length,
+                                           &auth_blob);
                if (!NT_STATUS_IS_OK(status)) {
                        return status;
                }
@@ -277,14 +416,14 @@ static NTSTATUS add_ntlmssp_auth_footer(struct auth_ntlmssp_state *auth_state,
 
        case DCERPC_AUTH_LEVEL_INTEGRITY:
                /* Data is signed. */
-               status = auth_ntlmssp_sign_packet(auth_state,
-                                            rpc_out->data,
-                                            rpc_out->data
-                                               + DCERPC_RESPONSE_LENGTH,
-                                            data_and_pad_len,
-                                            rpc_out->data,
-                                            rpc_out->length,
-                                            &auth_blob);
+               status = gensec_sign_packet(gensec_security,
+                                           rpc_out->data,
+                                           rpc_out->data
+                                           + DCERPC_RESPONSE_LENGTH,
+                                           data_and_pad_len,
+                                           rpc_out->data,
+                                           rpc_out->length,
+                                           &auth_blob);
                if (!NT_STATUS_IS_OK(status)) {
                        return status;
                }
@@ -313,7 +452,7 @@ static NTSTATUS add_ntlmssp_auth_footer(struct auth_ntlmssp_state *auth_state,
  Check/unseal the NTLMSSP auth data. (Unseal in place).
  ********************************************************************/
 
-static NTSTATUS get_ntlmssp_auth_footer(struct auth_ntlmssp_state *auth_state,
+static NTSTATUS get_ntlmssp_auth_footer(struct gensec_security *gensec_security,
                                        enum dcerpc_AuthLevel auth_level,
                                        DATA_BLOB *data, DATA_BLOB *full_pkt,
                                        DATA_BLOB *auth_token)
@@ -321,21 +460,21 @@ static NTSTATUS get_ntlmssp_auth_footer(struct auth_ntlmssp_state *auth_state,
        switch (auth_level) {
        case DCERPC_AUTH_LEVEL_PRIVACY:
                /* Data portion is encrypted. */
-               return auth_ntlmssp_unseal_packet(auth_state,
-                                                 data->data,
-                                                 data->length,
-                                                 full_pkt->data,
-                                                 full_pkt->length,
-                                                 auth_token);
+               return gensec_unseal_packet(gensec_security,
+                                           data->data,
+                                           data->length,
+                                           full_pkt->data,
+                                           full_pkt->length,
+                                           auth_token);
 
        case DCERPC_AUTH_LEVEL_INTEGRITY:
                /* Data is signed. */
-               return auth_ntlmssp_check_packet(auth_state,
-                                                data->data,
-                                                data->length,
-                                                full_pkt->data,
-                                                full_pkt->length,
-                                                auth_token);
+               return gensec_check_packet(gensec_security,
+                                          data->data,
+                                          data->length,
+                                          full_pkt->data,
+                                          full_pkt->length,
+                                          auth_token);
 
        default:
                return NT_STATUS_INVALID_PARAMETER;
@@ -420,7 +559,7 @@ static NTSTATUS get_schannel_auth_footer(TALLOC_CTX *mem_ctx,
        case DCERPC_AUTH_LEVEL_PRIVACY:
                /* Data portion is encrypted. */
                return netsec_incoming_packet(auth_state,
-                                               mem_ctx, true,
+                                               true,
                                                data->data,
                                                data->length,
                                                auth_token);
@@ -428,7 +567,7 @@ static NTSTATUS get_schannel_auth_footer(TALLOC_CTX *mem_ctx,
        case DCERPC_AUTH_LEVEL_INTEGRITY:
                /* Data is signed. */
                return netsec_incoming_packet(auth_state,
-                                               mem_ctx, false,
+                                               false,
                                                data->data,
                                                data->length,
                                                auth_token);
@@ -522,42 +661,82 @@ static NTSTATUS add_spnego_auth_footer(struct spnego_context *spnego_ctx,
                                        enum dcerpc_AuthLevel auth_level,
                                        DATA_BLOB *rpc_out)
 {
-       enum dcerpc_AuthType auth_type;
-       struct gse_context *gse_ctx;
-       void *auth_ctx;
+       DATA_BLOB auth_blob;
+       DATA_BLOB rpc_data;
        NTSTATUS status;
 
        if (!spnego_ctx) {
                return NT_STATUS_INVALID_PARAMETER;
        }
 
-       status = spnego_get_negotiated_mech(spnego_ctx,
-                                           &auth_type, &auth_ctx);
-       if (!NT_STATUS_IS_OK(status)) {
-               return status;
-       }
+       rpc_data = data_blob_const(rpc_out->data
+                                       + DCERPC_RESPONSE_LENGTH,
+                                  rpc_out->length
+                                       - DCERPC_RESPONSE_LENGTH
+                                       - DCERPC_AUTH_TRAILER_LENGTH);
 
-       switch (auth_type) {
-       case DCERPC_AUTH_TYPE_KRB5:
-               gse_ctx = talloc_get_type(auth_ctx, struct gse_context);
-               if (!gse_ctx) {
-                       status = NT_STATUS_INTERNAL_ERROR;
-                       break;
+       switch (auth_level) {
+       case DCERPC_AUTH_LEVEL_PRIVACY:
+               /* Data portion is encrypted. */
+               status = spnego_seal(rpc_out->data, spnego_ctx,
+                                    &rpc_data, rpc_out, &auth_blob);
+               break;
+
+               if (!NT_STATUS_IS_OK(status)) {
+                       return status;
                }
-               status = add_gssapi_auth_footer(gse_ctx,
-                                               auth_level, rpc_out);
                break;
 
        case DCERPC_AUTH_LEVEL_INTEGRITY:
-               status = NT_STATUS_NOT_IMPLEMENTED;
+               /* Data is signed. */
+               status = spnego_sign(rpc_out->data, spnego_ctx,
+                                    &rpc_data, rpc_out, &auth_blob);
                break;
 
-       default:
-               status = NT_STATUS_INTERNAL_ERROR;
+               if (!NT_STATUS_IS_OK(status)) {
+                       return status;
+               }
                break;
+
+       default:
+               /* Can't happen. */
+               smb_panic("bad auth level");
+               /* Notreached. */
+               return NT_STATUS_INVALID_PARAMETER;
        }
 
-       return status;
+       /* Finally attach the blob. */
+       if (!data_blob_append(NULL, rpc_out,
+                               auth_blob.data, auth_blob.length)) {
+               DEBUG(0, ("Failed to add %u bytes auth blob.\n",
+                         (unsigned int)auth_blob.length));
+               return NT_STATUS_NO_MEMORY;
+       }
+       data_blob_free(&auth_blob);
+
+       return NT_STATUS_OK;
+}
+
+static NTSTATUS get_spnego_auth_footer(TALLOC_CTX *mem_ctx,
+                                       struct spnego_context *sp_ctx,
+                                       enum dcerpc_AuthLevel auth_level,
+                                       DATA_BLOB *data, DATA_BLOB *full_pkt,
+                                       DATA_BLOB *auth_token)
+{
+       switch (auth_level) {
+       case DCERPC_AUTH_LEVEL_PRIVACY:
+               /* Data portion is encrypted. */
+               return spnego_unseal(mem_ctx, sp_ctx,
+                                    data, full_pkt, auth_token);
+
+       case DCERPC_AUTH_LEVEL_INTEGRITY:
+               /* Data is signed. */
+               return spnego_sigcheck(mem_ctx, sp_ctx,
+                                      data, full_pkt, auth_token);
+
+       default:
+               return NT_STATUS_INVALID_PARAMETER;
+       }
 }
 
 /**
@@ -572,12 +751,17 @@ static NTSTATUS add_spnego_auth_footer(struct spnego_context *spnego_ctx,
 NTSTATUS dcerpc_add_auth_footer(struct pipe_auth_data *auth,
                                size_t pad_len, DATA_BLOB *rpc_out)
 {
+       struct schannel_state *schannel_auth;
+       struct gensec_security *gensec_security;
+       struct spnego_context *spnego_ctx;
+       struct gse_context *gse_ctx;
        char pad[CLIENT_NDR_PADDING_SIZE] = { 0, };
        DATA_BLOB auth_info;
        DATA_BLOB auth_blob;
        NTSTATUS status;
 
-       if (auth->auth_type == DCERPC_AUTH_TYPE_NONE) {
+       if (auth->auth_type == DCERPC_AUTH_TYPE_NONE ||
+           auth->auth_type == DCERPC_AUTH_TYPE_NCALRPC_AS_SYSTEM) {
                return NT_STATUS_OK;
        }
 
@@ -615,27 +799,33 @@ NTSTATUS dcerpc_add_auth_footer(struct pipe_auth_data *auth,
        /* Generate any auth sign/seal and add the auth footer. */
        switch (auth->auth_type) {
        case DCERPC_AUTH_TYPE_NONE:
+       case DCERPC_AUTH_TYPE_NCALRPC_AS_SYSTEM:
                status = NT_STATUS_OK;
                break;
        case DCERPC_AUTH_TYPE_SPNEGO:
-               if (auth->spnego_type != PIPE_AUTH_TYPE_SPNEGO_NTLMSSP) {
-                       return add_spnego_auth_footer(auth->a_u.spnego_state,
-                                                     auth->auth_level,
-                                                     rpc_out);
-               }
-               /* fall thorugh */
+               spnego_ctx = talloc_get_type_abort(auth->auth_ctx,
+                                                  struct spnego_context);
+               status = add_spnego_auth_footer(spnego_ctx,
+                                               auth->auth_level, rpc_out);
+               break;
        case DCERPC_AUTH_TYPE_NTLMSSP:
-               status = add_ntlmssp_auth_footer(auth->a_u.auth_ntlmssp_state,
+               gensec_security = talloc_get_type_abort(auth->auth_ctx,
+                                               struct gensec_security);
+               status = add_ntlmssp_auth_footer(gensec_security,
                                                 auth->auth_level,
                                                 rpc_out);
                break;
        case DCERPC_AUTH_TYPE_SCHANNEL:
-               status = add_schannel_auth_footer(auth->a_u.schannel_auth,
+               schannel_auth = talloc_get_type_abort(auth->auth_ctx,
+                                                     struct schannel_state);
+               status = add_schannel_auth_footer(schannel_auth,
                                                  auth->auth_level,
                                                  rpc_out);
                break;
        case DCERPC_AUTH_TYPE_KRB5:
-               status = add_gssapi_auth_footer(auth->a_u.gssapi_state,
+               gse_ctx = talloc_get_type_abort(auth->auth_ctx,
+                                               struct gse_context);
+               status = add_gssapi_auth_footer(gse_ctx,
                                                auth->auth_level,
                                                rpc_out);
                break;
@@ -666,6 +856,10 @@ NTSTATUS dcerpc_check_auth(struct pipe_auth_data *auth,
                           DATA_BLOB *raw_pkt,
                           size_t *pad_len)
 {
+       struct schannel_state *schannel_auth;
+       struct gensec_security *gensec_security;
+       struct spnego_context *spnego_ctx;
+       struct gse_context *gse_ctx;
        NTSTATUS status;
        struct dcerpc_auth auth_info;
        uint32_t auth_length;
@@ -707,9 +901,9 @@ NTSTATUS dcerpc_check_auth(struct pipe_auth_data *auth,
        if (pkt->auth_length > pkt->frag_length) {
                return NT_STATUS_INFO_LENGTH_MISMATCH;
        }
-       if ((pkt->auth_length
-            + DCERPC_AUTH_TRAILER_LENGTH < pkt->auth_length) ||
-           (pkt->auth_length
+       if (((unsigned int)pkt->auth_length
+            + DCERPC_AUTH_TRAILER_LENGTH < (unsigned int)pkt->auth_length) ||
+           ((unsigned int)pkt->auth_length
             + DCERPC_AUTH_TRAILER_LENGTH < DCERPC_AUTH_TRAILER_LENGTH)) {
                /* Integer wrap attempt. */
                return NT_STATUS_INFO_LENGTH_MISMATCH;
@@ -728,43 +922,28 @@ NTSTATUS dcerpc_check_auth(struct pipe_auth_data *auth,
 
        switch (auth->auth_type) {
        case DCERPC_AUTH_TYPE_NONE:
+       case DCERPC_AUTH_TYPE_NCALRPC_AS_SYSTEM:
                return NT_STATUS_OK;
 
        case DCERPC_AUTH_TYPE_SPNEGO:
-               if (auth->spnego_type != PIPE_AUTH_TYPE_SPNEGO_NTLMSSP) {
-                       enum dcerpc_AuthType auth_type;
-                       struct gse_context *gse_ctx;
-                       void *auth_ctx;
-
-                       status = spnego_get_negotiated_mech(
-                                               auth->a_u.spnego_state,
-                                               &auth_type, &auth_ctx);
-                       if (!NT_STATUS_IS_OK(status)) {
-                               return status;
-                       }
-                       gse_ctx = talloc_get_type(auth_ctx,
-                                                 struct gse_context);
-                       if (!gse_ctx) {
-                               return NT_STATUS_INVALID_PARAMETER;
-                       }
-
-                       DEBUG(10, ("KRB5 auth\n"));
-
-                       status = get_gssapi_auth_footer(pkt, gse_ctx,
+               spnego_ctx = talloc_get_type_abort(auth->auth_ctx,
+                                                  struct spnego_context);
+               status = get_spnego_auth_footer(pkt, spnego_ctx,
                                                auth->auth_level,
                                                &data, &full_pkt,
                                                &auth_info.credentials);
-                       if (!NT_STATUS_IS_OK(status)) {
-                               return status;
-                       }
-                       break;
+               if (!NT_STATUS_IS_OK(status)) {
+                       return status;
                }
-               /* fall through */
+               break;
+
        case DCERPC_AUTH_TYPE_NTLMSSP:
 
                DEBUG(10, ("NTLMSSP auth\n"));
 
-               status = get_ntlmssp_auth_footer(auth->a_u.auth_ntlmssp_state,
+               gensec_security = talloc_get_type_abort(auth->auth_ctx,
+                                               struct gensec_security);
+               status = get_ntlmssp_auth_footer(gensec_security,
                                                 auth->auth_level,
                                                 &data, &full_pkt,
                                                 &auth_info.credentials);
@@ -777,8 +956,9 @@ NTSTATUS dcerpc_check_auth(struct pipe_auth_data *auth,
 
                DEBUG(10, ("SCHANNEL auth\n"));
 
-               status = get_schannel_auth_footer(pkt,
-                                                 auth->a_u.schannel_auth,
+               schannel_auth = talloc_get_type_abort(auth->auth_ctx,
+                                                     struct schannel_state);
+               status = get_schannel_auth_footer(pkt, schannel_auth,
                                                  auth->auth_level,
                                                  &data, &full_pkt,
                                                  &auth_info.credentials);
@@ -791,8 +971,9 @@ NTSTATUS dcerpc_check_auth(struct pipe_auth_data *auth,
 
                DEBUG(10, ("KRB5 auth\n"));
 
-               status = get_gssapi_auth_footer(pkt,
-                                               auth->a_u.gssapi_state,
+               gse_ctx = talloc_get_type_abort(auth->auth_ctx,
+                                               struct gse_context);
+               status = get_gssapi_auth_footer(pkt, gse_ctx,
                                                auth->auth_level,
                                                &data, &full_pkt,
                                                &auth_info.credentials);