---
name: "gss_init_sec_context"
section: "3"
platform: "linux"
package: "gss-man"
version: "1.0.4-4"
release: "Debian 13 (Trixie)"
platforms: ["linux"]
url: "https://new.carta.tech/gss_init_sec_context"
---

# gss\_init\_sec\_context(3)

API function

## NAME

gss\_init\_sec\_context - API function

## SYNOPSIS

**#include \<gss.h\>**

**OM\_uint32 gss\_init\_sec\_context(OM\_uint32 \*** *minor\_status***, const gss\_cred\_id\_t** *initiator\_cred\_handle***, gss\_ctx\_id\_t \*** *context\_handle***, const gss\_name\_t** *target\_name***, const gss\_OID** *mech\_type***, OM\_uint32** *req\_flags***, OM\_uint32** *time\_req***, const gss\_channel\_bindings\_t** *input\_chan\_bindings***, const gss\_buffer\_t** *input\_token***, gss\_OID \*** *actual\_mech\_type***, gss\_buffer\_t** *output\_token***, OM\_uint32 \*** *ret\_flags***, OM\_uint32 \*** *time\_rec***);**

## ARGUMENTS

- OM\_uint32 \* minor\_status

  (integer, modify) Mechanism specific status code.

- const gss\_cred\_id\_t initiator\_cred\_handle

  (gss\_cred\_id\_t, read, optional) Handle for\
     credentials claimed.  Supply GSS\_C\_NO\_CREDENTIAL to act as a\
     default initiator principal.  If no default initiator is defined,\
     the function will return GSS\_S\_NO\_CRED.

- gss\_ctx\_id\_t \* context\_handle

  (gss\_ctx\_id\_t, read/modify) Context handle for new\
     context.  Supply GSS\_C\_NO\_CONTEXT for first call; use value\
     returned by first call in continuation calls.  Resources\
     associated with this context-handle must be released by the\
     application after use with a call to **gss\_delete\_sec\_context()**.

- const gss\_name\_t target\_name

  (gss\_name\_t, read) Name of target.

- const gss\_OID mech\_type

  (OID, read, optional) Object ID of desired\
     mechanism. Supply GSS\_C\_NO\_OID to obtain an implementation\
     specific default.

- OM\_uint32 req\_flags

  (bit-mask, read) Contains various independent flags,\
     each of which requests that the context support a specific\
     service option.  Symbolic names are provided for each flag, and\
     the symbolic names corresponding to the required flags should be\
     logically-ORed together to form the bit-mask value.  See below\
     for the flags.

- OM\_uint32 time\_req

  (Integer, read, optional) Desired number of seconds for\
     which context should remain valid.  Supply 0 to request a default\
     validity period.

- const gss\_channel\_bindings\_t input\_chan\_bindings

  (channel bindings, read, optional)\
     Application-specified bindings.  Allows application to securely\
     bind channel identification information to the security context.\
     Specify GSS\_C\_NO\_CHANNEL\_BINDINGS if channel bindings are not\
     used.

- const gss\_buffer\_t input\_token

  (buffer, opaque, read, optional) Token received from\
     peer application.  Supply GSS\_C\_NO\_BUFFER, or a pointer to a\
     buffer containing the value GSS\_C\_EMPTY\_BUFFER on initial call.

- gss\_OID \* actual\_mech\_type

  (OID, modify, optional) Actual mechanism used.\
     The OID returned via this parameter will be a pointer to static\
     storage that should be treated as read-only; In particular the\
     application should not attempt to free it.  Specify NULL if not\
     required.

- gss\_buffer\_t output\_token

  (buffer, opaque, modify) Token to be sent to peer\
     application.  If the length field of the returned buffer is zero,\
     no token need be sent to the peer application.  Storage\
     associated with this buffer must be freed by the application\
     after use with a call to **gss\_release\_buffer()**.

