1613a2f6bSGordon Ross /* 2613a2f6bSGordon Ross * Copyright (c) 2000-2001, Boris Popov 3613a2f6bSGordon Ross * All rights reserved. 4613a2f6bSGordon Ross * 5613a2f6bSGordon Ross * Redistribution and use in source and binary forms, with or without 6613a2f6bSGordon Ross * modification, are permitted provided that the following conditions 7613a2f6bSGordon Ross * are met: 8613a2f6bSGordon Ross * 1. Redistributions of source code must retain the above copyright 9613a2f6bSGordon Ross * notice, this list of conditions and the following disclaimer. 10613a2f6bSGordon Ross * 2. Redistributions in binary form must reproduce the above copyright 11613a2f6bSGordon Ross * notice, this list of conditions and the following disclaimer in the 12613a2f6bSGordon Ross * documentation and/or other materials provided with the distribution. 13613a2f6bSGordon Ross * 3. All advertising materials mentioning features or use of this software 14613a2f6bSGordon Ross * must display the following acknowledgement: 15613a2f6bSGordon Ross * This product includes software developed by Boris Popov. 16613a2f6bSGordon Ross * 4. Neither the name of the author nor the names of any co-contributors 17613a2f6bSGordon Ross * may be used to endorse or promote products derived from this software 18613a2f6bSGordon Ross * without specific prior written permission. 19613a2f6bSGordon Ross * 20613a2f6bSGordon Ross * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 21613a2f6bSGordon Ross * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22613a2f6bSGordon Ross * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23613a2f6bSGordon Ross * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 24613a2f6bSGordon Ross * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25613a2f6bSGordon Ross * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26613a2f6bSGordon Ross * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27613a2f6bSGordon Ross * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28613a2f6bSGordon Ross * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29613a2f6bSGordon Ross * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30613a2f6bSGordon Ross * SUCH DAMAGE. 31613a2f6bSGordon Ross * 32613a2f6bSGordon Ross * $Id: smb_crypt.c,v 1.13 2005/01/26 23:50:50 lindak Exp $ 33613a2f6bSGordon Ross */ 34613a2f6bSGordon Ross 35613a2f6bSGordon Ross /* 36*15359501SGordon Ross * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved. 37613a2f6bSGordon Ross */ 38613a2f6bSGordon Ross 39613a2f6bSGordon Ross /* 40613a2f6bSGordon Ross * NTLM support functions 41613a2f6bSGordon Ross * 42613a2f6bSGordon Ross * Some code from the driver: smb_smb.c, smb_crypt.c 43613a2f6bSGordon Ross */ 44613a2f6bSGordon Ross 45613a2f6bSGordon Ross #include <sys/errno.h> 46613a2f6bSGordon Ross #include <sys/types.h> 47613a2f6bSGordon Ross #include <sys/md4.h> 48613a2f6bSGordon Ross #include <sys/md5.h> 49613a2f6bSGordon Ross 50613a2f6bSGordon Ross #include <ctype.h> 51613a2f6bSGordon Ross #include <stdlib.h> 52613a2f6bSGordon Ross #include <strings.h> 53613a2f6bSGordon Ross 54613a2f6bSGordon Ross #include <netsmb/smb_lib.h> 55613a2f6bSGordon Ross 56613a2f6bSGordon Ross #include "private.h" 57613a2f6bSGordon Ross #include "charsets.h" 58613a2f6bSGordon Ross #include "smb_crypt.h" 59613a2f6bSGordon Ross #include "ntlm.h" 60613a2f6bSGordon Ross 61613a2f6bSGordon Ross 62613a2f6bSGordon Ross /* 63613a2f6bSGordon Ross * ntlm_compute_lm_hash 64613a2f6bSGordon Ross * 65613a2f6bSGordon Ross * Compute an LM hash given a password 66613a2f6bSGordon Ross * 67613a2f6bSGordon Ross * Output: 68613a2f6bSGordon Ross * hash: 16-byte "LanMan" (LM) hash. 69613a2f6bSGordon Ross * Inputs: 70613a2f6bSGordon Ross * ucpw: User's password, upper-case UTF-8 string. 71613a2f6bSGordon Ross * 72613a2f6bSGordon Ross * Source: Implementing CIFS (Chris Hertel) 73613a2f6bSGordon Ross * 74613a2f6bSGordon Ross * P14 = UCPW padded to 14-bytes, or truncated (as needed) 75613a2f6bSGordon Ross * result = Encrypt(Key=P14, Data=MagicString) 76613a2f6bSGordon Ross */ 77613a2f6bSGordon Ross int 78613a2f6bSGordon Ross ntlm_compute_lm_hash(uchar_t *hash, const char *pass) 79613a2f6bSGordon Ross { 80613a2f6bSGordon Ross static const uchar_t M8[8] = "KGS!@#$%"; 81613a2f6bSGordon Ross uchar_t P14[14 + 1]; 82613a2f6bSGordon Ross int err; 83613a2f6bSGordon Ross char *ucpw; 84613a2f6bSGordon Ross 85613a2f6bSGordon Ross /* First, convert the p/w to upper case. */ 86613a2f6bSGordon Ross ucpw = utf8_str_toupper(pass); 87613a2f6bSGordon Ross if (ucpw == NULL) 88613a2f6bSGordon Ross return (ENOMEM); 89613a2f6bSGordon Ross 90613a2f6bSGordon Ross /* Pad or truncate the upper-case P/W as needed. */ 91613a2f6bSGordon Ross bzero(P14, sizeof (P14)); 92613a2f6bSGordon Ross (void) strncpy((char *)P14, ucpw, 14); 93613a2f6bSGordon Ross 94613a2f6bSGordon Ross /* Compute the hash. */ 95613a2f6bSGordon Ross err = smb_encrypt_DES(hash, NTLM_HASH_SZ, 96613a2f6bSGordon Ross P14, 14, M8, 8); 97613a2f6bSGordon Ross 98613a2f6bSGordon Ross free(ucpw); 99613a2f6bSGordon Ross return (err); 100613a2f6bSGordon Ross } 101613a2f6bSGordon Ross 102613a2f6bSGordon Ross /* 103613a2f6bSGordon Ross * ntlm_compute_nt_hash 104613a2f6bSGordon Ross * 105613a2f6bSGordon Ross * Compute an NT hash given a password in UTF-8. 106613a2f6bSGordon Ross * 107613a2f6bSGordon Ross * Output: 108613a2f6bSGordon Ross * hash: 16-byte "NT" hash. 109613a2f6bSGordon Ross * Inputs: 110613a2f6bSGordon Ross * upw: User's password, mixed-case UCS-2LE. 111613a2f6bSGordon Ross * pwlen: Size (in bytes) of upw 112613a2f6bSGordon Ross */ 113613a2f6bSGordon Ross int 114613a2f6bSGordon Ross ntlm_compute_nt_hash(uchar_t *hash, const char *pass) 115613a2f6bSGordon Ross { 116613a2f6bSGordon Ross MD4_CTX ctx; 117613a2f6bSGordon Ross uint16_t *unipw = NULL; 118613a2f6bSGordon Ross int pwsz; 119613a2f6bSGordon Ross 120613a2f6bSGordon Ross /* First, convert the password to unicode. */ 121613a2f6bSGordon Ross unipw = convert_utf8_to_leunicode(pass); 122613a2f6bSGordon Ross if (unipw == NULL) 123613a2f6bSGordon Ross return (ENOMEM); 124613a2f6bSGordon Ross pwsz = unicode_strlen(unipw) << 1; 125613a2f6bSGordon Ross 126613a2f6bSGordon Ross /* Compute the hash. */ 127613a2f6bSGordon Ross MD4Init(&ctx); 128613a2f6bSGordon Ross MD4Update(&ctx, unipw, pwsz); 129613a2f6bSGordon Ross MD4Final(hash, &ctx); 130613a2f6bSGordon Ross 131613a2f6bSGordon Ross free(unipw); 132613a2f6bSGordon Ross return (0); 133613a2f6bSGordon Ross } 134613a2f6bSGordon Ross 135613a2f6bSGordon Ross /* 136613a2f6bSGordon Ross * ntlm_v1_response 137613a2f6bSGordon Ross * 138613a2f6bSGordon Ross * Create an LM response from the given LM hash and challenge, 139613a2f6bSGordon Ross * or an NTLM repsonse from a given NTLM hash and challenge. 140613a2f6bSGordon Ross * Both response types are 24 bytes (NTLM_V1_RESP_SZ) 141613a2f6bSGordon Ross */ 142613a2f6bSGordon Ross static int 143613a2f6bSGordon Ross ntlm_v1_response(uchar_t *resp, 144613a2f6bSGordon Ross const uchar_t *hash, 145613a2f6bSGordon Ross const uchar_t *chal, int clen) 146613a2f6bSGordon Ross { 147613a2f6bSGordon Ross uchar_t S21[21]; 148613a2f6bSGordon Ross int err; 149613a2f6bSGordon Ross 150613a2f6bSGordon Ross /* 151613a2f6bSGordon Ross * 14-byte LM Hash should be padded with 5 nul bytes to create 152613a2f6bSGordon Ross * a 21-byte string to be used in producing LM response 153613a2f6bSGordon Ross */ 154613a2f6bSGordon Ross bzero(&S21, sizeof (S21)); 155613a2f6bSGordon Ross bcopy(hash, S21, NTLM_HASH_SZ); 156613a2f6bSGordon Ross 157613a2f6bSGordon Ross /* padded LM Hash -> LM Response */ 158613a2f6bSGordon Ross err = smb_encrypt_DES(resp, NTLM_V1_RESP_SZ, 159613a2f6bSGordon Ross S21, 21, chal, clen); 160613a2f6bSGordon Ross return (err); 161613a2f6bSGordon Ross } 162613a2f6bSGordon Ross 163613a2f6bSGordon Ross /* 164613a2f6bSGordon Ross * Calculate an NTLMv1 session key (16 bytes). 165613a2f6bSGordon Ross */ 166613a2f6bSGordon Ross static void 167613a2f6bSGordon Ross ntlm_v1_session_key(uchar_t *ssn_key, const uchar_t *nt_hash) 168613a2f6bSGordon Ross { 169613a2f6bSGordon Ross MD4_CTX md4; 170613a2f6bSGordon Ross 171613a2f6bSGordon Ross MD4Init(&md4); 172613a2f6bSGordon Ross MD4Update(&md4, nt_hash, NTLM_HASH_SZ); 173613a2f6bSGordon Ross MD4Final(ssn_key, &md4); 174613a2f6bSGordon Ross } 175613a2f6bSGordon Ross 176613a2f6bSGordon Ross /* 177613a2f6bSGordon Ross * Compute both the LM(v1) response and the NTLM(v1) response, 178613a2f6bSGordon Ross * and put them in the mbdata chains passed. This allocates 179613a2f6bSGordon Ross * mbuf chains in the output args, which the caller frees. 180613a2f6bSGordon Ross */ 181613a2f6bSGordon Ross int 182613a2f6bSGordon Ross ntlm_put_v1_responses(struct smb_ctx *ctx, 183613a2f6bSGordon Ross struct mbdata *lm_mbp, struct mbdata *nt_mbp) 184613a2f6bSGordon Ross { 185613a2f6bSGordon Ross uchar_t *lmresp, *ntresp; 186613a2f6bSGordon Ross int err; 187613a2f6bSGordon Ross 188613a2f6bSGordon Ross /* Get mbuf chain for the LM response. */ 18902d09e03SGordon Ross if ((err = mb_init_sz(lm_mbp, NTLM_V1_RESP_SZ)) != 0) 190613a2f6bSGordon Ross return (err); 191613a2f6bSGordon Ross 192613a2f6bSGordon Ross /* Get mbuf chain for the NT response. */ 19302d09e03SGordon Ross if ((err = mb_init_sz(nt_mbp, NTLM_V1_RESP_SZ)) != 0) 194613a2f6bSGordon Ross return (err); 195613a2f6bSGordon Ross 196613a2f6bSGordon Ross /* 197613a2f6bSGordon Ross * Compute the LM response, derived 198613a2f6bSGordon Ross * from the challenge and the ASCII 199613a2f6bSGordon Ross * password (if authflags allow). 200613a2f6bSGordon Ross */ 20102d09e03SGordon Ross err = mb_fit(lm_mbp, NTLM_V1_RESP_SZ, (char **)&lmresp); 20202d09e03SGordon Ross if (err) 20302d09e03SGordon Ross return (err); 204613a2f6bSGordon Ross bzero(lmresp, NTLM_V1_RESP_SZ); 205613a2f6bSGordon Ross if (ctx->ct_authflags & SMB_AT_LM1) { 206613a2f6bSGordon Ross /* They asked to send the LM hash too. */ 207613a2f6bSGordon Ross err = ntlm_v1_response(lmresp, ctx->ct_lmhash, 208613a2f6bSGordon Ross ctx->ct_ntlm_chal, NTLM_CHAL_SZ); 209613a2f6bSGordon Ross if (err) 210613a2f6bSGordon Ross return (err); 211613a2f6bSGordon Ross } 212613a2f6bSGordon Ross 213613a2f6bSGordon Ross /* 214613a2f6bSGordon Ross * Compute the NTLM response, derived from 215613a2f6bSGordon Ross * the challenge and the NT hash. 216613a2f6bSGordon Ross */ 21702d09e03SGordon Ross err = mb_fit(nt_mbp, NTLM_V1_RESP_SZ, (char **)&ntresp); 21802d09e03SGordon Ross if (err) 21902d09e03SGordon Ross return (err); 220613a2f6bSGordon Ross bzero(ntresp, NTLM_V1_RESP_SZ); 221613a2f6bSGordon Ross err = ntlm_v1_response(ntresp, ctx->ct_nthash, 222613a2f6bSGordon Ross ctx->ct_ntlm_chal, NTLM_CHAL_SZ); 223613a2f6bSGordon Ross 224613a2f6bSGordon Ross /* 225613a2f6bSGordon Ross * Compute the session key 226613a2f6bSGordon Ross */ 227613a2f6bSGordon Ross ntlm_v1_session_key(ctx->ct_ssn_key, ctx->ct_nthash); 228613a2f6bSGordon Ross 229613a2f6bSGordon Ross return (err); 230613a2f6bSGordon Ross } 231613a2f6bSGordon Ross 232613a2f6bSGordon Ross /* 233613a2f6bSGordon Ross * A variation on HMAC-MD5 known as HMACT64 is used by Windows systems. 234613a2f6bSGordon Ross * The HMACT64() function is the same as the HMAC-MD5() except that 235613a2f6bSGordon Ross * it truncates the input key to 64 bytes rather than hashing it down 236613a2f6bSGordon Ross * to 16 bytes using the MD5() function. 237613a2f6bSGordon Ross * 238613a2f6bSGordon Ross * Output: digest (16-bytes) 239613a2f6bSGordon Ross */ 240613a2f6bSGordon Ross static void 241613a2f6bSGordon Ross HMACT64(uchar_t *digest, 242613a2f6bSGordon Ross const uchar_t *key, size_t key_len, 243613a2f6bSGordon Ross const uchar_t *data, size_t data_len) 244613a2f6bSGordon Ross { 245613a2f6bSGordon Ross MD5_CTX context; 246613a2f6bSGordon Ross uchar_t k_ipad[64]; /* inner padding - key XORd with ipad */ 247613a2f6bSGordon Ross uchar_t k_opad[64]; /* outer padding - key XORd with opad */ 248613a2f6bSGordon Ross int i; 249613a2f6bSGordon Ross 250613a2f6bSGordon Ross /* if key is longer than 64 bytes use only the first 64 bytes */ 251613a2f6bSGordon Ross if (key_len > 64) 252613a2f6bSGordon Ross key_len = 64; 253613a2f6bSGordon Ross 254613a2f6bSGordon Ross /* 255613a2f6bSGordon Ross * The HMAC-MD5 (and HMACT64) transform looks like: 256613a2f6bSGordon Ross * 257613a2f6bSGordon Ross * MD5(K XOR opad, MD5(K XOR ipad, data)) 258613a2f6bSGordon Ross * 259613a2f6bSGordon Ross * where K is an n byte key 260613a2f6bSGordon Ross * ipad is the byte 0x36 repeated 64 times 261613a2f6bSGordon Ross * opad is the byte 0x5c repeated 64 times 262613a2f6bSGordon Ross * and data is the data being protected. 263613a2f6bSGordon Ross */ 264613a2f6bSGordon Ross 265613a2f6bSGordon Ross /* start out by storing key in pads */ 266613a2f6bSGordon Ross bzero(k_ipad, sizeof (k_ipad)); 267613a2f6bSGordon Ross bzero(k_opad, sizeof (k_opad)); 268613a2f6bSGordon Ross bcopy(key, k_ipad, key_len); 269613a2f6bSGordon Ross bcopy(key, k_opad, key_len); 270613a2f6bSGordon Ross 271613a2f6bSGordon Ross /* XOR key with ipad and opad values */ 272613a2f6bSGordon Ross for (i = 0; i < 64; i++) { 273613a2f6bSGordon Ross k_ipad[i] ^= 0x36; 274613a2f6bSGordon Ross k_opad[i] ^= 0x5c; 275613a2f6bSGordon Ross } 276613a2f6bSGordon Ross 277613a2f6bSGordon Ross /* 278613a2f6bSGordon Ross * perform inner MD5 279613a2f6bSGordon Ross */ 280613a2f6bSGordon Ross MD5Init(&context); /* init context for 1st pass */ 281613a2f6bSGordon Ross MD5Update(&context, k_ipad, 64); /* start with inner pad */ 282613a2f6bSGordon Ross MD5Update(&context, data, data_len); /* then data of datagram */ 283613a2f6bSGordon Ross MD5Final(digest, &context); /* finish up 1st pass */ 284613a2f6bSGordon Ross 285613a2f6bSGordon Ross /* 286613a2f6bSGordon Ross * perform outer MD5 287613a2f6bSGordon Ross */ 288613a2f6bSGordon Ross MD5Init(&context); /* init context for 2nd pass */ 289613a2f6bSGordon Ross MD5Update(&context, k_opad, 64); /* start with outer pad */ 290613a2f6bSGordon Ross MD5Update(&context, digest, 16); /* then results of 1st hash */ 291613a2f6bSGordon Ross MD5Final(digest, &context); /* finish up 2nd pass */ 292613a2f6bSGordon Ross } 293613a2f6bSGordon Ross 294613a2f6bSGordon Ross 295613a2f6bSGordon Ross /* 296613a2f6bSGordon Ross * Compute an NTLMv2 hash given the NTLMv1 hash, the user name, 297613a2f6bSGordon Ross * and the destination (machine or domain name). 298613a2f6bSGordon Ross * 299613a2f6bSGordon Ross * Output: 300613a2f6bSGordon Ross * v2hash: 16-byte NTLMv2 hash. 301613a2f6bSGordon Ross * Inputs: 302613a2f6bSGordon Ross * v1hash: 16-byte NTLMv1 hash. 303613a2f6bSGordon Ross * user: User name, UPPER-case UTF-8 string. 304613a2f6bSGordon Ross * destination: Domain or server, MIXED-case UTF-8 string. 305613a2f6bSGordon Ross */ 306613a2f6bSGordon Ross static int 307613a2f6bSGordon Ross ntlm_v2_hash(uchar_t *v2hash, const uchar_t *v1hash, 308613a2f6bSGordon Ross const char *user, const char *destination) 309613a2f6bSGordon Ross { 310613a2f6bSGordon Ross int ulen, dlen; 311613a2f6bSGordon Ross size_t ucs2len; 312613a2f6bSGordon Ross uint16_t *ucs2data = NULL; 313613a2f6bSGordon Ross char *utf8data = NULL; 314613a2f6bSGordon Ross int err = ENOMEM; 315613a2f6bSGordon Ross 316613a2f6bSGordon Ross /* 317613a2f6bSGordon Ross * v2hash = HMACT64(v1hash, 16, concat(upcase(user), dest)) 318613a2f6bSGordon Ross * where "dest" is the domain or server name ("target name") 319613a2f6bSGordon Ross * Note: user name is converted to upper-case by the caller. 320613a2f6bSGordon Ross */ 321613a2f6bSGordon Ross 322613a2f6bSGordon Ross /* utf8data = concat(user, dest) */ 323613a2f6bSGordon Ross ulen = strlen(user); 324613a2f6bSGordon Ross dlen = strlen(destination); 325613a2f6bSGordon Ross utf8data = malloc(ulen + dlen + 1); 326613a2f6bSGordon Ross if (utf8data == NULL) 327613a2f6bSGordon Ross goto out; 328613a2f6bSGordon Ross bcopy(user, utf8data, ulen); 329613a2f6bSGordon Ross bcopy(destination, utf8data + ulen, dlen + 1); 330613a2f6bSGordon Ross 331613a2f6bSGordon Ross /* Convert to UCS-2LE */ 332613a2f6bSGordon Ross ucs2data = convert_utf8_to_leunicode(utf8data); 333613a2f6bSGordon Ross if (ucs2data == NULL) 334613a2f6bSGordon Ross goto out; 335613a2f6bSGordon Ross ucs2len = 2 * unicode_strlen(ucs2data); 336613a2f6bSGordon Ross 337613a2f6bSGordon Ross HMACT64(v2hash, v1hash, NTLM_HASH_SZ, 338613a2f6bSGordon Ross (uchar_t *)ucs2data, ucs2len); 339613a2f6bSGordon Ross err = 0; 340613a2f6bSGordon Ross out: 341613a2f6bSGordon Ross if (ucs2data) 342613a2f6bSGordon Ross free(ucs2data); 343613a2f6bSGordon Ross if (utf8data) 344613a2f6bSGordon Ross free(utf8data); 345613a2f6bSGordon Ross return (err); 346613a2f6bSGordon Ross } 347613a2f6bSGordon Ross 348613a2f6bSGordon Ross /* 349613a2f6bSGordon Ross * Compute a partial LMv2 or NTLMv2 response (first 16-bytes). 350613a2f6bSGordon Ross * The full response is composed by the caller by 351613a2f6bSGordon Ross * appending the client_data to the returned hash. 352613a2f6bSGordon Ross * 353613a2f6bSGordon Ross * Output: 354613a2f6bSGordon Ross * rhash: _partial_ LMv2/NTLMv2 response (first 16-bytes) 355613a2f6bSGordon Ross * Inputs: 356613a2f6bSGordon Ross * v2hash: 16-byte NTLMv2 hash. 357613a2f6bSGordon Ross * C8: Challenge from server (8 bytes) 358613a2f6bSGordon Ross * client_data: client nonce (for LMv2) or the 359613a2f6bSGordon Ross * "blob" from ntlm_build_target_info (NTLMv2) 360613a2f6bSGordon Ross */ 361613a2f6bSGordon Ross static int 362613a2f6bSGordon Ross ntlm_v2_resp_hash(uchar_t *rhash, 363613a2f6bSGordon Ross const uchar_t *v2hash, const uchar_t *C8, 364613a2f6bSGordon Ross const uchar_t *client_data, size_t cdlen) 365613a2f6bSGordon Ross { 366613a2f6bSGordon Ross size_t dlen; 367613a2f6bSGordon Ross uchar_t *data = NULL; 368613a2f6bSGordon Ross 369613a2f6bSGordon Ross /* data = concat(C8, client_data) */ 370613a2f6bSGordon Ross dlen = 8 + cdlen; 371613a2f6bSGordon Ross data = malloc(dlen); 372613a2f6bSGordon Ross if (data == NULL) 373613a2f6bSGordon Ross return (ENOMEM); 374613a2f6bSGordon Ross bcopy(C8, data, 8); 375613a2f6bSGordon Ross bcopy(client_data, data + 8, cdlen); 376613a2f6bSGordon Ross 377613a2f6bSGordon Ross HMACT64(rhash, v2hash, NTLM_HASH_SZ, data, dlen); 378613a2f6bSGordon Ross 379613a2f6bSGordon Ross free(data); 380613a2f6bSGordon Ross return (0); 381613a2f6bSGordon Ross } 382613a2f6bSGordon Ross 383613a2f6bSGordon Ross /* 384613a2f6bSGordon Ross * Calculate an NTLMv2 session key (16 bytes). 385613a2f6bSGordon Ross */ 386613a2f6bSGordon Ross static void 387613a2f6bSGordon Ross ntlm_v2_session_key(uchar_t *ssn_key, 388613a2f6bSGordon Ross const uchar_t *v2hash, 389613a2f6bSGordon Ross const uchar_t *ntresp) 390613a2f6bSGordon Ross { 391613a2f6bSGordon Ross 392613a2f6bSGordon Ross /* session key uses only 1st 16 bytes of ntresp */ 393613a2f6bSGordon Ross HMACT64(ssn_key, v2hash, NTLM_HASH_SZ, ntresp, NTLM_HASH_SZ); 394613a2f6bSGordon Ross } 395613a2f6bSGordon Ross 396613a2f6bSGordon Ross 397613a2f6bSGordon Ross /* 398613a2f6bSGordon Ross * Compute both the LMv2 response and the NTLMv2 response, 399613a2f6bSGordon Ross * and put them in the mbdata chains passed. This allocates 400613a2f6bSGordon Ross * mbuf chains in the output args, which the caller frees. 401613a2f6bSGordon Ross * Also computes the session key. 402613a2f6bSGordon Ross */ 403613a2f6bSGordon Ross int 404613a2f6bSGordon Ross ntlm_put_v2_responses(struct smb_ctx *ctx, struct mbdata *ti_mbp, 405613a2f6bSGordon Ross struct mbdata *lm_mbp, struct mbdata *nt_mbp) 406613a2f6bSGordon Ross { 407613a2f6bSGordon Ross uchar_t *lmresp, *ntresp; 408613a2f6bSGordon Ross int err; 409*15359501SGordon Ross char *ucuser = NULL; /* upper-case user name */ 410613a2f6bSGordon Ross uchar_t v2hash[NTLM_HASH_SZ]; 411613a2f6bSGordon Ross struct mbuf *tim = ti_mbp->mb_top; 412613a2f6bSGordon Ross 41302d09e03SGordon Ross if ((err = mb_init(lm_mbp)) != 0) 414613a2f6bSGordon Ross return (err); 41502d09e03SGordon Ross if ((err = mb_init(nt_mbp)) != 0) 416613a2f6bSGordon Ross return (err); 417613a2f6bSGordon Ross 418613a2f6bSGordon Ross /* 419613a2f6bSGordon Ross * Convert the user name to upper-case, as 420613a2f6bSGordon Ross * that's what's used when computing LMv2 421*15359501SGordon Ross * and NTLMv2 responses. Note that the 422*15359501SGordon Ross * domain name is NOT upper-cased! 423613a2f6bSGordon Ross */ 424613a2f6bSGordon Ross ucuser = utf8_str_toupper(ctx->ct_user); 425*15359501SGordon Ross if (ucuser == NULL) { 426613a2f6bSGordon Ross err = ENOMEM; 427613a2f6bSGordon Ross goto out; 428613a2f6bSGordon Ross } 429613a2f6bSGordon Ross 430613a2f6bSGordon Ross /* 431*15359501SGordon Ross * Compute the NTLMv2 hash 432613a2f6bSGordon Ross */ 433*15359501SGordon Ross err = ntlm_v2_hash(v2hash, ctx->ct_nthash, 434*15359501SGordon Ross ucuser, ctx->ct_domain); 435613a2f6bSGordon Ross if (err) 436613a2f6bSGordon Ross goto out; 437613a2f6bSGordon Ross 438613a2f6bSGordon Ross /* 439613a2f6bSGordon Ross * Compute the LMv2 response, derived from 440613a2f6bSGordon Ross * the v2hash, the server challenge, and 441613a2f6bSGordon Ross * the client nonce (random bits). 442613a2f6bSGordon Ross * 443613a2f6bSGordon Ross * We compose it from two parts: 444613a2f6bSGordon Ross * 1: 16-byte response hash 445613a2f6bSGordon Ross * 2: Client nonce 446613a2f6bSGordon Ross */ 447613a2f6bSGordon Ross lmresp = (uchar_t *)lm_mbp->mb_pos; 44802d09e03SGordon Ross mb_put_mem(lm_mbp, NULL, NTLM_HASH_SZ, MB_MSYSTEM); 449613a2f6bSGordon Ross err = ntlm_v2_resp_hash(lmresp, 450613a2f6bSGordon Ross v2hash, ctx->ct_ntlm_chal, 451613a2f6bSGordon Ross ctx->ct_clnonce, NTLM_CHAL_SZ); 452613a2f6bSGordon Ross if (err) 453613a2f6bSGordon Ross goto out; 45402d09e03SGordon Ross mb_put_mem(lm_mbp, ctx->ct_clnonce, NTLM_CHAL_SZ, MB_MSYSTEM); 455613a2f6bSGordon Ross 456613a2f6bSGordon Ross /* 457613a2f6bSGordon Ross * Compute the NTLMv2 response, derived 458613a2f6bSGordon Ross * from the server challenge and the 459613a2f6bSGordon Ross * "target info." blob passed in. 460613a2f6bSGordon Ross * 461613a2f6bSGordon Ross * Again composed from two parts: 462613a2f6bSGordon Ross * 1: 16-byte response hash 463613a2f6bSGordon Ross * 2: "target info." blob 464613a2f6bSGordon Ross */ 465613a2f6bSGordon Ross ntresp = (uchar_t *)nt_mbp->mb_pos; 46602d09e03SGordon Ross mb_put_mem(nt_mbp, NULL, NTLM_HASH_SZ, MB_MSYSTEM); 467613a2f6bSGordon Ross err = ntlm_v2_resp_hash(ntresp, 468613a2f6bSGordon Ross v2hash, ctx->ct_ntlm_chal, 469613a2f6bSGordon Ross (uchar_t *)tim->m_data, tim->m_len); 470613a2f6bSGordon Ross if (err) 471613a2f6bSGordon Ross goto out; 47202d09e03SGordon Ross mb_put_mem(nt_mbp, tim->m_data, tim->m_len, MB_MSYSTEM); 473613a2f6bSGordon Ross 474613a2f6bSGordon Ross /* 475613a2f6bSGordon Ross * Compute the session key 476613a2f6bSGordon Ross */ 477613a2f6bSGordon Ross ntlm_v2_session_key(ctx->ct_ssn_key, v2hash, ntresp); 478613a2f6bSGordon Ross 479613a2f6bSGordon Ross out: 480613a2f6bSGordon Ross if (err) { 481613a2f6bSGordon Ross mb_done(lm_mbp); 482613a2f6bSGordon Ross mb_done(nt_mbp); 483613a2f6bSGordon Ross } 484613a2f6bSGordon Ross free(ucuser); 485613a2f6bSGordon Ross 486613a2f6bSGordon Ross return (err); 487613a2f6bSGordon Ross } 488613a2f6bSGordon Ross 489613a2f6bSGordon Ross /* 490613a2f6bSGordon Ross * Helper for ntlm_build_target_info below. 491613a2f6bSGordon Ross * Put a name in the NTLMv2 "target info." blob. 492613a2f6bSGordon Ross */ 493613a2f6bSGordon Ross static void 494613a2f6bSGordon Ross smb_put_blob_name(struct mbdata *mbp, char *name, int type) 495613a2f6bSGordon Ross { 496613a2f6bSGordon Ross uint16_t *ucs = NULL; 497613a2f6bSGordon Ross int nlen; 498613a2f6bSGordon Ross 499613a2f6bSGordon Ross if (name) 500613a2f6bSGordon Ross ucs = convert_utf8_to_leunicode(name); 501613a2f6bSGordon Ross if (ucs) 502613a2f6bSGordon Ross nlen = unicode_strlen(ucs); 503613a2f6bSGordon Ross else 504613a2f6bSGordon Ross nlen = 0; 505613a2f6bSGordon Ross 506613a2f6bSGordon Ross nlen <<= 1; /* length in bytes, without null. */ 507613a2f6bSGordon Ross 508613a2f6bSGordon Ross mb_put_uint16le(mbp, type); 509613a2f6bSGordon Ross mb_put_uint16le(mbp, nlen); 51002d09e03SGordon Ross mb_put_mem(mbp, (char *)ucs, nlen, MB_MSYSTEM); 511613a2f6bSGordon Ross 512613a2f6bSGordon Ross if (ucs) 513613a2f6bSGordon Ross free(ucs); 514613a2f6bSGordon Ross } 515613a2f6bSGordon Ross 516613a2f6bSGordon Ross /* 517613a2f6bSGordon Ross * Build an NTLMv2 "target info." blob. When called from NTLMSSP, 518613a2f6bSGordon Ross * the list of names comes from the Type 2 message. Otherwise, 519613a2f6bSGordon Ross * we create the name list here. 520613a2f6bSGordon Ross */ 521613a2f6bSGordon Ross int 522613a2f6bSGordon Ross ntlm_build_target_info(struct smb_ctx *ctx, struct mbuf *names, 523613a2f6bSGordon Ross struct mbdata *mbp) 524613a2f6bSGordon Ross { 525613a2f6bSGordon Ross struct timeval now; 526613a2f6bSGordon Ross uint64_t nt_time; 527613a2f6bSGordon Ross 528613a2f6bSGordon Ross char *ucdom = NULL; /* user's domain */ 529613a2f6bSGordon Ross int err; 530613a2f6bSGordon Ross 531613a2f6bSGordon Ross /* Get mbuf chain for the "target info". */ 53202d09e03SGordon Ross if ((err = mb_init(mbp)) != 0) 533613a2f6bSGordon Ross return (err); 534613a2f6bSGordon Ross 535613a2f6bSGordon Ross /* 536613a2f6bSGordon Ross * Construct the client nonce by getting 537613a2f6bSGordon Ross * some random data from /dev/urandom 538613a2f6bSGordon Ross */ 539613a2f6bSGordon Ross err = smb_get_urandom(ctx->ct_clnonce, NTLM_CHAL_SZ); 540613a2f6bSGordon Ross if (err) 541613a2f6bSGordon Ross goto out; 542613a2f6bSGordon Ross 543613a2f6bSGordon Ross /* 544613a2f6bSGordon Ross * Get the "NT time" for the target info header. 545613a2f6bSGordon Ross */ 546613a2f6bSGordon Ross (void) gettimeofday(&now, 0); 547613a2f6bSGordon Ross smb_time_local2NT(&now, 0, &nt_time); 548613a2f6bSGordon Ross 549613a2f6bSGordon Ross /* 550613a2f6bSGordon Ross * Build the "target info." block. 551613a2f6bSGordon Ross * 552613a2f6bSGordon Ross * Based on information at: 553613a2f6bSGordon Ross * http://davenport.sourceforge.net/ntlm.html#theNtlmv2Response 554613a2f6bSGordon Ross * 555613a2f6bSGordon Ross * First the fixed-size part. 556613a2f6bSGordon Ross */ 557613a2f6bSGordon Ross mb_put_uint32le(mbp, 0x101); /* Blob signature */ 558613a2f6bSGordon Ross mb_put_uint32le(mbp, 0); /* reserved */ 559613a2f6bSGordon Ross mb_put_uint64le(mbp, nt_time); /* NT time stamp */ 56002d09e03SGordon Ross mb_put_mem(mbp, ctx->ct_clnonce, NTLM_CHAL_SZ, MB_MSYSTEM); 561613a2f6bSGordon Ross mb_put_uint32le(mbp, 0); /* unknown */ 562613a2f6bSGordon Ross 563613a2f6bSGordon Ross /* 564613a2f6bSGordon Ross * Now put the list of names, either from the 565613a2f6bSGordon Ross * NTLMSSP Type 2 message or composed here. 566613a2f6bSGordon Ross */ 567613a2f6bSGordon Ross if (names) { 56802d09e03SGordon Ross err = mb_put_mem(mbp, names->m_data, names->m_len, MB_MSYSTEM); 569613a2f6bSGordon Ross } else { 570613a2f6bSGordon Ross /* Get upper-case names. */ 571613a2f6bSGordon Ross ucdom = utf8_str_toupper(ctx->ct_domain); 572613a2f6bSGordon Ross if (ucdom == NULL) { 573613a2f6bSGordon Ross err = ENOMEM; 574613a2f6bSGordon Ross goto out; 575613a2f6bSGordon Ross } 576613a2f6bSGordon Ross smb_put_blob_name(mbp, ucdom, NAMETYPE_DOMAIN_NB); 577613a2f6bSGordon Ross smb_put_blob_name(mbp, NULL, NAMETYPE_EOL); 578613a2f6bSGordon Ross /* OK, that's the whole "target info." blob! */ 579613a2f6bSGordon Ross } 580613a2f6bSGordon Ross err = 0; 581613a2f6bSGordon Ross 582613a2f6bSGordon Ross out: 583613a2f6bSGordon Ross free(ucdom); 584613a2f6bSGordon Ross return (err); 585613a2f6bSGordon Ross } 586