make smb_signing more generic...
[metze/samba/wip.git] / source / smb_server / smb / signing.c
1 /* 
2    Unix SMB/CIFS implementation.
3    
4    Copyright (C) Andrew Tridgell              2004
5    
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "includes.h"
21 #include "smb_server/smb_server.h"
22 #include "libcli/raw/libcliraw.h"
23 #include "libcli/raw/raw_proto.h"
24 #include "param/param.h"
25
26
27 /*
28   sign an outgoing packet
29 */
30 void smbsrv_sign_packet(struct smbsrv_request *req)
31 {
32         smb_signing_create_signature(&req->smb_conn->signing,
33                                      &req->out, req->seq_num+1, &req->in);
34 }
35
36 /*
37   setup the signing key for a connection. Called after authentication succeeds
38   in a session setup
39 */
40 bool smbsrv_setup_signing(struct smbsrv_connection *smb_conn,
41                           DATA_BLOB *session_key,
42                           DATA_BLOB *response)
43 {
44         if (!smb_signing_allow_state_change(&smb_conn->signing)) {
45                 return false;
46         }
47         return smb_signing_set_mac_key(smb_conn, &smb_conn->signing,
48                                        session_key, response);
49 }
50
51 void smbsrv_signing_restart(struct smbsrv_connection *smb_conn,
52                             DATA_BLOB *session_key,
53                             DATA_BLOB *response,
54                             bool authenticated_session) 
55 {
56         bool ok;
57
58         if (!smb_signing_allow_state_change(&smb_conn->signing)) {
59                 return;
60         }
61
62 /* TODO: handle anon...*/
63
64         DEBUG(5, ("Client did not send a valid signature on "
65                   "SPNEGO session setup - ignored, expect good next time\n"));
66
67         ok = smb_signing_set_mac_key(smb_conn, &smb_conn->signing,
68                                      session_key, response);
69         if (ok) {
70                 smb_signing_next_seq_num(&smb_conn->signing, false);
71         }
72 }
73
74 void smbsrv_init_signing(struct smbsrv_connection *smb_conn)
75 {
76         enum smb_signing_state signing = lp_server_signing(smb_conn->lp_ctx);
77
78         if (signing == SMB_SIGNING_AUTO) {
79                 if (lp_server_role(smb_conn->lp_ctx) == ROLE_DOMAIN_CONTROLLER) {
80                         signing = SMB_SIGNING_REQUIRED;
81                 } else {
82                         signing = SMB_SIGNING_SUPPORTED;
83                 }
84         }
85
86         smb_signing_init_context(&smb_conn->signing, signing);
87 }
88
89 bool smbsrv_signing_check_incoming(struct smbsrv_request *req,
90                                    bool single_increment)
91 {
92         req->seq_num = smb_signing_next_seq_num(&req->smb_conn->signing,
93                                                 single_increment);
94
95         return smb_signing_check_signature(&req->smb_conn->signing,
96                                            &req->in, req->seq_num);
97 }