xref: /illumos-gate/usr/src/lib/krb5/kdb/decrypt_key.c (revision 7c478bd9)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
3*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
4*7c478bd9Sstevel@tonic-gate  */
5*7c478bd9Sstevel@tonic-gate 
6*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
7*7c478bd9Sstevel@tonic-gate 
8*7c478bd9Sstevel@tonic-gate /*
9*7c478bd9Sstevel@tonic-gate  * lib/kdb/decrypt_key.c
10*7c478bd9Sstevel@tonic-gate  *
11*7c478bd9Sstevel@tonic-gate  * Copyright 1990,1991 by the Massachusetts Institute of Technology.
12*7c478bd9Sstevel@tonic-gate  * All Rights Reserved.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * Export of this software from the United States of America may
15*7c478bd9Sstevel@tonic-gate  *   require a specific license from the United States Government.
16*7c478bd9Sstevel@tonic-gate  *   It is the responsibility of any person or organization contemplating
17*7c478bd9Sstevel@tonic-gate  *   export to obtain such a license before exporting.
18*7c478bd9Sstevel@tonic-gate  *
19*7c478bd9Sstevel@tonic-gate  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
20*7c478bd9Sstevel@tonic-gate  * distribute this software and its documentation for any purpose and
21*7c478bd9Sstevel@tonic-gate  * without fee is hereby granted, provided that the above copyright
22*7c478bd9Sstevel@tonic-gate  * notice appear in all copies and that both that copyright notice and
23*7c478bd9Sstevel@tonic-gate  * this permission notice appear in supporting documentation, and that
24*7c478bd9Sstevel@tonic-gate  * the name of M.I.T. not be used in advertising or publicity pertaining
25*7c478bd9Sstevel@tonic-gate  * to distribution of the software without specific, written prior
26*7c478bd9Sstevel@tonic-gate  * permission.  Furthermore if you modify this software you must label
27*7c478bd9Sstevel@tonic-gate  * your software as modified software and not distribute it in such a
28*7c478bd9Sstevel@tonic-gate  * fashion that it might be confused with the original M.I.T. software.
29*7c478bd9Sstevel@tonic-gate  * M.I.T. makes no representations about the suitability of
30*7c478bd9Sstevel@tonic-gate  * this software for any purpose.  It is provided "as is" without express
31*7c478bd9Sstevel@tonic-gate  * or implied warranty.
32*7c478bd9Sstevel@tonic-gate  *
33*7c478bd9Sstevel@tonic-gate  *
34*7c478bd9Sstevel@tonic-gate  * krb5_kdb_encrypt_key(), krb5_kdb_decrypt_key functions
35*7c478bd9Sstevel@tonic-gate  */
36*7c478bd9Sstevel@tonic-gate 
37*7c478bd9Sstevel@tonic-gate /*
38*7c478bd9Sstevel@tonic-gate  * Copyright (C) 1998 by the FundsXpress, INC.
39*7c478bd9Sstevel@tonic-gate  *
40*7c478bd9Sstevel@tonic-gate  * All rights reserved.
41*7c478bd9Sstevel@tonic-gate  *
42*7c478bd9Sstevel@tonic-gate  * Export of this software from the United States of America may require
43*7c478bd9Sstevel@tonic-gate  * a specific license from the United States Government.  It is the
44*7c478bd9Sstevel@tonic-gate  * responsibility of any person or organization contemplating export to
45*7c478bd9Sstevel@tonic-gate  * obtain such a license before exporting.
46*7c478bd9Sstevel@tonic-gate  *
47*7c478bd9Sstevel@tonic-gate  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
48*7c478bd9Sstevel@tonic-gate  * distribute this software and its documentation for any purpose and
49*7c478bd9Sstevel@tonic-gate  * without fee is hereby granted, provided that the above copyright
50*7c478bd9Sstevel@tonic-gate  * notice appear in all copies and that both that copyright notice and
51*7c478bd9Sstevel@tonic-gate  * this permission notice appear in supporting documentation, and that
52*7c478bd9Sstevel@tonic-gate  * the name of FundsXpress. not be used in advertising or publicity pertaining
53*7c478bd9Sstevel@tonic-gate  * to distribution of the software without specific, written prior
54*7c478bd9Sstevel@tonic-gate  * permission.  FundsXpress makes no representations about the suitability of
55*7c478bd9Sstevel@tonic-gate  * this software for any purpose.  It is provided "as is" without express
56*7c478bd9Sstevel@tonic-gate  * or implied warranty.
57*7c478bd9Sstevel@tonic-gate  *
58*7c478bd9Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
59*7c478bd9Sstevel@tonic-gate  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
60*7c478bd9Sstevel@tonic-gate  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
61*7c478bd9Sstevel@tonic-gate  */
62*7c478bd9Sstevel@tonic-gate 
63*7c478bd9Sstevel@tonic-gate #include "k5-int.h"
64*7c478bd9Sstevel@tonic-gate 
65*7c478bd9Sstevel@tonic-gate /*
66*7c478bd9Sstevel@tonic-gate  * Decrypt a key from storage in the database.  "eblock" is used
67*7c478bd9Sstevel@tonic-gate  * to decrypt the key in "in" into "out"; the storage pointed to by "out"
68*7c478bd9Sstevel@tonic-gate  * is allocated before use.
69*7c478bd9Sstevel@tonic-gate  */
70*7c478bd9Sstevel@tonic-gate 
71*7c478bd9Sstevel@tonic-gate krb5_error_code
72*7c478bd9Sstevel@tonic-gate krb5_dbekd_decrypt_key_data(context, mkey, key_data, dbkey, keysalt)
73*7c478bd9Sstevel@tonic-gate     krb5_context 	  context;
74*7c478bd9Sstevel@tonic-gate     const krb5_keyblock	* mkey;
75*7c478bd9Sstevel@tonic-gate     const krb5_key_data	* key_data;
76*7c478bd9Sstevel@tonic-gate     krb5_keyblock 	* dbkey;
77*7c478bd9Sstevel@tonic-gate     krb5_keysalt 	* keysalt;
78*7c478bd9Sstevel@tonic-gate {
79*7c478bd9Sstevel@tonic-gate     krb5_error_code 	  retval = 0;
80*7c478bd9Sstevel@tonic-gate     krb5_int16		  tmplen;
81*7c478bd9Sstevel@tonic-gate     krb5_octet		* ptr;
82*7c478bd9Sstevel@tonic-gate     krb5_enc_data	  cipher;
83*7c478bd9Sstevel@tonic-gate     krb5_data		  plain;
84*7c478bd9Sstevel@tonic-gate 
85*7c478bd9Sstevel@tonic-gate     ptr = key_data->key_data_contents[0];
86*7c478bd9Sstevel@tonic-gate 
87*7c478bd9Sstevel@tonic-gate     if (ptr) {
88*7c478bd9Sstevel@tonic-gate 	krb5_kdb_decode_int16(ptr, tmplen);
89*7c478bd9Sstevel@tonic-gate 	ptr += 2;
90*7c478bd9Sstevel@tonic-gate 
91*7c478bd9Sstevel@tonic-gate 	cipher.enctype = ENCTYPE_UNKNOWN;
92*7c478bd9Sstevel@tonic-gate 	cipher.ciphertext.length = key_data->key_data_length[0]-2;
93*7c478bd9Sstevel@tonic-gate 	cipher.ciphertext.data = (char *)ptr;  /* SUNWresync121 XXX */
94*7c478bd9Sstevel@tonic-gate 	plain.length = key_data->key_data_length[0]-2;
95*7c478bd9Sstevel@tonic-gate 	if ((plain.data = (char *) malloc(plain.length)) == NULL)
96*7c478bd9Sstevel@tonic-gate 	    return(ENOMEM);
97*7c478bd9Sstevel@tonic-gate 	(void) memset(plain.data, 0, plain.length);
98*7c478bd9Sstevel@tonic-gate 
99*7c478bd9Sstevel@tonic-gate 	if ((retval = krb5_c_decrypt(context, mkey, 0 /* XXX */, 0,
100*7c478bd9Sstevel@tonic-gate 				     &cipher, &plain))) {
101*7c478bd9Sstevel@tonic-gate 	    krb5_xfree(plain.data);
102*7c478bd9Sstevel@tonic-gate 	    return retval;
103*7c478bd9Sstevel@tonic-gate 	}
104*7c478bd9Sstevel@tonic-gate 
105*7c478bd9Sstevel@tonic-gate 	/* tmplen is the true length of the key.  plain.data is the
106*7c478bd9Sstevel@tonic-gate 	   plaintext data length, but it may be padded, since the
107*7c478bd9Sstevel@tonic-gate 	   old-style etypes didn't store the real length.  I can check
108*7c478bd9Sstevel@tonic-gate 	   to make sure that there are enough bytes, but I can't do
109*7c478bd9Sstevel@tonic-gate 	   any better than that. */
110*7c478bd9Sstevel@tonic-gate 
111*7c478bd9Sstevel@tonic-gate 	if (tmplen > plain.length) {
112*7c478bd9Sstevel@tonic-gate 	    krb5_xfree(plain.data);
113*7c478bd9Sstevel@tonic-gate 	    return(KRB5_CRYPTO_INTERNAL);
114*7c478bd9Sstevel@tonic-gate 	}
115*7c478bd9Sstevel@tonic-gate 
116*7c478bd9Sstevel@tonic-gate 	dbkey->magic = KV5M_KEYBLOCK;
117*7c478bd9Sstevel@tonic-gate 	dbkey->enctype = key_data->key_data_type[0];
118*7c478bd9Sstevel@tonic-gate 	dbkey->length = tmplen;
119*7c478bd9Sstevel@tonic-gate 	dbkey->contents = (unsigned char *) plain.data;  /* SUNWresync121 XXX */
120*7c478bd9Sstevel@tonic-gate 	dbkey->dk_list = NULL;
121*7c478bd9Sstevel@tonic-gate 	dbkey->hKey = CK_INVALID_HANDLE;
122*7c478bd9Sstevel@tonic-gate     }
123*7c478bd9Sstevel@tonic-gate 
124*7c478bd9Sstevel@tonic-gate     /* Decode salt data */
125*7c478bd9Sstevel@tonic-gate     if (keysalt) {
126*7c478bd9Sstevel@tonic-gate 	if (key_data->key_data_ver == 2) {
127*7c478bd9Sstevel@tonic-gate 	    keysalt->type = key_data->key_data_type[1];
128*7c478bd9Sstevel@tonic-gate 	    if ((keysalt->data.length = key_data->key_data_length[1])) {
129*7c478bd9Sstevel@tonic-gate 		if (!(keysalt->data.data=(char *)malloc(keysalt->data.length))){
130*7c478bd9Sstevel@tonic-gate 		    if (key_data->key_data_contents[0]) {
131*7c478bd9Sstevel@tonic-gate 			krb5_xfree(dbkey->contents);
132*7c478bd9Sstevel@tonic-gate 			dbkey->contents = 0;
133*7c478bd9Sstevel@tonic-gate 			dbkey->length = 0;
134*7c478bd9Sstevel@tonic-gate 		    }
135*7c478bd9Sstevel@tonic-gate 		    return ENOMEM;
136*7c478bd9Sstevel@tonic-gate 		}
137*7c478bd9Sstevel@tonic-gate 		memcpy(keysalt->data.data, key_data->key_data_contents[1],
138*7c478bd9Sstevel@tonic-gate 		       (size_t) keysalt->data.length);
139*7c478bd9Sstevel@tonic-gate 	    } else
140*7c478bd9Sstevel@tonic-gate 		keysalt->data.data = (char *) NULL;
141*7c478bd9Sstevel@tonic-gate 	} else {
142*7c478bd9Sstevel@tonic-gate 	    keysalt->type = KRB5_KDB_SALTTYPE_NORMAL;
143*7c478bd9Sstevel@tonic-gate 	    keysalt->data.data = (char *) NULL;
144*7c478bd9Sstevel@tonic-gate 	    keysalt->data.length = 0;
145*7c478bd9Sstevel@tonic-gate 	}
146*7c478bd9Sstevel@tonic-gate     }
147*7c478bd9Sstevel@tonic-gate 
148*7c478bd9Sstevel@tonic-gate     return retval;
149*7c478bd9Sstevel@tonic-gate }
150