]>
git.ipfire.org Git - thirdparty/squid.git/blob - lib/smblib/smbencrypt.c
4 * Unix SMB/Netbios implementation.
6 * SMB parameters and setup
7 * Copyright (C) Andrew Tridgell 1992-1997
8 * Modified by Jeremy Allison 1995.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #include <sys/types.h>
27 #include <sys/socket.h>
28 #include <netinet/in.h>
29 #include <arpa/inet.h>
34 /* AI inclusion for Solaris filesystem */
39 #include "smblib/smblib-priv.h"
40 #define uchar unsigned char
42 #include "rfcnb/byteorder.h"
44 #include "smblib/md4.h"
45 #include "smblib/smbdes.h"
46 #include "smblib/smbencrypt.h"
48 static void E_md4hash(unsigned char *passwd
, unsigned char *p16
);
49 static char *StrnCpy(char *dest
, char *src
, int n
);
50 static void strupper(char *s
);
53 * This implements the X/Open SMB password encryption
54 * It takes a password, a 8 byte "crypt key" and puts 24 bytes of
55 * encrypted password into p24 */
57 SMBencrypt(uchar
* passwd
, uchar
* c8
, uchar
* p24
)
59 uchar p14
[15], p21
[21];
61 memset(p21
, '\0', 21);
62 memset(p14
, '\0', 14);
63 StrnCpy((char *) p14
, (char *) passwd
, 14);
65 strupper((char *) p14
);
70 /* Routines for Windows NT MD4 Hash functions. */
72 _my_wcslen(int16_t * str
)
81 * Convert a string into an NT UNICODE string.
82 * Note that regardless of processor type
83 * this must be in intel (little-endian)
88 _my_mbstowcs(int16_t * dst
, uchar
* src
, int len
)
93 for (i
= 0; i
< len
; i
++) {
105 * Creates the MD4 Hash of the users password in NT UNICODE.
109 E_md4hash(uchar
* passwd
, uchar
* p16
)
114 /* Password cannot be longer than 128 characters */
115 len
= strlen((char *) passwd
);
118 /* Password must be converted to NT unicode */
119 _my_mbstowcs(wpwd
, passwd
, len
);
120 wpwd
[len
] = 0; /* Ensure string is null terminated */
121 /* Calculate length in bytes */
122 len
= _my_wcslen(wpwd
) * sizeof(int16_t);
124 mdfour(p16
, (unsigned char *) wpwd
, len
);
127 /* Does the NT MD4 hash then des encryption. */
130 SMBNTencrypt(uchar
* passwd
, uchar
* c8
, uchar
* p24
)
134 memset(p21
, '\0', 21);
136 E_md4hash(passwd
, p21
);
140 /* Does both the NT and LM owfs of a user's password */
143 nt_lm_owf_gen(char *pwd
, char *nt_p16
, char *p16
)
146 StrnCpy(passwd
, pwd
, sizeof(passwd
) - 1);
148 /* Calculate the MD4 hash (NT compatible) of the password */
149 memset(nt_p16
, '\0', 16);
150 E_md4hash((uchar
*) passwd
, (uchar
*) nt_p16
);
152 /* Mangle the passwords into Lanman format */
156 /* Calculate the SMB (lanman) hash functions of the password */
158 memset(p16
, '\0', 16);
159 E_P16((uchar
*) passwd
, (uchar
*) p16
);
161 /* clear out local copy of user's password (just being paranoid). */
162 memset(passwd
, 0, sizeof(passwd
));
165 /****************************************************************************
166 line strncpy but always null terminates. Make sure there is room!
167 ****************************************************************************/
169 StrnCpy(char *dest
, char *src
, int n
)
178 while (n
-- && (*d
++ = *src
++));
188 #if !defined(KANJI_WIN95_COMPATIBILITY)
189 if (lp_client_code_page() == KANJI_CODEPAGE
) {
191 if (is_shift_jis(*s
)) {
192 if (is_sj_lower(s
[0], s
[1]))
193 s
[1] = sj_toupper2(s
[1]);
195 } else if (is_kana(*s
)) {
198 if (islower((int)(unsigned char)*s
))
199 *s
= toupper((int)(unsigned char)*s
);
203 #endif /* KANJI_WIN95_COMPATIBILITY */
204 #endif /* UNUSED_CODE */
206 if (islower((int)(unsigned char)*s
))
207 *s
= toupper((int)(unsigned char)*s
);