title: Obtain Kerberos ticket in C/C++
body: <p><strong>Does anyone know how to get a ticket from the Key Distribution Center (KDC) using the MIT krb5 API in C/C++?</strong></p>
  <p>I already have a working Java Client which uses GSS-API to obtain a ticket from the KDC (using a local TGT) and forwards it to a Java Server.</p>
  <p>The server accepts the security context using the following logic:</p>
  <pre><code>private GSSContext acceptSecurityContext(Subject serverSubject, final byte[] kerberosServiceTicket) {
   return Subject.doAs(serverSubject, (PrivilegedAction&lt;GSSContext&gt;) () -&gt; {
              GSSContext gssContext;
              try {
                  gssContext = manager.createContext((GSSCredential) null);
              } catch (GSSException ex) {
                  LOGGER.warn(&quot;Could not create Kerberos gssContext: &quot; + ex.getMessage(), ex);
                  return null;
              }
              try {
                  gssContext.acceptSecContext(kerberosServiceTicket, 0, kerberosServiceTicket.length);
              } catch (GSSException ex) {
                  LOGGER.warn(&quot;Could not accept security context: &quot; + ex.getMessage(), ex);
                  return null;
              }
              return gssContext;
          });
  }
  </code></pre>
  <p>I am trying to implement a C client - similar to the Java one - using  MIT krb5 API and I can't seem to make it work. So far this is my C client code:</p>
  <pre><code>    krb5_context context;
      krb5_ccache ccache;
      krb5_creds *outCreds = NULL;
      krb5_creds inCreds;
      int retval;
      char *principal = &quot;...&quot;;
  
      retval = krb5_init_secure_context(&amp;context);
      ...
  
      retval = krb5_cc_default(context, &amp;ccache);
      ...
  
      memset(&amp;inCreds, 0, sizeof(inCreds));
      retval = krb5_parse_name(context, principal, &amp;inCreds.server);
      ...
  
      retval = krb5_cc_get_principal(context, ccache, &amp;inCreds.client);
      ...
  
      retval = krb5_get_credentials(context, 0, ccache, &amp;inCreds, &amp;outCreds);
      ...
  
      // also tried using the following: krb5Ticket-&gt;enc_part.ciphertext.data
      // (maybe this is the correct way, but I should somehow decrypt it and use krb5Ticket-&gt;enc_part2 ?)
      // retval = krb5_decode_ticket(&amp;outCreds-&gt;ticket, &amp;krb5Ticket);
      // ...
  
      char *base64KerberosTicket = base64_encode(outCreds-&gt;ticket.data, strlen(outCreds-&gt;ticket.data));
  
      char *response = loginKerberos(base64KerberosTicket);
      ...
  </code></pre>