]>
git.ipfire.org Git - thirdparty/squid.git/blob - lib/smblib/smbencrypt.c
656c41aad31b6f4130d8f99e02dbbe34b0b26ef8
2 * Copyright (C) 1996-2015 The Squid Software Foundation and contributors
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
10 * Unix SMB/Netbios implementation.
12 * SMB parameters and setup
13 * Copyright (C) Andrew Tridgell 1992-1997
14 * Modified by Jeremy Allison 1995.
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2 of the License, or
21 * (at your option) any later version.
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with this program; if not, write to the Free Software
30 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
36 #include <sys/types.h>
37 #include <sys/socket.h>
38 #include <netinet/in.h>
39 #include <arpa/inet.h>
44 /* AI inclusion for Solaris filesystem */
49 #include "smblib/smblib-priv.h"
50 #define uchar unsigned char
52 #include "rfcnb/byteorder.h"
54 #include "smblib/md4.h"
55 #include "smblib/smbdes.h"
56 #include "smblib/smbencrypt.h"
58 static void E_md4hash(unsigned char *passwd
, unsigned char *p16
);
59 static char *StrnCpy(char *dest
, char *src
, int n
);
60 static void strupper(char *s
);
63 * This implements the X/Open SMB password encryption
64 * It takes a password, a 8 byte "crypt key" and puts 24 bytes of
65 * encrypted password into p24 */
67 SMBencrypt(uchar
* passwd
, uchar
* c8
, uchar
* p24
)
69 uchar p14
[15], p21
[21];
71 memset(p21
, '\0', 21);
72 memset(p14
, '\0', 14);
73 StrnCpy((char *) p14
, (char *) passwd
, 14);
75 strupper((char *) p14
);
80 /* Routines for Windows NT MD4 Hash functions. */
82 _my_wcslen(int16_t * str
)
91 * Convert a string into an NT UNICODE string.
92 * Note that regardless of processor type
93 * this must be in intel (little-endian)
98 _my_mbstowcs(int16_t * dst
, uchar
* src
, int len
)
103 for (i
= 0; i
< len
; i
++) {
115 * Creates the MD4 Hash of the users password in NT UNICODE.
119 E_md4hash(uchar
* passwd
, uchar
* p16
)
124 /* Password cannot be longer than 128 characters */
125 len
= strlen((char *) passwd
);
128 /* Password must be converted to NT unicode */
129 _my_mbstowcs(wpwd
, passwd
, len
);
130 wpwd
[len
] = 0; /* Ensure string is null terminated */
131 /* Calculate length in bytes */
132 len
= _my_wcslen(wpwd
) * sizeof(int16_t);
134 mdfour(p16
, (unsigned char *) wpwd
, len
);
137 /* Does the NT MD4 hash then des encryption. */
140 SMBNTencrypt(uchar
* passwd
, uchar
* c8
, uchar
* p24
)
144 memset(p21
, '\0', 21);
146 E_md4hash(passwd
, p21
);
150 /* Does both the NT and LM owfs of a user's password */
153 nt_lm_owf_gen(char *pwd
, char *nt_p16
, char *p16
)
156 StrnCpy(passwd
, pwd
, sizeof(passwd
) - 1);
158 /* Calculate the MD4 hash (NT compatible) of the password */
159 memset(nt_p16
, '\0', 16);
160 E_md4hash((uchar
*) passwd
, (uchar
*) nt_p16
);
162 /* Mangle the passwords into Lanman format */
166 /* Calculate the SMB (lanman) hash functions of the password */
168 memset(p16
, '\0', 16);
169 E_P16((uchar
*) passwd
, (uchar
*) p16
);
171 /* clear out local copy of user's password (just being paranoid). */
172 memset(passwd
, 0, sizeof(passwd
));
175 /****************************************************************************
176 line strncpy but always null terminates. Make sure there is room!
177 ****************************************************************************/
179 StrnCpy(char *dest
, char *src
, int n
)
188 while (n
-- && (*d
++ = *src
++));
198 #if !defined(KANJI_WIN95_COMPATIBILITY)
199 if (lp_client_code_page() == KANJI_CODEPAGE
) {
201 if (is_shift_jis(*s
)) {
202 if (is_sj_lower(s
[0], s
[1]))
203 s
[1] = sj_toupper2(s
[1]);
205 } else if (is_kana(*s
)) {
208 if (islower((int)(unsigned char)*s
))
209 *s
= toupper((int)(unsigned char)*s
);
213 #endif /* KANJI_WIN95_COMPATIBILITY */
214 #endif /* UNUSED_CODE */
216 if (islower((int)(unsigned char)*s
))
217 *s
= toupper((int)(unsigned char)*s
);