summaryrefslogtreecommitdiffstats
path: root/src/lib/krb5/krb/mk_priv.c
blob: 30ed10ddb4d97d89f9d56d46a2199d4331997f00 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*
 * $Source$
 * $Author$
 *
 * Copyright 1990,1991 by the Massachusetts Institute of Technology.
 * All Rights Reserved.
 *
 * For copying and distribution information, please see the file
 * <krb5/copyright.h>.
 *
 * krb5_mk_priv()
 */

#if !defined(lint) && !defined(SABER)
static char rcsid_mk_priv_c[] =
"$Id$";
#endif	/* !lint & !SABER */

#include <krb5/krb5.h>
#include <krb5/asn1.h>
#include <krb5/los-proto.h>
#include <krb5/ext-proto.h>

/*
 Formats a KRB_PRIV message into outbuf.

 userdata is formatted as the user data in the message.
 etype specifies the encryption type; key specifies the key for the
 encryption; sender_addr and recv_addr specify the full addresses (host
 and port) of the sender and receiver.

 i_vector is used as an initialization vector for the encryption, and if
 non-NULL its contents are replaced with the last block of the encrypted
 data upon exit.

 The outbuf buffer storage is allocated, and should be freed by the
 caller when finished.

 returns system errors
*/
krb5_error_code
krb5_mk_priv(DECLARG(const krb5_data *, userdata),
	     DECLARG(const krb5_enctype, etype),
	     DECLARG(const krb5_keyblock *, key),
	     DECLARG(const krb5_address *, sender_addr),
	     DECLARG(const krb5_address *, recv_addr),
	     DECLARG(krb5_int32, seq_number),
	     DECLARG(krb5_int32, priv_flags),
	     DECLARG(krb5_rcache, rcache),
	     DECLARG(krb5_pointer, i_vector),
	     DECLARG(krb5_data *, outbuf))
OLDDECLARG(const krb5_data *, userdata)
OLDDECLARG(const krb5_enctype, etype)
OLDDECLARG(const krb5_keyblock *, key)
OLDDECLARG(const krb5_address *, sender_addr)
OLDDECLARG(const krb5_address *, recv_addr)
OLDDECLARG(krb5_int32, seq_number)
OLDDECLARG(krb5_int32, priv_flags)
OLDDECLARG(krb5_rcache, rcache)
OLDDECLARG(krb5_pointer, i_vector)
OLDDECLARG(krb5_data *, outbuf)
{
    krb5_error_code retval;
    krb5_encrypt_block eblock;
    krb5_priv privmsg;
    krb5_priv_enc_part privmsg_enc_part;
    krb5_data *scratch;

    if (!valid_etype(etype))
	return KRB5_PROG_ETYPE_NOSUPP;
    privmsg.enc_part.etype = etype; 
    privmsg.enc_part.kvno = 0;	/* XXX allow user-set? */

    privmsg_enc_part.user_data = *userdata;
    privmsg_enc_part.s_address = (krb5_address *)sender_addr;
    if (recv_addr)
	privmsg_enc_part.r_address = (krb5_address *)recv_addr;
    else
	privmsg_enc_part.r_address = 0;

    if (!(priv_flags & KRB5_PRIV_NOTIME)) {
	if (!rcache)
	    /* gotta provide an rcache in this case... */
	    return KRB5_RC_REQUIRED;
	if (retval = krb5_us_timeofday(&privmsg_enc_part.timestamp,
				       &privmsg_enc_part.usec))
	    return retval;
    }
    if (priv_flags & KRB5_PRIV_DOSEQUENCE) {
	privmsg_enc_part.seq_number = seq_number;
    } else
	privmsg_enc_part.seq_number = 0;

    /* start by encoding to-be-encrypted part of the message */

    if (retval = encode_krb5_enc_priv_part(&privmsg_enc_part, &scratch))
	return retval;

#define cleanup_scratch() { (void) memset(scratch->data, 0, scratch->length); krb5_free_data(scratch); }

    /* put together an eblock for this encryption */

    krb5_use_cstype(&eblock, etype);
    privmsg.enc_part.ciphertext.length = krb5_encrypt_size(scratch->length,
						eblock.crypto_entry);
    /* add padding area, and zero it */
    if (!(scratch->data = realloc(scratch->data,
				  privmsg.enc_part.ciphertext.length))) {
	/* may destroy scratch->data */
	xfree(scratch);
	return ENOMEM;
    }
    memset(scratch->data + scratch->length, 0,
	  privmsg.enc_part.ciphertext.length - scratch->length);
    if (!(privmsg.enc_part.ciphertext.data =
	  malloc(privmsg.enc_part.ciphertext.length))) {
        retval = ENOMEM;
        goto clean_scratch;
    }

#define cleanup_encpart() {\
	(void) memset(privmsg.enc_part.ciphertext.data, 0, \
	     privmsg.enc_part.ciphertext.length); \
	free(privmsg.enc_part.ciphertext.data); \
	privmsg.enc_part.ciphertext.length = 0; \
	privmsg.enc_part.ciphertext.data = 0;}

    /* do any necessary key pre-processing */
    if (retval = krb5_process_key(&eblock, key)) {
        goto clean_encpart;
    }

#define cleanup_prockey() {(void) krb5_finish_key(&eblock);}

    /* call the encryption routine */
    if (retval = krb5_encrypt((krb5_pointer) scratch->data,
			      (krb5_pointer) privmsg.enc_part.ciphertext.data,
			      scratch->length, &eblock,
			      i_vector)) {
        goto clean_prockey;
    }


    /* put last block into the i_vector */
    if (i_vector)
	memcpy(i_vector,
	       privmsg.enc_part.ciphertext.data +
	       (privmsg.enc_part.ciphertext.length -
	        eblock.crypto_entry->block_length),
	       eblock.crypto_entry->block_length);
	   
    /* private message is now assembled-- do some cleanup */
    cleanup_scratch();

    if (retval = krb5_finish_key(&eblock)) {
        cleanup_encpart();
        return retval;
    }
    /* encode private message */
    if (retval = encode_krb5_priv(&privmsg, &scratch))  {
        cleanup_encpart();
	return retval;
    }

    cleanup_encpart();
    if (!(priv_flags & KRB5_PRIV_NOTIME)) {
	krb5_donot_replay replay;

	if (retval = krb5_gen_replay_name(sender_addr, "_priv",
					  &replay.client)) {
	    cleanup_scratch();
	    return retval;
	}

	replay.server = "";		/* XXX */
	replay.cusec = privmsg_enc_part.usec;
	replay.ctime = privmsg_enc_part.timestamp;
	if (retval = krb5_rc_store(rcache, &replay)) {
	    /* should we really error out here? XXX */
	    cleanup_scratch();
	    xfree(replay.client);
	    return retval;
	}
	xfree(replay.client);
    }
    *outbuf = *scratch;
    xfree(scratch);
    return 0;

 clean_prockey:
    cleanup_prockey();
 clean_encpart:
    cleanup_encpart();
 clean_scratch:
    cleanup_scratch();

    return retval;
}