2 Unix SMB/CIFS implementation.
4 code to encrypt/decrypt data using the user session key
6 Copyright (C) Andrew Tridgell 2004
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.
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.
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/>.
23 #include "libcli/auth/libcli_auth.h"
26 encrypt or decrypt a blob of data using the user session key
27 as used in lsa_SetSecret
29 before calling, the out blob must be initialised to be the same size
32 int sess_crypt_blob(DATA_BLOB
*out
, const DATA_BLOB
*in
, const DATA_BLOB
*session_key
,
33 enum samba_gnutls_direction encrypt
)
37 if (in
->length
% 8 != 0) {
38 return GNUTLS_E_INVALID_REQUEST
;
41 if (session_key
->length
< 7) {
42 return GNUTLS_E_INVALID_REQUEST
;
48 uint8_t bin
[8], bout
[8], key
[7];
50 memcpy(bin
, &in
->data
[i
], 8);
52 if (k
+ 7 > session_key
->length
) {
53 k
= (session_key
->length
- k
);
55 memcpy(key
, &session_key
->data
[k
], 7);
57 rc
= des_crypt56_gnutls(bout
, bin
, key
, encrypt
);
62 memcpy(&out
->data
[i
], bout
, 8);
69 a convenient wrapper around sess_crypt_blob() for strings, using the LSA convention
71 note that we round the length to a multiple of 8. This seems to be needed for
72 compatibility with windows
74 caller should free using data_blob_free()
76 DATA_BLOB
sess_encrypt_string(const char *str
, const DATA_BLOB
*session_key
)
79 int slen
= strlen(str
);
80 int dlen
= (slen
+7) & ~7;
83 src
= data_blob(NULL
, 8+dlen
);
85 return data_blob(NULL
, 0);
88 ret
= data_blob(NULL
, 8+dlen
);
91 return data_blob(NULL
, 0);
94 SIVAL(src
.data
, 0, slen
);
95 SIVAL(src
.data
, 4, 1);
96 memset(src
.data
+8, 0, dlen
);
97 memcpy(src
.data
+8, str
, slen
);
99 rc
= sess_crypt_blob(&ret
, &src
, session_key
, SAMBA_GNUTLS_ENCRYPT
);
101 data_blob_free(&src
);
103 data_blob_free(&ret
);
104 return data_blob(NULL
, 0);
111 a convenient wrapper around sess_crypt_blob() for strings, using the LSA convention
113 caller should free the returned string
115 char *sess_decrypt_string(TALLOC_CTX
*mem_ctx
,
116 DATA_BLOB
*blob
, const DATA_BLOB
*session_key
)
122 if (blob
->length
< 8) {
126 out
= data_blob_talloc(mem_ctx
, NULL
, blob
->length
);
131 rc
= sess_crypt_blob(&out
, blob
, session_key
, SAMBA_GNUTLS_DECRYPT
);
133 data_blob_free(&out
);
137 if (IVAL(out
.data
, 4) != 1) {
138 DEBUG(0,("Unexpected revision number %d in session encrypted string\n",
140 data_blob_free(&out
);
144 slen
= IVAL(out
.data
, 0);
145 if (slen
> blob
->length
- 8) {
146 DEBUG(0,("Invalid crypt length %d\n", slen
));
147 data_blob_free(&out
);
151 ret
= talloc_strndup(mem_ctx
, (const char *)(out
.data
+8), slen
);
153 data_blob_free(&out
);
155 DEBUG(0,("decrypted string '%s' of length %d\n", ret
, slen
));
161 a convenient wrapper around sess_crypt_blob() for DATA_BLOBs, using the LSA convention
163 note that we round the length to a multiple of 8. This seems to be needed for
164 compatibility with windows
166 caller should free using data_blob_free()
168 DATA_BLOB
sess_encrypt_blob(TALLOC_CTX
*mem_ctx
, DATA_BLOB
*blob_in
, const DATA_BLOB
*session_key
)
171 int dlen
= (blob_in
->length
+7) & ~7;
174 src
= data_blob_talloc(mem_ctx
, NULL
, 8+dlen
);
176 return data_blob(NULL
, 0);
179 ret
= data_blob_talloc(mem_ctx
, NULL
, 8+dlen
);
181 data_blob_free(&src
);
182 return data_blob(NULL
, 0);
185 SIVAL(src
.data
, 0, blob_in
->length
);
186 SIVAL(src
.data
, 4, 1);
187 memset(src
.data
+8, 0, dlen
);
188 memcpy(src
.data
+8, blob_in
->data
, blob_in
->length
);
190 rc
= sess_crypt_blob(&ret
, &src
, session_key
, SAMBA_GNUTLS_ENCRYPT
);
192 data_blob_free(&src
);
194 data_blob_free(&ret
);
195 return data_blob(NULL
, 0);
202 Decrypt a DATA_BLOB using the LSA convention
204 NTSTATUS
sess_decrypt_blob(TALLOC_CTX
*mem_ctx
, const DATA_BLOB
*blob
, const DATA_BLOB
*session_key
,
210 if (blob
->length
< 8) {
211 DEBUG(0, ("Unexpected length %d in session encrypted secret (BLOB)\n",
213 return NT_STATUS_INVALID_PARAMETER
;
216 out
= data_blob_talloc(mem_ctx
, NULL
, blob
->length
);
218 return NT_STATUS_NO_MEMORY
;
221 rc
= sess_crypt_blob(&out
, blob
, session_key
, SAMBA_GNUTLS_DECRYPT
);
223 data_blob_free(&out
);
224 return gnutls_error_to_ntstatus(rc
, NT_STATUS_ACCESS_DISABLED_BY_POLICY_OTHER
);
227 if (IVAL(out
.data
, 4) != 1) {
228 DEBUG(2,("Unexpected revision number %d in session encrypted secret (BLOB)\n",
230 return NT_STATUS_UNKNOWN_REVISION
;
233 slen
= IVAL(out
.data
, 0);
234 if (slen
> blob
->length
- 8) {
235 DEBUG(0,("Invalid crypt length %d in session encrypted secret (BLOB)\n", slen
));
236 return NT_STATUS_WRONG_PASSWORD
;
239 *ret
= data_blob_talloc(mem_ctx
, out
.data
+8, slen
);
240 if (slen
&& !ret
->data
) {
241 return NT_STATUS_NO_MEMORY
;
244 data_blob_free(&out
);