xref: /freebsd/crypto/heimdal/lib/hdb/common.c (revision 6990ffd8a95caaba6858ad44ff1b3157d1efba8f)
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.8 2001/01/30 01:22:17 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 	ret = ENOMEM;
54 	goto out;
55     }
56     ret = encode_Principal(buf + len - 1, len, &new, &len);
57     if(ret){
58 	free(buf);
59 	goto out;
60     }
61     key->data = buf;
62     key->length = len;
63 out:
64     free_Principal(&new);
65     return ret;
66 }
67 
68 int
69 hdb_key2principal(krb5_context context, krb5_data *key, krb5_principal p)
70 {
71     return decode_Principal(key->data, key->length, p, NULL);
72 }
73 
74 int
75 hdb_entry2value(krb5_context context, hdb_entry *ent, krb5_data *value)
76 {
77     unsigned char *buf;
78     size_t len;
79     int ret;
80 
81     len = length_hdb_entry(ent);
82     buf = malloc(len);
83     if(buf == NULL)
84 	return ENOMEM;
85     ret = encode_hdb_entry(buf + len - 1, len, ent, &len);
86     if(ret){
87 	free(buf);
88 	return ret;
89     }
90     value->data = buf;
91     value->length = len;
92     return 0;
93 }
94 
95 int
96 hdb_value2entry(krb5_context context, krb5_data *value, hdb_entry *ent)
97 {
98     return decode_hdb_entry(value->data, value->length, ent, NULL);
99 }
100 
101 krb5_error_code
102 _hdb_fetch(krb5_context context, HDB *db, unsigned flags, hdb_entry *entry)
103 {
104     krb5_data key, value;
105     int code = 0;
106 
107     hdb_principal2key(context, entry->principal, &key);
108     code = db->_get(context, db, key, &value);
109     krb5_data_free(&key);
110     if(code)
111 	return code;
112     hdb_value2entry(context, &value, entry);
113     if (db->master_key_set && (flags & HDB_F_DECRYPT)) {
114 	code = hdb_unseal_keys (context, db, entry);
115 	if (code)
116 	    hdb_free_entry(context, entry);
117     }
118     krb5_data_free(&value);
119     return code;
120 }
121 
122 krb5_error_code
123 _hdb_store(krb5_context context, HDB *db, unsigned flags, hdb_entry *entry)
124 {
125     krb5_data key, value;
126     int code;
127 
128     hdb_principal2key(context, entry->principal, &key);
129     code = hdb_seal_keys(context, db, entry);
130     if (code) {
131 	krb5_data_free(&key);
132 	return code;
133     }
134     hdb_entry2value(context, entry, &value);
135     code = db->_put(context, db, flags & HDB_F_REPLACE, key, value);
136     krb5_data_free(&value);
137     krb5_data_free(&key);
138     return code;
139 }
140 
141 krb5_error_code
142 _hdb_remove(krb5_context context, HDB *db, hdb_entry *entry)
143 {
144     krb5_data key;
145     int code;
146 
147     hdb_principal2key(context, entry->principal, &key);
148     code = db->_del(context, db, key);
149     krb5_data_free(&key);
150     return code;
151 }
152 
153