1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2016, 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 https://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 ***************************************************************************/
23 #include "curl_setup.h"
25 #ifdef USE_WINDOWS_SSPI
27 #include <curl/curl.h>
28 #include "curl_sspi.h"
29 #include "curl_multibyte.h"
30 #include "system_win32.h"
33 /* The last #include files should be: */
34 #include "curl_memory.h"
37 /* We use our own typedef here since some headers might lack these */
38 typedef PSecurityFunctionTable (APIENTRY *INITSECURITYINTERFACE_FN)(VOID);
40 /* See definition of SECURITY_ENTRYPOINT in sspi.h */
43 # define SECURITYENTRYPOINT L"InitSecurityInterfaceW"
45 # define SECURITYENTRYPOINT "InitSecurityInterfaceW"
48 # define SECURITYENTRYPOINT "InitSecurityInterfaceA"
51 /* Handle of security.dll or secur32.dll, depending on Windows version */
52 HMODULE s_hSecDll = NULL;
54 /* Pointer to SSPI dispatch table */
55 PSecurityFunctionTable s_pSecFn = NULL;
58 * Curl_sspi_global_init()
60 * This is used to load the Security Service Provider Interface (SSPI)
61 * dynamic link library portably across all Windows versions, without
62 * the need to directly link libcurl, nor the application using it, at
65 * Once this function has been executed, Windows SSPI functions can be
66 * called through the Security Service Provider Interface dispatch table.
72 * Returns CURLE_OK on success.
74 CURLcode Curl_sspi_global_init(void)
76 INITSECURITYINTERFACE_FN pInitSecurityInterface;
78 /* If security interface is not yet initialized try to do this */
80 /* Security Service Provider Interface (SSPI) functions are located in
81 * security.dll on WinNT 4.0 and in secur32.dll on Win9x. Win2K and XP
82 * have both these DLLs (security.dll forwards calls to secur32.dll) */
84 /* Load SSPI dll into the address space of the calling process */
85 if(Curl_verify_windows_version(4, 0, PLATFORM_WINNT, VERSION_EQUAL))
86 s_hSecDll = Curl_load_library(TEXT("security.dll"));
88 s_hSecDll = Curl_load_library(TEXT("secur32.dll"));
90 return CURLE_FAILED_INIT;
92 /* Get address of the InitSecurityInterfaceA function from the SSPI dll */
93 pInitSecurityInterface = (INITSECURITYINTERFACE_FN)
94 GetProcAddress(s_hSecDll, SECURITYENTRYPOINT);
95 if(!pInitSecurityInterface)
96 return CURLE_FAILED_INIT;
98 /* Get pointer to Security Service Provider Interface dispatch table */
99 s_pSecFn = pInitSecurityInterface();
101 return CURLE_FAILED_INIT;
108 * Curl_sspi_global_cleanup()
110 * This deinitializes the Security Service Provider Interface from libcurl.
116 void Curl_sspi_global_cleanup(void)
119 FreeLibrary(s_hSecDll);
126 * Curl_create_sspi_identity()
128 * This is used to populate a SSPI identity structure based on the supplied
129 * username and password.
133 * userp [in] - The user name in the format User or Domain\User.
134 * passdwp [in] - The user's password.
135 * identity [in/out] - The identity structure.
137 * Returns CURLE_OK on success.
139 CURLcode Curl_create_sspi_identity(const char *userp, const char *passwdp,
140 SEC_WINNT_AUTH_IDENTITY *identity)
142 xcharp_u useranddomain;
143 xcharp_u user, dup_user;
144 xcharp_u domain, dup_domain;
145 xcharp_u passwd, dup_passwd;
148 domain.const_tchar_ptr = TEXT("");
150 /* Initialize the identity */
151 memset(identity, 0, sizeof(*identity));
153 useranddomain.tchar_ptr = Curl_convert_UTF8_to_tchar((char *)userp);
154 if(!useranddomain.tchar_ptr)
155 return CURLE_OUT_OF_MEMORY;
157 user.const_tchar_ptr = _tcschr(useranddomain.const_tchar_ptr, TEXT('\\'));
158 if(!user.const_tchar_ptr)
159 user.const_tchar_ptr = _tcschr(useranddomain.const_tchar_ptr, TEXT('/'));
162 domain.tchar_ptr = useranddomain.tchar_ptr;
163 domlen = user.tchar_ptr - useranddomain.tchar_ptr;
167 user.tchar_ptr = useranddomain.tchar_ptr;
168 domain.const_tchar_ptr = TEXT("");
172 /* Setup the identity's user and length */
173 dup_user.tchar_ptr = _tcsdup(user.tchar_ptr);
174 if(!dup_user.tchar_ptr) {
175 Curl_unicodefree(useranddomain.tchar_ptr);
176 return CURLE_OUT_OF_MEMORY;
178 identity->User = dup_user.tbyte_ptr;
179 identity->UserLength = curlx_uztoul(_tcslen(dup_user.tchar_ptr));
180 dup_user.tchar_ptr = NULL;
182 /* Setup the identity's domain and length */
183 dup_domain.tchar_ptr = malloc(sizeof(TCHAR) * (domlen + 1));
184 if(!dup_domain.tchar_ptr) {
185 Curl_unicodefree(useranddomain.tchar_ptr);
186 return CURLE_OUT_OF_MEMORY;
188 _tcsncpy(dup_domain.tchar_ptr, domain.tchar_ptr, domlen);
189 *(dup_domain.tchar_ptr + domlen) = TEXT('\0');
190 identity->Domain = dup_domain.tbyte_ptr;
191 identity->DomainLength = curlx_uztoul(domlen);
192 dup_domain.tchar_ptr = NULL;
194 Curl_unicodefree(useranddomain.tchar_ptr);
196 /* Setup the identity's password and length */
197 passwd.tchar_ptr = Curl_convert_UTF8_to_tchar((char *)passwdp);
198 if(!passwd.tchar_ptr)
199 return CURLE_OUT_OF_MEMORY;
200 dup_passwd.tchar_ptr = _tcsdup(passwd.tchar_ptr);
201 if(!dup_passwd.tchar_ptr) {
202 Curl_unicodefree(passwd.tchar_ptr);
203 return CURLE_OUT_OF_MEMORY;
205 identity->Password = dup_passwd.tbyte_ptr;
206 identity->PasswordLength = curlx_uztoul(_tcslen(dup_passwd.tchar_ptr));
207 dup_passwd.tchar_ptr = NULL;
209 Curl_unicodefree(passwd.tchar_ptr);
211 /* Setup the identity's flags */
212 identity->Flags = SECFLAG_WINNT_AUTH_IDENTITY;
218 * Curl_sspi_free_identity()
220 * This is used to free the contents of a SSPI identifier structure.
224 * identity [in/out] - The identity structure.
226 void Curl_sspi_free_identity(SEC_WINNT_AUTH_IDENTITY *identity)
229 Curl_safefree(identity->User);
230 Curl_safefree(identity->Password);
231 Curl_safefree(identity->Domain);
235 #endif /* USE_WINDOWS_SSPI */