Path: blob/main/crypto/krb5/src/lib/kdb/encrypt_key.c
39566 views
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */1/* lib/kdb/encrypt_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/*55* Encrypt dbkey for storage in the database, putting the result into56* key_data_out.57*/58krb5_error_code59krb5_dbe_def_encrypt_key_data(krb5_context context, const krb5_keyblock *mkey,60const krb5_keyblock *dbkey,61const krb5_keysalt *keysalt, int keyver,62krb5_key_data *key_data_out)63{64krb5_error_code ret;65size_t clen;66krb5_data plain;67krb5_enc_data cipher;68krb5_key_data kd = { 0 };6970memset(key_data_out, 0, sizeof(*key_data_out));7172kd.key_data_ver = 1;73kd.key_data_kvno = keyver;7475ret = krb5_c_encrypt_length(context, mkey->enctype, dbkey->length, &clen);76if (ret)77goto cleanup;7879/* The first element of the type/length/contents fields is the key80* type/length/contents. */81kd.key_data_type[0] = dbkey->enctype;82kd.key_data_length[0] = 2 + clen;83kd.key_data_contents[0] = k5alloc(kd.key_data_length[0], &ret);84if (kd.key_data_contents[0] == NULL)85goto cleanup;86store_16_le(dbkey->length, kd.key_data_contents[0]);8788plain = make_data(dbkey->contents, dbkey->length);89cipher.ciphertext = make_data(kd.key_data_contents[0] + 2, clen);90ret = krb5_c_encrypt(context, mkey, 0, 0, &plain, &cipher);91if (ret)92goto cleanup;9394/* The second element of each array is the salt, if necessary. */95if (keysalt != NULL && keysalt->type > 0) {96kd.key_data_ver++;97kd.key_data_type[1] = keysalt->type;98kd.key_data_length[1] = keysalt->data.length;99if (keysalt->data.length > 0) {100kd.key_data_contents[1] = k5memdup(keysalt->data.data,101keysalt->data.length, &ret);102if (kd.key_data_contents[1] == NULL)103goto cleanup;104}105}106107*key_data_out = kd;108memset(&kd, 0, sizeof(kd));109110cleanup:111krb5_dbe_free_key_data_contents(context, &kd);112return ret;113}114115116