10Sstevel@tonic-gate /*
2*7934SMark.Phalan@Sun.COM * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
30Sstevel@tonic-gate * Use is subject to license terms.
40Sstevel@tonic-gate */
50Sstevel@tonic-gate
60Sstevel@tonic-gate
70Sstevel@tonic-gate /*
80Sstevel@tonic-gate * lib/kdb/encrypt_key.c
90Sstevel@tonic-gate *
100Sstevel@tonic-gate * Copyright 1990,1991 by the Massachusetts Institute of Technology.
110Sstevel@tonic-gate * All Rights Reserved.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * Export of this software from the United States of America may
140Sstevel@tonic-gate * require a specific license from the United States Government.
150Sstevel@tonic-gate * It is the responsibility of any person or organization contemplating
160Sstevel@tonic-gate * export to obtain such a license before exporting.
170Sstevel@tonic-gate *
180Sstevel@tonic-gate * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
190Sstevel@tonic-gate * distribute this software and its documentation for any purpose and
200Sstevel@tonic-gate * without fee is hereby granted, provided that the above copyright
210Sstevel@tonic-gate * notice appear in all copies and that both that copyright notice and
220Sstevel@tonic-gate * this permission notice appear in supporting documentation, and that
230Sstevel@tonic-gate * the name of M.I.T. not be used in advertising or publicity pertaining
240Sstevel@tonic-gate * to distribution of the software without specific, written prior
250Sstevel@tonic-gate * permission. Furthermore if you modify this software you must label
260Sstevel@tonic-gate * your software as modified software and not distribute it in such a
270Sstevel@tonic-gate * fashion that it might be confused with the original M.I.T. software.
280Sstevel@tonic-gate * M.I.T. makes no representations about the suitability of
290Sstevel@tonic-gate * this software for any purpose. It is provided "as is" without express
300Sstevel@tonic-gate * or implied warranty.
310Sstevel@tonic-gate *
320Sstevel@tonic-gate *
330Sstevel@tonic-gate * krb5_kdb_encrypt_key(), krb5_kdb_decrypt_key functions
340Sstevel@tonic-gate */
350Sstevel@tonic-gate
360Sstevel@tonic-gate /*
370Sstevel@tonic-gate * Copyright (C) 1998 by the FundsXpress, INC.
380Sstevel@tonic-gate *
390Sstevel@tonic-gate * All rights reserved.
400Sstevel@tonic-gate *
410Sstevel@tonic-gate * Export of this software from the United States of America may require
420Sstevel@tonic-gate * a specific license from the United States Government. It is the
430Sstevel@tonic-gate * responsibility of any person or organization contemplating export to
440Sstevel@tonic-gate * obtain such a license before exporting.
450Sstevel@tonic-gate *
460Sstevel@tonic-gate * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
470Sstevel@tonic-gate * distribute this software and its documentation for any purpose and
480Sstevel@tonic-gate * without fee is hereby granted, provided that the above copyright
490Sstevel@tonic-gate * notice appear in all copies and that both that copyright notice and
500Sstevel@tonic-gate * this permission notice appear in supporting documentation, and that
510Sstevel@tonic-gate * the name of FundsXpress. not be used in advertising or publicity pertaining
520Sstevel@tonic-gate * to distribution of the software without specific, written prior
530Sstevel@tonic-gate * permission. FundsXpress makes no representations about the suitability of
540Sstevel@tonic-gate * this software for any purpose. It is provided "as is" without express
550Sstevel@tonic-gate * or implied warranty.
560Sstevel@tonic-gate *
570Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
580Sstevel@tonic-gate * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
590Sstevel@tonic-gate * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
600Sstevel@tonic-gate */
610Sstevel@tonic-gate
620Sstevel@tonic-gate #include "k5-int.h"
634960Swillf #include <krb5/kdb.h>
640Sstevel@tonic-gate
650Sstevel@tonic-gate /*
660Sstevel@tonic-gate * Encrypt a key for storage in the database. "eblock" is used
670Sstevel@tonic-gate * to encrypt the key in "in" into "out"; the storage pointed to by "out"
680Sstevel@tonic-gate * is allocated before use.
690Sstevel@tonic-gate */
700Sstevel@tonic-gate
710Sstevel@tonic-gate krb5_error_code
krb5_dbekd_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)72*7934SMark.Phalan@Sun.COM krb5_dbekd_encrypt_key_data( krb5_context context,
73*7934SMark.Phalan@Sun.COM const krb5_keyblock * mkey,
74*7934SMark.Phalan@Sun.COM const krb5_keyblock * dbkey,
75*7934SMark.Phalan@Sun.COM const krb5_keysalt * keysalt,
76*7934SMark.Phalan@Sun.COM int keyver,
77*7934SMark.Phalan@Sun.COM krb5_key_data * key_data)
780Sstevel@tonic-gate {
790Sstevel@tonic-gate krb5_error_code retval;
800Sstevel@tonic-gate krb5_octet * ptr;
810Sstevel@tonic-gate size_t len;
820Sstevel@tonic-gate int i;
830Sstevel@tonic-gate krb5_data plain;
840Sstevel@tonic-gate krb5_enc_data cipher;
850Sstevel@tonic-gate
860Sstevel@tonic-gate for (i = 0; i < key_data->key_data_ver; i++)
870Sstevel@tonic-gate if (key_data->key_data_contents[i])
880Sstevel@tonic-gate krb5_xfree(key_data->key_data_contents[i]);
890Sstevel@tonic-gate
900Sstevel@tonic-gate key_data->key_data_ver = 1;
910Sstevel@tonic-gate key_data->key_data_kvno = keyver;
920Sstevel@tonic-gate
930Sstevel@tonic-gate /*
940Sstevel@tonic-gate * The First element of the type/length/contents
950Sstevel@tonic-gate * fields is the key type/length/contents
960Sstevel@tonic-gate */
970Sstevel@tonic-gate if ((retval = krb5_c_encrypt_length(context, mkey->enctype, dbkey->length,
980Sstevel@tonic-gate &len)))
990Sstevel@tonic-gate return(retval);
1000Sstevel@tonic-gate
1010Sstevel@tonic-gate if ((ptr = (krb5_octet *) malloc(2 + len)) == NULL)
1020Sstevel@tonic-gate return(ENOMEM);
1030Sstevel@tonic-gate
1040Sstevel@tonic-gate (void) memset(ptr, 0, 2 + len);
1050Sstevel@tonic-gate
1060Sstevel@tonic-gate key_data->key_data_type[0] = dbkey->enctype;
1070Sstevel@tonic-gate key_data->key_data_length[0] = 2 + len;
1080Sstevel@tonic-gate key_data->key_data_contents[0] = ptr;
1090Sstevel@tonic-gate
1100Sstevel@tonic-gate krb5_kdb_encode_int16(dbkey->length, ptr);
1110Sstevel@tonic-gate ptr += 2;
1120Sstevel@tonic-gate
1130Sstevel@tonic-gate plain.length = dbkey->length;
1140Sstevel@tonic-gate plain.data = (char *)dbkey->contents; /* SUNWresync121 XXX */
1150Sstevel@tonic-gate
1160Sstevel@tonic-gate cipher.ciphertext.length = len;
1170Sstevel@tonic-gate cipher.ciphertext.data = (char *)ptr; /* SUNWresync121 XXX */
1180Sstevel@tonic-gate
1190Sstevel@tonic-gate if ((retval = krb5_c_encrypt(context, mkey, /* XXX */ 0, 0,
1200Sstevel@tonic-gate &plain, &cipher))) {
1210Sstevel@tonic-gate krb5_xfree(key_data->key_data_contents[0]);
1220Sstevel@tonic-gate return retval;
1230Sstevel@tonic-gate }
1240Sstevel@tonic-gate
1250Sstevel@tonic-gate /* After key comes the salt in necessary */
1260Sstevel@tonic-gate if (keysalt) {
1270Sstevel@tonic-gate if (keysalt->type > 0) {
1280Sstevel@tonic-gate key_data->key_data_ver++;
1290Sstevel@tonic-gate key_data->key_data_type[1] = keysalt->type;
1302881Smp153739 if ((key_data->key_data_length[1] = keysalt->data.length) != 0) {
1310Sstevel@tonic-gate key_data->key_data_contents[1] =
1320Sstevel@tonic-gate (krb5_octet *)malloc(keysalt->data.length);
1330Sstevel@tonic-gate if (key_data->key_data_contents[1] == NULL) {
1340Sstevel@tonic-gate krb5_xfree(key_data->key_data_contents[0]);
1350Sstevel@tonic-gate return ENOMEM;
1360Sstevel@tonic-gate }
1370Sstevel@tonic-gate memcpy(key_data->key_data_contents[1], keysalt->data.data,
1380Sstevel@tonic-gate (size_t) keysalt->data.length);
1390Sstevel@tonic-gate }
1400Sstevel@tonic-gate }
1410Sstevel@tonic-gate }
1420Sstevel@tonic-gate
1430Sstevel@tonic-gate return retval;
1440Sstevel@tonic-gate }
145