xref: /freebsd/crypto/krb5/src/ccapi/test/test_ccapi_util.c (revision 7f2fe78b9dd5f51c821d771b63d2e096f6fd49e9)
1 #include <string.h>
2 #include <time.h>
3 #include <stdio.h>
4 #include <stdarg.h>
5 #include <stdlib.h>
6 #include <malloc.h>
7 
8 #include "k5-platform.h"	/* pull in asprintf decl/defn */
9 #include "test_ccapi_util.h"
10 
11 
12 // ---------------------------------------------------------------------------
13 
destroy_all_ccaches(cc_context_t context)14 cc_int32 destroy_all_ccaches(cc_context_t context) {
15 	cc_int32 err = ccNoError;
16 	cc_ccache_t ccache = NULL;
17 
18 	while (!err) {
19 		err = cc_context_open_default_ccache(context, &ccache);
20 		if (!err) {
21 			err = cc_ccache_destroy(ccache);
22 		}
23 	}
24 	if (err == ccErrCCacheNotFound) {
25 		err = ccNoError;
26 	}
27 	else {
28 		log_error("cc_context_open_default_ccache or cc_ccache_destroy failed with %s (%d)", translate_ccapi_error(err), err);
29 	}
30 
31 	return err;
32 }
33 
34 
35 // ---------------------------------------------------------------------------
36 
new_v5_creds_union(cc_credentials_union * out_union,const char * realm)37 cc_int32 new_v5_creds_union (cc_credentials_union *out_union, const char *realm)
38 {
39     cc_int32 err = ccNoError;
40     cc_credentials_union *cred_union = NULL;
41 	cc_credentials_v5_t *v5creds = NULL;
42     static int num_runs = 1;
43 	char *client = NULL;
44 	char *server = NULL;
45 
46     if (!out_union) { err = ccErrBadParam; }
47 
48 	if (!err) {
49 		v5creds = malloc (sizeof (*v5creds));
50 		if (!v5creds) {
51 			err = ccErrNoMem;
52 		}
53 	}
54 
55 	if (!err) {
56 		asprintf(&client, "client@%s", realm);
57 		asprintf(&server, "host/%d%s@%s", num_runs++, realm, realm);
58 		if (!client || !server) {
59 			err = ccErrNoMem;
60 		}
61 	}
62 
63 	if (!err) {
64 		v5creds->client = client;
65 		v5creds->server = server;
66 		v5creds->keyblock.type = 1;
67 		v5creds->keyblock.length = 0;
68 		v5creds->keyblock.data = NULL;
69 		v5creds->authtime = time (NULL);
70 		v5creds->starttime = time (NULL);
71 		v5creds->endtime = time(NULL) + 1000;
72 		v5creds->renew_till = time(NULL) + 10000;
73 		v5creds->is_skey = 0;
74 		v5creds->ticket_flags = TKT_FLG_FORWARDABLE | TKT_FLG_PROXIABLE | TKT_FLG_RENEWABLE | TKT_FLG_INITIAL;
75 		v5creds->addresses = NULL;
76 		v5creds->ticket.type = 0;
77 		v5creds->ticket.length = 0;
78 		v5creds->ticket.data = NULL;
79 		v5creds->second_ticket.type = 0;
80 		v5creds->second_ticket.length = 0;
81 		v5creds->second_ticket.data = NULL;
82 		v5creds->authdata = NULL;
83 	}
84 
85 
86     if (!err) {
87         cred_union = malloc (sizeof (*cred_union));
88         if (cred_union) {
89 			cred_union->version = cc_credentials_v5;
90 			cred_union->credentials.credentials_v5 = v5creds;
91         } else {
92             err = ccErrNoMem;
93         }
94     }
95 	if (!err) {
96 		*out_union = *cred_union;
97 		cred_union = NULL;
98 	}
99 
100     return err;
101 }
102 
103 
104 // ---------------------------------------------------------------------------
105 
release_v5_creds_union(cc_credentials_union * creds_union)106 void release_v5_creds_union(cc_credentials_union *creds_union) {
107 	cc_credentials_v5_t *v5creds = NULL;
108 
109 	if (creds_union) {
110 		if (creds_union->credentials.credentials_v5) {
111 			v5creds = creds_union->credentials.credentials_v5;
112 			if (v5creds->client) { free(v5creds->client); }
113 			if (v5creds->server) { free(v5creds->server); }
114 			if (v5creds->keyblock.data) { free(v5creds->keyblock.data); }
115 			if (v5creds->ticket.data) { free(v5creds->ticket.data); }
116 			if (v5creds->second_ticket.data) { free(v5creds->second_ticket.data); }
117 			free(v5creds);
118 		}
119 		//free(creds_union);
120 	}
121 }
122 
123 
124 // ---------------------------------------------------------------------------
125 
126 // return zero when both unions are considered equal, non-zero when not
127 
compare_v5_creds_unions(const cc_credentials_union * a,const cc_credentials_union * b)128 int compare_v5_creds_unions(const cc_credentials_union *a, const cc_credentials_union *b) {
129 	int retval = -1;
130 
131 	if (a &&
132 		b &&
133 		(a->version == cc_credentials_v5) &&
134 		(a->version == b->version) &&
135 		(strcmp(a->credentials.credentials_v5->client, b->credentials.credentials_v5->client) == 0) &&
136 		(strcmp(a->credentials.credentials_v5->server, b->credentials.credentials_v5->server) == 0))
137 	{
138 		retval = 0;
139 	}
140 
141 	return retval;
142 }
143