xref: /freebsd/crypto/heimdal/lib/hdb/common.c (revision 4137ff4cc173ea2e05227027e1c9e0ea42bcc0dc)
1 /*
2  * Copyright (c) 1997-2001 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.10 2001/07/13 06:30:41 assar 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     unsigned char *buf;
44     int ret;
45 
46     ret = copy_Principal(p, &new);
47     if(ret)
48 	goto out;
49     new.name.name_type = 0;
50     len = length_Principal(&new);
51     buf  = malloc(len);
52     if(buf == NULL){
53 	krb5_set_error_string(context, "malloc: out of memory");
54 	ret = ENOMEM;
55 	goto out;
56     }
57     ret = encode_Principal(buf + len - 1, len, &new, &len);
58     if(ret){
59 	free(buf);
60 	goto out;
61     }
62     key->data = buf;
63     key->length = len;
64 out:
65     free_Principal(&new);
66     return ret;
67 }
68 
69 int
70 hdb_key2principal(krb5_context context, krb5_data *key, krb5_principal p)
71 {
72     return decode_Principal(key->data, key->length, p, NULL);
73 }
74 
75 int
76 hdb_entry2value(krb5_context context, hdb_entry *ent, krb5_data *value)
77 {
78     unsigned char *buf;
79     size_t len;
80     int ret;
81 
82     len = length_hdb_entry(ent);
83     buf = malloc(len);
84     if(buf == NULL) {
85 	krb5_set_error_string(context, "malloc: out of memory");
86 	return ENOMEM;
87     }
88     ret = encode_hdb_entry(buf + len - 1, len, ent, &len);
89     if(ret){
90 	free(buf);
91 	return ret;
92     }
93     value->data = buf;
94     value->length = len;
95     return 0;
96 }
97 
98 int
99 hdb_value2entry(krb5_context context, krb5_data *value, hdb_entry *ent)
100 {
101     return decode_hdb_entry(value->data, value->length, ent, NULL);
102 }
103 
104 krb5_error_code
105 _hdb_fetch(krb5_context context, HDB *db, unsigned flags, hdb_entry *entry)
106 {
107     krb5_data key, value;
108     int code = 0;
109 
110     hdb_principal2key(context, entry->principal, &key);
111     code = db->_get(context, db, key, &value);
112     krb5_data_free(&key);
113     if(code)
114 	return code;
115     hdb_value2entry(context, &value, entry);
116     if (db->master_key_set && (flags & HDB_F_DECRYPT)) {
117 	code = hdb_unseal_keys (context, db, entry);
118 	if (code)
119 	    hdb_free_entry(context, entry);
120     }
121     krb5_data_free(&value);
122     return code;
123 }
124 
125 krb5_error_code
126 _hdb_store(krb5_context context, HDB *db, unsigned flags, hdb_entry *entry)
127 {
128     krb5_data key, value;
129     int code;
130 
131     if(entry->generation == NULL) {
132 	struct timeval t;
133 	entry->generation = malloc(sizeof(*entry->generation));
134 	if(entry->generation == NULL) {
135 	    krb5_set_error_string(context, "malloc: out of memory");
136 	    return ENOMEM;
137 	}
138 	gettimeofday(&t, NULL);
139 	entry->generation->time = t.tv_sec;
140 	entry->generation->usec = t.tv_usec;
141 	entry->generation->gen = 0;
142     } else
143 	entry->generation->gen++;
144     hdb_principal2key(context, entry->principal, &key);
145     code = hdb_seal_keys(context, db, entry);
146     if (code) {
147 	krb5_data_free(&key);
148 	return code;
149     }
150     hdb_entry2value(context, entry, &value);
151     code = db->_put(context, db, flags & HDB_F_REPLACE, key, value);
152     krb5_data_free(&value);
153     krb5_data_free(&key);
154     return code;
155 }
156 
157 krb5_error_code
158 _hdb_remove(krb5_context context, HDB *db, hdb_entry *entry)
159 {
160     krb5_data key;
161     int code;
162 
163     hdb_principal2key(context, entry->principal, &key);
164     code = db->_del(context, db, key);
165     krb5_data_free(&key);
166     return code;
167 }
168 
169