10 #define DECL_SSPI_FUNCTION(linkage, rettype, name, params) \
11 typedef rettype (WINAPI *t_##name) params; \
12 linkage t_##name p_##name
13 #define GET_SSPI_FUNCTION(module, name) \
14 p_##name = module ? (t_##name) GetProcAddress(module, #name) : NULL
16 DECL_SSPI_FUNCTION(static, SECURITY_STATUS,
17 AcquireCredentialsHandleA,
18 (SEC_CHAR *, SEC_CHAR *, ULONG, PLUID,
19 PVOID, SEC_GET_KEY_FN, PVOID, PCredHandle, PTimeStamp));
20 DECL_SSPI_FUNCTION(static, SECURITY_STATUS,
21 InitializeSecurityContextA,
22 (PCredHandle, PCtxtHandle, SEC_CHAR *, ULONG, ULONG,
23 ULONG, PSecBufferDesc, ULONG, PCtxtHandle,
24 PSecBufferDesc, PULONG, PTimeStamp));
25 DECL_SSPI_FUNCTION(static, SECURITY_STATUS,
28 DECL_SSPI_FUNCTION(static, SECURITY_STATUS,
29 FreeCredentialsHandle,
31 DECL_SSPI_FUNCTION(static, SECURITY_STATUS,
32 DeleteSecurityContext,
34 DECL_SSPI_FUNCTION(static, SECURITY_STATUS,
35 QueryContextAttributesA,
36 (PCtxtHandle, ULONG, PVOID));
37 DECL_SSPI_FUNCTION(static, SECURITY_STATUS,
39 (PCtxtHandle, ULONG, PSecBufferDesc, ULONG));
41 static HMODULE security_module = NULL;
43 typedef struct winSsh_gss_ctx {
44 unsigned long maj_stat;
45 unsigned long min_stat;
46 CredHandle cred_handle;
48 PCtxtHandle context_handle;
53 const Ssh_gss_buf gss_mech_krb5={9,"\x2A\x86\x48\x86\xF7\x12\x01\x02\x02"};
55 int ssh_gss_init(void)
58 return 1; /* already initialised */
60 security_module = LoadLibrary("secur32.dll");
61 if (security_module) {
62 GET_SSPI_FUNCTION(security_module, AcquireCredentialsHandleA);
63 GET_SSPI_FUNCTION(security_module, InitializeSecurityContextA);
64 GET_SSPI_FUNCTION(security_module, FreeContextBuffer);
65 GET_SSPI_FUNCTION(security_module, FreeCredentialsHandle);
66 GET_SSPI_FUNCTION(security_module, DeleteSecurityContext);
67 GET_SSPI_FUNCTION(security_module, QueryContextAttributesA);
68 GET_SSPI_FUNCTION(security_module, MakeSignature);
74 Ssh_gss_stat ssh_gss_indicate_mech(Ssh_gss_buf *mech)
76 *mech = gss_mech_krb5;
81 Ssh_gss_stat ssh_gss_import_name(char *host, Ssh_gss_name *srv_name)
86 if (host == NULL) return SSH_GSS_FAILURE;
88 /* copy it into form host/FQDN */
89 pStr = dupcat("host/", host, NULL);
91 *srv_name = (Ssh_gss_name) pStr;
96 Ssh_gss_stat ssh_gss_acquire_cred(Ssh_gss_ctx *ctx)
98 winSsh_gss_ctx *winctx = snew(winSsh_gss_ctx);
100 /* prepare our "wrapper" structure */
101 winctx->maj_stat = winctx->min_stat = SEC_E_OK;
102 winctx->context_handle = NULL;
104 /* Specifying no principal name here means use the credentials of
105 the current logged-in user */
107 winctx->maj_stat = p_AcquireCredentialsHandleA(NULL,
109 SECPKG_CRED_OUTBOUND,
114 &winctx->cred_handle,
117 if (winctx->maj_stat != SEC_E_OK) return SSH_GSS_FAILURE;
119 *ctx = (Ssh_gss_ctx) winctx;
124 Ssh_gss_stat ssh_gss_init_sec_context(Ssh_gss_ctx *ctx,
125 Ssh_gss_name srv_name,
127 Ssh_gss_buf *recv_tok,
128 Ssh_gss_buf *send_tok)
130 winSsh_gss_ctx *winctx = (winSsh_gss_ctx *) *ctx;
131 SecBuffer wsend_tok = {send_tok->len,SECBUFFER_TOKEN,send_tok->data};
132 SecBuffer wrecv_tok = {recv_tok->len,SECBUFFER_TOKEN,recv_tok->data};
133 SecBufferDesc output_desc={SECBUFFER_VERSION,1,&wsend_tok};
134 SecBufferDesc input_desc ={SECBUFFER_VERSION,1,&wrecv_tok};
135 unsigned long flags=ISC_REQ_MUTUAL_AUTH|ISC_REQ_REPLAY_DETECT|
136 ISC_REQ_CONFIDENTIALITY|ISC_REQ_ALLOCATE_MEMORY;
137 unsigned long ret_flags=0;
139 /* check if we have to delegate ... */
140 if (to_deleg) flags |= ISC_REQ_DELEGATE;
141 winctx->maj_stat = p_InitializeSecurityContextA(&winctx->cred_handle,
142 winctx->context_handle,
146 SECURITY_NATIVE_DREP,
154 /* prepare for the next round */
155 winctx->context_handle = &winctx->context;
156 send_tok->data = (char*) wsend_tok.pvBuffer;
157 send_tok->len = wsend_tok.cbBuffer;
159 /* check & return our status */
160 if (winctx->maj_stat==SEC_E_OK) return SSH_GSS_S_COMPLETE;
161 if (winctx->maj_stat==SEC_I_CONTINUE_NEEDED) return SSH_GSS_S_CONTINUE_NEEDED;
163 return SSH_GSS_FAILURE;
166 Ssh_gss_stat ssh_gss_free_tok(Ssh_gss_buf *send_tok)
169 if (send_tok == NULL) return SSH_GSS_FAILURE;
171 /* free Windows buffer */
172 p_FreeContextBuffer(send_tok->data);
173 send_tok->len = 0; send_tok->data = NULL;
178 Ssh_gss_stat ssh_gss_release_cred(Ssh_gss_ctx *ctx)
180 winSsh_gss_ctx *winctx= (winSsh_gss_ctx *) *ctx;
183 if (winctx == NULL) return SSH_GSS_FAILURE;
185 /* free Windows data */
186 p_FreeCredentialsHandle(&winctx->cred_handle);
187 p_DeleteSecurityContext(&winctx->context);
189 /* delete our "wrapper" structure */
191 *ctx = (Ssh_gss_ctx) NULL;
197 Ssh_gss_stat ssh_gss_release_name(Ssh_gss_name *srv_name)
199 char *pStr= (char *) *srv_name;
201 if (pStr == NULL) return SSH_GSS_FAILURE;
203 *srv_name = (Ssh_gss_name) NULL;
208 Ssh_gss_stat ssh_gss_display_status(Ssh_gss_ctx ctx, Ssh_gss_buf *buf)
210 winSsh_gss_ctx *winctx = (winSsh_gss_ctx *) ctx;
213 if (winctx == NULL) return SSH_GSS_FAILURE;
215 /* decode the error code */
216 switch (winctx->maj_stat) {
217 case SEC_E_OK: msg="SSPI status OK"; break;
218 case SEC_E_INVALID_HANDLE: msg="The handle passed to the function"
221 case SEC_E_TARGET_UNKNOWN: msg="The target was not recognized."; break;
222 case SEC_E_LOGON_DENIED: msg="The logon failed."; break;
223 case SEC_E_INTERNAL_ERROR: msg="The Local Security Authority cannot"
226 case SEC_E_NO_CREDENTIALS: msg="No credentials are available in the"
227 " security package.";
229 case SEC_E_NO_AUTHENTICATING_AUTHORITY:
230 msg="No authority could be contacted for authentication."
231 "The domain name of the authenticating party could be wrong,"
232 " the domain could be unreachable, or there might have been"
233 " a trust relationship failure.";
235 case SEC_E_INSUFFICIENT_MEMORY:
236 msg="One or more of the SecBufferDesc structures passed as"
237 " an OUT parameter has a buffer that is too small.";
239 case SEC_E_INVALID_TOKEN:
240 msg="The error is due to a malformed input token, such as a"
241 " token corrupted in transit, a token"
242 " of incorrect size, or a token passed into the wrong"
243 " security package. Passing a token to"
244 " the wrong package can happen if client and server did not"
245 " negotiate the proper security package.";
248 msg = "Internal SSPI error";
252 buf->data = dupstr(msg);
253 buf->len = strlen(buf->data);
258 Ssh_gss_stat ssh_gss_get_mic(Ssh_gss_ctx ctx, Ssh_gss_buf *buf,
261 winSsh_gss_ctx *winctx= (winSsh_gss_ctx *) ctx;
262 SecPkgContext_Sizes ContextSizes;
263 SecBufferDesc InputBufferDescriptor;
264 SecBuffer InputSecurityToken[2];
266 if (winctx == NULL) return SSH_GSS_FAILURE;
268 winctx->maj_stat = 0;
270 memset(&ContextSizes, 0, sizeof(ContextSizes));
272 winctx->maj_stat = p_QueryContextAttributesA(&winctx->context,
276 if (winctx->maj_stat != SEC_E_OK ||
277 ContextSizes.cbMaxSignature == 0)
278 return winctx->maj_stat;
280 InputBufferDescriptor.cBuffers = 2;
281 InputBufferDescriptor.pBuffers = InputSecurityToken;
282 InputBufferDescriptor.ulVersion = SECBUFFER_VERSION;
283 InputSecurityToken[0].BufferType = SECBUFFER_DATA;
284 InputSecurityToken[0].cbBuffer = buf->len;
285 InputSecurityToken[0].pvBuffer = buf->data;
286 InputSecurityToken[1].BufferType = SECBUFFER_TOKEN;
287 InputSecurityToken[1].cbBuffer = ContextSizes.cbMaxSignature;
288 InputSecurityToken[1].pvBuffer = snewn(ContextSizes.cbMaxSignature, char);
290 winctx->maj_stat = p_MakeSignature(&winctx->context,
292 &InputBufferDescriptor,
295 if (winctx->maj_stat == SEC_E_OK) {
296 hash->len = InputSecurityToken[1].cbBuffer;
297 hash->data = InputSecurityToken[1].pvBuffer;
300 return winctx->maj_stat;
303 Ssh_gss_stat ssh_gss_free_mic(Ssh_gss_buf *hash)
311 /* Dummy function so this source file defines something if NO_GSSAPI
314 int ssh_gss_init(void)