InitializeSecurityContext (Digest) function
The InitializeSecurityContext (Digest) function initiates the client side, outbound security context from a credential handle. The function is used to build a security context between the client application and a remote peer. InitializeSecurityContext (Digest) returns a token that the client must pass to the remote peer, which the peer in turn submits to the local security implementation through the AcceptSecurityContext (Digest) call. The token generated should be considered opaque by all callers.
Typically, the InitializeSecurityContext (Digest) function is called in a loop until a sufficient security context is established.
SECURITY_STATUS SEC_Entry InitializeSecurityContext(
_In_opt_ PCredHandle phCredential,
_In_opt_ PCtxtHandle phContext,
_In_opt_ SEC_CHAR *pszTargetName,
_In_ ULONG fContextReq,
_In_ ULONG Reserved1,
_In_ ULONG TargetDataRep,
_In_opt_ PSecBufferDesc pInput,
_In_ ULONG Reserved2,
_Inout_opt_ PCtxtHandle phNewContext,
_Inout_opt_ PSecBufferDesc pOutput,
_Out_ PULONG pfContextAttr,
_Out_opt_ PTimeStamp ptsExpiry
A handle to the credentials returned by AcquireCredentialsHandle (Digest). This handle is used to build the security context. The InitializeSecurityContext (Digest) function requires at least OUTBOUND credentials.
A pointer to a CtxtHandle structure. On the first call to InitializeSecurityContext (Digest), this pointer is
NULL. On the second call, this parameter is a pointer to the handle to the partially formed context returned in the phNewContext parameter by the first call.
Do not use the same context handle in concurrent calls to InitializeSecurityContext (Digest). The API implementation in the security service providers is not thread-safe.
A pointer to a null-terminated string that uniquely identifies the URI of the requested resource. The string must be composed of characters that are allowed in a URI and must be representable by the US ASCII code set. Percent encoding can be used to represent characters outside the US ASCII code set.
Bit flags that indicate requests for the context. Not all packages can support all requirements. Flags used for this parameter are prefixed with ISC_REQ_, for example, ISC_REQ_DELEGATE. This parameter can be one or more of the following attributes flags.
|The security package allocates output buffers for you. When you have finished using the output buffers, free them by calling the FreeContextBuffer function.
|Encrypt messages by using the EncryptMessage function.
|When errors occur, the remote party will be notified.
|Use Digest for HTTP. Omit this flag to use Digest as a SASL mechanism.
|Sign messages and verify signatures by using the EncryptMessage and MakeSignature functions.
|The mutual authentication policy of the service will be satisfied.
CAUTION: This does not necessarily mean that mutual authentication is performed, only that the authentication policy of the service is satisfied. To ensure that mutual authentication is performed, call the QueryContextAttributes (Digest) function.
|Detect replayed messages that have been encoded by using the EncryptMessage or MakeSignature functions.
|Detect messages received out of sequence.
|Support a stream-oriented connection.
The requested attributes may not be supported by the client. For more information, see the pfContextAttr parameter.
For further descriptions of the various attributes, see Context Requirements.
This parameter is reserved and must be set to zero.
This parameter is not used with Digest. Set it to zero.
A pointer to a SecBufferDesc structure that contains pointers to the buffers supplied as input to the package. Unless the client context was initiated by the server, the value of this parameter must be
NULL on the first call to the function. On subsequent calls to the function or when the client context was initiated by the server, the value of this parameter is a pointer to a buffer allocated with enough memory to hold the token returned by the remote computer.
For information about buffer configuration, see Input Buffers for the Digest Challenge Response.
This parameter is reserved and must be set to zero.
[in, out, optional]
A pointer to a CtxtHandle structure. On the first call to InitializeSecurityContext (Digest), this pointer receives the new context handle. On the second call, phNewContext can be the same as the handle specified in the phContext parameter. phNewContext should never be
[in, out, optional]
A pointer to a SecBufferDesc structure that contains pointers to the SecBuffer structure that receives the output data. If a buffer was typed as SEC_READWRITE in the input, it will be there on output. The system will allocate a buffer for the security token if requested (through ISC_REQ_ALLOCATE_MEMORY) and fill in the address in the buffer descriptor for the security token.
This parameter receives the challenge response that must be sent to the server.
Flags used for this parameter are prefixed with ISC_RET, such as ISC_RET_DELEGATE. For a list of valid values, see the fContextReq parameter.
Do not check for security-related attributes until the final function call returns successfully. Attribute flags that are not related to security, such as the ASC_RET_ALLOCATED_MEMORY flag, can be checked before the final return.
Particular context attributes can change during negotiation with a remote peer.
A pointer to a TimeStamp structure that receives the expiration time of the context.
There is no expiration time for Microsoft Digest SSP security contexts or credentials.
If the function succeeds, the function returns one of the following success codes.
|The security context was successfully initialized. There is no need for another InitializeSecurityContext (Digest) call. If the function returns an output token, that is, if the SECBUFFER_TOKEN in pOutput is of nonzero length, that token must be sent to the server.
|The client must call CompleteAuthToken and then pass the output to the server. The client then waits for a returned token and passes it, in another call, to InitializeSecurityContext (Digest).
|The client must finish building the message and then call the CompleteAuthToken function.
|The client must send the output token to the server and wait for a return token. The returned token is then passed in another call to InitializeSecurityContext (Digest). The output token can be empty.
|Use with Schannel. The server has requested client authentication, and the supplied credentials either do not include a certificate or the certificate was not issued by a certification authority (CA) trusted by the server. For more information, see Remarks.
If the function fails, the function returns one of the following error codes.
|There is not enough memory available to complete the requested action.
|An error occurred that did not map to an SSPI error code.
|The handle passed to the function is not valid.
|The error is due to a malformed input token, such as a token corrupted in transit, a token of incorrect size, or a token passed into the wrong constrained delegation. Passing a token to the wrong package can happen if the client and server did not negotiate the proper constrained delegation.
|The logon failed.
|No authority could be contacted for authentication. The domain name of the authenticating party could be wrong, the domain could be unreachable, or there might have been a trust relationship failure.
|No credentials are available in the constrained delegation.
|The target was not recognized.
|A context attribute flag that is not valid (ISC_REQ_DELEGATE or ISC_REQ_PROMPT_FOR_CREDS) was specified in the fContextReq parameter.
|The principal that received the authentication request is not the same as the one passed into the pszTargetName parameter. This indicates a failure in mutual authentication.
The caller is responsible for determining whether the final context attributes are sufficient. If, for example, confidentiality was requested, but could not be established, some applications may choose to shut down the connection immediately.
The InitializeSecurityContext (Digest) function is used by a client to initialize an outbound context.
For a two-leg security context, the calling sequence is as follows:
- The client calls the function with phContext set to
NULLand fills in the buffer descriptor with the input message.
- The security package examines the parameters and constructs an opaque token, placing it in the TOKEN element in the buffer array. If the fContextReq parameter includes the ISC_REQ_ALLOCATE_MEMORY flag, the security package allocates the memory and returns the pointer in the TOKEN element.
- The client sends the token returned in the pOutput buffer to the target server. The server then passes the token as an input argument in a call to the AcceptSecurityContext (Digest) function.
- AcceptSecurityContext (Digest) may return a token, which the server sends to the client for a second call to InitializeSecurityContext (Digest) if the first call returned SEC_I_CONTINUE_NEEDED.
For multiple-leg security contexts, such as mutual authentication, the calling sequence is as follows:
- The client calls the function as described earlier, but the package returns the SEC_I_CONTINUE_NEEDED success code.
- The client sends the output token to the server and waits for the server's reply.
- Upon receipt of the server's response, the client calls InitializeSecurityContext (Digest) again, with phContext set to the handle that was returned from the last call. The token received from the server is supplied in the pInput parameter.
- Do not use the phContext value in concurrent calls to InitializeSecurityContext (Digest). The implementation in the security providers is not thread-safe.
If the server has successfully responded, the security package returns SEC_E_OK and a secure session is established.
If the function returns one of the error responses, the server's response is not accepted, and the session is not established.
If the function returns SEC_I_CONTINUE_NEEDED, SEC_I_COMPLETE_NEEDED, or SEC_I_COMPLETE_AND_CONTINUE, steps 2 and 3 are repeated.
To initialize a security context, more than one call to this function may be required, depending on the underlying authentication mechanism as well as the choices specified in the fContextReq parameter.
The fContextReq and pfContextAttributes parameters are bitmasks that represent various context attributes. For a description of the various attributes, see Context Requirements. The pfContextAttributes parameter is valid on any successful return, but only on the final successful return should you examine the flags that pertain to security aspects of the context. Intermediate returns can set, for example, the ISC_RET_ALLOCATED_MEMORY flag.
If the ISC_REQ_USE_SUPPLIED_CREDS flag is set, the security package must look for a SECBUFFER_PKG_PARAMS buffer type in the pInput input buffer. This is not a generic solution, but it allows for a strong pairing of security package and application when appropriate.
If ISC_REQ_ALLOCATE_MEMORY was specified, the caller must free the memory by calling the FreeContextBuffer function.
For example, the input token could be the challenge from a LAN Manager. In this case, the output token would be the NTLM-encrypted response to the challenge.
The action the client takes depends on the return code from this function. If the return code is SEC_E_OK, there will be no second InitializeSecurityContext (Digest) call, and no response from the server is expected. If the return code is SEC_I_CONTINUE_NEEDED, the client expects a token in response from the server and passes it in a second call to InitializeSecurityContext (Digest). The SEC_I_COMPLETE_NEEDED return code indicates that the client must finish building the message and call the CompleteAuthToken function. The SEC_I_COMPLETE_AND_CONTINUE code incorporates both of these actions.
If InitializeSecurityContext (Digest) returns success on the first (or only) call, then the caller must eventually call the DeleteSecurityContext function on the returned handle, even if the call fails on a later leg of the authentication exchange.
The client may call InitializeSecurityContext (Digest) again after it has completed successfully. This indicates to the security package that a reauthentication is wanted.
Kernel mode callers have the following differences: the target name is a Unicode string that must be allocated in virtual memory by using VirtualAlloc; it must not be allocated from the pool. Buffers passed and supplied in pInput and pOutput must be in virtual memory, not in the pool.
|Minimum supported client
|Windows XP [desktop apps only]
|Minimum supported server
|Windows Server 2003 [desktop apps only]
|Sspi.h (include Security.h)