xref: /freebsd/crypto/krb5/src/lib/kdb/decrypt_key.c (revision 7f2fe78b9dd5f51c821d771b63d2e096f6fd49e9)
1*7f2fe78bSCy Schubert /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2*7f2fe78bSCy Schubert /* lib/kdb/decrypt_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 /* Decrypt key_data, putting the result into dbkey_out and (if not null)
56*7f2fe78bSCy Schubert  * keysalt_out. */
57*7f2fe78bSCy Schubert krb5_error_code
krb5_dbe_def_decrypt_key_data(krb5_context context,const krb5_keyblock * mkey,const krb5_key_data * kd,krb5_keyblock * dbkey_out,krb5_keysalt * keysalt_out)58*7f2fe78bSCy Schubert krb5_dbe_def_decrypt_key_data(krb5_context context, const krb5_keyblock *mkey,
59*7f2fe78bSCy Schubert                               const krb5_key_data *kd,
60*7f2fe78bSCy Schubert                               krb5_keyblock *dbkey_out,
61*7f2fe78bSCy Schubert                               krb5_keysalt *keysalt_out)
62*7f2fe78bSCy Schubert {
63*7f2fe78bSCy Schubert     krb5_error_code ret;
64*7f2fe78bSCy Schubert     int16_t keylen;
65*7f2fe78bSCy Schubert     krb5_enc_data cipher;
66*7f2fe78bSCy Schubert     krb5_data plain = empty_data();
67*7f2fe78bSCy Schubert     krb5_keyblock kb = { 0 };
68*7f2fe78bSCy Schubert     krb5_keysalt salt = { 0 };
69*7f2fe78bSCy Schubert 
70*7f2fe78bSCy Schubert     memset(dbkey_out, 0, sizeof(*dbkey_out));
71*7f2fe78bSCy Schubert     if (keysalt_out != NULL)
72*7f2fe78bSCy Schubert         memset(keysalt_out, 0, sizeof(*keysalt_out));
73*7f2fe78bSCy Schubert 
74*7f2fe78bSCy Schubert     if (mkey == NULL)
75*7f2fe78bSCy Schubert         return KRB5_KDB_BADSTORED_MKEY;
76*7f2fe78bSCy Schubert 
77*7f2fe78bSCy Schubert     if (kd->key_data_contents[0] != NULL && kd->key_data_length[0] >= 2) {
78*7f2fe78bSCy Schubert         keylen = load_16_le(kd->key_data_contents[0]);
79*7f2fe78bSCy Schubert         if (keylen < 0)
80*7f2fe78bSCy Schubert             return EINVAL;
81*7f2fe78bSCy Schubert         cipher.enctype = ENCTYPE_UNKNOWN;
82*7f2fe78bSCy Schubert         cipher.ciphertext = make_data(kd->key_data_contents[0] + 2,
83*7f2fe78bSCy Schubert                                       kd->key_data_length[0] - 2);
84*7f2fe78bSCy Schubert         ret = alloc_data(&plain, kd->key_data_length[0] - 2);
85*7f2fe78bSCy Schubert         if (ret)
86*7f2fe78bSCy Schubert             goto cleanup;
87*7f2fe78bSCy Schubert 
88*7f2fe78bSCy Schubert         ret = krb5_c_decrypt(context, mkey, 0, 0, &cipher, &plain);
89*7f2fe78bSCy Schubert         if (ret)
90*7f2fe78bSCy Schubert             goto cleanup;
91*7f2fe78bSCy Schubert 
92*7f2fe78bSCy Schubert         /* Make sure the plaintext has at least as many bytes as the true ke
93*7f2fe78bSCy Schubert          * length (it may have more due to padding). */
94*7f2fe78bSCy Schubert         if ((unsigned int)keylen > plain.length) {
95*7f2fe78bSCy Schubert             ret = KRB5_CRYPTO_INTERNAL;
96*7f2fe78bSCy Schubert             if (ret)
97*7f2fe78bSCy Schubert                 goto cleanup;
98*7f2fe78bSCy Schubert         }
99*7f2fe78bSCy Schubert 
100*7f2fe78bSCy Schubert         kb.magic = KV5M_KEYBLOCK;
101*7f2fe78bSCy Schubert         kb.enctype = kd->key_data_type[0];
102*7f2fe78bSCy Schubert         kb.length = keylen;
103*7f2fe78bSCy Schubert         kb.contents = (uint8_t *)plain.data;
104*7f2fe78bSCy Schubert         plain = empty_data();
105*7f2fe78bSCy Schubert     }
106*7f2fe78bSCy Schubert 
107*7f2fe78bSCy Schubert     /* Decode salt data. */
108*7f2fe78bSCy Schubert     if (keysalt_out != NULL) {
109*7f2fe78bSCy Schubert         if (kd->key_data_ver == 2) {
110*7f2fe78bSCy Schubert             salt.type = kd->key_data_type[1];
111*7f2fe78bSCy Schubert             salt.data.length = kd->key_data_length[1];
112*7f2fe78bSCy Schubert             if (kd->key_data_length[1] > 0) {
113*7f2fe78bSCy Schubert                 ret = alloc_data(&salt.data, kd->key_data_length[1]);
114*7f2fe78bSCy Schubert                 if (ret)
115*7f2fe78bSCy Schubert                     goto cleanup;
116*7f2fe78bSCy Schubert                 memcpy(salt.data.data, kd->key_data_contents[1],
117*7f2fe78bSCy Schubert                        salt.data.length);
118*7f2fe78bSCy Schubert             }
119*7f2fe78bSCy Schubert         } else {
120*7f2fe78bSCy Schubert             salt.type = KRB5_KDB_SALTTYPE_NORMAL;
121*7f2fe78bSCy Schubert         }
122*7f2fe78bSCy Schubert     }
123*7f2fe78bSCy Schubert 
124*7f2fe78bSCy Schubert     *dbkey_out = kb;
125*7f2fe78bSCy Schubert     if (keysalt_out != NULL)
126*7f2fe78bSCy Schubert         *keysalt_out = salt;
127*7f2fe78bSCy Schubert     memset(&kb, 0, sizeof(kb));
128*7f2fe78bSCy Schubert     memset(&salt, 0, sizeof(salt));
129*7f2fe78bSCy Schubert 
130*7f2fe78bSCy Schubert cleanup:
131*7f2fe78bSCy Schubert     zapfree(plain.data, plain.length);
132*7f2fe78bSCy Schubert     krb5_free_keyblock_contents(context, &kb);
133*7f2fe78bSCy Schubert     free(salt.data.data);
134*7f2fe78bSCy Schubert     return ret;
135*7f2fe78bSCy Schubert }
136