s3/torture: local_rbtree: avoid birthday collisions
[samba4-gss.git] / libcli / auth / session.c
blob515b7aed629bc58b7902cd83039bf1598ec8b747
1 /*
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/>.
22 #include "includes.h"
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
30 as the in blob
32 int sess_crypt_blob(DATA_BLOB *out, const DATA_BLOB *in, const DATA_BLOB *session_key,
33 enum samba_gnutls_direction encrypt)
35 int i, k, rc;
37 if (in->length % 8 != 0) {
38 return GNUTLS_E_INVALID_REQUEST;
41 if (session_key->length < 7) {
42 return GNUTLS_E_INVALID_REQUEST;
45 for (i=0,k=0;
46 i<in->length;
47 i += 8, k += 7) {
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);
58 if (rc != 0) {
59 return rc;
62 memcpy(&out->data[i], bout, 8);
64 return 0;
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)
78 DATA_BLOB ret, src;
79 int slen = strlen(str);
80 int dlen = (slen+7) & ~7;
81 int rc;
83 src = data_blob(NULL, 8+dlen);
84 if (!src.data) {
85 return data_blob(NULL, 0);
88 ret = data_blob(NULL, 8+dlen);
89 if (!ret.data) {
90 data_blob_free(&src);
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);
102 if (rc != 0) {
103 data_blob_free(&ret);
104 return data_blob(NULL, 0);
107 return ret;
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)
118 DATA_BLOB out;
119 int rc, slen;
120 char *ret;
122 if (blob->length < 8) {
123 return NULL;
126 out = data_blob_talloc(mem_ctx, NULL, blob->length);
127 if (!out.data) {
128 return NULL;
131 rc = sess_crypt_blob(&out, blob, session_key, SAMBA_GNUTLS_DECRYPT);
132 if (rc != 0) {
133 data_blob_free(&out);
134 return NULL;
137 if (IVAL(out.data, 4) != 1) {
138 DEBUG(0,("Unexpected revision number %d in session encrypted string\n",
139 IVAL(out.data, 4)));
140 data_blob_free(&out);
141 return NULL;
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);
148 return NULL;
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));
157 return ret;
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)
170 DATA_BLOB ret, src;
171 int dlen = (blob_in->length+7) & ~7;
172 int rc;
174 src = data_blob_talloc(mem_ctx, NULL, 8+dlen);
175 if (!src.data) {
176 return data_blob(NULL, 0);
179 ret = data_blob_talloc(mem_ctx, NULL, 8+dlen);
180 if (!ret.data) {
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);
193 if (rc != 0) {
194 data_blob_free(&ret);
195 return data_blob(NULL, 0);
198 return ret;
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,
205 DATA_BLOB *ret)
207 DATA_BLOB out;
208 int rc, slen;
210 if (blob->length < 8) {
211 DEBUG(0, ("Unexpected length %d in session encrypted secret (BLOB)\n",
212 (int)blob->length));
213 return NT_STATUS_INVALID_PARAMETER;
216 out = data_blob_talloc(mem_ctx, NULL, blob->length);
217 if (!out.data) {
218 return NT_STATUS_NO_MEMORY;
221 rc = sess_crypt_blob(&out, blob, session_key, SAMBA_GNUTLS_DECRYPT);
222 if (rc != 0) {
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",
229 IVAL(out.data, 4)));
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);
246 return NT_STATUS_OK;