import 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 <string.h>
28 #ifdef HAVE_SYS_VFS_H
29 #include <sys/vfs.h>
30 #endif
31
32 #include "smblib-priv.h"
33 #define uchar unsigned char
34 extern int DEBUGLEVEL;
35
36 void strupper(char *s);
37
38 /*
39    This implements the X/Open SMB password encryption
40    It takes a password, a 8 byte "crypt key" and puts 24 bytes of
41    encrypted password into p24 */
42 void SMBencrypt(uchar *passwd, uchar *c8, uchar *p24)
43 {
44   uchar p14[15], p21[21];
45
46   memset(p21,'\0',21);
47   memset(p14,'\0',14);
48   strlcpy((char *)p14,(char *)passwd,14);
49
50   strupper((char *)p14);
51   E_P16(p14, p21);
52   E_P24(p21, c8, p24);
53 }
54
55 /* Routines for Windows NT MD4 Hash functions. */
56 static int _my_wcslen(int16 *str)
57 {
58         int len = 0;
59         while(*str++ != 0)
60                 len++;
61         return len;
62 }
63
64 /*
65  * Convert a string into an NT UNICODE string.
66  * Note that regardless of processor type
67  * this must be in intel (little-endian)
68  * format.
69  */
70
71 static int _my_mbstowcs(int16 *dst, uchar *src, int len)
72 {
73         int i;
74         int16 val;
75
76         for(i = 0; i < len; i++) {
77                 val = *src;
78                 SSVAL(dst,0,val);
79                 dst++;
80                 src++;
81                 if(val == 0)
82                         break;
83         }
84         return i;
85 }
86
87 /*
88  * Creates the MD4 Hash of the users password in NT UNICODE.
89  */
90
91 void E_md4hash(uchar *passwd, uchar *p16)
92 {
93         int len;
94         int16 wpwd[129];
95
96         /* Password cannot be longer than 128 characters */
97         len = strlen((char *)passwd);
98         if(len > 128)
99                 len = 128;
100         /* Password must be converted to NT unicode */
101         _my_mbstowcs(wpwd, passwd, len);
102         wpwd[len] = 0; /* Ensure string is null terminated */
103         /* Calculate length in bytes */
104         len = _my_wcslen(wpwd) * sizeof(int16);
105
106         mdfour(p16, (unsigned char *)wpwd, len);
107 }
108
109 /* Does the NT MD4 hash then des encryption. */
110
111 void SMBNTencrypt(uchar *passwd, uchar *c8, uchar *p24)
112 {
113         uchar p21[21];
114
115         memset(p21,'\0',21);
116
117         E_md4hash(passwd, p21);
118         E_P24(p21, c8, p24);
119 }
120
121 /* Does both the NT and LM owfs of a user's password */
122
123 void nt_lm_owf_gen(char *pwd, char *nt_p16, char *p16)
124 {
125         char passwd[130];
126         strlcpy(passwd, pwd, sizeof(passwd));
127
128         /* Calculate the MD4 hash (NT compatible) of the password */
129         memset(nt_p16, '\0', 16);
130         E_md4hash((uchar *)passwd, (uchar *)nt_p16);
131
132         /* Mangle the passwords into Lanman format */
133         passwd[14] = '\0';
134         strupper(passwd);
135
136         /* Calculate the SMB (lanman) hash functions of the password */
137
138         memset(p16, '\0', 16);
139         E_P16((uchar *) passwd, (uchar *)p16);
140
141         /* clear out local copy of user's password (just being paranoid). */
142         bzero(passwd, sizeof(passwd));
143 }
144
145 void strupper(char *s)
146 {
147   while (*s)
148   {
149     /*
150 #if !defined(KANJI_WIN95_COMPATIBILITY)
151     if(lp_client_code_page() == KANJI_CODEPAGE)
152     {
153
154       if (is_shift_jis (*s))
155       {
156         if (is_sj_lower (s[0], s[1]))
157           s[1] = sj_toupper2 (s[1]);
158         s += 2;
159       }
160       else if (is_kana (*s))
161       {
162         s++;
163       }
164       else
165       {
166         if (islower(*s))
167           *s = toupper(*s);
168         s++;
169       }
170     }
171     else
172 #endif */ /* KANJI_WIN95_COMPATIBILITY */
173     {
174       if (islower(*s))
175         *s = toupper(*s);
176       s++;
177     }
178   }
179 }