1*7f2fe78bSCy Schubert /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2*7f2fe78bSCy Schubert /* lib/kdb/encrypt_key.c */
3*7f2fe78bSCy Schubert /*
4*7f2fe78bSCy Schubert * Copyright 1990,1991,2023 by the Massachusetts Institute of Technology.
5*7f2fe78bSCy Schubert * All Rights Reserved.
6*7f2fe78bSCy Schubert *
7*7f2fe78bSCy Schubert * Export of this software from the United States of America may
8*7f2fe78bSCy Schubert * require a specific license from the United States Government.
9*7f2fe78bSCy Schubert * It is the responsibility of any person or organization contemplating
10*7f2fe78bSCy Schubert * export to obtain such a license before exporting.
11*7f2fe78bSCy Schubert *
12*7f2fe78bSCy Schubert * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
13*7f2fe78bSCy Schubert * distribute this software and its documentation for any purpose and
14*7f2fe78bSCy Schubert * without fee is hereby granted, provided that the above copyright
15*7f2fe78bSCy Schubert * notice appear in all copies and that both that copyright notice and
16*7f2fe78bSCy Schubert * this permission notice appear in supporting documentation, and that
17*7f2fe78bSCy Schubert * the name of M.I.T. not be used in advertising or publicity pertaining
18*7f2fe78bSCy Schubert * to distribution of the software without specific, written prior
19*7f2fe78bSCy Schubert * permission. Furthermore if you modify this software you must label
20*7f2fe78bSCy Schubert * your software as modified software and not distribute it in such a
21*7f2fe78bSCy Schubert * fashion that it might be confused with the original M.I.T. software.
22*7f2fe78bSCy Schubert * M.I.T. makes no representations about the suitability of
23*7f2fe78bSCy Schubert * this software for any purpose. It is provided "as is" without express
24*7f2fe78bSCy Schubert * or implied warranty.
25*7f2fe78bSCy Schubert */
26*7f2fe78bSCy Schubert /*
27*7f2fe78bSCy Schubert * Copyright (C) 1998 by the FundsXpress, INC.
28*7f2fe78bSCy Schubert *
29*7f2fe78bSCy Schubert * All rights reserved.
30*7f2fe78bSCy Schubert *
31*7f2fe78bSCy Schubert * Export of this software from the United States of America may require
32*7f2fe78bSCy Schubert * a specific license from the United States Government. It is the
33*7f2fe78bSCy Schubert * responsibility of any person or organization contemplating export to
34*7f2fe78bSCy Schubert * obtain such a license before exporting.
35*7f2fe78bSCy Schubert *
36*7f2fe78bSCy Schubert * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
37*7f2fe78bSCy Schubert * distribute this software and its documentation for any purpose and
38*7f2fe78bSCy Schubert * without fee is hereby granted, provided that the above copyright
39*7f2fe78bSCy Schubert * notice appear in all copies and that both that copyright notice and
40*7f2fe78bSCy Schubert * this permission notice appear in supporting documentation, and that
41*7f2fe78bSCy Schubert * the name of FundsXpress. not be used in advertising or publicity pertaining
42*7f2fe78bSCy Schubert * to distribution of the software without specific, written prior
43*7f2fe78bSCy Schubert * permission. FundsXpress makes no representations about the suitability of
44*7f2fe78bSCy Schubert * this software for any purpose. It is provided "as is" without express
45*7f2fe78bSCy Schubert * or implied warranty.
46*7f2fe78bSCy Schubert *
47*7f2fe78bSCy Schubert * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
48*7f2fe78bSCy Schubert * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
49*7f2fe78bSCy Schubert * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
50*7f2fe78bSCy Schubert */
51*7f2fe78bSCy Schubert
52*7f2fe78bSCy Schubert #include "k5-int.h"
53*7f2fe78bSCy Schubert #include "kdb.h"
54*7f2fe78bSCy Schubert
55*7f2fe78bSCy Schubert /*
56*7f2fe78bSCy Schubert * Encrypt dbkey for storage in the database, putting the result into
57*7f2fe78bSCy Schubert * key_data_out.
58*7f2fe78bSCy Schubert */
59*7f2fe78bSCy Schubert krb5_error_code
krb5_dbe_def_encrypt_key_data(krb5_context context,const krb5_keyblock * mkey,const krb5_keyblock * dbkey,const krb5_keysalt * keysalt,int keyver,krb5_key_data * key_data_out)60*7f2fe78bSCy Schubert krb5_dbe_def_encrypt_key_data(krb5_context context, const krb5_keyblock *mkey,
61*7f2fe78bSCy Schubert const krb5_keyblock *dbkey,
62*7f2fe78bSCy Schubert const krb5_keysalt *keysalt, int keyver,
63*7f2fe78bSCy Schubert krb5_key_data *key_data_out)
64*7f2fe78bSCy Schubert {
65*7f2fe78bSCy Schubert krb5_error_code ret;
66*7f2fe78bSCy Schubert size_t clen;
67*7f2fe78bSCy Schubert krb5_data plain;
68*7f2fe78bSCy Schubert krb5_enc_data cipher;
69*7f2fe78bSCy Schubert krb5_key_data kd = { 0 };
70*7f2fe78bSCy Schubert
71*7f2fe78bSCy Schubert memset(key_data_out, 0, sizeof(*key_data_out));
72*7f2fe78bSCy Schubert
73*7f2fe78bSCy Schubert kd.key_data_ver = 1;
74*7f2fe78bSCy Schubert kd.key_data_kvno = keyver;
75*7f2fe78bSCy Schubert
76*7f2fe78bSCy Schubert ret = krb5_c_encrypt_length(context, mkey->enctype, dbkey->length, &clen);
77*7f2fe78bSCy Schubert if (ret)
78*7f2fe78bSCy Schubert goto cleanup;
79*7f2fe78bSCy Schubert
80*7f2fe78bSCy Schubert /* The first element of the type/length/contents fields is the key
81*7f2fe78bSCy Schubert * type/length/contents. */
82*7f2fe78bSCy Schubert kd.key_data_type[0] = dbkey->enctype;
83*7f2fe78bSCy Schubert kd.key_data_length[0] = 2 + clen;
84*7f2fe78bSCy Schubert kd.key_data_contents[0] = k5alloc(kd.key_data_length[0], &ret);
85*7f2fe78bSCy Schubert if (kd.key_data_contents[0] == NULL)
86*7f2fe78bSCy Schubert goto cleanup;
87*7f2fe78bSCy Schubert store_16_le(dbkey->length, kd.key_data_contents[0]);
88*7f2fe78bSCy Schubert
89*7f2fe78bSCy Schubert plain = make_data(dbkey->contents, dbkey->length);
90*7f2fe78bSCy Schubert cipher.ciphertext = make_data(kd.key_data_contents[0] + 2, clen);
91*7f2fe78bSCy Schubert ret = krb5_c_encrypt(context, mkey, 0, 0, &plain, &cipher);
92*7f2fe78bSCy Schubert if (ret)
93*7f2fe78bSCy Schubert goto cleanup;
94*7f2fe78bSCy Schubert
95*7f2fe78bSCy Schubert /* The second element of each array is the salt, if necessary. */
96*7f2fe78bSCy Schubert if (keysalt != NULL && keysalt->type > 0) {
97*7f2fe78bSCy Schubert kd.key_data_ver++;
98*7f2fe78bSCy Schubert kd.key_data_type[1] = keysalt->type;
99*7f2fe78bSCy Schubert kd.key_data_length[1] = keysalt->data.length;
100*7f2fe78bSCy Schubert if (keysalt->data.length > 0) {
101*7f2fe78bSCy Schubert kd.key_data_contents[1] = k5memdup(keysalt->data.data,
102*7f2fe78bSCy Schubert keysalt->data.length, &ret);
103*7f2fe78bSCy Schubert if (kd.key_data_contents[1] == NULL)
104*7f2fe78bSCy Schubert goto cleanup;
105*7f2fe78bSCy Schubert }
106*7f2fe78bSCy Schubert }
107*7f2fe78bSCy Schubert
108*7f2fe78bSCy Schubert *key_data_out = kd;
109*7f2fe78bSCy Schubert memset(&kd, 0, sizeof(kd));
110*7f2fe78bSCy Schubert
111*7f2fe78bSCy Schubert cleanup:
112*7f2fe78bSCy Schubert krb5_dbe_free_key_data_contents(context, &kd);
113*7f2fe78bSCy Schubert return ret;
114*7f2fe78bSCy Schubert }
115