# Kerberos Credentials Cache API draft specification

Original version 1/27/96 by Theodore Ts'o

970628 revision 2 by Steve Rothwell for the V4Cache Team (Paul Hill, Jenny Khuon, Jean Luker, Dave Detlefs, Allan Bjorklund, & Steve Rothwell)

970725 revision 3 by Steve Rothwell after initial implementation and alpha release. All changes to this document for revision 3 will appear as this paragraph does. The term "credentials cache" was previously used to mean both "the main cache" and individual "named cache"s within the main cache. I have started using the term "NC" for "named cache" to make the distinction clearer and to reduce the overloading of the word "cache".

970908 revision 4 by Steve Rothwell to incorporate changes initiated by Ted Tso. Further changes are expected in the comments for cc_create() and cc_get_change_time().

This is a draft specification for an API which provides Credentials Cache services for both Kerberos V5 and V4. The idea behind this API is that multiple Kerberos implementations can share a single Credentials Cache, mediated by this API specification. On the Microsoft Windows platform this will allow single-signon, even when more than one Kerberos DLL is in use on a particular system. Ideally, this problem could be solved by standardizing the Kerberos V5 API library interface. However, the Kerberos API is complicated enough that this would be hard to accomplish. Standardizing the interface for credentials cache access is much simpler.

This specification has been revised to allow storage and manipulation of both V4 and V5 tickets. A cache contains one or more "Named Cache"s. It is assumed that V4 and V5 credentials would each be stored in separate "Named Cache"s and not mixed in a single "Named Cache".

- Added cred version type to cc_create() & cc_open()
- New functions
  - cc_get_NC_info(), returns NC_info list for all NCs
  - cc_free_NC_info(), frees NC_info list
  - cc_get_cred_version(), returns version type of NC
  - cc_get_name(), returns name of NC
  - cc_free_name(), frees name aquired via cc_get_name()
  - cc_seq_fetch_NCs(), iterate over all NCs
- New return codes
  - CC_BAD_PARM
  - CC_ERR_CACHE_ATTACH
  - CC_ERR_CACHE_RELEASE
  - CC_ERR_CACHE_FULL
  - CC_ERR_CRED_VERSION
- Modified functions
  - cc_create(), cc_open(), pass version type of NC
  - cc_store(), cc_remove(), cc_
- New & Modified typedefs & data structures
  - cc_cred_vers { CC_CRED_VUNKNOWN, CC_CRED_V4, CC_CRED_V5 }
  - cred_ptr_union : contains pointer to credentials (either V4 or V5)
  - cred_union : contains version type and cred_ptr_union
  - modified V4Cred_type
  - enum StringToKey_Type { STK_AFS or STK_DES }
  - copies of the maximum V4 string size indicators KRB_PRINCIPAL_SZ, KRB_SERVICE_SZ, KRB_INSTANCE_SZ, KRB_REALM_SZ, ADDR_SZ

##### The proposed changes to this API are:

- DLLs to be named KrbCC16.dll and KrbCC32.dll
- Handles that were "no longer valid after this call" are set to NULL before returning to the caller. The level of indirection in the affected calls was changed as nec. (e.g. cc_close(), cc_destroy(), cc_shutdown())
- Redundant (second) "void* cc_ctx" removed from cc_lock_request() parameter list.
- A "LastCacheChangeTime" will be maintained. This will be available via the new cc_get_change_time() function.
- New function cc_get_cache_names() returns parallel null terminated lists of cache names & principals.
- New functions cc_free_name_list() and cc_free_principal_list() to free the name and principal lists returned by cc_get_cache_names.
- We hope to define Get/Set functions for all components of _cc_creds, but have not yet done so. This will allow future changes to the data structure to be transparent to the caller. This also makes backward compatibility much easier to maintain.