- OM\_uint32 \* ret\_flags

  (bit-mask, modify, optional) Contains various\
     independent flags, each of which indicates that the context\
     supports a specific service option.  Specify NULL if not\
     required.  Symbolic names are provided for each flag, and the\
     symbolic names corresponding to the required flags should be\
     logically-ANDed with the ret\_flags value to test whether a given\
     option is supported by the context.  See below for the flags.

- OM\_uint32 \* time\_rec

  (Integer, modify, optional) Number of seconds for which\
     the context will remain valid. If the implementation does not\
     support context expiration, the value GSS\_C\_INDEFINITE will be\
     returned.  Specify NULL if not required.

## DESCRIPTION

Initiates the establishment of a security context between the application and a remote peer.  Initially, the input\_token parameter should be specified either as GSS\_C\_NO\_BUFFER, or as a pointer to a gss\_buffer\_desc object whose length field contains the value zero.  The routine may return a output\_token which should be transferred to the peer application, where the peer application will present it to gss\_accept\_sec\_context.  If no token need be sent, gss\_init\_sec\_context will indicate this by setting the length field of the output\_token argument to zero. To complete the context establishment, one or more reply tokens may be required from the peer application; if so, gss\_init\_sec\_context will return a status containing the supplementary information bit GSS\_S\_CONTINUE\_NEEDED. In this case, gss\_init\_sec\_context should be called again when the reply token is received from the peer application, passing the reply token to gss\_init\_sec\_context via the input\_token parameters.

Portable applications should be constructed to use the token length and return status to determine whether a token needs to be sent or waited for.  Thus a typical portable caller should always invoke gss\_init\_sec\_context within a loop:

\--------------------------------------------------- int context\_established = 0; gss\_ctx\_id\_t context\_hdl = GSS\_C\_NO\_CONTEXT;\
        ... input\_token-\>length = 0;

while (!context\_established) {\
   maj\_stat = gss\_init\_sec\_context(&min\_stat,\
                                   cred\_hdl,\
                                   &context\_hdl,\
                                   target\_name,\
                                   desired\_mech,\
                                   desired\_services,\
                                   desired\_time,\
                                   input\_bindings,\
                                   input\_token,\
                                   &actual\_mech,\
                                   output\_token,\
                                   &actual\_services,\
                                   &actual\_time);\
   if (GSS\_ERROR(maj\_stat)) {\
     report\_error(maj\_stat, min\_stat);\
   };

  if (output\_token-\>length != 0) {\
     send\_token\_to\_peer(output\_token);\
     gss\_release\_buffer(&min\_stat, output\_token)\
   };\
   if (GSS\_ERROR(maj\_stat)) {

    if (context\_hdl != GSS\_C\_NO\_CONTEXT)\
       gss\_delete\_sec\_context(&min\_stat,\
                              &context\_hdl,\
                              GSS\_C\_NO\_BUFFER);\
     break;\
   };

  if (maj\_stat & GSS\_S\_CONTINUE\_NEEDED) {\
     receive\_token\_from\_peer(input\_token);\
   } else {\
     context\_established = 1;\
   }; }; ---------------------------------------------------

Whenever the routine returns a major status that includes the value GSS\_S\_CONTINUE\_NEEDED, the context is not fully established and the following restrictions apply to the output parameters:

\- The value returned via the time\_rec parameter is undefined unless the accompanying ret\_flags parameter contains the bit GSS\_C\_PROT\_READY\_FLAG, indicating that per-message services may be applied in advance of a successful completion status, the value returned via the actual\_mech\_type parameter is undefined until the routine returns a major status value of GSS\_S\_COMPLETE.

