xref: /freebsd/crypto/heimdal/doc/doxyout/krb5/html/krb5_introduction.html (revision 7ef62cebc2f965b0f640263e179276928885e33d)
1<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2<html><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
3<title>HeimdalKerberos5library: Introduction to the Kerberos 5 API</title>
4<link href="doxygen.css" rel="stylesheet" type="text/css">
5<link href="tabs.css" rel="stylesheet" type="text/css">
6</head><body>
7<p>
8<a href="http://www.h5l.org/"><img src="http://www.h5l.org/keyhole-heimdal.png" alt="keyhole logo"/></a>
9</p>
10<!-- end of header marker -->
11<!-- Generated by Doxygen 1.5.6 -->
12<div class="navigation" id="top">
13  <div class="tabs">
14    <ul>
15      <li><a href="index.html"><span>Main&nbsp;Page</span></a></li>
16      <li><a href="pages.html"><span>Related&nbsp;Pages</span></a></li>
17      <li><a href="modules.html"><span>Modules</span></a></li>
18      <li><a href="annotated.html"><span>Data&nbsp;Structures</span></a></li>
19    </ul>
20  </div>
21</div>
22<div class="contents">
23<h1><a class="anchor" name="krb5_introduction">Introduction to the Kerberos 5 API </a></h1><h2><a class="anchor" name="api_overview">
24Kerberos 5 API Overview</a></h2>
25All functions are documented in manual pages. This section tries to give an overview of the major components used in Kerberos library, and point to where to look for a specific function.<h3><a class="anchor" name="intro_krb5_context">
26Kerberos context</a></h3>
27A kerberos context (krb5_context) holds all per thread state. All global variables that are context specific are stored in this structure, including default encryption types, credential cache (for example, a ticket file), and default realms.<p>
28The internals of the structure should never be accessed directly, functions exist for extracting information.<p>
29See the manual page for <a class="el" href="group__krb5.html#gbd94206e186c58a093975424a4a567a8">krb5_init_context()</a> how to create a context and module <a class="el" href="group__krb5.html">Heimdal Kerberos 5 library</a> for more information about the functions.<h3><a class="anchor" name="intro_krb5_auth_context">
30Kerberos authentication context</a></h3>
31Kerberos authentication context (krb5_auth_context) holds all context related to an authenticated connection, in a similar way to the kerberos context that holds the context for the thread or process.<p>
32The krb5_auth_context is used by various functions that are directly related to authentication between the server/client. Example of data that this structure contains are various flags, addresses of client and server, port numbers, keyblocks (and subkeys), sequence numbers, replay cache, and checksum types.<h3><a class="anchor" name="intro_krb5_principal">
33Kerberos principal</a></h3>
34The Kerberos principal is the structure that identifies a user or service in Kerberos. The structure that holds the principal is the krb5_principal. There are function to extract the realm and elements of the principal, but most applications have no reason to inspect the content of the structure.<p>
35The are several ways to create a principal (with different degree of portability), and one way to free it.<p>
36See also the page <a class="el" href="krb5_principal_intro.html">The principal handing functions.</a> for more information and also module <a class="el" href="group__krb5__principal.html">Heimdal Kerberos 5 principal functions</a>.<h3><a class="anchor" name="intro_krb5_ccache">
37Credential cache</a></h3>
38A credential cache holds the tickets for a user. A given user can have several credential caches, one for each realm where the user have the initial tickets (the first krbtgt).<p>
39The credential cache data can be stored internally in different way, each of them for different proposes. File credential (FILE) caches and processes based (KCM) caches are for permanent storage. While memory caches (MEMORY) are local caches to the local process.<p>
40Caches are opened with <a class="el" href="group__krb5__ccache.html#ge8ab9d6f4af5710dab860f2806a7d13b">krb5_cc_resolve()</a> or created with <a class="el" href="group__krb5__ccache.html#g86c0f70d0c2b5de2d876edf4b693b5b9">krb5_cc_new_unique()</a>.<p>
41If the cache needs to be opened again (using <a class="el" href="group__krb5__ccache.html#ge8ab9d6f4af5710dab860f2806a7d13b">krb5_cc_resolve()</a>) <a class="el" href="group__krb5__ccache.html#gebc0dd2a77529c05fb49e27235da7017">krb5_cc_close()</a> will close the handle, but not the remove the cache. <a class="el" href="group__krb5__ccache.html#g3115bcccd71594374831caa9a07b1290">krb5_cc_destroy()</a> will zero out the cache, remove the cache so it can no longer be referenced.<p>
42See also <a class="el" href="krb5_ccache_intro.html">The credential cache functions</a> and <a class="el" href="group__krb5__ccache.html">Heimdal Kerberos 5 credential cache functions</a> .<h3><a class="anchor" name="intro_krb5_error_code">
43Kerberos errors</a></h3>
44Kerberos errors are based on the com_err library. All error codes are 32-bit signed numbers, the first 24 bits define what subsystem the error originates from, and last 8 bits are 255 error codes within the library. Each error code have fixed string associated with it. For example, the error-code -1765328383 have the symbolic name KRB5KDC_ERR_NAME_EXP, and associated error string ``Client's entry in database has expired''.<p>
45This is a great improvement compared to just getting one of the unix error-codes back. However, Heimdal have an extention to pass back customised errors messages. Instead of getting ``Key table entry not found'', the user might back ``failed to find host/host.example.com@EXAMLE.COM(kvno 3) in keytab /etc/krb5.keytab (des-cbc-crc)''. This improves the chance that the user find the cause of the error so you should use the customised error message whenever it's available.<p>
46See also module <a class="el" href="group__krb5__error.html">Heimdal Kerberos 5 error reporting functions</a> .<h3><a class="anchor" name="intro_krb5_keytab">
47Keytab management</a></h3>
48A keytab is a storage for locally stored keys. Heimdal includes keytab support for Kerberos 5 keytabs, Kerberos 4 srvtab, AFS-KeyFile's, and for storing keys in memory.<p>
49Keytabs are used for servers and long-running services.<p>
50See also <a class="el" href="krb5_keytab_intro.html">The keytab handing functions</a> and <a class="el" href="group__krb5__keytab.html">Heimdal Kerberos 5 keytab handling functions</a> .<h3><a class="anchor" name="intro_krb5_crypto">
51Kerberos crypto</a></h3>
52Heimdal includes a implementation of the Kerberos crypto framework, all crypto operations. To create a crypto context call <a class="el" href="group__krb5__crypto.html#gd7003a8a81cef633cc0a2cc07c93dd32">krb5_crypto_init()</a>.<p>
53See also module <a class="el" href="group__krb5__crypto.html">Heimdal Kerberos 5 cryptography functions</a> .<h2><a class="anchor" name="kerberos5_client">
54Walkthrough of a sample Kerberos 5 client</a></h2>
55This example contains parts of a sample TCP Kerberos 5 clients, if you want a real working client, please look in appl/test directory in the Heimdal distribution.<p>
56All Kerberos error-codes that are returned from kerberos functions in this program are passed to krb5_err, that will print a descriptive text of the error code and exit. Graphical programs can convert error-code to a human readable error-string with the krb5_get_error_message() function.<p>
57Note that you should not use any Kerberos function before <a class="el" href="group__krb5.html#gbd94206e186c58a093975424a4a567a8">krb5_init_context()</a> have completed successfully. That is the reason err() is used when <a class="el" href="group__krb5.html#gbd94206e186c58a093975424a4a567a8">krb5_init_context()</a> fails.<p>
58First the client needs to call krb5_init_context to initialise the Kerberos 5 library. This is only needed once per thread in the program. If the function returns a non-zero value it indicates that either the Kerberos implementation is failing or it's disabled on this host.<p>
59<div class="fragment"><pre class="fragment"><span class="preprocessor"> #include &lt;krb5.h&gt;</span>
60
61 <span class="keywordtype">int</span>
62 main(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span> **argv)
63 {
64         krb5_context context;
65
66         <span class="keywordflow">if</span> (<a class="code" href="group__krb5.html#gbd94206e186c58a093975424a4a567a8">krb5_init_context</a>(&amp;context))
67                 errx (1, <span class="stringliteral">"krb5_context"</span>);
68</pre></div><p>
69Now the client wants to connect to the host at the other end. The preferred way of doing this is using getaddrinfo (for operating system that have this function implemented), since getaddrinfo is neutral to the address type and can use any protocol that is available.<p>
70<div class="fragment"><pre class="fragment">         <span class="keyword">struct </span>addrinfo *ai, *a;
71         <span class="keyword">struct </span>addrinfo hints;
72         <span class="keywordtype">int</span> error;
73
74         memset (&amp;hints, 0, <span class="keyword">sizeof</span>(hints));
75         hints.ai_socktype = SOCK_STREAM;
76         hints.ai_protocol = IPPROTO_TCP;
77
78         error = getaddrinfo (hostname, <span class="stringliteral">"pop3"</span>, &amp;hints, &amp;ai);
79         <span class="keywordflow">if</span> (error)
80                 errx (1, <span class="stringliteral">"%s: %s"</span>, hostname, gai_strerror(error));
81
82         <span class="keywordflow">for</span> (a = ai; a != NULL; a = a-&gt;ai_next) {
83                 <span class="keywordtype">int</span> s;
84
85                 s = socket (a-&gt;ai_family, a-&gt;ai_socktype, a-&gt;ai_protocol);
86                 <span class="keywordflow">if</span> (s &lt; 0)
87                         <span class="keywordflow">continue</span>;
88                 <span class="keywordflow">if</span> (connect (s, a-&gt;ai_addr, a-&gt;ai_addrlen) &lt; 0) {
89                         warn (<span class="stringliteral">"connect(%s)"</span>, hostname);
90                             close (s);
91                             <span class="keywordflow">continue</span>;
92                 }
93                 freeaddrinfo (ai);
94                 ai = NULL;
95         }
96         <span class="keywordflow">if</span> (ai) {
97                     freeaddrinfo (ai);
98                     errx (<span class="stringliteral">"failed to contact %s"</span>, hostname);
99         }
100</pre></div><p>
101Before authenticating, an authentication context needs to be created. This context keeps all information for one (to be) authenticated connection (see krb5_auth_context).<p>
102<div class="fragment"><pre class="fragment">         status = krb5_auth_con_init (context, &amp;auth_context);
103         <span class="keywordflow">if</span> (status)
104                 krb5_err (context, 1, status, <span class="stringliteral">"krb5_auth_con_init"</span>);
105</pre></div><p>
106For setting the address in the authentication there is a help function krb5_auth_con_setaddrs_from_fd() that does everything that is needed when given a connected file descriptor to the socket.<p>
107<div class="fragment"><pre class="fragment">         status = krb5_auth_con_setaddrs_from_fd (context,
108                                                  auth_context,
109                                                  &amp;sock);
110         <span class="keywordflow">if</span> (status)
111                 krb5_err (context, 1, status,
112                           <span class="stringliteral">"krb5_auth_con_setaddrs_from_fd"</span>);
113</pre></div><p>
114The next step is to build a server principal for the service we want to connect to. (See also <a class="el" href="group__krb5__principal.html#g8be0f5000da6ee0d4bd5dcaf3cb01d08">krb5_sname_to_principal()</a>.)<p>
115<div class="fragment"><pre class="fragment">         status = <a class="code" href="group__krb5__principal.html#g8be0f5000da6ee0d4bd5dcaf3cb01d08">krb5_sname_to_principal</a> (context,
116                                           hostname,
117                                           service,
118                                           KRB5_NT_SRV_HST,
119                                           &amp;server);
120         <span class="keywordflow">if</span> (status)
121                 krb5_err (context, 1, status, <span class="stringliteral">"krb5_sname_to_principal"</span>);
122</pre></div><p>
123The client principal is not passed to krb5_sendauth() function, this causes the krb5_sendauth() function to try to figure it out itself.<p>
124The server program is using the function krb5_recvauth() to receive the Kerberos 5 authenticator.<p>
125In this case, mutual authentication will be tried. That means that the server will authenticate to the client. Using mutual authentication is good since it enables the user to verify that they are talking to the right server (a server that knows the key).<p>
126If you are using a non-blocking socket you will need to do all work of krb5_sendauth() yourself. Basically you need to send over the authenticator from krb5_mk_req() and, in case of mutual authentication, verifying the result from the server with krb5_rd_rep().<p>
127<div class="fragment"><pre class="fragment">         status = krb5_sendauth (context,
128                                 &amp;auth_context,
129                                 &amp;sock,
130                                 VERSION,
131                                 NULL,
132                                 server,
133                                 AP_OPTS_MUTUAL_REQUIRED,
134                                 NULL,
135                                 NULL,
136                                 NULL,
137                                 NULL,
138                                 NULL,
139                                 NULL);
140         <span class="keywordflow">if</span> (status)
141                 krb5_err (context, 1, status, <span class="stringliteral">"krb5_sendauth"</span>);
142</pre></div><p>
143Once authentication has been performed, it is time to send some data. First we create a krb5_data structure, then we sign it with krb5_mk_safe() using the auth_context that contains the session-key that was exchanged in the krb5_sendauth()/krb5_recvauth() authentication sequence.<p>
144<div class="fragment"><pre class="fragment">         data.data   = <span class="stringliteral">"hej"</span>;
145         data.length = 3;
146
147         <a class="code" href="group__krb5.html#ga059e96dde4e0b8c082eb6f3d570b7bc">krb5_data_zero</a> (&amp;packet);
148
149         status = krb5_mk_safe (context,
150                                auth_context,
151                                &amp;data,
152                                &amp;packet,
153                                NULL);
154         <span class="keywordflow">if</span> (status)
155                 krb5_err (context, 1, status, <span class="stringliteral">"krb5_mk_safe"</span>);
156</pre></div><p>
157And send it over the network.<p>
158<div class="fragment"><pre class="fragment">         len = packet.length;
159         net_len = htonl(len);
160
161         <span class="keywordflow">if</span> (krb5_net_write (context, &amp;sock, &amp;net_len, 4) != 4)
162                 err (1, <span class="stringliteral">"krb5_net_write"</span>);
163         <span class="keywordflow">if</span> (krb5_net_write (context, &amp;sock, packet.data, len) != len)
164                 err (1, <span class="stringliteral">"krb5_net_write"</span>);
165</pre></div><p>
166To send encrypted (and signed) data krb5_mk_priv() should be used instead. krb5_mk_priv() works the same way as krb5_mk_safe(), with the exception that it encrypts the data in addition to signing it.<p>
167<div class="fragment"><pre class="fragment">         data.data   = <span class="stringliteral">"hemligt"</span>;
168         data.length = 7;
169
170         <a class="code" href="group__krb5.html#gb4b80ac7a8bbab89fe947ae1c7828ea8">krb5_data_free</a> (&amp;packet);
171
172         status = krb5_mk_priv (context,
173                                auth_context,
174                                &amp;data,
175                                &amp;packet,
176                                NULL);
177         <span class="keywordflow">if</span> (status)
178                 krb5_err (context, 1, status, <span class="stringliteral">"krb5_mk_priv"</span>);
179</pre></div><p>
180And send it over the network.<p>
181<div class="fragment"><pre class="fragment">         len = packet.length;
182         net_len = htonl(len);
183
184         <span class="keywordflow">if</span> (krb5_net_write (context, &amp;sock, &amp;net_len, 4) != 4)
185                 err (1, <span class="stringliteral">"krb5_net_write"</span>);
186         <span class="keywordflow">if</span> (krb5_net_write (context, &amp;sock, packet.data, len) != len)
187                 err (1, <span class="stringliteral">"krb5_net_write"</span>);
188</pre></div><p>
189The server is using krb5_rd_safe() and krb5_rd_priv() to verify the signature and decrypt the packet.<h2><a class="anchor" name="intro_krb5_verify_user">
190Validating a password in an application</a></h2>
191See the manual page for krb5_verify_user().<h2><a class="anchor" name="mit_differences">
192API differences to MIT Kerberos</a></h2>
193This section is somewhat disorganised, but so far there is no overall structure to the differences, though some of the have their root in that Heimdal uses an ASN.1 compiler and MIT doesn't.<h3><a class="anchor" name="mit_krb5_principal">
194Principal and realms</a></h3>
195Heimdal stores the realm as a krb5_realm, that is a char *. MIT Kerberos uses a krb5_data to store a realm.<p>
196In Heimdal krb5_principal doesn't contain the component name_type; it's instead stored in component name.name_type. To get and set the nametype in Heimdal, use <a class="el" href="group__krb5__principal.html#gf4a599c42592ff7485753e80b8de67b7">krb5_principal_get_type()</a> and <a class="el" href="group__krb5__principal.html#g28b750b990452f02922bc74a6cac0313">krb5_principal_set_type()</a>.<p>
197For more information about principal and realms, see krb5_principal.<h3><a class="anchor" name="mit_krb5_error_code">
198Error messages</a></h3>
199To get the error string, Heimdal uses krb5_get_error_message(). This is to return custom error messages (like ``Can't find host/datan.example.com@CODE.COM in /etc/krb5.conf.'' instead of a ``Key table entry not found'' that error_message returns.<p>
200Heimdal uses a threadsafe(r) version of the com_err interface; the global com_err table isn't initialised. Then error_message returns quite a boring error string (just the error code itself). </div>
201<hr size="1"><address style="text-align: right;"><small>
202Generated on Wed Jan 11 14:07:47 2012 for HeimdalKerberos5library by&nbsp;<a href="http://www.doxygen.org/index.html"><img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.5.6</small></address>
203</body>
204</html>
205