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