\- The values of the GSS\_C\_DELEG\_FLAG, GSS\_C\_MUTUAL\_FLAG, GSS\_C\_REPLAY\_FLAG, GSS\_C\_SEQUENCE\_FLAG, GSS\_C\_CONF\_FLAG, GSS\_C\_INTEG\_FLAG and GSS\_C\_ANON\_FLAG bits returned via the ret\_flags parameter should contain the values that the implementation expects would be valid if context establishment were to succeed.  In particular, if the application has requested a service such as delegation or anonymous authentication via the req\_flags argument, and such a service is unavailable from the underlying mechanism, gss\_init\_sec\_context should generate a token that will not provide the service, and indicate via the ret\_flags argument that the service will not be supported.  The application may choose to abort the context establishment by calling gss\_delete\_sec\_context (if it cannot continue in the absence of the service), or it may choose to transmit the token and continue context establishment (if the service was merely desired but not mandatory).

\- The values of the GSS\_C\_PROT\_READY\_FLAG and GSS\_C\_TRANS\_FLAG bits within ret\_flags should indicate the actual state at the time gss\_init\_sec\_context returns, whether or not the context is fully established.

\- GSS-API implementations that support per-message protection are encouraged to set the GSS\_C\_PROT\_READY\_FLAG in the final ret\_flags returned to a caller (i.e. when accompanied by a GSS\_S\_COMPLETE status code).  However, applications should not rely on this behavior as the flag was not defined in Version 1 of the GSS-API. Instead, applications should determine what per-message services are available after a successful context establishment according to the GSS\_C\_INTEG\_FLAG and GSS\_C\_CONF\_FLAG values.

\- All other bits within the ret\_flags argument should be set to zero.

If the initial call of **gss\_init\_sec\_context()** fails, the implementation should not create a context object, and should leave the value of the context\_handle parameter set to GSS\_C\_NO\_CONTEXT to indicate this.  In the event of a failure on a subsequent call, the implementation is permitted to delete the "half-built" security context (in which case it should set the context\_handle parameter to GSS\_C\_NO\_CONTEXT), but the preferred behavior is to leave the security context untouched for the application to delete (using gss\_delete\_sec\_context).

During context establishment, the informational status bits GSS\_S\_OLD\_TOKEN and GSS\_S\_DUPLICATE\_TOKEN indicate fatal errors, and GSS-API mechanisms should always return them in association with a routine error of GSS\_S\_FAILURE.  This requirement for pairing did not exist in version 1 of the GSS-API specification, so applications that wish to run over version 1 implementations must special-case these codes.

