1 // SPDX-License-Identifier: LGPL-2.1 2 /* 3 * 4 * Encryption and hashing operations relating to NTLM, NTLMv2. See MS-NLMP 5 * for more detailed information 6 * 7 * Copyright (C) International Business Machines Corp., 2005,2013 8 * Author(s): Steve French (sfrench@us.ibm.com) 9 * 10 */ 11 12 #include <linux/fs.h> 13 #include <linux/slab.h> 14 #include "cifsglob.h" 15 #include "cifs_debug.h" 16 #include "cifs_unicode.h" 17 #include "cifsproto.h" 18 #include "ntlmssp.h" 19 #include <linux/ctype.h> 20 #include <linux/random.h> 21 #include <linux/highmem.h> 22 #include <linux/fips.h> 23 #include <linux/iov_iter.h> 24 #include <crypto/aead.h> 25 #include <crypto/arc4.h> 26 #include <crypto/md5.h> 27 #include <crypto/sha2.h> 28 29 static int cifs_sig_update(struct cifs_calc_sig_ctx *ctx, 30 const u8 *data, size_t len) 31 { 32 if (ctx->md5) { 33 md5_update(ctx->md5, data, len); 34 return 0; 35 } 36 if (ctx->hmac) { 37 hmac_sha256_update(ctx->hmac, data, len); 38 return 0; 39 } 40 return crypto_shash_update(ctx->shash, data, len); 41 } 42 43 static int cifs_sig_final(struct cifs_calc_sig_ctx *ctx, u8 *out) 44 { 45 if (ctx->md5) { 46 md5_final(ctx->md5, out); 47 return 0; 48 } 49 if (ctx->hmac) { 50 hmac_sha256_final(ctx->hmac, out); 51 return 0; 52 } 53 return crypto_shash_final(ctx->shash, out); 54 } 55 56 static size_t cifs_sig_step(void *iter_base, size_t progress, size_t len, 57 void *priv, void *priv2) 58 { 59 struct cifs_calc_sig_ctx *ctx = priv; 60 int ret, *pret = priv2; 61 62 ret = cifs_sig_update(ctx, iter_base, len); 63 if (ret < 0) { 64 *pret = ret; 65 return len; 66 } 67 return 0; 68 } 69 70 /* 71 * Pass the data from an iterator into a hash. 72 */ 73 static int cifs_sig_iter(const struct iov_iter *iter, size_t maxsize, 74 struct cifs_calc_sig_ctx *ctx) 75 { 76 struct iov_iter tmp_iter = *iter; 77 size_t did; 78 int err; 79 80 did = iterate_and_advance_kernel(&tmp_iter, maxsize, ctx, &err, 81 cifs_sig_step); 82 if (did != maxsize) 83 return smb_EIO2(smb_eio_trace_sig_iter, did, maxsize); 84 return 0; 85 } 86 87 int __cifs_calc_signature(struct smb_rqst *rqst, struct TCP_Server_Info *server, 88 char *signature, struct cifs_calc_sig_ctx *ctx) 89 { 90 struct iov_iter iter; 91 ssize_t rc; 92 size_t size = 0; 93 94 for (int i = 0; i < rqst->rq_nvec; i++) 95 size += rqst->rq_iov[i].iov_len; 96 97 iov_iter_kvec(&iter, ITER_SOURCE, rqst->rq_iov, rqst->rq_nvec, size); 98 99 if (iov_iter_count(&iter) <= 4) 100 return smb_EIO2(smb_eio_trace_sig_data_too_small, 101 iov_iter_count(&iter), 4); 102 103 rc = cifs_sig_iter(&iter, iov_iter_count(&iter), ctx); 104 if (rc < 0) 105 return rc; 106 107 rc = cifs_sig_iter(&rqst->rq_iter, iov_iter_count(&rqst->rq_iter), ctx); 108 if (rc < 0) 109 return rc; 110 111 rc = cifs_sig_final(ctx, signature); 112 if (rc) 113 cifs_dbg(VFS, "%s: Could not generate hash\n", __func__); 114 115 return rc; 116 } 117 118 /* Build a proper attribute value/target info pairs blob. 119 * Fill in netbios and dns domain name and workstation name 120 * and client time (total five av pairs and + one end of fields indicator. 121 * Allocate domain name which gets freed when session struct is deallocated. 122 */ 123 static int 124 build_avpair_blob(struct cifs_ses *ses, const struct nls_table *nls_cp) 125 { 126 unsigned int dlen; 127 unsigned int size = 2 * sizeof(struct ntlmssp2_name); 128 char *defdmname = "WORKGROUP"; 129 unsigned char *blobptr; 130 struct ntlmssp2_name *attrptr; 131 132 if (!ses->domainName) { 133 ses->domainName = kstrdup(defdmname, GFP_KERNEL); 134 if (!ses->domainName) 135 return -ENOMEM; 136 } 137 138 dlen = strlen(ses->domainName); 139 140 /* 141 * The length of this blob is two times the size of a 142 * structure (av pair) which holds name/size 143 * ( for NTLMSSP_AV_NB_DOMAIN_NAME followed by NTLMSSP_AV_EOL ) + 144 * unicode length of a netbios domain name 145 */ 146 kfree_sensitive(ses->auth_key.response); 147 ses->auth_key.len = size + 2 * dlen; 148 ses->auth_key.response = kzalloc(ses->auth_key.len, GFP_KERNEL); 149 if (!ses->auth_key.response) { 150 ses->auth_key.len = 0; 151 return -ENOMEM; 152 } 153 154 blobptr = ses->auth_key.response; 155 attrptr = (struct ntlmssp2_name *) blobptr; 156 157 /* 158 * As defined in MS-NTLM 3.3.2, just this av pair field 159 * is sufficient as part of the temp 160 */ 161 attrptr->type = cpu_to_le16(NTLMSSP_AV_NB_DOMAIN_NAME); 162 attrptr->length = cpu_to_le16(2 * dlen); 163 blobptr = (unsigned char *)attrptr + sizeof(struct ntlmssp2_name); 164 cifs_strtoUTF16((__le16 *)blobptr, ses->domainName, dlen, nls_cp); 165 166 return 0; 167 } 168 169 #define AV_TYPE(av) (le16_to_cpu(av->type)) 170 #define AV_LEN(av) (le16_to_cpu(av->length)) 171 #define AV_DATA_PTR(av) ((void *)av->data) 172 173 #define av_for_each_entry(ses, av) \ 174 for (av = NULL; (av = find_next_av(ses, av));) 175 176 static struct ntlmssp2_name *find_next_av(struct cifs_ses *ses, 177 struct ntlmssp2_name *av) 178 { 179 u16 len; 180 u8 *end; 181 182 end = (u8 *)ses->auth_key.response + ses->auth_key.len; 183 if (!av) { 184 if (unlikely(!ses->auth_key.response || !ses->auth_key.len)) 185 return NULL; 186 av = (void *)ses->auth_key.response; 187 } else { 188 av = (void *)((u8 *)av + sizeof(*av) + AV_LEN(av)); 189 } 190 191 if ((u8 *)av + sizeof(*av) > end) 192 return NULL; 193 194 len = AV_LEN(av); 195 if (AV_TYPE(av) == NTLMSSP_AV_EOL) 196 return NULL; 197 if ((u8 *)av + sizeof(*av) + len > end) 198 return NULL; 199 return av; 200 } 201 202 /* 203 * Check if server has provided av pair of @type in the NTLMSSP 204 * CHALLENGE_MESSAGE blob. 205 */ 206 static int find_av_name(struct cifs_ses *ses, u16 type, char **name, u16 maxlen) 207 { 208 const struct nls_table *nlsc = ses->local_nls; 209 struct ntlmssp2_name *av; 210 u16 len, nlen; 211 212 if (*name) 213 return 0; 214 215 av_for_each_entry(ses, av) { 216 len = AV_LEN(av); 217 if (AV_TYPE(av) != type || !len) 218 continue; 219 if (!IS_ALIGNED(len, sizeof(__le16))) { 220 cifs_dbg(VFS | ONCE, "%s: bad length(%u) for type %u\n", 221 __func__, len, type); 222 continue; 223 } 224 nlen = len / sizeof(__le16); 225 if (nlen <= maxlen) { 226 ++nlen; 227 *name = kmalloc(nlen, GFP_KERNEL); 228 if (!*name) 229 return -ENOMEM; 230 cifs_from_utf16(*name, AV_DATA_PTR(av), nlen, 231 len, nlsc, NO_MAP_UNI_RSVD); 232 break; 233 } 234 } 235 return 0; 236 } 237 238 /* Server has provided av pairs/target info in the type 2 challenge 239 * packet and we have plucked it and stored within smb session. 240 * We parse that blob here to find the server given timestamp 241 * as part of ntlmv2 authentication (or local current time as 242 * default in case of failure) 243 */ 244 static __le64 find_timestamp(struct cifs_ses *ses) 245 { 246 struct ntlmssp2_name *av; 247 struct timespec64 ts; 248 249 av_for_each_entry(ses, av) { 250 if (AV_TYPE(av) == NTLMSSP_AV_TIMESTAMP && 251 AV_LEN(av) == sizeof(u64)) 252 return *((__le64 *)AV_DATA_PTR(av)); 253 } 254 ktime_get_real_ts64(&ts); 255 return cpu_to_le64(cifs_UnixTimeToNT(ts)); 256 } 257 258 static int calc_ntlmv2_hash(struct cifs_ses *ses, char *ntlmv2_hash, 259 const struct nls_table *nls_cp) 260 { 261 int len; 262 char nt_hash[CIFS_NTHASH_SIZE]; 263 struct hmac_md5_ctx hmac_ctx; 264 __le16 *user; 265 wchar_t *domain; 266 wchar_t *server; 267 268 /* calculate md4 hash of password */ 269 E_md4hash(ses->password, nt_hash, nls_cp); 270 271 hmac_md5_init_usingrawkey(&hmac_ctx, nt_hash, CIFS_NTHASH_SIZE); 272 273 /* convert ses->user_name to unicode */ 274 len = ses->user_name ? strlen(ses->user_name) : 0; 275 user = kmalloc(2 + (len * 2), GFP_KERNEL); 276 if (user == NULL) 277 return -ENOMEM; 278 279 if (len) { 280 len = cifs_strtoUTF16(user, ses->user_name, len, nls_cp); 281 UniStrupr(user); 282 } else { 283 *(u16 *)user = 0; 284 } 285 286 hmac_md5_update(&hmac_ctx, (const u8 *)user, 2 * len); 287 kfree(user); 288 289 /* convert ses->domainName to unicode and uppercase */ 290 if (ses->domainName) { 291 len = strlen(ses->domainName); 292 293 domain = kmalloc(2 + (len * 2), GFP_KERNEL); 294 if (domain == NULL) 295 return -ENOMEM; 296 297 len = cifs_strtoUTF16((__le16 *)domain, ses->domainName, len, 298 nls_cp); 299 hmac_md5_update(&hmac_ctx, (const u8 *)domain, 2 * len); 300 kfree(domain); 301 } else { 302 /* We use ses->ip_addr if no domain name available */ 303 len = strlen(ses->ip_addr); 304 305 server = kmalloc(2 + (len * 2), GFP_KERNEL); 306 if (server == NULL) 307 return -ENOMEM; 308 309 len = cifs_strtoUTF16((__le16 *)server, ses->ip_addr, len, nls_cp); 310 hmac_md5_update(&hmac_ctx, (const u8 *)server, 2 * len); 311 kfree(server); 312 } 313 314 hmac_md5_final(&hmac_ctx, ntlmv2_hash); 315 return 0; 316 } 317 318 static void CalcNTLMv2_response(const struct cifs_ses *ses, char *ntlmv2_hash) 319 { 320 struct ntlmv2_resp *ntlmv2 = (struct ntlmv2_resp *) 321 (ses->auth_key.response + CIFS_SESS_KEY_SIZE); 322 unsigned int hash_len; 323 324 /* The MD5 hash starts at challenge_key.key */ 325 hash_len = ses->auth_key.len - (CIFS_SESS_KEY_SIZE + 326 offsetof(struct ntlmv2_resp, challenge.key[0])); 327 328 if (ses->server->negflavor == CIFS_NEGFLAVOR_EXTENDED) 329 memcpy(ntlmv2->challenge.key, ses->ntlmssp->cryptkey, CIFS_SERVER_CHALLENGE_SIZE); 330 else 331 memcpy(ntlmv2->challenge.key, ses->server->cryptkey, CIFS_SERVER_CHALLENGE_SIZE); 332 333 /* Note that the HMAC-MD5 value overwrites ntlmv2->challenge.key */ 334 hmac_md5_usingrawkey(ntlmv2_hash, CIFS_HMAC_MD5_HASH_SIZE, 335 ntlmv2->challenge.key, hash_len, 336 ntlmv2->ntlmv2_hash); 337 } 338 339 /* 340 * Set up NTLMv2 response blob with SPN (cifs/<hostname>) appended to the 341 * existing list of AV pairs. 342 */ 343 static int set_auth_key_response(struct cifs_ses *ses) 344 { 345 size_t baselen = CIFS_SESS_KEY_SIZE + sizeof(struct ntlmv2_resp); 346 size_t len, spnlen, tilen = 0, num_avs = 2 /* SPN + EOL */; 347 struct TCP_Server_Info *server = ses->server; 348 char *spn __free(kfree) = NULL; 349 struct ntlmssp2_name *av; 350 char *rsp = NULL; 351 int rc; 352 353 spnlen = strlen(server->hostname); 354 len = sizeof("cifs/") + spnlen; 355 spn = kmalloc(len, GFP_KERNEL); 356 if (!spn) { 357 rc = -ENOMEM; 358 goto out; 359 } 360 361 spnlen = scnprintf(spn, len, "cifs/%.*s", 362 (int)spnlen, server->hostname); 363 364 av_for_each_entry(ses, av) 365 tilen += sizeof(*av) + AV_LEN(av); 366 367 len = baselen + tilen + spnlen * sizeof(__le16) + num_avs * sizeof(*av); 368 rsp = kmalloc(len, GFP_KERNEL); 369 if (!rsp) { 370 rc = -ENOMEM; 371 goto out; 372 } 373 374 memcpy(rsp + baselen, ses->auth_key.response, tilen); 375 av = (void *)(rsp + baselen + tilen); 376 av->type = cpu_to_le16(NTLMSSP_AV_TARGET_NAME); 377 av->length = cpu_to_le16(spnlen * sizeof(__le16)); 378 cifs_strtoUTF16((__le16 *)av->data, spn, spnlen, ses->local_nls); 379 av = (void *)((__u8 *)av + sizeof(*av) + AV_LEN(av)); 380 av->type = cpu_to_le16(NTLMSSP_AV_EOL); 381 av->length = 0; 382 383 rc = 0; 384 ses->auth_key.len = len; 385 out: 386 ses->auth_key.response = rsp; 387 return rc; 388 } 389 390 int 391 setup_ntlmv2_rsp(struct cifs_ses *ses, const struct nls_table *nls_cp) 392 { 393 unsigned char *tiblob = NULL; /* target info blob */ 394 struct ntlmv2_resp *ntlmv2; 395 char ntlmv2_hash[16]; 396 __le64 rsp_timestamp; 397 __u64 cc; 398 int rc; 399 400 if (nls_cp == NULL) { 401 cifs_dbg(VFS, "%s called with nls_cp==NULL\n", __func__); 402 return -EINVAL; 403 } 404 405 if (ses->server->negflavor == CIFS_NEGFLAVOR_EXTENDED) { 406 if (!ses->domainName) { 407 if (ses->domainAuto) { 408 /* 409 * Domain (workgroup) hasn't been specified in 410 * mount options, so try to find it in 411 * CHALLENGE_MESSAGE message and then use it as 412 * part of NTLMv2 authentication. 413 */ 414 rc = find_av_name(ses, NTLMSSP_AV_NB_DOMAIN_NAME, 415 &ses->domainName, 416 CIFS_MAX_DOMAINNAME_LEN); 417 if (rc) 418 goto setup_ntlmv2_rsp_ret; 419 } else { 420 ses->domainName = kstrdup("", GFP_KERNEL); 421 if (!ses->domainName) { 422 rc = -ENOMEM; 423 goto setup_ntlmv2_rsp_ret; 424 } 425 } 426 } 427 rc = find_av_name(ses, NTLMSSP_AV_DNS_DOMAIN_NAME, 428 &ses->dns_dom, CIFS_MAX_DOMAINNAME_LEN); 429 if (rc) 430 goto setup_ntlmv2_rsp_ret; 431 } else { 432 rc = build_avpair_blob(ses, nls_cp); 433 if (rc) { 434 cifs_dbg(VFS, "error %d building av pair blob\n", rc); 435 goto setup_ntlmv2_rsp_ret; 436 } 437 } 438 439 /* Must be within 5 minutes of the server (or in range +/-2h 440 * in case of Mac OS X), so simply carry over server timestamp 441 * (as Windows 7 does) 442 */ 443 rsp_timestamp = find_timestamp(ses); 444 get_random_bytes(&cc, sizeof(cc)); 445 446 cifs_server_lock(ses->server); 447 448 tiblob = ses->auth_key.response; 449 rc = set_auth_key_response(ses); 450 if (rc) { 451 ses->auth_key.len = 0; 452 goto unlock; 453 } 454 455 ntlmv2 = (struct ntlmv2_resp *) 456 (ses->auth_key.response + CIFS_SESS_KEY_SIZE); 457 ntlmv2->blob_signature = cpu_to_le32(0x00000101); 458 ntlmv2->reserved = 0; 459 ntlmv2->time = rsp_timestamp; 460 ntlmv2->client_chal = cc; 461 ntlmv2->reserved2 = 0; 462 463 if (fips_enabled) { 464 cifs_dbg(VFS, "NTLMv2 support is disabled due to FIPS\n"); 465 rc = -EOPNOTSUPP; 466 goto unlock; 467 } 468 469 /* calculate ntlmv2_hash */ 470 rc = calc_ntlmv2_hash(ses, ntlmv2_hash, nls_cp); 471 if (rc) { 472 cifs_dbg(VFS, "Could not get NTLMv2 hash, rc=%d\n", rc); 473 goto unlock; 474 } 475 476 /* calculate first part of the client response (CR1) */ 477 CalcNTLMv2_response(ses, ntlmv2_hash); 478 479 /* now calculate the session key for NTLMv2 */ 480 hmac_md5_usingrawkey(ntlmv2_hash, CIFS_HMAC_MD5_HASH_SIZE, 481 ntlmv2->ntlmv2_hash, CIFS_HMAC_MD5_HASH_SIZE, 482 ses->auth_key.response); 483 rc = 0; 484 unlock: 485 cifs_server_unlock(ses->server); 486 setup_ntlmv2_rsp_ret: 487 kfree_sensitive(tiblob); 488 489 return rc; 490 } 491 492 int 493 calc_seckey(struct cifs_ses *ses) 494 { 495 unsigned char sec_key[CIFS_SESS_KEY_SIZE]; /* a nonce */ 496 struct arc4_ctx *ctx_arc4; 497 498 if (fips_enabled) 499 return -ENODEV; 500 501 get_random_bytes(sec_key, CIFS_SESS_KEY_SIZE); 502 503 ctx_arc4 = kmalloc(sizeof(*ctx_arc4), GFP_KERNEL); 504 if (!ctx_arc4) { 505 cifs_dbg(VFS, "Could not allocate arc4 context\n"); 506 return -ENOMEM; 507 } 508 509 arc4_setkey(ctx_arc4, ses->auth_key.response, CIFS_SESS_KEY_SIZE); 510 arc4_crypt(ctx_arc4, ses->ntlmssp->ciphertext, sec_key, 511 CIFS_CPHTXT_SIZE); 512 513 /* make secondary_key/nonce as session key */ 514 memcpy(ses->auth_key.response, sec_key, CIFS_SESS_KEY_SIZE); 515 /* and make len as that of session key only */ 516 ses->auth_key.len = CIFS_SESS_KEY_SIZE; 517 518 memzero_explicit(sec_key, CIFS_SESS_KEY_SIZE); 519 kfree_sensitive(ctx_arc4); 520 return 0; 521 } 522 523 void 524 cifs_crypto_secmech_release(struct TCP_Server_Info *server) 525 { 526 cifs_free_hash(&server->secmech.aes_cmac); 527 528 if (server->secmech.enc) { 529 crypto_free_aead(server->secmech.enc); 530 server->secmech.enc = NULL; 531 } 532 if (server->secmech.dec) { 533 crypto_free_aead(server->secmech.dec); 534 server->secmech.dec = NULL; 535 } 536 } 537