## Type definitions

    typedef cc_int32 cc_time_t;
    typedef cc_int32 cc_nc_flags;

    typedef struct opaque_dll_control_block_type* apiCB;
    typedef struct opaque_ccache_pointer_type* ccache_p;
    typedef struct opaque_credential_iterator_type* ccache_cit;

    typedef struct _cc_creds {
        char* 	client;	/* client's principal identifier */
        char* 	server;	/* server's principal identifier */
        cc_data 	keyblock;	/* session encryption key info */
        cc_time_t	authtime;
        cc_time_t	starttime;
        cc_time_t	endtime;
        cc_time_t	renew_till;
        cc_int32 	is_skey;	/* true if ticket is encrypted in
    				   another ticket's skey */
        cc_int32 	ticket_flags;	/* flags in ticket */
        cc_data**	addresses;	/* addrs in ticket */
        cc_data 	ticket;		/* ticket string itself */
        cc_data 	second_ticket;	/* second ticket, if related to
    				   ticket (via DUPLICATE-SKEY or
    				   ENC-TKT-IN-SKEY) */
        cc_data**	authdata;	/* authorization data */
    } cc_creds;

    typedef struct _cc_data {
        cc_int32 		type;
        cc_int32 		length;
        unsigned char*	data;
    } cc_data;

    // use an enumerated type so all callers infer the same meaning
    // these values are what krbv4win uses internally.
    enum StringToKey_Type { STK_AFS = 0, STK_DES = 1 };

    enum { MAX_V4_CRED_LEN = 1250 };

    // V4 Credentials
    typedef struct _V4credential {
        unsigned char	kversion;
        char			principal[KRB_PRINCIPAL_SZ];
        char			principal_instance[KRB_INSTANCE_SZ];
        char			service[KRB_SERVICE_SZ];
        char			service_instance[KRB_INSTANCE_SZ];
        char			realm[KRB_REALM_SZ];
        unsigned char	session_key[8];
        cc_int32		kvno;                   // k95 used BYTE skvno
        enum StringToKey_Type
    					str_to_key;				// k4 infers dynamically, k95 stores
        long			issue_date;             // k95 called this issue_time
        cc_int32		lifetime;               // k95 used LONG expiration_time
        char			address[ADDR_SZ];       // IP Address of local host
        cc_int32		ticket_sz;              // k95 used BYTE, k4 ktext uses int to hold up to 1250
        unsigned char	ticket[MAX_V4_CRED_LEN];
        unsigned long	oops;                   // zero to catch runaways
    } V4Cred_type;

    enum cc_cred_vers {
        CC_CRED_VUNKNOWN = 0,       // For validation
        CC_CRED_V4 = 1,
        CC_CRED_V5 = 2,
        CC_CRED_VMAX = 3            // For validation
    };

    typedef union cred_ptr_union_type {
        V4Cred_type* pV4Cred;
        cc_creds*    pV5Cred;
    } cred_ptr_union;

    typedef struct cred_union_type {
    #ifdef CRED_TYPE_IN_UNION
        enum cc_cred_vers cred_type;
    #endif
        cred_ptr_union cred;
    } cred_union;

#### The cc_data structure

The cc_data structure is used to store the following elements:

- keyblock
- addresses
- ticket (and second_ticket)
- authorization data

For cc_creds.ticket and cc_creds.second_ticket, the cc_data.type field MUST be zero. For the cc_creds.addresses, cc_creds.authdata, and cc_data.keyblock, the cc_data.type field should be the address type, authorization data type, and encryption type, as defined by the Kerberos V5 protocol definition.

#### cc_time_t

The cc_time_t fields are used to represent time. The time should be stored as the number of seconds since midnight GMT on January 1, 1970.

#### Principal names

Principal names are stored as C strings in this API. The C strings may contain UTF-8 encoded strings for internationalization purposes.

## Error Codes Definition

0 | CC_NOERROR | "Successful return"
1 | CC_BADNAME | "Bad credential cache name format"
2 | CC_NOTFOUD | "Matching credential not found"
3 | CC_END | "End of credential cache reached"
4 | CC_IO | "Credentials cache I/O operation failed"
5 | CC_WRITE | "Error writing to credentials cache file"
6 | CC_NOMEM | "No memory"
7 | CC_FORMAT | "Corrupted credentials cache"
8 | CC_LOCKED | "The credentials cache or NC is locked"
9 | CC_BAD_API_VERSION | "Unsupported API version"
10 | CC_NO_EXIST | "Credentials cache or NC does not exist"
11 | CC_NOT_SUPP | "Function not supported"
12 | CC_BAD_PARM | "Bad Paramter Passed"
13 | CC_ERR_CACHE_ATTACH | "Failed to attach cache"
14 | CC_ERR_CACHE_RELEASE | "Failed to release cache"
15 | CC_ERR_CACHE_FULL | "Cache FULL"
16 | CC_ERR_CRED_VERSION | "Wrong Cred Version"

