LCOV - code coverage report
Current view: top level - auth/ntlmssp - gensec_ntlmssp.c (source / functions) Hit Total Coverage
Test: coverage report for master 2f515e9b Lines: 77 88 87.5 %
Date: 2024-04-21 15:09:00 Functions: 11 11 100.0 %

          Line data    Source code
       1             : /*
       2             :  *  Unix SMB/CIFS implementation.
       3             :  *  Version 3.0
       4             :  *  NTLMSSP Signing routines
       5             :  *  Copyright (C) Luke Kenneth Casson Leighton 1996-2001
       6             :  *  Copyright (C) Andrew Bartlett <abartlet@samba.org> 2003-2005
       7             :  *
       8             :  *  This program is free software; you can redistribute it and/or modify
       9             :  *  it under the terms of the GNU General Public License as published by
      10             :  *  the Free Software Foundation; either version 3 of the License, or
      11             :  *  (at your option) any later version.
      12             :  *
      13             :  *  This program is distributed in the hope that it will be useful,
      14             :  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
      15             :  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      16             :  *  GNU General Public License for more details.
      17             :  *
      18             :  *  You should have received a copy of the GNU General Public License
      19             :  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
      20             :  */
      21             : 
      22             : #include "includes.h"
      23             : #include "auth/ntlmssp/ntlmssp.h"
      24             : #include "auth/gensec/gensec.h"
      25             : #include "auth/gensec/gensec_internal.h"
      26             : #include "auth/ntlmssp/ntlmssp_private.h"
      27             : 
      28         102 : NTSTATUS gensec_ntlmssp_magic(struct gensec_security *gensec_security,
      29             :                               const DATA_BLOB *first_packet)
      30             : {
      31         102 :         if (ntlmssp_blob_matches_magic(first_packet)) {
      32         102 :                 return NT_STATUS_OK;
      33             :         } else {
      34           0 :                 return NT_STATUS_INVALID_PARAMETER;
      35             :         }
      36             : }
      37             : 
      38             : /**
      39             :  * Return the NTLMSSP master session key
      40             :  *
      41             :  * @param ntlmssp_state NTLMSSP State
      42             :  */
      43             : 
      44       54614 : NTSTATUS gensec_ntlmssp_session_key(struct gensec_security *gensec_security,
      45             :                                     TALLOC_CTX *mem_ctx,
      46             :                                     DATA_BLOB *session_key)
      47             : {
      48         262 :         struct gensec_ntlmssp_context *gensec_ntlmssp =
      49       54614 :                 talloc_get_type_abort(gensec_security->private_data,
      50             :                                       struct gensec_ntlmssp_context);
      51       54614 :         struct ntlmssp_state *ntlmssp_state = gensec_ntlmssp->ntlmssp_state;
      52             : 
      53       54614 :         if (ntlmssp_state->expected_state != NTLMSSP_DONE) {
      54           0 :                 return NT_STATUS_NO_USER_SESSION_KEY;
      55             :         }
      56             : 
      57       54614 :         if (!ntlmssp_state->session_key.data) {
      58          28 :                 return NT_STATUS_NO_USER_SESSION_KEY;
      59             :         }
      60       54586 :         *session_key = data_blob_talloc(mem_ctx, ntlmssp_state->session_key.data, ntlmssp_state->session_key.length);
      61       54586 :         if (!session_key->data) {
      62           0 :                 return NT_STATUS_NO_MEMORY;
      63             :         }
      64             : 
      65       54586 :         return NT_STATUS_OK;
      66             : }
      67             : 
      68     1159975 : bool gensec_ntlmssp_have_feature(struct gensec_security *gensec_security,
      69             :                                  uint32_t feature)
      70             : {
      71        1908 :         struct gensec_ntlmssp_context *gensec_ntlmssp =
      72     1159975 :                 talloc_get_type_abort(gensec_security->private_data,
      73             :                                       struct gensec_ntlmssp_context);
      74     1159975 :         struct ntlmssp_state *ntlmssp_state = gensec_ntlmssp->ntlmssp_state;
      75             : 
      76     1159975 :         if (feature & GENSEC_FEATURE_SIGN) {
      77      653272 :                 if (!ntlmssp_state->session_key.length) {
      78          84 :                         return false;
      79             :                 }
      80      653188 :                 if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_SIGN) {
      81      650241 :                         return true;
      82             :                 }
      83             :         }
      84      508275 :         if (feature & GENSEC_FEATURE_SEAL) {
      85      222485 :                 if (!ntlmssp_state->session_key.length) {
      86           0 :                         return false;
      87             :                 }
      88      222485 :                 if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_SEAL) {
      89      194876 :                         return true;
      90             :                 }
      91             :         }
      92      313399 :         if (feature & GENSEC_FEATURE_SESSION_KEY) {
      93       42912 :                 if (ntlmssp_state->session_key.length) {
      94       42666 :                         return true;
      95             :                 }
      96             :         }
      97      270487 :         if (feature & GENSEC_FEATURE_DCE_STYLE) {
      98      144195 :                 return true;
      99             :         }
     100      126292 :         if (feature & GENSEC_FEATURE_ASYNC_REPLIES) {
     101       21426 :                 if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_NTLM2) {
     102       20746 :                         return true;
     103             :                 }
     104             :         }
     105      105546 :         if (feature & GENSEC_FEATURE_SIGN_PKT_HEADER) {
     106        4040 :                 return true;
     107             :         }
     108      101506 :         if (feature & GENSEC_FEATURE_NEW_SPNEGO) {
     109       71645 :                 if (!ntlmssp_state->session_key.length) {
     110          28 :                         return false;
     111             :                 }
     112       71617 :                 if (!(ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_SIGN)) {
     113         368 :                         return false;
     114             :                 }
     115       71249 :                 return ntlmssp_state->new_spnego;
     116             :         }
     117             : 
     118       29861 :         return false;
     119             : }
     120             : 
     121      107639 : NTSTATUS gensec_ntlmssp_start(struct gensec_security *gensec_security)
     122             : {
     123         446 :         struct gensec_ntlmssp_context *gensec_ntlmssp;
     124             : 
     125      107639 :         gensec_ntlmssp = talloc_zero(gensec_security,
     126             :                                      struct gensec_ntlmssp_context);
     127      107639 :         if (!gensec_ntlmssp) {
     128           0 :                 return NT_STATUS_NO_MEMORY;
     129             :         }
     130             : 
     131      107639 :         gensec_security->private_data = gensec_ntlmssp;
     132      107639 :         return NT_STATUS_OK;
     133             : }
     134             : 
     135       78398 : NTSTATUS gensec_ntlmssp_sign_packet(struct gensec_security *gensec_security,
     136             :                                     TALLOC_CTX *sig_mem_ctx,
     137             :                                     const uint8_t *data, size_t length,
     138             :                                     const uint8_t *whole_pdu, size_t pdu_length,
     139             :                                     DATA_BLOB *sig)
     140             : {
     141         269 :         struct gensec_ntlmssp_context *gensec_ntlmssp =
     142       78398 :                 talloc_get_type_abort(gensec_security->private_data,
     143             :                                       struct gensec_ntlmssp_context);
     144         269 :         NTSTATUS nt_status;
     145             : 
     146       78398 :         nt_status = ntlmssp_sign_packet(gensec_ntlmssp->ntlmssp_state,
     147             :                                         sig_mem_ctx,
     148             :                                         data, length,
     149             :                                         whole_pdu, pdu_length,
     150             :                                         sig);
     151             : 
     152       78398 :         return nt_status;
     153             : }
     154             : 
     155       74557 : NTSTATUS gensec_ntlmssp_check_packet(struct gensec_security *gensec_security,
     156             :                                      const uint8_t *data, size_t length,
     157             :                                      const uint8_t *whole_pdu, size_t pdu_length,
     158             :                                      const DATA_BLOB *sig)
     159             : {
     160         261 :         struct gensec_ntlmssp_context *gensec_ntlmssp =
     161       74557 :                 talloc_get_type_abort(gensec_security->private_data,
     162             :                                       struct gensec_ntlmssp_context);
     163         261 :         NTSTATUS nt_status;
     164             : 
     165       74557 :         nt_status = ntlmssp_check_packet(gensec_ntlmssp->ntlmssp_state,
     166             :                                          data, length,
     167             :                                          whole_pdu, pdu_length,
     168             :                                          sig);
     169             : 
     170       74557 :         return nt_status;
     171             : }
     172             : 
     173       27747 : NTSTATUS gensec_ntlmssp_seal_packet(struct gensec_security *gensec_security,
     174             :                                     TALLOC_CTX *sig_mem_ctx,
     175             :                                     uint8_t *data, size_t length,
     176             :                                     const uint8_t *whole_pdu, size_t pdu_length,
     177             :                                     DATA_BLOB *sig)
     178             : {
     179           0 :         struct gensec_ntlmssp_context *gensec_ntlmssp =
     180       27747 :                 talloc_get_type_abort(gensec_security->private_data,
     181             :                                       struct gensec_ntlmssp_context);
     182           0 :         NTSTATUS nt_status;
     183             : 
     184       27747 :         nt_status = ntlmssp_seal_packet(gensec_ntlmssp->ntlmssp_state,
     185             :                                         sig_mem_ctx,
     186             :                                         data, length,
     187             :                                         whole_pdu, pdu_length,
     188             :                                         sig);
     189             : 
     190       27747 :         return nt_status;
     191             : }
     192             : 
     193             : /*
     194             :   wrappers for the ntlmssp_*() functions
     195             : */
     196       27738 : NTSTATUS gensec_ntlmssp_unseal_packet(struct gensec_security *gensec_security,
     197             :                                       uint8_t *data, size_t length,
     198             :                                       const uint8_t *whole_pdu, size_t pdu_length,
     199             :                                       const DATA_BLOB *sig)
     200             : {
     201           0 :         struct gensec_ntlmssp_context *gensec_ntlmssp =
     202       27738 :                 talloc_get_type_abort(gensec_security->private_data,
     203             :                                       struct gensec_ntlmssp_context);
     204           0 :         NTSTATUS nt_status;
     205             : 
     206       27738 :         nt_status = ntlmssp_unseal_packet(gensec_ntlmssp->ntlmssp_state,
     207             :                                           data, length,
     208             :                                           whole_pdu, pdu_length,
     209             :                                           sig);
     210             : 
     211       27738 :         return nt_status;
     212             : }
     213             : 
     214       39104 : size_t gensec_ntlmssp_sig_size(struct gensec_security *gensec_security, size_t data_size)
     215             : {
     216       39104 :         return NTLMSSP_SIG_SIZE;
     217             : }
     218             : 
     219      456646 : NTSTATUS gensec_ntlmssp_wrap(struct gensec_security *gensec_security,
     220             :                              TALLOC_CTX *out_mem_ctx,
     221             :                              const DATA_BLOB *in,
     222             :                              DATA_BLOB *out)
     223             : {
     224           0 :         struct gensec_ntlmssp_context *gensec_ntlmssp =
     225      456646 :                 talloc_get_type_abort(gensec_security->private_data,
     226             :                                       struct gensec_ntlmssp_context);
     227             : 
     228      456646 :         return ntlmssp_wrap(gensec_ntlmssp->ntlmssp_state,
     229             :                             out_mem_ctx,
     230             :                             in, out);
     231             : }
     232             : 
     233             : 
     234      456645 : NTSTATUS gensec_ntlmssp_unwrap(struct gensec_security *gensec_security,
     235             :                                TALLOC_CTX *out_mem_ctx,
     236             :                                const DATA_BLOB *in,
     237             :                                DATA_BLOB *out)
     238             : {
     239           0 :         struct gensec_ntlmssp_context *gensec_ntlmssp =
     240      456645 :                 talloc_get_type_abort(gensec_security->private_data,
     241             :                                       struct gensec_ntlmssp_context);
     242             : 
     243      456645 :         return ntlmssp_unwrap(gensec_ntlmssp->ntlmssp_state,
     244             :                               out_mem_ctx,
     245             :                               in, out);
     246             : }

Generated by: LCOV version 1.14