Pull fix from branch_1_1
[freeradius.git] / src / modules / rlm_smb / smbencrypt.c
1 /*
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    SMB parameters and setup
5    Copyright (C) Andrew Tridgell 1992-1997
6    Modified by Jeremy Allison 1995.
7    Copyright 2006 The FreeRADIUS server project
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24 #include <freeradius-devel/ident.h>
25 RCSID("$Id$")
26
27 #include "smblib-priv.h"
28 #define uchar unsigned char
29 extern int DEBUGLEVEL;
30
31 #include <string.h>
32
33 #include <string.h>
34 #ifdef HAVE_SYS_VFS_H
35 #include <sys/vfs.h>
36 #endif
37
38 #include "byteorder.h"
39
40 void strupper(char *s);
41
42 /*
43    This implements the X/Open SMB password encryption
44    It takes a password, a 8 byte "crypt key" and puts 24 bytes of
45    encrypted password into p24 */
46 void SMBencrypt(uchar *passwd, uchar *c8, uchar *p24)
47 {
48   uchar p14[15], p21[21];
49
50   memset(p21,'\0',21);
51   memset(p14,'\0',14);
52   strlcpy((char *)p14,(char *)passwd,14);
53
54   strupper((char *)p14);
55   E_P16(p14, p21);
56   E_P24(p21, c8, p24);
57 }
58
59 /* Routines for Windows NT MD4 Hash functions. */
60 static int _my_wcslen(int16 *str)
61 {
62         int len = 0;
63         while(*str++ != 0)
64                 len++;
65         return len;
66 }
67
68 /*
69  * Convert a string into an NT UNICODE string.
70  * Note that regardless of processor type
71  * this must be in intel (little-endian)
72  * format.
73  */
74
75 static int _my_mbstowcs(int16 *dst, uchar *src, int len)
76 {
77         int i;
78         int16 val;
79
80         for(i = 0; i < len; i++) {
81                 val = *src;
82                 SSVAL(dst,0,val);
83                 dst++;
84                 src++;
85                 if(val == 0)
86                         break;
87         }
88         return i;
89 }
90
91 /*
92  * Creates the MD4 Hash of the users password in NT UNICODE.
93  */
94
95 void E_md4hash(uchar *passwd, uchar *p16)
96 {
97         int len;
98         int16 wpwd[129];
99
100         /* Password cannot be longer than 128 characters */
101         len = strlen((char *)passwd);
102         if(len > 128)
103                 len = 128;
104         /* Password must be converted to NT unicode */
105         _my_mbstowcs(wpwd, passwd, len);
106         wpwd[len] = 0; /* Ensure string is null terminated */
107         /* Calculate length in bytes */
108         len = _my_wcslen(wpwd) * sizeof(int16);
109
110         mdfour(p16, (unsigned char *)wpwd, len);
111 }
112
113 /* Does the NT MD4 hash then des encryption. */
114
115 void SMBNTencrypt(uchar *passwd, uchar *c8, uchar *p24)
116 {
117         uchar p21[21];
118
119         memset(p21,'\0',21);
120
121         E_md4hash(passwd, p21);
122         E_P24(p21, c8, p24);
123 }
124
125 /* Does both the NT and LM owfs of a user's password */
126
127 void nt_lm_owf_gen(char *pwd, char *nt_p16, char *p16)
128 {
129         char passwd[130];
130         strlcpy(passwd, pwd, sizeof(passwd));
131
132         /* Calculate the MD4 hash (NT compatible) of the password */
133         memset(nt_p16, '\0', 16);
134         E_md4hash((uchar *)passwd, (uchar *)nt_p16);
135
136         /* Mangle the passwords into Lanman format */
137         passwd[14] = '\0';
138         strupper(passwd);
139
140         /* Calculate the SMB (lanman) hash functions of the password */
141
142         memset(p16, '\0', 16);
143         E_P16((uchar *) passwd, (uchar *)p16);
144
145         /* clear out local copy of user's password (just being paranoid). */
146         bzero(passwd, sizeof(passwd));
147 }
148
149 void strupper(char *s)
150 {
151   while (*s)
152   {
153     /*
154 #if !defined(KANJI_WIN95_COMPATIBILITY)
155     if(lp_client_code_page() == KANJI_CODEPAGE)
156     {
157
158       if (is_shift_jis (*s))
159       {
160         if (is_sj_lower (s[0], s[1]))
161           s[1] = sj_toupper2 (s[1]);
162         s += 2;
163       }
164       else if (is_kana (*s))
165       {
166         s++;
167       }
168       else
169       {
170         if (islower(*s))
171           *s = toupper(*s);
172         s++;
173       }
174     }
175     else
176 #endif */ /* KANJI_WIN95_COMPATIBILITY */
177     {
178       if (islower(*s))
179         *s = toupper(*s);
180       s++;
181     }
182   }
183 }