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/decrypt_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 * Decrypt a key from storage in the database. "eblock" is used
670Sstevel@tonic-gate * to decrypt 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_decrypt_key_data(krb5_context context,const krb5_keyblock * mkey,const krb5_key_data * key_data,krb5_keyblock * dbkey,krb5_keysalt * keysalt)72*7934SMark.Phalan@Sun.COM krb5_dbekd_decrypt_key_data( krb5_context context,
73*7934SMark.Phalan@Sun.COM const krb5_keyblock * mkey,
74*7934SMark.Phalan@Sun.COM const krb5_key_data * key_data,
75*7934SMark.Phalan@Sun.COM krb5_keyblock * dbkey,
76*7934SMark.Phalan@Sun.COM krb5_keysalt * keysalt)
770Sstevel@tonic-gate {
780Sstevel@tonic-gate krb5_error_code retval = 0;
790Sstevel@tonic-gate krb5_int16 tmplen;
800Sstevel@tonic-gate krb5_octet * ptr;
810Sstevel@tonic-gate krb5_enc_data cipher;
820Sstevel@tonic-gate krb5_data plain;
830Sstevel@tonic-gate
840Sstevel@tonic-gate ptr = key_data->key_data_contents[0];
850Sstevel@tonic-gate
860Sstevel@tonic-gate if (ptr) {
870Sstevel@tonic-gate krb5_kdb_decode_int16(ptr, tmplen);
880Sstevel@tonic-gate ptr += 2;
890Sstevel@tonic-gate
900Sstevel@tonic-gate cipher.enctype = ENCTYPE_UNKNOWN;
910Sstevel@tonic-gate cipher.ciphertext.length = key_data->key_data_length[0]-2;
920Sstevel@tonic-gate cipher.ciphertext.data = (char *)ptr; /* SUNWresync121 XXX */
930Sstevel@tonic-gate plain.length = key_data->key_data_length[0]-2;
940Sstevel@tonic-gate if ((plain.data = (char *) malloc(plain.length)) == NULL)
950Sstevel@tonic-gate return(ENOMEM);
960Sstevel@tonic-gate (void) memset(plain.data, 0, plain.length);
970Sstevel@tonic-gate
980Sstevel@tonic-gate if ((retval = krb5_c_decrypt(context, mkey, 0 /* XXX */, 0,
990Sstevel@tonic-gate &cipher, &plain))) {
1000Sstevel@tonic-gate krb5_xfree(plain.data);
1010Sstevel@tonic-gate return retval;
1020Sstevel@tonic-gate }
1030Sstevel@tonic-gate
1040Sstevel@tonic-gate /* tmplen is the true length of the key. plain.data is the
1050Sstevel@tonic-gate plaintext data length, but it may be padded, since the
1060Sstevel@tonic-gate old-style etypes didn't store the real length. I can check
1070Sstevel@tonic-gate to make sure that there are enough bytes, but I can't do
1080Sstevel@tonic-gate any better than that. */
1090Sstevel@tonic-gate
1100Sstevel@tonic-gate if (tmplen > plain.length) {
1110Sstevel@tonic-gate krb5_xfree(plain.data);
1120Sstevel@tonic-gate return(KRB5_CRYPTO_INTERNAL);
1130Sstevel@tonic-gate }
1140Sstevel@tonic-gate
1150Sstevel@tonic-gate dbkey->magic = KV5M_KEYBLOCK;
1160Sstevel@tonic-gate dbkey->enctype = key_data->key_data_type[0];
1170Sstevel@tonic-gate dbkey->length = tmplen;
1180Sstevel@tonic-gate dbkey->contents = (unsigned char *) plain.data; /* SUNWresync121 XXX */
1190Sstevel@tonic-gate dbkey->dk_list = NULL;
1200Sstevel@tonic-gate dbkey->hKey = CK_INVALID_HANDLE;
1210Sstevel@tonic-gate }
1220Sstevel@tonic-gate
1230Sstevel@tonic-gate /* Decode salt data */
1240Sstevel@tonic-gate if (keysalt) {
1250Sstevel@tonic-gate if (key_data->key_data_ver == 2) {
1260Sstevel@tonic-gate keysalt->type = key_data->key_data_type[1];
1270Sstevel@tonic-gate if ((keysalt->data.length = key_data->key_data_length[1])) {
1280Sstevel@tonic-gate if (!(keysalt->data.data=(char *)malloc(keysalt->data.length))){
1290Sstevel@tonic-gate if (key_data->key_data_contents[0]) {
1300Sstevel@tonic-gate krb5_xfree(dbkey->contents);
1310Sstevel@tonic-gate dbkey->contents = 0;
1320Sstevel@tonic-gate dbkey->length = 0;
1330Sstevel@tonic-gate }
1340Sstevel@tonic-gate return ENOMEM;
1350Sstevel@tonic-gate }
1360Sstevel@tonic-gate memcpy(keysalt->data.data, key_data->key_data_contents[1],
1370Sstevel@tonic-gate (size_t) keysalt->data.length);
1380Sstevel@tonic-gate } else
1390Sstevel@tonic-gate keysalt->data.data = (char *) NULL;
1400Sstevel@tonic-gate } else {
1410Sstevel@tonic-gate keysalt->type = KRB5_KDB_SALTTYPE_NORMAL;
1420Sstevel@tonic-gate keysalt->data.data = (char *) NULL;
1430Sstevel@tonic-gate keysalt->data.length = 0;
1440Sstevel@tonic-gate }
1450Sstevel@tonic-gate }
1460Sstevel@tonic-gate
1470Sstevel@tonic-gate return retval;
1480Sstevel@tonic-gate }
149