xref: /freebsd/crypto/heimdal/lib/krb5/mk_priv.c (revision 9207b4cff7b8d483f4dd3c62266c2b58819eb7f9)
1 /*
2  * Copyright (c) 1997 - 2001 Kungliga Tekniska H�gskolan
3  * (Royal Institute of Technology, Stockholm, Sweden).
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * 3. Neither the name of the Institute nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #include <krb5_locl.h>
35 
36 RCSID("$Id: mk_priv.c,v 1.29 2001/05/14 06:14:49 assar Exp $");
37 
38 /*
39  *
40  */
41 
42 krb5_error_code
43 krb5_mk_priv(krb5_context context,
44 	     krb5_auth_context auth_context,
45 	     const krb5_data *userdata,
46 	     krb5_data *outbuf,
47 	     /*krb5_replay_data*/ void *outdata)
48 {
49   krb5_error_code ret;
50   KRB_PRIV s;
51   EncKrbPrivPart part;
52   u_char *buf;
53   size_t buf_size;
54   size_t len;
55   u_int32_t tmp_seq;
56   krb5_keyblock *key;
57   int32_t sec, usec;
58   KerberosTime sec2;
59   int usec2;
60   krb5_crypto crypto;
61 
62   /* XXX - Is this right? */
63 
64   if (auth_context->local_subkey)
65       key = auth_context->local_subkey;
66   else if (auth_context->remote_subkey)
67       key = auth_context->remote_subkey;
68   else
69       key = auth_context->keyblock;
70 
71   krb5_us_timeofday (context, &sec, &usec);
72 
73   part.user_data = *userdata;
74   sec2           = sec;
75   part.timestamp = &sec2;
76   usec2          = usec;
77   part.usec      = &usec2;
78   if (auth_context->flags & KRB5_AUTH_CONTEXT_DO_SEQUENCE) {
79     tmp_seq = auth_context->local_seqnumber;
80     part.seq_number = &tmp_seq;
81   } else {
82     part.seq_number = NULL;
83   }
84 
85   part.s_address = auth_context->local_address;
86   part.r_address = auth_context->remote_address;
87 
88   buf_size = 1024;
89   buf = malloc (buf_size);
90   if (buf == NULL) {
91       krb5_set_error_string (context, "malloc: out of memory");
92       return ENOMEM;
93   }
94 
95   krb5_data_zero (&s.enc_part.cipher);
96 
97   do {
98       ret = encode_EncKrbPrivPart (buf + buf_size - 1, buf_size,
99 				   &part, &len);
100       if (ret) {
101 	  if (ret == ASN1_OVERFLOW) {
102 	      u_char *tmp;
103 
104 	      buf_size *= 2;
105 	      tmp = realloc (buf, buf_size);
106 	      if (tmp == NULL) {
107 		  krb5_set_error_string (context, "malloc: out of memory");
108 		  ret = ENOMEM;
109 		  goto fail;
110 	      }
111 	      buf = tmp;
112 	  } else {
113 	      goto fail;
114 	  }
115       }
116   } while(ret == ASN1_OVERFLOW);
117 
118   s.pvno = 5;
119   s.msg_type = krb_priv;
120   s.enc_part.etype = key->keytype;
121   s.enc_part.kvno = NULL;
122 
123   ret = krb5_crypto_init(context, key, 0, &crypto);
124   if (ret) {
125       free (buf);
126       return ret;
127   }
128   ret = krb5_encrypt (context,
129 		      crypto,
130 		      KRB5_KU_KRB_PRIV,
131 		      buf + buf_size - len,
132 		      len,
133 		      &s.enc_part.cipher);
134   krb5_crypto_destroy(context, crypto);
135   if (ret) {
136       free(buf);
137       return ret;
138   }
139 
140   do {
141       ret = encode_KRB_PRIV (buf + buf_size - 1, buf_size, &s, &len);
142 
143       if (ret){
144 	  if (ret == ASN1_OVERFLOW) {
145 	      u_char *tmp;
146 
147 	      buf_size *= 2;
148 	      tmp = realloc (buf, buf_size);
149 	      if (tmp == NULL) {
150 		  krb5_set_error_string (context, "malloc: out of memory");
151 		  ret = ENOMEM;
152 		  goto fail;
153 	      }
154 	      buf = tmp;
155 	  } else {
156 	      goto fail;
157 	  }
158       }
159   } while(ret == ASN1_OVERFLOW);
160   krb5_data_free (&s.enc_part.cipher);
161 
162   outbuf->length = len;
163   outbuf->data   = malloc (len);
164   if (outbuf->data == NULL) {
165       krb5_set_error_string (context, "malloc: out of memory");
166       free(buf);
167       return ENOMEM;
168   }
169   memcpy (outbuf->data, buf + buf_size - len, len);
170   free (buf);
171   if (auth_context->flags & KRB5_AUTH_CONTEXT_DO_SEQUENCE)
172       auth_context->local_seqnumber =
173 	  (auth_context->local_seqnumber + 1) & 0xFFFFFFFF;
174   return 0;
175 
176 fail:
177   free (buf);
178   krb5_data_free (&s.enc_part.cipher);
179   return ret;
180 }
181