## Function definitions

### Main Cache Functions

#### cc_initialize

    cc_initialize(apiCB** cc_ctx, int api_version, int* api_supported, char** vendor)

This function performs any initialization required by the API. It must be called before any other function in the API is called. The cc_ctx returned by this function should be passed to all other API functions as the first argument.

The api_version field must be CC_API_VER_1 (which is #defined to be 1).

If api_supported non-NULL, then cc_initialize will store the maximum API version number supported by the application there.

If the vendor is non-NULL, then cc_initialize will store a pointer to a read/only C string which contains a string describing the vendor which implemented the credentials cache API.

Possible error codes: CC_NOMEM, CC_BAD_API_VERSION, CC_BAD_PARM

#### cc_shutdown

    cc_shutdown(apiCB** cc_ctx)

This function performs any cleanup required by the API. cc_ctx will be NULL on return. The application program must call cc_initialize() again before making any credentials cache API calls.

Possible error codes: CC_NO_EXIST

#### cc_get_change_time

    cc_get_change_time(apiCB* cc_ctx, cc_time_t* time)

This function returns the time of the most recent change for the entire cache. There is ONE timestamp maintained for the entire cache. By maintaining a local copy the caller can deduce whether "something changed" or not. "descriptive text to talk about possible implementation problems" to be supplied by pbh

Possible error codes: CC_NO_EXIST, CC_NOMEM

#### cc_get_NC_info

    cc_get_NC_info(apiCB* cc_ctx, struct _infoNC*** ppNCi)

    typedef struct _infoNC {
    	char*	name;
    	char*	principal;
    	cc_cred_vers vers;
    } infoNC;

cc_get_NC_info is a wrapper for cc_seq_fetch_NCs, cc_get_name, cc_get_cred_version, and cc_get_principal. It returns all the information needed to uniquely identify each NC in the cache (name and cred_version) and the associated principal. Specifically it returns a null terminated list of pointers to infoNC structs. Each infoNC struct contain a pointer to the NC's name, a pointer to the the principal associated with the NC, and the version number (as an enumerated type) of the credentials stored in this NC.

The ppNCi (the entire data structure) aquired by this routine should be freed with cc_free_NC_info().

Possible error codes: CC_NO_EXIST, CC_NOMEM

#### cc_open

    cc_open(apiCB* cc_ctx, char* name, const cc_cred_vers vers, int cc_flags, ccache_p**
    ccache_pointer)

Opens an already exising NC identified by both name, and vers. It fills in the parameter **ccache_pointer with a pointer to the NC.

The list of cache names, principals, and credentials versions may be retrieved via cc_seq_fetch_NCs, cc_get_name, cc_get_cred_version, & cc_get_principal OR via cc_get_NC_info().

Possible error codes: CC_BADNAME, CC_NO_EXIST, CC_NOMEM

#### cc_create

    cc_create(apiCB* cc_ctx, char* name, char* principal, const cc_cred_vers vers,
    		int cc_flags, ccache_p** ccache_pointer)

Create a new NC. The NC is uniquely identified by the combination of it's name and the "cc_creds_vers" (i.e. which credentials version it holds). The principal given is also associated with the NC. A NULL name is not allowed. If name is non-null and there is already a NC named name, the old NC is destroyed and a new one created with that name. The NC is created with a primary principal specified by principal.(NOTE: The alpha implementation does not do this. If same name/vers is used, CC_BADNAME is returned.

The "name" of an NC is expected to be of the form <principal>[.<instance>]@<realm> (e.g. phonebone@madmagazine.com). This syntax is not currently enforced. But without a common, agreed upon naming scheme, the usefullness of a shared cache is greatly diminished. The intent is that calling code should be able to easily infer which NC to use (i.e. open or create).

An NC is intended to hold credentials for a single principal in a single realm, and for a single credentials version (i.e. V4 or V5). The cache can contain credentials for other credential versions, other realms, and even other principals, but each in a separate NC. This rule will allow callers that can only handle a single principal in a single realm to continue to work by dealing with only one NC. Callers that can deal with multiple principals, multiple realms, and/or multiple credentials versions can do so by dealing with multiple NCs. By doing it this way, the callers that are able to handle multiple principals, realms, and/or versions can do so without interfering with "differently abled" code.

The two preceding paragraphs need revision. We are working on reaching consensus and will revise when reached.

The list of cache names, principals, & cred_versions may be retrieved via cc_get_NC_info().

Possible error codes: CC_BADNAME, CC_BAD_PARM, CC_NO_EXIST, CC_NOMEM

#### cc_close

    cc_close(apiCB* cc_ctx, ccache_p** ccache_pointer)

Close the NC. The ccache_pointer related memory is deallocated, and ccache_pointer is set to NULL before being returned to caller.

Possible error codes: CC_NO_EXIST

#### cc_destroy

    cc_destroy(apiCB* cc_ctx, ccache_p** ccache_pointer)

Destroy the NC pointed to by ccache_pointer. The ccache_pointer related memory is deallocated, and ccache_pointer is set to NULL before being returned to caller.

Possible error codes: CC_NO_EXIST

#### cc_seq_fetch_NCs

    cc_seq_fetch_NCs(apiCB* cc_ctx,	ccache_p** ccache_pointer,	ccache_cit** itNCs)

is used to sequentially open every NC in the cache.

Ccache_pointer should be a pointer to a ccache_p*. *Ccache_pointer should initially be set to NULL. The ccache_pointer returned may be used to get information about the NC by calling cc_get_name, cc_get_cred_version, & cc_get_principal. Ccache_pointer's returned must be freed via cc_close between calls to cc_seq_fetch_NCs.

... Need words about returning valid NCptr with rc = CC_END, confused Jenny ...

itNCs should be a pointer to a ccache_cit* variable provided by the calling application and which is used by cc_seq_fetch_NCs to determine the next NC to return. To fetch the first NC, the ccache_cit* variable should be initialized to NULL.

Note that both CC_NOERROR and CC_END indicate a successful return from cc_seq_fetch_NCs(). CC_END indicates that the ccache_pointer returned is the last in the sequence.

Possible error codes: CC_NO_EXIST, CC_NOMEM

Possible success codes: CC_NOERROR, CC_END

### NC Functions

#### cc_get_name

    cc_get_name(apiCB* cc_ctx, const ccache_p* ccache_pointer, char** name)

cc_get_name returns the name of the NC indicated by ccache_pointer. The name can be used in cc_open() or cc_create(). The combination of the name and the credentials version uniqeuly identify an NC. The returned name should be freed via cc_free_name().

Possible error codes: CC_NOMEM, CC_NO_EXIST

#### cc_get_cred_version

    cc_get_cred_version(apiCB* cc_ctx, const ccache_p* ccache_pointer, cc_cred_vers* vers)

cc_get_cred_version returns one of the enumerated type cc_cred_vers in vers. The expected values are CC_CRED_V4, or CC_CRED_V5. The combination of the name and the credentials version uniqeuly identify an NC.

Possible error codes: CC_NO_EXIST

#### cc_set_principal

    cc_set_principal(apiCB* cc_ctx, const ccache_p* ccache_pointer, const cc_cred_vers vers, const char* principal)

Set the primary principal for the NC indicated by ccache_pointer. This is the complement to cc_get_principal().

vers is used as a double check.

principal points to a null terminated string that will be copied into the NC. This new principal will be returned if you call cc_get_principal() for this NC.

Possible error codes: CC_NOMEM, CC_NO_EXIST, CC_ERR_CRED_VERSION

#### cc_get_principal

    cc_get_principal(apiCB* cc_ctx, const ccache_p* ccache_pointer, char** principal)

Return the primary principal for the NC that was set via cc_create or cc_set_principal. The returned principal should be freed via cc_free_principal

Possible error codes: CC_NOMEM, CC_NO_EXIST

#### cc_store

    cc_store(apiCB* cc_ctx, const ccache_p* ccache_pointer, const cred_union cred)

Store (make a copy of) cred in the NC indicated by ccache_pointer.

    typedef union cred_ptr_union_type {
        V4Cred_type* pV4Cred;
        cc_creds*    pV5Cred;
    } cred_ptr_union;

    typedef struct cred_union_type {
    #ifdef CRED_TYPE_IN_UNION
        cc_cred_vers cred_type;
    #endif
        cred_ptr_union_type cred;
    } cred_union;

A cred_union contains a cred_type indicator and a cred_ptr_union. A cred_ptr_union can contain either a V4Cred_type pointer or a cc_creds (V5 creds) pointer. Cred_type indicates which type of pointer is in the cred_ptr_union. This also allows the API to enforce the credentials version declared in cc_create or cc_open.

Possible error codes: CC_NO_EXIST, CC_ERR_CACHE_FULL, CC_ERR_CRED_VERSION

#### cc_remove_cred

    cc_remove_cred(apiCB* cc_ctx, const ccache_p* ccache_pointer, const cred_union cred)

Removes the credential cred from ccache_pointer. The credentials in the NC indicated by ccache_pointer are searched to find a matching credential. If found, that credential is removed from the NC. The cred parameter is not modified and should be freed via cc_free_creds. It is legitimate to call this function during a sequential fetch, and the deletion of a credential already returned by cc_seq_fetch_creds() should not disturb sequence of credentials returned by cc_seq_fetch_creds.

use of cred_union is the same as is explained in cc_store().

Possible error codes: CC_NO_EXIST, CC_NOTFOUND, CC_ERR_CRED_VERSION

#### cc_seq_fetch_creds

    cc_seq_fetch_creds(apiCB* cc_ctx, const ccache_p* ccache_pointer,
    	cred_union* cred, ccache_cit** itCreds)

cc_seq_fetch_creds is used to sequentially read every set of credentials in the NC indicated by ccache_pointer. It requires that ccache_pointer identify a valid NC.

itCreds should be a pointer to a ccache_cit* variable provided by the calling application and which is used by cc_seq_fetch_creds to determine the next cached credential to return. To fetch the first cached credentials, the ccache_cit* variable should be initialized to NULL.

The credentials are filled into the cred_union pointed to by creds. Note that the cred_union contains elements which are dynamically allocated, so must be freed using cc_free_creds between calls to cc_seq_fetch_creds.

When the last credential in the sequence is returned, the return code from cc_seq_fetch_creds will be CC_END, and itCreds will be NULL.

Possible error codes: CC_NO_EXIST, CC_NOMEM, CC_END

#### cc_lock_request

    cc_lock_request(apiCB* cc_ctx, const ccache_p* ccache_pointer, int lock_type)

This function is currently NOT IMPLEMENTED. All functions attach to the cache, take action, and detach from the cache before returning to the caller.

This function will lock or unlock the NC based on the argument value of lock_type:

    	CC_LOCK_UNLOCK	1	Unlock the NC
    	CC_LOCK_READER	2	Lock the NC for reading
    	CC_LOCK_WRITER	3	Lock the NC for writing

    	CC_LOCK_NOBLOCK	16	Don't block, but return an error code if
    				the request cannot be satisfied.

Locking is done on a per-thread basis. At most one thread may have the credentials locked for writing; if so, there must not be any threads that have the credentials locked for reading.

Multiple threads may have the cache locked for reading, as long as there is not a writer lock asserted on the cache.

If a thread has a cache locked for reading, that lock may be upgraded to a writer lock by calling cc_lock_request() with a lock_type of CC_LOCK_WRITER. If a thread has the cache locked for reading or writing, a request to cc_lock_request() for a reader or writer lock, respectively, is a no-op. If a thread does not have the cache locked, and calls cc_lock_request with a lock_type of CC_LOCK_UNLOCK, this is also a no-op.

A request for CC_LOCK_READER and CC_LOCK_WRITER may be made non-blocking by logical or'ing the value CC_LOCK_NOBLOCK. In that case, if it is not possible to satisfy the lock request, the error CC_LOCKED will be returned.

### Liberation Functions

#### cc_free_principal

    cc_free_principal(apiCB* cc_ctx, char* principal)

This function frees the principal returned by cc_get_principal and sets *principal to NULL.

Possible error code: CC_NO_EXIST

#### cc_free_name

    cc_free_name(apiCB* cc_ctx, char* name)

This function frees the name returned by cc_get_name() and sets *name to NULL.

Possible error code: CC_NO_EXIST

#### cc_free_creds

    cc_free_creds(apiCB* cc_ctx, cred_union** creds)

This function frees all storage associated with creds returned by cc_seq_fetch_creds and sets the creds pointer to NULL.

Possible error code: CC_NO_EXIST

#### cc_free_NC_info

    cc_free_NC_info(apiCB* cc_ctx, struct _infoNC*** ppNCi)

This routine frees all storage aquired by cc_get_NC_info and sets ppNCi to NULL.

Possible error code: CC_NO_EXIST