xref: /netbsd-src/crypto/external/bsd/heimdal/dist/lib/kadm5/randkey_s.c (revision d3273b5b76f5afaafe308cead5511dbb8df8c5e9)
1 /*	$NetBSD: randkey_s.c,v 1.2 2017/01/28 21:31:49 christos Exp $	*/
2 
3 /*
4  * Copyright (c) 1997-2001, 2003-2006 Kungliga Tekniska Högskolan
5  * (Royal Institute of Technology, Stockholm, Sweden).
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * 3. Neither the name of the Institute nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #include "kadm5_locl.h"
37 
38 __RCSID("$NetBSD: randkey_s.c,v 1.2 2017/01/28 21:31:49 christos Exp $");
39 
40 /*
41  * Set the keys of `princ' to random values, returning the random keys
42  * in `new_keys', `n_keys'.
43  */
44 
45 kadm5_ret_t
kadm5_s_randkey_principal(void * server_handle,krb5_principal princ,krb5_boolean keepold,int n_ks_tuple,krb5_key_salt_tuple * ks_tuple,krb5_keyblock ** new_keys,int * n_keys)46 kadm5_s_randkey_principal(void *server_handle,
47 			  krb5_principal princ,
48 			  krb5_boolean keepold,
49 			  int n_ks_tuple,
50 			  krb5_key_salt_tuple *ks_tuple,
51 			  krb5_keyblock **new_keys,
52 			  int *n_keys)
53 {
54     kadm5_server_context *context = server_handle;
55     hdb_entry_ex ent;
56     kadm5_ret_t ret;
57 
58     memset(&ent, 0, sizeof(ent));
59     if (!context->keep_open) {
60 	ret = context->db->hdb_open(context->context, context->db, O_RDWR, 0);
61 	if(ret)
62 	    return ret;
63     }
64 
65     ret = kadm5_log_init(context);
66     if (ret)
67         goto out;
68 
69     ret = context->db->hdb_fetch_kvno(context->context, context->db, princ,
70 				      HDB_F_GET_ANY|HDB_F_ADMIN_DATA, 0, &ent);
71     if(ret)
72 	goto out2;
73 
74     if (keepold) {
75 	ret = hdb_add_current_keys_to_history(context->context, &ent.entry);
76 	if (ret)
77 	    goto out3;
78     }
79 
80     ret = _kadm5_set_keys_randomly(context, &ent.entry, n_ks_tuple, ks_tuple,
81                                    new_keys, n_keys);
82     if (ret)
83 	goto out3;
84     ent.entry.kvno++;
85 
86     ent.entry.flags.require_pwchange = 0;
87 
88     ret = _kadm5_set_modifier(context, &ent.entry);
89     if(ret)
90 	goto out4;
91     ret = _kadm5_bump_pw_expire(context, &ent.entry);
92     if (ret)
93 	goto out4;
94 
95     if (keepold) {
96 	ret = hdb_seal_keys(context->context, context->db, &ent.entry);
97 	if (ret)
98 	    goto out4;
99     } else {
100 	HDB_extension ext;
101 
102 	memset(&ext, 0, sizeof (ext));
103         ext.mandatory = FALSE;
104 	ext.data.element = choice_HDB_extension_data_hist_keys;
105 	ext.data.u.hist_keys.len = 0;
106 	ext.data.u.hist_keys.val = NULL;
107 	hdb_replace_extension(context->context, &ent.entry, &ext);
108     }
109 
110     /* This logs the change for iprop and writes to the HDB */
111     ret = kadm5_log_modify(context, &ent.entry,
112                            KADM5_ATTRIBUTES | KADM5_PRINCIPAL |
113                            KADM5_MOD_NAME | KADM5_MOD_TIME |
114                            KADM5_KEY_DATA | KADM5_KVNO |
115                            KADM5_PW_EXPIRATION | KADM5_TL_DATA);
116 
117  out4:
118     if (ret) {
119 	int i;
120 
121 	for (i = 0; i < *n_keys; ++i)
122 	    krb5_free_keyblock_contents(context->context, &(*new_keys)[i]);
123 	free (*new_keys);
124 	*new_keys = NULL;
125 	*n_keys = 0;
126     }
127  out3:
128     hdb_free_entry(context->context, &ent);
129  out2:
130     (void) kadm5_log_end(context);
131  out:
132     if (!context->keep_open) {
133         kadm5_ret_t ret2;
134         ret2 = context->db->hdb_close(context->context, context->db);
135         if (ret == 0 && ret2 != 0)
136             ret = ret2;
137     }
138     return _kadm5_error_code(ret);
139 }
140