1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2011, Daniel Stenberg, <daniel@haxx.se>, et al.
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at http://curl.haxx.se/docs/copyright.html.
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
25 #if defined(USE_NTLM) && !defined(USE_WINDOWS_SSPI)
30 * http://davenport.sourceforge.net/ntlm.html
31 * http://www.innovation.ch/java/ntlm.html
37 # include <openssl/des.h>
38 # ifndef OPENSSL_NO_MD4
39 # include <openssl/md4.h>
41 # include <openssl/md5.h>
42 # include <openssl/ssl.h>
43 # include <openssl/rand.h>
46 # ifndef OPENSSL_NO_MD4
53 # if (OPENSSL_VERSION_NUMBER < 0x00907001L)
54 # define DES_key_schedule des_key_schedule
55 # define DES_cblock des_cblock
56 # define DES_set_odd_parity des_set_odd_parity
57 # define DES_set_key des_set_key
58 # define DES_ecb_encrypt des_ecb_encrypt
60 # define DESKEYARG(x) x
62 # define DESKEYARG(x) *x
66 #elif defined(USE_GNUTLS)
69 # define MD5_DIGEST_LENGTH 16
70 # define MD4_DIGEST_LENGTH 16
72 #elif defined(USE_NSS)
77 # include "curl_md4.h"
78 # define MD5_DIGEST_LENGTH MD5_LENGTH
81 # error "Can't compile NTLM support without a crypto library."
85 #include "non-ascii.h"
87 #include "curl_memory.h"
88 #include "curl_ntlm_core.h"
90 #define _MPRINTF_REPLACE /* use our functions only */
91 #include <curl/mprintf.h>
93 /* The last #include file should be: */
98 * Turns a 56 bit key into the 64 bit, odd parity key and sets the key. The
99 * key schedule ks is also set.
101 static void setup_des_key(const unsigned char *key_56,
102 DES_key_schedule DESKEYARG(ks))
107 key[1] = (unsigned char)(((key_56[0] << 7) & 0xFF) | (key_56[1] >> 1));
108 key[2] = (unsigned char)(((key_56[1] << 6) & 0xFF) | (key_56[2] >> 2));
109 key[3] = (unsigned char)(((key_56[2] << 5) & 0xFF) | (key_56[3] >> 3));
110 key[4] = (unsigned char)(((key_56[3] << 4) & 0xFF) | (key_56[4] >> 4));
111 key[5] = (unsigned char)(((key_56[4] << 3) & 0xFF) | (key_56[5] >> 5));
112 key[6] = (unsigned char)(((key_56[5] << 2) & 0xFF) | (key_56[6] >> 6));
113 key[7] = (unsigned char) ((key_56[6] << 1) & 0xFF);
115 DES_set_odd_parity(&key);
116 DES_set_key(&key, ks);
119 #else /* defined(USE_SSLEAY) */
122 * Turns a 56 bit key into the 64 bit, odd parity key. Used by GnuTLS and NSS.
124 static void extend_key_56_to_64(const unsigned char *key_56, char *key)
127 key[1] = (unsigned char)(((key_56[0] << 7) & 0xFF) | (key_56[1] >> 1));
128 key[2] = (unsigned char)(((key_56[1] << 6) & 0xFF) | (key_56[2] >> 2));
129 key[3] = (unsigned char)(((key_56[2] << 5) & 0xFF) | (key_56[3] >> 3));
130 key[4] = (unsigned char)(((key_56[3] << 4) & 0xFF) | (key_56[4] >> 4));
131 key[5] = (unsigned char)(((key_56[4] << 3) & 0xFF) | (key_56[5] >> 5));
132 key[6] = (unsigned char)(((key_56[5] << 2) & 0xFF) | (key_56[6] >> 6));
133 key[7] = (unsigned char) ((key_56[6] << 1) & 0xFF);
136 #if defined(USE_GNUTLS)
139 * Turns a 56 bit key into the 64 bit, odd parity key and sets the key.
141 static void setup_des_key(const unsigned char *key_56,
142 gcry_cipher_hd_t *des)
145 extend_key_56_to_64(key_56, key);
146 gcry_cipher_setkey(*des, key, 8);
149 #elif defined(USE_NSS)
152 * Expands a 56 bit key KEY_56 to 64 bit and encrypts 64 bit of data, using
153 * the expanded key. The caller is responsible for giving 64 bit of valid
154 * data is IN and (at least) 64 bit large buffer as OUT.
156 static bool encrypt_des(const unsigned char *in, unsigned char *out,
157 const unsigned char *key_56)
159 const CK_MECHANISM_TYPE mech = CKM_DES_ECB; /* DES cipher in ECB mode */
160 PK11SlotInfo *slot = NULL;
161 char key[8]; /* expanded 64 bit key */
163 PK11SymKey *symkey = NULL;
164 SECItem *param = NULL;
165 PK11Context *ctx = NULL;
166 int out_len; /* not used, required by NSS */
169 /* use internal slot for DES encryption (requires NSS to be initialized) */
170 slot = PK11_GetInternalKeySlot();
174 /* expand the 56 bit key to 64 bit and wrap by NSS */
175 extend_key_56_to_64(key_56, key);
176 key_item.data = (unsigned char *)key;
177 key_item.len = /* hard-wired */ 8;
178 symkey = PK11_ImportSymKey(slot, mech, PK11_OriginUnwrap, CKA_ENCRYPT,
183 /* create DES encryption context */
184 param = PK11_ParamFromIV(mech, /* no IV in ECB mode */ NULL);
187 ctx = PK11_CreateContextBySymKey(mech, CKA_ENCRYPT, symkey, param);
191 /* perform the encryption */
192 if(SECSuccess == PK11_CipherOp(ctx, out, &out_len, /* outbuflen */ 8,
193 (unsigned char *)in, /* inbuflen */ 8)
194 && SECSuccess == PK11_Finalize(ctx))
195 rv = /* all OK */ TRUE;
200 PK11_DestroyContext(ctx, PR_TRUE);
202 PK11_FreeSymKey(symkey);
204 SECITEM_FreeItem(param, PR_TRUE);
209 #endif /* defined(USE_NSS) */
211 #endif /* defined(USE_SSLEAY) */
214 * takes a 21 byte array and treats it as 3 56-bit DES keys. The
215 * 8 byte plaintext is encrypted with each key and the resulting 24
216 * bytes are stored in the results array.
218 void Curl_ntlm_core_lm_resp(const unsigned char *keys,
219 const unsigned char *plaintext,
220 unsigned char *results)
225 setup_des_key(keys, DESKEY(ks));
226 DES_ecb_encrypt((DES_cblock*) plaintext, (DES_cblock*) results,
227 DESKEY(ks), DES_ENCRYPT);
229 setup_des_key(keys + 7, DESKEY(ks));
230 DES_ecb_encrypt((DES_cblock*) plaintext, (DES_cblock*) (results + 8),
231 DESKEY(ks), DES_ENCRYPT);
233 setup_des_key(keys + 14, DESKEY(ks));
234 DES_ecb_encrypt((DES_cblock*) plaintext, (DES_cblock*) (results + 16),
235 DESKEY(ks), DES_ENCRYPT);
236 #elif defined(USE_GNUTLS)
237 gcry_cipher_hd_t des;
239 gcry_cipher_open(&des, GCRY_CIPHER_DES, GCRY_CIPHER_MODE_ECB, 0);
240 setup_des_key(keys, &des);
241 gcry_cipher_encrypt(des, results, 8, plaintext, 8);
242 gcry_cipher_close(des);
244 gcry_cipher_open(&des, GCRY_CIPHER_DES, GCRY_CIPHER_MODE_ECB, 0);
245 setup_des_key(keys + 7, &des);
246 gcry_cipher_encrypt(des, results + 8, 8, plaintext, 8);
247 gcry_cipher_close(des);
249 gcry_cipher_open(&des, GCRY_CIPHER_DES, GCRY_CIPHER_MODE_ECB, 0);
250 setup_des_key(keys + 14, &des);
251 gcry_cipher_encrypt(des, results + 16, 8, plaintext, 8);
252 gcry_cipher_close(des);
253 #elif defined(USE_NSS)
254 encrypt_des(plaintext, results, keys);
255 encrypt_des(plaintext, results + 8, keys + 7);
256 encrypt_des(plaintext, results + 16, keys + 14);
261 * Set up lanmanager hashed password
263 void Curl_ntlm_core_mk_lm_hash(struct SessionHandle *data,
264 const char *password,
265 unsigned char *lmbuffer /* 21 bytes */)
268 unsigned char pw[14];
269 static const unsigned char magic[] = {
270 0x4B, 0x47, 0x53, 0x21, 0x40, 0x23, 0x24, 0x25 /* i.e. KGS!@#$% */
272 size_t len = CURLMIN(strlen(password), 14);
274 Curl_strntoupper((char *)pw, password, len);
275 memset(&pw[len], 0, 14 - len);
278 * The LanManager hashed password needs to be created using the
279 * password in the network encoding not the host encoding.
281 res = Curl_convert_to_network(data, (char *)pw, 14);
286 /* Create LanManager hashed password. */
291 setup_des_key(pw, DESKEY(ks));
292 DES_ecb_encrypt((DES_cblock *)magic, (DES_cblock *)lmbuffer,
293 DESKEY(ks), DES_ENCRYPT);
295 setup_des_key(pw + 7, DESKEY(ks));
296 DES_ecb_encrypt((DES_cblock *)magic, (DES_cblock *)(lmbuffer + 8),
297 DESKEY(ks), DES_ENCRYPT);
298 #elif defined(USE_GNUTLS)
299 gcry_cipher_hd_t des;
301 gcry_cipher_open(&des, GCRY_CIPHER_DES, GCRY_CIPHER_MODE_ECB, 0);
302 setup_des_key(pw, &des);
303 gcry_cipher_encrypt(des, lmbuffer, 8, magic, 8);
304 gcry_cipher_close(des);
306 gcry_cipher_open(&des, GCRY_CIPHER_DES, GCRY_CIPHER_MODE_ECB, 0);
307 setup_des_key(pw + 7, &des);
308 gcry_cipher_encrypt(des, lmbuffer + 8, 8, magic, 8);
309 gcry_cipher_close(des);
310 #elif defined(USE_NSS)
311 encrypt_des(magic, lmbuffer, pw);
312 encrypt_des(magic, lmbuffer + 8, pw + 7);
315 memset(lmbuffer + 16, 0, 21 - 16);
320 static void ascii_to_unicode_le(unsigned char *dest, const char *src,
324 for(i = 0; i < srclen; i++) {
325 dest[2 * i] = (unsigned char)src[i];
326 dest[2 * i + 1] = '\0';
331 * Set up nt hashed passwords
333 CURLcode Curl_ntlm_core_mk_nt_hash(struct SessionHandle *data,
334 const char *password,
335 unsigned char *ntbuffer /* 21 bytes */)
337 size_t len = strlen(password);
338 unsigned char *pw = malloc(len * 2);
341 return CURLE_OUT_OF_MEMORY;
343 ascii_to_unicode_le(pw, password, len);
346 * The NT hashed password needs to be created using the password in the
347 * network encoding not the host encoding.
349 result = Curl_convert_to_network(data, (char *)pw, len * 2);
354 /* Create NT hashed password. */
358 MD4_Update(&MD4pw, pw, 2 * len);
359 MD4_Final(ntbuffer, &MD4pw);
360 #elif defined(USE_GNUTLS)
362 gcry_md_open(&MD4pw, GCRY_MD_MD4, 0);
363 gcry_md_write(MD4pw, pw, 2 * len);
364 memcpy (ntbuffer, gcry_md_read (MD4pw, 0), MD4_DIGEST_LENGTH);
365 gcry_md_close(MD4pw);
366 #elif defined(USE_NSS)
367 Curl_md4it(ntbuffer, pw, 2 * len);
370 memset(ntbuffer + 16, 0, 21 - 16);
377 #endif /* USE_NTRESPONSES */
379 #endif /* USE_NTLM && !USE_WINDOWS_SSPI */