xref: /freebsd/crypto/heimdal/lib/hdb/common.c (revision f0adf7f5cdd241db2f2c817683191a6ef64a4e95)
1 /*
2  * Copyright (c) 1997-2002 Kungliga Tekniska H�gskolan
3  * (Royal Institute of Technology, Stockholm, Sweden).
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  *
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.
16  *
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.
20  *
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.
32  */
33 
34 #include "hdb_locl.h"
35 
36 RCSID("$Id: common.c,v 1.12 2003/01/14 06:54:32 lha Exp $");
37 
38 int
39 hdb_principal2key(krb5_context context, krb5_principal p, krb5_data *key)
40 {
41     Principal new;
42     size_t len;
43     int ret;
44 
45     ret = copy_Principal(p, &new);
46     if(ret)
47 	return ret;
48     new.name.name_type = 0;
49 
50     ASN1_MALLOC_ENCODE(Principal, key->data, key->length, &new, &len, ret);
51     free_Principal(&new);
52     return ret;
53 }
54 
55 int
56 hdb_key2principal(krb5_context context, krb5_data *key, krb5_principal p)
57 {
58     return decode_Principal(key->data, key->length, p, NULL);
59 }
60 
61 int
62 hdb_entry2value(krb5_context context, hdb_entry *ent, krb5_data *value)
63 {
64     size_t len;
65     int ret;
66 
67     ASN1_MALLOC_ENCODE(hdb_entry, value->data, value->length, ent, &len, ret);
68     return ret;
69 }
70 
71 int
72 hdb_value2entry(krb5_context context, krb5_data *value, hdb_entry *ent)
73 {
74     return decode_hdb_entry(value->data, value->length, ent, NULL);
75 }
76 
77 krb5_error_code
78 _hdb_fetch(krb5_context context, HDB *db, unsigned flags, hdb_entry *entry)
79 {
80     krb5_data key, value;
81     int code;
82 
83     hdb_principal2key(context, entry->principal, &key);
84     code = db->_get(context, db, key, &value);
85     krb5_data_free(&key);
86     if(code)
87 	return code;
88     code = hdb_value2entry(context, &value, entry);
89     krb5_data_free(&value);
90     if (code)
91 	return code;
92     if (db->master_key_set && (flags & HDB_F_DECRYPT)) {
93 	code = hdb_unseal_keys (context, db, entry);
94 	if (code)
95 	    hdb_free_entry(context, entry);
96     }
97     return code;
98 }
99 
100 krb5_error_code
101 _hdb_store(krb5_context context, HDB *db, unsigned flags, hdb_entry *entry)
102 {
103     krb5_data key, value;
104     int code;
105 
106     if(entry->generation == NULL) {
107 	struct timeval t;
108 	entry->generation = malloc(sizeof(*entry->generation));
109 	if(entry->generation == NULL) {
110 	    krb5_set_error_string(context, "malloc: out of memory");
111 	    return ENOMEM;
112 	}
113 	gettimeofday(&t, NULL);
114 	entry->generation->time = t.tv_sec;
115 	entry->generation->usec = t.tv_usec;
116 	entry->generation->gen = 0;
117     } else
118 	entry->generation->gen++;
119     hdb_principal2key(context, entry->principal, &key);
120     code = hdb_seal_keys(context, db, entry);
121     if (code) {
122 	krb5_data_free(&key);
123 	return code;
124     }
125     hdb_entry2value(context, entry, &value);
126     code = db->_put(context, db, flags & HDB_F_REPLACE, key, value);
127     krb5_data_free(&value);
128     krb5_data_free(&key);
129     return code;
130 }
131 
132 krb5_error_code
133 _hdb_remove(krb5_context context, HDB *db, hdb_entry *entry)
134 {
135     krb5_data key;
136     int code;
137 
138     hdb_principal2key(context, entry->principal, &key);
139     code = db->_del(context, db, key);
140     krb5_data_free(&key);
141     return code;
142 }
143 
144