The \`req\_flags\` values:

\`GSS\_C\_DELEG\_FLAG\`:: - True - Delegate credentials to remote peer. - False - Don't delegate.

\`GSS\_C\_MUTUAL\_FLAG\`:: - True - Request that remote peer authenticate itself. - False - Authenticate self to remote peer only.

\`GSS\_C\_REPLAY\_FLAG\`:: - True - Enable replay detection for messages protected with gss\_wrap or gss\_get\_mic. - False - Don't attempt to detect replayed messages.

\`GSS\_C\_SEQUENCE\_FLAG\`:: - True - Enable detection of out-of-sequence protected messages. - False - Don't attempt to detect out-of-sequence messages.

\`GSS\_C\_CONF\_FLAG\`:: - True - Request that confidentiality service be made available (via gss\_wrap). - False - No per-message confidentiality service is required.

\`GSS\_C\_INTEG\_FLAG\`:: - True - Request that integrity service be made available (via gss\_wrap or gss\_get\_mic). - False - No per-message integrity service is required.

\`GSS\_C\_ANON\_FLAG\`:: - True - Do not reveal the initiator's identity to the acceptor. - False - Authenticate normally.

The \`ret\_flags\` values:

\`GSS\_C\_DELEG\_FLAG\`:: - True - Credentials were delegated to the remote peer. - False - No credentials were delegated.

\`GSS\_C\_MUTUAL\_FLAG\`:: - True - The remote peer has authenticated itself. - False - Remote peer has not authenticated itself.

\`GSS\_C\_REPLAY\_FLAG\`:: - True - replay of protected messages will be detected. - False - replayed messages will not be detected.

\`GSS\_C\_SEQUENCE\_FLAG\`:: - True - out-of-sequence protected messages will be detected. - False - out-of-sequence messages will not be detected.

\`GSS\_C\_CONF\_FLAG\`:: - True - Confidentiality service may be invoked by calling gss\_wrap routine. - False - No confidentiality service (via gss\_wrap) available. gss\_wrap will provide message encapsulation, data-origin authentication and integrity services only.

\`GSS\_C\_INTEG\_FLAG\`:: - True - Integrity service may be invoked by calling either gss\_get\_mic or gss\_wrap routines. - False - Per-message integrity service unavailable.

\`GSS\_C\_ANON\_FLAG\`:: - True - The initiator's identity has not been revealed, and will not be revealed if any emitted token is passed to the acceptor. - False - The initiator's identity has been or will be authenticated normally.

\`GSS\_C\_PROT\_READY\_FLAG\`:: - True - Protection services (as specified by the states of the GSS\_C\_CONF\_FLAG and GSS\_C\_INTEG\_FLAG) are available for use if the accompanying major status return value is either GSS\_S\_COMPLETE or GSS\_S\_CONTINUE\_NEEDED. - False - Protection services (as specified by the states of the GSS\_C\_CONF\_FLAG and GSS\_C\_INTEG\_FLAG) are available only if the accompanying major status return value is GSS\_S\_COMPLETE.

\`GSS\_C\_TRANS\_FLAG\`:: - True - The resultant security context may be transferred to other processes via a call to **gss\_export\_sec\_context()**. - False - The security context is not transferable.

All other bits should be set to zero.

## RETURN VALUE

\`GSS\_S\_COMPLETE\`: Successful completion.

\`GSS\_S\_CONTINUE\_NEEDED\`: Indicates that a token from the peer application is required to complete the context, and that gss\_init\_sec\_context must be called again with that token.

\`GSS\_S\_DEFECTIVE\_TOKEN\`: Indicates that consistency checks performed on the input\_token failed.

\`GSS\_S\_DEFECTIVE\_CREDENTIAL\`: Indicates that consistency checks performed on the credential failed.

\`GSS\_S\_NO\_CRED\`: The supplied credentials were not valid for context initiation, or the credential handle did not reference any credentials.

\`GSS\_S\_CREDENTIALS\_EXPIRED\`: The referenced credentials have expired.

\`GSS\_S\_BAD\_BINDINGS\`: The input\_token contains different channel bindings to those specified via the input\_chan\_bindings parameter.

\`GSS\_S\_BAD\_SIG\`: The input\_token contains an invalid MIC, or a MIC that could not be verified.

\`GSS\_S\_OLD\_TOKEN\`: The input\_token was too old.  This is a fatal error during context establishment.

\`GSS\_S\_DUPLICATE\_TOKEN\`: The input\_token is valid, but is a duplicate of a token already processed.  This is a fatal error during context establishment.

\`GSS\_S\_NO\_CONTEXT\`: Indicates that the supplied context handle did not refer to a valid context.

\`GSS\_S\_BAD\_NAMETYPE\`: The provided target\_name parameter contained an invalid or unsupported type of name.

\`GSS\_S\_BAD\_NAME\`: The provided target\_name parameter was ill-formed.

\`GSS\_S\_BAD\_MECH\`: The specified mechanism is not supported by the provided credential, or is unrecognized by the implementation.

## REPORTING BUGS

Report bugs to \<bug-gss@gnu.org\>. GNU Generic Security Service home page: http://www.gnu.org/software/gss/ General help using GNU software: http://www.gnu.org/gethelp/

## COPYRIGHT

Copyright © 2003-2022 Simon Josefsson.\
 Copying and distribution of this file, with or without modification, are permitted in any medium without royalty provided the copyright notice and this notice are preserved.

## SEE ALSO

The full documentation for **gss** is maintained as a Texinfo manual.  If the **info** and **gss** programs are properly installed at your site, the command

-

  **info gss**

should give you access to the complete manual.
