Path: blob/main/crypto/krb5/src/lib/kdb/decrypt_key.c
39566 views
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */1/* lib/kdb/decrypt_key.c */2/*3* Copyright 1990,1991,2023 by the Massachusetts Institute of Technology.4* All Rights Reserved.5*6* Export of this software from the United States of America may7* require a specific license from the United States Government.8* It is the responsibility of any person or organization contemplating9* export to obtain such a license before exporting.10*11* WITHIN THAT CONSTRAINT, permission to use, copy, modify, and12* distribute this software and its documentation for any purpose and13* without fee is hereby granted, provided that the above copyright14* notice appear in all copies and that both that copyright notice and15* this permission notice appear in supporting documentation, and that16* the name of M.I.T. not be used in advertising or publicity pertaining17* to distribution of the software without specific, written prior18* permission. Furthermore if you modify this software you must label19* your software as modified software and not distribute it in such a20* fashion that it might be confused with the original M.I.T. software.21* M.I.T. makes no representations about the suitability of22* this software for any purpose. It is provided "as is" without express23* or implied warranty.24*/25/*26* Copyright (C) 1998 by the FundsXpress, INC.27*28* All rights reserved.29*30* Export of this software from the United States of America may require31* a specific license from the United States Government. It is the32* responsibility of any person or organization contemplating export to33* obtain such a license before exporting.34*35* WITHIN THAT CONSTRAINT, permission to use, copy, modify, and36* distribute this software and its documentation for any purpose and37* without fee is hereby granted, provided that the above copyright38* notice appear in all copies and that both that copyright notice and39* this permission notice appear in supporting documentation, and that40* the name of FundsXpress. not be used in advertising or publicity pertaining41* to distribution of the software without specific, written prior42* permission. FundsXpress makes no representations about the suitability of43* this software for any purpose. It is provided "as is" without express44* or implied warranty.45*46* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR47* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED48* WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.49*/5051#include "k5-int.h"52#include "kdb.h"5354/* Decrypt key_data, putting the result into dbkey_out and (if not null)55* keysalt_out. */56krb5_error_code57krb5_dbe_def_decrypt_key_data(krb5_context context, const krb5_keyblock *mkey,58const krb5_key_data *kd,59krb5_keyblock *dbkey_out,60krb5_keysalt *keysalt_out)61{62krb5_error_code ret = KRB5_CRYPTO_INTERNAL;63int16_t keylen;64krb5_enc_data cipher;65krb5_data plain = empty_data();66krb5_keyblock kb = { 0 };67krb5_keysalt salt = { 0 };6869memset(dbkey_out, 0, sizeof(*dbkey_out));70if (keysalt_out != NULL)71memset(keysalt_out, 0, sizeof(*keysalt_out));7273if (mkey == NULL)74return KRB5_KDB_BADSTORED_MKEY;7576if (kd->key_data_contents[0] == NULL || kd->key_data_length[0] < 2)77return KRB5_KDB_INVALIDKEYSIZE;7879keylen = load_16_le(kd->key_data_contents[0]);80if (keylen < 0)81return KRB5_KDB_INVALIDKEYSIZE;8283cipher.enctype = ENCTYPE_UNKNOWN;84cipher.ciphertext = make_data(kd->key_data_contents[0] + 2,85kd->key_data_length[0] - 2);86ret = alloc_data(&plain, kd->key_data_length[0] - 2);87if (ret)88goto cleanup;8990ret = krb5_c_decrypt(context, mkey, 0, 0, &cipher, &plain);91if (ret)92goto cleanup;9394/* Make sure the plaintext has at least as many bytes as the true key95* length (it may have more due to padding). */96if ((unsigned int)keylen > plain.length) {97ret = KRB5_CRYPTO_INTERNAL;98if (ret)99goto cleanup;100}101102kb.magic = KV5M_KEYBLOCK;103kb.enctype = kd->key_data_type[0];104kb.length = keylen;105kb.contents = (uint8_t *)plain.data;106plain = empty_data();107108/* Decode salt data. */109if (keysalt_out != NULL) {110if (kd->key_data_ver == 2) {111salt.type = kd->key_data_type[1];112salt.data.length = kd->key_data_length[1];113if (kd->key_data_length[1] > 0) {114ret = alloc_data(&salt.data, kd->key_data_length[1]);115if (ret)116goto cleanup;117memcpy(salt.data.data, kd->key_data_contents[1],118salt.data.length);119}120} else {121salt.type = KRB5_KDB_SALTTYPE_NORMAL;122}123}124125*dbkey_out = kb;126if (keysalt_out != NULL)127*keysalt_out = salt;128memset(&kb, 0, sizeof(kb));129memset(&salt, 0, sizeof(salt));130131cleanup:132zapfree(plain.data, plain.length);133krb5_free_keyblock_contents(context, &kb);134free(salt.data.data);135return ret;136}137138139