1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */ 2 /* lib/kdb/decrypt_key.c */ 3 /* 4 * Copyright 1990,1991,2023 by the Massachusetts Institute of Technology. 5 * All Rights Reserved. 6 * 7 * Export of this software from the United States of America may 8 * require a specific license from the United States Government. 9 * It is the responsibility of any person or organization contemplating 10 * export to obtain such a license before exporting. 11 * 12 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and 13 * distribute this software and its documentation for any purpose and 14 * without fee is hereby granted, provided that the above copyright 15 * notice appear in all copies and that both that copyright notice and 16 * this permission notice appear in supporting documentation, and that 17 * the name of M.I.T. not be used in advertising or publicity pertaining 18 * to distribution of the software without specific, written prior 19 * permission. Furthermore if you modify this software you must label 20 * your software as modified software and not distribute it in such a 21 * fashion that it might be confused with the original M.I.T. software. 22 * M.I.T. makes no representations about the suitability of 23 * this software for any purpose. It is provided "as is" without express 24 * or implied warranty. 25 */ 26 /* 27 * Copyright (C) 1998 by the FundsXpress, INC. 28 * 29 * All rights reserved. 30 * 31 * Export of this software from the United States of America may require 32 * a specific license from the United States Government. It is the 33 * responsibility of any person or organization contemplating export to 34 * obtain such a license before exporting. 35 * 36 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and 37 * distribute this software and its documentation for any purpose and 38 * without fee is hereby granted, provided that the above copyright 39 * notice appear in all copies and that both that copyright notice and 40 * this permission notice appear in supporting documentation, and that 41 * the name of FundsXpress. not be used in advertising or publicity pertaining 42 * to distribution of the software without specific, written prior 43 * permission. FundsXpress makes no representations about the suitability of 44 * this software for any purpose. It is provided "as is" without express 45 * or implied warranty. 46 * 47 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 48 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 49 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 50 */ 51 52 #include "k5-int.h" 53 #include "kdb.h" 54 55 /* Decrypt key_data, putting the result into dbkey_out and (if not null) 56 * keysalt_out. */ 57 krb5_error_code 58 krb5_dbe_def_decrypt_key_data(krb5_context context, const krb5_keyblock *mkey, 59 const krb5_key_data *kd, 60 krb5_keyblock *dbkey_out, 61 krb5_keysalt *keysalt_out) 62 { 63 krb5_error_code ret; 64 int16_t keylen; 65 krb5_enc_data cipher; 66 krb5_data plain = empty_data(); 67 krb5_keyblock kb = { 0 }; 68 krb5_keysalt salt = { 0 }; 69 70 memset(dbkey_out, 0, sizeof(*dbkey_out)); 71 if (keysalt_out != NULL) 72 memset(keysalt_out, 0, sizeof(*keysalt_out)); 73 74 if (mkey == NULL) 75 return KRB5_KDB_BADSTORED_MKEY; 76 77 if (kd->key_data_contents[0] != NULL && kd->key_data_length[0] >= 2) { 78 keylen = load_16_le(kd->key_data_contents[0]); 79 if (keylen < 0) 80 return EINVAL; 81 cipher.enctype = ENCTYPE_UNKNOWN; 82 cipher.ciphertext = make_data(kd->key_data_contents[0] + 2, 83 kd->key_data_length[0] - 2); 84 ret = alloc_data(&plain, kd->key_data_length[0] - 2); 85 if (ret) 86 goto cleanup; 87 88 ret = krb5_c_decrypt(context, mkey, 0, 0, &cipher, &plain); 89 if (ret) 90 goto cleanup; 91 92 /* Make sure the plaintext has at least as many bytes as the true ke 93 * length (it may have more due to padding). */ 94 if ((unsigned int)keylen > plain.length) { 95 ret = KRB5_CRYPTO_INTERNAL; 96 if (ret) 97 goto cleanup; 98 } 99 100 kb.magic = KV5M_KEYBLOCK; 101 kb.enctype = kd->key_data_type[0]; 102 kb.length = keylen; 103 kb.contents = (uint8_t *)plain.data; 104 plain = empty_data(); 105 } 106 107 /* Decode salt data. */ 108 if (keysalt_out != NULL) { 109 if (kd->key_data_ver == 2) { 110 salt.type = kd->key_data_type[1]; 111 salt.data.length = kd->key_data_length[1]; 112 if (kd->key_data_length[1] > 0) { 113 ret = alloc_data(&salt.data, kd->key_data_length[1]); 114 if (ret) 115 goto cleanup; 116 memcpy(salt.data.data, kd->key_data_contents[1], 117 salt.data.length); 118 } 119 } else { 120 salt.type = KRB5_KDB_SALTTYPE_NORMAL; 121 } 122 } 123 124 *dbkey_out = kb; 125 if (keysalt_out != NULL) 126 *keysalt_out = salt; 127 memset(&kb, 0, sizeof(kb)); 128 memset(&salt, 0, sizeof(salt)); 129 130 cleanup: 131 zapfree(plain.data, plain.length); 132 krb5_free_keyblock_contents(context, &kb); 133 free(salt.data.data); 134 return ret; 135 } 136