Stop using SALT_TYPE_AFS_LENGTH
[krb5.git] / src / include / k5-int.h
1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /*
3  * Copyright (C) 1989,1990,1991,1992,1993,1994,1995,2000,2001,
4  * 2003,2006,2007,2008,2009 by the Massachusetts Institute of Technology,
5  * Cambridge, MA, USA.  All Rights Reserved.
6  *
7  * This software is being provided to you, the LICENSEE, by the
8  * Massachusetts Institute of Technology (M.I.T.) under the following
9  * license.  By obtaining, using and/or copying this software, you agree
10  * that you have read, understood, and will comply with these terms and
11  * conditions:
12  *
13  * Export of this software from the United States of America may
14  * require a specific license from the United States Government.
15  * It is the responsibility of any person or organization contemplating
16  * export to obtain such a license before exporting.
17  *
18  * WITHIN THAT CONSTRAINT, permission to use, copy, modify and distribute
19  * this software and its documentation for any purpose and without fee or
20  * royalty is hereby granted, provided that you agree to comply with the
21  * following copyright notice and statements, including the disclaimer, and
22  * that the same appear on ALL copies of the software and documentation,
23  * including modifications that you make for internal use or for
24  * distribution:
25  *
26  * THIS SOFTWARE IS PROVIDED "AS IS", AND M.I.T. MAKES NO REPRESENTATIONS
27  * OR WARRANTIES, EXPRESS OR IMPLIED.  By way of example, but not
28  * limitation, M.I.T. MAKES NO REPRESENTATIONS OR WARRANTIES OF
29  * MERCHANTABILITY OR FITNESS FOR ANY PARTICULAR PURPOSE OR THAT THE USE OF
30  * THE LICENSED SOFTWARE OR DOCUMENTATION WILL NOT INFRINGE ANY THIRD PARTY
31  * PATENTS, COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS.
32  *
33  * The name of the Massachusetts Institute of Technology or M.I.T. may NOT
34  * be used in advertising or publicity pertaining to distribution of the
35  * software.  Title to copyright in this software and any associated
36  * documentation shall at all times remain with M.I.T., and USER agrees to
37  * preserve same.
38  *
39  * Furthermore if you modify this software you must label
40  * your software as modified software and not distribute it in such a
41  * fashion that it might be confused with the original M.I.T. software.
42  */
43 /*
44  * Copyright (C) 1998 by the FundsXpress, INC.
45  *
46  * All rights reserved.
47  *
48  * Export of this software from the United States of America may require
49  * a specific license from the United States Government.  It is the
50  * responsibility of any person or organization contemplating export to
51  * obtain such a license before exporting.
52  *
53  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
54  * distribute this software and its documentation for any purpose and
55  * without fee is hereby granted, provided that the above copyright
56  * notice appear in all copies and that both that copyright notice and
57  * this permission notice appear in supporting documentation, and that
58  * the name of FundsXpress. not be used in advertising or publicity pertaining
59  * to distribution of the software without specific, written prior
60  * permission.  FundsXpress makes no representations about the suitability of
61  * this software for any purpose.  It is provided "as is" without express
62  * or implied warranty.
63  *
64  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
65  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
66  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
67  */
68
69 /*
70  * This prototype for k5-int.h (Krb5 internals include file)
71  * includes the user-visible definitions from krb5.h and then
72  * includes other definitions that are not user-visible but are
73  * required for compiling Kerberos internal routines.
74  *
75  * John Gilmore, Cygnus Support, Sat Jan 21 22:45:52 PST 1995
76  */
77
78 #ifndef _KRB5_INT_H
79 #define _KRB5_INT_H
80
81 #ifdef KRB5_GENERAL__
82 #error krb5.h included before k5-int.h
83 #endif /* KRB5_GENERAL__ */
84
85 #include "osconf.h"
86
87 #if defined(__MACH__) && defined(__APPLE__)
88 #       include <TargetConditionals.h>
89 #    if TARGET_RT_MAC_CFM
90 #       error "Use KfM 4.0 SDK headers for CFM compilation."
91 #    endif
92 #endif
93
94 /*
95  * Begin "k5-config.h"
96  */
97 #ifndef KRB5_CONFIG__
98 #define KRB5_CONFIG__
99
100 /*
101  * Machine-type definitions: PC Clone 386 running Microloss Windows
102  */
103
104 #if defined(_MSDOS) || defined(_WIN32)
105 #include "win-mac.h"
106
107 /* Kerberos Windows initialization file */
108 #define KERBEROS_INI    "kerberos.ini"
109 #define INI_FILES       "Files"
110 #define INI_KRB_CCACHE  "krb5cc"        /* Location of the ccache */
111 #define INI_KRB5_CONF   "krb5.ini"      /* Location of krb5.conf file */
112 #define ANSI_STDIO
113 #define DISABLE_TRACING
114 #endif
115
116 #include "autoconf.h"
117
118 #ifndef KRB5_SYSTYPES__
119 #define KRB5_SYSTYPES__
120
121 #ifdef HAVE_SYS_TYPES_H         /* From autoconf.h */
122 #include <sys/types.h>
123 #else /* HAVE_SYS_TYPES_H */
124 typedef unsigned long   u_long;
125 typedef unsigned int    u_int;
126 typedef unsigned short  u_short;
127 typedef unsigned char   u_char;
128 #endif /* HAVE_SYS_TYPES_H */
129 #endif /* KRB5_SYSTYPES__ */
130
131
132 #include "k5-platform.h"
133 #include "k5-trace.h"
134 /* not used in krb5.h (yet) */
135 typedef UINT64_TYPE krb5_ui_8;
136 typedef INT64_TYPE krb5_int64;
137
138
139 #define DEFAULT_PWD_STRING1 "Enter password"
140 #define DEFAULT_PWD_STRING2 "Re-enter password for verification"
141
142 #define KRB5_KDB_MAX_LIFE       (60*60*24) /* one day */
143 #define KRB5_KDB_MAX_RLIFE      (60*60*24*7) /* one week */
144 #define KRB5_KDB_EXPIRATION     2145830400 /* Thu Jan  1 00:00:00 2038 UTC */
145
146 /*
147  * Windows requires a different api interface to each function. Here
148  * just define it as NULL.
149  */
150 #ifndef KRB5_CALLCONV
151 #define KRB5_CALLCONV
152 #define KRB5_CALLCONV_C
153 #endif
154 #ifndef O_BINARY
155 #define O_BINARY 0
156 #endif
157
158 /* #define KRB5_OLD_CRYPTO is done in krb5.h */
159
160 #endif /* KRB5_CONFIG__ */
161
162 /*
163  * End "k5-config.h"
164  */
165
166 /*
167  * After loading the configuration definitions, load the Kerberos definitions.
168  */
169 #include <errno.h>
170 #include "krb5.h"
171 #include <krb5/plugin.h>
172 #include "profile.h"
173
174 #include "port-sockets.h"
175 #include "socket-utils.h"
176
177 /* Get mutex support; currently used only for the replay cache.  */
178 #include "k5-thread.h"
179
180 /* Get error info support.  */
181 #include "k5-err.h"
182
183 /* Get string buffer support. */
184 #include "k5-buf.h"
185
186 /* cofiguration variables */
187 #define KRB5_CONF_ACL_FILE                       "acl_file"
188 #define KRB5_CONF_ADMIN_SERVER                   "admin_server"
189 #define KRB5_CONF_ALLOW_WEAK_CRYPTO              "allow_weak_crypto"
190 #define KRB5_CONF_AP_REQ_CHECKSUM_TYPE           "ap_req_checksum_type"
191 #define KRB5_CONF_AUTH_TO_LOCAL                  "auth_to_local"
192 #define KRB5_CONF_AUTH_TO_LOCAL_NAMES            "auth_to_local_names"
193 #define KRB5_CONF_CANONICALIZE                   "canonicalize"
194 #define KRB5_CONF_CCACHE_TYPE                    "ccache_type"
195 #define KRB5_CONF_CLOCKSKEW                      "clockskew"
196 #define KRB5_CONF_DATABASE_NAME                  "database_name"
197 #define KRB5_CONF_DB_MODULE_DIR                  "db_module_dir"
198 #define KRB5_CONF_DEFAULT                        "default"
199 #define KRB5_CONF_DEFAULT_REALM                  "default_realm"
200 #define KRB5_CONF_DEFAULT_DOMAIN                 "default_domain"
201 #define KRB5_CONF_DEFAULT_TKT_ENCTYPES           "default_tkt_enctypes"
202 #define KRB5_CONF_DEFAULT_TGS_ENCTYPES           "default_tgs_enctypes"
203 #define KRB5_CONF_DEFAULT_KEYTAB_NAME            "default_keytab_name"
204 #define KRB5_CONF_DEFAULT_PRINCIPAL_EXPIRATION   "default_principal_expiration"
205 #define KRB5_CONF_DEFAULT_PRINCIPAL_FLAGS        "default_principal_flags"
206 #define KRB5_CONF_DICT_FILE                   "dict_file"
207 #define KRB5_CONF_DISABLE                     "disable"
208 #define KRB5_CONF_DISABLE_LAST_SUCCESS        "disable_last_success"
209 #define KRB5_CONF_DISABLE_LOCKOUT             "disable_lockout"
210 #define KRB5_CONF_DNS_LOOKUP_KDC              "dns_lookup_kdc"
211 #define KRB5_CONF_DNS_LOOKUP_REALM            "dns_lookup_realm"
212 #define KRB5_CONF_DNS_FALLBACK                "dns_fallback"
213 #define KRB5_CONF_DOMAIN_REALM                "domain_realm"
214 #define KRB5_CONF_ENABLE_ONLY                 "enable_only"
215 #define KRB5_CONF_EXTRA_ADDRESSES             "extra_addresses"
216 #define KRB5_CONF_FORWARDABLE                 "forwardable"
217 #define KRB5_CONF_HOST_BASED_SERVICES         "host_based_services"
218 #define KRB5_CONF_IGNORE_ACCEPTOR_HOSTNAME    "ignore_acceptor_hostname"
219 #define KRB5_CONF_IPROP_ENABLE                "iprop_enable"
220 #define KRB5_CONF_IPROP_MASTER_ULOGSIZE       "iprop_master_ulogsize"
221 #define KRB5_CONF_IPROP_PORT                  "iprop_port"
222 #define KRB5_CONF_IPROP_SLAVE_POLL            "iprop_slave_poll"
223 #define KRB5_CONF_IPROP_LOGFILE               "iprop_logfile"
224 #define KRB5_CONF_K5LOGIN_AUTHORITATIVE       "k5login_authoritative"
225 #define KRB5_CONF_K5LOGIN_DIRECTORY           "k5login_directory"
226 #define KRB5_CONF_KADMIND_PORT                "kadmind_port"
227 #define KRB5_CONF_KRB524_SERVER               "krb524_server"
228 #define KRB5_CONF_KDC                         "kdc"
229 #define KRB5_CONF_KDCDEFAULTS                 "kdcdefaults"
230 #define KRB5_CONF_KDC_PORTS                   "kdc_ports"
231 #define KRB5_CONF_KDC_TCP_PORTS               "kdc_tcp_ports"
232 #define KRB5_CONF_MAX_DGRAM_REPLY_SIZE        "kdc_max_dgram_reply_size"
233 #define KRB5_CONF_KDC_DEFAULT_OPTIONS         "kdc_default_options"
234 #define KRB5_CONF_KDC_TIMESYNC                "kdc_timesync"
235 #define KRB5_CONF_KDC_REQ_CHECKSUM_TYPE       "kdc_req_checksum_type"
236 #define KRB5_CONF_KEY_STASH_FILE              "key_stash_file"
237 #define KRB5_CONF_KPASSWD_PORT                "kpasswd_port"
238 #define KRB5_CONF_KPASSWD_SERVER              "kpasswd_server"
239 #define KRB5_CONF_LDAP_CONNS_PER_SERVER       "ldap_conns_per_server"
240 #define KRB5_CONF_LDAP_KADMIN_DN              "ldap_kadmind_dn"
241 #define KRB5_CONF_LDAP_KDC_DN                 "ldap_kdc_dn"
242 #define KRB5_CONF_LDAP_KERBEROS_CONTAINER_DN  "ldap_kerberos_container_dn"
243 #define KRB5_CONF_LDAP_KPASSWDD_DN            "ldap_kpasswdd_dn"
244 #define KRB5_CONF_LDAP_SERVERS                "ldap_servers"
245 #define KRB5_CONF_LDAP_SERVICE_PASSWORD_FILE  "ldap_service_password_file"
246 #define KRB5_CONF_LIBDEFAULTS                 "libdefaults"
247 #define KRB5_CONF_LOGGING                     "logging"
248 #define KRB5_CONF_MASTER_KEY_NAME             "master_key_name"
249 #define KRB5_CONF_MASTER_KEY_TYPE             "master_key_type"
250 #define KRB5_CONF_MASTER_KDC                  "master_kdc"
251 #define KRB5_CONF_MAX_LIFE                    "max_life"
252 #define KRB5_CONF_MAX_RENEWABLE_LIFE          "max_renewable_life"
253 #define KRB5_CONF_MODULE                      "module"
254 #define KRB5_CONF_NOADDRESSES                 "noaddresses"
255 #define KRB5_CONF_NO_HOST_REFERRAL            "no_host_referral"
256 #define KRB5_CONF_PERMITTED_ENCTYPES          "permitted_enctypes"
257 #define KRB5_CONF_PLUGINS                     "plugins"
258 #define KRB5_CONF_PLUGIN_BASE_DIR             "plugin_base_dir"
259 #define KRB5_CONF_PREFERRED_PREAUTH_TYPES     "preferred_preauth_types"
260 #define KRB5_CONF_PROXIABLE                   "proxiable"
261 #define KRB5_CONF_RDNS                        "rdns"
262 #define KRB5_CONF_REALMS                      "realms"
263 #define KRB5_CONF_REALM_TRY_DOMAINS           "realm_try_domains"
264 #define KRB5_CONF_REJECT_BAD_TRANSIT          "reject_bad_transit"
265 #define KRB5_CONF_RENEW_LIFETIME              "renew_lifetime"
266 #define KRB5_CONF_RESTRICT_ANONYMOUS_TO_TGT   "restrict_anonymous_to_tgt"
267 #define KRB5_CONF_SAFE_CHECKSUM_TYPE          "safe_checksum_type"
268 #define KRB5_CONF_SUPPORTED_ENCTYPES          "supported_enctypes"
269 #define KRB5_CONF_TICKET_LIFETIME             "ticket_lifetime"
270 #define KRB5_CONF_UDP_PREFERENCE_LIMIT        "udp_preference_limit"
271 #define KRB5_CONF_VERIFY_AP_REQ_NOFAIL        "verify_ap_req_nofail"
272 #define KRB5_CONF_V4_INSTANCE_CONVERT         "v4_instance_convert"
273 #define KRB5_CONF_V4_REALM                    "v4_realm"
274 #define KRB5_CONF_ASTERISK                    "*"
275
276 /* Cache configuration variables */
277 #define KRB5_CONF_FAST_AVAIL                  "fast_avail"
278 #define KRB5_CONF_PROXY_IMPERSONATOR          "proxy_impersonator"
279
280 /* Error codes used in KRB_ERROR protocol messages.
281    Return values of library routines are based on a different error table
282    (which allows non-ambiguous error codes between subsystems) */
283
284 /* KDC errors */
285 #define KDC_ERR_NONE                    0 /* No error */
286 #define KDC_ERR_NAME_EXP                1 /* Client's entry in DB expired */
287 #define KDC_ERR_SERVICE_EXP             2 /* Server's entry in DB expired */
288 #define KDC_ERR_BAD_PVNO                3 /* Requested pvno not supported */
289 #define KDC_ERR_C_OLD_MAST_KVNO         4 /* C's key encrypted in old master */
290 #define KDC_ERR_S_OLD_MAST_KVNO         5 /* S's key encrypted in old master */
291 #define KDC_ERR_C_PRINCIPAL_UNKNOWN     6 /* Client not found in Kerberos DB */
292 #define KDC_ERR_S_PRINCIPAL_UNKNOWN     7 /* Server not found in Kerberos DB */
293 #define KDC_ERR_PRINCIPAL_NOT_UNIQUE    8 /* Multiple entries in Kerberos DB */
294 #define KDC_ERR_NULL_KEY                9 /* The C or S has a null key */
295 #define KDC_ERR_CANNOT_POSTDATE         10 /* Tkt ineligible for postdating */
296 #define KDC_ERR_NEVER_VALID             11 /* Requested starttime > endtime */
297 #define KDC_ERR_POLICY                  12 /* KDC policy rejects request */
298 #define KDC_ERR_BADOPTION               13 /* KDC can't do requested opt. */
299 #define KDC_ERR_ENCTYPE_NOSUPP          14 /* No support for encryption type */
300 #define KDC_ERR_SUMTYPE_NOSUPP          15 /* No support for checksum type */
301 #define KDC_ERR_PADATA_TYPE_NOSUPP      16 /* No support for padata type */
302 #define KDC_ERR_TRTYPE_NOSUPP           17 /* No support for transited type */
303 #define KDC_ERR_CLIENT_REVOKED          18 /* C's creds have been revoked */
304 #define KDC_ERR_SERVICE_REVOKED         19 /* S's creds have been revoked */
305 #define KDC_ERR_TGT_REVOKED             20 /* TGT has been revoked */
306 #define KDC_ERR_CLIENT_NOTYET           21 /* C not yet valid */
307 #define KDC_ERR_SERVICE_NOTYET          22 /* S not yet valid */
308 #define KDC_ERR_KEY_EXP                 23 /* Password has expired */
309 #define KDC_ERR_PREAUTH_FAILED          24 /* Preauthentication failed */
310 #define KDC_ERR_PREAUTH_REQUIRED        25 /* Additional preauthentication */
311                                            /* required */
312 #define KDC_ERR_SERVER_NOMATCH          26 /* Requested server and */
313                                            /* ticket don't match*/
314 #define KDC_ERR_MUST_USE_USER2USER      27 /* Server principal valid for */
315                                            /*   user2user only */
316 #define KDC_ERR_PATH_NOT_ACCEPTED       28 /* KDC policy rejected transited */
317                                            /*   path */
318 #define KDC_ERR_SVC_UNAVAILABLE         29 /* A service is not
319                                             * available that is
320                                             * required to process the
321                                             * request */
322 /* Application errors */
323 #define KRB_AP_ERR_BAD_INTEGRITY 31     /* Decrypt integrity check failed */
324 #define KRB_AP_ERR_TKT_EXPIRED  32      /* Ticket expired */
325 #define KRB_AP_ERR_TKT_NYV      33      /* Ticket not yet valid */
326 #define KRB_AP_ERR_REPEAT       34      /* Request is a replay */
327 #define KRB_AP_ERR_NOT_US       35      /* The ticket isn't for us */
328 #define KRB_AP_ERR_BADMATCH     36      /* Ticket/authenticator don't match */
329 #define KRB_AP_ERR_SKEW         37      /* Clock skew too great */
330 #define KRB_AP_ERR_BADADDR      38      /* Incorrect net address */
331 #define KRB_AP_ERR_BADVERSION   39      /* Protocol version mismatch */
332 #define KRB_AP_ERR_MSG_TYPE     40      /* Invalid message type */
333 #define KRB_AP_ERR_MODIFIED     41      /* Message stream modified */
334 #define KRB_AP_ERR_BADORDER     42      /* Message out of order */
335 #define KRB_AP_ERR_BADKEYVER    44      /* Key version is not available */
336 #define KRB_AP_ERR_NOKEY        45      /* Service key not available */
337 #define KRB_AP_ERR_MUT_FAIL     46      /* Mutual authentication failed */
338 #define KRB_AP_ERR_BADDIRECTION 47      /* Incorrect message direction */
339 #define KRB_AP_ERR_METHOD       48      /* Alternative authentication */
340                                         /* method required */
341 #define KRB_AP_ERR_BADSEQ       49      /* Incorrect sequence numnber */
342                                         /* in message */
343 #define KRB_AP_ERR_INAPP_CKSUM  50      /* Inappropriate type of */
344                                         /* checksum in message */
345 #define KRB_AP_PATH_NOT_ACCEPTED 51     /* Policy rejects transited path */
346 #define KRB_ERR_RESPONSE_TOO_BIG 52     /* Response too big for UDP, */
347                                         /*   retry with TCP */
348
349 /* other errors */
350 #define KRB_ERR_GENERIC         60      /* Generic error (description */
351                                         /* in e-text) */
352 #define KRB_ERR_FIELD_TOOLONG   61      /* Field is too long for impl. */
353
354 /* PKINIT server-reported errors */
355 #define KDC_ERR_CLIENT_NOT_TRUSTED              62 /* client cert not trusted */
356 #define KDC_ERR_KDC_NOT_TRUSTED                 63
357 #define KDC_ERR_INVALID_SIG                     64 /* client signature verify failed */
358 #define KDC_ERR_DH_KEY_PARAMETERS_NOT_ACCEPTED  65 /* invalid Diffie-Hellman parameters */
359 #define KDC_ERR_CERTIFICATE_MISMATCH            66
360 #define KRB_AP_ERR_NO_TGT                       67
361 #define KDC_ERR_WRONG_REALM                     68
362 #define KRB_AP_ERR_USER_TO_USER_REQUIRED        69
363 #define KDC_ERR_CANT_VERIFY_CERTIFICATE         70 /* client cert not verifiable to */
364                                                    /* trusted root cert */
365 #define KDC_ERR_INVALID_CERTIFICATE             71 /* client cert had invalid signature */
366 #define KDC_ERR_REVOKED_CERTIFICATE             72 /* client cert was revoked */
367 #define KDC_ERR_REVOCATION_STATUS_UNKNOWN       73 /* client cert revoked, reason unknown */
368 #define KDC_ERR_REVOCATION_STATUS_UNAVAILABLE   74
369 #define KDC_ERR_CLIENT_NAME_MISMATCH            75 /* mismatch between client cert and */
370                                                    /* principal name */
371 #define KDC_ERR_INCONSISTENT_KEY_PURPOSE        77 /* bad extended key use */
372 #define KDC_ERR_DIGEST_IN_CERT_NOT_ACCEPTED     78 /* bad digest algorithm in client cert */
373 #define KDC_ERR_PA_CHECKSUM_MUST_BE_INCLUDED    79 /* missing paChecksum in PA-PK-AS-REQ */
374 #define KDC_ERR_DIGEST_IN_SIGNED_DATA_NOT_ACCEPTED 80 /* bad digest algorithm in SignedData */
375 #define KDC_ERR_PUBLIC_KEY_ENCRYPTION_NOT_SUPPORTED 81
376 #define KRB_AP_ERR_IAKERB_KDC_NOT_FOUND         85 /* The IAKERB proxy could
377                                                       not find a KDC */
378 #define KRB_AP_ERR_IAKERB_KDC_NO_RESPONSE       86 /* The KDC did not respond
379                                                       to the IAKERB proxy */
380
381 /*
382  * A null-terminated array of this structure is returned by the KDC as
383  * the data part of the ETYPE_INFO preauth type.  It informs the
384  * client which encryption types are supported.
385  * The  same data structure is used by both etype-info and etype-info2
386  * but s2kparams must be null when encoding etype-info.
387  */
388 typedef struct _krb5_etype_info_entry {
389     krb5_magic      magic;
390     krb5_enctype    etype;
391     unsigned int    length;
392     krb5_octet      *salt;
393     krb5_data s2kparams;
394 } krb5_etype_info_entry;
395
396 /*
397  *  This is essentially -1 without sign extension which can screw up
398  *  comparisons on 64 bit machines. If the length is this value, then
399  *  the salt data is not present. This is to distinguish between not
400  *  being set and being of 0 length.
401  */
402 #define KRB5_ETYPE_NO_SALT VALID_UINT_BITS
403
404 typedef krb5_etype_info_entry ** krb5_etype_info;
405
406 /* RFC 4537 */
407 typedef struct _krb5_etype_list {
408     int             length;
409     krb5_enctype    *etypes;
410 } krb5_etype_list;
411
412 /* sam_type values -- informational only */
413 #define PA_SAM_TYPE_ENIGMA     1   /*  Enigma Logic */
414 #define PA_SAM_TYPE_DIGI_PATH  2   /*  Digital Pathways */
415 #define PA_SAM_TYPE_SKEY_K0    3   /*  S/key where  KDC has key 0 */
416 #define PA_SAM_TYPE_SKEY       4   /*  Traditional S/Key */
417 #define PA_SAM_TYPE_SECURID    5   /*  Security Dynamics */
418 #define PA_SAM_TYPE_CRYPTOCARD 6   /*  CRYPTOCard */
419 #if 1 /* XXX need to figure out who has which numbers assigned */
420 #define PA_SAM_TYPE_ACTIVCARD_DEC  6   /*  ActivCard decimal mode */
421 #define PA_SAM_TYPE_ACTIVCARD_HEX  7   /*  ActivCard hex mode */
422 #define PA_SAM_TYPE_DIGI_PATH_HEX  8   /*  Digital Pathways hex mode */
423 #endif
424 #define PA_SAM_TYPE_EXP_BASE    128 /* experimental */
425 #define PA_SAM_TYPE_GRAIL               (PA_SAM_TYPE_EXP_BASE+0) /* testing */
426 #define PA_SAM_TYPE_SECURID_PREDICT     (PA_SAM_TYPE_EXP_BASE+1) /* special */
427
428 typedef struct _krb5_sam_challenge_2 {
429     krb5_data       sam_challenge_2_body;
430     krb5_checksum   **sam_cksum;            /* Array of checksums */
431 } krb5_sam_challenge_2;
432
433 typedef struct _krb5_sam_challenge_2_body {
434     krb5_magic      magic;
435     krb5_int32      sam_type; /* information */
436     krb5_flags      sam_flags; /* KRB5_SAM_* values */
437     krb5_data       sam_type_name;
438     krb5_data       sam_track_id;
439     krb5_data       sam_challenge_label;
440     krb5_data       sam_challenge;
441     krb5_data       sam_response_prompt;
442     krb5_data       sam_pk_for_sad;
443     krb5_int32      sam_nonce;
444     krb5_enctype    sam_etype;
445 } krb5_sam_challenge_2_body;
446
447 typedef struct _krb5_sam_response_2 {
448     krb5_magic      magic;
449     krb5_int32      sam_type; /* informational */
450     krb5_flags      sam_flags; /* KRB5_SAM_* values */
451     krb5_data       sam_track_id; /* copied */
452     krb5_enc_data   sam_enc_nonce_or_sad; /* krb5_enc_sam_response_enc */
453     krb5_int32      sam_nonce;
454 } krb5_sam_response_2;
455
456 typedef struct _krb5_enc_sam_response_enc_2 {
457     krb5_magic      magic;
458     krb5_int32      sam_nonce;
459     krb5_data       sam_sad;
460 } krb5_enc_sam_response_enc_2;
461
462 /*
463  * Keep the pkinit definitions in a separate file so that the plugin
464  * only has to include k5-int-pkinit.h rather than k5-int.h
465  */
466
467 #include "k5-int-pkinit.h"
468
469 #include <stdlib.h>
470 #include <string.h>
471
472 #ifndef HAVE_STRDUP
473 extern char *strdup (const char *);
474 #endif
475
476 #ifdef HAVE_UNISTD_H
477 #include <unistd.h>
478 #endif
479
480 #ifdef HAVE_SYS_TIME_H
481 #include <sys/time.h>
482 #ifdef TIME_WITH_SYS_TIME
483 #include <time.h>
484 #endif
485 #else
486 #include <time.h>
487 #endif
488
489 #ifdef HAVE_SYS_STAT_H
490 #include <sys/stat.h>                   /* struct stat, stat() */
491 #endif
492
493 #ifdef HAVE_SYS_PARAM_H
494 #include <sys/param.h>                  /* MAXPATHLEN */
495 #endif
496
497 #ifdef HAVE_SYS_FILE_H
498 #include <sys/file.h>                   /* prototypes for file-related
499                                            syscalls; flags for open &
500                                            friends */
501 #endif
502
503 #ifdef HAVE_FCNTL_H
504 #include <fcntl.h>
505 #endif
506
507 #include <stdio.h>
508
509 #include "k5-gmt_mktime.h"
510
511 struct sendto_callback_info;
512
513 /* libos.spec */
514 krb5_error_code krb5_lock_file(krb5_context, int, int);
515 krb5_error_code krb5_unlock_file(krb5_context, int);
516 krb5_error_code krb5_sendto_kdc(krb5_context, const krb5_data *,
517                                 const krb5_data *, krb5_data *, int *, int);
518
519 krb5_error_code krb5_get_krbhst(krb5_context, const krb5_data *, char *** );
520 krb5_error_code krb5_free_krbhst(krb5_context, char * const * );
521 krb5_error_code krb5_create_secure_file(krb5_context, const char * pathname);
522 krb5_error_code krb5_sync_disk_file(krb5_context, FILE *fp);
523
524 krb5_error_code krb5int_init_context_kdc(krb5_context *);
525
526 krb5_error_code krb5_os_init_context(krb5_context context, profile_t profile,
527                                      krb5_flags flags);
528
529 void krb5_os_free_context(krb5_context);
530
531 /* This function is needed by KfM's KerberosPreferences API
532  * because it needs to be able to specify "secure" */
533 krb5_error_code
534 os_get_default_config_files(profile_filespec_t **pfiles, krb5_boolean secure);
535
536 krb5_error_code
537 krb5_os_hostaddr(krb5_context, const char *, krb5_address ***);
538
539 krb5_error_code
540 krb5int_get_domain_realm_mapping(krb5_context , const char *, char ***);
541
542 struct derived_key {
543     krb5_data constant;
544     krb5_key dkey;
545     struct derived_key *next;
546 };
547
548 /* Internal structure of an opaque key identifier */
549 struct krb5_key_st {
550     krb5_keyblock keyblock;
551     int refcount;
552     struct derived_key *derived;
553     /*
554      * Cache of data private to the cipher implementation, which we
555      * don't want to have to recompute for every operation.  This may
556      * include key schedules, iteration counts, etc.
557      *
558      * The cipher implementation is responsible for setting this up
559      * whenever needed, and the enc_provider key_cleanup method must
560      * then be provided to dispose of it.
561      */
562     void *cache;
563 };
564
565 krb5_error_code
566 krb5int_arcfour_gsscrypt(const krb5_keyblock *keyblock, krb5_keyusage usage,
567                          const krb5_data *kd_data, krb5_crypto_iov *data,
568                          size_t num_data);
569
570 /*
571  * Attempt to zero memory in a way that compilers won't optimize out.
572  *
573  * This mechanism should work even for heap storage about to be freed,
574  * or automatic storage right before we return from a function.
575  *
576  * Then, even if we leak uninitialized memory someplace, or UNIX
577  * "core" files get created with world-read access, some of the most
578  * sensitive data in the process memory will already be safely wiped.
579  *
580  * We're not going so far -- yet -- as to try to protect key data that
581  * may have been written into swap space....
582  */
583 #ifdef _WIN32
584 # define zap(ptr, len) SecureZeroMemory(ptr, len)
585 #elif defined(__GNUC__)
586 static inline void zap(void *ptr, size_t len)
587 {
588     memset(ptr, 0, len);
589     /*
590      * Some versions of gcc have gotten clever enough to eliminate a
591      * memset call right before the block in question is released.
592      * This (empty) asm requires it to assume that we're doing
593      * something interesting with the stored (zero) value, so the
594      * memset can't be eliminated.
595      *
596      * An optimizer that looks at assembly or object code may not be
597      * fooled, and may still cause the memset to go away.  Address
598      * that problem if and when we encounter it.
599      *
600      * This also may not be enough if free() does something
601      * interesting like purge memory locations from a write-back cache
602      * that hasn't written back the zero bytes yet.  A memory barrier
603      * instruction would help in that case.
604      */
605     asm volatile ("" : : "g" (ptr), "g" (len));
606 }
607 #else
608 /* Use a function from libkrb5support to defeat inlining. */
609 # define zap(ptr, len) krb5int_zap(ptr, len)
610 #endif
611
612 /* Convenience function: zap and free ptr if it is non-NULL. */
613 static inline void
614 zapfree(void *ptr, size_t len)
615 {
616     if (ptr != NULL) {
617         zap(ptr, len);
618         free(ptr);
619     }
620 }
621
622 /*
623  * Combine two keys (normally used by the hardware preauth mechanism)
624  */
625 krb5_error_code
626 krb5int_c_combine_keys(krb5_context context, krb5_keyblock *key1,
627                        krb5_keyblock *key2, krb5_keyblock *outkey);
628
629 void krb5int_c_free_keyblock(krb5_context, krb5_keyblock *key);
630 void krb5int_c_free_keyblock_contents(krb5_context, krb5_keyblock *);
631 krb5_error_code krb5int_c_init_keyblock(krb5_context, krb5_enctype enctype,
632                                         size_t length, krb5_keyblock **out);
633 krb5_error_code krb5int_c_copy_keyblock(krb5_context context,
634                                         const krb5_keyblock *from,
635                                         krb5_keyblock **to);
636 krb5_error_code krb5int_c_copy_keyblock_contents(krb5_context context,
637                                                  const krb5_keyblock *from,
638                                                  krb5_keyblock *to);
639
640 #ifdef KRB5_OLD_CRYPTO
641 /* old provider api */
642
643 krb5_error_code krb5_crypto_os_localaddr(krb5_address ***);
644
645 krb5_error_code krb5_crypto_us_timeofday(krb5_int32 *, krb5_int32 *);
646
647 #endif /* KRB5_OLD_CRYPTO */
648
649 /* this helper fct is in libkrb5, but it makes sense declared here. */
650
651 krb5_error_code
652 krb5_encrypt_keyhelper(krb5_context context, krb5_key key,
653                        krb5_keyusage keyusage, const krb5_data *plain,
654                        krb5_enc_data *cipher);
655
656 /*
657  * End "los-proto.h"
658  */
659
660 typedef struct _krb5_os_context {
661     krb5_magic              magic;
662     krb5_int32              time_offset;
663     krb5_int32              usec_offset;
664     krb5_int32              os_flags;
665     char *                  default_ccname;
666 } *krb5_os_context;
667
668 /* Get the current time of day plus a specified offset. */
669 krb5_error_code k5_time_with_offset(krb5_timestamp offset,
670                                     krb5_int32 offset_usec,
671                                     krb5_timestamp *time_out,
672                                     krb5_int32 *usec_out);
673
674 /*
675  * Flags for the os_flags field
676  *
677  * KRB5_OS_TOFFSET_VALID means that the time offset fields are valid.
678  * The intention is that this facility to correct the system clocks so
679  * that they reflect the "real" time, for systems where for some
680  * reason we can't set the system clock.  Instead we calculate the
681  * offset between the system time and real time, and store the offset
682  * in the os context so that we can correct the system clock as necessary.
683  *
684  * KRB5_OS_TOFFSET_TIME means that the time offset fields should be
685  * returned as the time by the krb5 time routines.  This should only
686  * be used for testing purposes (obviously!)
687  */
688 #define KRB5_OS_TOFFSET_VALID   1
689 #define KRB5_OS_TOFFSET_TIME    2
690
691 /* lock mode flags */
692 #define KRB5_LOCKMODE_SHARED    0x0001
693 #define KRB5_LOCKMODE_EXCLUSIVE 0x0002
694 #define KRB5_LOCKMODE_DONTBLOCK 0x0004
695 #define KRB5_LOCKMODE_UNLOCK    0x0008
696
697 /*
698  * Define our view of the size of a DES key.
699  */
700 #define KRB5_MIT_DES_KEYSIZE            8
701 #define KRB5_MIT_DES3_KEYSIZE           24
702 #define KRB5_MIT_DES3_KEY_BYTES         21
703
704 /*
705  * Check if des_int.h has been included before us.  If so, then check to see
706  * that our view of the DES key size is the same as des_int.h's.
707  */
708 #ifdef  MIT_DES_KEYSIZE
709 #if     MIT_DES_KEYSIZE != KRB5_MIT_DES_KEYSIZE
710 error(MIT_DES_KEYSIZE does not equal KRB5_MIT_DES_KEYSIZE)
711 #endif  /* MIT_DES_KEYSIZE != KRB5_MIT_DES_KEYSIZE */
712 #endif  /* MIT_DES_KEYSIZE */
713
714 /*
715  * Begin "preauth.h"
716  *
717  * (Originally written by Glen Machin at Sandia Labs.)
718  */
719 /*
720  * Sandia National Laboratories also makes no representations about the
721  * suitability of the modifications, or additions to this software for
722  * any purpose.  It is provided "as is" without express or implied warranty.
723  */
724 #ifndef KRB5_PREAUTH__
725 #define KRB5_PREAUTH__
726
727 #include <krb5/preauth_plugin.h>
728
729 typedef krb5_error_code
730 (*krb5_gic_get_as_key_fct)(krb5_context, krb5_principal, krb5_enctype,
731                            krb5_prompter_fct, void *prompter_data,
732                            krb5_data *salt, krb5_data *s2kparams,
733                            krb5_keyblock *as_key, void *gak_data);
734
735 #define CLIENT_ROCK_MAGIC 0x4352434b
736 /*
737  * This structure is passed into the clpreauth methods and passed back to
738  * clpreauth callbacks so that they can locate the requested information.  It
739  * is opaque to the plugin code and can be expanded in the future as new types
740  * of requests are defined which may require other things to be passed through.
741  * All pointer fields are aliases and should not be freed.
742  */
743 struct krb5int_fast_request_state;
744 struct krb5_clpreauth_rock_st {
745     krb5_magic magic;
746     krb5_enctype *etype;
747     struct krb5int_fast_request_state *fast_state;
748
749     /*
750      * These fields allow gak_fct to be called via the rock.  The
751      * gak_fct and gak_data fields have an extra level of indirection
752      * since they can change in the init_creds context.
753      */
754     krb5_keyblock *as_key;
755     krb5_gic_get_as_key_fct *gak_fct;
756     void **gak_data;
757     krb5_boolean *default_salt;
758     krb5_data *salt;
759     krb5_data *s2kparams;
760     krb5_principal client;
761     krb5_prompter_fct prompter;
762     void *prompter_data;
763
764     /* Discovered offset of server time during preauth */
765     krb5_timestamp pa_offset;
766     krb5_int32 pa_offset_usec;
767     enum { NO_OFFSET = 0, UNAUTH_OFFSET, AUTH_OFFSET } pa_offset_state;
768 };
769
770 typedef struct _krb5_pa_enc_ts {
771     krb5_timestamp      patimestamp;
772     krb5_int32          pausec;
773 } krb5_pa_enc_ts;
774
775 typedef struct _krb5_pa_for_user {
776     krb5_principal      user;
777     krb5_checksum       cksum;
778     krb5_data           auth_package;
779 } krb5_pa_for_user;
780
781 typedef struct _krb5_s4u_userid {
782     krb5_int32          nonce;
783     krb5_principal      user;
784     krb5_data           subject_cert;
785     krb5_flags          options;
786 } krb5_s4u_userid;
787
788 #define KRB5_S4U_OPTS_CHECK_LOGON_HOURS         0x40000000 /* check logon hour restrictions */
789 #define KRB5_S4U_OPTS_USE_REPLY_KEY_USAGE       0x20000000 /* sign with usage 27 instead of 26 */
790
791 typedef struct _krb5_pa_s4u_x509_user {
792     krb5_s4u_userid     user_id;
793     krb5_checksum       cksum;
794 } krb5_pa_s4u_x509_user;
795
796 enum {
797     KRB5_FAST_ARMOR_AP_REQUEST = 0x1
798 };
799
800 typedef struct _krb5_fast_armor {
801     krb5_int32 armor_type;
802     krb5_data armor_value;
803 } krb5_fast_armor;
804 typedef struct _krb5_fast_armored_req {
805     krb5_magic magic;
806     krb5_fast_armor *armor;
807     krb5_checksum req_checksum;
808     krb5_enc_data enc_part;
809 } krb5_fast_armored_req;
810
811 typedef struct _krb5_fast_req {
812     krb5_magic magic;
813     krb5_flags fast_options;
814     /* padata from req_body is used*/
815     krb5_kdc_req *req_body;
816 } krb5_fast_req;
817
818 /* Bits 0-15 are critical in fast options.*/
819 #define UNSUPPORTED_CRITICAL_FAST_OPTIONS 0x00ff
820 #define KRB5_FAST_OPTION_HIDE_CLIENT_NAMES 0x01
821
822 typedef struct _krb5_fast_finished {
823     krb5_timestamp timestamp;
824     krb5_int32 usec;
825     krb5_principal client;
826     krb5_checksum ticket_checksum;
827 } krb5_fast_finished;
828
829 typedef struct _krb5_fast_response {
830     krb5_magic magic;
831     krb5_pa_data **padata;
832     krb5_keyblock *strengthen_key;
833     krb5_fast_finished *finished;
834     krb5_int32 nonce;
835 } krb5_fast_response;
836
837 typedef struct _krb5_ad_kdcissued {
838     krb5_checksum ad_checksum;
839     krb5_principal i_principal;
840     krb5_authdata **elements;
841 } krb5_ad_kdcissued;
842
843 typedef struct _krb5_ad_signedpath_data {
844     krb5_principal client;
845     krb5_timestamp authtime;
846     krb5_principal *delegated;
847     krb5_pa_data **method_data;
848     krb5_authdata **authorization_data;
849 } krb5_ad_signedpath_data;
850
851 typedef struct _krb5_ad_signedpath {
852     krb5_enctype enctype;
853     krb5_checksum checksum;
854     krb5_principal *delegated;
855     krb5_pa_data **method_data;
856 } krb5_ad_signedpath;
857
858 typedef struct _krb5_iakerb_header {
859     krb5_data target_realm;
860     krb5_data *cookie;
861 } krb5_iakerb_header;
862
863 typedef struct _krb5_iakerb_finished {
864     krb5_checksum checksum;
865 } krb5_iakerb_finished;
866
867 typedef krb5_error_code
868 (*krb5_preauth_obtain_proc)(krb5_context, krb5_pa_data *,
869                             krb5_etype_info, krb5_keyblock *,
870                             krb5_error_code (*)(krb5_context,
871                                                 const krb5_enctype,
872                                                 krb5_data *,
873                                                 krb5_const_pointer,
874                                                 krb5_keyblock **),
875                             krb5_const_pointer, krb5_creds *,
876                             krb5_kdc_req *, krb5_pa_data **);
877
878 typedef krb5_error_code
879 (*krb5_preauth_process_proc)(krb5_context, krb5_pa_data *, krb5_kdc_req *,
880                              krb5_kdc_rep *,
881                              krb5_error_code (*)(krb5_context,
882                                                  const krb5_enctype,
883                                                  krb5_data *,
884                                                  krb5_const_pointer,
885                                                  krb5_keyblock **),
886                              krb5_const_pointer,
887                              krb5_error_code (*)(krb5_context,
888                                                  const krb5_keyblock *,
889                                                  krb5_const_pointer,
890                                                  krb5_kdc_rep * ),
891                              krb5_keyblock **, krb5_creds *, krb5_int32 *,
892                              krb5_int32 *);
893
894 typedef struct _krb5_preauth_ops {
895     krb5_magic magic;
896     int     type;
897     int flags;
898     krb5_preauth_obtain_proc    obtain;
899     krb5_preauth_process_proc   process;
900 } krb5_preauth_ops;
901
902 krb5_pa_data *
903 krb5int_find_pa_data(krb5_context, krb5_pa_data *const *, krb5_preauthtype);
904 /* Does not return a copy; original padata sequence responsible for freeing*/
905
906 void krb5_free_etype_info(krb5_context, krb5_etype_info);
907
908 /*
909  * Preauthentication property flags
910  */
911 #define KRB5_PREAUTH_FLAGS_ENCRYPT      0x00000001
912 #define KRB5_PREAUTH_FLAGS_HARDWARE     0x00000002
913
914 #endif /* KRB5_PREAUTH__ */
915 /*
916  * End "preauth.h"
917  */
918
919 /*
920  * Extending the krb5_get_init_creds_opt structure.  The original
921  * krb5_get_init_creds_opt structure is defined publicly.  The
922  * new extended version is private.  The original interface
923  * assumed a pre-allocated structure which was passed to
924  * krb5_get_init_creds_init().  The new interface assumes that
925  * the caller will call krb5_get_init_creds_alloc() and
926  * krb5_get_init_creds_free().
927  *
928  * Callers MUST NOT call krb5_get_init_creds_init() after allocating an
929  * opts structure using krb5_get_init_creds_alloc().  To do so will
930  * introduce memory leaks.  Unfortunately, there is no way to enforce
931  * this behavior.
932  *
933  * Two private flags are added for backward compatibility.
934  * KRB5_GET_INIT_CREDS_OPT_EXTENDED says that the structure was allocated
935  * with the new krb5_get_init_creds_opt_alloc() function.
936  * KRB5_GET_INIT_CREDS_OPT_SHADOWED is set to indicate that the extended
937  * structure is a shadow copy of an original krb5_get_init_creds_opt
938  * structure.
939  * If KRB5_GET_INIT_CREDS_OPT_SHADOWED is set after a call to
940  * krb5int_gic_opt_to_opte(), the resulting extended structure should be
941  * freed (using krb5_get_init_creds_free).  Otherwise, the original
942  * structure was already extended and there is no need to free it.
943  */
944
945 #define KRB5_GET_INIT_CREDS_OPT_EXTENDED 0x80000000
946 #define KRB5_GET_INIT_CREDS_OPT_SHADOWED 0x40000000
947
948 #define krb5_gic_opt_is_extended(s)                                     \
949     ((s) && ((s)->flags & KRB5_GET_INIT_CREDS_OPT_EXTENDED) ? 1 : 0)
950 #define krb5_gic_opt_is_shadowed(s)                                     \
951     ((s) && ((s)->flags & KRB5_GET_INIT_CREDS_OPT_SHADOWED) ? 1 : 0)
952
953
954 typedef struct _krb5_gic_opt_private {
955     int num_preauth_data;
956     krb5_gic_opt_pa_data *preauth_data;
957     char * fast_ccache_name;
958     krb5_ccache out_ccache;
959     krb5_flags fast_flags;
960     krb5_expire_callback_func expire_cb;
961     void *expire_data;
962 } krb5_gic_opt_private;
963
964 /*
965  * On the Mac, ensure that the layout of krb5_gic_opt_ext matches that
966  * of krb5_get_init_creds_opt.
967  */
968 #if TARGET_OS_MAC
969 #    pragma pack(push,2)
970 #endif
971
972 typedef struct _krb5_gic_opt_ext {
973     krb5_flags flags;
974     krb5_deltat tkt_life;
975     krb5_deltat renew_life;
976     int forwardable;
977     int proxiable;
978     krb5_enctype *etype_list;
979     int etype_list_length;
980     krb5_address **address_list;
981     krb5_preauthtype *preauth_list;
982     int preauth_list_length;
983     krb5_data *salt;
984     /*
985      * Do not change anything above this point in this structure.
986      * It is identical to the public krb5_get_init_creds_opt structure.
987      * New members must be added below.
988      */
989     krb5_gic_opt_private *opt_private;
990 } krb5_gic_opt_ext;
991
992 #if TARGET_OS_MAC
993 #    pragma pack(pop)
994 #endif
995
996 krb5_error_code
997 krb5int_gic_opt_to_opte(krb5_context context, krb5_get_init_creds_opt *opt,
998                         krb5_gic_opt_ext **opte, unsigned int force,
999                         const char *where);
1000
1001 krb5_error_code
1002 krb5int_copy_data_contents(krb5_context, const krb5_data *, krb5_data *);
1003
1004 krb5_error_code
1005 krb5int_copy_data_contents_add0(krb5_context, const krb5_data *, krb5_data *);
1006
1007 krb5_error_code
1008 krb5int_copy_creds_contents(krb5_context, const krb5_creds *, krb5_creds *);
1009
1010 krb5_error_code KRB5_CALLCONV
1011 krb5int_get_init_creds(krb5_context context, krb5_creds *creds,
1012                        krb5_principal client, krb5_prompter_fct prompter,
1013                        void *prompter_data, krb5_deltat start_time,
1014                        char *in_tkt_service, krb5_get_init_creds_opt *options,
1015                        krb5_gic_get_as_key_fct gak, void *gak_data,
1016                        int *master, krb5_kdc_rep **as_reply);
1017
1018 krb5_error_code
1019 krb5int_populate_gic_opt (krb5_context, krb5_get_init_creds_opt **,
1020                           krb5_flags options, krb5_address *const *addrs,
1021                           krb5_enctype *ktypes,
1022                           krb5_preauthtype *pre_auth_types, krb5_creds *creds);
1023
1024
1025 krb5_error_code KRB5_CALLCONV
1026 krb5_do_preauth(krb5_context context, krb5_kdc_req *request,
1027                 krb5_data *encoded_request_body,
1028                 krb5_data *encoded_previous_request, krb5_pa_data **in_padata,
1029                 krb5_pa_data ***out_padata, krb5_prompter_fct prompter,
1030                 void *prompter_data, krb5_clpreauth_rock preauth_rock,
1031                 krb5_gic_opt_ext *opte, krb5_boolean *got_real_out);
1032
1033 krb5_error_code KRB5_CALLCONV
1034 krb5_do_preauth_tryagain(krb5_context context, krb5_kdc_req *request,
1035                          krb5_data *encoded_request_body,
1036                          krb5_data *encoded_previous_request,
1037                          krb5_pa_data **in_padata, krb5_pa_data ***out_padata,
1038                          krb5_error *err_reply, krb5_pa_data **err_padata,
1039                          krb5_prompter_fct prompter, void *prompter_data,
1040                          krb5_clpreauth_rock preauth_rock,
1041                          krb5_gic_opt_ext *opte);
1042
1043 void KRB5_CALLCONV krb5_init_preauth_context(krb5_context);
1044 void KRB5_CALLCONV krb5_free_preauth_context(krb5_context);
1045 void KRB5_CALLCONV krb5_clear_preauth_context_use_counts(krb5_context);
1046 void KRB5_CALLCONV krb5_preauth_prepare_request(krb5_context,
1047                                                 krb5_gic_opt_ext *,
1048                                                 krb5_kdc_req *);
1049 void KRB5_CALLCONV krb5_preauth_request_context_init(krb5_context);
1050 void KRB5_CALLCONV krb5_preauth_request_context_fini(krb5_context);
1051
1052 void KRB5_CALLCONV
1053 krb5_free_sam_challenge_2(krb5_context, krb5_sam_challenge_2 *);
1054
1055 void KRB5_CALLCONV
1056 krb5_free_sam_challenge_2_body(krb5_context, krb5_sam_challenge_2_body *);
1057
1058 void KRB5_CALLCONV
1059 krb5_free_sam_response_2(krb5_context, krb5_sam_response_2 *);
1060
1061 void KRB5_CALLCONV
1062 krb5_free_enc_sam_response_enc_2(krb5_context, krb5_enc_sam_response_enc_2 *);
1063
1064 void KRB5_CALLCONV
1065 krb5_free_sam_challenge_2_contents(krb5_context, krb5_sam_challenge_2 *);
1066
1067 void KRB5_CALLCONV
1068 krb5_free_sam_challenge_2_body_contents(krb5_context,
1069                                         krb5_sam_challenge_2_body *);
1070
1071 void KRB5_CALLCONV
1072 krb5_free_sam_response_2_contents(krb5_context, krb5_sam_response_2 *);
1073
1074 void KRB5_CALLCONV
1075 krb5_free_enc_sam_response_enc_2_contents(krb5_context,
1076                                           krb5_enc_sam_response_enc_2 * );
1077
1078 void KRB5_CALLCONV
1079 krb5_free_pa_enc_ts(krb5_context, krb5_pa_enc_ts *);
1080
1081 void KRB5_CALLCONV
1082 krb5_free_pa_for_user(krb5_context, krb5_pa_for_user *);
1083
1084 void KRB5_CALLCONV
1085 krb5_free_s4u_userid_contents(krb5_context, krb5_s4u_userid *);
1086
1087 void KRB5_CALLCONV
1088 krb5_free_pa_s4u_x509_user(krb5_context, krb5_pa_s4u_x509_user *);
1089
1090 void KRB5_CALLCONV
1091 krb5_free_pa_pac_req(krb5_context, krb5_pa_pac_req * );
1092
1093 void KRB5_CALLCONV
1094 krb5_free_etype_list(krb5_context, krb5_etype_list * );
1095
1096 void KRB5_CALLCONV krb5_free_fast_armor(krb5_context, krb5_fast_armor *);
1097 void KRB5_CALLCONV krb5_free_fast_armored_req(krb5_context,
1098                                               krb5_fast_armored_req *);
1099 void KRB5_CALLCONV krb5_free_fast_req(krb5_context, krb5_fast_req *);
1100 void KRB5_CALLCONV krb5_free_fast_finished(krb5_context, krb5_fast_finished *);
1101 void KRB5_CALLCONV krb5_free_fast_response(krb5_context, krb5_fast_response *);
1102 void KRB5_CALLCONV krb5_free_ad_kdcissued(krb5_context, krb5_ad_kdcissued *);
1103 void KRB5_CALLCONV krb5_free_ad_signedpath(krb5_context, krb5_ad_signedpath *);
1104 void KRB5_CALLCONV krb5_free_iakerb_header(krb5_context, krb5_iakerb_header *);
1105 void KRB5_CALLCONV krb5_free_iakerb_finished(krb5_context,
1106                                              krb5_iakerb_finished *);
1107
1108 /* #include "krb5/wordsize.h" -- comes in through base-defs.h. */
1109 #include "com_err.h"
1110 #include "k5-plugin.h"
1111
1112 #include <krb5/authdata_plugin.h>
1113
1114 struct _krb5_authdata_context {
1115     krb5_magic magic;
1116     int n_modules;
1117     struct _krb5_authdata_context_module {
1118         krb5_authdatatype ad_type;
1119         void *plugin_context;
1120         authdata_client_plugin_fini_proc client_fini;
1121         krb5_flags flags;
1122         krb5plugin_authdata_client_ftable_v0 *ftable;
1123         authdata_client_request_init_proc client_req_init;
1124         authdata_client_request_fini_proc client_req_fini;
1125         const char *name;
1126         void *request_context;
1127         void **request_context_pp;
1128     } *modules;
1129     struct plugin_dir_handle plugins;
1130 };
1131
1132 typedef struct _krb5_authdata_context *krb5_authdata_context;
1133
1134 void KRB5_CALLCONV
1135 krb5int_free_data_list(krb5_context context, krb5_data *data);
1136
1137 krb5_error_code KRB5_CALLCONV
1138 krb5_authdata_context_init(krb5_context kcontext,
1139                            krb5_authdata_context *pcontext);
1140
1141 void KRB5_CALLCONV
1142 krb5_authdata_context_free(krb5_context kcontext,
1143                            krb5_authdata_context context);
1144
1145 krb5_error_code KRB5_CALLCONV
1146 krb5_authdata_export_authdata(krb5_context kcontext,
1147                               krb5_authdata_context context, krb5_flags usage,
1148                               krb5_authdata ***pauthdata);
1149
1150 krb5_error_code KRB5_CALLCONV
1151 krb5_authdata_get_attribute_types(krb5_context kcontext,
1152                                   krb5_authdata_context context,
1153                                   krb5_data **attrs);
1154
1155 krb5_error_code KRB5_CALLCONV
1156 krb5_authdata_get_attribute(krb5_context kcontext,
1157                             krb5_authdata_context context,
1158                             const krb5_data *attribute,
1159                             krb5_boolean *authenticated,
1160                             krb5_boolean *complete, krb5_data *value,
1161                             krb5_data *display_value, int *more);
1162
1163 krb5_error_code KRB5_CALLCONV
1164 krb5_authdata_set_attribute(krb5_context kcontext,
1165                             krb5_authdata_context context,
1166                             krb5_boolean complete, const krb5_data *attribute,
1167                             const krb5_data *value);
1168
1169 krb5_error_code KRB5_CALLCONV
1170 krb5_authdata_delete_attribute(krb5_context kcontext,
1171                                krb5_authdata_context context,
1172                                const krb5_data *attribute);
1173
1174 krb5_error_code KRB5_CALLCONV
1175 krb5_authdata_import_attributes(krb5_context kcontext,
1176                                 krb5_authdata_context context,
1177                                 krb5_flags usage, const krb5_data *attributes);
1178
1179 krb5_error_code KRB5_CALLCONV
1180 krb5_authdata_export_attributes(krb5_context kcontext,
1181                                 krb5_authdata_context context,
1182                                 krb5_flags usage, krb5_data **pattributes);
1183
1184 krb5_error_code KRB5_CALLCONV
1185 krb5_authdata_export_internal(krb5_context kcontext,
1186                               krb5_authdata_context context,
1187                               krb5_boolean restrict_authenticated,
1188                               const char *module, void **ptr);
1189
1190 krb5_error_code KRB5_CALLCONV
1191 krb5_authdata_context_copy(krb5_context kcontext, krb5_authdata_context src,
1192                            krb5_authdata_context *dst);
1193
1194 krb5_error_code KRB5_CALLCONV
1195 krb5_authdata_free_internal(krb5_context kcontext,
1196                             krb5_authdata_context context, const char *module,
1197                             void *ptr);
1198
1199 /*** Plugin framework ***/
1200
1201 /*
1202  * This framework can be used to create pluggable interfaces.  Not all existing
1203  * pluggable interface use this framework, but new ones should.  A new
1204  * pluggable interface entails:
1205  *
1206  * - An interface ID definition in the list of #defines below.
1207  *
1208  * - A name in the interface_names array in lib/krb5/krb/plugins.c.
1209  *
1210  * - An installed public header file in include/krb5.  The public header should
1211  *   include <krb5/plugin.h> and should declare a vtable structure for each
1212  *   supported major version of the interface.
1213  *
1214  * - A consumer API implementation, located within the code unit which makes
1215  *   use of the pluggable interface.  The consumer API should consist of:
1216  *
1217  *   . An interface-specific handle type which contains a vtable structure for
1218  *     the module (or a union of several such structures, if there are multiple
1219  *     supported major versions) and, optionally, resource data bound to the
1220  *     handle.
1221  *
1222  *   . An interface-specific loader function which creates a handle or list of
1223  *     handles.  A list of handles would be created if the interface is a
1224  *     one-to-many interface where the consumer wants to consult all available
1225  *     modules; a single handle would be created for an interface where the
1226  *     consumer wants to consult a specific module.  The loader function should
1227  *     use k5_plugin_load or k5_plugin_load_all to produce one or a list of
1228  *     vtable initializer functions, and should use those functions to fill in
1229  *     the vtable structure for the module (if necessary, trying each supported
1230  *     major version starting from the most recent).  The loader function can
1231  *     also bind resource data into the handle based on caller arguments, if
1232  *     appropriate.
1233  *
1234  *   . For each plugin method, a wrapper function which accepts a krb5_context,
1235  *     a plugin handle, and the method arguments.  Wrapper functions should
1236  *     invoke the method function contained in the handle's vtable.
1237  *
1238  * - Possibly, built-in implementations of the interface, also located within
1239  *   the code unit which makes use of the interface.  Built-in implementations
1240  *   must be registered with k5_plugin_register before the first call to
1241  *   k5_plugin_load or k5_plugin_load_all.
1242  *
1243  * A pluggable interface should have one or more currently supported major
1244  * versions, starting at 1.  Each major version should have a current minor
1245  * version, also starting at 1.  If new methods are added to a vtable, the
1246  * minor version should be incremented and the vtable stucture should document
1247  * where each minor vtable version ends.  If method signatures for a vtable are
1248  * changed, the major version should be incremented.
1249  *
1250  * Plugin module implementations (either built-in or dynamically loaded) should
1251  * define a function named <interfacename>_<modulename>_initvt, matching the
1252  * signature of krb5_plugin_initvt_fn as declared in include/krb5/plugin.h.
1253  * The initvt function should check the given maj_ver argument against its own
1254  * supported major versions, cast the vtable pointer to the appropriate
1255  * interface-specific vtable type, and fill in the vtable methods, stopping as
1256  * appropriate for the given min_ver.  Memory for the vtable structure is
1257  * allocated by the caller, not by the module.
1258  *
1259  * Dynamic plugin modules are registered with the framework through the
1260  * [plugins] section of the profile, as described in the admin documentation
1261  * and krb5.conf man page.
1262  */
1263
1264 /*
1265  * A linked list entry mapping a module name to a module initvt function.  The
1266  * entry may also include a dynamic object handle so that it can be released
1267  * when the context is destroyed.
1268  */
1269 struct plugin_mapping {
1270     char *modname;
1271     krb5_plugin_initvt_fn module;
1272     struct plugin_file_handle *dyn_handle;
1273     struct plugin_mapping *next;
1274 };
1275
1276 /* Holds krb5_context information about each pluggable interface. */
1277 struct plugin_interface {
1278     struct plugin_mapping *modules;
1279     krb5_boolean configured;
1280 };
1281
1282 /* A list of plugin interface IDs.  Make sure to increment
1283  * PLUGIN_NUM_INTERFACES when a new interface is added, and add an entry to the
1284  * interface_names table in lib/krb5/krb/plugin.c. */
1285 #define PLUGIN_INTERFACE_PWQUAL      0
1286 #define PLUGIN_INTERFACE_KADM5_HOOK  1
1287 #define PLUGIN_INTERFACE_CLPREAUTH   2
1288 #define PLUGIN_INTERFACE_KDCPREAUTH  3
1289 #define PLUGIN_INTERFACE_CCSELECT    4
1290 #define PLUGIN_NUM_INTERFACES        5
1291
1292 /* Retrieve the plugin module of type interface_id and name modname,
1293  * storing the result into module. */
1294 krb5_error_code
1295 k5_plugin_load(krb5_context context, int interface_id, const char *modname,
1296                krb5_plugin_initvt_fn *module);
1297
1298 /* Retrieve all plugin modules of type interface_id, storing the result
1299  * into modules.  Free the result with k5_plugin_free_handles. */
1300 krb5_error_code
1301 k5_plugin_load_all(krb5_context context, int interface_id,
1302                    krb5_plugin_initvt_fn **modules);
1303
1304 /* Release a module list allocated by k5_plugin_load_all. */
1305 void
1306 k5_plugin_free_modules(krb5_context context, krb5_plugin_initvt_fn *modules);
1307
1308 /* Register a plugin module of type interface_id and name modname. */
1309 krb5_error_code
1310 k5_plugin_register(krb5_context context, int interface_id, const char *modname,
1311                    krb5_plugin_initvt_fn module);
1312
1313 /*
1314  * Register a plugin module which is part of the krb5 tree but is built as a
1315  * dynamic plugin.  Look for the module in modsubdir relative to the
1316  * context->base_plugin_dir.
1317  */
1318 krb5_error_code
1319 k5_plugin_register_dyn(krb5_context context, int interface_id,
1320                        const char *modname, const char *modsubdir);
1321
1322 /* Destroy the module state within context; used by krb5_free_context. */
1323 void
1324 k5_plugin_free_context(krb5_context context);
1325
1326 struct _kdb5_dal_handle;        /* private, in kdb5.h */
1327 typedef struct _kdb5_dal_handle kdb5_dal_handle;
1328 struct _kdb_log_context;
1329 typedef struct krb5_preauth_context_st krb5_preauth_context;
1330 struct ccselect_module_handle;
1331 struct _krb5_context {
1332     krb5_magic      magic;
1333     krb5_enctype    *in_tkt_etypes;
1334     krb5_enctype    *tgs_etypes;
1335     struct _krb5_os_context os_context;
1336     char            *default_realm;
1337     profile_t       profile;
1338     kdb5_dal_handle *dal_handle;
1339     int             ser_ctx_count;
1340     void            *ser_ctx;
1341     /* allowable clock skew */
1342     krb5_deltat     clockskew;
1343     krb5_cksumtype  kdc_req_sumtype;
1344     krb5_cksumtype  default_ap_req_sumtype;
1345     krb5_cksumtype  default_safe_sumtype;
1346     krb5_flags      kdc_default_options;
1347     krb5_flags      library_options;
1348     krb5_boolean    profile_secure;
1349     int             fcc_default_format;
1350     krb5_prompt_type *prompt_types;
1351     /* Message size above which we'll try TCP first in send-to-kdc
1352        type code.  Aside from the 2**16 size limit, we put no
1353        absolute limit on the UDP packet size.  */
1354     int             udp_pref_limit;
1355
1356     /* Use the config-file ktypes instead of app-specified?  */
1357     krb5_boolean    use_conf_ktypes;
1358
1359 #ifdef KRB5_DNS_LOOKUP
1360     krb5_boolean    profile_in_memory;
1361 #endif /* KRB5_DNS_LOOKUP */
1362
1363     /* locate_kdc module stuff */
1364     struct plugin_dir_handle libkrb5_plugins;
1365     struct krb5plugin_service_locate_ftable *vtbl;
1366     void (**locate_fptrs)(void);
1367
1368     /* preauth module stuff */
1369     krb5_preauth_context *preauth_context;
1370
1371     /* cache module stuff */
1372     struct ccselect_module_handle **ccselect_handles;
1373
1374     /* error detail info */
1375     struct errinfo err;
1376
1377     /* For Sun iprop code; does this really have to be here?  */
1378     struct _kdb_log_context *kdblog_context;
1379
1380     krb5_boolean allow_weak_crypto;
1381     krb5_boolean ignore_acceptor_hostname;
1382
1383     krb5_trace_callback trace_callback;
1384     void *trace_callback_data;
1385
1386     struct plugin_interface plugins[PLUGIN_NUM_INTERFACES];
1387     char *plugin_base_dir;
1388 };
1389
1390 /* could be used in a table to find an etype and initialize a block */
1391
1392
1393 #define KRB5_LIBOPT_SYNC_KDCTIME        0x0001
1394
1395 /* internal message representations */
1396
1397 typedef struct _krb5_safe {
1398     krb5_magic magic;
1399     krb5_data user_data;                /* user data */
1400     krb5_timestamp timestamp;           /* client time, optional */
1401     krb5_int32 usec;                    /* microsecond portion of time,
1402                                            optional */
1403     krb5_ui_4 seq_number;               /* sequence #, optional */
1404     krb5_address *s_address;    /* sender address */
1405     krb5_address *r_address;    /* recipient address, optional */
1406     krb5_checksum *checksum;    /* data integrity checksum */
1407 } krb5_safe;
1408
1409 typedef struct _krb5_priv {
1410     krb5_magic magic;
1411     krb5_enc_data enc_part;             /* encrypted part */
1412 } krb5_priv;
1413
1414 typedef struct _krb5_priv_enc_part {
1415     krb5_magic magic;
1416     krb5_data user_data;                /* user data */
1417     krb5_timestamp timestamp;           /* client time, optional */
1418     krb5_int32 usec;                    /* microsecond portion of time, opt. */
1419     krb5_ui_4 seq_number;               /* sequence #, optional */
1420     krb5_address *s_address;    /* sender address */
1421     krb5_address *r_address;    /* recipient address, optional */
1422 } krb5_priv_enc_part;
1423
1424 void KRB5_CALLCONV krb5_free_safe(krb5_context, krb5_safe *);
1425 void KRB5_CALLCONV krb5_free_priv(krb5_context, krb5_priv *);
1426 void KRB5_CALLCONV krb5_free_priv_enc_part(krb5_context, krb5_priv_enc_part *);
1427
1428 /*
1429  * Begin "asn1.h"
1430  */
1431 #ifndef KRB5_ASN1__
1432 #define KRB5_ASN1__
1433
1434 /* ASN.1 encoding knowledge; KEEP IN SYNC WITH ASN.1 defs! */
1435 /* here we use some knowledge of ASN.1 encodings */
1436 /*
1437   Ticket is APPLICATION 1.
1438   Authenticator is APPLICATION 2.
1439   AS_REQ is APPLICATION 10.
1440   AS_REP is APPLICATION 11.
1441   TGS_REQ is APPLICATION 12.
1442   TGS_REP is APPLICATION 13.
1443   AP_REQ is APPLICATION 14.
1444   AP_REP is APPLICATION 15.
1445   KRB_SAFE is APPLICATION 20.
1446   KRB_PRIV is APPLICATION 21.
1447   KRB_CRED is APPLICATION 22.
1448   EncASRepPart is APPLICATION 25.
1449   EncTGSRepPart is APPLICATION 26.
1450   EncAPRepPart is APPLICATION 27.
1451   EncKrbPrivPart is APPLICATION 28.
1452   EncKrbCredPart is APPLICATION 29.
1453   KRB_ERROR is APPLICATION 30.
1454 */
1455 /* allow either constructed or primitive encoding, so check for bit 6
1456    set or reset */
1457 #define krb5int_is_app_tag(dat,tag)                     \
1458     ((dat != NULL) && (dat)->length &&                  \
1459      ((((dat)->data[0] & ~0x20) == ((tag) | 0x40))))
1460 #define krb5_is_krb_ticket(dat)               krb5int_is_app_tag(dat, 1)
1461 #define krb5_is_krb_authenticator(dat)        krb5int_is_app_tag(dat, 2)
1462 #define krb5_is_as_req(dat)                   krb5int_is_app_tag(dat, 10)
1463 #define krb5_is_as_rep(dat)                   krb5int_is_app_tag(dat, 11)
1464 #define krb5_is_tgs_req(dat)                  krb5int_is_app_tag(dat, 12)
1465 #define krb5_is_tgs_rep(dat)                  krb5int_is_app_tag(dat, 13)
1466 #define krb5_is_ap_req(dat)                   krb5int_is_app_tag(dat, 14)
1467 #define krb5_is_ap_rep(dat)                   krb5int_is_app_tag(dat, 15)
1468 #define krb5_is_krb_safe(dat)                 krb5int_is_app_tag(dat, 20)
1469 #define krb5_is_krb_priv(dat)                 krb5int_is_app_tag(dat, 21)
1470 #define krb5_is_krb_cred(dat)                 krb5int_is_app_tag(dat, 22)
1471 #define krb5_is_krb_enc_as_rep_part(dat)      krb5int_is_app_tag(dat, 25)
1472 #define krb5_is_krb_enc_tgs_rep_part(dat)     krb5int_is_app_tag(dat, 26)
1473 #define krb5_is_krb_enc_ap_rep_part(dat)      krb5int_is_app_tag(dat, 27)
1474 #define krb5_is_krb_enc_krb_priv_part(dat)    krb5int_is_app_tag(dat, 28)
1475 #define krb5_is_krb_enc_krb_cred_part(dat)    krb5int_is_app_tag(dat, 29)
1476 #define krb5_is_krb_error(dat)                krb5int_is_app_tag(dat, 30)
1477
1478 /*************************************************************************
1479  * Prototypes for krb5_encode.c
1480  *************************************************************************/
1481
1482 /*
1483   krb5_error_code encode_krb5_structure(const krb5_structure *rep,
1484   krb5_data **code);
1485   modifies  *code
1486   effects   Returns the ASN.1 encoding of *rep in **code.
1487   Returns ASN1_MISSING_FIELD if a required field is emtpy in *rep.
1488   Returns ENOMEM if memory runs out.
1489 */
1490
1491 krb5_error_code
1492 encode_krb5_authenticator(const krb5_authenticator *rep, krb5_data **code);
1493
1494 krb5_error_code
1495 encode_krb5_ticket(const krb5_ticket *rep, krb5_data **code);
1496
1497 krb5_error_code
1498 encode_krb5_enc_tkt_part(const krb5_enc_tkt_part *rep, krb5_data **code);
1499
1500 krb5_error_code
1501 encode_krb5_enc_kdc_rep_part(const krb5_enc_kdc_rep_part *rep,
1502                              krb5_data **code);
1503
1504 /* yes, the translation is identical to that used for KDC__REP */
1505 krb5_error_code
1506 encode_krb5_as_rep(const krb5_kdc_rep *rep, krb5_data **code);
1507
1508 /* yes, the translation is identical to that used for KDC__REP */
1509 krb5_error_code
1510 encode_krb5_tgs_rep(const krb5_kdc_rep *rep, krb5_data **code);
1511
1512 krb5_error_code
1513 encode_krb5_ap_req(const krb5_ap_req *rep, krb5_data **code);
1514
1515 krb5_error_code
1516 encode_krb5_ap_rep(const krb5_ap_rep *rep, krb5_data **code);
1517
1518 krb5_error_code
1519 encode_krb5_ap_rep_enc_part(const krb5_ap_rep_enc_part *rep, krb5_data **code);
1520
1521 krb5_error_code
1522 encode_krb5_as_req(const krb5_kdc_req *rep, krb5_data **code);
1523
1524 krb5_error_code
1525 encode_krb5_tgs_req(const krb5_kdc_req *rep, krb5_data **code);
1526
1527 krb5_error_code
1528 encode_krb5_kdc_req_body(const krb5_kdc_req *rep, krb5_data **code);
1529
1530 krb5_error_code
1531 encode_krb5_safe(const krb5_safe *rep, krb5_data **code);
1532
1533 struct krb5_safe_with_body {
1534     krb5_safe *safe;
1535     krb5_data *body;
1536 };
1537 krb5_error_code
1538 encode_krb5_safe_with_body(const struct krb5_safe_with_body *rep,
1539                            krb5_data **code);
1540
1541 krb5_error_code
1542 encode_krb5_priv(const krb5_priv *rep, krb5_data **code);
1543
1544 krb5_error_code
1545 encode_krb5_enc_priv_part(const krb5_priv_enc_part *rep, krb5_data **code);
1546
1547 krb5_error_code
1548 encode_krb5_cred(const krb5_cred *rep, krb5_data **code);
1549 krb5_error_code
1550 encode_krb5_checksum(const krb5_checksum *, krb5_data **);
1551
1552 krb5_error_code
1553 encode_krb5_enc_cred_part(const krb5_cred_enc_part *rep, krb5_data **code);
1554
1555 krb5_error_code
1556 encode_krb5_error(const krb5_error *rep, krb5_data **code);
1557
1558 krb5_error_code
1559 encode_krb5_authdata(krb5_authdata *const *rep, krb5_data **code);
1560
1561 krb5_error_code
1562 encode_krb5_padata_sequence(krb5_pa_data *const *rep, krb5_data **code);
1563
1564 krb5_error_code
1565 encode_krb5_typed_data(krb5_pa_data *const *rep, krb5_data **code);
1566
1567 krb5_error_code
1568 encode_krb5_etype_info(krb5_etype_info_entry *const *, krb5_data **code);
1569
1570 krb5_error_code
1571 encode_krb5_etype_info2(krb5_etype_info_entry *const *, krb5_data **code);
1572
1573 krb5_error_code
1574 encode_krb5_pa_enc_ts(const krb5_pa_enc_ts *, krb5_data **);
1575
1576 krb5_error_code
1577 encode_krb5_sam_challenge_2(const krb5_sam_challenge_2 * , krb5_data **);
1578
1579 krb5_error_code
1580 encode_krb5_sam_challenge_2_body(const krb5_sam_challenge_2_body *,
1581                                  krb5_data **);
1582
1583 krb5_error_code
1584 encode_krb5_enc_sam_response_enc_2(const krb5_enc_sam_response_enc_2 *,
1585                                    krb5_data **);
1586
1587 krb5_error_code
1588 encode_krb5_sam_response_2(const krb5_sam_response_2 * , krb5_data **);
1589
1590 struct krb5_setpw_req {
1591     krb5_principal target;
1592     krb5_data password;
1593 };
1594 krb5_error_code
1595 encode_krb5_setpw_req(const struct krb5_setpw_req *rep, krb5_data **code);
1596
1597 krb5_error_code
1598 encode_krb5_pa_for_user(const krb5_pa_for_user *, krb5_data **);
1599
1600 krb5_error_code
1601 encode_krb5_s4u_userid(const krb5_s4u_userid *, krb5_data **);
1602
1603 krb5_error_code
1604 encode_krb5_pa_s4u_x509_user(const krb5_pa_s4u_x509_user *, krb5_data **);
1605
1606 krb5_error_code
1607 encode_krb5_pa_pac_req(const krb5_pa_pac_req *, krb5_data **);
1608
1609 krb5_error_code
1610 encode_krb5_etype_list(const krb5_etype_list * , krb5_data **);
1611
1612 krb5_error_code
1613 encode_krb5_pa_fx_fast_request(const krb5_fast_armored_req *, krb5_data **);
1614
1615 krb5_error_code
1616 encode_krb5_fast_req(const krb5_fast_req *, krb5_data **);
1617
1618 krb5_error_code
1619 encode_krb5_pa_fx_fast_reply(const krb5_enc_data *, krb5_data **);
1620
1621 krb5_error_code
1622 encode_krb5_iakerb_header(const krb5_iakerb_header *, krb5_data **);
1623
1624 krb5_error_code
1625 encode_krb5_iakerb_finished(const krb5_iakerb_finished *, krb5_data **);
1626
1627 krb5_error_code
1628 encode_krb5_fast_response(const krb5_fast_response *, krb5_data **);
1629
1630 krb5_error_code
1631 encode_krb5_ad_kdcissued(const krb5_ad_kdcissued *, krb5_data **);
1632
1633 krb5_error_code
1634 encode_krb5_ad_signedpath(const krb5_ad_signedpath *, krb5_data **);
1635
1636 krb5_error_code
1637 encode_krb5_ad_signedpath_data(const krb5_ad_signedpath_data *, krb5_data **);
1638
1639 /*************************************************************************
1640  * End of prototypes for krb5_encode.c
1641  *************************************************************************/
1642
1643 krb5_error_code
1644 decode_krb5_sam_challenge_2(const krb5_data *, krb5_sam_challenge_2 **);
1645
1646 krb5_error_code
1647 decode_krb5_sam_challenge_2_body(const krb5_data *,
1648                                  krb5_sam_challenge_2_body **);
1649
1650 krb5_error_code
1651 decode_krb5_enc_sam_response_enc_2(const krb5_data *,
1652                                    krb5_enc_sam_response_enc_2 **);
1653
1654 krb5_error_code
1655 decode_krb5_sam_response_2(const krb5_data *, krb5_sam_response_2 **);
1656
1657
1658 /*************************************************************************
1659  * Prototypes for krb5_decode.c
1660  *************************************************************************/
1661 /*
1662   krb5_error_code decode_krb5_structure(const krb5_data *code,
1663   krb5_structure **rep);
1664
1665   requires  Expects **rep to not have been allocated;
1666   a new *rep is allocated regardless of the old value.
1667   effects   Decodes *code into **rep.
1668   Returns ENOMEM if memory is exhausted.
1669   Returns asn1 and krb5 errors.
1670 */
1671
1672 krb5_error_code
1673 decode_krb5_authenticator(const krb5_data *code, krb5_authenticator **rep);
1674
1675 krb5_error_code
1676 decode_krb5_ticket(const krb5_data *code, krb5_ticket **rep);
1677
1678 krb5_error_code
1679 decode_krb5_encryption_key(const krb5_data *output, krb5_keyblock **rep);
1680
1681 krb5_error_code
1682 decode_krb5_enc_tkt_part(const krb5_data *output, krb5_enc_tkt_part **rep);
1683
1684 krb5_error_code
1685 decode_krb5_enc_kdc_rep_part(const krb5_data *output,
1686                              krb5_enc_kdc_rep_part **rep);
1687
1688 krb5_error_code
1689 decode_krb5_as_rep(const krb5_data *output, krb5_kdc_rep **rep);
1690
1691 krb5_error_code
1692 decode_krb5_tgs_rep(const krb5_data *output, krb5_kdc_rep **rep);
1693
1694 krb5_error_code
1695 decode_krb5_ap_req(const krb5_data *output, krb5_ap_req **rep);
1696
1697 krb5_error_code
1698 decode_krb5_ap_rep(const krb5_data *output, krb5_ap_rep **rep);
1699
1700 krb5_error_code
1701 decode_krb5_ap_rep_enc_part(const krb5_data *output,
1702                             krb5_ap_rep_enc_part **rep);
1703
1704 krb5_error_code
1705 decode_krb5_as_req(const krb5_data *output, krb5_kdc_req **rep);
1706
1707 krb5_error_code
1708 decode_krb5_tgs_req(const krb5_data *output, krb5_kdc_req **rep);
1709
1710 krb5_error_code
1711 decode_krb5_kdc_req_body(const krb5_data *output, krb5_kdc_req **rep);
1712
1713 krb5_error_code
1714 decode_krb5_safe(const krb5_data *output, krb5_safe **rep);
1715
1716 krb5_error_code
1717 decode_krb5_safe_with_body(const krb5_data *output, krb5_safe **rep,
1718                            krb5_data **body);
1719
1720 krb5_error_code
1721 decode_krb5_priv(const krb5_data *output, krb5_priv **rep);
1722
1723 krb5_error_code
1724 decode_krb5_enc_priv_part(const krb5_data *output, krb5_priv_enc_part **rep);
1725 krb5_error_code
1726 decode_krb5_checksum(const krb5_data *, krb5_checksum **);
1727
1728 krb5_error_code
1729 decode_krb5_cred(const krb5_data *output, krb5_cred **rep);
1730
1731 krb5_error_code
1732 decode_krb5_enc_cred_part(const krb5_data *output, krb5_cred_enc_part **rep);
1733
1734 krb5_error_code
1735 decode_krb5_error(const krb5_data *output, krb5_error **rep);
1736
1737 krb5_error_code
1738 decode_krb5_authdata(const krb5_data *output, krb5_authdata ***rep);
1739
1740 krb5_error_code
1741 decode_krb5_padata_sequence(const krb5_data *output, krb5_pa_data ***rep);
1742
1743 krb5_error_code
1744 decode_krb5_typed_data(const krb5_data *, krb5_pa_data ***);
1745
1746 krb5_error_code
1747 decode_krb5_etype_info(const krb5_data *output, krb5_etype_info_entry ***rep);
1748
1749 krb5_error_code
1750 decode_krb5_etype_info2(const krb5_data *output, krb5_etype_info_entry ***rep);
1751
1752 krb5_error_code
1753 decode_krb5_enc_data(const krb5_data *output, krb5_enc_data **rep);
1754
1755 krb5_error_code
1756 decode_krb5_pa_enc_ts(const krb5_data *output, krb5_pa_enc_ts **rep);
1757
1758 krb5_error_code
1759 decode_krb5_setpw_req(const krb5_data *, krb5_data **, krb5_principal *);
1760
1761 krb5_error_code
1762 decode_krb5_pa_for_user(const krb5_data *, krb5_pa_for_user **);
1763
1764 krb5_error_code
1765 decode_krb5_pa_s4u_x509_user(const krb5_data *, krb5_pa_s4u_x509_user **);
1766
1767 krb5_error_code
1768 decode_krb5_pa_pac_req(const krb5_data *, krb5_pa_pac_req **);
1769
1770 krb5_error_code
1771 decode_krb5_etype_list(const krb5_data *, krb5_etype_list **);
1772
1773 krb5_error_code
1774 decode_krb5_pa_fx_fast_request(const krb5_data *, krb5_fast_armored_req **);
1775
1776 krb5_error_code
1777 decode_krb5_fast_req(const krb5_data *, krb5_fast_req **);
1778
1779 krb5_error_code
1780 decode_krb5_pa_fx_fast_reply(const krb5_data *, krb5_enc_data **);
1781
1782 krb5_error_code
1783 decode_krb5_fast_response(const krb5_data *, krb5_fast_response **);
1784
1785 krb5_error_code
1786 decode_krb5_ad_kdcissued(const krb5_data *, krb5_ad_kdcissued **);
1787
1788 krb5_error_code
1789 decode_krb5_ad_signedpath(const krb5_data *, krb5_ad_signedpath **);
1790
1791 krb5_error_code
1792 decode_krb5_iakerb_header(const krb5_data *, krb5_iakerb_header **);
1793
1794 krb5_error_code
1795 decode_krb5_iakerb_finished(const krb5_data *, krb5_iakerb_finished **);
1796
1797 struct _krb5_key_data;          /* kdb.h */
1798
1799 struct ldap_seqof_key_data {
1800     krb5_int32 mkvno;           /* Master key version number */
1801     krb5_int16 kvno;            /* kvno of key_data elements (all the same) */
1802     struct _krb5_key_data *key_data;
1803     krb5_int16 n_key_data;
1804 };
1805 typedef struct ldap_seqof_key_data ldap_seqof_key_data;
1806
1807 krb5_error_code
1808 krb5int_ldap_encode_sequence_of_keys(const ldap_seqof_key_data *val,
1809                                      krb5_data **code);
1810
1811 krb5_error_code
1812 krb5int_ldap_decode_sequence_of_keys(const krb5_data *in,
1813                                      ldap_seqof_key_data **rep);
1814
1815 /*************************************************************************
1816  * End of prototypes for krb5_decode.c
1817  *************************************************************************/
1818
1819 #endif /* KRB5_ASN1__ */
1820 /*
1821  * End "asn1.h"
1822  */
1823
1824
1825 /*
1826  * Internal krb5 library routines
1827  */
1828 krb5_error_code
1829 krb5_encrypt_tkt_part(krb5_context, const krb5_keyblock *, krb5_ticket *);
1830
1831 krb5_error_code
1832 krb5_encode_kdc_rep(krb5_context, krb5_msgtype, const krb5_enc_kdc_rep_part *,
1833                     int using_subkey, const krb5_keyblock *, krb5_kdc_rep *,
1834                     krb5_data ** );
1835
1836 /*
1837  * [De]Serialization Handle and operations.
1838  */
1839 struct __krb5_serializer {
1840     krb5_magic          odtype;
1841     krb5_error_code     (*sizer) (krb5_context,
1842                                   krb5_pointer,
1843                                   size_t *);
1844     krb5_error_code     (*externalizer) (krb5_context,
1845                                          krb5_pointer,
1846                                          krb5_octet **,
1847                                          size_t *);
1848     krb5_error_code     (*internalizer) (krb5_context,
1849                                          krb5_pointer *,
1850                                          krb5_octet **,
1851                                          size_t *);
1852 };
1853 typedef const struct __krb5_serializer * krb5_ser_handle;
1854 typedef struct __krb5_serializer krb5_ser_entry;
1855
1856 krb5_ser_handle krb5_find_serializer(krb5_context, krb5_magic);
1857 krb5_error_code krb5_register_serializer(krb5_context, const krb5_ser_entry *);
1858
1859 /* Determine the external size of a particular opaque structure */
1860 krb5_error_code KRB5_CALLCONV
1861 krb5_size_opaque(krb5_context, krb5_magic, krb5_pointer, size_t *);
1862
1863 /* Serialize the structure into a buffer */
1864 krb5_error_code KRB5_CALLCONV
1865 krb5_externalize_opaque(krb5_context, krb5_magic, krb5_pointer, krb5_octet **,
1866                         size_t *);
1867
1868 /* Deserialize the structure from a buffer */
1869 krb5_error_code KRB5_CALLCONV
1870 krb5_internalize_opaque(krb5_context, krb5_magic, krb5_pointer *,
1871                         krb5_octet **, size_t *);
1872
1873 /* Serialize data into a buffer */
1874 krb5_error_code
1875 krb5_externalize_data(krb5_context, krb5_pointer, krb5_octet **, size_t *);
1876 /*
1877  * Initialization routines.
1878  */
1879
1880 /* Initialize serialization for krb5_[os_]context */
1881 krb5_error_code KRB5_CALLCONV krb5_ser_context_init(krb5_context);
1882
1883 /* Initialize serialization for krb5_auth_context */
1884 krb5_error_code KRB5_CALLCONV krb5_ser_auth_context_init(krb5_context);
1885
1886 /* Initialize serialization for krb5_keytab */
1887 krb5_error_code KRB5_CALLCONV krb5_ser_keytab_init(krb5_context);
1888
1889 /* Initialize serialization for krb5_ccache */
1890 krb5_error_code KRB5_CALLCONV krb5_ser_ccache_init(krb5_context);
1891
1892 /* Initialize serialization for krb5_rcache */
1893 krb5_error_code KRB5_CALLCONV krb5_ser_rcache_init(krb5_context);
1894
1895 /* [De]serialize 4-byte integer */
1896 krb5_error_code KRB5_CALLCONV
1897 krb5_ser_pack_int32(krb5_int32, krb5_octet **, size_t *);
1898
1899 krb5_error_code KRB5_CALLCONV
1900 krb5_ser_unpack_int32(krb5_int32 *, krb5_octet **, size_t *);
1901
1902 /* [De]serialize 8-byte integer */
1903 krb5_error_code KRB5_CALLCONV
1904 krb5_ser_pack_int64(krb5_int64, krb5_octet **, size_t *);
1905
1906 krb5_error_code KRB5_CALLCONV
1907 krb5_ser_unpack_int64(krb5_int64 *, krb5_octet **, size_t *);
1908
1909 /* [De]serialize byte string */
1910 krb5_error_code KRB5_CALLCONV
1911 krb5_ser_pack_bytes(krb5_octet *, size_t, krb5_octet **, size_t *);
1912
1913 krb5_error_code KRB5_CALLCONV
1914 krb5_ser_unpack_bytes(krb5_octet *, size_t, krb5_octet **, size_t *);
1915
1916 krb5_error_code KRB5_CALLCONV
1917 krb5int_cc_default(krb5_context, krb5_ccache *);
1918
1919 krb5_error_code KRB5_CALLCONV
1920 krb5_cc_retrieve_cred_default(krb5_context, krb5_ccache, krb5_flags,
1921                               krb5_creds *, krb5_creds *);
1922
1923 krb5_boolean KRB5_CALLCONV
1924 krb5_creds_compare(krb5_context in_context, krb5_creds *in_creds,
1925                    krb5_creds *in_compare_creds);
1926
1927 void
1928 krb5int_set_prompt_types(krb5_context, krb5_prompt_type *);
1929
1930 krb5_error_code
1931 krb5int_generate_and_save_subkey(krb5_context, krb5_auth_context,
1932                                  krb5_keyblock * /* Old keyblock, not new!  */,
1933                                  krb5_enctype);
1934
1935 struct srv_dns_entry {
1936     struct srv_dns_entry *next;
1937     int priority;
1938     int weight;
1939     unsigned short port;
1940     char *host;
1941 };
1942
1943 #define MAX_DNS_NAMELEN (15*(MAXHOSTNAMELEN + 1)+1)
1944
1945 #ifdef KRB5_DNS_LOOKUP
1946 krb5_error_code
1947 krb5int_make_srv_query_realm(const krb5_data *realm,
1948                              const char *service,
1949                              const char *protocol,
1950                              struct srv_dns_entry **answers);
1951 void krb5int_free_srv_dns_data(struct srv_dns_entry *);
1952 #endif
1953
1954 /* value to use when requesting a keytab entry and KVNO doesn't matter */
1955 #define IGNORE_VNO 0
1956 /* value to use when requesting a keytab entry and enctype doesn't matter */
1957 #define IGNORE_ENCTYPE 0
1958
1959 /*
1960  * Convenience function for structure magic number
1961  */
1962 #define KRB5_VERIFY_MAGIC(structure,magic_number)                       \
1963     if ((structure)->magic != (magic_number)) return (magic_number);
1964
1965 /* to keep lint happy */
1966 #define krb5_xfree(val) free((char *)(val))
1967
1968 /* To keep happy libraries which are (for now) accessing internal stuff */
1969
1970 /* Make sure to increment by one when changing the struct */
1971 #define KRB5INT_ACCESS_STRUCT_VERSION 21
1972
1973 typedef struct _krb5int_access {
1974     krb5_error_code (*auth_con_get_subkey_enctype)(krb5_context,
1975                                                    krb5_auth_context,
1976                                                    krb5_enctype *);
1977
1978     krb5_error_code (*clean_hostname)(krb5_context, const char *, char *,
1979                                       size_t);
1980
1981     krb5_error_code (*mandatory_cksumtype)(krb5_context, krb5_enctype,
1982                                            krb5_cksumtype *);
1983     krb5_error_code (KRB5_CALLCONV *ser_pack_int64)(krb5_int64, krb5_octet **,
1984                                                     size_t *);
1985     krb5_error_code (KRB5_CALLCONV *ser_unpack_int64)(krb5_int64 *,
1986                                                       krb5_octet **, size_t *);
1987
1988     /* Used for KDB LDAP back end.  */
1989     krb5_error_code
1990     (*asn1_ldap_encode_sequence_of_keys)(const ldap_seqof_key_data *val,
1991                                          krb5_data **code);
1992
1993     krb5_error_code
1994     (*asn1_ldap_decode_sequence_of_keys)(krb5_data *in,
1995                                          ldap_seqof_key_data **);
1996
1997     /*
1998      * pkinit asn.1 encode/decode functions
1999      */
2000     krb5_error_code
2001     (*encode_krb5_auth_pack)(const krb5_auth_pack *rep, krb5_data **code);
2002
2003     krb5_error_code
2004     (*encode_krb5_auth_pack_draft9)(const krb5_auth_pack_draft9 *rep,
2005                                     krb5_data **code);
2006
2007     krb5_error_code
2008     (*encode_krb5_kdc_dh_key_info)(const krb5_kdc_dh_key_info *rep,
2009                                    krb5_data **code);
2010
2011     krb5_error_code
2012     (*encode_krb5_pa_pk_as_rep)(const krb5_pa_pk_as_rep *rep,
2013                                 krb5_data **code);
2014
2015     krb5_error_code
2016     (*encode_krb5_pa_pk_as_rep_draft9)(const krb5_pa_pk_as_rep_draft9 *rep,
2017                                        krb5_data **code);
2018
2019     krb5_error_code
2020     (*encode_krb5_pa_pk_as_req)(const krb5_pa_pk_as_req *rep,
2021                                 krb5_data **code);
2022
2023     krb5_error_code
2024     (*encode_krb5_pa_pk_as_req_draft9)(const krb5_pa_pk_as_req_draft9 *rep,
2025                                        krb5_data **code);
2026
2027     krb5_error_code
2028     (*encode_krb5_reply_key_pack)(const krb5_reply_key_pack *,
2029                                   krb5_data **code);
2030
2031     krb5_error_code
2032     (*encode_krb5_reply_key_pack_draft9)(const krb5_reply_key_pack_draft9 *,
2033                                          krb5_data **code);
2034
2035     krb5_error_code
2036     (*encode_krb5_td_dh_parameters)(const krb5_algorithm_identifier **,
2037                                     krb5_data **code);
2038
2039     krb5_error_code
2040     (*encode_krb5_td_trusted_certifiers)(const
2041                                          krb5_external_principal_identifier **,
2042                                          krb5_data **code);
2043
2044     krb5_error_code
2045     (*decode_krb5_auth_pack)(const krb5_data *, krb5_auth_pack **);
2046
2047     krb5_error_code
2048     (*decode_krb5_auth_pack_draft9)(const krb5_data *,
2049                                     krb5_auth_pack_draft9 **);
2050
2051     krb5_error_code
2052     (*decode_krb5_pa_pk_as_req)(const krb5_data *, krb5_pa_pk_as_req **);
2053
2054     krb5_error_code
2055     (*decode_krb5_pa_pk_as_req_draft9)(const krb5_data *,
2056                                        krb5_pa_pk_as_req_draft9 **);
2057
2058     krb5_error_code
2059     (*decode_krb5_pa_pk_as_rep)(const krb5_data *, krb5_pa_pk_as_rep **);
2060
2061     krb5_error_code
2062     (*decode_krb5_kdc_dh_key_info)(const krb5_data *, krb5_kdc_dh_key_info **);
2063
2064     krb5_error_code
2065     (*decode_krb5_principal_name)(const krb5_data *, krb5_principal_data **);
2066
2067     krb5_error_code
2068     (*decode_krb5_reply_key_pack)(const krb5_data *, krb5_reply_key_pack **);
2069
2070     krb5_error_code
2071     (*decode_krb5_reply_key_pack_draft9)(const krb5_data *,
2072                                          krb5_reply_key_pack_draft9 **);
2073
2074     krb5_error_code
2075     (*decode_krb5_td_dh_parameters)(const krb5_data *,
2076                                     krb5_algorithm_identifier ***);
2077
2078     krb5_error_code
2079     (*decode_krb5_td_trusted_certifiers)(const krb5_data *,
2080                                          krb5_external_principal_identifier
2081                                          ***);
2082
2083     krb5_error_code
2084     (*encode_krb5_kdc_req_body)(const krb5_kdc_req *rep, krb5_data **code);
2085
2086     void
2087     (KRB5_CALLCONV *free_kdc_req)(krb5_context, krb5_kdc_req * );
2088     void
2089     (*set_prompt_types)(krb5_context, krb5_prompt_type *);
2090 } krb5int_access;
2091
2092 #define KRB5INT_ACCESS_VERSION                                          \
2093     (((krb5_int32)((sizeof(krb5int_access) & 0xFFFF) |                  \
2094                    (KRB5INT_ACCESS_STRUCT_VERSION << 16))) & 0xFFFFFFFF)
2095
2096 krb5_error_code KRB5_CALLCONV
2097 krb5int_accessor(krb5int_access*, krb5_int32);
2098
2099 /* Ick -- some krb524 and krb4 support placed in the krb5 library,
2100    because AFS (and potentially other applications?) use the krb4
2101    object as an opaque token, which (in some implementations) is not
2102    in fact a krb4 ticket, so we don't want to drag in the krb4 support
2103    just to enable this.  */
2104
2105 #define KRB524_SERVICE "krb524"
2106 #define KRB524_PORT 4444
2107
2108 /* temporary -- this should be under lib/krb5/ccache somewhere */
2109
2110 struct _krb5_ccache {
2111     krb5_magic magic;
2112     const struct _krb5_cc_ops *ops;
2113     krb5_pointer data;
2114 };
2115
2116 /*
2117  * Per-type ccache cursor.
2118  */
2119 struct krb5_cc_ptcursor_s {
2120     const struct _krb5_cc_ops *ops;
2121     krb5_pointer data;
2122 };
2123 typedef struct krb5_cc_ptcursor_s *krb5_cc_ptcursor;
2124
2125 struct _krb5_cc_ops {
2126     krb5_magic magic;
2127     char *prefix;
2128     const char * (KRB5_CALLCONV *get_name)(krb5_context, krb5_ccache);
2129     krb5_error_code (KRB5_CALLCONV *resolve)(krb5_context, krb5_ccache *,
2130                                              const char *);
2131     krb5_error_code (KRB5_CALLCONV *gen_new)(krb5_context, krb5_ccache *);
2132     krb5_error_code (KRB5_CALLCONV *init)(krb5_context, krb5_ccache,
2133                                           krb5_principal);
2134     krb5_error_code (KRB5_CALLCONV *destroy)(krb5_context, krb5_ccache);
2135     krb5_error_code (KRB5_CALLCONV *close)(krb5_context, krb5_ccache);
2136     krb5_error_code (KRB5_CALLCONV *store)(krb5_context, krb5_ccache,
2137                                            krb5_creds *);
2138     krb5_error_code (KRB5_CALLCONV *retrieve)(krb5_context, krb5_ccache,
2139                                               krb5_flags, krb5_creds *,
2140                                               krb5_creds *);
2141     krb5_error_code (KRB5_CALLCONV *get_princ)(krb5_context, krb5_ccache,
2142                                                krb5_principal *);
2143     krb5_error_code (KRB5_CALLCONV *get_first)(krb5_context, krb5_ccache,
2144                                                krb5_cc_cursor *);
2145     krb5_error_code (KRB5_CALLCONV *get_next)(krb5_context, krb5_ccache,
2146                                               krb5_cc_cursor *, krb5_creds *);
2147     krb5_error_code (KRB5_CALLCONV *end_get)(krb5_context, krb5_ccache,
2148                                              krb5_cc_cursor *);
2149     krb5_error_code (KRB5_CALLCONV *remove_cred)(krb5_context, krb5_ccache,
2150                                                  krb5_flags, krb5_creds *);
2151     krb5_error_code (KRB5_CALLCONV *set_flags)(krb5_context, krb5_ccache,
2152                                                krb5_flags);
2153     krb5_error_code (KRB5_CALLCONV *get_flags)(krb5_context, krb5_ccache,
2154                                                krb5_flags *);
2155     krb5_error_code (KRB5_CALLCONV *ptcursor_new)(krb5_context,
2156                                                   krb5_cc_ptcursor *);
2157     krb5_error_code (KRB5_CALLCONV *ptcursor_next)(krb5_context,
2158                                                    krb5_cc_ptcursor,
2159                                                    krb5_ccache *);
2160     krb5_error_code (KRB5_CALLCONV *ptcursor_free)(krb5_context,
2161                                                    krb5_cc_ptcursor *);
2162     krb5_error_code (KRB5_CALLCONV *move)(krb5_context, krb5_ccache,
2163                                           krb5_ccache);
2164     krb5_error_code (KRB5_CALLCONV *lastchange)(krb5_context,
2165                                                 krb5_ccache, krb5_timestamp *);
2166     krb5_error_code (KRB5_CALLCONV *wasdefault)(krb5_context, krb5_ccache,
2167                                                 krb5_timestamp *);
2168     krb5_error_code (KRB5_CALLCONV *lock)(krb5_context, krb5_ccache);
2169     krb5_error_code (KRB5_CALLCONV *unlock)(krb5_context, krb5_ccache);
2170     krb5_error_code (KRB5_CALLCONV *switch_to)(krb5_context, krb5_ccache);
2171 };
2172
2173 extern const krb5_cc_ops *krb5_cc_dfl_ops;
2174
2175 krb5_error_code
2176 krb5int_cc_os_default_name(krb5_context context, char **name);
2177
2178 typedef struct _krb5_donot_replay {
2179     krb5_magic magic;
2180     krb5_ui_4 hash;
2181     char *server;                       /* null-terminated */
2182     char *client;                       /* null-terminated */
2183     char *msghash;                      /* null-terminated */
2184     krb5_int32 cusec;
2185     krb5_timestamp ctime;
2186 } krb5_donot_replay;
2187
2188 krb5_error_code krb5_rc_default(krb5_context, krb5_rcache *);
2189 krb5_error_code krb5_rc_resolve_type(krb5_context, krb5_rcache *,char *);
2190 krb5_error_code krb5_rc_resolve_full(krb5_context, krb5_rcache *,char *);
2191 char *krb5_rc_get_type(krb5_context, krb5_rcache);
2192 char *krb5_rc_default_type(krb5_context);
2193 char *krb5_rc_default_name(krb5_context);
2194 krb5_error_code krb5_auth_to_rep(krb5_context, krb5_tkt_authent *,
2195                                  krb5_donot_replay *);
2196 krb5_error_code krb5_rc_hash_message(krb5_context context,
2197                                      const krb5_data *message, char **out);
2198
2199 krb5_error_code KRB5_CALLCONV
2200 krb5_rc_initialize(krb5_context, krb5_rcache, krb5_deltat);
2201
2202 krb5_error_code KRB5_CALLCONV
2203 krb5_rc_recover_or_initialize(krb5_context, krb5_rcache,krb5_deltat);
2204
2205 krb5_error_code KRB5_CALLCONV
2206 krb5_rc_recover(krb5_context, krb5_rcache);
2207
2208 krb5_error_code KRB5_CALLCONV
2209 krb5_rc_destroy(krb5_context, krb5_rcache);
2210
2211 krb5_error_code KRB5_CALLCONV
2212 krb5_rc_close(krb5_context, krb5_rcache);
2213
2214 krb5_error_code KRB5_CALLCONV
2215 krb5_rc_store(krb5_context, krb5_rcache, krb5_donot_replay *);
2216
2217 krb5_error_code KRB5_CALLCONV
2218 krb5_rc_expunge(krb5_context, krb5_rcache);
2219
2220 krb5_error_code KRB5_CALLCONV
2221 krb5_rc_get_lifespan(krb5_context, krb5_rcache,krb5_deltat *);
2222
2223 char *KRB5_CALLCONV
2224 krb5_rc_get_name(krb5_context, krb5_rcache);
2225
2226 krb5_error_code KRB5_CALLCONV
2227 krb5_rc_resolve(krb5_context, krb5_rcache, char *);
2228
2229 /*
2230  * This structure was exposed and used in macros in krb5 1.2, so do not
2231  * change its ABI.
2232  */
2233 typedef struct _krb5_kt_ops {
2234     krb5_magic magic;
2235     char *prefix;
2236
2237     /* routines always present */
2238     krb5_error_code (KRB5_CALLCONV *resolve)(krb5_context, const char *,
2239                                              krb5_keytab *);
2240     krb5_error_code (KRB5_CALLCONV *get_name)(krb5_context, krb5_keytab,
2241                                               char *, unsigned int);
2242     krb5_error_code (KRB5_CALLCONV *close)(krb5_context, krb5_keytab);
2243     krb5_error_code (KRB5_CALLCONV *get)(krb5_context, krb5_keytab,
2244                                          krb5_const_principal, krb5_kvno,
2245                                          krb5_enctype, krb5_keytab_entry *);
2246     krb5_error_code (KRB5_CALLCONV *start_seq_get)(krb5_context, krb5_keytab,
2247                                                    krb5_kt_cursor *);
2248     krb5_error_code (KRB5_CALLCONV *get_next)(krb5_context, krb5_keytab,
2249                                               krb5_keytab_entry *,
2250                                               krb5_kt_cursor *);
2251     krb5_error_code (KRB5_CALLCONV *end_get)(krb5_context, krb5_keytab,
2252                                              krb5_kt_cursor *);
2253     /* routines to be included on extended version (write routines) */
2254     krb5_error_code (KRB5_CALLCONV *add)(krb5_context, krb5_keytab,
2255                                          krb5_keytab_entry *);
2256     krb5_error_code (KRB5_CALLCONV *remove)(krb5_context, krb5_keytab,
2257                                             krb5_keytab_entry *);
2258
2259     /* Handle for serializer */
2260     const krb5_ser_entry *serializer;
2261 } krb5_kt_ops;
2262
2263 extern const krb5_kt_ops krb5_kt_dfl_ops;
2264
2265 extern krb5_error_code krb5int_translate_gai_error(int);
2266
2267 /* Not sure it's ready for exposure just yet.  */
2268 extern krb5_error_code
2269 krb5int_c_mandatory_cksumtype(krb5_context, krb5_enctype, krb5_cksumtype *);
2270
2271 /*
2272  * Referral definitions, debugging hooks, and subfunctions.
2273  */
2274 #define        KRB5_REFERRAL_MAXHOPS    10
2275 /* #define DEBUG_REFERRALS */
2276
2277 #ifdef DEBUG_REFERRALS
2278 void krb5int_dbgref_dump_principal(char *, krb5_principal);
2279 #endif
2280
2281 /* Common hostname-parsing code. */
2282 krb5_error_code
2283 krb5int_clean_hostname(krb5_context, const char *, char *, size_t);
2284
2285 #if 0
2286 /*
2287  * There are no IANA assignments for these enctypes or cksumtypes yet.  They
2288  * must be defined to local-use negative numbers at build time for Camellia
2289  * support to function at the moment.  If one is defined, they should all be
2290  * defined.  When IANA assignments exist, these definitions should move to the
2291  * appropriate places in krb5.hin and all CAMELLIA conditional code should be
2292  * made unconditional.
2293  *
2294  * The present code is experimental and may not be compatible with the
2295  * standardized version.
2296  */
2297 #define ENCTYPE_CAMELLIA128_CTS_CMAC -XXX /* Camellia CTS mode, 128-bit key */
2298 #define ENCTYPE_CAMELLIA256_CTS_CMAC -YYY /* Camellia CTS mode, 256-bit key */
2299 #define CKSUMTYPE_CMAC_CAMELLIA128 -XXX   /* CMAC, 128-bit Camellia key */
2300 #define CKSUMTYPE_CMAC_CAMELLIA256 -YYY   /* CMAC, 256-bit Camellia key */
2301 #endif
2302
2303 #ifdef ENCTYPE_CAMELLIA128_CTS_CMAC
2304 #define CAMELLIA
2305 #endif
2306
2307 struct _krb5_kt {       /* should move into k5-int.h */
2308     krb5_magic magic;
2309     const struct _krb5_kt_ops *ops;
2310     krb5_pointer data;
2311 };
2312
2313 krb5_error_code krb5_set_default_in_tkt_ktypes(krb5_context,
2314                                                const krb5_enctype *);
2315
2316 krb5_error_code krb5_get_default_in_tkt_ktypes(krb5_context, krb5_enctype **);
2317
2318 krb5_error_code krb5_set_default_tgs_ktypes(krb5_context,
2319                                             const krb5_enctype *);
2320
2321 krb5_error_code KRB5_CALLCONV
2322 krb5_get_tgs_ktypes(krb5_context, krb5_const_principal, krb5_enctype **);
2323
2324 void KRB5_CALLCONV krb5_free_ktypes(krb5_context, krb5_enctype *);
2325
2326 krb5_boolean krb5_is_permitted_enctype(krb5_context, krb5_enctype);
2327
2328 typedef struct
2329 {
2330     krb5_enctype *etype;
2331     krb5_boolean *etype_ok;
2332     krb5_int32 etype_count;
2333 } krb5_etypes_permitted;
2334
2335 krb5_boolean krb5_is_permitted_enctype_ext(krb5_context,
2336                                            krb5_etypes_permitted *);
2337
2338 krb5_boolean KRB5_CALLCONV krb5int_c_weak_enctype(krb5_enctype);
2339
2340 krb5_error_code krb5_kdc_rep_decrypt_proc(krb5_context, const krb5_keyblock *,
2341                                           krb5_const_pointer, krb5_kdc_rep *);
2342 krb5_error_code KRB5_CALLCONV krb5_decrypt_tkt_part(krb5_context,
2343                                                     const krb5_keyblock *,
2344                                                     krb5_ticket * );
2345
2346 krb5_error_code krb5_get_cred_via_tkt(krb5_context, krb5_creds *, krb5_flags,
2347                                       krb5_address *const *, krb5_creds *,
2348                                       krb5_creds **);
2349
2350 krb5_error_code KRB5_CALLCONV krb5_copy_addr(krb5_context,
2351                                              const krb5_address *,
2352                                              krb5_address **);
2353
2354 void krb5_init_ets(krb5_context);
2355 void krb5_free_ets(krb5_context);
2356 krb5_error_code krb5_generate_subkey(krb5_context, const krb5_keyblock *,
2357                                      krb5_keyblock **);
2358 krb5_error_code krb5_generate_subkey_extended(krb5_context,
2359                                               const krb5_keyblock *,
2360                                               krb5_enctype, krb5_keyblock **);
2361 krb5_error_code krb5_generate_seq_number(krb5_context, const krb5_keyblock *,
2362                                          krb5_ui_4 *);
2363
2364 krb5_error_code KRB5_CALLCONV krb5_kt_register(krb5_context,
2365                                                const struct _krb5_kt_ops *);
2366
2367 krb5_error_code k5_kt_get_principal(krb5_context context, krb5_keytab keytab,
2368                                     krb5_principal *princ_out);
2369
2370 krb5_error_code krb5_principal2salt_norealm(krb5_context, krb5_const_principal,
2371                                             krb5_data *);
2372
2373 unsigned int KRB5_CALLCONV krb5_get_notification_message(void);
2374
2375 /* chk_trans.c */
2376 krb5_error_code krb5_check_transited_list(krb5_context, const krb5_data *trans,
2377                                           const krb5_data *realm1,
2378                                           const krb5_data *realm2);
2379
2380 /* free_rtree.c */
2381 void krb5_free_realm_tree(krb5_context, krb5_principal *);
2382
2383 void KRB5_CALLCONV krb5_free_authenticator_contents(krb5_context,
2384                                                     krb5_authenticator *);
2385
2386 void KRB5_CALLCONV krb5_free_address(krb5_context, krb5_address *);
2387
2388 void KRB5_CALLCONV krb5_free_enc_tkt_part(krb5_context, krb5_enc_tkt_part *);
2389
2390 void KRB5_CALLCONV krb5_free_tickets(krb5_context, krb5_ticket **);
2391 void KRB5_CALLCONV krb5_free_kdc_req(krb5_context, krb5_kdc_req *);
2392 void KRB5_CALLCONV krb5_free_kdc_rep(krb5_context, krb5_kdc_rep *);
2393 void KRB5_CALLCONV krb5_free_last_req(krb5_context, krb5_last_req_entry **);
2394 void KRB5_CALLCONV krb5_free_enc_kdc_rep_part(krb5_context,
2395                                               krb5_enc_kdc_rep_part *);
2396 void KRB5_CALLCONV krb5_free_ap_req(krb5_context, krb5_ap_req *);
2397 void KRB5_CALLCONV krb5_free_ap_rep(krb5_context, krb5_ap_rep *);
2398 void KRB5_CALLCONV krb5_free_cred(krb5_context, krb5_cred *);
2399 void KRB5_CALLCONV krb5_free_cred_enc_part(krb5_context, krb5_cred_enc_part *);
2400 void KRB5_CALLCONV krb5_free_pa_data(krb5_context, krb5_pa_data **);
2401 void KRB5_CALLCONV krb5_free_tkt_authent(krb5_context, krb5_tkt_authent *);
2402 void KRB5_CALLCONV krb5_free_enc_data(krb5_context, krb5_enc_data *);
2403 krb5_error_code krb5_set_config_files(krb5_context, const char **);
2404
2405 krb5_error_code KRB5_CALLCONV krb5_get_default_config_files(char ***filenames);
2406
2407 void KRB5_CALLCONV krb5_free_config_files(char **filenames);
2408
2409 krb5_error_code krb5_rd_req_decoded(krb5_context, krb5_auth_context *,
2410                                     const krb5_ap_req *, krb5_const_principal,
2411                                     krb5_keytab, krb5_flags *, krb5_ticket **);
2412
2413 krb5_error_code krb5_rd_req_decoded_anyflag(krb5_context, krb5_auth_context *,
2414                                             const krb5_ap_req *,
2415                                             krb5_const_principal, krb5_keytab,
2416                                             krb5_flags *, krb5_ticket **);
2417
2418 krb5_error_code KRB5_CALLCONV
2419 krb5_cc_register(krb5_context, const krb5_cc_ops *, krb5_boolean );
2420
2421 krb5_error_code krb5_walk_realm_tree(krb5_context, const krb5_data *,
2422                                      const krb5_data *, krb5_principal **,
2423                                      int);
2424
2425 krb5_error_code
2426 k5_client_realm_path(krb5_context context, const krb5_data *client,
2427                      const krb5_data *server, krb5_data **rpath_out);
2428
2429 krb5_error_code
2430 krb5_auth_con_set_safe_cksumtype(krb5_context, krb5_auth_context,
2431                                  krb5_cksumtype);
2432
2433 krb5_error_code krb5_auth_con_setivector(krb5_context, krb5_auth_context,
2434                                          krb5_pointer);
2435
2436 krb5_error_code krb5_auth_con_getivector(krb5_context, krb5_auth_context,
2437                                          krb5_pointer *);
2438
2439 krb5_error_code krb5_auth_con_setpermetypes(krb5_context, krb5_auth_context,
2440                                             const krb5_enctype *);
2441
2442 krb5_error_code krb5_auth_con_getpermetypes(krb5_context, krb5_auth_context,
2443                                             krb5_enctype **);
2444
2445 krb5_error_code krb5_auth_con_get_subkey_enctype(krb5_context context,
2446                                                  krb5_auth_context,
2447                                                  krb5_enctype *);
2448
2449 krb5_error_code
2450 krb5_auth_con_get_authdata_context(krb5_context context,
2451                                    krb5_auth_context auth_context,
2452                                    krb5_authdata_context *ad_context);
2453
2454 krb5_error_code
2455 krb5_auth_con_set_authdata_context(krb5_context context,
2456                                    krb5_auth_context auth_context,
2457                                    krb5_authdata_context ad_context);
2458
2459 krb5_error_code KRB5_CALLCONV
2460 krb5int_server_decrypt_ticket_keyblock(krb5_context context,
2461                                        const krb5_keyblock *key,
2462                                        krb5_ticket  *ticket);
2463
2464 krb5_error_code krb5_read_message(krb5_context, krb5_pointer, krb5_data *);
2465 krb5_error_code krb5_write_message(krb5_context, krb5_pointer, krb5_data *);
2466 krb5_error_code krb5int_write_messages(krb5_context, krb5_pointer, krb5_data *,
2467                                        int);
2468 int krb5_net_read(krb5_context, int , char *, int);
2469 int krb5_net_write(krb5_context, int , const char *, int);
2470
2471 krb5_error_code KRB5_CALLCONV krb5_get_realm_domain(krb5_context,
2472                                                     const char *, char ** );
2473
2474 krb5_error_code krb5_gen_portaddr(krb5_context, const krb5_address *,
2475                                   krb5_const_pointer, krb5_address **);
2476
2477 krb5_error_code krb5_gen_replay_name(krb5_context, const krb5_address *,
2478                                      const char *, char **);
2479 krb5_error_code krb5_make_fulladdr(krb5_context, krb5_address *,
2480                                    krb5_address *, krb5_address *);
2481
2482 krb5_error_code krb5_set_debugging_time(krb5_context, krb5_timestamp,
2483                                         krb5_int32);
2484 krb5_error_code krb5_use_natural_time(krb5_context);
2485 krb5_error_code krb5_set_time_offsets(krb5_context, krb5_timestamp,
2486                                       krb5_int32);
2487
2488 /* Internal principal function used by KIM to avoid code duplication */
2489 krb5_error_code KRB5_CALLCONV
2490 krb5int_build_principal_alloc_va(krb5_context context,
2491                                  krb5_principal *princ,
2492                                  unsigned int rlen,
2493                                  const char *realm,
2494                                  const char *first,
2495                                  va_list ap);
2496
2497 /* Some data comparison and conversion functions.  */
2498 static inline int
2499 data_eq(krb5_data d1, krb5_data d2)
2500 {
2501     return (d1.length == d2.length && !memcmp(d1.data, d2.data, d1.length));
2502 }
2503
2504 static inline int
2505 data_eq_string (krb5_data d, const char *s)
2506 {
2507     return (d.length == strlen(s) && !memcmp(d.data, s, d.length));
2508 }
2509
2510 static inline krb5_data
2511 make_data(void *data, unsigned int len)
2512 {
2513     krb5_data d;
2514
2515     d.magic = KV5M_DATA;
2516     d.data = (char *) data;
2517     d.length = len;
2518     return d;
2519 }
2520
2521 static inline krb5_data
2522 empty_data()
2523 {
2524     return make_data(NULL, 0);
2525 }
2526
2527 static inline krb5_data
2528 string2data(char *str)
2529 {
2530     return make_data(str, strlen(str));
2531 }
2532
2533 static inline krb5_error_code
2534 alloc_data(krb5_data *data, unsigned int len)
2535 {
2536     /* Allocate at least one byte since zero-byte allocs may return NULL. */
2537     char *ptr = (char *) calloc((len > 0) ? len : 1, 1);
2538
2539     if (ptr == NULL)
2540         return ENOMEM;
2541     data->magic = KV5M_DATA;
2542     data->data = ptr;
2543     data->length = len;
2544     return 0;
2545 }
2546
2547 static inline int
2548 authdata_eq(krb5_authdata a1, krb5_authdata a2)
2549 {
2550     return (a1.ad_type == a2.ad_type
2551             && a1.length == a2.length
2552             && !memcmp(a1.contents, a2.contents, a1.length));
2553 }
2554
2555 /* Allocate zeroed memory; set *code to 0 on success or ENOMEM on failure. */
2556 static inline void *
2557 k5alloc(size_t len, krb5_error_code *code)
2558 {
2559     void *ptr;
2560
2561     /* Allocate at least one byte since zero-byte allocs may return NULL. */
2562     ptr = calloc((len > 0) ? len : 1, 1);
2563     *code = (ptr == NULL) ? ENOMEM : 0;
2564     return ptr;
2565 }
2566
2567 krb5_error_code KRB5_CALLCONV
2568 krb5_get_credentials_for_user(krb5_context context, krb5_flags options,
2569                               krb5_ccache ccache,
2570                               krb5_creds *in_creds,
2571                               krb5_data *cert,
2572                               krb5_creds **out_creds);
2573
2574 krb5_error_code KRB5_CALLCONV
2575 krb5_get_credentials_for_proxy(krb5_context context,
2576                                krb5_flags options,
2577                                krb5_ccache ccache,
2578                                krb5_creds *in_creds,
2579                                krb5_ticket *evidence_tkt,
2580                                krb5_creds **out_creds);
2581
2582 krb5_error_code KRB5_CALLCONV
2583 krb5int_get_authdata_containee_types(krb5_context context,
2584                                      const krb5_authdata *container,
2585                                      unsigned int *nad_types,
2586                                      krb5_authdatatype **ad_types);
2587
2588 krb5_error_code krb5int_parse_enctype_list(krb5_context context,
2589                                            const char *profkey, char *profstr,
2590                                            krb5_enctype *default_list,
2591                                            krb5_enctype **result);
2592
2593 #ifdef DEBUG_ERROR_LOCATIONS
2594 #define krb5_set_error_message(ctx, code, ...)                          \
2595     krb5_set_error_message_fl(ctx, code, __FILE__, __LINE__, __VA_ARGS__)
2596 #endif
2597 void KRB5_CALLCONV_C
2598 krb5_set_error_message_fl(krb5_context ctx, krb5_error_code code,
2599                           const char *file, int line, const char *fmt, ...)
2600 #ifdef __GNUC__
2601     __attribute__((__format__(printf,5,6)))
2602 #endif
2603     ;
2604
2605 #ifndef DISABLE_TRACING
2606 /* Do not use these functions directly; see k5-trace.h. */
2607 void krb5int_init_trace(krb5_context context);
2608 void krb5int_trace(krb5_context context, const char *fmt, ...);
2609 #endif
2610
2611 #endif /* _KRB5_INT_H */