2 Unix SMB/CIFS implementation.
5 Copyright (C) Stefan Metzmacher 2009
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "smbd/globals.h"
23 #include "../libcli/smb/smb_common.h"
24 #include "../lib/crypto/crypto.h"
26 NTSTATUS smb2_signing_sign_pdu(DATA_BLOB session_key,
32 struct HMACSHA256Context m;
33 uint8_t res[SHA256_DIGEST_LENGTH];
37 return NT_STATUS_INVALID_PARAMETER;
40 if (vector[0].iov_len != SMB2_HDR_BODY) {
41 return NT_STATUS_INVALID_PARAMETER;
44 hdr = (uint8_t *)vector[0].iov_base;
46 session_id = BVAL(hdr, SMB2_HDR_SESSION_ID);
47 if (session_id == 0) {
49 * do not sign messages with a zero session_id.
50 * See MS-SMB2 3.2.4.1.1
55 if (session_key.length == 0) {
56 DEBUG(2,("Wrong session key length %u for SMB2 signing\n",
57 (unsigned)session_key.length));
58 return NT_STATUS_ACCESS_DENIED;
61 memset(hdr + SMB2_HDR_SIGNATURE, 0, 16);
63 SIVAL(hdr, SMB2_HDR_FLAGS, IVAL(hdr, SMB2_HDR_FLAGS) | SMB2_HDR_FLAG_SIGNED);
66 hmac_sha256_init(session_key.data, MIN(session_key.length, 16), &m);
67 for (i=0; i < count; i++) {
68 hmac_sha256_update((const uint8_t *)vector[i].iov_base,
69 vector[i].iov_len, &m);
71 hmac_sha256_final(res, &m);
72 DEBUG(5,("signed SMB2 message\n"));
74 memcpy(hdr + SMB2_HDR_SIGNATURE, res, 16);
79 NTSTATUS smb2_signing_check_pdu(DATA_BLOB session_key,
80 const struct iovec *vector,
86 struct HMACSHA256Context m;
87 uint8_t res[SHA256_DIGEST_LENGTH];
88 static const uint8_t zero_sig[16] = { 0, };
92 return NT_STATUS_INVALID_PARAMETER;
95 if (vector[0].iov_len != SMB2_HDR_BODY) {
96 return NT_STATUS_INVALID_PARAMETER;
99 hdr = (const uint8_t *)vector[0].iov_base;
101 session_id = BVAL(hdr, SMB2_HDR_SESSION_ID);
102 if (session_id == 0) {
104 * do not sign messages with a zero session_id.
105 * See MS-SMB2 3.2.4.1.1
110 if (session_key.length == 0) {
111 /* we don't have the session key yet */
115 sig = hdr+SMB2_HDR_SIGNATURE;
118 hmac_sha256_init(session_key.data, MIN(session_key.length, 16), &m);
119 hmac_sha256_update(hdr, SMB2_HDR_SIGNATURE, &m);
120 hmac_sha256_update(zero_sig, 16, &m);
121 for (i=1; i < count; i++) {
122 hmac_sha256_update((const uint8_t *)vector[i].iov_base,
123 vector[i].iov_len, &m);
125 hmac_sha256_final(res, &m);
127 if (memcmp(res, sig, 16) != 0) {
128 DEBUG(0,("Bad SMB2 signature for message\n"));
129 dump_data(0, sig, 16);
130 dump_data(0, res, 16);
131 return NT_STATUS_ACCESS_DENIED;