r995: - renamed many of our crypto routines to use the industry standard
[kai/samba-autobuild/.git] / source4 / libcli / auth / session.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    code to encrypt/decrypt data using the user session key
5
6    Copyright (C) Andrew Tridgell 2004
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 2 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, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24
25 /*
26   encrypt or decrypt a blob of data using the user session key
27   as used in lsa_SetSecret
28
29   before calling, the out blob must be initialised to be the same size
30   as the in blob
31 */
32 void sess_crypt_blob(DATA_BLOB *out, const DATA_BLOB *in, const DATA_BLOB *session_key,
33                      BOOL forward)
34 {
35         int i, k;
36
37         for (i=0,k=0;
38              i<in->length;
39              i += 8, k += 7) {
40                 uint8_t bin[8], bout[8], key[7];
41
42                 memset(bin, 0, 8);
43                 memcpy(bin,  &in->data[i], MIN(8, in->length-i));
44
45                 if (k + 7 > session_key->length) {
46                         k = (session_key->length - k);
47                 }
48                 memcpy(key, &session_key->data[k], 7);
49
50                 des_crypt56(bout, bin, key, forward?1:0);
51
52                 memcpy(&out->data[i], bout, MIN(8, in->length-i));
53         }
54 }
55
56
57 /*
58   a convenient wrapper around sess_crypt_blob() for strings, using the LSA convention
59
60   note that we round the length to a multiple of 8. This seems to be needed for 
61   compatibility with windows
62
63   caller should free using data_blob_free()
64 */
65 DATA_BLOB sess_encrypt_string(const char *str, const DATA_BLOB *session_key)
66 {
67         DATA_BLOB ret, src;
68         int slen = strlen(str);
69         int dlen = (slen+7) & ~7;
70
71         src = data_blob(NULL, 8+dlen);
72         if (!src.data) {
73                 return data_blob(NULL, 0);
74         }
75
76         ret = data_blob(NULL, 8+dlen);
77         if (!ret.data) {
78                 data_blob_free(&src);
79                 return data_blob(NULL, 0);
80         }
81
82         SIVAL(src.data, 0, slen);
83         SIVAL(src.data, 4, 1);
84         memset(src.data+8, 0,   dlen);
85         memcpy(src.data+8, str, slen);
86
87         sess_crypt_blob(&ret, &src, session_key, True);
88         
89         data_blob_free(&src);
90
91         return ret;
92 }
93
94 /*
95   a convenient wrapper around sess_crypt_blob() for strings, using the LSA convention
96
97   caller should free the returned string
98 */
99 char *sess_decrypt_string(DATA_BLOB *blob, const DATA_BLOB *session_key)
100 {
101         DATA_BLOB out;
102         int slen;
103         char *ret;
104
105         if (blob->length < 8) {
106                 return NULL;
107         }
108         
109         out = data_blob(NULL, blob->length);
110         if (!out.data) {
111                 return NULL;
112         }
113
114         sess_crypt_blob(&out, blob, session_key, False);
115
116         slen = IVAL(out.data, 0);
117         if (slen > blob->length - 8) {
118                 DEBUG(0,("Invalid crypt length %d\n", slen));
119                 return NULL;
120         }
121
122         if (IVAL(out.data, 4) != 1) {
123                 DEBUG(0,("Unexpected revision number %d in session crypted string\n",
124                          IVAL(out.data, 4)));
125                 return NULL;
126         }
127                 
128         ret = strndup(out.data+8, slen);
129
130         data_blob_free(&out);
131
132         return ret;
133 }