libtommath: Fix possible integer overflow CVE-2023-36328
[heimdal.git] / lib / hdb / keytab.c
blobdf16cb782faa797d44aa7ac2b1538d8a400e5cc5
1 /*
2 * Copyright (c) 1999 - 2002 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
34 #include "hdb_locl.h"
36 /* keytab backend for HDB databases */
38 struct hdb_data {
39 char *dbname;
40 char *mkey;
43 struct hdb_cursor {
44 HDB *db;
45 hdb_entry hdb_entry;
46 int first, next;
47 int key_idx;
51 * the format for HDB keytabs is:
52 * HDB:[HDBFORMAT:database-specific-data[:mkey=mkey-file]]
55 static krb5_error_code KRB5_CALLCONV
56 hdb_resolve(krb5_context context, const char *name, krb5_keytab id)
58 struct hdb_data *d;
59 const char *db, *mkey;
61 d = malloc(sizeof(*d));
62 if(d == NULL) {
63 krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
64 return ENOMEM;
66 db = name;
67 mkey = strstr(name, ":mkey=");
68 if(mkey == NULL || mkey[6] == '\0') {
69 if(*name == '\0')
70 d->dbname = NULL;
71 else {
72 d->dbname = strdup(name);
73 if(d->dbname == NULL) {
74 free(d);
75 krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
76 return ENOMEM;
79 d->mkey = NULL;
80 } else {
81 d->dbname = malloc(mkey - db + 1);
82 if(d->dbname == NULL) {
83 free(d);
84 krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
85 return ENOMEM;
87 memmove(d->dbname, db, mkey - db);
88 d->dbname[mkey - db] = '\0';
90 d->mkey = strdup(mkey + 6);
91 if(d->mkey == NULL) {
92 free(d->dbname);
93 free(d);
94 krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
95 return ENOMEM;
98 id->data = d;
99 return 0;
102 static krb5_error_code KRB5_CALLCONV
103 hdb_close(krb5_context context, krb5_keytab id)
105 struct hdb_data *d = id->data;
107 free(d->dbname);
108 free(d->mkey);
109 free(d);
110 return 0;
113 static krb5_error_code KRB5_CALLCONV
114 hdb_get_name(krb5_context context,
115 krb5_keytab id,
116 char *name,
117 size_t namesize)
119 struct hdb_data *d = id->data;
121 snprintf(name, namesize, "%s%s%s",
122 d->dbname ? d->dbname : "",
123 (d->dbname || d->mkey) ? ":" : "",
124 d->mkey ? d->mkey : "");
125 return 0;
129 * try to figure out the database (`dbname') and master-key (`mkey')
130 * that should be used for `principal'.
133 static krb5_error_code
134 find_db (krb5_context context,
135 char **dbname,
136 char **mkey,
137 krb5_const_principal principal)
139 krb5_const_realm realm = krb5_principal_get_realm(context, principal);
140 krb5_error_code ret;
141 struct hdb_dbinfo *head, *dbinfo = NULL;
143 *dbname = *mkey = NULL;
145 ret = hdb_get_dbinfo(context, &head);
146 if (ret)
147 return ret;
149 while ((dbinfo = hdb_dbinfo_get_next(head, dbinfo)) != NULL) {
150 const char *p = hdb_dbinfo_get_realm(context, dbinfo);
151 if (p && strcmp (realm, p) == 0) {
152 p = hdb_dbinfo_get_dbname(context, dbinfo);
153 if (p)
154 *dbname = strdup(p);
155 p = hdb_dbinfo_get_mkey_file(context, dbinfo);
156 if (p)
157 *mkey = strdup(p);
158 break;
161 hdb_free_dbinfo(context, &head);
162 if (*dbname == NULL &&
163 (*dbname = strdup(hdb_default_db(context))) == NULL) {
164 free(*mkey);
165 *mkey = NULL;
166 return krb5_enomem(context);
168 return 0;
172 * find the keytab entry in `id' for `principal, kvno, enctype' and return
173 * it in `entry'. return 0 or an error code
176 static krb5_error_code KRB5_CALLCONV
177 hdb_get_entry(krb5_context context,
178 krb5_keytab id,
179 krb5_const_principal principal,
180 krb5_kvno kvno,
181 krb5_enctype enctype,
182 krb5_keytab_entry *entry)
184 hdb_entry ent;
185 krb5_error_code ret;
186 struct hdb_data *d = id->data;
187 const char *dbname = d->dbname;
188 const char *mkey = d->mkey;
189 char *fdbname = NULL, *fmkey = NULL;
190 HDB *db;
191 size_t i;
193 if (!principal)
194 return KRB5_KT_NOTFOUND;
196 memset(&ent, 0, sizeof(ent));
198 if (dbname == NULL) {
199 ret = find_db(context, &fdbname, &fmkey, principal);
200 if (ret)
201 return ret;
202 dbname = fdbname;
203 mkey = fmkey;
206 ret = hdb_create (context, &db, dbname);
207 if (ret)
208 goto out2;
209 ret = hdb_set_master_keyfile (context, db, mkey);
210 if (ret) {
211 (*db->hdb_destroy)(context, db);
212 goto out2;
215 ret = (*db->hdb_open)(context, db, O_RDONLY, 0);
216 if (ret) {
217 (*db->hdb_destroy)(context, db);
218 goto out2;
221 ret = hdb_fetch_kvno(context, db, principal,
222 HDB_F_DECRYPT|HDB_F_KVNO_SPECIFIED|
223 HDB_F_GET_CLIENT|HDB_F_GET_SERVER|HDB_F_GET_KRBTGT,
224 0, 0, kvno, &ent);
226 if (ret == HDB_ERR_WRONG_REALM || ret == HDB_ERR_NOENTRY)
227 ret = KRB5_KT_NOTFOUND;
228 if (ret)
229 goto out;
231 if(kvno && (krb5_kvno)ent.kvno != kvno) {
232 hdb_free_entry(context, db, &ent);
233 ret = KRB5_KT_NOTFOUND;
234 goto out;
236 if(enctype == 0)
237 if(ent.keys.len > 0)
238 enctype = ent.keys.val[0].key.keytype;
239 ret = KRB5_KT_NOTFOUND;
240 for(i = 0; i < ent.keys.len; i++) {
241 if(ent.keys.val[i].key.keytype == enctype) {
242 krb5_copy_principal(context, principal, &entry->principal);
243 entry->vno = ent.kvno;
244 krb5_copy_keyblock_contents(context,
245 &ent.keys.val[i].key,
246 &entry->keyblock);
247 ret = 0;
248 break;
251 hdb_free_entry(context, db, &ent);
252 out:
253 (*db->hdb_close)(context, db);
254 (*db->hdb_destroy)(context, db);
255 out2:
256 free(fdbname);
257 free(fmkey);
258 return ret;
262 * find the keytab entry in `id' for `principal, kvno, enctype' and return
263 * it in `entry'. return 0 or an error code
266 static krb5_error_code KRB5_CALLCONV
267 hdb_start_seq_get(krb5_context context,
268 krb5_keytab id,
269 krb5_kt_cursor *cursor)
271 krb5_error_code ret;
272 struct hdb_cursor *c;
273 struct hdb_data *d = id->data;
274 const char *dbname = d->dbname;
275 const char *mkey = d->mkey;
276 HDB *db;
278 if (dbname == NULL) {
280 * We don't support enumerating without being told what
281 * backend to enumerate on
283 ret = KRB5_KT_NOTFOUND;
284 return ret;
287 ret = hdb_create (context, &db, dbname);
288 if (ret)
289 return ret;
290 ret = hdb_set_master_keyfile (context, db, mkey);
291 if (ret) {
292 (*db->hdb_destroy)(context, db);
293 return ret;
296 ret = (*db->hdb_open)(context, db, O_RDONLY, 0);
297 if (ret) {
298 (*db->hdb_destroy)(context, db);
299 return ret;
302 cursor->data = c = malloc (sizeof(*c));
303 if(c == NULL){
304 (*db->hdb_close)(context, db);
305 (*db->hdb_destroy)(context, db);
306 krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
307 return ENOMEM;
310 c->db = db;
311 c->first = TRUE;
312 c->next = TRUE;
313 c->key_idx = 0;
315 cursor->data = c;
316 return ret;
319 static int KRB5_CALLCONV
320 hdb_next_entry(krb5_context context,
321 krb5_keytab id,
322 krb5_keytab_entry *entry,
323 krb5_kt_cursor *cursor)
325 struct hdb_cursor *c = cursor->data;
326 krb5_error_code ret;
328 memset(entry, 0, sizeof(*entry));
330 if (c->first) {
331 c->first = FALSE;
332 ret = (c->db->hdb_firstkey)(context, c->db,
333 HDB_F_DECRYPT|
334 HDB_F_GET_CLIENT|HDB_F_GET_SERVER|HDB_F_GET_KRBTGT,
335 &c->hdb_entry);
336 if (ret == HDB_ERR_NOENTRY)
337 return KRB5_KT_END;
338 else if (ret)
339 return ret;
341 if (c->hdb_entry.keys.len == 0)
342 hdb_free_entry(context, c->db, &c->hdb_entry);
343 else
344 c->next = FALSE;
347 while (c->next) {
348 ret = (c->db->hdb_nextkey)(context, c->db,
349 HDB_F_DECRYPT|
350 HDB_F_GET_CLIENT|HDB_F_GET_SERVER|HDB_F_GET_KRBTGT,
351 &c->hdb_entry);
352 if (ret == HDB_ERR_NOENTRY)
353 return KRB5_KT_END;
354 else if (ret)
355 return ret;
357 /* If no keys on this entry, try again */
358 if (c->hdb_entry.keys.len == 0)
359 hdb_free_entry(context, c->db, &c->hdb_entry);
360 else
361 c->next = FALSE;
365 * Return next enc type (keytabs are one slot per key, while
366 * hdb is one record per principal.
369 ret = krb5_copy_principal(context,
370 c->hdb_entry.principal,
371 &entry->principal);
372 if (ret)
373 return ret;
375 entry->vno = c->hdb_entry.kvno;
376 ret = krb5_copy_keyblock_contents(context,
377 &c->hdb_entry.keys.val[c->key_idx].key,
378 &entry->keyblock);
379 if (ret) {
380 krb5_free_principal(context, entry->principal);
381 memset(entry, 0, sizeof(*entry));
382 return ret;
384 c->key_idx++;
387 * Once we get to the end of the list, signal that we want the
388 * next entry
391 if ((size_t)c->key_idx == c->hdb_entry.keys.len) {
392 hdb_free_entry(context, c->db, &c->hdb_entry);
393 c->next = TRUE;
394 c->key_idx = 0;
397 return 0;
401 static int KRB5_CALLCONV
402 hdb_end_seq_get(krb5_context context,
403 krb5_keytab id,
404 krb5_kt_cursor *cursor)
406 struct hdb_cursor *c = cursor->data;
408 if (!c->next)
409 hdb_free_entry(context, c->db, &c->hdb_entry);
411 (c->db->hdb_close)(context, c->db);
412 (c->db->hdb_destroy)(context, c->db);
414 free(c);
415 return 0;
418 krb5_kt_ops hdb_kt_ops = {
419 "HDB",
420 hdb_resolve,
421 hdb_get_name,
422 hdb_close,
423 NULL, /* destroy */
424 hdb_get_entry,
425 hdb_start_seq_get,
426 hdb_next_entry,
427 hdb_end_seq_get,
428 NULL, /* add */
429 NULL, /* remove */
430 NULL,
434 krb5_kt_ops hdb_get_kt_ops = {
435 "HDBGET",
436 hdb_resolve,
437 hdb_get_name,
438 hdb_close,
439 NULL,
440 hdb_get_entry,
441 NULL,
442 NULL,
443 NULL,
444 NULL,
445 NULL,
446 NULL,